/** Inverse matrix */ matrix<double> matrix<double>::inverse() { matrix<double> m1(_matrix); matrix<double> m2(_matrix); int signum; gsl_permutation *p; if (size_j() != size_i()) { std::cout << "\n ** Size mismatch in matrix<double> inverse" << std::endl; exit(EXIT_FAILURE); } if ((p = gsl_permutation_alloc(size_i())) == NULL) { std::cout << "\n ** Error in matrix<double> inverse" << std::endl; exit(EXIT_FAILURE); } if(gsl_linalg_LU_decomp (m1.as_gsl_type_ptr(), p, &signum)) { std::cout << "\n ** Error in matrix<double> inverse" << std::endl; gsl_permutation_free(p); exit(EXIT_FAILURE); } if(gsl_linalg_LU_invert(m1.as_gsl_type_ptr(), p, m2.as_gsl_type_ptr())) { std::cout << "\n ** Error in matrix<double> inverse" << std::endl; gsl_permutation_free(p); exit(EXIT_FAILURE); } gsl_permutation_free(p); return m2; }
Matrix<N, M, Container> Matrix<M, N, Container>::invert() const { Matrix<N, M, Container> result; gsl_matrix* Z1 = gsl_matrix_alloc(M, M); gsl_matrix* Z = gsl_matrix_alloc(M, M); gsl_permutation* perm = gsl_permutation_alloc(M); int k; for (uint i = 0; i < M; i++) for (uint j = 0; j < M; j++) gsl_matrix_set(Z1, i, j, (*this)(i, j)); if (gsl_linalg_LU_decomp(Z1, perm, &k)) THROW_INVALID_ARG("gsl_linalg_LU_decomp failed"); if (gsl_linalg_LU_invert(Z1, perm, Z)) THROW_INVALID_ARG("gsl_linalg_LU_invert failed"); for (uint i = 0; i < M; i++) for (uint j = 0; j < M; j++) result(i, j) = gsl_matrix_get(Z, i, j); gsl_permutation_free(perm); gsl_matrix_free(Z); gsl_matrix_free(Z1); return result; }
CAMLprim value ml_gsl_linalg_LU_invert(value LU, value P, value INV) { GSL_PERMUT_OF_BIGARRAY(P); _DECLARE_MATRIX2(LU, INV); _CONVERT_MATRIX2(LU, INV); gsl_linalg_LU_invert(&m_LU, &perm_P, &m_INV); return Val_unit; }
void matrix_inv(gsl_matrix *input, gsl_matrix *output){ int s; gsl_permutation * p = gsl_permutation_alloc (input->size1); gsl_linalg_LU_decomp (input, p, &s); gsl_linalg_LU_invert (input, p, output); gsl_permutation_free(p); }
void mcmclib_matrix_inverse(gsl_matrix* A) { gsl_permutation* p = gsl_permutation_alloc(A->size1); gsl_matrix* A1 = gsl_matrix_alloc(A->size1, A->size1); int tmp=0; gsl_matrix_memcpy(A1, A); gsl_linalg_LU_decomp(A1, p, &tmp); gsl_linalg_LU_invert(A1, p, A); gsl_matrix_free(A1); gsl_permutation_free(p); }
// Sets inv_A to the inverse of A, and returns the determinant of A. If inv_A is NULL, then // A is inverted in place. If worspaces p and LU are provided, the function does not have to // allocate its own workspaces. double invert_matrix(gsl_matrix* A, gsl_matrix* inv_A, gsl_permutation* p, gsl_matrix* LU) { unsigned int N = A->size1; assert(N == A->size2); // Allocate workspaces if none are provided bool del_p = false; bool del_LU = false; if(p == NULL) { p = gsl_permutation_alloc(N); del_p = true; } if(LU == NULL) { LU = gsl_matrix_alloc(N, N); del_LU = true; } int s; int status = 1; int count = 0; while(status) { if(count > 5) { std::cerr << "! Error inverting matrix." << std::endl; abort(); } // Invert A using LU decomposition gsl_matrix_memcpy(LU, A); if(count != 0) { // If inversion fails the first time, add small constant to diagonal gsl_matrix_add_diagonal(LU, pow10((double)count - 6.)); std::cerr << "Invert matrix: Added 10^" << count - 6 << " to diagonal." << std::endl; } gsl_linalg_LU_decomp(LU, p, &s); if(inv_A == NULL) { status = gsl_linalg_LU_invert(LU, p, A); } else { assert(N == inv_A->size1); assert(N == inv_A->size2); status = gsl_linalg_LU_invert(LU, p, inv_A); } count++; } // Get the determinant of A double det_A = gsl_linalg_LU_det(LU, s); // Free workspaces if none were provided if(del_p) { gsl_permutation_free(p); } if(del_LU) { gsl_matrix_free(LU); } return det_A; }
void xmi_inverse_matrix(double x[3], double y[3], double z[3], double **inverseF) { gsl_matrix *m = gsl_matrix_alloc(3,3); gsl_matrix *inverse = gsl_matrix_alloc(3,3); gsl_permutation *p = gsl_permutation_calloc(3); int signum; double *rv; int i,j,k; gsl_matrix_set(m,0,0, x[0]); gsl_matrix_set(m,1,0, x[1]); gsl_matrix_set(m,2,0, x[2]); gsl_matrix_set(m,0,1, y[0]); gsl_matrix_set(m,1,1, y[1]); gsl_matrix_set(m,2,1, y[2]); gsl_matrix_set(m,0,2, z[0]); gsl_matrix_set(m,1,2, z[1]); gsl_matrix_set(m,2,2, z[2]); #if DEBUG == 2 fprintf(stdout,"input matrix\n"); gsl_matrix_fprintf(stdout,m , "%g"); #endif //invert the sucker gsl_linalg_LU_decomp(m,p,&signum); gsl_linalg_LU_invert(m,p,inverse); #if DEBUG == 2 fprintf(stdout,"inverted matrix\n"); gsl_matrix_fprintf(stdout,inverse , "%g"); #endif gsl_matrix_transpose(inverse); #if DEBUG == 2 fprintf(stdout,"transposed inverted matrix\n"); gsl_matrix_fprintf(stdout,inverse , "%g"); #endif rv = (double *) malloc(9*sizeof(double)); k = 0; for (i = 0 ; i < 3 ; i++) for (j = 0 ; j < 3 ; j++) rv[k++] = gsl_matrix_get(inverse, i,j); gsl_matrix_free(m); gsl_matrix_free(inverse); gsl_permutation_free(p); *inverseF = rv; }
/* void invert(valarray<double>& Mi,const valarray<double>& M){ int Dim = sqrt(double(M.size())); vector<int> indx(Dim); valarray<double> Mt = M; valarray<double> vv(Dim); //// LU decomposition for(int i=0; i<Dim; ++i){ double big = 0.0; for(int j=0; j<Dim; ++j) big = max(big,fabs(Mt[i*Dim+j])); vv[i] = 1.0/big; } for(int j=0; j<Dim; ++j){ for(int i=0; i<j; ++i){ double sum = Mt[i*Dim+j]; for(int k=0; k<i; ++k) sum -= Mt[i*Dim+k]*Mt[k*Dim+j]; Mt[i*Dim+j] = sum; } double big=0.0; int imax; for(int i=j; i<Dim; ++i){ imax = j; double sum = Mt[i*Dim+j]; for(int k=0; k<j; ++k) sum -= Mt[i*Dim+k]*Mt[k*Dim+j]; Mt[i*Dim+j] = sum; if(double dum= vv[i]*fabs(sum) >= big){ big = dum; imax = i; } } if(j!=imax){ for(int k=0; k<Dim; ++k) swap(Mt[imax*Dim+k],Mt[j*Dim+k]); vv[imax] = vv[j]; } indx[j]= imax; if(j !=Dim-1) for(int i=j+1; i<Dim; ++i) Mt[i*Dim+j] /= Mt[j*Dim+j]; } /////// for(int k=0; k<Dim; ++k){ for(int l=0; l<Dim; ++l) CCIO::cout<<" LU["<<k<<","<<l<<"]="<<Mt[k*Dim+l]; CCIO::cout<<"\n"; } //// forward/backward subtractions for(int j=0; j<Dim; ++j){ vector<double> col(Dim,0.0); col[j] = 1.0; int ii = 0; for(int i=0; i<Dim; ++i){ double sum = col[indx[i]]; col[indx[i]] = col[i]; if(ii) for(int k=ii; k<i; ++k) sum -= Mt[i*Dim+k]*col[k]; else if(sum) ii = i; col[i] = sum; } for(int i=Dim-1; i>=0; --i){ double sum = col[i]; for(int k=i+1; k<Dim; ++k) sum -= Mt[i*Dim+k]*col[k]; col[i] = sum/Mt[i*Dim+i]; } for(int i=0; i<Dim; ++i) Mi[i*Dim+j] = col[i]; } } */ void invert(valarray<double>& Mi,const valarray<double>& M){ int Dim = sqrt(double(M.size())); valarray<double> Mt(M); gsl_matrix_view m = gsl_matrix_view_array(&(Mt[0]),Dim,Dim); gsl_matrix_view mi = gsl_matrix_view_array(&(Mi[0]),Dim,Dim); gsl_permutation* p = gsl_permutation_alloc(Dim); int sgn; gsl_linalg_LU_decomp(&m.matrix,p,&sgn); gsl_linalg_LU_invert(&m.matrix,p,&mi.matrix); }
int invertirmatriz (gsl_matrix * m, gsl_matrix * inversa, int n) { int s; gsl_permutation * perm = gsl_permutation_alloc (n); gsl_linalg_LU_decomp (m, perm, &s); gsl_linalg_LU_invert (m, perm, inversa); return 0; }
void mygsl_linalg_invert(const gsl_matrix * A, gsl_matrix * A_inv) { gsl_matrix * tmp = gsl_matrix_alloc(A->size1, A->size2); gsl_matrix_memcpy(tmp, A); gsl_permutation * perm = gsl_permutation_alloc(A->size1); int signum; gsl_linalg_LU_decomp(tmp, perm, &signum); gsl_linalg_LU_invert(tmp, perm, A_inv); gsl_matrix_free(tmp); gsl_permutation_free(perm); }
/* gsl_matrix_inverse function inverts a gsl_matrix */ gsl_matrix *gsl_matrix_inverse(const gsl_matrix *src) { int sign; gsl_matrix *LU = gsl_matrix_alloc(src->size1, src->size2); gsl_matrix *inv = gsl_matrix_calloc(src->size1, src->size2); gsl_permutation *p = gsl_permutation_alloc(src->size1); gsl_matrix_memcpy(LU, src); gsl_linalg_LU_decomp(LU, p, &sign); gsl_linalg_LU_invert(LU, p, inv); gsl_permutation_free(p); gsl_matrix_free(LU); return inv; }
/// Invert this matrix void GSLMatrix::invert() { if (size1() != size2()) { throw std::runtime_error("Matrix inverse: the matrix must be square."); } size_t n = size1(); int s; GSLMatrix LU(*this); gsl_permutation *p = gsl_permutation_alloc(n); gsl_linalg_LU_decomp(LU.gsl(), p, &s); gsl_linalg_LU_invert(LU.gsl(), p, this->gsl()); gsl_permutation_free(p); }
gsl_matrix inv_matrix(gsl_matrix *X, gsl_matrix *inv) { int s; int n = X->size1; int m = X->size2; gsl_matrix *a_copy = gsl_matrix_alloc(m, n); gsl_matrix_memcpy( a_copy, X ); gsl_permutation *P = gsl_permutation_alloc(n); gsl_linalg_LU_decomp(a_copy, P, &s); gsl_linalg_LU_invert(a_copy, P, inv); return(*inv); }
double ldmvnorm(const int n, const gsl_vector *x, const gsl_vector *mean, const gsl_matrix *var){ /* log-multivariate normal density function */ /* * n dimension of the random vetor * mean vector of means of size n * var variance matrix of dimension n x n */ int s; double ax,ay; gsl_vector *ym, *xm; gsl_matrix *work = gsl_matrix_alloc(n,n), *winv = gsl_matrix_alloc(n,n); #ifdef PRINTSTIFF /* Print Stiffness indicator S=max(eigen)/min(eigen)*/ gsl_vector *eval = gsl_vector_alloc (n); gsl_matrix *evec = gsl_matrix_alloc (n, n); gsl_matrix_memcpy(work,var); gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc(n); gsl_eigen_symmv (work, eval, evec, w); gsl_eigen_symmv_free (w); gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC); printf("%f ", gsl_vector_get(eval,n-1) / gsl_vector_get(eval,0)); gsl_vector_free(eval); gsl_matrix_free(evec); #endif gsl_permutation *p = gsl_permutation_alloc(n); gsl_matrix_memcpy( work, var ); gsl_linalg_LU_decomp( work, p, &s ); gsl_linalg_LU_invert( work, p, winv ); ax = gsl_linalg_LU_det( work, s ); gsl_matrix_free( work ); gsl_permutation_free( p ); xm = gsl_vector_alloc(n); gsl_vector_memcpy( xm, x); gsl_vector_sub( xm, mean ); ym = gsl_vector_alloc(n); gsl_blas_dsymv(CblasUpper,1.0,winv,xm,0.0,ym); gsl_matrix_free( winv ); gsl_blas_ddot( xm, ym, &ay); gsl_vector_free(xm); gsl_vector_free(ym); /* * ay = exp(-0.5*ay)/sqrt( pow((2*M_PI),n)*ax ); */ ay = -0.5*( ay + n*log(2*M_PI) + log(ax) ); return ay; }
void updateCovariance_particle_arr(particle_arr *part) { int d = part->d; int p = part->p; gsl_matrix *cov = part->cov; double *meanArr = part->mean->array; double w_tot = 0.0; double w_sq_tot = 0.0; double mean_i, mean_j, sum; particle_t *pa; int k; for (k=0; k<p; k++) { w_tot += part->array[k]->weight; w_sq_tot += pow(part->array[k]->weight, 2); } int i, j; for (j=0; j<d; j++) { for (i=0; i<d; i++) { sum = 0.0; if (i < j) { gsl_matrix_set(cov, i, j, gsl_matrix_get(cov, j, i)); continue; } mean_i = meanArr[i]; mean_j = meanArr[j]; for (k=0; k<p; k++) { pa = part->array[k]; sum += pa->weight * (pa->param[i] - mean_i) * (pa->param[j] - mean_j); } sum *= w_tot / (pow(w_tot, 2) - w_sq_tot); gsl_matrix_set(cov, i, j, sum); } } //-- Make a copy, because the invertion will destroy cov gsl_matrix_memcpy(part->cov2, cov); //-- Make LU decomposition and invert int s; gsl_linalg_LU_decomp(part->cov2, part->perm, &s); gsl_linalg_LU_invert(part->cov2, part->perm, part->invCov); //-- Debias the C_{ij}^{-1} //-- C^-1_unbiased = (p - d - 2) / (p - 1) * C^-1_biased double factor = (p - d -2) / (double)(p - 1); gsl_matrix_scale(part->invCov, factor); return; }
void ccl_mat_inv (const double *A, int i,int j, double *invA){ // pinv(A)*B = B/A gsl_matrix *A_ = gsl_matrix_alloc(i,j); memcpy(A_->data,A,i*j*sizeof(double)); gsl_matrix * invA_ = gsl_matrix_alloc(i,j); int s; gsl_permutation * p = gsl_permutation_alloc (A_->size1); gsl_linalg_LU_decomp (A_, p, &s); gsl_linalg_LU_invert(A_,p,invA_); memcpy(invA,invA_->data,i*j*sizeof(double)); gsl_permutation_free(p); gsl_matrix_free(invA_); gsl_matrix_free(A_); }
void SeriesSmoother::computeCoeffs() { gsl_matrix * X = gsl_matrix_alloc( m_pointCount, m_coeffCount ); // Shift between the x index and the row number int delta = (m_pointCount-1)/2; for( int i = 0; i < m_pointCount; ++i ) for (int j = 0; j < m_coeffCount; ++j) gsl_matrix_set( X, i, j, pow( i-delta, j ) ); // [X]^T [X] gsl_matrix * XtX = gsl_matrix_alloc( m_coeffCount, m_coeffCount ); gsl_blas_dgemm( CblasTrans, CblasNoTrans, 1, X, X, 0, XtX ); // ([X]^T [X])^-1 gsl_matrix * XtXi = gsl_matrix_alloc( m_coeffCount, m_coeffCount ); int iXtXi; //the calculation needs an int pointer gsl_permutation * pXtX = gsl_permutation_alloc( m_coeffCount ); gsl_linalg_LU_decomp( XtX, pXtX, &iXtXi ); gsl_linalg_LU_invert( XtX, pXtX, XtXi ); // [X] ([X]^t [X])^-1 gsl_matrix * XXtXi = gsl_matrix_alloc( m_pointCount, m_coeffCount ); gsl_blas_dgemm( CblasNoTrans, CblasNoTrans, 1, X, XtXi, 0, XXtXi ); // [coeffs] = [X] ([X]^t [X])^-1 [X]^t gsl_matrix * coeffs = gsl_matrix_alloc( m_pointCount, m_pointCount ); gsl_blas_dgemm( CblasNoTrans, CblasTrans, 1, XXtXi, X, 0, coeffs ); // Move the coeffs matrix to m_coeffs m_coeffs.resize(m_pointCount); for (int i = 0; i < m_coeffs.size(); ++i) { m_coeffs[i].resize(m_pointCount); for (int j = 0; j < m_pointCount; ++j) m_coeffs[i][j] = gsl_matrix_get( coeffs, i, j ); } // Delete the matrices gsl_matrix_free(X); gsl_matrix_free(XtX); gsl_matrix_free(XtXi); gsl_permutation_free(pXtX); gsl_matrix_free(XXtXi); gsl_matrix_free(coeffs); }
int kalman_meas (Kalman * k, const double * z, int M, double dt, KalmanMeasFunc meas_func, KalmanMeasJacobFunc meas_jacob_func, KalmanMeasCovFunc meas_cov_func) { kalman_pred (k, dt); double K[k->N * M]; double PHt[k->N * M]; double H[M * k->N]; double R[M * M]; double I[M * M]; double h[M]; gsl_matrix_view Kv = gsl_matrix_view_array (K, k->N, M); gsl_matrix_view PHtv = gsl_matrix_view_array (PHt, k->N, M); gsl_matrix_view Hv = gsl_matrix_view_array (H, M, k->N); gsl_matrix_view Rv = gsl_matrix_view_array (R, M, M); gsl_matrix_view Iv = gsl_matrix_view_array (I, M, M); gsl_vector_view hv = gsl_vector_view_array (h, M); meas_jacob_func (H, M, k->x, k->N, k->user); /* K = P_*H'*inv(H*P_*H' + R) */ gsl_blas_dgemm (CblasNoTrans, CblasTrans, 1.0, &k->Pv.matrix, &Hv.matrix, 0.0, &PHtv.matrix); meas_cov_func (R, M, k->user); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &Hv.matrix, &PHtv.matrix, 1.0, &Rv.matrix); size_t permv[M]; gsl_permutation perm = { M, permv }; int signum; gsl_linalg_LU_decomp (&Rv.matrix, &perm, &signum); gsl_linalg_LU_invert (&Rv.matrix, &perm, &Iv.matrix); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &PHtv.matrix, &Iv.matrix, 0.0, &Kv.matrix); /* x = x + K*(z - h(x)) */ meas_func (h, M, k->x, k->N, k->user); vector_sub_nd (z, h, M, h); gsl_blas_dgemv (CblasNoTrans, 1.0, &Kv.matrix, &hv.vector, 1.0, &k->xv.vector); /* P = P_ - K*H*P_ */ gsl_blas_dgemm (CblasNoTrans, CblasTrans, -1.0, &Kv.matrix, &PHtv.matrix, 1.0, &k->Pv.matrix); return 0; }
Matrix* Matrix::invMatrix(gsl_matrix* ludecomp, gsl_permutation* p) { //cout << "Old Value : " << gsl_matrix_get(matrix,0,0) << endl; //cout << "New Value : " << gsl_matrix_get(ludecomp,0,0) << endl; Matrix* minv=new Matrix(row,col); ludecomp->size1=row; ludecomp->size2=col; p->size=row; gsl_matrix_memcpy(ludecomp, matrix); int signum=0; gsl_linalg_LU_decomp(ludecomp, p, &signum); gsl_linalg_LU_invert(ludecomp, p, minv->matrix); return minv; }
/* * matrix inversion using blas * */ void matrix_inverse(gsl_matrix* m, gsl_matrix* inverse) { gsl_matrix *lu; gsl_permutation* p; int signum; p = gsl_permutation_alloc(m->size1); lu = gsl_matrix_alloc(m->size1, m->size2); gsl_matrix_memcpy(lu, m); gsl_linalg_LU_decomp(lu, p, &signum); gsl_linalg_LU_invert(lu, p, inverse); gsl_matrix_free(lu); gsl_permutation_free(p); }
double R0(const double theta[numParam], const double r0time, double * eigenvec) { gsl_matrix * Fmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * Vmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * VmatInv = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * ngm = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); createNGM(theta, r0time, Fmat, Vmat); gsl_permutation * p = gsl_permutation_alloc(NG*(DS-1)*RG); int s; gsl_linalg_LU_decomp(Vmat, p, &s); gsl_linalg_LU_invert(Vmat, p, VmatInv); gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, Fmat, VmatInv, 0.0, ngm); gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(NG*(DS-1)*RG); gsl_vector_complex * eval = gsl_vector_complex_alloc(NG*(DS-1)*RG); gsl_matrix_complex * evec = gsl_matrix_complex_alloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_set_error_handler_off(); gsl_eigen_nonsymmv(ngm, eval, evec, w); size_t r0_idx = 0; double r0 = 0.0; for(size_t i = 0; i < NG*(DS-1)*RG; i++){ if(GSL_REAL(gsl_vector_complex_get(eval, i)) > r0){ r0_idx = i; r0 = GSL_REAL(gsl_vector_complex_get(eval, i)); } } if(eigenvec != NULL){ for(size_t i = 0; i < NG*(DS-1)*RG; i++) eigenvec[i] = GSL_REAL(gsl_matrix_complex_get(evec, i, r0_idx)); } gsl_matrix_free(Fmat); gsl_matrix_free(Vmat); gsl_matrix_free(VmatInv); gsl_matrix_free(ngm); gsl_permutation_free(p); gsl_eigen_nonsymmv_free(w); gsl_vector_complex_free(eval); gsl_matrix_complex_free(evec); return r0; }
void Matrix::invert() { int rows = d_table->numRows(); int cols = d_table->numCols(); if (rows != cols) { QMessageBox::critical(0,tr("QtiPlot - Error"), tr("Inversion failed, the matrix is not square!")); return; } QApplication::setOverrideCursor(waitCursor); gsl_matrix *A = gsl_matrix_alloc (rows, cols); int i, j; for (i=0; i<rows; i++) { for (j=0; j<cols; j++) { QString s = d_table->text(i,j); gsl_matrix_set (A, i, j, s.toDouble()); } } int s; gsl_permutation * p = gsl_permutation_alloc (cols); gsl_linalg_LU_decomp (A, p, &s); gsl_matrix *inverse = gsl_matrix_alloc (rows, cols); gsl_linalg_LU_invert (A, p, inverse); gsl_matrix_free (A); gsl_permutation_free (p); for (i=0; i<rows; i++) { for (j=0; j<cols; j++) { double val = gsl_matrix_get (inverse, i, j); d_table->setText(i, j, QString::number(val)); } } gsl_matrix_free (inverse); QApplication::restoreOverrideCursor(); }
void Matrix::invert() { allow_modification_signals = false; int rows = numRows(); int cols = numCols(); if (rows != cols) { QMessageBox::critical(0,tr("QtiPlot - Error"), tr("Inversion failed, the matrix is not square!")); allow_modification_signals = true; return; } QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); gsl_matrix *A = gsl_matrix_alloc(rows, cols); int i, j; for(i=0; i<rows; i++) { for(j=0; j<cols; j++) { QString s = text(i,j); gsl_matrix_set(A, i, j, s.toDouble()); } } gsl_permutation * p = gsl_permutation_alloc(cols); gsl_linalg_LU_decomp(A, p, &i); gsl_matrix *inverse = gsl_matrix_alloc(rows, cols); gsl_linalg_LU_invert(A, p, inverse); gsl_matrix_free(A); gsl_permutation_free(p); for(i=0; i<rows; i++) { for(j=0; j<cols; j++) setCell(i, j, gsl_matrix_get(inverse, i, j)); } gsl_matrix_free(inverse); QApplication::restoreOverrideCursor(); allow_modification_signals = true; emit modifiedWindow(this); }
bool Matrix_Inverse(gsl_matrix *A, gsl_matrix *B){ int s; gsl_matrix *T; gsl_permutation *p; T=gsl_matrix_alloc(B->size1,B->size2); gsl_matrix_memcpy(T, B); p=gsl_permutation_alloc (T->size1); gsl_linalg_LU_decomp (T, p, &s); gsl_linalg_LU_invert (T, p,A); gsl_permutation_free(p); gsl_matrix_free(T); return true; }
val egsl_inverse(val v1){ gsl_matrix*A = egsl_gslm(v1); val v2 = egsl_alloc(A->size1,A->size1); gsl_matrix*invA = egsl_gslm(v2); size_t n = A->size1; gsl_matrix * m = gsl_matrix_alloc(n,n); gsl_matrix_memcpy(m,A); gsl_permutation * perm = gsl_permutation_alloc (n); /* Make LU decomposition of matrix m */ int s; gsl_linalg_LU_decomp (m, perm, &s); /* Invert the matrix m */ gsl_linalg_LU_invert (m, perm, invA); gsl_permutation_free(perm); gsl_matrix_free(m); return v2; }
void update_chi2_t(chi2_t *chichi, hist_t *obsHist, double *dataMat) { //-- data should be n*d matrix int N = chichi->N; int d = chichi->d; gsl_vector *X_model = chichi->X_model; gsl_matrix *cov = chichi->cov; gsl_matrix *cov2 = chichi->cov2; double value; //-- Update mean int i; for (i=0; i<d; i++) { value = gsl_stats_mean(dataMat+i*N, 1, N); gsl_vector_set(X_model, i, value); gsl_vector_set(chichi->X_obs, i, (double)(obsHist->n[i])); } //-- Update covariance int j; for (j=0; j<d; j++) { for (i=0; i<d; i++) { if (i < j) { value = gsl_matrix_get(cov, j, i); gsl_matrix_set(cov, i, j, value); continue; } value = gsl_stats_covariance_m(dataMat+i*N, 1, dataMat+j*N, 1, N, gsl_vector_get(X_model, i), gsl_vector_get(X_model, j)); gsl_matrix_set(cov, i, j, value); } } //-- Make a copy, because the invertion will destroy cov gsl_matrix_memcpy(cov2, cov); //-- Copy cov to cov2 //-- Make LU decomposition and invert int s; gsl_linalg_LU_decomp(cov2, chichi->perm, &s); gsl_linalg_LU_invert(cov2, chichi->perm, chichi->invCov); //-- Debias the C_{ij}^{-1} //-- C^-1_nonbias = (N - d - 2) / (N - 1) * C^-1_bias double factor = (N - d - 2) / (double)(N - 1); gsl_matrix_scale(chichi->invCov, factor); //-- invCov *= factor return; }
Matrix* Matrix::invMatrix() { Matrix* minv=new Matrix(row,col); gsl_matrix* ludecomp=gsl_matrix_alloc(row,col); gsl_matrix_memcpy(ludecomp, matrix); //cout << "Old Value : " << gsl_matrix_get(matrix,0,0) << endl; //cout << "New Value : " << gsl_matrix_get(ludecomp,0,0) << endl; gsl_permutation* p=gsl_permutation_alloc(row); int signum=0; gsl_linalg_LU_decomp(ludecomp, p, &signum); gsl_linalg_LU_invert(ludecomp, p, minv->matrix); gsl_matrix_free(ludecomp); gsl_permutation_free(p); return minv; }
/** * \brief Compute Savitzky-Golay coefficients and store them into #h. * * This function follows GSL conventions in that it writes its result into a matrix allocated by * the caller and returns a non-zero result on error. * * The coefficient matrix is defined as the matrix H mapping a set of input values to the values * of the polynomial of order #polynom_order which minimizes squared deviations from the input * values. It is computed using the formula \$H=V(V^TV)^(-1)V^T\$, where \$V\$ is the Vandermonde * matrix of the point indices. * * For a short description of the mathematical background, see * http://www.statistics4u.info/fundstat_eng/cc_filter_savgol_math.html */ int SmoothFilter::savitzkyGolayCoefficients(int points, int polynom_order, gsl_matrix *h) { int error = 0; // catch GSL error codes // compute Vandermonde matrix gsl_matrix *vandermonde = gsl_matrix_alloc(points, polynom_order+1); for (int i = 0; i < points; ++i) { gsl_matrix_set(vandermonde, i, 0, 1.0); for (int j = 1; j <= polynom_order; ++j) gsl_matrix_set(vandermonde, i, j, gsl_matrix_get(vandermonde,i,j-1) * i); } // compute V^TV gsl_matrix *vtv = gsl_matrix_alloc(polynom_order+1, polynom_order+1); error = gsl_blas_dgemm(CblasTrans, CblasNoTrans, 1.0, vandermonde, vandermonde, 0.0, vtv); if (!error) { // compute (V^TV)^(-1) using LU decomposition gsl_permutation *p = gsl_permutation_alloc(polynom_order+1); int signum; error = gsl_linalg_LU_decomp(vtv, p, &signum); if (!error) { gsl_matrix *vtv_inv = gsl_matrix_alloc(polynom_order+1, polynom_order+1); error = gsl_linalg_LU_invert(vtv, p, vtv_inv); if (!error) { // compute (V^TV)^(-1)V^T gsl_matrix *vtv_inv_vt = gsl_matrix_alloc(polynom_order+1, points); error = gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, vtv_inv, vandermonde, 0.0, vtv_inv_vt); if (!error) { // finally, compute H = V(V^TV)^(-1)V^T error = gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, vandermonde, vtv_inv_vt, 0.0, h); } gsl_matrix_free(vtv_inv_vt); } gsl_matrix_free(vtv_inv); } gsl_permutation_free(p); } gsl_matrix_free(vtv); gsl_matrix_free(vandermonde); return error; }
int matrix_inverse(long double **output,int row,int col,long double **input) { register int i,j; gsl_matrix *m; m=gsl_matrix_calloc (row,col); for (i=0;i<row;i++) { for (j=0;j<col;j++) { gsl_matrix_set(m,i,j,input[i][j]); } } gsl_matrix *inv; gsl_permutation *p; int *signum; inv=gsl_matrix_calloc (row,col); p=gsl_permutation_calloc(row); signum=(int *)malloc( sizeof(int)*row); gsl_linalg_LU_decomp(m,p,signum); gsl_linalg_LU_invert(m,p,inv); for (i=0;i<row;i++) { for (j=0;j<col;j++) { output[i][j]=(long double) gsl_matrix_get(inv,i,j);; } } free(signum); gsl_matrix_free (m); gsl_matrix_free (inv); gsl_permutation_free(p); return OK; }
void CNumMat::SetToInverse(const CNumMat & mat) { assert(mat.NumRows()==mat.NumCols()); const unsigned N=mat.NumRows(); CNumMat LU(mat); gsl_permutation *p=gsl_permutation_alloc(N); int signum; if(gsl_linalg_LU_decomp(LU.m_mat, p, &signum)) throw BPException("gsl_linalg_LU_decomp"); resize(N,N); if(gsl_linalg_LU_invert(LU.m_mat,p,m_mat)) throw BPException("gsl_linalg_LU_invert"); gsl_permutation_free(p); }