sparse_extra.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2010 Gael Guennebaud <g.gael@free.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 
00011 // import basic and product tests for deprectaed DynamicSparseMatrix
00012 #define EIGEN_NO_DEPRECATED_WARNING
00013 #include "sparse_basic.cpp"
00014 #include "sparse_product.cpp"
00015 #include <Eigen/SparseExtra>
00016 
00017 template<typename SetterType,typename DenseType, typename Scalar, int Options>
00018 bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
00019 {
00020   {
00021     sm.setZero();
00022     SetterType w(sm);
00023     std::vector<Vector2i> remaining = nonzeroCoords;
00024     while(!remaining.empty())
00025     {
00026       int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
00027       w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
00028       remaining[i] = remaining.back();
00029       remaining.pop_back();
00030     }
00031   }
00032   return sm.isApprox(ref);
00033 }
00034 
00035 template<typename SetterType,typename DenseType, typename T>
00036 bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
00037 {
00038   sm.setZero();
00039   std::vector<Vector2i> remaining = nonzeroCoords;
00040   while(!remaining.empty())
00041   {
00042     int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
00043     sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
00044     remaining[i] = remaining.back();
00045     remaining.pop_back();
00046   }
00047   return sm.isApprox(ref);
00048 }
00049 
00050 template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
00051 {
00052   typedef typename SparseMatrixType::Index Index;
00053   const Index rows = ref.rows();
00054   const Index cols = ref.cols();
00055   typedef typename SparseMatrixType::Scalar Scalar;
00056   enum { Flags = SparseMatrixType::Flags };
00057 
00058   double density = (std::max)(8./(rows*cols), 0.01);
00059   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
00060   typedef Matrix<Scalar,Dynamic,1> DenseVector;
00061   Scalar eps = 1e-6;
00062 
00063   SparseMatrixType m(rows, cols);
00064   DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
00065   DenseVector vec1 = DenseVector::Random(rows);
00066 
00067   std::vector<Vector2i> zeroCoords;
00068   std::vector<Vector2i> nonzeroCoords;
00069   initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
00070 
00071   if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
00072     return;
00073 
00074   // test coeff and coeffRef
00075   for (int i=0; i<(int)zeroCoords.size(); ++i)
00076   {
00077     VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
00078     if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
00079       VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
00080   }
00081   VERIFY_IS_APPROX(m, refMat);
00082 
00083   m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
00084   refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
00085 
00086   VERIFY_IS_APPROX(m, refMat);
00087 
00088   // random setter
00089 //   {
00090 //     m.setZero();
00091 //     VERIFY_IS_NOT_APPROX(m, refMat);
00092 //     SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
00093 //     std::vector<Vector2i> remaining = nonzeroCoords;
00094 //     while(!remaining.empty())
00095 //     {
00096 //       int i = internal::random<int>(0,remaining.size()-1);
00097 //       w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
00098 //       remaining[i] = remaining.back();
00099 //       remaining.pop_back();
00100 //     }
00101 //   }
00102 //   VERIFY_IS_APPROX(m, refMat);
00103 
00104     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
00105     #ifdef EIGEN_UNORDERED_MAP_SUPPORT
00106     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
00107     #endif
00108     #ifdef _DENSE_HASH_MAP_H_
00109     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
00110     #endif
00111     #ifdef _SPARSE_HASH_MAP_H_
00112     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
00113     #endif
00114 
00115 
00116   // test RandomSetter
00117   /*{
00118     SparseMatrixType m1(rows,cols), m2(rows,cols);
00119     DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
00120     initSparse<Scalar>(density, refM1, m1);
00121     {
00122       Eigen::RandomSetter<SparseMatrixType > setter(m2);
00123       for (int j=0; j<m1.outerSize(); ++j)
00124         for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
00125           setter(i.index(), j) = i.value();
00126     }
00127     VERIFY_IS_APPROX(m1, m2);
00128   }*/
00129 
00130 
00131 }
00132 
00133 void test_sparse_extra()
00134 {
00135   for(int i = 0; i < g_repeat; i++) {
00136     int s = Eigen::internal::random<int>(1,50);
00137     CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
00138     CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(s, s)) );
00139     CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(s, s)) );
00140 
00141     CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(s, s)) );
00142 //    CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double>(s, s)) ));
00143 //    CALL_SUBTEST_3(( sparse_basic(DynamicSparseMatrix<double,ColMajor,long int>(s, s)) ));
00144 
00145     CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
00146     CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
00147   }
00148 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:36:02