sparse_vector.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 Daniel Gomez Ferro <dgomezferro@gmail.com>
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 
00027 template<typename Scalar> void sparse_vector(int rows, int cols)
00028 {
00029   double densityMat = std::max(8./(rows*cols), 0.01);
00030   double densityVec = std::max(8./float(rows), 0.1);
00031   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
00032   typedef Matrix<Scalar,Dynamic,1> DenseVector;
00033   typedef SparseVector<Scalar> SparseVectorType;
00034   typedef SparseMatrix<Scalar> SparseMatrixType;
00035   Scalar eps = 1e-6;
00036 
00037   SparseMatrixType m1(rows,cols);
00038   SparseVectorType v1(rows), v2(rows), v3(rows);
00039   DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
00040   DenseVector refV1 = DenseVector::Random(rows),
00041     refV2 = DenseVector::Random(rows),
00042     refV3 = DenseVector::Random(rows);
00043 
00044   std::vector<int> zerocoords, nonzerocoords;
00045   initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords);
00046   initSparse<Scalar>(densityMat, refM1, m1);
00047 
00048   initSparse<Scalar>(densityVec, refV2, v2);
00049   initSparse<Scalar>(densityVec, refV3, v3);
00050 
00051   Scalar s1 = internal::random<Scalar>();
00052 
00053   // test coeff and coeffRef
00054   for (unsigned int i=0; i<zerocoords.size(); ++i)
00055   {
00056     VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps );
00057     //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 );
00058   }
00059   {
00060     VERIFY(int(nonzerocoords.size()) == v1.nonZeros());
00061     int j=0;
00062     for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j)
00063     {
00064       VERIFY(nonzerocoords[j]==it.index());
00065       VERIFY(it.value()==v1.coeff(it.index()));
00066       VERIFY(it.value()==refV1.coeff(it.index()));
00067     }
00068   }
00069   VERIFY_IS_APPROX(v1, refV1);
00070 
00071   v1.coeffRef(nonzerocoords[0]) = Scalar(5);
00072   refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
00073   VERIFY_IS_APPROX(v1, refV1);
00074 
00075   VERIFY_IS_APPROX(v1+v2, refV1+refV2);
00076   VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
00077 
00078   VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
00079 
00080   VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
00081   VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
00082 
00083   VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
00084   VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
00085 
00086   VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
00087   VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
00088 
00089   VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
00090 
00091 }
00092 
00093 void test_sparse_vector()
00094 {
00095   for(int i = 0; i < g_repeat; i++) {
00096     CALL_SUBTEST_1( sparse_vector<double>(8, 8) );
00097     CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) );
00098     CALL_SUBTEST_1( sparse_vector<double>(299, 535) );
00099   }
00100 }
00101 


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:33:32