From cfca7f71fea444f46249375106e5f64d83533be8 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Sat, 18 Oct 2008 11:11:10 +0000 Subject: sparse module: much much faster transposition code --- Eigen/src/Sparse/SparseMatrix.h | 54 +++++++++++++++++++++++++++++++++++++--- Eigen/src/Sparse/SparseProduct.h | 5 +++- 2 files changed, 54 insertions(+), 5 deletions(-) (limited to 'Eigen') diff --git a/Eigen/src/Sparse/SparseMatrix.h b/Eigen/src/Sparse/SparseMatrix.h index e3b224d7f..5dd1f1365 100644 --- a/Eigen/src/Sparse/SparseMatrix.h +++ b/Eigen/src/Sparse/SparseMatrix.h @@ -65,6 +65,7 @@ class SparseMatrix enum { RowMajor = SparseBase::RowMajor }; + typedef SparseMatrix TransposedSparseMatrix; int m_outerSize; int m_innerSize; @@ -225,8 +226,7 @@ class SparseMatrix else { resize(other.rows(), other.cols()); - for (int j=0; j<=m_outerSize; ++j) - m_outerIndex[j] = other.m_outerIndex[j]; + memcpy(m_outerIndex, other.m_outerIndex, (m_outerSize+1)*sizeof(int)); m_data = other.m_data; } return *this; @@ -235,8 +235,54 @@ class SparseMatrix template inline SparseMatrix& operator=(const MatrixBase& other) { -// std::cout << "SparseMatrix& operator=(const MatrixBase& other)\n"; - return SparseMatrixBase::operator=(other.derived()); + const bool needToTranspose = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit); + if (needToTranspose) + { + // two passes algorithm: + // 1 - compute the number of coeffs per dest inner vector + // 2 - do the actual copy/eval + // Since each coeff of the rhs has to be evaluated twice, let's evauluate it if needed + typedef typename ei_nested::type OtherCopy; + OtherCopy otherCopy(other.derived()); + typedef typename ei_cleantype::type _OtherCopy; + + resize(other.rows(), other.cols()); + Map(m_outerIndex,outerSize()).setZero(); + // pass 1 + // FIXME the above copy could be merged with that pass + for (int j=0; j::operator=(other.derived()); + } } friend std::ostream & operator << (std::ostream & s, const SparseMatrix& m) diff --git a/Eigen/src/Sparse/SparseProduct.h b/Eigen/src/Sparse/SparseProduct.h index 28b05f6a0..c430ed928 100644 --- a/Eigen/src/Sparse/SparseProduct.h +++ b/Eigen/src/Sparse/SparseProduct.h @@ -169,7 +169,10 @@ struct ei_sparse_product_selector } } for (typename AmbiVector::Iterator it(tempVector); it; ++it) - res.fill(it.index(), j) = it.value(); + if (ResultType::Flags&RowMajorBit) + res.fill(j,it.index()) = it.value(); + else + res.fill(it.index(), j) = it.value(); } res.endFill(); } -- cgit v1.2.3