From d99d9407df76d432bf9459ce000a2506e887c03b Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Thu, 27 Sep 2007 19:20:06 +0000 Subject: [PATCH] add operators *= and /= --- src/internal/EigenBase.h | 16 ++++++++++++++++ src/internal/Matrix.h | 2 ++ src/internal/MatrixOps.h | 23 ++++++++++++++++------- src/internal/ScalarOps.h | 16 ++++++++++++++++ src/internal/Util.h | 14 ++++++++++++-- test/matrixmanip.cpp | 3 ++- test/matrixops.cpp | 3 +++ test/vectorops.cpp | 4 +++- 8 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/internal/EigenBase.h b/src/internal/EigenBase.h index 9f77ea6fc..804880c75 100644 --- a/src/internal/EigenBase.h +++ b/src/internal/EigenBase.h @@ -89,6 +89,22 @@ template class EigenBase Derived& operator+=(const EigenBase& other); template Derived& operator-=(const EigenBase& other); + template + Derived& operator*=(const EigenBase& other); + + Derived& operator*=(const int& other); + Derived& operator*=(const float& other); + Derived& operator*=(const double& other); + Derived& operator*=(const std::complex& other); + Derived& operator*=(const std::complex& other); + Derived& operator*=(const std::complex& other); + + Derived& operator/=(const int& other); + Derived& operator/=(const float& other); + Derived& operator/=(const double& other); + Derived& operator/=(const std::complex& other); + Derived& operator/=(const std::complex& other); + Derived& operator/=(const std::complex& other); Scalar operator()(int row, int col = 0) const { return read(row, col); } diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h index c07aa2d08..e7ab5e8d8 100644 --- a/src/internal/Matrix.h +++ b/src/internal/Matrix.h @@ -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_SCALAR_ASSIGNMENT_OPERATOR(Matrix, *=) + EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Matrix, /=) explicit Matrix(int rows = 1, int cols = 1) : Storage(rows, cols) {} template diff --git a/src/internal/MatrixOps.h b/src/internal/MatrixOps.h index 4a4e2ca98..9fb639fa3 100644 --- a/src/internal/MatrixOps.h +++ b/src/internal/MatrixOps.h @@ -149,13 +149,6 @@ template class MatrixProduct const RhsRef m_rhs; }; -template -MatrixProduct -operator*(const EigenBase &mat1, const EigenBase &mat2) -{ - return MatrixProduct(mat1.ref(), mat2.ref()); -} - template MatrixSum operator+(const EigenBase &mat1, const EigenBase &mat2) @@ -170,6 +163,13 @@ operator-(const EigenBase &mat1, const EigenBase(mat1.ref(), mat2.ref()); } +template +MatrixProduct +operator*(const EigenBase &mat1, const EigenBase &mat2) +{ + return MatrixProduct(mat1.ref(), mat2.ref()); +} + template template Derived & @@ -188,6 +188,15 @@ EigenBase::operator-=(const EigenBase &ot return *static_cast(this); } +template +template +Derived & +EigenBase::operator*=(const EigenBase &other) +{ + *this = *this * other; + return *static_cast(this); +} + } // namespace Eigen #endif // EIGEN_MATRIXOPS_H diff --git a/src/internal/ScalarOps.h b/src/internal/ScalarOps.h index 41325fd75..fb8e340dc 100644 --- a/src/internal/ScalarOps.h +++ b/src/internal/ScalarOps.h @@ -86,6 +86,22 @@ operator/(const EigenBase& matrix, \ OtherScalar scalar) \ { \ return matrix * (static_cast(1) / scalar); \ +} \ + \ +template \ +Derived & \ +EigenBase::operator*=(const OtherScalar &other) \ +{ \ + *this = *this * other; \ + return *static_cast(this); \ +} \ + \ +template \ +Derived & \ +EigenBase::operator/=(const OtherScalar &other) \ +{ \ + *this = *this / other; \ + return *static_cast(this); \ } EIGEN_MAKE_SCALAR_OPS(int) diff --git a/src/internal/Util.h b/src/internal/Util.h index 0469d82a9..1851bdd6c 100644 --- a/src/internal/Util.h +++ b/src/internal/Util.h @@ -79,17 +79,27 @@ const int DynamicSize = -1; template \ Derived& operator Op(const EigenBase& other) \ { \ - return EigenBase::operator Op(other); \ + return EigenBase::operator Op(other); \ } \ Derived& operator Op(const Derived& other) \ { \ return EigenBase::operator Op(other); \ } +#define EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \ +template \ +Derived& operator Op(const Other& scalar) \ +{ \ + return EigenBase::operator Op(scalar); \ +} + #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_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \ +EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=) + } // namespace Eigen diff --git a/test/matrixmanip.cpp b/test/matrixmanip.cpp index 9e7b613b2..c95d33d1f 100644 --- a/test/matrixmanip.cpp +++ b/test/matrixmanip.cpp @@ -29,7 +29,7 @@ template void matrixManip(const MatrixType& m) { int rows = m.rows(), cols = m.cols(); int i = rand()%rows, j = rand()%cols; - + MatrixType a(rows, cols), b(rows, cols); a.row(i); a.col(j); @@ -37,6 +37,7 @@ template void matrixManip(const MatrixType& m) a.block(1, rows-1, 1, cols-1); 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) -= eval(a.block(1, rows-1, 1, cols-1)); } diff --git a/test/matrixops.cpp b/test/matrixops.cpp index 1369a72a0..f148d8a82 100644 --- a/test/matrixops.cpp +++ b/test/matrixops.cpp @@ -47,6 +47,9 @@ template void vectorOps(const VectorType& v) a = eval(s * (b - c)); a += b; - a += b + b; + a -= b + b; + a *= s; + b /= s; a += eval(a + a); }