sparse_vector.cpp File Reference
#include "sparse.h"

Functions

template<typename Scalar , typename StorageIndex >
void sparse_vector (int rows, int cols)
 
void test_pruning ()
 
 EIGEN_DECLARE_TEST (sparse_vector)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( sparse_vector  )
220  {
221  for (int i = 0; i < g_repeat; i++) {
222  int r = Eigen::internal::random<int>(1, 500), c = Eigen::internal::random<int>(1, 500);
223  if (Eigen::internal::random<int>(0, 4) == 0) {
224  r = c; // check square matrices in 25% of tries
225  }
227 
228  CALL_SUBTEST_1((sparse_vector<double, int>(8, 8)));
229  CALL_SUBTEST_2((sparse_vector<std::complex<double>, int>(r, c)));
230  CALL_SUBTEST_1((sparse_vector<double, long int>(r, c)));
231  CALL_SUBTEST_1((sparse_vector<double, short>(r, c)));
232  }
233 
235 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
static int g_repeat
Definition: main.h:191
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100
void test_pruning()
Definition: sparse_vector.cpp:193
void sparse_vector(int rows, int cols)
Definition: sparse_vector.cpp:13
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10

References calibrate::c, CALL_SUBTEST_1, CALL_SUBTEST_2, EIGEN_UNUSED_VARIABLE, Eigen::g_repeat, i, UniformPSDSelfTest::r, sparse_vector(), and test_pruning().

◆ sparse_vector()

template<typename Scalar , typename StorageIndex >
void sparse_vector ( int  rows,
int  cols 
)
13  {
14  double densityMat = (std::max)(8. / (rows * cols), 0.01);
15  double densityVec = (std::max)(8. / (rows), 0.1);
18  typedef Matrix<DenseIndex, Dynamic, 1> DenseIndexVector;
19  typedef SparseVector<Scalar, 0, StorageIndex> SparseVectorType;
20  typedef SparseMatrix<Scalar, 0, StorageIndex> SparseMatrixType;
21  Scalar eps = 1e-6;
22 
23  SparseMatrixType m1(rows, rows);
24  SparseVectorType v1(rows), v2(rows), v3(rows);
26  DenseVector refV1 = DenseVector::Random(rows), refV2 = DenseVector::Random(rows), refV3 = DenseVector::Random(rows);
27 
28  std::vector<int> zerocoords, nonzerocoords;
29  initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords);
30  initSparse<Scalar>(densityMat, refM1, m1);
31 
32  initSparse<Scalar>(densityVec, refV2, v2);
33  initSparse<Scalar>(densityVec, refV3, v3);
34 
35  Scalar s1 = internal::random<Scalar>();
36 
37  // test coeff and coeffRef
38  for (unsigned int i = 0; i < zerocoords.size(); ++i) {
39  VERIFY_IS_MUCH_SMALLER_THAN(v1.coeff(zerocoords[i]), eps);
40  // VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 );
41  }
42  {
43  VERIFY(int(nonzerocoords.size()) == v1.nonZeros());
44  int j = 0;
45  for (typename SparseVectorType::InnerIterator it(v1); it; ++it, ++j) {
46  VERIFY(nonzerocoords[j] == it.index());
47  VERIFY_IS_EQUAL(it.value(), v1.coeff(it.index()));
48  VERIFY_IS_EQUAL(it.value(), refV1.coeff(it.index()));
49  }
50  }
51  VERIFY_IS_APPROX(v1, refV1);
52 
53  // test coeffRef with reallocation
54  {
55  SparseVectorType v4(rows);
57  for (int k = 0; k < rows; ++k) {
58  int i = internal::random<int>(0, rows - 1);
59  Scalar v = internal::random<Scalar>();
60  v4.coeffRef(i) += v;
61  v5.coeffRef(i) += v;
62  }
63  VERIFY_IS_APPROX(v4, v5);
64  }
65 
66  v1.coeffRef(nonzerocoords[0]) = Scalar(5);
67  refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
68  VERIFY_IS_APPROX(v1, refV1);
69 
70  VERIFY_IS_APPROX(v1 + v2, refV1 + refV2);
71  VERIFY_IS_APPROX(v1 + v2 + v3, refV1 + refV2 + refV3);
72 
73  VERIFY_IS_APPROX(v1 * s1 - v2, refV1 * s1 - refV2);
74 
75  VERIFY_IS_APPROX(v1 *= s1, refV1 *= s1);
76  VERIFY_IS_APPROX(v1 /= s1, refV1 /= s1);
77 
78  VERIFY_IS_APPROX(v1 += v2, refV1 += refV2);
79  VERIFY_IS_APPROX(v1 -= v2, refV1 -= refV2);
80 
81  VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
82  VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
83 
84  VERIFY_IS_APPROX(m1 * v2, refM1 * refV2);
85  VERIFY_IS_APPROX(v1.dot(m1 * v2), refV1.dot(refM1 * refV2));
86  {
87  int i = internal::random<int>(0, rows - 1);
88  VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i)));
89  }
90 
91  VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
92 
93  VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm());
94 
95  // test aliasing
96  VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1));
97  VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval()));
98  VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1));
99 
100  // sparse matrix to sparse vector
101  SparseMatrixType mv1;
102  VERIFY_IS_APPROX((mv1 = v1), v1);
103  VERIFY_IS_APPROX(mv1, (v1 = mv1));
104  VERIFY_IS_APPROX(mv1, (v1 = mv1.transpose()));
105 
106  // check copy to dense vector with transpose
107  refV3.resize(0);
108  VERIFY_IS_APPROX(refV3 = v1.transpose(), v1.toDense());
109  VERIFY_IS_APPROX(DenseVector(v1), v1.toDense());
110 
111  // test move
112  {
113  SparseVectorType tmp(std::move(v1));
114  VERIFY_IS_APPROX(tmp, refV1);
115  v1 = tmp;
116  }
117 
118  {
119  SparseVectorType tmp;
120  tmp = std::move(v1);
121  VERIFY_IS_APPROX(tmp, refV1);
122  v1 = tmp;
123  }
124 
125  {
126  SparseVectorType tmp(std::move(mv1));
127  VERIFY_IS_APPROX(tmp, refV1);
128  mv1 = tmp;
129  }
130 
131  {
132  SparseVectorType tmp;
133  tmp = std::move(mv1);
134  VERIFY_IS_APPROX(tmp, refV1);
135  mv1 = tmp;
136  }
137 
138  // test conservative resize
139  {
140  std::vector<StorageIndex> inc;
141  if (rows > 3) inc.push_back(-3);
142  inc.push_back(0);
143  inc.push_back(3);
144  inc.push_back(1);
145  inc.push_back(10);
146 
147  for (std::size_t i = 0; i < inc.size(); i++) {
148  StorageIndex incRows = inc[i];
149  SparseVectorType vec1(rows);
151  initSparse<Scalar>(densityVec, refVec1, vec1);
152 
153  vec1.conservativeResize(rows + incRows);
154  refVec1.conservativeResize(rows + incRows);
155  if (incRows > 0) refVec1.tail(incRows).setZero();
156 
157  VERIFY_IS_APPROX(vec1, refVec1);
158 
159  // Insert new values
160  if (incRows > 0) vec1.insert(vec1.rows() - 1) = refVec1(refVec1.rows() - 1) = 1;
161 
162  VERIFY_IS_APPROX(vec1, refVec1);
163  }
164  }
165 
166  // test sort
167  if (rows > 1) {
168  SparseVectorType vec1(rows);
170  DenseIndexVector innerIndices(rows);
171  innerIndices.setLinSpaced(0, rows - 1);
172  std::random_device rd;
173  std::mt19937 g(rd());
174  std::shuffle(innerIndices.begin(), innerIndices.end(), g);
175  Index nz = internal::random<Index>(2, rows / 2);
176  for (Index k = 0; k < nz; k++) {
177  Index i = innerIndices[k];
178  Scalar val = internal::random<Scalar>();
179  refVec1.coeffRef(i) = val;
180  vec1.insert(i) = val;
181  }
182 
183  vec1.template sortInnerIndices<std::greater<>>();
184  VERIFY_IS_APPROX(vec1, refVec1);
185  VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::greater<>>(), 1);
186  VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::less<>>(), 0);
187  vec1.template sortInnerIndices<std::less<>>();
188  VERIFY_IS_APPROX(vec1, refVec1);
189  VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::greater<>>(), 0);
190  VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::less<>>(), 1);
191  }
192 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Matrix3d m1
Definition: IOFormat.cpp:2
RowVectorXd vec1(3)
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:217
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:198
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResize(Index rows, Index cols)
Definition: PlainObjectBase.h:398
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
a sparse vector class
Definition: SparseVector.h:62
#define max(a, b)
Definition: datatypes.h:23
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
char char char int int * k
Definition: level2_impl.h:374
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
#define VERIFY(a)
Definition: main.h:362
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
double eps
Definition: crbond_bessel.cc:24
EIGEN_STRONG_INLINE Packet2d shuffle(const Packet2d &m, const Packet2d &n, int mask)
Definition: LSX/PacketMath.h:150
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const unsigned nz
Definition: ConstraintElementsUnitTest.cpp:32
val
Definition: calibrate.py:119
double Zero
Definition: pseudosolid_node_update_elements.cc:35
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::PlainObjectBase< Derived >::coeff(), Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::coeffRef(), cols, Eigen::PlainObjectBase< Derived >::conservativeResize(), e(), CRBond_Bessel::eps, i, j, k, m1, max, Mesh_Parameters::nz, rows, Eigen::PlainObjectBase< Derived >::rows(), Eigen::PlainObjectBase< Derived >::setZero(), Eigen::internal::shuffle(), tmp, v, v1(), v2(), calibrate::val, vec1(), VERIFY, VERIFY_IS_APPROX, VERIFY_IS_EQUAL, VERIFY_IS_MUCH_SMALLER_THAN, and oomph::PseudoSolidHelper::Zero.

