sparse_extra.cpp File Reference
#include <cstdlib>
#include <string>
#include "sparse.h"
#include <Eigen/SparseExtra>

Functions

std::string GetTestTempFilename (const char *filename)
 
template<typename SetterType , typename DenseType , typename Scalar , int Options>
bool test_random_setter (SparseMatrix< Scalar, Options > &sm, const DenseType &ref, const std::vector< Vector2i > &nonzeroCoords)
 
template<typename SparseMatrixType >
void sparse_extra (const SparseMatrixType &ref)
 
template<typename SparseMatrixType >
void check_marketio ()
 
template<typename VectorType >
void check_marketio_vector ()
 
template<typename DenseMatrixType >
void check_marketio_dense ()
 
template<typename Scalar >
void check_sparse_inverse ()
 
 EIGEN_DECLARE_TEST (sparse_extra)
 

Function Documentation

◆ check_marketio()

template<typename SparseMatrixType >
void check_marketio ( )
126  {
128  Index rows = internal::random<Index>(1, 100);
129  Index cols = internal::random<Index>(1, 100);
130  SparseMatrixType m1, m2;
131  m1 = DenseMatrix::Random(rows, cols).sparseView();
132  std::string filename = GetTestTempFilename("sparse_extra.mtx");
136 }
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
bool saveMarket(const SparseMatrixType &mat, const std::string &filename, int sym=0)
writes a sparse Matrix to a marketmarket format file
Definition: MarketIO.h:299
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Loads a sparse matrix from a matrixmarket format file.
Definition: MarketIO.h:156
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
string filename
Definition: MergeRestartFiles.py:39
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
std::string GetTestTempFilename(const char *filename)
Definition: sparse_extra.cpp:27

