/** Determinant of matrix */ double matrix<double>::determinant(){ matrix<double> m1(_matrix); int signum; gsl_permutation *p; if (size_j() != size_i()) { std::cout << "\n ** Size mismatch in matrix<double> determinant" << std::endl; exit(EXIT_FAILURE); } if ((p = gsl_permutation_alloc(size_i())) == NULL) { std::cout << "\n ** Error in matrix<double> determinant" << std::endl; exit(EXIT_FAILURE); } if(gsl_linalg_LU_decomp (m1.as_gsl_type_ptr(), p, &signum)) { std::cout << "\n ** Error in matrix<double> determinant" << std::endl; gsl_permutation_free(p); exit(EXIT_FAILURE); } gsl_permutation_free(p); return gsl_linalg_LU_det(m1.as_gsl_type_ptr() , signum); }
/** * Adapted from: Multivariate Normal density function and random * number generator Using GSL from Ralph dos Santos Silva * Copyright (C) 2006 * * multivariate normal density function * * @param n dimension of the random vetor * @param mean vector of means of size n * @param var variance matrix of dimension n x n */ double ssm_dmvnorm(const int n, const gsl_vector *x, const gsl_vector *mean, const gsl_matrix *var, double sd_fac) { int s; double ax,ay; gsl_vector *ym, *xm; gsl_matrix *work = gsl_matrix_alloc(n,n), *winv = gsl_matrix_alloc(n,n); gsl_permutation *p = gsl_permutation_alloc(n); gsl_matrix_memcpy( work, var ); //scale var with sd_fac^2 gsl_matrix_scale(work, sd_fac*sd_fac); 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 ); return ay; }
//calculate determinant of gaudin matrix for given Bethe rapidity set double detgaudinnorm (double kEpshiftinv_gelesen[N+6]) { double k[N];//Bethe rapidities double determinante; double c = kEpshiftinv_gelesen[0];//interaction strenght for(int ll=0; ll<N; ll++) k[ll]=kEpshiftinv_gelesen[ll+1]; //allocate and calculate gaudin matrix: gsl_matrix * m = gsl_matrix_alloc (N, N); for (int ii=0; ii<N; ii++) for (int jj=0; jj<N; jj++) gsl_matrix_set (m, ii, jj, matrixelementenorm(k, c, ii, jj)); //calculate determinant via LU decomposition int sign_permutation; gsl_permutation * p = gsl_permutation_alloc (N); gsl_linalg_LU_decomp (m, p, &sign_permutation); determinante = gsl_linalg_LU_det (m, sign_permutation); gsl_permutation_free (p); gsl_matrix_free (m); //end LU decomposition and det calc return determinante; }
double ran_mv_normal_pdf(const gsl_vector *x, const gsl_vector *mu, const gsl_matrix *Sigma) { const int k = x->size; int s; double det, den; gsl_vector *y = gsl_vector_alloc(k); gsl_vector *work_k = gsl_vector_alloc(k); gsl_matrix *work_k_k = gsl_matrix_alloc(k, k); gsl_matrix *Sigmainv = gsl_matrix_alloc(k, k); gsl_permutation *p = gsl_permutation_alloc(k); gsl_vector_memcpy(y, x); gsl_vector_sub(y, mu); gsl_matrix_memcpy(work_k_k, Sigma); gsl_linalg_LU_decomp(work_k_k, p, &s); gsl_linalg_LU_invert(work_k_k, p, Sigmainv); det = gsl_linalg_LU_det(work_k_k, s); gsl_blas_dgemv(CblasNoTrans, 1.0, Sigmainv, y, 0.0, work_k); gsl_blas_ddot(y, work_k, &den); den = exp(-0.5*den) / sqrt(pow((2*M_PI), k)*det); gsl_vector_free(y); gsl_vector_free(work_k); gsl_matrix_free(work_k_k); gsl_matrix_free(Sigmainv); gsl_permutation_free(p); return den; }
/// /// Compute the determinant of a metric \f$ g_{ij} \f$. /// double XLALMetricDeterminant( const gsl_matrix *g_ij ///< [in] Parameter-space metric \f$ g_{ij} \f$ ) { // Check input XLAL_CHECK_REAL8( g_ij != NULL, XLAL_EFAULT ); const size_t n = g_ij->size1; // Allocate memory gsl_matrix *GAMAT_REAL8( LU_decomp, n, n ); gsl_permutation *GAPERM_REAL8( LU_perm, n ); // Compute LU decomposition gsl_matrix_memcpy( LU_decomp, g_ij ); int LU_sign = 0; XLAL_CHECK_REAL8( gsl_linalg_LU_decomp( LU_decomp, LU_perm, &LU_sign ) == 0, XLAL_EFAILED, "'g_ij' cannot be LU-decomposed" ); // Compute determinant const double determinant = gsl_linalg_LU_det( LU_decomp, LU_sign ); // Cleanup GFMAT( LU_decomp ); GFPERM( LU_perm ); return determinant; }
/* This code returns 1 if and only if SC_EIG is an eigenvalue of the partitioned matrix of our current graph. */ unsigned partAMcond(void) { unsigned i,j; unsigned total_edges = 0; gsl_matrix_set_zero(par_adj); for (i = 0; i < N+1; i++) { gsl_matrix_set(par_adj, i, i, -SC_EIG); for (j = i+1; j < N+1; j++) { if (nbrs[i] & (1U << j)) { gsl_matrix_set(par_adj, i, j, 1); gsl_matrix_set(par_adj, j, i, 1); } } unsigned deg = bitCount[ nbrs[i] ]; gsl_matrix_set(par_adj, i, N+1, VAL-deg); gsl_matrix_set(par_adj, N+1, i, (double)(VAL-deg)/(NTOT-N-1)); total_edges += deg; } gsl_matrix_set(par_adj, N+1, N+1, -SC_EIG + (double) 2*(NTOT*VAL/2 + total_edges/2 - (N+1)*VAL)/(NTOT-N-1)); int signum; gsl_linalg_LU_decomp(par_adj, par_perm, &signum); double det = gsl_linalg_LU_det(par_adj, signum); /* The determinant is not zero. Hence SC_EIG is not an eigenvalue of our graph. */ if (fabs(det) > 0.0000001) { return 0; } return 1; }
double dmvt(const unsigned int n, const gsl_vector *x, const gsl_vector *location, const gsl_matrix *scale, const unsigned int dof) { int s; double ax,ay,az=0.5*(dof + n); gsl_vector *ym, *xm; gsl_matrix *work = gsl_matrix_alloc(n,n), *winv = gsl_matrix_alloc(n,n); gsl_permutation *p = gsl_permutation_alloc(n); gsl_matrix_memcpy( work, scale ); 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, location ); 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 = pow((1+ay/dof),-az)*gsl_sf_gamma(az)/(gsl_sf_gamma(0.5*dof)*sqrt( pow((dof*M_PI),double(n))*ax )); return ay; }
static bool matrix_determinant(CMATRIX *m, COMPLEX_VALUE *det) { int sign = 0; int size = WIDTH(m); if (size != HEIGHT(m)) return TRUE; gsl_permutation *p = gsl_permutation_calloc(size); if (COMPLEX(m)) { gsl_matrix_complex *tmp = gsl_matrix_complex_alloc(size, size); gsl_matrix_complex_memcpy(tmp, CMAT(m)); gsl_linalg_complex_LU_decomp(tmp, p, &sign); det->z = gsl_linalg_complex_LU_det(tmp, sign); gsl_matrix_complex_free(tmp); } else { gsl_matrix *tmp = gsl_matrix_alloc(size, size); gsl_matrix_memcpy(tmp, MAT(m)); gsl_linalg_LU_decomp(tmp, p, &sign); det->x = gsl_linalg_LU_det(tmp, sign); det->z.dat[1] = 0; gsl_matrix_free(tmp); } gsl_permutation_free(p); return FALSE; }
// Calculates the covariance matrix Sigma, alongside Sigma^{-1} and det(Sigma) void TStats::get_cov_matrix(gsl_matrix* Sigma, gsl_matrix* invSigma, double* detSigma) const { // Check that the matrices are the correct size assert(Sigma->size1 == N); assert(Sigma->size2 == N); assert(invSigma->size1 == N); assert(invSigma->size2 == N); // Calculate the covariance matrix Sigma double tmp; for(unsigned int i=0; i<N; i++) { for(unsigned int j=i; j<N; j++) { tmp = cov(i,j); gsl_matrix_set(Sigma, i, j, tmp); if(i != j) { gsl_matrix_set(Sigma, j, i, tmp); } } } // Get the inverse of Sigma int s; gsl_permutation* p = gsl_permutation_alloc(N); gsl_matrix* LU = gsl_matrix_alloc(N, N); gsl_matrix_memcpy(LU, Sigma); gsl_linalg_LU_decomp(LU, p, &s); gsl_linalg_LU_invert(LU, p, invSigma); // Get the determinant of sigma *detSigma = gsl_linalg_LU_det(LU, s); // Cleanup gsl_matrix_free(LU); gsl_permutation_free(p); }
int update_circuit(Circuit* c) { gsl_matrix* A; gsl_vector* b; to_matrix(c,&A,&b); if(A==NULL){ return -1; } gsl_permutation * p=gsl_permutation_alloc(b->size); gsl_vector* x =gsl_vector_alloc(b->size); if(p==NULL||x==NULL){ printf("unable to allocate memory (update_circuit()), freeing and halting\n"); gsl_matrix_free(A); gsl_vector_free(b); if(p!=NULL){ gsl_permutation_free(p); } if(x!=NULL){ gsl_vector_free(x); } return -1; } int i,j; for(i=0;i<A->size1;i++){ printf("[ "); for(j=0;j<A->size2;j++) { printf("%6.3g ",gsl_matrix_get(A,i,j)); } printf("] [ %6.3g ]\n",gsl_vector_get(b,i)); } int s; gsl_linalg_LU_decomp(A,p,&s); double det=gsl_linalg_LU_det(A,s); printf("\ndeterminant is %g\n",det); if(det==0.0||(det<0x1p-16&&det>-0x1p-16)){ printf("ERROR, NON-TRIVIAL SOLUTION\nFREEING MEMORY AND HALTING COMPUTATION\n..."); gsl_vector_free(x); gsl_vector_free(b); gsl_matrix_free(A); gsl_permutation_free(p); printf("DONE\n"); return -1; } gsl_linalg_LU_solve(A,p,b,x); printf("\nthe solution is:\n"); print_vector(x); to_circuit(x,c); gsl_vector_free(x); gsl_matrix_free(A); gsl_vector_free(b); gsl_permutation_free(p); return 0; }
void MVEE::calculateOptimalValue() { gsl_matrix* LU=gsl_matrix_alloc(M->size1, M->size2); gsl_matrix_memcpy(LU, M); gsl_permutation* p = gsl_permutation_alloc(M->size1); int signum = 0; gsl_linalg_LU_decomp (LU, p, &signum); optimalValue = log(gsl_linalg_LU_det(LU,signum)); gsl_matrix_free(LU); gsl_permutation_free(p); }
/* gsl_matrix_det function returns the determinant of gsl_matrix mat */ double gsl_matrix_det(const gsl_matrix *mat) { gsl_permutation *p = gsl_permutation_alloc(mat->size1); gsl_matrix *LU = gsl_matrix_alloc(mat->size1, mat->size2); int sign = 0; double det = 0.0; gsl_matrix_memcpy(LU, mat); gsl_linalg_LU_decomp(LU, p, &sign); det = gsl_linalg_LU_det(LU, sign); gsl_matrix_free(LU); gsl_permutation_free(p); return det; }
double matrix_determ(gsl_matrix *X) { 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); double my_det = gsl_linalg_LU_det (a_copy, s); return(my_det); }
/// Calculate the determinant double GSLMatrix::det() { 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); double res = gsl_linalg_LU_det(LU.gsl(), s); gsl_permutation_free(p); return res; }
double ran_wishart_pdf(const gsl_matrix *X, const double nu, const gsl_matrix *V) { const int k = X->size1; double detX, detV, den, temp; int s, i; gsl_matrix *work_k_k = gsl_matrix_alloc(k, k); gsl_matrix *Vinv = gsl_matrix_alloc(k, k); gsl_permutation *p = gsl_permutation_alloc(k); gsl_matrix_memcpy(work_k_k, X); gsl_linalg_LU_decomp(work_k_k, p, &s); detX = gsl_linalg_LU_det(work_k_k, s); gsl_matrix_memcpy(work_k_k, V); gsl_linalg_LU_decomp(work_k_k, p, &s); detV = gsl_linalg_LU_det(work_k_k, s); gsl_linalg_LU_invert(work_k_k, p, Vinv); gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, Vinv, X, 0.0, work_k_k); den = 0; for (i=0; i<k; i++) { den -= 0.5 * gsl_matrix_get(work_k_k, i, i); } //den = exp(den); temp = 0.5*(nu-k-1)*log(detX) - 0.5*nu*k*log(2) -0.5*nu*log(detV); temp -= sf_mv_lngamma(nu/2, k); den += temp; den = exp(den); gsl_matrix_free(work_k_k); gsl_matrix_free(Vinv); gsl_permutation_free(p); return den; }
double Matrix::detMatrix(gsl_matrix* ludecomp, gsl_permutation* p) { ludecomp->size1=row; ludecomp->size2=col; p->size=row; gsl_matrix_memcpy(ludecomp, matrix); int signum=0; gsl_linalg_LU_decomp(ludecomp, p, &signum); double det=gsl_linalg_LU_det(ludecomp, signum); return det; }
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; }
double Matrix::detMatrix() { gsl_matrix* ludecomp=gsl_matrix_alloc(row,col); gsl_matrix_memcpy(ludecomp, matrix); gsl_permutation* p=gsl_permutation_alloc(row); int signum=0; gsl_linalg_LU_decomp(ludecomp, p, &signum); double det=gsl_linalg_LU_det(ludecomp, signum); gsl_matrix_free(ludecomp); gsl_permutation_free(p); return det; }
static unsigned weakPosDefCondition(void) { gsl_matrix_memcpy(det_adj, E2); int signum; gsl_linalg_LU_decomp(det_adj, p , &signum); double det = gsl_linalg_LU_det(det_adj, signum); if (det < -0.00001) { return 0; } return 1; }
double calc_detWs(double s, double tres, double* Qxx_m, double* Qyy_m, double* Qxy_m, double* Qyx_m, int kx, int ky){ int sc; double result; gsl_matrix* Ws = calc_Ws(s,tres,Qxx_m,Qyy_m,Qxy_m,Qyx_m,kx,ky); gsl_permutation * p = gsl_permutation_alloc (kx); gsl_linalg_LU_decomp(Ws, p, &sc); result = gsl_linalg_LU_det(Ws, sc); //cleanup gsl_permutation_free(p); gsl_matrix_free(Ws); return result; }
double SimplexVolumef(float **coord,int nvert, int ndims) { int i,j,k; double det; int s; double m_data[(nvert-1)*(nvert-1)]; double w_data[(nvert-1)*(nvert-1)]; /* double w_data[3*3] = {1.,0.,0., 1.,1.,0., 1.,1.,1.};*/ //double wt_data[(nvert-1)*ndims]; gsl_permutation * perm; gsl_matrix_view m = gsl_matrix_view_array (m_data, nvert-1, nvert-1); gsl_matrix_view w = gsl_matrix_view_array (w_data, nvert-1, ndims); //gsl_matrix_view wt = gsl_matrix_view_array (wt_data, nvert-1,ndims); if (nvert==2) { for (i=0,det=0;i<ndims;i++) det+=(double)(coord[1][i]-coord[0][i])*(double)(coord[1][i]-coord[0][i]); return sqrt(det); } perm = gsl_permutation_alloc (nvert-1); for (i=0,k=0;i<nvert-1;i++) for (j=0;j<nvert-1;j++,k++) w_data[k]=coord[i+1][j]-coord[0][j]; //gsl_matrix_transpose_memcpy (wt,w); gsl_blas_dgemm (CblasNoTrans, CblasTrans, 1.0, &w.matrix, &w.matrix, 0.0, &m.matrix); gsl_linalg_LU_decomp(&m.matrix,perm,&s); det=gsl_linalg_LU_det(&m.matrix,s); gsl_permutation_free(perm); for (i=2,j=1;i<=nvert-1;i++) j*=i; return sqrt(fabs(det))/j; }
// 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; }
/* This function returns 1 if the graph stored in adj does not have SC_EIG as an eigenvalue and 0 otherwise. */ static unsigned validSC(void) { unsigned i; /* FIXME is det_adj actually changed or can we work on adj???? */ gsl_matrix_memcpy(det_adj, adj); for (i = 0; i < N+1; i++) { gsl_matrix_set(det_adj, i, i, -SC_EIG); } int signum; gsl_linalg_LU_decomp(det_adj, p , &signum); double det = gsl_linalg_LU_det(det_adj, signum); if (fabs(det) > EPS) { return 1; } return 0; }
double Matrix::determinant() { int rows = numRows(); int cols = numCols(); if (rows != cols){ QMessageBox::critical((ApplicationWindow *)applicationWindow(), tr("QtiPlot - Error"), tr("Calculation failed, the matrix is not square!")); return GSL_POSINF; } gsl_set_error_handler_off(); gsl_matrix *A = gsl_matrix_alloc(rows, cols); gsl_permutation * p = gsl_permutation_alloc(rows); if (!A || !p){ QApplication::restoreOverrideCursor(); QMessageBox::critical((ApplicationWindow *)applicationWindow(), tr("QtiPlot") + " - " + tr("Memory Allocation Error"), tr("Not enough memory, operation aborted!")); return 0.0; } QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); double *data = d_matrix_model->dataVector(); int i, cell = 0; for(i=0; i<rows; i++) for(int j=0; j<cols; j++) gsl_matrix_set(A, i, j, data[cell++]); gsl_linalg_LU_decomp(A, p, &i); double det = gsl_linalg_LU_det(A, i); gsl_matrix_free(A); gsl_permutation_free(p); QApplication::restoreOverrideCursor(); return det; }
static int md_det(lua_State *L) /* (-1,+1,e) */ { mMatReal *m = qlua_checkMatReal(L, 1); mMatReal *lu; gsl_permutation *p; int signum; double d; if (m->l_size != m->r_size) return luaL_error(L, "square matrix expected"); p = new_permutation(L, m->l_size); lu = qlua_newMatReal(L, m->l_size, m->l_size); gsl_matrix_memcpy(lu->m, m->m); gsl_linalg_LU_decomp(lu->m, p, &signum); d = gsl_linalg_LU_det(lu->m, signum); gsl_permutation_free(p); lua_pushnumber(L, d); return 1; }
double ran_mv_t_pdf(const gsl_vector *x, const gsl_vector *mu, const gsl_matrix *Sigma, const double nu) { const int k = x->size; int s; double det,temp, den; gsl_vector *y = gsl_vector_alloc(k); gsl_vector *work_k = gsl_vector_alloc(k); gsl_matrix *work_k_k = gsl_matrix_alloc(k, k); gsl_matrix *Sigmainv = gsl_matrix_alloc(k, k); gsl_permutation *p = gsl_permutation_alloc(k); gsl_vector_memcpy(y, x); gsl_vector_sub(y, mu); gsl_matrix_memcpy(work_k_k, Sigma); gsl_linalg_LU_decomp(work_k_k, p, &s); gsl_linalg_LU_invert(work_k_k, p, Sigmainv); det = gsl_linalg_LU_det(work_k_k, s); gsl_blas_dgemv(CblasNoTrans, 1.0/k, Sigmainv, y, 0.0, work_k); gsl_blas_ddot(y, work_k, &temp); temp = pow((1+temp), (nu+ (double) k)/2 ); temp *= gsl_sf_gamma(nu/2) * pow(nu, k/2) * pow(M_PI, k/2) * sqrt(det); den = gsl_sf_gamma((nu+ (double) k)/2); den /= temp; gsl_vector_free(y); gsl_vector_free(work_k); gsl_matrix_free(work_k_k); gsl_matrix_free(Sigmainv); gsl_permutation_free(p); return den; }
double Matrix::determinant() { int rows = d_table->numRows(); int cols = d_table->numCols(); if (rows != cols) { QMessageBox::critical(0,tr("QtiPlot - Error"), tr("Calculation failed, the matrix is not square!")); return GSL_POSINF; } 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 (rows); gsl_linalg_LU_decomp (A, p, &s); double det = gsl_linalg_LU_det (A, s); gsl_matrix_free (A); gsl_permutation_free (p); QApplication::restoreOverrideCursor(); return det; }
/**************************************************** LU分解を用い共分散行列の行列式・逆行列を計算 gaussian_params のメンバ double *cov から 行列式と逆行列を計算し、メンバ double detcov と double *icov に計算結果をそれぞれ格納する。 ****************************************************/ void gaussian_params_det_and_inv_covariance (gaussian_params *par) { int s; // Permutation gsl_permutation *p; gsl_matrix *lu; // 1次元配列 par->cov に対する行列の像 // Matrix_view gsl_matrix_view cov; // 1次元配列 par->icov に対する行列の像 gsl_matrix_view icov; cov = gsl_matrix_view_array (par->cov, par->ndim, par->ndim); if (par->icov == NULL) par->icov = (double *) malloc (par->ndim * par->ndim * sizeof (double)); icov = gsl_matrix_view_array (par->icov, par->ndim, par->ndim); p = gsl_permutation_alloc (par->ndim); // LU分解を計算するために用いるテンポラリな行列 *lu に cov.matrix をコピー lu = gsl_matrix_alloc (par->ndim, par->ndim); gsl_matrix_memcpy (lu, &cov.matrix); // LU-Decomposition gsl_linalg_LU_decomp (lu, p, &s); // LU分解 par->detcov = gsl_linalg_LU_det (lu, s); // 行列式 gsl_linalg_LU_invert (lu, p, &icov.matrix); // 逆行列 gsl_permutation_free (p); gsl_matrix_free (lu); return; }
void xmi_determinant_matrix(double x[3], double y[3], double z[3]) { gsl_matrix *m = gsl_matrix_alloc(3,3); gsl_permutation *p = gsl_permutation_calloc(3); int signum; int i,j,k; double det; 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]); gsl_linalg_LU_decomp(m,p,&signum); det=gsl_linalg_LU_det(m,signum); fprintf(stdout,"Determinant is: %lf\n",det); return; }
/*============================================================================*/ int ighmm_invert_det(double *sigmainv, double *det, int length, double *cov) { #define CUR_PROC "invert_det" #ifdef DO_WITH_GSL int i, j, s; gsl_matrix *tmp; gsl_matrix *inv; tmp = gsl_matrix_alloc(length, length); inv = gsl_matrix_alloc(length, length); gsl_permutation *permutation = gsl_permutation_calloc(length); for (i=0; i<length; ++i) { for (j=0; j<length; ++j) { #ifdef DO_WITH_GSL_DIAGONAL_HACK if (i == j){ gsl_matrix_set(tmp, i, j, cov[i*length+j]); }else{ gsl_matrix_set(tmp, i, j, 0.0); } #else gsl_matrix_set(tmp, i, j, cov[i*length+j]); #endif } } gsl_linalg_LU_decomp(tmp, permutation, &s); gsl_linalg_LU_invert(tmp, permutation, inv); *det = gsl_linalg_LU_det(tmp, s); gsl_matrix_free(tmp); gsl_permutation_free(permutation); for (i=0; i<length; ++i) { for (j=0; j<length; ++j) { sigmainv[i*length+j] = gsl_matrix_get(inv, i, j); } } gsl_matrix_free(inv); #elif defined HAVE_CLAPACK_DGETRF && HAVE_CLAPACK_DGETRI char sign; int info, i; int *ipiv; double det_tmp; ipiv = malloc(length * sizeof *ipiv); /* copy cov. matrix entries to result matrix, the rest is done in-place */ memcpy(sigmainv, cov, length * length * sizeof *cov); /* perform in-place LU factorization of covariance matrix */ info = clapack_dgetrf(CblasRowMajor, length, length, sigmainv, length, ipiv); /* determinant */ sign = 1; for( i=0; i<length; ++i) if( ipiv[i]!=i ) sign *= -1; det_tmp = sigmainv[0]; for( i=length+1; i<(length*length); i+=length+1 ) det_tmp *= sigmainv[i]; *det = det_tmp * sign; /* use the LU factorization to get inverse */ info = clapack_dgetri(CblasRowMajor, length, sigmainv, length, ipiv); free(ipiv); #else *det = ighmm_determinant(cov, length); ighmm_inverse(cov, length, *det, sigmainv); #endif return 0; #undef CUR_PROC }