add operators *= and /=

This commit is contained in:
Benoit Jacob 2007-09-27 19:20:06 +00:00
parent 628b1a8f6d
commit d99d9407df
8 changed files with 70 additions and 11 deletions

View File

@ -89,6 +89,22 @@ template<typename _Scalar, typename Derived> class EigenBase
Derived& operator+=(const EigenBase<Scalar, OtherDerived>& other); Derived& operator+=(const EigenBase<Scalar, OtherDerived>& other);
template<typename OtherDerived> template<typename OtherDerived>
Derived& operator-=(const EigenBase<Scalar, OtherDerived>& other); Derived& operator-=(const EigenBase<Scalar, OtherDerived>& other);
template<typename OtherDerived>
Derived& operator*=(const EigenBase<Scalar, OtherDerived>& other);
Derived& operator*=(const int& other);
Derived& operator*=(const float& other);
Derived& operator*=(const double& other);
Derived& operator*=(const std::complex<int>& other);
Derived& operator*=(const std::complex<float>& other);
Derived& operator*=(const std::complex<double>& other);
Derived& operator/=(const int& other);
Derived& operator/=(const float& other);
Derived& operator/=(const double& other);
Derived& operator/=(const std::complex<int>& other);
Derived& operator/=(const std::complex<float>& other);
Derived& operator/=(const std::complex<double>& other);
Scalar operator()(int row, int col = 0) const Scalar operator()(int row, int col = 0) const
{ return read(row, col); } { return read(row, col); }

View File

@ -86,6 +86,8 @@ class Matrix : public EigenBase<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=) EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=)
EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, -=) EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, -=)
EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Matrix, *=)
EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Matrix, /=)
explicit Matrix(int rows = 1, int cols = 1) : Storage(rows, cols) {} explicit Matrix(int rows = 1, int cols = 1) : Storage(rows, cols) {}
template<typename OtherDerived> template<typename OtherDerived>

View File

@ -149,13 +149,6 @@ template<typename Lhs, typename Rhs> class MatrixProduct
const RhsRef m_rhs; const RhsRef m_rhs;
}; };
template<typename Scalar, typename Derived1, typename Derived2>
MatrixProduct<Derived1, Derived2>
operator*(const EigenBase<Scalar, Derived1> &mat1, const EigenBase<Scalar, Derived2> &mat2)
{
return MatrixProduct<Derived1, Derived2>(mat1.ref(), mat2.ref());
}
template<typename Scalar, typename Derived1, typename Derived2> template<typename Scalar, typename Derived1, typename Derived2>
MatrixSum<Derived1, Derived2> MatrixSum<Derived1, Derived2>
operator+(const EigenBase<Scalar, Derived1> &mat1, const EigenBase<Scalar, Derived2> &mat2) operator+(const EigenBase<Scalar, Derived1> &mat1, const EigenBase<Scalar, Derived2> &mat2)
@ -170,6 +163,13 @@ operator-(const EigenBase<Scalar, Derived1> &mat1, const EigenBase<Scalar, Deriv
return MatrixDifference<Derived1, Derived2>(mat1.ref(), mat2.ref()); return MatrixDifference<Derived1, Derived2>(mat1.ref(), mat2.ref());
} }
template<typename Scalar, typename Derived1, typename Derived2>
MatrixProduct<Derived1, Derived2>
operator*(const EigenBase<Scalar, Derived1> &mat1, const EigenBase<Scalar, Derived2> &mat2)
{
return MatrixProduct<Derived1, Derived2>(mat1.ref(), mat2.ref());
}
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
template<typename OtherDerived> template<typename OtherDerived>
Derived & Derived &
@ -188,6 +188,15 @@ EigenBase<Scalar, Derived>::operator-=(const EigenBase<Scalar, OtherDerived> &ot
return *static_cast<Derived*>(this); return *static_cast<Derived*>(this);
} }
template<typename Scalar, typename Derived>
template<typename OtherDerived>
Derived &
EigenBase<Scalar, Derived>::operator*=(const EigenBase<Scalar, OtherDerived> &other)
{
*this = *this * other;
return *static_cast<Derived*>(this);
}
} // namespace Eigen } // namespace Eigen
#endif // EIGEN_MATRIXOPS_H #endif // EIGEN_MATRIXOPS_H

View File

@ -86,6 +86,22 @@ operator/(const EigenBase<Scalar, Derived>& matrix, \
OtherScalar scalar) \ OtherScalar scalar) \
{ \ { \
return matrix * (static_cast<typename Derived::Scalar>(1) / scalar); \ return matrix * (static_cast<typename Derived::Scalar>(1) / scalar); \
} \
\
template<typename Scalar, typename Derived> \
Derived & \
EigenBase<Scalar, Derived>::operator*=(const OtherScalar &other) \
{ \
*this = *this * other; \
return *static_cast<Derived*>(this); \
} \
\
template<typename Scalar, typename Derived> \
Derived & \
EigenBase<Scalar, Derived>::operator/=(const OtherScalar &other) \
{ \
*this = *this / other; \
return *static_cast<Derived*>(this); \
} }
EIGEN_MAKE_SCALAR_OPS(int) EIGEN_MAKE_SCALAR_OPS(int)

View File

@ -79,17 +79,27 @@ const int DynamicSize = -1;
template<typename OtherScalar, typename OtherDerived> \ template<typename OtherScalar, typename OtherDerived> \
Derived& operator Op(const EigenBase<OtherScalar, OtherDerived>& other) \ Derived& operator Op(const EigenBase<OtherScalar, OtherDerived>& other) \
{ \ { \
return EigenBase<OtherScalar, Derived>::operator Op(other); \ return EigenBase<Scalar, Derived>::operator Op(other); \
} \ } \
Derived& operator Op(const Derived& other) \ Derived& operator Op(const Derived& other) \
{ \ { \
return EigenBase<Scalar, Derived>::operator Op(other); \ return EigenBase<Scalar, Derived>::operator Op(other); \
} }
#define EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
template<typename Other> \
Derived& operator Op(const Other& scalar) \
{ \
return EigenBase<Scalar, Derived>::operator Op(scalar); \
}
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \ #define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \ EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \ EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=) EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=) \
EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \
EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=)
} // namespace Eigen } // namespace Eigen

View File

@ -37,6 +37,7 @@ template<typename MatrixType> void matrixManip(const MatrixType& m)
a.block(1, rows-1, 1, cols-1); a.block(1, rows-1, 1, cols-1);
a.row(i) = b.row(i); a.row(i) = b.row(i);
a.row(i) += b.row(i); 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) = 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) -= eval(a.block(1, rows-1, 1, cols-1));
} }

View File

@ -47,6 +47,9 @@ template<typename MatrixType1,
a = eval(a + b); a = eval(a + b);
a += b; a += b;
a -= b + b; a -= b + b;
a *= s;
b /= s;
if(rows1 == cols1) a *= b;
MatrixType1 d(rows1, cols1); MatrixType1 d(rows1, cols1);
MatrixType2 e(rows2, cols2); MatrixType2 e(rows2, cols2);

View File

@ -45,7 +45,9 @@ template<typename VectorType> void vectorOps(const VectorType& v)
a = eval(s * (b - c)); a = eval(s * (b - c));
a += b; a += b;
a += b + b; a -= b + b;
a *= s;
b /= s;
a += eval(a + a); a += eval(a + a);
} }