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.
This commit is contained in:
Benoit Jacob 2007-09-27 07:42:12 +00:00
parent 8a024825d2
commit 628b1a8f6d
9 changed files with 50 additions and 41 deletions

View File

@ -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

View File

@ -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; }

View File

@ -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"

View File

@ -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; }

View File

@ -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(); }

View File

@ -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; }

View File

@ -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; }

View File

@ -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

View File

@ -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