Skip to content

Commit

Permalink
added LINBCG and SPRSTP
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesDuvert committed Dec 21, 2023
1 parent fd76456 commit f95d114
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/libinit_ac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ void LibInit_ac()
new DLibFunRetNew(lib::sprsax_fun,string("SPRSAX"),2,sprsaxKey);
new DLibFunRetNew(lib::fulstr_fun,string("FULSTR"),1);
new DLibFunRetNew(lib::sprstp_fun,string("SPRSTP"),1);
const string linbcgKey[]={"ITER", "DOUBLE", KLISTEND};
const string linbcgIgnoreKey[]={"ITOL", "TOL", "ITMAX", KLISTEND};
new DLibFunRetNew(lib::linbcg_fun,string("LINBCG"),3,linbcgKey,linbcgIgnoreKey);

const string spl1Key[]={"YP0","YPN_1","YP1","DOUBLE","HELP",KLISTEND}; //YP1 is old value for YP0
new DLibFunRetNew(lib::spl_init_fun,string("SPL_INIT"),2,spl1Key);
Expand Down
31 changes: 30 additions & 1 deletion src/sparse_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ namespace lib {
SizeT nParam = e->NParam(1);
SPMATRowMajDbl *Mat=getFromPtr(e, 0);
SPMATRowMajDbl *res=new SPMATRowMajDbl((*Mat).transpose());
delete Mat;
return convertToPtr(res);

Check warning on line 191 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L187-L191

Added lines #L187 - L191 were not covered by tests
}
BaseGDL* sprsax_fun(EnvT* e) {
Expand All @@ -208,6 +207,36 @@ namespace lib {
return convertToGDL(Mat3);

Check warning on line 207 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L206-L207

Added lines #L206 - L207 were not covered by tests
}

BaseGDL* linbcg_fun(EnvT* e) {
SizeT nParam = e->NParam(3);
SPMATRowMajDbl* A = getFromPtr(e, 0);
BaseGDL* p1 = e->GetParDefined(1); // Right Hand B
DType varType = p1->Type();

Check warning on line 214 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L210-L214

Added lines #L210 - L214 were not covered by tests
if (p1->Dim().Rank() != 1) e->Throw("Argument " + e->GetString(1) + " must be a vector.");
if (varType == GDL_STRING) e->Throw("Argument " + e->GetString(1) + " must not be of STRING type.");
int m = p1->Dim(0);

Check warning on line 217 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L217

Added line #L217 was not covered by tests
if (m != A->cols()) e->Throw("Argument " + e->GetString(1) + " does not have correct size.");
DDoubleGDL* Bgdl = e->GetParAs<DDoubleGDL>(1);

Check warning on line 219 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L219

Added line #L219 was not covered by tests
Eigen::Map<Eigen::VectorXd> B(&(*Bgdl)[0], m);
BaseGDL* p2 = e->GetParDefined(2); // Initial Guess
varType = p2->Type();

Check warning on line 222 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L221-L222

Added lines #L221 - L222 were not covered by tests
if (p2->Dim().Rank() != 1) e->Throw("Argument " + e->GetString(2) + " must be a vector.");
if (varType == GDL_STRING) e->Throw("Argument " + e->GetString(2) + " must not be of STRING type.");
int n = p2->Dim(0);

Check warning on line 225 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L225

Added line #L225 was not covered by tests
if (n != m) e->Throw("Argument " + e->GetString(2) + " does not have correct size.");
DDoubleGDL* Xgdl = e->GetParAs<DDoubleGDL>(2);
Eigen::Map<Eigen::VectorXd> X(&(*Xgdl)[0], n);

Check warning on line 228 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L227-L228

Added lines #L227 - L228 were not covered by tests
//solve ax=b
Eigen::SparseLU<Eigen::SparseMatrix<double>> sparseLU;
sparseLU.compute(*A);

Check warning on line 231 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L230-L231

Added lines #L230 - L231 were not covered by tests
if(sparseLU.info()!=Eigen::Success) e->Throw("Matrix decomposition failed.");
X=sparseLU.solve(B);
if(sparseLU.info()!=Eigen::Success) e->Throw("No solution found.");
DDoubleGDL* resD =new DDoubleGDL(n, BaseGDL::NOZERO);
Eigen::Map<Eigen::VectorXd>(&(*resD)[0], n) = X;
return resD;
}

Check warning on line 238 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L235-L238

Added lines #L235 - L238 were not covered by tests

BaseGDL* fulstr_fun(EnvT* e){
SizeT nParam = e->NParam(1);

Check warning on line 241 in src/sparse_matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/sparse_matrix.cpp#L240-L241

Added lines #L240 - L241 were not covered by tests
// SPMATRowMajDbl Mat=getFromIndexInMatVec(e, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/sparse_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ namespace lib {
BaseGDL* sprsab_fun(EnvT* e);
BaseGDL* sprstp_fun(EnvT* e);
BaseGDL* fulstr_fun(EnvT* e);

BaseGDL* linbcg_fun(EnvT* e);
}
#endif
20 changes: 20 additions & 0 deletions testsuite/test_sparse.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; no a test, really. Just a suggestion for test, plu sthe fact
; that it exercises the coverage.
pro test_sparse

a = [[ 2.0, 1.0, 1.0],[ 4.0, -6.0, 0.0],[-2.0, 7.0, 2.0]]
z=sprsin(a, thresh=0.5)
zz=sprstp(z)
q=fulstr(z)
if abs(total(a-q)) gt 1E-6 then exit,status=1
res=sprsab(z,zz)
result=fulstr(res)
if total(result) ne 29 then exit,status=1
if total(sprsax(z,[1,1,1])) ne 9 then exit,status=1
aludc=a
LUDC, aludc, index
b = [3,-8.0,10]
x = LUSOL(aludc, index, b)
r= LINBCG(SPRSIN(a), B, X)
if abs(total(r-x)) gt 1E-6 then exit,status=1
end

0 comments on commit f95d114

Please sign in to comment.