cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

sparse_extra.cpp (8414B)


      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>
    167 void check_marketio()
    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");
    176   VERIFY_IS_EQUAL(DenseMatrix(m1),DenseMatrix(m2));
    177 }
    178 
    179 template<typename VectorType>
    180 void check_marketio_vector()
    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");
    187   VERIFY_IS_EQUAL(v1,v2);
    188 }
    189 
    190 EIGEN_DECLARE_TEST(sparse_extra)
    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 
    224     TEST_SET_BUT_UNUSED_VARIABLE(s);
    225   }
    226 }