Referenced by EIGEN_DECLARE_TEST().

◆ test_pruning()

void test_pruning ( )
193  {
194  using SparseVectorType = SparseVector<double, 0, int>;
195 
196  SparseVectorType vec;
197  auto init_vec = [&]() {
198  ;
199  vec.resize(10);
200  vec.insert(3) = 0.1;
201  vec.insert(5) = 1.0;
202  vec.insert(8) = -0.1;
203  vec.insert(9) = -0.2;
204  };
205  init_vec();
206 
207  VERIFY_IS_EQUAL(vec.nonZeros(), 4);
208  VERIFY_IS_EQUAL(vec.prune(0.1, 1.0), 2);
209  VERIFY_IS_EQUAL(vec.nonZeros(), 2);
210  VERIFY_IS_EQUAL(vec.coeff(5), 1.0);
211  VERIFY_IS_EQUAL(vec.coeff(9), -0.2);
212 
213  init_vec();
214  VERIFY_IS_EQUAL(vec.prune([](double v) { return v >= 0; }), 2);
215  VERIFY_IS_EQUAL(vec.nonZeros(), 2);
216  VERIFY_IS_EQUAL(vec.coeff(3), 0.1);
217  VERIFY_IS_EQUAL(vec.coeff(5), 1.0);
218 }
void resize(Index rows, Index cols)
Definition: SparseVector.h:225

References Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::resize(), v, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().