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); }
/* * X * p2 = p1, p1/p2 = X, Error */ Matrix* calc_div(Matrix* p1, Matrix* p2) { Matrix *oldAns = ans; if (p1 == NULL || p2 == NULL) { //Error return NULL; } ans = NULL; if (!calc_mul(p1, calc_inverse(p2))) { //Error return NULL; } stor_freeMatrix(oldAns); return ans; }
/* * Error */ Matrix* calc_ex(Matrix* p,long ex) { Matrix *oldAns = ans; Matrix *temp = NULL, *temp2 = NULL; int n; if (p == NULL) { //Error return NULL; } if (p->m != p->n) { //Error return NULL; } n = p->m; if (ex < 0) { ans = NULL; if (!(temp = calc_inverse(p))) { //Error return NULL; } ans = NULL; ex = -ex; } else { if (!stor_createMatrix(&temp, n, n)) { //Error return NULL; } if (!stor_assign(temp, p)) { //Error return NULL; } ans = NULL; } if (!calc_eye(n)) { //Error return NULL; } while (ex) { if (ex % 2) { if (!calc_mul(temp, ans)) { //Error return NULL; } } temp2 = ans; //protect ans ans = NULL; //protect ans if (!calc_mul(temp, temp)) { //Error return NULL; } stor_freeMatrix(temp); temp = ans; ans = temp2; //protect ans ex /= 2; } stor_freeMatrix(temp); stor_freeMatrix(oldAns); return ans; }