References cols, MergeRestartFiles::filename, GetTestTempFilename(), Eigen::loadMarket(), m1, m2(), rows, Eigen::saveMarket(), oomph::Global_string_for_annotation::string(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ check_marketio_dense()

template<typename DenseMatrixType >
void check_marketio_dense ( )
150  {
151  Index rows = DenseMatrixType::MaxRowsAtCompileTime;
152  if (DenseMatrixType::MaxRowsAtCompileTime == Dynamic) {
153  rows = internal::random<Index>(1, 100);
154  } else if (DenseMatrixType::RowsAtCompileTime == Dynamic) {
155  rows = internal::random<Index>(1, DenseMatrixType::MaxRowsAtCompileTime);
156  }
157 
158  Index cols = DenseMatrixType::MaxColsAtCompileTime;
159  if (DenseMatrixType::MaxColsAtCompileTime == Dynamic) {
160  cols = internal::random<Index>(1, 100);
161  } else if (DenseMatrixType::ColsAtCompileTime == Dynamic) {
162  cols = internal::random<Index>(1, DenseMatrixType::MaxColsAtCompileTime);
163  }
164 
165  DenseMatrixType m1, m2;
166  m1 = DenseMatrixType::Random(rows, cols);
167  std::string filename = GetTestTempFilename("dense_extra.mtx");
171 }
bool loadMarketDense(DenseType &mat, const std::string &filename)
Loads a dense Matrix or Vector from a matrixmarket file. If a statically sized matrix has to be parse...
Definition: MarketIO.h:226
bool saveMarketDense(const DenseType &mat, const std::string &filename)
writes a dense Matrix or vector to a marketmarket format file
Definition: MarketIO.h:333
const int Dynamic
Definition: Constants.h:25

References cols, Eigen::Dynamic, MergeRestartFiles::filename, GetTestTempFilename(), Eigen::loadMarketDense(), m1, m2(), rows, Eigen::saveMarketDense(), oomph::Global_string_for_annotation::string(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ check_marketio_vector()

template<typename VectorType >
void check_marketio_vector ( )
139  {
140  Index size = internal::random<Index>(1, 100);
141  VectorType v1, v2;
142  v1 = VectorType::Random(size);
143  std::string filename = GetTestTempFilename("vector_extra.mtx");
147 }
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
bool loadMarketVector(VectorType &vec, const std::string &filename)
Same functionality as loadMarketDense, deprecated.
Definition: MarketIO.h:284
bool saveMarketVector(const VectorType &vec, const std::string &filename)
Same functionality as saveMarketDense, deprecated.
Definition: MarketIO.h:360
Definition: fft_test_shared.h:66

References MergeRestartFiles::filename, GetTestTempFilename(), Eigen::loadMarketVector(), Eigen::saveMarketVector(), size, oomph::Global_string_for_annotation::string(), v1(), v2(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ check_sparse_inverse()

template<typename Scalar >
void check_sparse_inverse ( )
174  {
176 
177  Matrix<Scalar, -1, -1> A;
178  A.resize(1000, 1000);
179  A.fill(0);
180  A.setIdentity();
181  A.col(0).array() += 1;
182  A.row(0).array() += 2;
183  A.col(2).array() += 3;
184  A.row(7).array() += 3;
185  A.col(9).array() += 3;
186  A.block(3, 4, 4, 2).array() += 9;
187  A.middleRows(10, 50).array() += 3;
188  A.middleCols(50, 50).array() += 40;
189  A.block(500, 300, 40, 20).array() += 10;
190  A.transposeInPlace();
191 
193  slu.compute(A.sparseView());
194  Matrix<Scalar, -1, -1> Id(A.rows(), A.cols());
195  Id.setIdentity();
196  Matrix<Scalar, -1, -1> inv = slu.solve(Id);
197 
198  const MatrixType sparseInv = Eigen::SparseInverse<Scalar>().compute(A.sparseView()).inverse();
199 
200  Scalar sumdiff = 0; // Check the diff only of the non-zero elements
201  for (Eigen::Index j = 0; j < A.cols(); j++) {
202  for (typename MatrixType::InnerIterator iter(sparseInv, j); iter; ++iter) {
203  const Scalar diff = std::abs(inv(iter.row(), iter.col()) - iter.value());
204  VERIFY_IS_APPROX_OR_LESS_THAN(diff, 1e-11);
205 
206  if (iter.value() != 0) {
207  sumdiff += diff;
208  }
209  }
210  }
211 
212  VERIFY_IS_APPROX_OR_LESS_THAN(sumdiff, 1e-10);
213 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
Array< double, 1, 3 > e(1./3., 0.5, 2.)
SCALAR Scalar
Definition: bench_gemm.cpp:45
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
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
calculate sparse subset of inverse of sparse matrix
Definition: SparseInverse.h:128
const MatrixType & inverse() const
return the already-calculated sparse inverse, or a 0x0 matrix if it could not be computed
Definition: SparseInverse.h:157
SparseInverse & compute(const SparseMatrix< Scalar > &A)
Calculate the sparse inverse from a given sparse input.
Definition: SparseInverse.h:147
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:151
void compute(const MatrixType &matrix)
Analyze and factorize the matrix so the solver is ready to solve.
Definition: SparseLU.h:210
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:84
#define VERIFY_IS_APPROX_OR_LESS_THAN(a, b)
Definition: main.h:373
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References abs(), Eigen::PlainObjectBase< Derived >::cols(), Eigen::SparseLU< MatrixType_, OrderingType_ >::compute(), Eigen::SparseInverse< Scalar >::compute(), e(), Eigen::SparseInverse< Scalar >::inverse(), j, Eigen::PlainObjectBase< Derived >::resize(), Eigen::PlainObjectBase< Derived >::rows(), Eigen::SparseSolverBase< Derived >::solve(), and VERIFY_IS_APPROX_OR_LESS_THAN.

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( sparse_extra  )
215  {
216  for (int i = 0; i < g_repeat; i++) {
217  int s = Eigen::internal::random<int>(1, 50);
219  CALL_SUBTEST_2(sparse_extra(SparseMatrix<std::complex<double> >(s, s)));
221 
224  CALL_SUBTEST_3((check_marketio<SparseMatrix<std::complex<float>, ColMajor, int> >()));
225  CALL_SUBTEST_3((check_marketio<SparseMatrix<std::complex<double>, ColMajor, int> >()));
228  CALL_SUBTEST_3((check_marketio<SparseMatrix<std::complex<float>, ColMajor, long int> >()));
229  CALL_SUBTEST_3((check_marketio<SparseMatrix<std::complex<double>, ColMajor, long int> >()));
230 
234  CALL_SUBTEST_4((check_marketio_dense<Matrix<std::complex<float>, Dynamic, Dynamic> >()));
235  CALL_SUBTEST_4((check_marketio_dense<Matrix<std::complex<double>, Dynamic, Dynamic> >()));
240 
243  CALL_SUBTEST_5((check_marketio_vector<Matrix<std::complex<float>, 1, Dynamic> >()));
244  CALL_SUBTEST_5((check_marketio_vector<Matrix<std::complex<double>, 1, Dynamic> >()));
247  CALL_SUBTEST_5((check_marketio_vector<Matrix<std::complex<float>, Dynamic, 1> >()));
248  CALL_SUBTEST_5((check_marketio_vector<Matrix<std::complex<double>, Dynamic, 1> >()));
249 
250  CALL_SUBTEST_6((check_sparse_inverse<double>()));
251 
253  }
254 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
@ ColMajor
Definition: Constants.h:318
RealScalar s
Definition: level1_cplx_impl.h:130
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:139
static int g_repeat
Definition: main.h:191
void check_marketio()
Definition: sparse_extra.cpp:126
void check_marketio_dense()
Definition: sparse_extra.cpp:150
void check_marketio_vector()
Definition: sparse_extra.cpp:139
void sparse_extra(const SparseMatrixType &ref)
Definition: sparse_extra.cpp:53
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, check_marketio(), check_marketio_dense(), check_marketio_vector(), Eigen::ColMajor, Eigen::Dynamic, Eigen::g_repeat, i, s, sparse_extra(), and TEST_SET_BUT_UNUSED_VARIABLE.

◆ GetTestTempFilename()

std::string GetTestTempFilename ( const char filename)
27  {
28  const char* test_tmpdir = std::getenv("TEST_TMPDIR");
29  if (test_tmpdir == nullptr) {
30  return std::string(filename);
31  }
32  return std::string(test_tmpdir) + std::string("/") + std::string(filename);
33 }

References MergeRestartFiles::filename, and oomph::Global_string_for_annotation::string().

Referenced by check_marketio(), check_marketio_dense(), and check_marketio_vector().

◆ sparse_extra()

template<typename SparseMatrixType >
void sparse_extra ( const SparseMatrixType &  ref)
53  {
54  const Index rows = ref.rows();
55  const Index cols = ref.cols();
56  typedef typename SparseMatrixType::Scalar Scalar;
57  enum { Flags = SparseMatrixType::Flags };
58 
59  double density = (std::max)(8. / (rows * cols), 0.01);
62  Scalar eps = 1e-6;
63 
64  SparseMatrixType m(rows, cols);
66  DenseVector vec1 = DenseVector::Random(rows);
67 
68  std::vector<Vector2i> zeroCoords;
69  std::vector<Vector2i> nonzeroCoords;
70  initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
71 
72  if (zeroCoords.size() == 0 || nonzeroCoords.size() == 0) return;
73 
74  // test coeff and coeffRef
75  for (int i = 0; i < (int)zeroCoords.size(); ++i) {
76  VERIFY_IS_MUCH_SMALLER_THAN(m.coeff(zeroCoords[i].x(), zeroCoords[i].y()), eps);
77  if (internal::is_same<SparseMatrixType, SparseMatrix<Scalar, Flags> >::value)
78  VERIFY_RAISES_ASSERT(m.coeffRef(zeroCoords[0].x(), zeroCoords[0].y()) = 5);
79  }
80  VERIFY_IS_APPROX(m, refMat);
81 
82  m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
83  refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
84 
85  VERIFY_IS_APPROX(m, refMat);
86 
87  // random setter
88  // {
89  // m.setZero();
90  // VERIFY_IS_NOT_APPROX(m, refMat);
91  // SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
92  // std::vector<Vector2i> remaining = nonzeroCoords;
93  // while(!remaining.empty())
94  // {
95  // int i = internal::random<int>(0,remaining.size()-1);
96  // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
97  // remaining[i] = remaining.back();
98  // remaining.pop_back();
99  // }
100  // }
101  // VERIFY_IS_APPROX(m, refMat);
102 
105 #ifdef EIGEN_GOOGLEHASH_SUPPORT
108 #endif
109 
110  // test RandomSetter
111  /*{
112  SparseMatrixType m1(rows,cols), m2(rows,cols);
113  DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
114  initSparse<Scalar>(density, refM1, m1);
115  {
116  Eigen::RandomSetter<SparseMatrixType > setter(m2);
117  for (int j=0; j<m1.outerSize(); ++j)
118  for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
119  setter(i.index(), j) = i.value();
120  }
121  VERIFY_IS_APPROX(m1, m2);
122  }*/
123 }
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
RowVectorXd vec1(3)
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:217
The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access.
Definition: RandomSetter.h:156
#define max(a, b)
Definition: datatypes.h:23
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
Scalar * y
Definition: level1_cplx_impl.h:128
return int(ret)+1
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY(a)
Definition: main.h:362
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
double eps
Definition: crbond_bessel.cc:24
squared absolute value
Definition: GlobalFunctions.h:87
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
density
Definition: UniformPSDSelfTest.py:19
double Zero
Definition: pseudosolid_node_update_elements.cc:35
list x
Definition: plotDoE.py:28
bool test_random_setter(SparseMatrix< Scalar, Options > &sm, const DenseType &ref, const std::vector< Vector2i > &nonzeroCoords)
Definition: sparse_extra.cpp:36

References Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::coeffRef(), cols, UniformPSDSelfTest::density, e(), CRBond_Bessel::eps, i, int(), m, max, rows, test_random_setter(), Eigen::value, vec1(), VERIFY, VERIFY_IS_APPROX, VERIFY_IS_MUCH_SMALLER_THAN, VERIFY_RAISES_ASSERT, plotDoE::x, y, and oomph::PseudoSolidHelper::Zero.

Referenced by EIGEN_DECLARE_TEST().

◆ test_random_setter()

template<typename SetterType , typename DenseType , typename Scalar , int Options>
bool test_random_setter ( SparseMatrix< Scalar, Options > &  sm,
const DenseType &  ref,
const std::vector< Vector2i > &  nonzeroCoords 
)
37  {
38  {
39  sm.setZero();
40  SetterType w(sm);
41  std::vector<Vector2i> remaining = nonzeroCoords;
42  while (!remaining.empty()) {
43  int i = internal::random<int>(0, static_cast<int>(remaining.size()) - 1);
44  w(remaining[i].x(), remaining[i].y()) = ref.coeff(remaining[i].x(), remaining[i].y());
45  remaining[i] = remaining.back();
46  remaining.pop_back();
47  }
48  }
49  return sm.isApprox(ref);
50 }
RowVector3d w
Definition: Matrix_resize_int.cpp:3
bool isApprox(const SparseMatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseFuzzy.h:20
void setZero()
Definition: SparseMatrix.h:303

References i, Eigen::SparseMatrixBase< Derived >::isApprox(), Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::setZero(), w, plotDoE::x, and y.

Referenced by sparse_extra().