aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2007-09-27 07:42:12 +0000
committerGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2007-09-27 07:42:12 +0000
commit628b1a8f6d2635bc7dadfd16f3af56b4a664e233 (patch)
tree1496ce52ab13d6f220aab6fca6ab55e0416658ab
parent8a024825d26aee76431e4c7dab98f114bf51dff2 (diff)
implement scalar operators separately for each type using a macro.
This is required e.g. to allow "2 * m" with m a matrix of doubles.
-rw-r--r--doc/tutorial.cpp6
-rw-r--r--src/internal/Block.h2
-rw-r--r--src/internal/Matrix.h7
-rw-r--r--src/internal/MatrixOps.h6
-rw-r--r--src/internal/MatrixRef.h2
-rw-r--r--src/internal/Minor.h2
-rw-r--r--src/internal/RowAndCol.h4
-rw-r--r--src/internal/ScalarOps.h52
-rw-r--r--src/internal/Util.h10
9 files changed, 50 insertions, 41 deletions
diff --git a/doc/tutorial.cpp b/doc/tutorial.cpp
index efadc9bfa..18a1ced55 100644
--- a/doc/tutorial.cpp
+++ b/doc/tutorial.cpp
@@ -5,16 +5,12 @@ using namespace Eigen;
int main(int, char **)
{
- Matrix<double,2,2> m, n; // 2x2 fixed-size matrix with uninitialized entries
+ Matrix<double,2,2> m; // 2x2 fixed-size matrix with uninitialized entries
m(0,0) = 1;
m(0,1) = 2;
m(1,0) = 3;
m(1,1) = 4;
- n = m;
- n = eval(n*n+n);
- cout << n << endl;
-
cout << "Here is a 2x2 matrix m:" << endl << m << endl;
cout << "Let us now build a 4x4 matrix m2 by assembling together four 2x2 blocks." << endl;
MatrixXd m2(4,4); // dynamic matrix with initial size 4x4 and uninitialized entries
diff --git a/src/internal/Block.h b/src/internal/Block.h
index cd4422887..9be1d165c 100644
--- a/src/internal/Block.h
+++ b/src/internal/Block.h
@@ -54,7 +54,7 @@ template<typename MatrixType> class MatrixBlock
: m_matrix(other.m_matrix), m_startRow(other.m_startRow), m_endRow(other.m_endRow),
m_startCol(other.m_startCol), m_endCol(other.m_endCol) {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixBlock)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixBlock)
private:
const Ref& _ref() const { return *this; }
diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h
index 15042beed..c07aa2d08 100644
--- a/src/internal/Matrix.h
+++ b/src/internal/Matrix.h
@@ -84,8 +84,8 @@ class Matrix : public EigenBase<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
return Base::operator=(other);
}
- INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=)
- INHERIT_ASSIGNMENT_OPERATOR(Matrix, -=)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, -=)
explicit Matrix(int rows = 1, int cols = 1) : Storage(rows, cols) {}
template<typename OtherDerived>
@@ -124,6 +124,9 @@ EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<int>, ci)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
+#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
+#undef EIGEN_MAKE_TYPEDEFS
+
} // namespace Eigen
#include "MatrixOps.h"
diff --git a/src/internal/MatrixOps.h b/src/internal/MatrixOps.h
index 5697588ac..4a4e2ca98 100644
--- a/src/internal/MatrixOps.h
+++ b/src/internal/MatrixOps.h
@@ -50,7 +50,7 @@ template<typename Lhs, typename Rhs> class MatrixSum
MatrixSum(const MatrixSum& other)
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixSum)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixSum)
private:
@@ -90,7 +90,7 @@ template<typename Lhs, typename Rhs> class MatrixDifference
MatrixDifference(const MatrixDifference& other)
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixDifference)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixDifference)
private:
const Ref& _ref() const { return *this; }
@@ -129,7 +129,7 @@ template<typename Lhs, typename Rhs> class MatrixProduct
MatrixProduct(const MatrixProduct& other)
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixProduct)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixProduct)
private:
const Ref& _ref() const { return *this; }
diff --git a/src/internal/MatrixRef.h b/src/internal/MatrixRef.h
index d074adf2e..71545aba3 100644
--- a/src/internal/MatrixRef.h
+++ b/src/internal/MatrixRef.h
@@ -40,7 +40,7 @@ template<typename MatrixType> class MatrixRef
MatrixRef(const MatrixRef& other) : m_matrix(other.m_matrix) {}
~MatrixRef() {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixRef)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixRef)
private:
int _rows() const { return m_matrix.rows(); }
diff --git a/src/internal/Minor.h b/src/internal/Minor.h
index c7ddd5de3..627dbf723 100644
--- a/src/internal/Minor.h
+++ b/src/internal/Minor.h
@@ -50,7 +50,7 @@ template<typename MatrixType> class MatrixMinor
MatrixMinor(const MatrixMinor& other)
: m_matrix(other.m_matrix), m_row(other.m_row), m_col(other.m_col) {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixMinor)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixMinor)
private:
const Ref& _ref() const { return *this; }
diff --git a/src/internal/RowAndCol.h b/src/internal/RowAndCol.h
index c8e9eb46d..b99d70766 100644
--- a/src/internal/RowAndCol.h
+++ b/src/internal/RowAndCol.h
@@ -55,7 +55,7 @@ template<typename MatrixType> class MatrixRow
return EigenBase<Scalar, MatrixRow<MatrixType> >::operator=(other);
}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixRow)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixRow)
private:
const Ref& _ref() const { return *this; }
@@ -103,7 +103,7 @@ template<typename MatrixType> class MatrixCol
MatrixCol(const MatrixCol& other)
: m_matrix(other.m_matrix), m_col(other.m_col) {}
- INHERIT_ASSIGNMENT_OPERATORS(MatrixCol)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixCol)
private:
const Ref& _ref() const { return *this; }
diff --git a/src/internal/ScalarOps.h b/src/internal/ScalarOps.h
index ecfebcaf3..41325fd75 100644
--- a/src/internal/ScalarOps.h
+++ b/src/internal/ScalarOps.h
@@ -46,7 +46,7 @@ template<typename MatrixType> class ScalarProduct
ScalarProduct(const ScalarProduct& other)
: m_matrix(other.m_matrix), m_scalar(other.m_scalar) {}
- INHERIT_ASSIGNMENT_OPERATORS(ScalarProduct)
+ EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ScalarProduct)
private:
const Ref& _ref() const { return *this; }
@@ -63,29 +63,39 @@ template<typename MatrixType> class ScalarProduct
const Scalar m_scalar;
};
-template<typename Scalar, typename Derived>
-ScalarProduct<Derived>
-operator*(const EigenBase<Scalar, Derived>& matrix,
- Scalar scalar)
-{
- return ScalarProduct<Derived>(matrix.ref(), scalar);
+#define EIGEN_MAKE_SCALAR_OPS(OtherScalar) \
+template<typename Scalar, typename Derived> \
+ScalarProduct<Derived> \
+operator*(const EigenBase<Scalar, Derived>& matrix, \
+ OtherScalar scalar) \
+{ \
+ return ScalarProduct<Derived>(matrix.ref(), scalar); \
+} \
+ \
+template<typename Scalar, typename Derived> \
+ScalarProduct<Derived> \
+operator*(OtherScalar scalar, \
+ const EigenBase<Scalar, Derived>& matrix) \
+{ \
+ return ScalarProduct<Derived>(matrix.ref(), scalar); \
+} \
+ \
+template<typename Scalar, typename Derived> \
+ScalarProduct<Derived> \
+operator/(const EigenBase<Scalar, Derived>& matrix, \
+ OtherScalar scalar) \
+{ \
+ return matrix * (static_cast<typename Derived::Scalar>(1) / scalar); \
}
-template<typename Scalar, typename Derived>
-ScalarProduct<Derived>
-operator*(Scalar scalar,
- const EigenBase<Scalar, Derived>& matrix)
-{
- return ScalarProduct<Derived>(matrix.ref(), scalar);
-}
+EIGEN_MAKE_SCALAR_OPS(int)
+EIGEN_MAKE_SCALAR_OPS(float)
+EIGEN_MAKE_SCALAR_OPS(double)
+EIGEN_MAKE_SCALAR_OPS(std::complex<int>)
+EIGEN_MAKE_SCALAR_OPS(std::complex<float>)
+EIGEN_MAKE_SCALAR_OPS(std::complex<double>)
-template<typename Scalar, typename Derived>
-ScalarProduct<Derived>
-operator/(const EigenBase<Scalar, Derived>& matrix,
- Scalar scalar)
-{
- return matrix * (static_cast<typename Derived::Scalar>(1) / scalar);
-}
+#undef EIGEN_MAKE_SCALAR_OPS
} // namespace Eigen
diff --git a/src/internal/Util.h b/src/internal/Util.h
index 9bba7297e..0469d82a9 100644
--- a/src/internal/Util.h
+++ b/src/internal/Util.h
@@ -75,7 +75,7 @@ const int DynamicSize = -1;
#define EIGEN_UNUSED(x) (void)x
-#define INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
+#define EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
template<typename OtherScalar, typename OtherDerived> \
Derived& operator Op(const EigenBase<OtherScalar, OtherDerived>& other) \
{ \
@@ -86,10 +86,10 @@ Derived& operator Op(const Derived& other) \
return EigenBase<Scalar, Derived>::operator Op(other); \
}
-#define INHERIT_ASSIGNMENT_OPERATORS(Derived) \
-INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
-INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
-INHERIT_ASSIGNMENT_OPERATOR(Derived, -=)
+#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
+EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
+EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
+EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=)
} // namespace Eigen