00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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
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
00071 if(false)
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
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|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 }