11#include <Eigen/SparseCore>
12#include <Eigen/SparseLU>
15template<
typename Solver,
typename Rhs,
typename Guess,
typename Result>
17 if(internal::random<bool>())
20 x =
solver.derived().solveWithGuess(
b,g) + Result::Zero(
x.rows(),
x.cols());
25 x =
solver.derived().solveWithGuess(
b.derived(),g);
29template<
typename Solver,
typename Rhs,
typename Guess,
typename Result>
31 if(internal::random<bool>())
32 x =
solver.derived().solve(
b) + Result::Zero(
x.rows(),
x.cols());
37template<
typename Solver,
typename Rhs,
typename Guess,
typename Result>
42template<
typename Solver,
typename Rhs,
typename DenseMat,
typename DenseRhs>
45 typedef typename Solver::MatrixType
Mat;
47 typedef typename Mat::StorageIndex StorageIndex;
49 DenseRhs refX = dA.householderQr().solve(db);
57 std::cerr <<
"ERROR | sparse solver testing, factorization failed (" <<
typeid(Solver).name() <<
")\n";
63 std::cerr <<
"WARNING: sparse solver testing: solving failed (" <<
typeid(Solver).name() <<
")\n";
70 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
71 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
76 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
77 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
83 VERIFY(
solver.info() ==
Success &&
"factorization failed when using analyzePattern/factorize API");
85 VERIFY(
solver.info() ==
Success &&
"solving failed when using analyzePattern/factorize API");
86 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
87 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
100 VERIFY(oldb.isApprox(bm) &&
"sparse solver testing: the rhs should not be modified!");
101 VERIFY(xm.isApprox(refX,test_precision<Scalar>()));
109 Rhs
x(
b.rows(),
b.cols());
112 x = solver2.solve(
b);
113 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
118 DenseRhs
x(refX.rows(), refX.cols());
121 x.block(0,0,
x.rows(),
x.cols()) =
solver.solve(db.block(0,0,db.rows(),db.cols()));
122 VERIFY(oldb.isApprox(db) &&
"sparse solver testing: the rhs should not be modified!");
123 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
129 A2.reserve((ArrayXf::Random(
A.outerSize())+2).template cast<typename Mat::StorageIndex>().eval());
132 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
139 VERIFY(
x.isApprox(refX,test_precision<Scalar>()));
141 Solver solver2(0.5*(
A+
A));
142 Rhs x2 = solver2.solve(
b);
143 VERIFY(x2.isApprox(refX,test_precision<Scalar>()));
149template<
typename Scalar,
typename Rhs,
typename DenseMat,
typename DenseRhs>
153 typedef typename Mat::StorageIndex StorageIndex;
157 DenseRhs refX1 = dA.householderQr().
solve(db);
158 DenseRhs refX2 = dA.transpose().householderQr().solve(db);
159 DenseRhs refX3 = dA.adjoint().householderQr().solve(db);
163 Rhs x1(
A.
cols(),
b.cols());
164 Rhs x2(
A.
cols(),
b.cols());
165 Rhs x3(
A.
cols(),
b.cols());
171 std::cerr <<
"ERROR | sparse solver testing, factorization failed (" <<
typeid(Solver).name() <<
")\n";
177 std::cerr <<
"WARNING | sparse solver testing: solving failed (" <<
typeid(Solver).name() <<
")\n";
180 VERIFY(oldb.isApprox(
b,0.0) &&
"sparse solver testing: the rhs should not be modified!");
181 VERIFY(x1.isApprox(refX1,test_precision<Scalar>()));
184 x2 =
solver.transpose().solve(
b);
185 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
186 VERIFY(x2.isApprox(refX2,test_precision<Scalar>()));
191 x3 =
solver.adjoint().solve(
b);
192 VERIFY(oldb.isApprox(
b,0.0) &&
"sparse solver testing: the rhs should not be modified!");
193 VERIFY(x3.isApprox(refX3,test_precision<Scalar>()));
197 VERIFY(
solver.info() ==
Success &&
"solving failed when using analyzePattern/factorize API");
198 VERIFY(oldb.isApprox(
b,0.0) &&
"sparse solver testing: the rhs should not be modified!");
199 VERIFY(x1.isApprox(refX1,test_precision<Scalar>()));
207 VERIFY(
solver.info() ==
Success &&
"factorization failed when using analyzePattern/factorize API");
209 x2 =
solver.transpose().solve(
b);
210 x3 =
solver.adjoint().solve(
b);
212 VERIFY(
solver.info() ==
Success &&
"solving failed when using analyzePattern/factorize API");
213 VERIFY(oldb.isApprox(
b,0.0) &&
"sparse solver testing: the rhs should not be modified!");
214 VERIFY(x1.isApprox(refX1,test_precision<Scalar>()));
215 VERIFY(x2.isApprox(refX2,test_precision<Scalar>()));
216 VERIFY(x3.isApprox(refX3,test_precision<Scalar>()));
229 VERIFY(oldb.isApprox(bm,0.0) &&
"sparse solver testing: the rhs should not be modified!");
230 VERIFY(xm.isApprox(refX1,test_precision<Scalar>()));
238 Rhs
x(
b.rows(),
b.cols());
241 x = solver2.solve(
b);
242 VERIFY(
x.isApprox(refX1,test_precision<Scalar>()));
247 DenseRhs
x(refX1.rows(), refX1.cols());
250 x.block(0,0,
x.rows(),
x.cols()) =
solver.solve(db.block(0,0,db.rows(),db.cols()));
251 VERIFY(oldb.isApprox(db,0.0) &&
"sparse solver testing: the rhs should not be modified!");
252 VERIFY(
x.isApprox(refX1,test_precision<Scalar>()));
258 A2.reserve((ArrayXf::Random(
A.outerSize())+2).template cast<typename Mat::StorageIndex>().eval());
261 VERIFY(
x.isApprox(refX1,test_precision<Scalar>()));
268 VERIFY(
x.isApprox(refX1,test_precision<Scalar>()));
270 Solver solver2(0.5*(
A+
A));
271 Rhs x2 = solver2.solve(
b);
272 VERIFY(x2.isApprox(refX1,test_precision<Scalar>()));
278template<
typename Solver,
typename Rhs>
281 typedef typename Solver::MatrixType
Mat;
290 std::cerr <<
"ERROR | sparse solver testing, factorization failed (" <<
typeid(Solver).name() <<
")\n";
297 std::cerr <<
"WARNING | sparse solver testing, solving failed (" <<
typeid(Solver).name() <<
")\n";
302 VERIFY( (res_error <= test_precision<Scalar>() ) &&
"sparse solver failed without noticing it");
305 if(refX.size() != 0 && (refX -
x).norm()/refX.norm() > test_precision<Scalar>())
307 std::cerr <<
"WARNING | found solution is different from the provided reference one\n";
311template<
typename Solver,
typename DenseMat>
314 typedef typename Solver::MatrixType
Mat;
320 std::cerr <<
"WARNING | sparse solver testing: factorization failed (check_sparse_determinant)\n";
324 Scalar refDet = dA.determinant();
327template<
typename Solver,
typename DenseMat>
331 typedef typename Solver::MatrixType
Mat;
337 std::cerr <<
"WARNING | sparse solver testing: factorization failed (check_sparse_abs_determinant)\n";
345template<
typename Solver,
typename DenseMat>
348 typedef typename Solver::MatrixType
Mat;
352 int size = internal::random<int>(1,maxSize);
353 double density = (std::max)(8./(
size*
size), 0.01);
361 dA = dM * dM.adjoint();
367 halfA.template selfadjointView<Solver::UpLo>().rankUpdate(
M);
373#ifdef TEST_REAL_CASES
374template<
typename Scalar>
375inline std::string get_matrixfolder()
377 std::string mat_folder = TEST_REAL_CASES;
378 if( internal::is_same<
Scalar, std::complex<float> >::value || internal::is_same<
Scalar, std::complex<double> >::value )
379 mat_folder = mat_folder +
static_cast<std::string
>(
"/complex/");
381 mat_folder = mat_folder +
static_cast<std::string
>(
"/real/");
384std::string sym_to_string(
int sym)
387 if(sym==
SPD)
return "SPD ";
390template<
typename Derived>
393 std::stringstream ss;
394 ss <<
solver.iterations() <<
" iters, error: " <<
solver.error();
397template<
typename Derived>
406 typedef typename Solver::MatrixType
Mat;
408 typedef typename Mat::StorageIndex StorageIndex;
417 for (
int i = 0;
i < g_repeat;
i++) {
421 int rhsCols = internal::random<int>(1,16);
422 double density = (std::max)(8./(
size*rhsCols), 0.1);
442 b = DenseVector::Zero(
size);
448#ifdef TEST_REAL_CASES
452 std::string mat_folder = get_matrixfolder<Scalar>();
458 if(
A.diagonal().size() <= maxRealWorldSize)
467 halfA.template selfadjointView<Solver::UpLo>() =
A.template triangularView<Eigen::Lower>().twistedBy(pnull);
469 std::cout <<
"INFO | Testing " << sym_to_string(it.
sym()) <<
"sparse problem " << it.
matname()
470 <<
" (" <<
A.
rows() <<
"x" <<
A.
cols() <<
") using " <<
typeid(Solver).name() <<
"..." << std::endl;
472 std::string stats = solver_stats(
solver);
474 std::cout <<
"INFO | " << stats << std::endl;
479 std::cout <<
"INFO | Skip sparse problem \"" << it.
matname() <<
"\" (too large)" << std::endl;
491 typedef typename Solver::MatrixType
Mat;
500 for (
int i = 0;
i < g_repeat;
i++) {
506template<
typename Solver,
typename DenseMat>
509 typedef typename Solver::MatrixType
Mat;
512 Index size = internal::random<int>(1,maxSize);
513 double density = (std::max)(8./(
size*
size), 0.01);
518 initSparse<Scalar>(density, dA,
A, options);
527 template<
class Scalar>
536 typedef typename Solver::MatrixType
Mat;
543 int rhsCols = internal::random<int>(1,16);
547 for (
int i = 0;
i < g_repeat;
i++) {
554 double density = (std::max)(8./(
size*rhsCols), 0.1);
570 if(checkDeficient &&
size>1) {
579#ifdef TEST_REAL_CASES
583 std::string mat_folder = get_matrixfolder<Scalar>();
588 if(
A.diagonal().size() <= maxRealWorldSize)
592 std::cout <<
"INFO | Testing " << sym_to_string(it.
sym()) <<
"sparse problem " << it.
matname()
593 <<
" (" <<
A.
rows() <<
"x" <<
A.
cols() <<
") using " <<
typeid(Solver).name() <<
"..." << std::endl;
595 std::string stats = solver_stats(
solver);
597 std::cout <<
"INFO | " << stats << std::endl;
601 std::cout <<
"INFO | SKIP sparse problem \"" << it.
matname() <<
"\" (too large)" << std::endl;
613 typedef typename Solver::MatrixType
Mat;
617 for (
int i = 0;
i < g_repeat;
i++) {
622 int size = internal::random<int>(1,30);
625 dA = (dA.array().abs()<0.3).select(0,dA);
626 dA.diagonal() = (dA.diagonal().array()==0).select(1,dA.diagonal());
636 typedef typename Solver::MatrixType
Mat;
640 for (
int i = 0;
i < g_repeat;
i++) {
650template<
typename Solver,
typename DenseMat>
653 typedef typename Solver::MatrixType
Mat;
656 int rows = internal::random<int>(1,maxSize);
657 int cols = internal::random<int>(1,
rows);
658 double density = (std::max)(8./(
rows*
cols), 0.01);
663 initSparse<Scalar>(density, dA,
A, options);
668 typedef typename Solver::MatrixType
Mat;
674 int rhsCols = internal::random<int>(1,16);
678 for (
int i = 0;
i < g_repeat;
i++) {
685 double density = (std::max)(8./(
A.
rows()*rhsCols), 0.1);
695 b = DenseVector::Zero(
A.
rows());
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition BenchSparseUtil.h:24
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition BenchSparseUtil.h:23
BiCGSTAB< SparseMatrix< double > > solver
Definition BiCGSTAB_simple.cpp:5
int i
Definition BiCGSTAB_step_by_step.cpp:9
#define EIGEN_UNUSED_VARIABLE(var)
Definition Macros.h:1076
int rows
Definition Tutorial_commainit_02.cpp:1
int cols
Definition Tutorial_commainit_02.cpp:1
Eigen::SparseMatrix< double > SpMat
Definition Tutorial_sparse_example.cpp:5
Scalar Scalar * c
Definition benchVecAdd.cpp:17
Scalar * b
Definition benchVecAdd.cpp:17
Scalar Scalar int size
Definition benchVecAdd.cpp:17
SCALAR Scalar
Definition bench_gemm.cpp:46
Matrix< RealScalar, Dynamic, Dynamic > M
Definition bench_gemm.cpp:51
NumTraits< Scalar >::Real RealScalar
Definition bench_gemm.cpp:47
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition bench_gemm.cpp:48
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition bench_gemm.cpp:49
#define EIGEN_TEST_MAX_SIZE
Definition boostmultiprec.cpp:16
Base class for linear iterative solvers.
Definition IterativeSolverBase.h:144
A matrix or vector expression mapping an existing array of data.
Definition Map.h:96
Sparse matrix.
Definition MappedSparseMatrix.h:34
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Iterator to browse matrices from a specified folder.
Definition MatrixMarketIterator.h:43
MatrixType & matrix()
Definition MatrixMarketIterator.h:74
VectorType & refX()
Definition MatrixMarketIterator.h:145
std::string & matname()
Definition MatrixMarketIterator.h:163
VectorType & rhs()
Definition MatrixMarketIterator.h:113
int sym()
Definition MatrixMarketIterator.h:165
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
Permutation matrix.
Definition PermutationMatrix.h:298
NumTraits< Scalar >::Real RealScalar
Definition PlainObjectBase.h:109
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition PlainObjectBase.h:145
internal::traits< Derived >::Scalar Scalar
Definition PlainObjectBase.h:106
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition PlainObjectBase.h:271
Derived & setRandom(Index size)
Definition Random.h:151
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition PlainObjectBase.h:143
Sparse supernodal LU factorization for general matrices.
Definition SparseLU.h:132
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:28
A versatible sparse matrix representation.
Definition SparseMatrix.h:98
A base class for sparse solvers.
Definition SparseSolverBase.h:68
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition SparseSolverBase.h:88
a sparse vector class
Definition SparseVector.h:66
#define VERIFY(a)
Definition main.h:380
#define CALL_SUBTEST(FUNC)
Definition main.h:399
#define VERIFY_IS_EQUAL(a, b)
Definition main.h:386
#define abs(x)
Definition datatypes.h:17
Matrix< Scalar, Dynamic, Dynamic > Mat
Definition gemm_common.h:15
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition gnuplot_common_settings.hh:12
@ Symmetric
Definition Constants.h:227
@ Lower
Definition Constants.h:209
@ Upper
Definition Constants.h:211
@ NumericalIssue
Definition Constants.h:444
@ Success
Definition Constants.h:442
#define VERIFY_IS_APPROX(a, b)
Definition integer_types.cpp:15
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
@ SPD
Definition MatrixMarketIterator.h:17
@ ForceNonZeroDiag
Definition sparse.h:37
int generate_sparse_spd_problem(Solver &, typename Solver::MatrixType &A, typename Solver::MatrixType &halfA, DenseMat &dA, int maxSize=300)
Definition sparse_solver.h:346
void check_sparse_square_solving(Solver &solver, int maxSize=300, int maxRealWorldSize=100000, bool checkDeficient=false)
Definition sparse_solver.h:534
void generate_sparse_leastsquare_problem(Solver &, typename Solver::MatrixType &A, DenseMat &dA, int maxSize=300, int options=ForceNonZeroDiag)
Definition sparse_solver.h:651
Index generate_sparse_square_problem(Solver &, typename Solver::MatrixType &A, DenseMat &dA, int maxSize=300, int options=ForceNonZeroDiag)
Definition sparse_solver.h:507
void check_sparse_solving(Solver &solver, const typename Solver::MatrixType &A, const Rhs &b, const DenseMat &dA, const DenseRhs &db)
Definition sparse_solver.h:43
void check_sparse_square_abs_determinant(Solver &solver)
Definition sparse_solver.h:634
void check_sparse_spd_determinant(Solver &solver)
Definition sparse_solver.h:489
void solve_with_guess(IterativeSolverBase< Solver > &solver, const MatrixBase< Rhs > &b, const Guess &g, Result &x)
Definition sparse_solver.h:16
void check_sparse_spd_solving(Solver &solver, int maxSize=(std::min)(300, EIGEN_TEST_MAX_SIZE), int maxRealWorldSize=100000)
Definition sparse_solver.h:404
void check_sparse_determinant(Solver &solver, const typename Solver::MatrixType &A, const DenseMat &dA)
Definition sparse_solver.h:312
void check_sparse_abs_determinant(Solver &solver, const typename Solver::MatrixType &A, const DenseMat &dA)
Definition sparse_solver.h:328
void check_sparse_leastsquare_solving(Solver &solver)
Definition sparse_solver.h:666
void check_sparse_solving_real_cases(Solver &solver, const typename Solver::MatrixType &A, const Rhs &b, const typename Solver::MatrixType &fullA, const Rhs &refX)
Definition sparse_solver.h:279
void check_sparse_square_determinant(Solver &solver)
Definition sparse_solver.h:611
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:233
Definition sparse_solver.h:524
Index m_col
Definition sparse_solver.h:525
bool operator()(Index, Index col, const Scalar &) const
Definition sparse_solver.h:528
prune_column(Index col)
Definition sparse_solver.h:526