From 1e7b1a8a85bf0ea54d4471c8b79de443f6bb0ccb Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Mon, 13 Jul 2009 14:55:03 +0200 Subject: [PATCH] add a SparseNestByValue expression and fix issue in sparse adjoint evaluation --- Eigen/Sparse | 1 + Eigen/src/Sparse/SparseMatrix.h | 5 +- Eigen/src/Sparse/SparseMatrixBase.h | 10 ++-- Eigen/src/Sparse/SparseNestByValue.h | 84 ++++++++++++++++++++++++++++ Eigen/src/Sparse/SparseUtil.h | 1 + test/main.h | 1 + test/sparse_basic.cpp | 2 + 7 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 Eigen/src/Sparse/SparseNestByValue.h diff --git a/Eigen/Sparse b/Eigen/Sparse index 364fd50c9..a8888daa3 100644 --- a/Eigen/Sparse +++ b/Eigen/Sparse @@ -84,6 +84,7 @@ namespace Eigen { #include "src/Sparse/SparseUtil.h" #include "src/Sparse/SparseMatrixBase.h" +#include "src/Sparse/SparseNestByValue.h" #include "src/Sparse/CompressedStorage.h" #include "src/Sparse/AmbiVector.h" #include "src/Sparse/RandomSetter.h" diff --git a/Eigen/src/Sparse/SparseMatrix.h b/Eigen/src/Sparse/SparseMatrix.h index b751b5cb9..af3b5e5eb 100644 --- a/Eigen/src/Sparse/SparseMatrix.h +++ b/Eigen/src/Sparse/SparseMatrix.h @@ -443,9 +443,8 @@ class SparseMatrix // 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; - typedef typename ei_eval::type OtherCopy; + // Since each coeff of the rhs has to be evaluated twice, let's evaluate it if needed + typedef typename ei_nested::type OtherCopy; typedef typename ei_cleantype::type _OtherCopy; OtherCopy otherCopy(other.derived()); diff --git a/Eigen/src/Sparse/SparseMatrixBase.h b/Eigen/src/Sparse/SparseMatrixBase.h index 8d9406cfb..6cf4d5e96 100644 --- a/Eigen/src/Sparse/SparseMatrixBase.h +++ b/Eigen/src/Sparse/SparseMatrixBase.h @@ -99,8 +99,10 @@ template class SparseMatrixBase /** \internal the return type of MatrixBase::imag() */ typedef SparseCwiseUnaryOp, Derived> ImagReturnType; /** \internal the return type of MatrixBase::adjoint() */ - typedef SparseTranspose::type> /*>*/ - AdjointReturnType; + typedef typename ei_meta_if::IsComplex, + SparseCwiseUnaryOp, SparseNestByValue > >, + SparseTranspose + >::ret AdjointReturnType; #ifndef EIGEN_PARSED_BY_DOXYGEN /** This is the "real scalar" type; if the \a Scalar type is already real numbers @@ -342,7 +344,7 @@ template class SparseMatrixBase SparseTranspose transpose() { return derived(); } const SparseTranspose transpose() const { return derived(); } // void transposeInPlace(); - const AdjointReturnType adjoint() const { return conjugate()/*.nestByValue()*/; } + const AdjointReturnType adjoint() const { return transpose().nestByValue(); } // sub-vector SparseInnerVectorSet row(int i); @@ -520,7 +522,7 @@ template class SparseMatrixBase */ // inline int stride(void) const { return derived().stride(); } -// inline const NestByValue nestByValue() const; + inline const SparseNestByValue nestByValue() const; ConjugateReturnType conjugate() const; diff --git a/Eigen/src/Sparse/SparseNestByValue.h b/Eigen/src/Sparse/SparseNestByValue.h new file mode 100644 index 000000000..72fd056c4 --- /dev/null +++ b/Eigen/src/Sparse/SparseNestByValue.h @@ -0,0 +1,84 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2008-2009 Gael Guennebaud +// Copyright (C) 2006-2008 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#ifndef EIGEN_SPARSENESTBYVALUE_H +#define EIGEN_SPARSENESTBYVALUE_H + +/** \class SparseNestByValue + * + * \brief Expression which must be nested by value + * + * \param ExpressionType the type of the object of which we are requiring nesting-by-value + * + * This class is the return type of MatrixBase::nestByValue() + * and most of the time this is the only way it is used. + * + * \sa SparseMatrixBase::nestByValue(), class NestByValue + */ +template +struct ei_traits > : public ei_traits +{}; + +template class SparseNestByValue + : public SparseMatrixBase > +{ + public: + + class InnerIterator; + + EIGEN_SPARSE_GENERIC_PUBLIC_INTERFACE(SparseNestByValue) + + inline SparseNestByValue(const ExpressionType& matrix) : m_expression(matrix) {} + + EIGEN_STRONG_INLINE int rows() const { return m_expression.rows(); } + EIGEN_STRONG_INLINE int cols() const { return m_expression.cols(); } + + operator const ExpressionType&() const { return m_expression; } + + protected: + const ExpressionType m_expression; +}; + +/** \returns an expression of the temporary version of *this. + */ +template +inline const SparseNestByValue +SparseMatrixBase::nestByValue() const +{ + return SparseNestByValue(derived()); +} + +template +class SparseNestByValue::InnerIterator : public MatrixType::InnerIterator +{ + typedef typename MatrixType::InnerIterator Base; + public: + + EIGEN_STRONG_INLINE InnerIterator(const SparseNestByValue& expr, int outer) + : Base(expr.m_expression, outer) + {} +}; + +#endif // EIGEN_SPARSENESTBYVALUE_H diff --git a/Eigen/src/Sparse/SparseUtil.h b/Eigen/src/Sparse/SparseUtil.h index 52781aa46..a3b1d7ae9 100644 --- a/Eigen/src/Sparse/SparseUtil.h +++ b/Eigen/src/Sparse/SparseUtil.h @@ -106,6 +106,7 @@ template class DynamicSparseMatrix; template class SparseVector; template class MappedSparseMatrix; +template class SparseNestByValue; template class SparseTranspose; template class SparseInnerVectorSet; template class SparseCwise; diff --git a/test/main.h b/test/main.h index 53c8245c6..3947451cc 100644 --- a/test/main.h +++ b/test/main.h @@ -28,6 +28,7 @@ #include #include #include +#include #ifndef EIGEN_TEST_FUNC #error EIGEN_TEST_FUNC must be defined diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index 9b7d7c4e6..666eea872 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -299,6 +299,8 @@ template void sparse_basic(const SparseMatrixType& re initSparse(density, refMat2, m2); VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); + + VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint()); } // test prune