• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 deprectaed DynamicSparseMatrix
12 #define EIGEN_NO_DEPRECATED_WARNING
13 #include "sparse_basic.cpp"
14 #include "sparse_product.cpp"
15 #include <Eigen/SparseExtra>
16 
17 template<typename SetterType,typename DenseType, typename Scalar, int Options>
test_random_setter(SparseMatrix<Scalar,Options> & sm,const DenseType & ref,const std::vector<Vector2i> & nonzeroCoords)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>
test_random_setter(DynamicSparseMatrix<T> & sm,const DenseType & ref,const std::vector<Vector2i> & nonzeroCoords)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 
sparse_extra(const SparseMatrixType & ref)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);
58   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
59   typedef Matrix<Scalar,Dynamic,1> DenseVector;
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 
103     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
104     #ifdef EIGEN_UNORDERED_MAP_SUPPORT
105     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
106     #endif
107     #ifdef _DENSE_HASH_MAP_H_
108     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
109     #endif
110     #ifdef _SPARSE_HASH_MAP_H_
111     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
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 
test_sparse_extra()132 void test_sparse_extra()
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 
144     CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
145     CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
146   }
147 }
148