| 28 | |
| 29 | |
| 30 | template<typename SparseMatrixType> void sparse_product() |
| 31 | { |
| 32 | typedef typename SparseMatrixType::StorageIndex StorageIndex; |
| 33 | Index n = 100; |
| 34 | const Index rows = internal::random<Index>(1,n); |
| 35 | const Index cols = internal::random<Index>(1,n); |
| 36 | const Index depth = internal::random<Index>(1,n); |
| 37 | typedef typename SparseMatrixType::Scalar Scalar; |
| 38 | enum { Flags = SparseMatrixType::Flags }; |
| 39 | |
| 40 | double density = (std::max)(8./(rows*cols), 0.2); |
| 41 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 42 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 43 | typedef Matrix<Scalar,1,Dynamic> RowDenseVector; |
| 44 | typedef SparseVector<Scalar,0,StorageIndex> ColSpVector; |
| 45 | typedef SparseVector<Scalar,RowMajor,StorageIndex> RowSpVector; |
| 46 | |
| 47 | Scalar s1 = internal::random<Scalar>(); |
| 48 | Scalar s2 = internal::random<Scalar>(); |
| 49 | |
| 50 | // test matrix-matrix product |
| 51 | { |
| 52 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, depth); |
| 53 | DenseMatrix refMat2t = DenseMatrix::Zero(depth, rows); |
| 54 | DenseMatrix refMat3 = DenseMatrix::Zero(depth, cols); |
| 55 | DenseMatrix refMat3t = DenseMatrix::Zero(cols, depth); |
| 56 | DenseMatrix refMat4 = DenseMatrix::Zero(rows, cols); |
| 57 | DenseMatrix refMat4t = DenseMatrix::Zero(cols, rows); |
| 58 | DenseMatrix refMat5 = DenseMatrix::Random(depth, cols); |
| 59 | DenseMatrix refMat6 = DenseMatrix::Random(rows, rows); |
| 60 | DenseMatrix dm4 = DenseMatrix::Zero(rows, rows); |
| 61 | // DenseVector dv1 = DenseVector::Random(rows); |
| 62 | SparseMatrixType m2 (rows, depth); |
| 63 | SparseMatrixType m2t(depth, rows); |
| 64 | SparseMatrixType m3 (depth, cols); |
| 65 | SparseMatrixType m3t(cols, depth); |
| 66 | SparseMatrixType m4 (rows, cols); |
| 67 | SparseMatrixType m4t(cols, rows); |
| 68 | SparseMatrixType m6(rows, rows); |
| 69 | initSparse(density, refMat2, m2); |
| 70 | initSparse(density, refMat2t, m2t); |
| 71 | initSparse(density, refMat3, m3); |
| 72 | initSparse(density, refMat3t, m3t); |
| 73 | initSparse(density, refMat4, m4); |
| 74 | initSparse(density, refMat4t, m4t); |
| 75 | initSparse(density, refMat6, m6); |
| 76 | |
| 77 | // int c = internal::random<int>(0,depth-1); |
| 78 | |
| 79 | // sparse * sparse |
| 80 | VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3); |
| 81 | VERIFY_IS_APPROX(m4=m2t.transpose()*m3, refMat4=refMat2t.transpose()*refMat3); |
| 82 | VERIFY_IS_APPROX(m4=m2t.transpose()*m3t.transpose(), refMat4=refMat2t.transpose()*refMat3t.transpose()); |
| 83 | VERIFY_IS_APPROX(m4=m2*m3t.transpose(), refMat4=refMat2*refMat3t.transpose()); |
| 84 | |
| 85 | VERIFY_IS_APPROX(m4 = m2*m3/s1, refMat4 = refMat2*refMat3/s1); |
| 86 | VERIFY_IS_APPROX(m4 = m2*m3*s1, refMat4 = refMat2*refMat3*s1); |
| 87 | VERIFY_IS_APPROX(m4 = s2*m2*m3*s1, refMat4 = s2*refMat2*refMat3*s1); |
nothing calls this directly
no test coverage detected