简体   繁体   中英

Calculating the inverse of a sparse matrix using Cholmod and Cholmod-Extra

I recently installed Cholmod in order to perform sparse cholesky decompositions in some C++ code. I wanted to then use the decomp to calculate the matrix inverse (I have the following problem:

d^T . (A^-1 + B^-1)^-1 . d

where d is a vector ^T indicates transpose, A and B are sparse)

where I wanted to calculate the actual inverse of B , and then can just linear solve for the solution involving the sum. The code calling it is the following:

cholmod_common Common, *cm ;
cm = &Common ;
cholmod_start (cm) ;
cm->print=5;
Common.supernodal = CHOLMOD_SUPERNODAL ;
double *Tx, x;
int *Ti, *Tj, *Rdeg, *Cdeg,j;
cholmod_triplet *T ;
size_t nrow;           
size_t ncol;           
size_t nnz ;            

nrow=Csize;
ncol=Csize;
nnz=numpulsars*numpulsars*numcoeff;

T = cholmod_allocate_triplet(nrow,ncol,nnz,0,CHOLMOD_REAL, cm) ;
Ti=(int*)T->i;
Tj=(int*)T->j;
Tx=(double*)T->x;

for(int i=0;i<numpulsars;i++){      
            for(int j=0;j<numpulsars;j++){
                    if(i==j){
                            pcorr=1.0;
        }
                    else{   
                            angle=pulsarCartesian[i][0]*pulsarCartesian[j][0] +pulsarCartesian[i][1]*pulsarCartesian[j][1]+pulsarCartesian[i][2]*pulsarCartesian[j][2];
                            pcorr=(1.5*(0.5*(1-angle))*log(0.5*(1-angle)) - 0.25*0.5*(1-angle) + 0.5);
                    }

                    for(int c1=0; c1<numcoeff; c1++){
                                    val= pcorr*powercoeff[c1];
                Ti[T->nnz]=i*numcoeff+c1;
                Tj[T->nnz]=j*numcoeff+c1;
                Tx[T->nnz]=val;
                (T->nnz)++;


                    }
           }
    }


cholmod_sparse *PPFMSparse;
cholmod_factor *L ;
cholmod_dense *spinvK;
PPFMSparse=cholmod_triplet_to_sparse(T,T->nnz,cm);
//  cholmod_print_sparse(PPFMSparse, "PPFMSparse", cm); 
L = cholmod_analyze (PPFMSparse, cm) ;
cholmod_factorize (PPFMSparse, L, cm) ;

cholmod_sparse *PPFMinv; 

PPFMinv=cholmod_spinv(L,cm);
//  cholmod_print_sparse(PPFMinv, "PPFMinv", cm);
spinvK = cholmod_sparse_to_dense(PPFMinv, &Common) ;
cholmod_print_dense(spinvK, "spinvK", cm);
cholmod_free_sparse(&PPFMinv,cm);
cholmod_free_factor (&L, cm) ;
cholmod_free_sparse(&PPFMSparse,cm);
cholmod_free_triplet (&T, cm) ;
cholmod_free_dense (&spinvK, cm) ;
cholmod_finish(cm);

I found https://cholmod-extra.readthedocs.org/en/latest/functions.html this function which was meant to calculate the inverse, but it gives me something related to the square of the inverse, rather than the inverse. I was just wondering if anyone had any experience using this, or an equivilent thing to calculate the inverse of a sparse matrix in C++.

Cheers Lindley

I previously used JAMA . It has Cholesky decomposition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM