From 835e0c9f674554adf9621fd84b32c9cde9f2bdf6 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Fri, 28 Sep 2007 06:10:34 +0000 Subject: Found a way to have eval() be a member function of class EiObject, instead of a global function. CCMAIL:bensch128@yahoo.com --- doc/tutorial.cpp | 2 +- src/internal/Eval.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/internal/Matrix.h | 8 +------- src/internal/Object.h | 11 +++++++---- src/internal/Util.h | 1 + test/matrixmanip.cpp | 2 +- test/matrixops.cpp | 2 +- test/vectorops.cpp | 4 ++-- 8 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 src/internal/Eval.h diff --git a/doc/tutorial.cpp b/doc/tutorial.cpp index b0e13687b..d909779c5 100644 --- a/doc/tutorial.cpp +++ b/doc/tutorial.cpp @@ -45,7 +45,7 @@ int main(int, char **) << "Anyway, if you want to store m * m into m, you can do this:" << endl << " m = eval(m * m);" << endl; m = m_save; - m = eval(m * m); + m = (m * m).eval(); cout << "And m is now:" << endl << m << endl << "as was expected." << endl; return 0; } diff --git a/src/internal/Eval.h b/src/internal/Eval.h new file mode 100644 index 000000000..2b79866ed --- /dev/null +++ b/src/internal/Eval.h @@ -0,0 +1,52 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2006-2007 Benoit Jacob +// +// Eigen is free software; 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 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 General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with Eigen; if not, write to the Free Software Foundation, Inc., 51 +// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +// +// As a special exception, if other files instantiate templates or use macros +// or functions from this file, or you compile this file and link it +// with other works to produce a work based on this file, this file does not +// by itself cause the resulting work to be covered by the GNU General Public +// License. This exception does not invalidate any other reasons why a work +// based on this file might be covered by the GNU General Public License. + +#ifndef EI_EVAL_H +#define EI_EVAL_H + +template class EiEval + : public EiMatrix< typename Expression::Scalar, + Expression::Derived::RowsAtCompileTime, + Expression::Derived::ColsAtCompileTime > +{ + public: + typedef typename Expression::Scalar Scalar; + typedef typename Expression::Derived Derived; + typedef EiMatrix< Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime> MatrixType; + typedef Expression Base; + friend class EiObject; + + EI_INHERIT_ASSIGNMENT_OPERATORS(EiEval) + + EiEval(const Expression& expression) : MatrixType(expression) {} +}; + +template +EiEval > EiObject::eval() const +{ + return EiEval >(*this); +} + +#endif // EI_EVAL_H diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h index d56ac455a..738cc9f6b 100644 --- a/src/internal/Matrix.h +++ b/src/internal/Matrix.h @@ -97,13 +97,6 @@ class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >, ~EiMatrix() {} }; -template -EiMatrix -eval(const EiObject& expression) -{ - return EiMatrix(expression); -} - #define EI_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \ typedef EiMatrix EiMatrix##SizeSuffix##TypeSuffix; \ typedef EiMatrix EiVector##SizeSuffix##TypeSuffix; @@ -124,6 +117,7 @@ EI_MAKE_TYPEDEFS_ALL_SIZES(std::complex, cd) #undef EI_MAKE_TYPEDEFS_ALL_SIZES #undef EI_MAKE_TYPEDEFS +#include "Eval.h" #include "MatrixOps.h" #include "ScalarOps.h" #include "RowAndCol.h" diff --git a/src/internal/Object.h b/src/internal/Object.h index 6bf86f87a..fde835001 100644 --- a/src/internal/Object.h +++ b/src/internal/Object.h @@ -28,13 +28,14 @@ #include "Util.h" -template class EiObject +template class EiObject { - static const int RowsAtCompileTime = Derived::RowsAtCompileTime, - ColsAtCompileTime = Derived::ColsAtCompileTime; + static const int RowsAtCompileTime = _Derived::RowsAtCompileTime, + ColsAtCompileTime = _Derived::ColsAtCompileTime; public: - typedef typename EiForwardDecl::Ref Ref; + typedef typename EiForwardDecl<_Derived>::Ref Ref; typedef _Scalar Scalar; + typedef _Derived Derived; int rows() const { return static_cast(this)->_rows(); } int cols() const { return static_cast(this)->_cols(); } @@ -109,6 +110,8 @@ template class EiObject Scalar& operator()(int row, int col = 0) { return write(row, col); } + + EiEval eval() const; }; template diff --git a/src/internal/Util.h b/src/internal/Util.h index 05ebbe163..c7730d6fb 100644 --- a/src/internal/Util.h +++ b/src/internal/Util.h @@ -51,6 +51,7 @@ template class EiSum; template class EiDifference; template class EiMatrixProduct; template class EiScalarProduct; +template class EiEval; template struct EiForwardDecl { diff --git a/test/matrixmanip.cpp b/test/matrixmanip.cpp index 4851a9178..dd1887c87 100644 --- a/test/matrixmanip.cpp +++ b/test/matrixmanip.cpp @@ -39,7 +39,7 @@ template void matrixManip(const MatrixType& m) a.row(i) += b.row(i); a.col(j) *= 2; a.minor(i, j) = b.block(1, rows-1, 1, cols-1); - a.minor(i, j) -= eval(a.block(1, rows-1, 1, cols-1)); + a.minor(i, j) -= a.block(1, rows-1, 1, cols-1).eval(); } void EigenTest::testMatrixManip() diff --git a/test/matrixops.cpp b/test/matrixops.cpp index e5f01759e..f8f787819 100644 --- a/test/matrixops.cpp +++ b/test/matrixops.cpp @@ -44,7 +44,7 @@ template void vectorOps(const VectorType& v) a = b; a = b + c; a = s * (b - c); - a = eval(s * (b - c)); + a = (s * (b - c)).eval(); a += b; a -= b + b; a *= s; b /= s; - a += eval(a + a); + a += (a + a).eval(); } void EigenTest::testVectorOps() -- cgit v1.2.3