void m_inverse(MAT *feed, MAT *result) { MAT * tmp; tmp = m_get(feed->rows, feed->cols); CoFactor(feed->me, feed->cols, tmp->me); Transpose(tmp->me, tmp->cols); m_scalar_divide(tmp, result, m_det(feed)); m_free(tmp); }
void m_invert(matrix m,matrix &dest) { int i,j; float d; d = m_det(m); if (d == 0.0) { m_zero(dest); return; } for (i = 0;i<=3;i++) for (j = 0;j<=3;j++) dest[i][j] = m_signedsubdet(m, i, j); m_trans(dest); m_mults(dest, 1/d, dest); }
void matlib_main() { MATRIX *A, *B, *C; MATRIX *At, *Bt; long n, ni, i; double sum; random_1(-1); n = query_long("dimension of arrays", 3L); ni = query_long("number of iterations", 100L); #ifdef VAX_VMS init_stats(); #endif m_alloc(&A, n, n); m_alloc(&At, n, n); m_alloc(&B, n, n); m_alloc(&Bt, n, n); m_alloc(&C, n, n); for (i=sum=0; i<ni; i++) { m_rand(At, -1.0L, 1.0L); m_trans(A, At); m_invert(B, A); m_mult(C, A, B); sum += fabs(m_det(C)-1.0); } m_free(&A); m_free(&At); m_free(&B); m_free(&Bt); m_free(&C); printf("M.A.D.{DET{A.INV(A))}-1} = %e\n", sum/ni); #ifdef VAX_VMS report_stats(stdout, "stats: "); #endif }
float m_det(float M1[50][50],int n) { int D=0; //CHECK FOR SQUARE MATRIC if(n==1) return M1[0][0]; int sign=1; for(int i=0;i<n-ctr;i++) { //Get Cofactor of the matrix m[0][i] get_cofactor(M1,0,i,n); D+= sign * M1[0][i] * m_det(temp,n-1); sign=-sign; } return D; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// THE MAIN ////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// void main() { float *a;//Will contain pointer pointing to the resultant matrix int choice; //////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////START OF MENU/////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// do { choice=menu();//variable recieves option number selected by the user switch(choice) { case 0:choice=0; printf("\n\n\nTHANK YOU\n\n"); break; case 1:compatibility=1; get_input(compatibility);//will input all data into global struct A and B a=m_sum(X,Y);//ADDITION of struct matrix X,struct matrix Y show_output(a);//will print the result in matrix A[][] break; case 2:compatibility=1; get_input(compatibility);//will input all data into global struct A and B a=m_diff(X,Y);//Subraction of struct matrix X,struct matrix Y show_output(a);//will print the result in matrix A[][] break; case 3:compatibility=2; get_input(compatibility);//will input all data into global struct A and B a=m_prod(X,Y);//Product of struct matrix X,struct matrix Y show_output(a);//will print the result in matrix A[][] break; case 4:/* compatibility=2; get_input(compatibility); a=m_div(X,Y); show_output(a); */ break; case 5:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 6:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 7:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 8:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 9: compatibility=3; get_input(compatibility); a=m_reshape(X,Y); show_output(a); break; case 10: compatibility=0; get_input(compatibility); ans=m_det(X.arr,X.row_size*X.colm_size); printf("The result of the determinant is %f",ans); break; } }while(choice!=0); }