You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Christian Woltering edited this page Aug 25, 2024
·
1 revision
usingCSparse.Double;usingCSparse.Double.Factorization;usingCSparse.Storage;usingSystem;/// <summary>/// Compute the inverse of given matrix./// </summary>publicstaticCompressedColumnStorage<double>Inverse(CompressedColumnStorage<double>A){intsize=A.RowCount;if(A.ColumnCount!=size){thrownewArgumentException("Matrix must be square.");}varlu=SparseLU.Create(A,ColumnOrdering.MinimumDegreeAtPlusA,1.0);varb=newdouble[size];varx=newdouble[size];// Result might be dense. We use a sparse storage with memory fully// allocated for simplicity.varinverse=newCoordinateStorage<double>(size,size,size*size);for(inti=0;i<size;i++){b[i]=1.0;lu.Solve(b,x);for(intj=0;j<size;j++){inverse.At(j,i,x[j]);}b[i]=0.0;}returnSparseMatrix.OfIndexed(inverse);}