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 #if 0 // sparse_basic(DynamicSparseMatrix) does not compile at all -> disabled
13 static long g_realloc_count = 0;
14 #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++;
15 
16 static long g_dense_op_sparse_count = 0;
17 #define EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN g_dense_op_sparse_count++;
18 #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN g_dense_op_sparse_count+=10;
19 #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN g_dense_op_sparse_count+=20;
20 
21 #define EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA 1
22 #endif
23 
24 #define EIGEN_NO_DEPRECATED_WARNING
25 // Disable counting of temporaries, since sparse_product(DynamicSparseMatrix)
26 // has an extra copy-assignment.
27 #define EIGEN_SPARSE_PRODUCT_IGNORE_TEMPORARY_COUNT
28 #include "sparse_product.cpp"
29 
30 #if 0 // sparse_basic(DynamicSparseMatrix) does not compile at all -> disabled
31 #include "sparse_basic.cpp"
32 #endif
33 
34 #if EIGEN_HAS_CXX11
35 
36 #ifdef min
37 #undef min
38 #endif
39 
40 #ifdef max
41 #undef max
42 #endif
43 
44 #include <unordered_map>
45 #define EIGEN_UNORDERED_MAP_SUPPORT
46 
47 #endif
48 
49 
50 #include <Eigen/SparseExtra>
51 
52 template<typename SetterType,typename DenseType, typename Scalar, int Options>
53 bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
54 {
55  {
56  sm.setZero();
57  SetterType w(sm);
58  std::vector<Vector2i> remaining = nonzeroCoords;
59  while(!remaining.empty())
60  {
61  int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
62  w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
63  remaining[i] = remaining.back();
64  remaining.pop_back();
65  }
66  }
67  return sm.isApprox(ref);
68 }
69 
70 template<typename SetterType,typename DenseType, typename T>
71 bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
72 {
73  sm.setZero();
74  std::vector<Vector2i> remaining = nonzeroCoords;
75  while(!remaining.empty())
76  {
77  int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
78  sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
79  remaining[i] = remaining.back();
80  remaining.pop_back();
81  }
82  return sm.isApprox(ref);
83 }
84 
85 template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
86 {
87  const Index rows = ref.rows();
88  const Index cols = ref.cols();
89  typedef typename SparseMatrixType::Scalar Scalar;
90  enum { Flags = SparseMatrixType::Flags };
91 
92  double density = (std::max)(8./(rows*cols), 0.01);
93  typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
94  typedef Matrix<Scalar,Dynamic,1> DenseVector;
95  Scalar eps = 1e-6;
96 
97  SparseMatrixType m(rows, cols);
98  DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
99  DenseVector vec1 = DenseVector::Random(rows);
100 
101  std::vector<Vector2i> zeroCoords;
102  std::vector<Vector2i> nonzeroCoords;
103  initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
104 
105  if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
106  return;
107 
108  // test coeff and coeffRef
109  for (int i=0; i<(int)zeroCoords.size(); ++i)
110  {
111  VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
112  if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
113  VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
114  }
115  VERIFY_IS_APPROX(m, refMat);
116 
117  m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
118  refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
119 
120  VERIFY_IS_APPROX(m, refMat);
121 
122  // random setter
123 // {
124 // m.setZero();
125 // VERIFY_IS_NOT_APPROX(m, refMat);
126 // SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
127 // std::vector<Vector2i> remaining = nonzeroCoords;
128 // while(!remaining.empty())
129 // {
130 // int i = internal::random<int>(0,remaining.size()-1);
131 // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
132 // remaining[i] = remaining.back();
133 // remaining.pop_back();
134 // }
135 // }
136 // VERIFY_IS_APPROX(m, refMat);
137 
138  VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
139  #ifdef EIGEN_UNORDERED_MAP_SUPPORT
140  VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
141  #endif
142  #ifdef EIGEN_GOOGLEHASH_SUPPORT
143  VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
144  VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
145  #endif
146 
147 
148  // test RandomSetter
149  /*{
150  SparseMatrixType m1(rows,cols), m2(rows,cols);
151  DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
152  initSparse<Scalar>(density, refM1, m1);
153  {
154  Eigen::RandomSetter<SparseMatrixType > setter(m2);
155  for (int j=0; j<m1.outerSize(); ++j)
156  for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
157  setter(i.index(), j) = i.value();
158  }
159  VERIFY_IS_APPROX(m1, m2);
160  }*/
161 
162 
163 }
164 
165 
166 template<typename SparseMatrixType>
168 {
169  typedef Matrix<typename SparseMatrixType::Scalar, Dynamic, Dynamic> DenseMatrix;
170  Index rows = internal::random<Index>(1,100);
171  Index cols = internal::random<Index>(1,100);
172  SparseMatrixType m1, m2;
173  m1 = DenseMatrix::Random(rows, cols).sparseView();
174  saveMarket(m1, "sparse_extra.mtx");
175  loadMarket(m2, "sparse_extra.mtx");
177 }
178 
179 template<typename VectorType>
181 {
182  Index size = internal::random<Index>(1,100);
183  VectorType v1, v2;
184  v1 = VectorType::Random(size);
185  saveMarketVector(v1, "vector_extra.mtx");
186  loadMarketVector(v2, "vector_extra.mtx");
188 }
189 
191 {
192  for(int i = 0; i < g_repeat; i++) {
193  int s = Eigen::internal::random<int>(1,50);
194  CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
195  CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(s, s)) );
196  CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(s, s)) );
197 
198  CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(s, s)) );
199 // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double>(s, s)) ));
200 // CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double,ColMajor,long int>(s, s)) ));
201 
202  CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
203  CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
204 
205  CALL_SUBTEST_4( (check_marketio<SparseMatrix<float,ColMajor,int> >()) );
206  CALL_SUBTEST_4( (check_marketio<SparseMatrix<double,ColMajor,int> >()) );
207  CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<float>,ColMajor,int> >()) );
208  CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<double>,ColMajor,int> >()) );
209  CALL_SUBTEST_4( (check_marketio<SparseMatrix<float,ColMajor,long int> >()) );
210  CALL_SUBTEST_4( (check_marketio<SparseMatrix<double,ColMajor,long int> >()) );
211  CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<float>,ColMajor,long int> >()) );
212  CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<double>,ColMajor,long int> >()) );
213 
214 
215  CALL_SUBTEST_5( (check_marketio_vector<Matrix<float,1,Dynamic> >()) );
216  CALL_SUBTEST_5( (check_marketio_vector<Matrix<double,1,Dynamic> >()) );
217  CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<float>,1,Dynamic> >()) );
218  CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<double>,1,Dynamic> >()) );
219  CALL_SUBTEST_5( (check_marketio_vector<Matrix<float,Dynamic,1> >()) );
220  CALL_SUBTEST_5( (check_marketio_vector<Matrix<double,Dynamic,1> >()) );
221  CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<float>,Dynamic,1> >()) );
222  CALL_SUBTEST_5( (check_marketio_vector<Matrix<std::complex<double>,Dynamic,1> >()) );
223 
225  }
226 }
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
x
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
g_dense_op_sparse_count
static long g_dense_op_sparse_count
Definition: sparse_basic.cpp:16
m1
Matrix3d m1
Definition: IOFormat.cpp:2
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Eigen::loadMarketVector
bool loadMarketVector(VectorType &vec, const std::string &filename)
Definition: MarketIO.h:200
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
Eigen::saveMarketVector
bool saveMarketVector(const VectorType &vec, const std::string &filename)
Definition: MarketIO.h:258
ceres::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Definition: gtsam/3rdparty/ceres/eigen.h:42
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
sparse_basic.cpp
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
sparse_product
void sparse_product()
Definition: test/sparse_product.cpp:36
density
Definition: testGaussianConditional.cpp:127
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
DenseVector
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
y
Scalar * y
Definition: level1_cplx_impl.h:124
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
check_marketio
void check_marketio()
Definition: sparse_extra.cpp:167
check_marketio_vector
void check_marketio_vector()
Definition: sparse_extra.cpp:180
Eigen::saveMarket
bool saveMarket(const SparseMatrixType &mat, const std::string &filename, int sym=0)
Definition: MarketIO.h:232
v2
Vector v2
Definition: testSerializationBase.cpp:39
ref
Reference counting helper.
Definition: object.h:67
sparse_extra
void sparse_extra(const SparseMatrixType &ref)
Definition: sparse_extra.cpp:85
DenseMatrix
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
g_realloc_count
static long g_realloc_count
Definition: sparse_basic.cpp:13
VectorType
Definition: FFTW.cpp:65
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(sparse_extra)
Definition: sparse_extra.cpp:190
Eigen::loadMarket
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Definition: MarketIO.h:134
test_random_setter
bool test_random_setter(SparseMatrix< Scalar, Options > &sm, const DenseType &ref, const std::vector< Vector2i > &nonzeroCoords)
Definition: sparse_extra.cpp:53
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
max
#define max(a, b)
Definition: datatypes.h:20
test_callbacks.value
value
Definition: test_callbacks.py:160
vec1
RowVectorXd vec1(3)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:24