eigen2_sparse_product.cpp
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 #include "sparse.h"
00026 
00027 template<typename SparseMatrixType> void sparse_product(const SparseMatrixType& ref)
00028 {
00029   const int rows = ref.rows();
00030   const int cols = ref.cols();
00031   typedef typename SparseMatrixType::Scalar Scalar;
00032   enum { Flags = SparseMatrixType::Flags };
00033 
00034   double density = std::max(8./(rows*cols), 0.01);
00035   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
00036   typedef Matrix<Scalar,Dynamic,1> DenseVector;
00037 
00038   // test matrix-matrix product
00039   {
00040     DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
00041     DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows);
00042     DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows);
00043     DenseMatrix dm4 = DenseMatrix::Zero(rows, rows);
00044     SparseMatrixType m2(rows, rows);
00045     SparseMatrixType m3(rows, rows);
00046     SparseMatrixType m4(rows, rows);
00047     initSparse<Scalar>(density, refMat2, m2);
00048     initSparse<Scalar>(density, refMat3, m3);
00049     initSparse<Scalar>(density, refMat4, m4);
00050     VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
00051     VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
00052     VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
00053     VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
00054 
00055     // sparse * dense
00056     VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3);
00057     VERIFY_IS_APPROX(dm4=m2*refMat3.transpose(), refMat4=refMat2*refMat3.transpose());
00058     VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3, refMat4=refMat2.transpose()*refMat3);
00059     VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
00060 
00061     // dense * sparse
00062     VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3);
00063     VERIFY_IS_APPROX(dm4=refMat2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
00064     VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
00065     VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
00066 
00067     VERIFY_IS_APPROX(m3=m3*m3, refMat3=refMat3*refMat3);
00068   }
00069 
00070   // test matrix - diagonal product
00071   if(false) // it compiles, but the precision is terrible. probably doesn't matter in this branch....
00072   {
00073     DenseMatrix refM2 = DenseMatrix::Zero(rows, rows);
00074     DenseMatrix refM3 = DenseMatrix::Zero(rows, rows);
00075     DiagonalMatrix<DenseVector> d1(DenseVector::Random(rows));
00076     SparseMatrixType m2(rows, rows);
00077     SparseMatrixType m3(rows, rows);
00078     initSparse<Scalar>(density, refM2, m2);
00079     initSparse<Scalar>(density, refM3, m3);
00080     VERIFY_IS_APPROX(m3=m2*d1, refM3=refM2*d1);
00081     VERIFY_IS_APPROX(m3=m2.transpose()*d1, refM3=refM2.transpose()*d1);
00082     VERIFY_IS_APPROX(m3=d1*m2, refM3=d1*refM2);
00083     VERIFY_IS_APPROX(m3=d1*m2.transpose(), refM3=d1 * refM2.transpose());
00084   }
00085 
00086   // test self adjoint products
00087   {
00088     DenseMatrix b = DenseMatrix::Random(rows, rows);
00089     DenseMatrix x = DenseMatrix::Random(rows, rows);
00090     DenseMatrix refX = DenseMatrix::Random(rows, rows);
00091     DenseMatrix refUp = DenseMatrix::Zero(rows, rows);
00092     DenseMatrix refLo = DenseMatrix::Zero(rows, rows);
00093     DenseMatrix refS = DenseMatrix::Zero(rows, rows);
00094     SparseMatrixType mUp(rows, rows);
00095     SparseMatrixType mLo(rows, rows);
00096     SparseMatrixType mS(rows, rows);
00097     do {
00098       initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular);
00099     } while (refUp.isZero());
00100     refLo = refUp.transpose().conjugate();
00101     mLo = mUp.transpose().conjugate();
00102     refS = refUp + refLo;
00103     refS.diagonal() *= 0.5;
00104     mS = mUp + mLo;
00105     for (int k=0; k<mS.outerSize(); ++k)
00106       for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it)
00107         if (it.index() == k)
00108           it.valueRef() *= 0.5;
00109 
00110     VERIFY_IS_APPROX(refS.adjoint(), refS);
00111     VERIFY_IS_APPROX(mS.transpose().conjugate(), mS);
00112     VERIFY_IS_APPROX(mS, refS);
00113     VERIFY_IS_APPROX(x=mS*b, refX=refS*b);
00114     VERIFY_IS_APPROX(x=mUp.template marked<UpperTriangular|SelfAdjoint>()*b, refX=refS*b);
00115     VERIFY_IS_APPROX(x=mLo.template marked<LowerTriangular|SelfAdjoint>()*b, refX=refS*b);
00116     VERIFY_IS_APPROX(x=mS.template marked<SelfAdjoint>()*b, refX=refS*b);
00117   }
00118 
00119 }
00120 
00121 void test_eigen2_sparse_product()
00122 {
00123   for(int i = 0; i < g_repeat; i++) {
00124     CALL_SUBTEST_1( sparse_product(SparseMatrix<double>(8, 8)) );
00125     CALL_SUBTEST_2( sparse_product(SparseMatrix<std::complex<double> >(16, 16)) );
00126     CALL_SUBTEST_1( sparse_product(SparseMatrix<double>(33, 33)) );
00127 
00128     CALL_SUBTEST_3( sparse_product(DynamicSparseMatrix<double>(8, 8)) );
00129   }
00130 }


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