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


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