Add operator += and operator -= between matrices/vectors/expressions

This commit is contained in:
Benoit Jacob 2007-09-07 07:41:10 +00:00
parent e7ef6dba9d
commit c3731b36d1
6 changed files with 80 additions and 1 deletions

View File

@ -202,6 +202,16 @@ class MatrixBase
MatrixConstXpr<MatrixBlock<const ConstRef> >
block(int startRow, int endRow, int startCol = 0, int endCol = 0) const;
template<typename Content>
MatrixBase& operator+=(const MatrixConstXpr<Content> &xpr);
template<typename Content>
MatrixBase& operator-=(const MatrixConstXpr<Content> &xpr);
template<typename Derived2>
MatrixBase& operator+=(const MatrixBase<Derived2> &other);
template<typename Derived2>
MatrixBase& operator-=(const MatrixBase<Derived2> &other);
protected:
MatrixBase() {};

View File

@ -167,6 +167,54 @@ EIGEN_MAKE_MATRIX_OP(Product, *)
#undef EIGEN_MAKE_MATRIX_OP
#define EIGEN_MAKE_MATRIX_OP_EQ(SYMBOL) \
template<typename Derived1> \
template<typename Derived2> \
MatrixBase<Derived1> & \
MatrixBase<Derived1>::operator SYMBOL##=(const MatrixBase<Derived2> &mat2) \
{ \
return *this = *this SYMBOL mat2; \
} \
\
template<typename Derived> \
template<typename Content> \
MatrixBase<Derived> & \
MatrixBase<Derived>::operator SYMBOL##=(const MatrixConstXpr<Content> &xpr) \
{ \
return *this = *this SYMBOL xpr; \
} \
\
template<typename Content> \
template<typename Derived> \
MatrixXpr<Content> & \
MatrixXpr<Content>::operator SYMBOL##=(const MatrixBase<Derived> &mat) \
{ \
assert(rows() == mat.rows() && cols() == mat.cols()); \
for(int i = 0; i < rows(); i++) \
for(int j = 0; j < cols(); j++) \
this->operator()(i, j) SYMBOL##= mat(i, j); \
return *this; \
} \
\
template<typename Content1> \
template<typename Content2> \
MatrixXpr<Content1> & \
MatrixXpr<Content1>::operator SYMBOL##=(const MatrixConstXpr<Content2> &other) \
{ \
assert(rows() == other.rows() && cols() == other.cols()); \
for(int i = 0; i < rows(); i++) \
for(int j = 0; j < cols(); j++) \
this->operator()(i, j) SYMBOL##= other(i, j); \
return *this; \
}
EIGEN_MAKE_MATRIX_OP_EQ(+)
EIGEN_MAKE_MATRIX_OP_EQ(-)
#undef EIGEN_MAKE_MATRIX_OP_EQ
} // namespace Eigen
#endif // EIGEN_MATRIXOPS_H

View File

@ -115,6 +115,17 @@ template<typename Content> class MatrixXpr
MatrixXpr<MatrixBlock<MatrixXpr<Content> > >
block(int startRow, int endRow, int startCol= 0, int endCol = 0);
template<typename Content2>
MatrixXpr& operator+=(const MatrixConstXpr<Content2> &other);
template<typename Content2>
MatrixXpr& operator-=(const MatrixConstXpr<Content2> &other);
template<typename Derived>
MatrixXpr& operator+=(const MatrixBase<Derived> &matrix);
template<typename Derived>
MatrixXpr& operator-=(const MatrixBase<Derived> &matrix);
private:
void operator=(const MatrixXpr &other)
{}

View File

@ -36,8 +36,9 @@ template<typename MatrixType> void matrixManip(const MatrixType& m)
a.minor(i, j);
a.block(1, rows-1, 1, cols-1);
a.xpr().row(i) = b.row(i);
a.xpr().row(i) += b.row(i);
a.xpr().minor(i, j) = b.block(1, rows-1, 1, cols-1);
a.alias().xpr().minor(i, j) = a.block(1, rows-1, 1, cols-1);
a.alias().xpr().minor(i, j) -= a.block(1, rows-1, 1, cols-1);
}
void EigenTest::testMatrixManip()

View File

@ -46,6 +46,10 @@ template<typename MatrixType1,
a = s * (b - c);
a.alias() = a + b;
a += b;
a.alias().xpr() += b;
a -= b + b;
MatrixType1 d(rows1, cols1);
MatrixType2 e(rows2, cols2);
QVERIFY( (d * e).rows() == rows1 && (d * e).cols() == cols2 );

View File

@ -43,6 +43,11 @@ template<typename VectorType> void vectorOps(const VectorType& v)
a = b + c;
a = s * (b - c);
a.alias() = a + b;
a += b;
a += b + b;
a.xpr() -= b;
a.xpr() -= b + b;
}
void EigenTest::testVectorOps()