Patch by Gael Guennebaud: coeff-wise binary operators.

This unifies + and - and moreover this patch introduces
coeff-wise * and / based on this. Also, corresponding test.
This commit is contained in:
Benoit Jacob 2008-02-29 14:35:14 +00:00
parent f12e9c53ac
commit a2f8d4be6a
9 changed files with 331 additions and 9 deletions

View File

@ -18,8 +18,7 @@ namespace Eigen {
#include "src/Core/Matrix.h"
#include "src/Core/Cast.h"
#include "src/Core/Eval.h"
#include "src/Core/Sum.h"
#include "src/Core/Difference.h"
#include "src/Core/CwiseBinaryOp.h"
#include "src/Core/Opposite.h"
#include "src/Core/ScalarMultiple.h"
#include "src/Core/Product.h"

View File

@ -1,6 +1,7 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@gmail.com>
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or

View File

@ -0,0 +1,222 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@gmail.com>
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, 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 of
// the License, 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 Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.
#ifndef EIGEN_CWISE_BINARY_OP_H
#define EIGEN_CWISE_BINARY_OP_H
/** \class CwiseBinaryOp
*
* \brief Generic expression of a coefficient-wise operator between two matrices or vectors
*
* \param BinaryOp template functor implementing the operator
* \param Lhs the type of the left-hand side
* \param Rhs the type of the right-hand side
*
* This class represents an expression of a generic binary operator of two matrices or vectors.
* It is the return type of the operator+, operator-, cwiseiseProduct, cwiseQuotient between matrices or vectors, and most
* of the time this is the only way it is used.
*
* \sa class CwiseProductOp, class CwiseQuotientOp
*/
template<template<typename BinaryOpScalar> class BinaryOp, typename Lhs, typename Rhs>
class CwiseBinaryOp : NoOperatorEquals,
public MatrixBase<typename Lhs::Scalar, CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
{
public:
typedef typename Lhs::Scalar Scalar;
typedef typename Lhs::Ref LhsRef;
typedef typename Rhs::Ref RhsRef;
friend class MatrixBase<Scalar, CwiseBinaryOp>;
typedef MatrixBase<Scalar, CwiseBinaryOp> Base;
CwiseBinaryOp(const LhsRef& lhs, const RhsRef& rhs)
: m_lhs(lhs), m_rhs(rhs)
{
assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
}
private:
enum {
RowsAtCompileTime = Lhs::Traits::RowsAtCompileTime,
ColsAtCompileTime = Lhs::Traits::ColsAtCompileTime,
MaxRowsAtCompileTime = Lhs::Traits::MaxRowsAtCompileTime,
MaxColsAtCompileTime = Lhs::Traits::MaxColsAtCompileTime
};
const CwiseBinaryOp& _ref() const { return *this; }
int _rows() const { return m_lhs.rows(); }
int _cols() const { return m_lhs.cols(); }
Scalar _coeff(int row, int col) const
{
return BinaryOp<Scalar>::op(m_lhs.coeff(row, col), m_rhs.coeff(row, col));
}
protected:
const LhsRef m_lhs;
const RhsRef m_rhs;
};
/** \brief Template functor to compute the sum of two scalars
*
* \sa class CwiseBinaryOp, MatrixBase::operator+
*/
template<typename Scalar> struct CwiseSumOp {
static Scalar op(const Scalar& a, const Scalar& b) { return a + b; }
};
/** \brief Template functor to compute the difference of two scalars
*
* \sa class CwiseBinaryOp, MatrixBase::operator-
*/
template<typename Scalar> struct CwiseDifferenceOp {
static Scalar op(const Scalar& a, const Scalar& b) { return a - b; }
};
/** \brief Template functor to compute the product of two scalars
*
* \sa class CwiseBinaryOp, MatrixBase::cwiseProduct()
*/
template<typename Scalar> struct CwiseProductOp {
static Scalar op(const Scalar& a, const Scalar& b) { return a * b; }
};
/** \brief Template functor to compute the quotient of two scalars
*
* \sa class CwiseBinaryOp, MatrixBase::cwiseQuotient()
*/
template<typename Scalar> struct CwiseQuotientOp {
static Scalar op(const Scalar& a, const Scalar& b) { return a / b; }
};
/** \relates MatrixBase
*
* \returns an expression of the difference of \a mat1 and \a mat2
*
* \sa class CwiseBinaryOp, MatrixBase::operator-=()
*/
template<typename Scalar, typename Derived1, typename Derived2>
const CwiseBinaryOp<CwiseDifferenceOp, Derived1, Derived2>
operator-(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2)
{
return CwiseBinaryOp<CwiseDifferenceOp, Derived1, Derived2>(mat1.ref(), mat2.ref());
}
/** replaces \c *this by \c *this - \a other.
*
* \returns a reference to \c *this
*/
template<typename Scalar, typename Derived>
template<typename OtherDerived>
Derived &
MatrixBase<Scalar, Derived>::operator-=(const MatrixBase<Scalar, OtherDerived> &other)
{
return *this = *this - other;
}
/** \relates MatrixBase
*
* \returns an expression of the sum of \a mat1 and \a mat2
*
* \sa class CwiseBinaryOp, MatrixBase::operator+=()
*/
template<typename Scalar, typename Derived1, typename Derived2>
const CwiseBinaryOp<CwiseSumOp, Derived1, Derived2>
operator+(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2)
{
return CwiseBinaryOp<CwiseSumOp, Derived1, Derived2>(mat1.ref(), mat2.ref());
}
/** replaces \c *this by \c *this + \a other.
*
* \returns a reference to \c *this
*/
template<typename Scalar, typename Derived>
template<typename OtherDerived>
Derived &
MatrixBase<Scalar, Derived>::operator+=(const MatrixBase<Scalar, OtherDerived>& other)
{
return *this = *this + other;
}
/** \returns an expression of the Schur product (coefficient wise product) of *this and \a other
*
* \sa class CwiseBinaryOp
*/
template<typename Scalar, typename Derived>
template<typename OtherDerived>
const CwiseBinaryOp<CwiseProductOp, Derived, OtherDerived>
MatrixBase<Scalar, Derived>::cwiseProduct(const MatrixBase<Scalar, OtherDerived> &other) const
{
return CwiseBinaryOp<CwiseProductOp, Derived, OtherDerived>(ref(), other.ref());
}
/** \returns an expression of the coefficient-wise quotient of *this and \a other
*
* \sa class CwiseBinaryOp
*/
template<typename Scalar, typename Derived>
template<typename OtherDerived>
const CwiseBinaryOp<CwiseQuotientOp, Derived, OtherDerived>
MatrixBase<Scalar, Derived>::cwiseQuotient(const MatrixBase<Scalar, OtherDerived> &other) const
{
return CwiseBinaryOp<CwiseQuotientOp, Derived, OtherDerived>(ref(), other.ref());
}
/** \relates MatrixBase
*
* \returns an expression of a custom coefficient-wise operator of \a mat1 and \a mat2
*
* \param CustomBinaryOp template functor of the custom operator
*
* \sa class CwiseBinaryOp, MatrixBase::operator+, MatrixBase::operator-, MatrixBase::cwiseProduct, MatrixBase::cwiseQuotient
*/
template<template<typename BinaryOpScalar> class CustomBinaryOp, typename Scalar, typename Derived1, typename Derived2>
const CwiseBinaryOp<CustomBinaryOp, Derived1, Derived2>
cwise(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2)
{
return CwiseBinaryOp<CustomBinaryOp, Derived1, Derived2>(mat1.ref(), mat2.ref());
}
/** \returns an expression of a custom coefficient-wise operator of *this and \a other
*
* \param CustomBinaryOp template functor of the custom operator
*
* \sa class CwiseBinaryOp, MatrixBase::operator+, MatrixBase::operator-, MatrixBase::cwiseProduct, MatrixBase::cwiseQuotient
*/
template<typename Scalar, typename Derived>
template<template<typename BinaryOpScalar> class CustomBinaryOp, typename OtherDerived>
const CwiseBinaryOp<CustomBinaryOp, Derived, OtherDerived>
MatrixBase<Scalar, Derived>::cwise(const MatrixBase<Scalar, OtherDerived> &other) const
{
return CwiseBinaryOp<CustomBinaryOp, Derived, OtherDerived>(ref(), other.ref());
}
#endif // EIGEN_CWISE_BINARY_OP_H

View File

@ -69,7 +69,7 @@ struct DotUnroller<Index, 0, Derived1, Derived2>
*/
template<typename Scalar, typename Derived>
template<typename OtherDerived>
Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
Scalar MatrixBase<Scalar, Derived>::dot(const MatrixBase<Scalar, OtherDerived>& other) const
{
assert(Traits::IsVectorAtCompileTime
&& OtherDerived::Traits::IsVectorAtCompileTime
@ -79,7 +79,7 @@ Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
&& Traits::SizeAtCompileTime != Dynamic
&& Traits::SizeAtCompileTime <= 16)
DotUnroller<Traits::SizeAtCompileTime-1, Traits::SizeAtCompileTime,
Derived, OtherDerived>
Derived, MatrixBase<Scalar, OtherDerived> >
::run(*static_cast<const Derived*>(this), other, res);
else
{
@ -136,7 +136,7 @@ MatrixBase<Scalar, Derived>::normalized() const
template<typename Scalar, typename Derived>
template<typename OtherDerived>
bool MatrixBase<Scalar, Derived>::isOrtho
(const OtherDerived& other,
(const MatrixBase<Scalar, OtherDerived>& other,
typename NumTraits<Scalar>::Real prec) const
{
return ei_abs2(dot(other)) <= prec * prec * norm2() * other.norm2();

View File

@ -35,8 +35,9 @@ template<typename MatrixType, int BlockRows=Dynamic, int BlockCols=Dynamic> clas
template<typename MatrixType> class Transpose;
template<typename MatrixType> class Conjugate;
template<typename MatrixType> class Opposite;
template<typename Lhs, typename Rhs> class Sum;
template<typename Lhs, typename Rhs> class Difference;
template<template<typename BinaryOpScalar> class BinaryOp, typename Lhs, typename Rhs> class CwiseBinaryOp;
template<typename Scalar> struct CwiseProductOp;
template<typename Scalar> struct CwiseQuotientOp;
template<typename Lhs, typename Rhs> class Product;
template<typename MatrixType> class ScalarMultiple;
template<typename MatrixType> class Random;

View File

@ -212,12 +212,13 @@ template<typename Scalar, typename Derived> class MatrixBase
Scalar trace() const;
template<typename OtherDerived>
Scalar dot(const OtherDerived& other) const;
Scalar dot(const MatrixBase<Scalar, OtherDerived>& other) const;
RealScalar norm2() const;
RealScalar norm() const;
const ScalarMultiple<Derived> normalized() const;
template<typename OtherDerived>
bool isOrtho(const OtherDerived& other, RealScalar prec = precision<Scalar>()) const;
bool isOrtho(const MatrixBase<Scalar, OtherDerived>& other,
RealScalar prec = precision<Scalar>()) const;
bool isOrtho(RealScalar prec = precision<Scalar>()) const;
static const Eval<Random<Derived> > random(int rows, int cols);
@ -262,6 +263,18 @@ template<typename Scalar, typename Derived> class MatrixBase
const Opposite<Derived> operator-() const;
template<typename OtherDerived>
const CwiseBinaryOp<CwiseProductOp, Derived, OtherDerived>
cwiseProduct(const MatrixBase<Scalar, OtherDerived> &other) const;
template<typename OtherDerived>
const CwiseBinaryOp<CwiseQuotientOp, Derived, OtherDerived>
cwiseQuotient(const MatrixBase<Scalar, OtherDerived> &other) const;
template<template<typename BinaryOpScalar> class CustomBinaryOp, typename OtherDerived>
const CwiseBinaryOp<CustomBinaryOp, Derived, OtherDerived>
cwise(const MatrixBase<Scalar, OtherDerived> &other) const;
template<typename OtherDerived>
Derived& operator+=(const MatrixBase<Scalar, OtherDerived>& other);
template<typename OtherDerived>

View File

@ -5,6 +5,7 @@ FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE_DIRECTORIES( ${QT_INCLUDE_DIR} )
SET(test_SRCS
cwiseop.cpp
main.cpp
basicstuff.cpp
linearstructure.cpp

84
test/cwiseop.cpp Normal file
View File

@ -0,0 +1,84 @@
// 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-2008 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, 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 of
// the License, 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 Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.
#include "main.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
namespace Eigen {
template<typename Scalar> struct AddIfNull {
static Scalar op(const Scalar a, const Scalar b) {return a<1e-3 ? b : a;}
};
template<typename MatrixType> void cwiseops(const MatrixType& m)
{
typedef typename MatrixType::Scalar Scalar;
typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType;
int rows = m.rows();
int cols = m.cols();
MatrixType m1 = MatrixType::random(rows, cols),
m2 = MatrixType::random(rows, cols),
m3(rows, cols),
mzero = MatrixType::zero(rows, cols),
mones = MatrixType::ones(rows, cols),
identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
::identity(rows, rows),
square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
::random(rows, rows);
VectorType v1 = VectorType::random(rows),
v2 = VectorType::random(rows),
vzero = VectorType::zero(rows);
m2 = m2.cwise<AddIfNull>(mones);
//m2 = cwise<AddIfNull>(m2,mones);
std::cout << m2 << "\n";
VERIFY_IS_APPROX( mzero, m1-m1);
VERIFY_IS_APPROX( m2, m1+m2-m1);
VERIFY_IS_APPROX( mones, m2.cwiseQuotient(m2));
VERIFY_IS_APPROX( m1.cwiseProduct(m2), m2.cwiseProduct(m1));
//VERIFY_IS_APPROX( m1, m2.cwiseProduct(m1).cwiseQuotient(m2));
// VERIFY_IS_APPROX( cwiseMin(m1,m2), cwiseMin(m2,m1) );
// VERIFY_IS_APPROX( cwiseMin(m1,m1+mones), m1 );
// VERIFY_IS_APPROX( cwiseMin(m1,m1-mones), m1-mones );
}
void EigenTest::testCwiseops()
{
for(int i = 0; i < 1/*m_repeat*/ ; i++) {
// cwiseops(Matrix<float, 1, 1>());
// cwiseops(Matrix4d());
// cwiseops(MatrixXcf(3, 3));
cwiseops(MatrixXi(8, 12));
// cwiseops(MatrixXcd(20, 20));
}
}
} // namespace Eigen

View File

@ -121,6 +121,7 @@ class EigenTest : public QObject
void testMiscMatrices();
void testSmallVectors();
void testMap();
void testCwiseops();
protected:
int m_repeat;
};