// This file is part of Eigen, a lightweight C++ template library // for linear algebra. Eigen itself is part of the KDE project. // // Copyright (C) 2006-2007 Benoit Jacob // // Eigen is free software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the Free Software // Foundation; either version 2 or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // You should have received a copy of the GNU General Public License along // with Eigen; if not, write to the Free Software Foundation, Inc., 51 // Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. // // As a special exception, if other files instantiate templates or use macros // or functions from this file, or you compile this file and link it // with other works to produce a work based on this file, this file does not // by itself cause the resulting work to be covered by the GNU General Public // License. This exception does not invalidate any other reasons why a work // based on this file might be covered by the GNU General Public License. #ifndef EIGEN_MATRIXOPS_H #define EIGEN_MATRIXOPS_H namespace Eigen { #define EIGEN_MAKE_MATRIX_OP_XPR(NAME, SYMBOL) \ template class Matrix##NAME \ { \ public: \ typedef typename Lhs::Scalar Scalar; \ \ Matrix##NAME(const Lhs& lhs, const Rhs& rhs) \ : m_lhs(lhs), m_rhs(rhs) \ { \ assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols()); \ } \ \ Matrix##NAME(const Matrix##NAME& other) \ : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {} \ \ int rows() const { return m_lhs.rows(); } \ int cols() const { return m_lhs.cols(); } \ \ Scalar operator()(int row, int col) const \ { \ return m_lhs(row, col) SYMBOL m_rhs(row, col); \ } \ \ protected: \ const Lhs m_lhs; \ const Rhs m_rhs; \ }; EIGEN_MAKE_MATRIX_OP_XPR(Sum, +) EIGEN_MAKE_MATRIX_OP_XPR(Difference, -) #undef EIGEN_MAKE_MATRIX_OP_XPR template class MatrixProduct { public: typedef typename Lhs::Scalar Scalar; MatrixProduct(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs) { assert(lhs.cols() == rhs.rows()); } MatrixProduct(const MatrixProduct& other) : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {} int rows() const { return m_lhs.rows(); } int cols() const { return m_rhs.cols(); } Scalar operator()(int row, int col) const { Scalar x = static_cast(0); for(int i = 0; i < m_lhs.cols(); i++) x += m_lhs(row, i) * m_rhs(i, col); return x; } protected: const Lhs m_lhs; const Rhs m_rhs; }; #define EIGEN_MAKE_MATRIX_OP(NAME, SYMBOL) \ template \ const MatrixConstXpr< \ const Matrix##NAME< \ MatrixConstXpr, \ MatrixConstXpr \ > \ > \ operator SYMBOL(const MatrixConstXpr &xpr1, const MatrixConstXpr &xpr2) \ { \ typedef const Matrix##NAME< \ MatrixConstXpr, \ MatrixConstXpr \ > ProductType; \ typedef const MatrixConstXpr XprType; \ return XprType(ProductType(xpr1, xpr2)); \ } \ \ template \ const MatrixConstXpr< \ const Matrix##NAME< \ MatrixConstRef >, \ MatrixConstXpr \ > \ > \ operator SYMBOL(const MatrixBase &mat, const MatrixConstXpr &xpr) \ { \ typedef const Matrix##NAME< \ MatrixConstRef >, \ MatrixConstXpr \ > ProductType; \ typedef const MatrixConstXpr XprType; \ return XprType(ProductType(mat.constRef(), xpr)); \ } \ \ template \ const MatrixConstXpr< \ const Matrix##NAME< \ MatrixConstXpr, \ MatrixConstRef > \ > \ > \ operator SYMBOL(const MatrixConstXpr &xpr, const MatrixBase &mat) \ { \ typedef const Matrix##NAME< \ MatrixConstXpr, \ MatrixConstRef > \ > ProductType; \ typedef const MatrixConstXpr XprType; \ return XprType(ProductType(xpr, mat.constRef())); \ } \ \ template \ const MatrixConstXpr< \ const Matrix##NAME< \ MatrixConstRef >, \ MatrixConstRef > \ > \ > \ operator SYMBOL(const MatrixBase &mat1, const MatrixBase &mat2) \ { \ typedef const Matrix##NAME< \ MatrixConstRef >, \ MatrixConstRef > \ > ProductType; \ typedef const MatrixConstXpr XprType; \ return XprType(ProductType(MatrixConstRef >(mat1), \ MatrixConstRef >(mat2))); \ } EIGEN_MAKE_MATRIX_OP(Sum, +) EIGEN_MAKE_MATRIX_OP(Difference, -) EIGEN_MAKE_MATRIX_OP(Product, *) #undef EIGEN_MAKE_MATRIX_OP #define EIGEN_MAKE_MATRIX_OP_EQ(SYMBOL) \ template \ template \ MatrixBase & \ MatrixBase::operator SYMBOL##=(const MatrixBase &mat2) \ { \ return *this = *this SYMBOL mat2; \ } \ \ template \ template \ MatrixBase & \ MatrixBase::operator SYMBOL##=(const MatrixConstXpr &xpr) \ { \ return *this = *this SYMBOL xpr; \ } \ \ template \ template \ MatrixXpr & \ MatrixXpr::operator SYMBOL##=(const MatrixBase &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 \ template \ MatrixXpr & \ MatrixXpr::operator SYMBOL##=(const MatrixConstXpr &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