sparse_extra.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 Gael Guennebaud <g.gael@free.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 
11 // import basic and product tests for deprecated DynamicSparseMatrix
12 #define EIGEN_NO_DEPRECATED_WARNING
13 #include "sparse_product.cpp"
14 #include "sparse_basic.cpp"
15 #include <Eigen/SparseExtra>
16 
17 template<typename SetterType,typename DenseType, typename Scalar, int Options>
18 bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
19 {
20  {
21  sm.setZero();
22  SetterType w(sm);
23  std::vector<Vector2i> remaining = nonzeroCoords;
24  while(!remaining.empty())
25  {
26  int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
27  w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
28  remaining[i] = remaining.back();
29  remaining.pop_back();
30  }
31  }
32  return sm.isApprox(ref);
33 }
34 
35 template<typename SetterType,typename DenseType, typename T>
36 bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
37 {
38  sm.setZero();
39  std::vector<Vector2i> remaining = nonzeroCoords;
40  while(!remaining.empty())
41  {
42  int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
43  sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
44  remaining[i] = remaining.back();
45  remaining.pop_back();
46  }
47  return sm.isApprox(ref);
48 }
49 
50 template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
51 {
52  const Index rows = ref.rows();
53  const Index cols = ref.cols();
54  typedef typename SparseMatrixType::Scalar Scalar;
55  enum { Flags = SparseMatrixType::Flags };
56 
57  double density = (std::max)(8./(rows*cols), 0.01);
60  Scalar eps = 1e-6;
61 
62  SparseMatrixType m(rows, cols);
63  DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
64  DenseVector vec1 = DenseVector::Random(rows);
65 
66  std::vector<Vector2i> zeroCoords;
67  std::vector<Vector2i> nonzeroCoords;
68  initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
69 
70  if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
71  return;
72 
73  // test coeff and coeffRef
74  for (int i=0; i<(int)zeroCoords.size(); ++i)
75  {
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 
104  #ifdef EIGEN_UNORDERED_MAP_SUPPORT
106  #endif
107  #ifdef _DENSE_HASH_MAP_H_
109  #endif
110  #ifdef _SPARSE_HASH_MAP_H_
112  #endif
113 
114 
115  // test RandomSetter
116  /*{
117  SparseMatrixType m1(rows,cols), m2(rows,cols);
118  DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
119  initSparse<Scalar>(density, refM1, m1);
120  {
121  Eigen::RandomSetter<SparseMatrixType > setter(m2);
122  for (int j=0; j<m1.outerSize(); ++j)
123  for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
124  setter(i.index(), j) = i.value();
125  }
126  VERIFY_IS_APPROX(m1, m2);
127  }*/
128 
129 
130 }
131 
133 {
134  for(int i = 0; i < g_repeat; i++) {
135  int s = Eigen::internal::random<int>(1,50);
136  CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
137  CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(s, s)) );
138  CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(s, s)) );
139 
140  CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(s, s)) );
141 // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double>(s, s)) ));
142 // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double,ColMajor,long int>(s, s)) ));
143 
146  }
147 }
Matrix3f m
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:285
#define max(a, b)
Definition: datatypes.h:20
Scalar * y
return int(ret)+1
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
void sparse_extra(const SparseMatrixType &ref)
Matrix< Scalar, Dynamic, 1 > DenseVector
#define VERIFY_IS_APPROX(a, b)
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
A sparse matrix class designed for matrix assembly purpose.
Definition: SparseUtil.h:53
bool isApprox(const SparseMatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
RowVector3d w
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:335
bool test_random_setter(SparseMatrix< Scalar, Options > &sm, const DenseType &ref, const std::vector< Vector2i > &nonzeroCoords)
RowVectorXd vec1(3)
#define VERIFY(a)
Definition: main.h:325
The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access...
Definition: RandomSetter.h:154
void test_sparse_extra()
void sparse_product()
Scalar & coeffRef(Index row, Index col)
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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:44:17