Matrix *Matrix_inverse(Matrix *mat){ double det=Matrix_determinant(mat); if(mat->rows != mat->cols || det == 0.0){ return Matrix_create(mat->rows, mat->cols); } Matrix *temp = Matrix_clone(mat); int n = temp->rows; int m = temp->cols; int i,j; double tempdet,val; Matrix *Cofactor = Matrix_create(n, m); for(j = 0; j < m; j++){ for(i = 0; i < n; i++){ Matrix *Mintemp = Matrix_minor(temp,i, j); tempdet = Matrix_determinant(Mintemp); Matrix_set(Cofactor,i,j,tempdet); } } Cofactor = Matrix_transpose(Cofactor); for(i = 0; i < n; i++){ for(j = 0; j < m; j++){ val=Matrix_get(Cofactor,i,j) * pow(-1.0,(i+j)) /det; if(isnan(val)) val=0; Matrix_set(Cofactor,i,j, val); //Cofactor.setValue(i, j, (Cofactor.getValue(i, j) * Math.pow(-1D, i + j + 2)) / det); } } return Cofactor; }
static void testTranspose() { Matrix matrix; matrix = Matrix_transposed(Matrix_init(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f)); assertMatrixExact(matrix, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); matrix = Matrix_transposed(Matrix_init(0.0f, 0.4f, 0.8f, 1.2f, 0.1f, 0.5f, 0.9f, 1.3f, 0.2f, 0.6f, 1.0f, 1.4f, 0.3f, 0.7f, 1.1f, 1.5f)); assertMatrixExact(matrix, 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f); matrix = Matrix_init(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f); Matrix_transpose(&matrix); assertMatrixExact(matrix, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); matrix = Matrix_init(0.0f, 0.4f, 0.8f, 1.2f, 0.1f, 0.5f, 0.9f, 1.3f, 0.2f, 0.6f, 1.0f, 1.4f, 0.3f, 0.7f, 1.1f, 1.5f); Matrix_transpose(&matrix); assertMatrixExact(matrix, 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f); }
/* Paul Fournel */ Matrix *Matrix_create_random_SDP(int rows){ Matrix *mr = Matrix_create_random(rows); Matrix *res = Matrix_product(mr,Matrix_transpose(mr)); /*if(Matrix_determinant(res)>0){ printf("C'est bon det=%f>0\n",Matrix_determinant(res)); }else{ printf("C'est pas bon det=%f>0\n",Matrix_determinant(res)); }*/ /*Matrix *mortho = Matrix_create_random(rows);//Matrix_create_orthonormal(rows); Matrix *mdiag = Matrix_create_random_diag(rows); Matrix *res = Matrix_product(mortho, Matrix_product(mdiag,Matrix_transpose(mortho)));*/ return(res); }
Matrix Matrix_transposed(Matrix matrix) { Matrix_transpose(&matrix); return matrix; }