11 #include <Eigen/SparseCore>
12 #include <Eigen/SparseLU>
15 template <
typename Solver,
typename Rhs,
typename Guess,
typename Result>
17 if (internal::random<bool>()) {
22 x =
solver.derived().solveWithGuess(
b.derived(), g);
26 template <
typename Solver,
typename Rhs,
typename Guess,
typename Result>
28 if (internal::random<bool>())
34 template <
typename Solver,
typename Rhs,
typename Guess,
typename Result>
39 template <
typename Solver,
typename Rhs,
typename DenseMat,
typename DenseRhs>
44 typedef typename Mat::StorageIndex StorageIndex;
46 DenseRhs refX = dA.householderQr().solve(db);
53 std::cerr <<
"ERROR | sparse solver testing, factorization failed (" <<
typeid(Solver).
name() <<
")\n";
58 std::cerr <<
"WARNING: sparse solver testing: solving failed (" <<
typeid(Solver).
name() <<
")\n";
65 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
66 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
71 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
72 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
78 VERIFY(
solver.info() ==
Success &&
"factorization failed when using analyzePattern/factorize API");
80 VERIFY(
solver.info() ==
Success &&
"solving failed when using analyzePattern/factorize API");
81 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
82 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
87 A.
rows(),
A.
cols(),
A.nonZeros(),
const_cast<StorageIndex*
>(
A.outerIndexPtr()),
88 const_cast<StorageIndex*
>(
A.innerIndexPtr()),
const_cast<Scalar*
>(
A.valuePtr()));
97 VERIFY(oldb.isApprox(bm) &&
"sparse solver testing: the rhs should not be modified!");
98 VERIFY(xm.isApprox(refX, test_precision<Scalar>()));
106 VERIFY(outm.isApprox(refX, test_precision<Scalar>()));
110 if (
A.
rows() < 2000) {
113 Rhs x(
b.rows(),
b.cols());
116 x = solver2.solve(
b);
117 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
122 DenseRhs
x(refX.rows(), refX.cols());
125 x.block(0, 0,
x.rows(),
x.cols()) =
solver.solve(db.block(0, 0, db.rows(), db.cols()));
126 VERIFY(oldb.isApprox(db) &&
"sparse solver testing: the rhs should not be modified!");
127 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
133 A2.reserve((ArrayXf::Random(
A.outerSize()) + 2).template cast<typename Mat::StorageIndex>().eval());
136 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
143 VERIFY(
x.isApprox(refX, test_precision<Scalar>()));
145 Solver solver2(0.5 * (
A +
A));
146 Rhs x2 = solver2.solve(
b);
147 VERIFY(
x2.isApprox(refX, test_precision<Scalar>()));
153 template <
typename Scalar,
typename Rhs,
typename DenseMat,
typename DenseRhs>
156 const DenseRhs& db) {
158 typedef typename Mat::StorageIndex StorageIndex;
162 DenseRhs refX1 = dA.householderQr().
solve(db);
163 DenseRhs refX2 = dA.transpose().householderQr().solve(db);
164 DenseRhs refX3 = dA.adjoint().householderQr().solve(db);
174 std::cerr <<
"ERROR | sparse solver testing, factorization failed (" <<
typeid(Solver).
name() <<
")\n";
179 std::cerr <<
"WARNING | sparse solver testing: solving failed (" <<
typeid(Solver).
name() <<
")\n";
182 VERIFY(oldb.isApprox(
b, 0.0) &&
"sparse solver testing: the rhs should not be modified!");
183 VERIFY(
x1.isApprox(refX1, test_precision<Scalar>()));
187 VERIFY(oldb.isApprox(
b) &&
"sparse solver testing: the rhs should not be modified!");
188 VERIFY(
x2.isApprox(refX2, test_precision<Scalar>()));
192 x3 =
solver.adjoint().solve(
b);
193 VERIFY(oldb.isApprox(
b, 0.0) &&
"sparse solver testing: the rhs should not be modified!");
194 VERIFY(x3.isApprox(refX3, test_precision<Scalar>()));
198 VERIFY(
solver.info() ==
Success &&
"solving failed when using analyzePattern/factorize API");
199 VERIFY(oldb.isApprox(
b, 0.0) &&
"sparse solver testing: the rhs should not be modified!");
200 VERIFY(
x1.isApprox(refX1, test_precision<Scalar>()));
208 VERIFY(
solver.info() ==
Success &&
"factorization failed when using analyzePattern/factorize API");
211 x3 =
solver.adjoint().solve(
b);
213 VERIFY(
solver.info() ==
Success &&
"solving failed when using analyzePattern/factorize API");
214 VERIFY(oldb.isApprox(
b, 0.0) &&
"sparse solver testing: the rhs should not be modified!");
215 VERIFY(
x1.isApprox(refX1, test_precision<Scalar>()));
216 VERIFY(
x2.isApprox(refX2, test_precision<Scalar>()));
217 VERIFY(x3.isApprox(refX3, test_precision<Scalar>()));
222 A.
rows(),
A.
cols(),
A.nonZeros(),
const_cast<StorageIndex*
>(
A.outerIndexPtr()),
223 const_cast<StorageIndex*
>(
A.innerIndexPtr()),
const_cast<Scalar*
>(
A.valuePtr()));
232 VERIFY(oldb.isApprox(bm, 0.0) &&
"sparse solver testing: the rhs should not be modified!");
233 VERIFY(xm.isApprox(refX1, test_precision<Scalar>()));
237 if (
A.
rows() < 2000) {
240 Rhs x(
b.rows(),
b.cols());
243 x = solver2.solve(
b);
244 VERIFY(
x.isApprox(refX1, test_precision<Scalar>()));
249 DenseRhs
x(refX1.rows(), refX1.cols());
252 x.block(0, 0,
x.rows(),
x.cols()) =
solver.solve(db.block(0, 0, db.rows(), db.cols()));
253 VERIFY(oldb.isApprox(db, 0.0) &&
"sparse solver testing: the rhs should not be modified!");
254 VERIFY(
x.isApprox(refX1, test_precision<Scalar>()));
260 A2.reserve((ArrayXf::Random(
A.outerSize()) + 2).template cast<typename Mat::StorageIndex>().eval());
263 VERIFY(
x.isApprox(refX1, test_precision<Scalar>()));
270 VERIFY(
x.isApprox(refX1, test_precision<Scalar>()));
272 Solver solver2(0.5 * (
A +
A));
273 Rhs x2 = solver2.solve(
b);
274 VERIFY(
x2.isApprox(refX1, test_precision<Scalar>()));
279 template <
typename Solver,
typename Rhs>
290 std::cerr <<
"ERROR | sparse solver testing, factorization failed (" <<
typeid(Solver).
name() <<
")\n";
296 std::cerr <<
"WARNING | sparse solver testing, solving failed (" <<
typeid(Solver).
name() <<
")\n";
301 VERIFY((res_error <= test_precision<Scalar>()) &&
"sparse solver failed without noticing it");
303 if (refX.size() != 0 && (refX -
x).norm() / refX.norm() > test_precision<Scalar>()) {
304 std::cerr <<
"WARNING | found solution is different from the provided reference one\n";
307 template <
typename Solver,
typename DenseMat>
314 std::cerr <<
"WARNING | sparse solver testing: factorization failed (check_sparse_determinant)\n";
318 Scalar refDet = dA.determinant();
321 template <
typename Solver,
typename DenseMat>
329 std::cerr <<
"WARNING | sparse solver testing: factorization failed (check_sparse_abs_determinant)\n";
337 template <
typename Solver,
typename DenseMat>
339 DenseMat& dA,
int maxSize = 300) {
344 int size = internal::random<int>(1, maxSize);
353 dA = dM * dM.adjoint();
359 halfA.template selfadjointView<Solver::UpLo>().rankUpdate(
M);
364 #ifdef TEST_REAL_CASES
365 template <
typename Scalar>
368 if (internal::is_same<
Scalar, std::complex<float>>::
value || internal::is_same<
Scalar, std::complex<double>>::
value)
369 mat_folder = mat_folder +
static_cast<std::string>(
"/complex/");
371 mat_folder = mat_folder +
static_cast<std::string>(
"/real/");
375 if (sym ==
Symmetric)
return "Symmetric ";
376 if (sym ==
SPD)
return "SPD ";
379 template <
typename Derived>
381 std::stringstream ss;
382 ss <<
solver.iterations() <<
" iters, error: " <<
solver.error();
385 template <
typename Derived>
391 template <
typename Solver>
393 int maxRealWorldSize = 100000) {
396 typedef typename Mat::StorageIndex StorageIndex;
409 int rhsCols = internal::random<int>(1, 16);
435 #ifdef TEST_REAL_CASES
438 std::string mat_folder = get_matrixfolder<Scalar>();
443 if (
A.diagonal().size() <= maxRealWorldSize) {
451 halfA.template selfadjointView<Solver::UpLo>() =
A.template triangularView<Eigen::Lower>().twistedBy(pnull);
453 std::cout <<
"INFO | Testing " << sym_to_string(it.
sym()) <<
"sparse problem " << it.
matname() <<
" ("
454 <<
A.
rows() <<
"x" <<
A.
cols() <<
") using " <<
typeid(Solver).
name() <<
"..." << std::endl;
457 if (stats.size() > 0) std::cout <<
"INFO | " << stats << std::endl;
460 std::cout <<
"INFO | Skip sparse problem \"" << it.
matname() <<
"\" (too large)" << std::endl;
470 template <
typename Solver>
487 template <
typename Solver,
typename DenseMat>
489 DenseMat& dA,
int maxSize = 300) {
494 int size = internal::random<int>(1, maxSize);
502 A =
M *
M.transpose();
503 dA = dM * dM.transpose();
509 halfA =
A.template triangularView<Solver::UpLo>();
514 template <
typename Solver>
516 int maxRealWorldSize = 100000) {
519 typedef typename Mat::StorageIndex StorageIndex;
532 int rhsCols = internal::random<int>(1, 16);
560 template <
typename Solver>
577 template <
typename Solver>
586 template <
typename Solver,
typename DenseMat>
592 Index size = internal::random<int>(1, maxSize);
598 initSparse<Scalar>(
density, dA,
A, options);
606 template <
class Scalar>
612 template <
typename Solver>
614 bool checkDeficient =
false) {
622 int rhsCols = internal::random<int>(1, 16);
648 if (checkDeficient &&
size > 1) {
657 #ifdef TEST_REAL_CASES
660 std::string mat_folder = get_matrixfolder<Scalar>();
664 if (
A.diagonal().size() <= maxRealWorldSize) {
667 std::cout <<
"INFO | Testing " << sym_to_string(it.
sym()) <<
"sparse problem " << it.
matname() <<
" ("
668 <<
A.
rows() <<
"x" <<
A.
cols() <<
") using " <<
typeid(Solver).
name() <<
"..." << std::endl;
671 if (stats.size() > 0) std::cout <<
"INFO | " << stats << std::endl;
673 std::cout <<
"INFO | SKIP sparse problem \"" << it.
matname() <<
"\" (too large)" << std::endl;
682 template <
typename Solver>
693 int size = internal::random<int>(1, 30);
696 dA = (dA.array().abs() < 0.3).select(0, dA);
697 dA.diagonal() = (dA.diagonal().array() == 0).select(1, dA.diagonal());
705 template <
typename Solver>
721 template <
typename Solver,
typename DenseMat>
727 int rows = internal::random<int>(1, maxSize);
728 int cols = internal::random<int>(1,
rows);
734 initSparse<Scalar>(
density, dA,
A, options);
737 template <
typename Solver>
745 int rhsCols = internal::random<int>(1, 16);
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
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:966
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 int size
Definition: benchVecAdd.cpp:17
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:50
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:48
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:124
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Iterator to browse matrices from a specified folder.
Definition: MatrixMarketIterator.h:42
VectorType & refX()
Definition: MatrixMarketIterator.h:132
std::string & matname()
Definition: MatrixMarketIterator.h:147
VectorType & rhs()
Definition: MatrixMarketIterator.h:103
MatrixType & matrix()
Definition: MatrixMarketIterator.h:70
int sym()
Definition: MatrixMarketIterator.h:149
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
NumTraits< Scalar >::Real RealScalar
Definition: PlainObjectBase.h:130
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
internal::traits< Derived >::Scalar Scalar
Definition: PlainObjectBase.h:127
Derived & setRandom(Index size)
Definition: Random.h:147
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:294
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:151
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:84
a sparse vector class
Definition: SparseVector.h:62
Holds strides information for Map.
Definition: Stride.h:55
Definition: matrices.h:74
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
Matrix< Scalar, Dynamic, Dynamic > Mat
Definition: gemm_common.h:15
@ Symmetric
Definition: Constants.h:229
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
@ NumericalIssue
Definition: Constants.h:442
@ Success
Definition: Constants.h:440
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
#define VERIFY(a)
Definition: main.h:362
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
@ Rhs
Definition: TensorContractionMapper.h:20
squared absolute value
Definition: GlobalFunctions.h:87
@ SPD
Definition: MatrixMarketIterator.h:19
static int g_repeat
Definition: main.h:191
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
static int g_test_level
Definition: main.h:190
Vector< double > x1(const Vector< double > &coord)
Cartesian coordinates centered at the point (0.5,1)
Definition: poisson/poisson_with_singularity/two_d_poisson.cc:86
Vector< double > x2(const Vector< double > &coord)
Cartesian coordinates centered at the point (1.5,1)
Definition: poisson/poisson_with_singularity/two_d_poisson.cc:102
int c
Definition: calibrate.py:100
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
double Zero
Definition: pseudosolid_node_update_elements.cc:35
list x
Definition: plotDoE.py:28
string name
Definition: plotDoE.py:33
@ ForceNonZeroDiag
Definition: sparse.h:32
int generate_sparse_spd_problem(Solver &, typename Solver::MatrixType &A, typename Solver::MatrixType &halfA, DenseMat &dA, int maxSize=300)
Definition: sparse_solver.h:338
void check_sparse_square_solving(Solver &solver, int maxSize=300, int maxRealWorldSize=100000, bool checkDeficient=false)
Definition: sparse_solver.h:613
void generate_sparse_leastsquare_problem(Solver &, typename Solver::MatrixType &A, DenseMat &dA, int maxSize=300, int options=ForceNonZeroDiag)
Definition: sparse_solver.h:722
void check_sparse_nonhermitian_solving(Solver &solver, int maxSize=(std::min)(300, EIGEN_TEST_MAX_SIZE), int maxRealWorldSize=100000)
Definition: sparse_solver.h:515
void check_sparse_zero_matrix(Solver &solver)
Definition: sparse_solver.h:578
Index generate_sparse_square_problem(Solver &, typename Solver::MatrixType &A, DenseMat &dA, int maxSize=300, int options=ForceNonZeroDiag)
Definition: sparse_solver.h:587
void check_sparse_solving(Solver &solver, const typename Solver::MatrixType &A, const Rhs &b, const DenseMat &dA, const DenseRhs &db)
Definition: sparse_solver.h:40
void check_sparse_square_abs_determinant(Solver &solver)
Definition: sparse_solver.h:706
void check_sparse_spd_determinant(Solver &solver)
Definition: sparse_solver.h:471
int generate_sparse_nonhermitian_problem(Solver &, typename Solver::MatrixType &A, typename Solver::MatrixType &halfA, DenseMat &dA, int maxSize=300)
Definition: sparse_solver.h:488
void check_sparse_nonhermitian_determinant(Solver &solver)
Definition: sparse_solver.h:561
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:392
void check_sparse_determinant(Solver &solver, const typename Solver::MatrixType &A, const DenseMat &dA)
Definition: sparse_solver.h:308
void check_sparse_abs_determinant(Solver &solver, const typename Solver::MatrixType &A, const DenseMat &dA)
Definition: sparse_solver.h:322
void check_sparse_leastsquare_solving(Solver &solver)
Definition: sparse_solver.h:738
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:280
void check_sparse_square_determinant(Solver &solver)
Definition: sparse_solver.h:683
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: sparse_solver.h:603
Index m_col
Definition: sparse_solver.h:604
bool operator()(Index, Index col, const Scalar &) const
Definition: sparse_solver.h:607
prune_column(Index col)
Definition: sparse_solver.h:605
std::ofstream out("Result.txt")