From: Soikk <76824648+Soikk@users.noreply.github.com> Date: Wed, 31 Aug 2022 20:11:59 +0000 (+0200) Subject: Minor reworks of cofactor and adjugate X-Git-Url: https://git.xolatile.top/?a=commitdiff_plain;h=bd25a9380d75f0860015ec85046ea52053666f54;p=soikk-matrix.git Minor reworks of cofactor and adjugate --- diff --git a/matrix.c b/matrix.c index 0b9d88f..1f03129 100644 --- a/matrix.c +++ b/matrix.c @@ -176,7 +176,7 @@ long double determinant(matrix *m){ } } -matrix *cofactor(matrix *m){ +void cofactor(matrix *m){ if(!isSquare(m)){ fprintf(stderr, "Matrix is not square (%dx%d)\n", m->rows, m->cols); return NULL; @@ -190,7 +190,8 @@ matrix *cofactor(matrix *m){ r->data[i][j] = !((i+j)%2) ? ds : -ds; } } - return r; + copyMatrix(m, r); + freeMatrix(&r); } matrix *transpose(matrix *m){ @@ -210,11 +211,11 @@ matrix *dotProduct(matrix *a, matrix *b){ return r; } -matrix *adjugate(matrix *m){ - matrix *cm = cofactor(m); - matrix *r = transpose(cm); - freeMatrix(&cm); - return r; +void adjugate(matrix *m){ + cofactor(m); + matrix *t = transpose(cm); + copyMatrix(m, t); + freeMatrix(&t); } void invert(matrix *m){ @@ -223,10 +224,8 @@ void invert(matrix *m){ fprintf(stderr, "Determinant is 0, the matrix is not invertible\n"); return; } - matrix *r = adjugate(m); - multiplyMatrix(r, 1/d); - copyMatrix(m, r); - freeMatrix(&r); + adjugate(m); + multiplyMatrix(m, 1/d); } void raiseMatrix(matrix *m, int n){ diff --git a/matrix.h b/matrix.h index 6dd0d4e..0b2429e 100644 --- a/matrix.h +++ b/matrix.h @@ -45,13 +45,13 @@ matrix *subMatrix(matrix *m, int row, int col); long double determinant(matrix *m); -matrix *cofactor(matrix *m); +void cofactor(matrix *m); matrix *transpose(matrix *m); matrix *dotProduct(matrix *a, matrix *b); -matrix *adjugate(matrix *m); +void adjugate(matrix *m); void invert(matrix *m);