sparse.h
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 #ifndef EIGEN_TESTSPARSE_H
00026 
00027 #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
00028 
00029 #include "main.h"
00030 
00031 #if EIGEN_GNUC_AT_LEAST(4,0) && !defined __ICC && !defined(__clang__)
00032 #include <tr1/unordered_map>
00033 #define EIGEN_UNORDERED_MAP_SUPPORT
00034 namespace std {
00035   using std::tr1::unordered_map;
00036 }
00037 #endif
00038 
00039 #ifdef EIGEN_GOOGLEHASH_SUPPORT
00040   #include <google/sparse_hash_map>
00041 #endif
00042 
00043 #include <Eigen/Cholesky>
00044 #include <Eigen/LU>
00045 #include <Eigen/Sparse>
00046 
00047 enum {
00048   ForceNonZeroDiag = 1,
00049   MakeLowerTriangular = 2,
00050   MakeUpperTriangular = 4,
00051   ForceRealDiag = 8
00052 };
00053 
00054 /* Initializes both a sparse and dense matrix with same random values,
00055  * and a ratio of \a density non zero entries.
00056  * \param flags is a union of ForceNonZeroDiag, MakeLowerTriangular and MakeUpperTriangular
00057  *        allowing to control the shape of the matrix.
00058  * \param zeroCoords and nonzeroCoords allows to get the coordinate lists of the non zero,
00059  *        and zero coefficients respectively.
00060  */
00061 template<typename Scalar> void
00062 initSparse(double density,
00063            Matrix<Scalar,Dynamic,Dynamic>& refMat,
00064            SparseMatrix<Scalar>& sparseMat,
00065            int flags = 0,
00066            std::vector<Vector2i>* zeroCoords = 0,
00067            std::vector<Vector2i>* nonzeroCoords = 0)
00068 {
00069   sparseMat.setZero();
00070   sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
00071   for(int j=0; j<refMat.cols(); j++)
00072   {
00073     sparseMat.startVec(j);
00074     for(int i=0; i<refMat.rows(); i++)
00075     {
00076       Scalar v = (internal::random<double>(0,1) < density) ? internal::random<Scalar>() : Scalar(0);
00077       if ((flags&ForceNonZeroDiag) && (i==j))
00078       {
00079         v = internal::random<Scalar>()*Scalar(3.);
00080         v = v*v + Scalar(5.);
00081       }
00082       if ((flags & MakeLowerTriangular) && j>i)
00083         v = Scalar(0);
00084       else if ((flags & MakeUpperTriangular) && j<i)
00085         v = Scalar(0);
00086 
00087       if ((flags&ForceRealDiag) && (i==j))
00088         v = internal::real(v);
00089 
00090       if (v!=Scalar(0))
00091       {
00092         sparseMat.insertBackByOuterInner(j,i) = v;
00093         if (nonzeroCoords)
00094           nonzeroCoords->push_back(Vector2i(i,j));
00095       }
00096       else if (zeroCoords)
00097       {
00098         zeroCoords->push_back(Vector2i(i,j));
00099       }
00100       refMat(i,j) = v;
00101     }
00102   }
00103   sparseMat.finalize();
00104 }
00105 
00106 template<typename Scalar> void
00107 initSparse(double density,
00108            Matrix<Scalar,Dynamic,Dynamic>& refMat,
00109            DynamicSparseMatrix<Scalar>& sparseMat,
00110            int flags = 0,
00111            std::vector<Vector2i>* zeroCoords = 0,
00112            std::vector<Vector2i>* nonzeroCoords = 0)
00113 {
00114   sparseMat.setZero();
00115   sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
00116   for(int j=0; j<refMat.cols(); j++)
00117   {
00118     sparseMat.startVec(j); // not needed for DynamicSparseMatrix
00119     for(int i=0; i<refMat.rows(); i++)
00120     {
00121       Scalar v = (internal::random<double>(0,1) < density) ? internal::random<Scalar>() : Scalar(0);
00122       if ((flags&ForceNonZeroDiag) && (i==j))
00123       {
00124         v = internal::random<Scalar>()*Scalar(3.);
00125         v = v*v + Scalar(5.);
00126       }
00127       if ((flags & MakeLowerTriangular) && j>i)
00128         v = Scalar(0);
00129       else if ((flags & MakeUpperTriangular) && j<i)
00130         v = Scalar(0);
00131 
00132       if ((flags&ForceRealDiag) && (i==j))
00133         v = internal::real(v);
00134 
00135       if (v!=Scalar(0))
00136       {
00137         sparseMat.insertBackByOuterInner(j,i) = v;
00138         if (nonzeroCoords)
00139           nonzeroCoords->push_back(Vector2i(i,j));
00140       }
00141       else if (zeroCoords)
00142       {
00143         zeroCoords->push_back(Vector2i(i,j));
00144       }
00145       refMat(i,j) = v;
00146     }
00147   }
00148   sparseMat.finalize();
00149 }
00150 
00151 template<typename Scalar> void
00152 initSparse(double density,
00153            Matrix<Scalar,Dynamic,1>& refVec,
00154            SparseVector<Scalar>& sparseVec,
00155            std::vector<int>* zeroCoords = 0,
00156            std::vector<int>* nonzeroCoords = 0)
00157 {
00158   sparseVec.reserve(int(refVec.size()*density));
00159   sparseVec.setZero();
00160   for(int i=0; i<refVec.size(); i++)
00161   {
00162     Scalar v = (internal::random<double>(0,1) < density) ? internal::random<Scalar>() : Scalar(0);
00163     if (v!=Scalar(0))
00164     {
00165       sparseVec.insertBack(i) = v;
00166       if (nonzeroCoords)
00167         nonzeroCoords->push_back(i);
00168     }
00169     else if (zeroCoords)
00170         zeroCoords->push_back(i);
00171     refVec[i] = v;
00172   }
00173 }
00174 
00175 #endif // EIGEN_TESTSPARSE_H


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