static double *calc_inverse_improved( const double *src, const int size) { double *inverse = calc_inverse( src, size); if( inverse) { double *err_mat = (double *)calloc( 2 * size * size, sizeof( double)); double *b_times_delta = err_mat + size * size; int i; #ifdef LSQUARE_ERROR FILE *ofile = fopen( "lsquare.dat", "ab"); #endif mult_matrices( err_mat, src, size, size, inverse, size); for( i = 0; i < size; i++) err_mat[i * (size + 1)] -= 1.; #ifdef LSQUARE_ERROR fprintf( ofile, "%d-square matrix delta (= AB - I):\n", size); dump_matrix( ofile, err_mat, size); fclose( ofile); #endif mult_matrices( b_times_delta, inverse, size, size, err_mat, size); for( i = 0; i < size * size; i++) inverse[i] -= b_times_delta[i]; free( err_mat); } return( inverse); }
int main(int argc, char **argv) { if (argc != 2) { printf("Enter a file to be scanned.\n"); exit(0); } int *A, *B, *C; int m, n, p; read_matrices(&A, &B, &C, &m, &n, &p, *(argv+1)); mult_matrices(A, B, C, m, n, p); printf("\nMatrix A contents:\n"); print_matrices(A, m, n); printf("\nMatrix B contents:\n"); print_matrices(B, n, p); printf("\nMatrix A * B is:\n"); print_matrices(C, m, p); free(A); free(B); free(C); exit(0); }
/* {{{ check */ int check(matrix *m) { int i, j; REAL temp; init_empty_matrix(&lower, m->rows, m->cols); init_empty_matrix(&upper, m->rows, m->cols); for (i=0; i<m->rows; i++) { for (j=0; j<m->cols; j++) { if (i>j) { temp = GET(m,i,j); SET(&lower,i,j,temp); } if (i<=j) { temp = GET(m,i,j); SET(&upper,i,j,temp); } } SET(&lower,i,i,(REAL)1.0L); } mult_matrices(&mult, &lower, &upper); if (dbg) { printf("lower:\n"); print_matrix(&lower); printf("upper:\n"); print_matrix(&upper); printf("multiplied:\n"); print_matrix(&mult); } for (i=0; i<m->rows; i++) { for (j=0; j<m->cols; j++) { if (!EQUAL(GET(&mult,i,j),GET(&ref,i,j))) { printf("(%d,%d): " FMT " != " FMT "\n", i, j, GET(&mult,i,j), GET(&ref,i,j)); return EXIT_FAILURE; } } } return EXIT_SUCCESS; } /* }}} */