2008-03-12 17:17:36 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2007-10-13 14:31:27 +00:00
|
|
|
// for linear algebra. Eigen itself is part of the KDE project.
|
|
|
|
|
//
|
2008-01-07 09:34:21 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
|
2008-03-26 09:13:11 +00:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
2007-10-13 14:31:27 +00:00
|
|
|
//
|
2008-02-28 15:44:45 +00:00
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-03-04 12:34:58 +00:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-28 15:44:45 +00:00
|
|
|
// 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
|
2008-03-04 12:34:58 +00:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-28 15:44:45 +00:00
|
|
|
// the License, or (at your option) any later version.
|
2007-10-13 14:31:27 +00:00
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2008-02-28 15:44:45 +00:00
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
2007-10-13 14:31:27 +00:00
|
|
|
//
|
2008-03-04 12:34:58 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-28 15:44:45 +00:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-13 14:31:27 +00:00
|
|
|
|
2007-11-26 08:47:07 +00:00
|
|
|
#ifndef EIGEN_PRODUCT_H
|
|
|
|
|
#define EIGEN_PRODUCT_H
|
2007-10-13 14:31:27 +00:00
|
|
|
|
|
|
|
|
template<int Index, int Size, typename Lhs, typename Rhs>
|
2008-03-13 09:33:26 +00:00
|
|
|
struct ei_product_unroller
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
|
|
|
|
static void run(int row, int col, const Lhs& lhs, const Rhs& rhs,
|
2007-12-11 14:57:42 +00:00
|
|
|
typename Lhs::Scalar &res)
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
2008-03-13 09:33:26 +00:00
|
|
|
ei_product_unroller<Index-1, Size, Lhs, Rhs>::run(row, col, lhs, rhs, res);
|
2007-12-17 07:25:11 +00:00
|
|
|
res += lhs.coeff(row, Index) * rhs.coeff(Index, col);
|
2007-10-13 14:31:27 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<int Size, typename Lhs, typename Rhs>
|
2008-03-13 09:33:26 +00:00
|
|
|
struct ei_product_unroller<0, Size, Lhs, Rhs>
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
|
|
|
|
static void run(int row, int col, const Lhs& lhs, const Rhs& rhs,
|
2007-12-11 14:57:42 +00:00
|
|
|
typename Lhs::Scalar &res)
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
2007-12-17 07:25:11 +00:00
|
|
|
res = lhs.coeff(row, 0) * rhs.coeff(0, col);
|
2007-10-13 14:31:27 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<int Index, typename Lhs, typename Rhs>
|
2008-03-13 09:33:26 +00:00
|
|
|
struct ei_product_unroller<Index, Dynamic, Lhs, Rhs>
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
2007-12-11 15:25:43 +00:00
|
|
|
static void run(int, int, const Lhs&, const Rhs&, typename Lhs::Scalar&) {}
|
2007-10-13 14:31:27 +00:00
|
|
|
};
|
|
|
|
|
|
2007-12-11 10:04:39 +00:00
|
|
|
// prevent buggy user code from causing an infinite recursion
|
|
|
|
|
template<int Index, typename Lhs, typename Rhs>
|
2008-03-13 09:33:26 +00:00
|
|
|
struct ei_product_unroller<Index, 0, Lhs, Rhs>
|
2007-12-11 10:04:39 +00:00
|
|
|
{
|
2007-12-11 15:25:43 +00:00
|
|
|
static void run(int, int, const Lhs&, const Rhs&, typename Lhs::Scalar&) {}
|
2007-12-11 10:04:39 +00:00
|
|
|
};
|
|
|
|
|
|
2008-01-07 09:34:21 +00:00
|
|
|
/** \class Product
|
|
|
|
|
*
|
|
|
|
|
* \brief Expression of the product of two matrices
|
|
|
|
|
*
|
|
|
|
|
* \param Lhs the type of the left-hand side
|
|
|
|
|
* \param Rhs the type of the right-hand side
|
2008-03-21 20:26:14 +00:00
|
|
|
* \param EvalMode internal use only
|
2008-01-07 09:34:21 +00:00
|
|
|
*
|
|
|
|
|
* This class represents an expression of the product of two matrices.
|
2008-03-31 16:20:06 +00:00
|
|
|
* It is the return type of the operator* between matrices, and most of the time
|
|
|
|
|
* this is the only way it is used.
|
2008-01-07 09:34:21 +00:00
|
|
|
*
|
|
|
|
|
* \sa class Sum, class Difference
|
|
|
|
|
*/
|
2008-04-03 16:54:19 +00:00
|
|
|
template<typename Lhs, typename Rhs> struct ei_product_eval_mode
|
|
|
|
|
{
|
|
|
|
|
enum{ value = Lhs::MaxRowsAtCompileTime >= 8 && Rhs::MaxColsAtCompileTime >= 8
|
|
|
|
|
? CacheOptimal : UnrolledDotProduct };
|
|
|
|
|
};
|
|
|
|
|
|
2008-03-21 20:26:14 +00:00
|
|
|
template<typename Lhs, typename Rhs, int EvalMode>
|
|
|
|
|
struct ei_traits<Product<Lhs, Rhs, EvalMode> >
|
2008-03-12 17:17:36 +00:00
|
|
|
{
|
|
|
|
|
typedef typename Lhs::Scalar Scalar;
|
|
|
|
|
enum {
|
|
|
|
|
RowsAtCompileTime = Lhs::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = Rhs::ColsAtCompileTime,
|
|
|
|
|
MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime,
|
2008-03-30 18:43:22 +00:00
|
|
|
MaxColsAtCompileTime = Rhs::MaxColsAtCompileTime,
|
2008-04-03 16:54:19 +00:00
|
|
|
Flags = ( (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic)
|
|
|
|
|
? (unsigned int)(Lhs::Flags | Rhs::Flags)
|
|
|
|
|
: (unsigned int)(Lhs::Flags | Rhs::Flags) & ~LargeBit )
|
|
|
|
|
| EvalBeforeAssigningBit
|
|
|
|
|
| (ei_product_eval_mode<Lhs, Rhs>::value == (int)CacheOptimal ? EvalBeforeNestingBit : 0),
|
2008-04-03 11:10:17 +00:00
|
|
|
CoeffReadCost
|
|
|
|
|
= Lhs::ColsAtCompileTime == Dynamic
|
|
|
|
|
? Dynamic
|
|
|
|
|
: Lhs::ColsAtCompileTime
|
2008-04-03 14:17:56 +00:00
|
|
|
* (NumTraits<Scalar>::MulCost + Lhs::CoeffReadCost + Rhs::CoeffReadCost)
|
2008-04-03 11:10:17 +00:00
|
|
|
+ (Lhs::ColsAtCompileTime - 1) * NumTraits<Scalar>::AddCost
|
2008-03-12 17:17:36 +00:00
|
|
|
};
|
|
|
|
|
};
|
2008-03-10 17:23:11 +00:00
|
|
|
|
2008-03-21 20:26:14 +00:00
|
|
|
template<typename Lhs, typename Rhs, int EvalMode> class Product : ei_no_assignment_operator,
|
|
|
|
|
public MatrixBase<Product<Lhs, Rhs, EvalMode> >
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2008-03-12 17:17:36 +00:00
|
|
|
|
2008-03-13 09:33:26 +00:00
|
|
|
EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
|
2008-04-03 16:54:19 +00:00
|
|
|
|
2008-04-05 11:10:54 +00:00
|
|
|
typedef typename ei_eval_if_needed_before_nesting<Lhs,Rhs::ColsAtCompileTime>::CopyType CopyLhs;
|
|
|
|
|
typedef typename ei_eval_if_needed_before_nesting<Rhs,Lhs::RowsAtCompileTime>::CopyType CopyRhs;
|
|
|
|
|
typedef typename ei_eval_if_needed_before_nesting<Lhs,Rhs::ColsAtCompileTime>::XprType XprLhs;
|
|
|
|
|
typedef typename ei_eval_if_needed_before_nesting<Rhs,Lhs::RowsAtCompileTime>::XprType XprRhs;
|
|
|
|
|
|
|
|
|
|
Product(const Lhs& lhs, const Rhs& rhs)
|
2008-03-04 12:34:58 +00:00
|
|
|
: m_lhs(lhs), m_rhs(rhs)
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
2008-03-26 08:48:04 +00:00
|
|
|
ei_assert(lhs.cols() == rhs.rows());
|
2007-10-13 14:31:27 +00:00
|
|
|
}
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2008-03-21 20:26:14 +00:00
|
|
|
/** \internal */
|
|
|
|
|
template<typename DestDerived>
|
|
|
|
|
void _cacheOptimalEval(DestDerived& res) const;
|
|
|
|
|
|
2007-10-13 14:31:27 +00:00
|
|
|
private:
|
2007-12-19 09:30:53 +00:00
|
|
|
|
2007-10-13 14:31:27 +00:00
|
|
|
int _rows() const { return m_lhs.rows(); }
|
|
|
|
|
int _cols() const { return m_rhs.cols(); }
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2008-03-30 18:43:22 +00:00
|
|
|
const Scalar _coeff(int row, int col) const
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
|
|
|
|
Scalar res;
|
2007-12-11 10:04:39 +00:00
|
|
|
if(EIGEN_UNROLLED_LOOPS
|
2008-03-12 17:17:36 +00:00
|
|
|
&& Lhs::ColsAtCompileTime != Dynamic
|
2008-03-13 09:33:26 +00:00
|
|
|
&& Lhs::ColsAtCompileTime <= EIGEN_UNROLLING_LIMIT)
|
|
|
|
|
ei_product_unroller<Lhs::ColsAtCompileTime-1,
|
2008-03-13 20:36:01 +00:00
|
|
|
Lhs::ColsAtCompileTime <= EIGEN_UNROLLING_LIMIT
|
|
|
|
|
? Lhs::ColsAtCompileTime : Dynamic,
|
2008-04-05 11:10:54 +00:00
|
|
|
XprLhs, XprRhs>
|
2007-10-13 14:31:27 +00:00
|
|
|
::run(row, col, m_lhs, m_rhs, res);
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-12-17 07:25:11 +00:00
|
|
|
res = m_lhs.coeff(row, 0) * m_rhs.coeff(0, col);
|
2007-10-13 14:31:27 +00:00
|
|
|
for(int i = 1; i < m_lhs.cols(); i++)
|
2007-12-17 07:25:11 +00:00
|
|
|
res += m_lhs.coeff(row, i) * m_rhs.coeff(i, col);
|
2007-10-13 14:31:27 +00:00
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2007-10-13 14:31:27 +00:00
|
|
|
protected:
|
2008-04-05 11:10:54 +00:00
|
|
|
const CopyLhs m_lhs;
|
|
|
|
|
const CopyRhs m_rhs;
|
2007-10-13 14:31:27 +00:00
|
|
|
};
|
|
|
|
|
|
2008-03-14 10:38:37 +00:00
|
|
|
/** \returns the matrix product of \c *this and \a other.
|
2007-12-27 21:43:10 +00:00
|
|
|
*
|
|
|
|
|
* \note This function causes an immediate evaluation. If you want to perform a matrix product
|
2008-03-31 16:20:06 +00:00
|
|
|
* without immediate evaluation, call .lazy() on one of the matrices before taking the product.
|
2007-12-27 21:43:10 +00:00
|
|
|
*
|
2008-03-31 16:20:06 +00:00
|
|
|
* \sa lazy(), operator*=(const MatrixBase&)
|
2007-12-27 21:43:10 +00:00
|
|
|
*/
|
2008-03-14 10:38:37 +00:00
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
2008-04-05 11:10:54 +00:00
|
|
|
const Product<Derived,OtherDerived>
|
2008-03-14 10:38:37 +00:00
|
|
|
MatrixBase<Derived>::operator*(const MatrixBase<OtherDerived> &other) const
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
2008-04-05 11:10:54 +00:00
|
|
|
return Product<Derived,OtherDerived>(derived(), other.derived());
|
2007-10-13 14:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-07 09:34:21 +00:00
|
|
|
/** replaces \c *this by \c *this * \a other.
|
|
|
|
|
*
|
|
|
|
|
* \returns a reference to \c *this
|
|
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2007-10-13 14:31:27 +00:00
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived &
|
2008-03-10 17:23:11 +00:00
|
|
|
MatrixBase<Derived>::operator*=(const MatrixBase<OtherDerived> &other)
|
2007-10-13 14:31:27 +00:00
|
|
|
{
|
|
|
|
|
return *this = *this * other;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-21 20:26:14 +00:00
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-05 11:10:54 +00:00
|
|
|
Derived& MatrixBase<Derived>::lazyAssign(const Product<Derived1,Derived2,CacheOptimal>& product)
|
2008-03-21 20:26:14 +00:00
|
|
|
{
|
|
|
|
|
product._cacheOptimalEval(*this);
|
2008-03-26 08:48:04 +00:00
|
|
|
return derived();
|
2008-03-21 20:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs, int EvalMode>
|
|
|
|
|
template<typename DestDerived>
|
|
|
|
|
void Product<Lhs,Rhs,EvalMode>::_cacheOptimalEval(DestDerived& res) const
|
|
|
|
|
{
|
|
|
|
|
res.setZero();
|
|
|
|
|
const int cols4 = m_lhs.cols()&0xfffffffC;
|
|
|
|
|
for (int k=0; k<m_rhs.cols(); ++k)
|
|
|
|
|
{
|
|
|
|
|
int j=0;
|
|
|
|
|
for (; j<cols4; j+=4)
|
|
|
|
|
{
|
|
|
|
|
const Scalar tmp0 = m_rhs.coeff(j ,k);
|
|
|
|
|
const Scalar tmp1 = m_rhs.coeff(j+1,k);
|
|
|
|
|
const Scalar tmp2 = m_rhs.coeff(j+2,k);
|
|
|
|
|
const Scalar tmp3 = m_rhs.coeff(j+3,k);
|
|
|
|
|
for (int i=0; i<m_lhs.rows(); ++i)
|
2008-04-03 11:10:17 +00:00
|
|
|
res.coeffRef(i,k) += tmp0 * m_lhs.coeff(i,j) + tmp1 * m_lhs.coeff(i,j+1)
|
2008-04-05 11:10:54 +00:00
|
|
|
+ tmp2 * m_lhs.coeff(i,j+2) + tmp3 * m_lhs.coeff(i,j+3);
|
2008-03-21 20:26:14 +00:00
|
|
|
}
|
|
|
|
|
for (; j<m_lhs.cols(); ++j)
|
|
|
|
|
{
|
|
|
|
|
const Scalar tmp = m_rhs.coeff(j,k);
|
|
|
|
|
for (int i=0; i<m_lhs.rows(); ++i)
|
|
|
|
|
res.coeffRef(i,k) += tmp * m_lhs.coeff(i,j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 08:47:07 +00:00
|
|
|
#endif // EIGEN_PRODUCT_H
|