aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2007-09-28 06:10:34 +0000
committerGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2007-09-28 06:10:34 +0000
commit835e0c9f674554adf9621fd84b32c9cde9f2bdf6 (patch)
tree4cd4469110466b11a5c7809637279c092e706a3b
parent28c44a95c21bb18ec265faba69e750c430045f9c (diff)
Found a way to have eval() be a member function of class EiObject, instead of a global function.
CCMAIL:bensch128@yahoo.com
-rw-r--r--doc/tutorial.cpp2
-rw-r--r--src/internal/Eval.h52
-rw-r--r--src/internal/Matrix.h8
-rw-r--r--src/internal/Object.h11
-rw-r--r--src/internal/Util.h1
-rw-r--r--test/matrixmanip.cpp2
-rw-r--r--test/matrixops.cpp2
-rw-r--r--test/vectorops.cpp4
8 files changed, 66 insertions, 16 deletions
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 <jacob@math.jussieu.fr>
+//
+// 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<typename Expression> 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<Scalar, Derived>;
+
+ EI_INHERIT_ASSIGNMENT_OPERATORS(EiEval)
+
+ EiEval(const Expression& expression) : MatrixType(expression) {}
+};
+
+template<typename Scalar, typename Derived>
+EiEval<EiObject<Scalar, Derived> > EiObject<Scalar, Derived>::eval() const
+{
+ return EiEval<EiObject<Scalar, Derived> >(*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<typename Scalar, typename Derived>
-EiMatrix<Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime>
-eval(const EiObject<Scalar, Derived>& expression)
-{
- return EiMatrix<Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime>(expression);
-}
-
#define EI_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
typedef EiMatrix<Type, Size, Size> EiMatrix##SizeSuffix##TypeSuffix; \
typedef EiMatrix<Type, Size, 1> EiVector##SizeSuffix##TypeSuffix;
@@ -124,6 +117,7 @@ EI_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, 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<typename _Scalar, typename Derived> class EiObject
+template<typename _Scalar, typename _Derived> class EiObject
{
- static const int RowsAtCompileTime = Derived::RowsAtCompileTime,
- ColsAtCompileTime = Derived::ColsAtCompileTime;
+ static const int RowsAtCompileTime = _Derived::RowsAtCompileTime,
+ ColsAtCompileTime = _Derived::ColsAtCompileTime;
public:
- typedef typename EiForwardDecl<Derived>::Ref Ref;
+ typedef typename EiForwardDecl<_Derived>::Ref Ref;
typedef _Scalar Scalar;
+ typedef _Derived Derived;
int rows() const { return static_cast<const Derived *>(this)->_rows(); }
int cols() const { return static_cast<const Derived *>(this)->_cols(); }
@@ -109,6 +110,8 @@ template<typename _Scalar, typename Derived> class EiObject
Scalar& operator()(int row, int col = 0)
{ return write(row, col); }
+
+ EiEval<EiObject> eval() const;
};
template<typename Scalar, typename Derived>
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<typename Lhs, typename Rhs> class EiSum;
template<typename Lhs, typename Rhs> class EiDifference;
template<typename Lhs, typename Rhs> class EiMatrixProduct;
template<typename MatrixType> class EiScalarProduct;
+template<typename ExpressionType> class EiEval;
template<typename T> 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<typename MatrixType> 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<typename MatrixType1,
a = b;
a = b + c;
a = s * (b - c);
- a = eval(a + b);
+ a = (a + b).eval();
a += b;
a -= b + b;
a *= s;
diff --git a/test/vectorops.cpp b/test/vectorops.cpp
index b061206c2..26969bf94 100644
--- a/test/vectorops.cpp
+++ b/test/vectorops.cpp
@@ -42,13 +42,13 @@ template<typename VectorType> 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()