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 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "sparse.h"
00026 #include <Eigen/SparseExtra>
00027 
00028 template<typename SetterType,typename DenseType, typename Scalar, int Options>
00029 bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
00030 {
00031   typedef SparseMatrix<Scalar,Options> SparseType;
00032   {
00033     sm.setZero();
00034     SetterType w(sm);
00035     std::vector<Vector2i> remaining = nonzeroCoords;
00036     while(!remaining.empty())
00037     {
00038       int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
00039       w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
00040       remaining[i] = remaining.back();
00041       remaining.pop_back();
00042     }
00043   }
00044   return sm.isApprox(ref);
00045 }
00046 
00047 template<typename SetterType,typename DenseType, typename T>
00048 bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
00049 {
00050   sm.setZero();
00051   std::vector<Vector2i> remaining = nonzeroCoords;
00052   while(!remaining.empty())
00053   {
00054     int i = internal::random<int>(0,static_cast<int>(remaining.size())-1);
00055     sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
00056     remaining[i] = remaining.back();
00057     remaining.pop_back();
00058   }
00059   return sm.isApprox(ref);
00060 }
00061 
00062 template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& ref)
00063 {
00064   typedef typename SparseMatrixType::Index Index;
00065   const Index rows = ref.rows();
00066   const Index cols = ref.cols();
00067   typedef typename SparseMatrixType::Scalar Scalar;
00068   enum { Flags = SparseMatrixType::Flags };
00069 
00070   double density = std::max(8./(rows*cols), 0.01);
00071   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
00072   typedef Matrix<Scalar,Dynamic,1> DenseVector;
00073   Scalar eps = 1e-6;
00074 
00075   SparseMatrixType m(rows, cols);
00076   DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
00077   DenseVector vec1 = DenseVector::Random(rows);
00078 
00079   std::vector<Vector2i> zeroCoords;
00080   std::vector<Vector2i> nonzeroCoords;
00081   initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
00082 
00083   if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
00084     return;
00085 
00086   // test coeff and coeffRef
00087   for (int i=0; i<(int)zeroCoords.size(); ++i)
00088   {
00089     VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
00090     if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
00091       VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
00092   }
00093   VERIFY_IS_APPROX(m, refMat);
00094 
00095   m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
00096   refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
00097 
00098   VERIFY_IS_APPROX(m, refMat);
00099 
00100   // random setter
00101 //   {
00102 //     m.setZero();
00103 //     VERIFY_IS_NOT_APPROX(m, refMat);
00104 //     SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
00105 //     std::vector<Vector2i> remaining = nonzeroCoords;
00106 //     while(!remaining.empty())
00107 //     {
00108 //       int i = internal::random<int>(0,remaining.size()-1);
00109 //       w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
00110 //       remaining[i] = remaining.back();
00111 //       remaining.pop_back();
00112 //     }
00113 //   }
00114 //   VERIFY_IS_APPROX(m, refMat);
00115 
00116     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
00117     #ifdef EIGEN_UNORDERED_MAP_SUPPORT
00118     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
00119     #endif
00120     #ifdef _DENSE_HASH_MAP_H_
00121     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
00122     #endif
00123     #ifdef _SPARSE_HASH_MAP_H_
00124     VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
00125     #endif
00126 
00127 
00128   // test RandomSetter
00129   /*{
00130     SparseMatrixType m1(rows,cols), m2(rows,cols);
00131     DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
00132     initSparse<Scalar>(density, refM1, m1);
00133     {
00134       Eigen::RandomSetter<SparseMatrixType > setter(m2);
00135       for (int j=0; j<m1.outerSize(); ++j)
00136         for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
00137           setter(i.index(), j) = i.value();
00138     }
00139     VERIFY_IS_APPROX(m1, m2);
00140   }*/
00141 
00142 
00143 }
00144 
00145 void test_sparse_extra()
00146 {
00147   for(int i = 0; i < g_repeat; i++) {
00148     CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(8, 8)) );
00149     CALL_SUBTEST_2( sparse_extra(SparseMatrix<std::complex<double> >(16, 16)) );
00150     CALL_SUBTEST_1( sparse_extra(SparseMatrix<double>(33, 33)) );
00151 
00152     CALL_SUBTEST_3( sparse_extra(DynamicSparseMatrix<double>(8, 8)) );
00153   }
00154 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:45