2007-09-05 10:42:15 +00:00
|
|
|
// 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 <jacob@math.jussieu.fr>
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
#ifndef EI_MATRIXOPS_H
|
|
|
|
|
#define EI_MATRIXOPS_H
|
2007-09-05 10:42:15 +00:00
|
|
|
|
2007-09-27 19:38:40 +00:00
|
|
|
template<typename Lhs, typename Rhs> class EiSum
|
2007-09-27 19:54:04 +00:00
|
|
|
: public EiObject<typename Lhs::Scalar, EiSum<Lhs, Rhs> >
|
2007-09-26 14:06:14 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef typename Lhs::Scalar Scalar;
|
2007-09-29 17:27:20 +00:00
|
|
|
typedef typename Lhs::ConstRef LhsRef;
|
|
|
|
|
typedef typename Rhs::ConstRef RhsRef;
|
2007-09-27 19:54:04 +00:00
|
|
|
friend class EiObject<Scalar, EiSum>;
|
2007-09-26 14:06:26 +00:00
|
|
|
|
|
|
|
|
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = Rhs::ColsAtCompileTime;
|
2007-09-26 14:06:14 +00:00
|
|
|
|
2007-09-29 17:27:20 +00:00
|
|
|
EiSum(const LhsRef& lhs, const RhsRef& rhs)
|
2007-09-26 14:06:14 +00:00
|
|
|
: m_lhs(lhs), m_rhs(rhs)
|
|
|
|
|
{
|
|
|
|
|
assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
EiSum(const EiSum& other)
|
2007-09-26 14:06:14 +00:00
|
|
|
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
|
2007-09-30 14:56:11 +00:00
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
EI_INHERIT_ASSIGNMENT_OPERATORS(EiSum)
|
2007-09-26 14:06:14 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2007-10-01 06:51:25 +00:00
|
|
|
const EiSum& _ref() const { return *this; }
|
|
|
|
|
const EiSum& _constRef() const { return *this; }
|
2007-09-26 14:06:14 +00:00
|
|
|
int _rows() const { return m_lhs.rows(); }
|
|
|
|
|
int _cols() const { return m_lhs.cols(); }
|
|
|
|
|
|
|
|
|
|
Scalar _read(int row, int col) const
|
|
|
|
|
{
|
|
|
|
|
return m_lhs.read(row, col) + m_rhs.read(row, col);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
const LhsRef m_lhs;
|
|
|
|
|
const RhsRef m_rhs;
|
2007-09-05 10:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
2007-09-27 19:38:40 +00:00
|
|
|
template<typename Lhs, typename Rhs> class EiDifference
|
2007-09-27 19:54:04 +00:00
|
|
|
: public EiObject<typename Lhs::Scalar, EiDifference<Lhs, Rhs> >
|
2007-09-26 14:06:14 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef typename Lhs::Scalar Scalar;
|
2007-09-29 17:27:20 +00:00
|
|
|
typedef typename Lhs::ConstRef LhsRef;
|
|
|
|
|
typedef typename Rhs::ConstRef RhsRef;
|
2007-09-27 19:54:04 +00:00
|
|
|
friend class EiObject<Scalar, EiDifference>;
|
2007-09-26 14:06:14 +00:00
|
|
|
|
2007-09-26 14:06:26 +00:00
|
|
|
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = Rhs::ColsAtCompileTime;
|
|
|
|
|
|
2007-09-29 17:27:20 +00:00
|
|
|
EiDifference(const LhsRef& lhs, const RhsRef& rhs)
|
2007-09-26 14:06:14 +00:00
|
|
|
: m_lhs(lhs), m_rhs(rhs)
|
|
|
|
|
{
|
|
|
|
|
assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
|
|
|
|
|
}
|
2007-09-05 10:42:15 +00:00
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
EiDifference(const EiDifference& other)
|
2007-09-26 14:06:14 +00:00
|
|
|
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
|
|
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
EI_INHERIT_ASSIGNMENT_OPERATORS(EiDifference)
|
2007-09-26 14:06:14 +00:00
|
|
|
|
|
|
|
|
private:
|
2007-10-01 06:51:25 +00:00
|
|
|
const EiDifference& _ref() const { return *this; }
|
|
|
|
|
const EiDifference& _constRef() const { return *this; }
|
2007-09-26 14:06:14 +00:00
|
|
|
int _rows() const { return m_lhs.rows(); }
|
|
|
|
|
int _cols() const { return m_lhs.cols(); }
|
|
|
|
|
|
|
|
|
|
Scalar _read(int row, int col) const
|
|
|
|
|
{
|
|
|
|
|
return m_lhs.read(row, col) - m_rhs.read(row, col);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
const LhsRef m_lhs;
|
|
|
|
|
const RhsRef m_rhs;
|
|
|
|
|
};
|
2007-09-05 10:42:15 +00:00
|
|
|
|
2007-09-27 19:38:40 +00:00
|
|
|
template<typename Lhs, typename Rhs> class EiMatrixProduct
|
2007-09-27 19:54:04 +00:00
|
|
|
: public EiObject<typename Lhs::Scalar, EiMatrixProduct<Lhs, Rhs> >
|
2007-09-05 10:42:15 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef typename Lhs::Scalar Scalar;
|
2007-09-29 17:27:20 +00:00
|
|
|
typedef typename Lhs::ConstRef LhsRef;
|
|
|
|
|
typedef typename Rhs::ConstRef RhsRef;
|
2007-09-27 19:54:04 +00:00
|
|
|
friend class EiObject<Scalar, EiMatrixProduct>;
|
2007-09-26 14:06:14 +00:00
|
|
|
|
|
|
|
|
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = Rhs::ColsAtCompileTime;
|
2007-09-05 10:42:15 +00:00
|
|
|
|
2007-09-29 17:27:20 +00:00
|
|
|
EiMatrixProduct(const LhsRef& lhs, const RhsRef& rhs)
|
2007-09-05 10:42:15 +00:00
|
|
|
: m_lhs(lhs), m_rhs(rhs)
|
|
|
|
|
{
|
|
|
|
|
assert(lhs.cols() == rhs.rows());
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
EiMatrixProduct(const EiMatrixProduct& other)
|
2007-09-05 10:42:15 +00:00
|
|
|
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
|
|
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
EI_INHERIT_ASSIGNMENT_OPERATORS(EiMatrixProduct)
|
2007-09-05 10:42:15 +00:00
|
|
|
|
2007-09-26 14:06:14 +00:00
|
|
|
private:
|
2007-10-01 06:51:25 +00:00
|
|
|
const EiMatrixProduct& _ref() const { return *this; }
|
|
|
|
|
const EiMatrixProduct& _constRef() const { return *this; }
|
2007-09-26 14:06:14 +00:00
|
|
|
int _rows() const { return m_lhs.rows(); }
|
|
|
|
|
int _cols() const { return m_rhs.cols(); }
|
|
|
|
|
|
|
|
|
|
Scalar _read(int row, int col) const
|
2007-09-05 10:42:15 +00:00
|
|
|
{
|
2007-09-29 08:28:36 +00:00
|
|
|
if(Lhs::ColsAtCompileTime == 3)
|
|
|
|
|
{
|
|
|
|
|
return m_lhs(row,0) * m_rhs(0,col) + m_lhs(row,1) * m_rhs(1,col) + m_lhs(row,2) * m_rhs(2,col);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Scalar x = static_cast<Scalar>(0);
|
|
|
|
|
for(int i = 0; i < m_lhs.cols(); i++)
|
|
|
|
|
x += m_lhs.read(row, i) * m_rhs.read(i, col);
|
|
|
|
|
return x;
|
|
|
|
|
}
|
2007-09-05 10:42:15 +00:00
|
|
|
}
|
2007-09-26 14:06:14 +00:00
|
|
|
|
2007-09-05 10:42:15 +00:00
|
|
|
protected:
|
2007-09-26 14:06:14 +00:00
|
|
|
const LhsRef m_lhs;
|
|
|
|
|
const RhsRef m_rhs;
|
2007-09-05 10:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
2007-09-26 14:06:14 +00:00
|
|
|
template<typename Scalar, typename Derived1, typename Derived2>
|
2007-09-27 19:54:04 +00:00
|
|
|
EiSum<Derived1, Derived2>
|
2007-09-27 19:38:40 +00:00
|
|
|
operator+(const EiObject<Scalar, Derived1> &mat1, const EiObject<Scalar, Derived2> &mat2)
|
2007-09-26 14:06:14 +00:00
|
|
|
{
|
2007-09-29 17:27:20 +00:00
|
|
|
return EiSum<Derived1, Derived2>(mat1.constRef(), mat2.constRef());
|
2007-09-05 10:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-26 14:06:14 +00:00
|
|
|
template<typename Scalar, typename Derived1, typename Derived2>
|
2007-09-27 19:54:04 +00:00
|
|
|
EiDifference<Derived1, Derived2>
|
2007-09-27 19:38:40 +00:00
|
|
|
operator-(const EiObject<Scalar, Derived1> &mat1, const EiObject<Scalar, Derived2> &mat2)
|
2007-09-26 14:06:14 +00:00
|
|
|
{
|
2007-09-29 17:27:20 +00:00
|
|
|
return EiDifference<Derived1, Derived2>(mat1.constRef(), mat2.constRef());
|
2007-09-07 07:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-29 08:28:36 +00:00
|
|
|
template<typename Scalar, typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
EiMatrixProduct<Derived, OtherDerived>
|
|
|
|
|
EiObject<Scalar, Derived>::lazyMul(const EiObject<Scalar, OtherDerived> &other) const
|
|
|
|
|
{
|
2007-09-29 17:27:20 +00:00
|
|
|
return EiMatrixProduct<Derived, OtherDerived>(constRef(), other.constRef());
|
2007-09-29 08:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-27 19:20:06 +00:00
|
|
|
template<typename Scalar, typename Derived1, typename Derived2>
|
2007-09-29 08:28:36 +00:00
|
|
|
EiEval<EiMatrixProduct<Derived1, Derived2> >
|
2007-09-27 19:38:40 +00:00
|
|
|
operator*(const EiObject<Scalar, Derived1> &mat1, const EiObject<Scalar, Derived2> &mat2)
|
2007-09-27 19:20:06 +00:00
|
|
|
{
|
2007-09-29 08:28:36 +00:00
|
|
|
return mat1.lazyMul(mat2).eval();
|
2007-09-27 19:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-26 14:06:14 +00:00
|
|
|
template<typename Scalar, typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived &
|
2007-09-27 19:38:40 +00:00
|
|
|
EiObject<Scalar, Derived>::operator+=(const EiObject<Scalar, OtherDerived>& other)
|
2007-09-26 14:06:14 +00:00
|
|
|
{
|
2007-09-30 14:56:11 +00:00
|
|
|
return *this = *this + other;
|
2007-09-26 14:06:14 +00:00
|
|
|
}
|
2007-09-07 07:41:10 +00:00
|
|
|
|
2007-09-26 14:06:14 +00:00
|
|
|
template<typename Scalar, typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived &
|
2007-09-27 19:38:40 +00:00
|
|
|
EiObject<Scalar, Derived>::operator-=(const EiObject<Scalar, OtherDerived> &other)
|
2007-09-26 14:06:14 +00:00
|
|
|
{
|
2007-09-30 14:56:11 +00:00
|
|
|
return *this = *this - other;
|
2007-09-26 14:06:14 +00:00
|
|
|
}
|
2007-09-07 07:41:10 +00:00
|
|
|
|
2007-09-27 19:20:06 +00:00
|
|
|
template<typename Scalar, typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived &
|
2007-09-27 19:38:40 +00:00
|
|
|
EiObject<Scalar, Derived>::operator*=(const EiObject<Scalar, OtherDerived> &other)
|
2007-09-27 19:20:06 +00:00
|
|
|
{
|
2007-09-30 14:56:11 +00:00
|
|
|
return *this = *this * other;
|
2007-09-27 19:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-27 19:54:04 +00:00
|
|
|
#endif // EI_MATRIXOPS_H
|