Home | History | Annotate | Download | only in test
      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 (at) 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>
     18 bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
     19 {
     20   typedef SparseMatrix<Scalar,Options> SparseType;
     21   {
     22     sm.setZero();
     23     SetterType w(sm);
     24     std::vector<Vector2i> remaining = nonzeroCoords;
     25     while(!remaining.empty())
     26     {
     27       int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
     28       w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
     29       remaining[i] = remaining.back();
     30       remaining.pop_back();
     31     }
     32   }
     33   return sm.isApprox(ref);
     34 }
     35 
     36 template<typename SetterType,typename DenseType, typename T>
     37 bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
     38 {
     39   sm.setZero();
     40   std::vector<Vector2i> remaining = nonzeroCoords;
     41   while(!remaining.empty())
     42   {
     43     int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
     44     sm.coeffRef(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   return sm.isApprox(ref);
     49 }
     50 
     51 template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
     52 {
     53   typedef typename SparseMatrixType::Index Index;
     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);
     60   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
     61   typedef Matrix<Scalar,Dynamic,1> DenseVector;
     62   Scalar eps = 1e-6;
     63 
     64   SparseMatrixType m(rows, cols);
     65   DenseMatrix refMat = DenseMatrix::Zero(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)
     73     return;
     74 
     75   // test coeff and coeffRef
     76   for (int i=0; i<(int)zeroCoords.size(); ++i)
     77   {
     78     VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
     79     if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
     80       VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
     81   }
     82   VERIFY_IS_APPROX(m, refMat);
     83 
     84   m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
     85   refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
     86 
     87   VERIFY_IS_APPROX(m, refMat);
     88 
     89   // random setter
     90 //   {
     91 //     m.setZero();
     92 //     VERIFY_IS_NOT_APPROX(m, refMat);
     93 //     SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
     94 //     std::vector<Vector2i> remaining = nonzeroCoords;
     95 //     while(!remaining.empty())
     96 //     {
     97 //       int i = internal::random<int>(0,remaining.size()-1);
     98 //       w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
     99 //       remaining[i] = remaining.back();
    100 //       remaining.pop_back();
    101 //     }
    102 //   }
    103 //   VERIFY_IS_APPROX(m, refMat);
    104 
    105     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
    106     #ifdef EIGEN_UNORDERED_MAP_SUPPORT
    107     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
    108     #endif
    109     #ifdef _DENSE_HASH_MAP_H_
    110     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
    111     #endif
    112     #ifdef _SPARSE_HASH_MAP_H_
    113     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
    114     #endif
    115 
    116 
    117   // test RandomSetter
    118   /*{
    119     SparseMatrixType m1(rows,cols), m2(rows,cols);
    120     DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
    121     initSparse<Scalar>(density, refM1, m1);
    122     {
    123       Eigen::RandomSetter<SparseMatrixType > setter(m2);
    124       for (int j=0; j<m1.outerSize(); ++j)
    125         for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
    126           setter(i.index(), j) = i.value();
    127     }
    128     VERIFY_IS_APPROX(m1, m2);
    129   }*/
    130 
    131 
    132 }
    133 
    134 void test_sparse_extra()
    135 {
    136   for(int i = 0; i < g_repeat; i++) {
    137     int s = Eigen::internal::random<int>(1,50);
    138     CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
    139     CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(s, s)) );
    140     CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(s, s)) );
    141 
    142     CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(s, s)) );
    143 //    CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double>(s, s)) ));
    144 //    CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double,ColMajor,long int>(s, s)) ));
    145 
    146     CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
    147     CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
    148   }
    149 }
    150