// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2012 Gael Guennebaud // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #ifndef EIGEN_SPARSE_PERMUTATION_H #define EIGEN_SPARSE_PERMUTATION_H // This file implements sparse * permutation products namespace Eigen { namespace internal { template struct traits > { typedef typename remove_all::type MatrixTypeNestedCleaned; typedef typename MatrixTypeNestedCleaned::Scalar Scalar; typedef typename MatrixTypeNestedCleaned::StorageIndex StorageIndex; enum { SrcStorageOrder = MatrixTypeNestedCleaned::Flags&RowMajorBit ? RowMajor : ColMajor, MoveOuter = SrcStorageOrder==RowMajor ? Side==OnTheLeft : Side==OnTheRight }; typedef typename internal::conditional, SparseMatrix >::type ReturnType; }; template struct permut_sparsematrix_product_retval : public ReturnByValue > { typedef typename remove_all::type MatrixTypeNestedCleaned; typedef typename MatrixTypeNestedCleaned::Scalar Scalar; typedef typename MatrixTypeNestedCleaned::StorageIndex StorageIndex; enum { SrcStorageOrder = MatrixTypeNestedCleaned::Flags&RowMajorBit ? RowMajor : ColMajor, MoveOuter = SrcStorageOrder==RowMajor ? Side==OnTheLeft : Side==OnTheRight }; permut_sparsematrix_product_retval(const PermutationType& perm, const MatrixType& matrix) : m_permutation(perm), m_matrix(matrix) {} inline int rows() const { return m_matrix.rows(); } inline int cols() const { return m_matrix.cols(); } template inline void evalTo(Dest& dst) const { if(MoveOuter) { SparseMatrix tmp(m_matrix.rows(), m_matrix.cols()); Matrix sizes(m_matrix.outerSize()); for(Index j=0; j tmp(m_matrix.rows(), m_matrix.cols()); Matrix sizes(tmp.outerSize()); sizes.setZero(); PermutationMatrix perm; if((Side==OnTheLeft) ^ Transposed) perm = m_permutation; else perm = m_permutation.transpose(); for(Index j=0; j struct product_promote_storage_type { typedef Sparse ret; }; template struct product_promote_storage_type { typedef Sparse ret; }; // TODO, the following need cleaning, this is just a copy-past of the dense case template struct generic_product_impl { template static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) { permut_sparsematrix_product_retval pmpr(lhs, rhs); pmpr.evalTo(dst); } }; template struct generic_product_impl { template static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) { permut_sparsematrix_product_retval pmpr(rhs, lhs); pmpr.evalTo(dst); } }; template struct generic_product_impl, Rhs, PermutationShape, SparseShape, ProductTag> { template static void evalTo(Dest& dst, const Transpose& lhs, const Rhs& rhs) { permut_sparsematrix_product_retval pmpr(lhs.nestedPermutation(), rhs); pmpr.evalTo(dst); } }; template struct generic_product_impl, SparseShape, PermutationShape, ProductTag> { template static void evalTo(Dest& dst, const Lhs& lhs, const Transpose& rhs) { permut_sparsematrix_product_retval pmpr(rhs.nestedPermutation(), lhs); pmpr.evalTo(dst); } }; // TODO, the following two overloads are only needed to define the right temporary type through // typename traits >::ReturnType // while it should be correctly handled by traits >::PlainObject template struct product_evaluator, ProductTag, PermutationShape, SparseShape, typename traits::Scalar, typename traits::Scalar> : public evaluator >::ReturnType>::type { typedef Product XprType; typedef typename traits >::ReturnType PlainObject; typedef typename evaluator::type Base; explicit product_evaluator(const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) { ::new (static_cast(this)) Base(m_result); generic_product_impl::evalTo(m_result, xpr.lhs(), xpr.rhs()); } protected: PlainObject m_result; }; template struct product_evaluator, ProductTag, SparseShape, PermutationShape, typename traits::Scalar, typename traits::Scalar> : public evaluator >::ReturnType>::type { typedef Product XprType; typedef typename traits >::ReturnType PlainObject; typedef typename evaluator::type Base; explicit product_evaluator(const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) { ::new (static_cast(this)) Base(m_result); generic_product_impl::evalTo(m_result, xpr.lhs(), xpr.rhs()); } protected: PlainObject m_result; }; } // end namespace internal /** \returns the matrix with the permutation applied to the columns */ template inline const Product operator*(const SparseMatrixBase& matrix, const PermutationBase& perm) { return Product(matrix.derived(), perm.derived()); } /** \returns the matrix with the permutation applied to the rows */ template inline const Product operator*( const PermutationBase& perm, const SparseMatrixBase& matrix) { return Product(perm.derived(), matrix.derived()); } // TODO, the following specializations should not be needed as Transpose should be a PermutationBase. /** \returns the matrix with the inverse permutation applied to the columns. */ template inline const Product > > operator*(const SparseMatrixBase& matrix, const Transpose >& tperm) { return Product > >(matrix.derived(), tperm); } /** \returns the matrix with the inverse permutation applied to the rows. */ template inline const Product >, SparseDerived> operator*(const Transpose >& tperm, const SparseMatrixBase& matrix) { return Product >, SparseDerived>(tperm, matrix.derived()); } } // end namespace Eigen #endif // EIGEN_SPARSE_SELFADJOINTVIEW_H