2008-03-12 17:17:36 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2007-10-13 14:31:27 +00:00
|
|
|
//
|
2011-03-23 16:12:21 +01:00
|
|
|
// Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2007-10-13 14:31:27 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
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
|
|
|
|
2012-06-22 16:35:20 +02:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2013-11-29 17:50:59 +01:00
|
|
|
template<typename Lhs, typename Rhs, int Option, typename StorageKind> class ProductImpl;
|
2011-03-23 16:12:21 +01:00
|
|
|
|
|
|
|
|
/** \class Product
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2009-08-05 15:23:35 +02:00
|
|
|
*
|
2011-03-23 16:12:21 +01:00
|
|
|
* \brief Expression of the product of two arbitrary matrices or vectors
|
2009-08-05 15:23:35 +02:00
|
|
|
*
|
2011-03-23 16:12:21 +01:00
|
|
|
* \param Lhs the type of the left-hand side expression
|
|
|
|
|
* \param Rhs the type of the right-hand side expression
|
2009-08-05 15:23:35 +02:00
|
|
|
*
|
2011-03-23 16:12:21 +01:00
|
|
|
* This class represents an expression of the product of two arbitrary matrices.
|
2013-11-27 17:32:57 +01:00
|
|
|
*
|
|
|
|
|
* The other template parameters are:
|
|
|
|
|
* \tparam Option can be DefaultProduct or LazyProduct
|
2009-08-05 15:23:35 +02:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2012-06-29 13:07:21 +01:00
|
|
|
// Use ProductReturnType to get correct traits, in particular vectorization flags
|
2010-10-25 10:15:22 -04:00
|
|
|
namespace internal {
|
2013-11-29 17:50:59 +01:00
|
|
|
template<typename Lhs, typename Rhs, int Option>
|
|
|
|
|
struct traits<Product<Lhs, Rhs, Option> >
|
2013-12-02 17:53:26 +01:00
|
|
|
: traits<CoeffBasedProduct<Lhs, Rhs, NestByRefBit> >
|
2012-06-29 13:07:21 +01:00
|
|
|
{
|
|
|
|
|
// We want A+B*C to be of type Product<Matrix, Sum> and not Product<Matrix, Matrix>
|
|
|
|
|
// TODO: This flag should eventually go in a separate evaluator traits class
|
2009-08-20 12:19:15 -04:00
|
|
|
enum {
|
2013-12-02 17:53:26 +01:00
|
|
|
Flags = traits<CoeffBasedProduct<Lhs, Rhs, NestByRefBit> >::Flags & ~(EvalBeforeNestingBit | DirectAccessBit)
|
2009-08-05 15:23:35 +02:00
|
|
|
};
|
|
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
} // end namespace internal
|
2008-05-31 15:06:26 +00:00
|
|
|
|
2009-08-05 15:23:35 +02:00
|
|
|
|
2013-11-29 17:50:59 +01:00
|
|
|
template<typename _Lhs, typename _Rhs, int Option>
|
|
|
|
|
class Product : public ProductImpl<_Lhs,_Rhs,Option,
|
|
|
|
|
typename internal::promote_storage_type<typename internal::traits<_Lhs>::StorageKind,
|
|
|
|
|
typename internal::traits<_Rhs>::StorageKind>::ret>
|
2009-08-05 15:23:35 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2011-03-23 16:12:21 +01:00
|
|
|
|
2013-11-29 17:50:59 +01:00
|
|
|
typedef _Lhs Lhs;
|
|
|
|
|
typedef _Rhs Rhs;
|
|
|
|
|
|
2011-03-23 16:12:21 +01:00
|
|
|
typedef typename ProductImpl<
|
2013-11-29 17:50:59 +01:00
|
|
|
Lhs, Rhs, Option,
|
2011-03-23 16:12:21 +01:00
|
|
|
typename internal::promote_storage_type<typename Lhs::StorageKind,
|
|
|
|
|
typename Rhs::StorageKind>::ret>::Base Base;
|
|
|
|
|
EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
|
2013-11-29 17:50:59 +01:00
|
|
|
|
|
|
|
|
|
2011-03-23 16:12:21 +01:00
|
|
|
|
|
|
|
|
typedef typename Lhs::Nested LhsNested;
|
|
|
|
|
typedef typename Rhs::Nested RhsNested;
|
|
|
|
|
typedef typename internal::remove_all<LhsNested>::type LhsNestedCleaned;
|
|
|
|
|
typedef typename internal::remove_all<RhsNested>::type RhsNestedCleaned;
|
|
|
|
|
|
|
|
|
|
Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs)
|
2009-09-03 13:03:26 +02:00
|
|
|
{
|
2011-03-23 16:12:21 +01:00
|
|
|
eigen_assert(lhs.cols() == rhs.rows()
|
|
|
|
|
&& "invalid matrix product"
|
|
|
|
|
&& "if you wanted a coeff-wise or a dot product use the respective explicit functions");
|
2009-09-03 13:03:26 +02:00
|
|
|
}
|
2009-08-05 15:23:35 +02:00
|
|
|
|
2011-03-23 16:12:21 +01:00
|
|
|
inline Index rows() const { return m_lhs.rows(); }
|
|
|
|
|
inline Index cols() const { return m_rhs.cols(); }
|
2009-08-05 15:23:35 +02:00
|
|
|
|
2011-03-23 16:12:21 +01:00
|
|
|
const LhsNestedCleaned& lhs() const { return m_lhs; }
|
|
|
|
|
const RhsNestedCleaned& rhs() const { return m_rhs; }
|
2010-10-25 10:15:22 -04:00
|
|
|
|
2011-03-23 16:12:21 +01:00
|
|
|
protected:
|
2009-08-05 17:39:11 +02:00
|
|
|
|
2013-02-28 19:29:32 +01:00
|
|
|
LhsNested m_lhs;
|
|
|
|
|
RhsNested m_rhs;
|
2009-08-05 17:39:11 +02:00
|
|
|
};
|
|
|
|
|
|
2013-12-04 22:58:19 +01:00
|
|
|
namespace internal {
|
|
|
|
|
|
2013-12-14 22:53:47 +01:00
|
|
|
template<typename Lhs, typename Rhs, int Option, int ProductTag = internal::product_type<Lhs,Rhs>::ret>
|
2013-12-04 22:58:19 +01:00
|
|
|
class dense_product_base
|
|
|
|
|
: public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
|
|
|
|
|
{};
|
|
|
|
|
|
|
|
|
|
/** Convertion to scalar for inner-products */
|
2013-11-29 17:50:59 +01:00
|
|
|
template<typename Lhs, typename Rhs, int Option>
|
2013-12-04 22:58:19 +01:00
|
|
|
class dense_product_base<Lhs, Rhs, Option, InnerProduct>
|
|
|
|
|
: public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
|
|
|
|
|
{
|
|
|
|
|
typedef Product<Lhs,Rhs,Option> ProductXpr;
|
|
|
|
|
typedef typename internal::dense_xpr_base<ProductXpr>::type Base;
|
|
|
|
|
public:
|
|
|
|
|
using Base::derived;
|
|
|
|
|
typedef typename Base::Scalar Scalar;
|
|
|
|
|
typedef typename Base::Index Index;
|
|
|
|
|
|
|
|
|
|
operator const Scalar() const
|
|
|
|
|
{
|
|
|
|
|
return typename internal::evaluator<ProductXpr>::type(derived()).coeff(0,0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs, int Option>
|
|
|
|
|
class ProductImpl<Lhs,Rhs,Option,Dense>
|
|
|
|
|
: public internal::dense_product_base<Lhs,Rhs,Option>
|
2009-08-05 15:23:35 +02:00
|
|
|
{
|
2014-02-18 14:51:41 +01:00
|
|
|
typedef Product<Lhs, Rhs, Option> Derived;
|
|
|
|
|
|
2009-08-05 15:23:35 +02:00
|
|
|
public:
|
2013-12-04 22:58:19 +01:00
|
|
|
|
|
|
|
|
typedef typename internal::dense_product_base<Lhs, Rhs, Option> Base;
|
2011-03-23 16:12:21 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
|
2014-02-18 14:51:41 +01:00
|
|
|
protected:
|
|
|
|
|
enum {
|
|
|
|
|
IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
|
|
|
|
|
(ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
|
|
|
|
|
EnableCoeff = IsOneByOne || Option==LazyProduct
|
|
|
|
|
};
|
2014-02-18 13:32:30 +01:00
|
|
|
|
2014-02-18 14:51:41 +01:00
|
|
|
public:
|
|
|
|
|
|
2014-02-18 13:32:30 +01:00
|
|
|
Scalar coeff(Index row, Index col) const
|
|
|
|
|
{
|
2014-02-18 14:51:41 +01:00
|
|
|
EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
|
|
|
|
|
eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
|
2014-02-18 13:32:30 +01:00
|
|
|
|
|
|
|
|
return typename internal::evaluator<Derived>::type(derived()).coeff(row,col);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scalar coeff(Index i) const
|
|
|
|
|
{
|
2014-02-18 14:51:41 +01:00
|
|
|
EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
|
|
|
|
|
eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
|
2014-02-18 13:32:30 +01:00
|
|
|
|
|
|
|
|
return typename internal::evaluator<Derived>::type(derived()).coeff(i);
|
|
|
|
|
}
|
2014-02-18 14:51:41 +01:00
|
|
|
|
|
|
|
|
|
2009-08-05 15:23:35 +02:00
|
|
|
};
|
|
|
|
|
|
2012-06-22 09:39:35 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Implementation of matrix base methods
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** \internal used to test the evaluator only
|
|
|
|
|
*/
|
|
|
|
|
template<typename Lhs,typename Rhs>
|
|
|
|
|
const Product<Lhs,Rhs>
|
|
|
|
|
prod(const Lhs& lhs, const Rhs& rhs)
|
|
|
|
|
{
|
|
|
|
|
return Product<Lhs,Rhs>(lhs,rhs);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-27 17:32:57 +01:00
|
|
|
/** \internal used to test the evaluator only
|
|
|
|
|
*/
|
|
|
|
|
template<typename Lhs,typename Rhs>
|
|
|
|
|
const Product<Lhs,Rhs,LazyProduct>
|
|
|
|
|
lazyprod(const Lhs& lhs, const Rhs& rhs)
|
|
|
|
|
{
|
|
|
|
|
return Product<Lhs,Rhs,LazyProduct>(lhs,rhs);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-22 16:35:20 +02:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2007-11-26 08:47:07 +00:00
|
|
|
#endif // EIGEN_PRODUCT_H
|