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; }
void Matrix_postMultiply (Matrix *m, const Matrix *matrix) { float newMatrix[16]; const float *m1 = m->m_matrix, *m2 = matrix->m_matrix; newMatrix[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2]; newMatrix[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2]; newMatrix[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2]; newMatrix[3] = 0; newMatrix[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6]; newMatrix[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6]; newMatrix[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6]; newMatrix[7] = 0; newMatrix[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10]; newMatrix[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10]; newMatrix[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10]; newMatrix[11] = 0; newMatrix[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12]; newMatrix[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13]; newMatrix[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14]; newMatrix[15] = 1; Matrix_set(m, newMatrix); }
void main() { Matrix *mx; Matrix_init(mx,4,3); Matrix_set(mx,3,3,2); Scanline *s; Instruction ist; Instruction_Init(&ist,mx); Scanline_Init(&ist); Instruction_Do(&ist); Instruction_Getname(&ist); Instruction_Destroy(&ist); }
Matrix* Matrix_minor(Matrix *mat, int x, int y){ Matrix *temp = Matrix_clone(mat); Matrix *newmat = Matrix_create(temp->rows - 1, temp->cols - 1); int i,j; int n = temp->rows; int m = temp->cols; int cnt = 0; double *myArr = (double*)malloc((n - 1) * (m - 1) * sizeof(double)); for(i = 0; i < n; i++){ for(j = 0; j < m; j++){ if(i != x && j != y){ myArr[cnt] = Matrix_get(temp,i,j); cnt++; } } } cnt = 0; for(i = 0; i < n - 1; i++){ for(j = 0; j < m - 1; j++){ Matrix_set(newmat,i,j,myArr[cnt]); cnt++; } } return newmat; }
/* Paul Fournel */ Matrix *Matrix_create_sample(){ int rows = 3; Matrix *m=Matrix_create(rows,rows); Matrix_set(m,0,0, 14); Matrix_set(m,0,1, 8); Matrix_set(m,0,2, 17); Matrix_set(m,1,0, 8); Matrix_set(m,1,1, 16); Matrix_set(m,1,2, 8); Matrix_set(m,2,0, 17); Matrix_set(m,2,1, 8); Matrix_set(m,2,2, 29); return(m); }
Matrix *Matrix_transpose(Matrix *m){ Matrix *newmatrix=Matrix_create(m->cols, m->rows); int i,j; for (i=0;i<m->rows;i++){ for (j=0;j<m->cols;j++){ Matrix_set(newmatrix,j,i,Matrix_get(m,i,j)); } } return(newmatrix); }
Matrix *Matrix_product(Matrix *m1, Matrix *m2){ int i,j,y; Matrix *c=Matrix_create(m1->rows,m1->cols); for(i =0;i<m1->rows; i++){ for(j=0;j<m2->cols; j++){ for(y=0;y<m1->cols; y++){ Matrix_set(c,i,j,Matrix_get(c,i,j) + Matrix_get(m1,i,y) * Matrix_get(m2,y,j)); } } } return(c); }
Matrix *Matrix_echelon(Matrix* m){ int cols=m->cols; int rows=m->rows; int i=0,j=0,k=0; Matrix *temp=Matrix_clone(m); double pivot; for (i=0;i<rows;i++){ for(j = i + 1;j<rows;j++){ pivot = -1*Matrix_get(temp,j,i) / Matrix_get(temp,i,i); for(k = 0; k < cols; k++){ Matrix_set(temp,j,k, Matrix_get(temp,i,k) * pivot + Matrix_get(temp,j,k)); } } } return(temp); }
Matrix *Util_get_distance_matrix(Point * PointArr){ Matrix *DM = Matrix_create(4,4); double d0 = NAN; double d01 = Point_get_distance_between(&PointArr[0], &PointArr[1]); double d02 = Point_get_distance_between(&PointArr[0], &PointArr[2]); double d03 = Point_get_distance_between(&PointArr[0], &PointArr[3]); double d12 = Point_get_distance_between(&PointArr[1], &PointArr[2]); double d13 = Point_get_distance_between(&PointArr[1], &PointArr[3]); double d23 = Point_get_distance_between(&PointArr[2], &PointArr[3]); //diagonals Matrix_set(DM, 0, 0, d0); Matrix_set(DM, 1, 1, d0); Matrix_set(DM, 2, 2, d0); Matrix_set(DM, 3, 3, d0); Matrix_set(DM,0,1, d01); Matrix_set(DM,1,0, d01); Matrix_set(DM,0,2, d02); Matrix_set(DM,2,0, d02); Matrix_set(DM,0,3, d03); Matrix_set(DM,3,0, d03); Matrix_set(DM,1,2, d12); Matrix_set(DM,2,1, d12); Matrix_set(DM,1,3, d13); Matrix_set(DM,3,1, d13); Matrix_set(DM,2,3, d23); Matrix_set(DM,3,2, d23); return DM; }