DiagonalProduct.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_DIAGONALPRODUCT_H
27 #define EIGEN_DIAGONALPRODUCT_H
28 
29 namespace Eigen {
30 
31 namespace internal {
32 template<typename MatrixType, typename DiagonalType, int ProductOrder>
33 struct traits<DiagonalProduct<MatrixType, DiagonalType, ProductOrder> >
34  : traits<MatrixType>
35 {
36  typedef typename scalar_product_traits<typename MatrixType::Scalar, typename DiagonalType::Scalar>::ReturnType Scalar;
37  enum {
38  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
39  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
40  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
41  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
42 
43  _StorageOrder = MatrixType::Flags & RowMajorBit ? RowMajor : ColMajor,
44  _PacketOnDiag = !((int(_StorageOrder) == RowMajor && int(ProductOrder) == OnTheLeft)
45  ||(int(_StorageOrder) == ColMajor && int(ProductOrder) == OnTheRight)),
46  _SameTypes = is_same<typename MatrixType::Scalar, typename DiagonalType::Scalar>::value,
47  // FIXME currently we need same types, but in the future the next rule should be the one
48  //_Vectorizable = bool(int(MatrixType::Flags)&PacketAccessBit) && ((!_PacketOnDiag) || (_SameTypes && bool(int(DiagonalType::Flags)&PacketAccessBit))),
49  _Vectorizable = bool(int(MatrixType::Flags)&PacketAccessBit) && _SameTypes && ((!_PacketOnDiag) || (bool(int(DiagonalType::Flags)&PacketAccessBit))),
50 
51  Flags = (HereditaryBits & (unsigned int)(MatrixType::Flags)) | (_Vectorizable ? PacketAccessBit : 0),
52  CoeffReadCost = NumTraits<Scalar>::MulCost + MatrixType::CoeffReadCost + DiagonalType::DiagonalVectorType::CoeffReadCost
53  };
54 };
55 }
56 
57 template<typename MatrixType, typename DiagonalType, int ProductOrder>
58 class DiagonalProduct : internal::no_assignment_operator,
59  public MatrixBase<DiagonalProduct<MatrixType, DiagonalType, ProductOrder> >
60 {
61  public:
62 
65 
66  inline DiagonalProduct(const MatrixType& matrix, const DiagonalType& diagonal)
67  : m_matrix(matrix), m_diagonal(diagonal)
68  {
69  eigen_assert(diagonal.diagonal().size() == (ProductOrder == OnTheLeft ? matrix.rows() : matrix.cols()));
70  }
71 
72  inline Index rows() const { return m_matrix.rows(); }
73  inline Index cols() const { return m_matrix.cols(); }
74 
75  const Scalar coeff(Index row, Index col) const
76  {
77  return m_diagonal.diagonal().coeff(ProductOrder == OnTheLeft ? row : col) * m_matrix.coeff(row, col);
78  }
79 
80  template<int LoadMode>
82  {
83  enum {
84  StorageOrder = Flags & RowMajorBit ? RowMajor : ColMajor
85  };
86  const Index indexInDiagonalVector = ProductOrder == OnTheLeft ? row : col;
87 
88  return packet_impl<LoadMode>(row,col,indexInDiagonalVector,typename internal::conditional<
89  ((int(StorageOrder) == RowMajor && int(ProductOrder) == OnTheLeft)
90  ||(int(StorageOrder) == ColMajor && int(ProductOrder) == OnTheRight)), internal::true_type, internal::false_type>::type());
91  }
92 
93  protected:
94  template<int LoadMode>
96  {
97  return internal::pmul(m_matrix.template packet<LoadMode>(row, col),
98  internal::pset1<PacketScalar>(m_diagonal.diagonal().coeff(id)));
99  }
100 
101  template<int LoadMode>
103  {
104  enum {
105  InnerSize = (MatrixType::Flags & RowMajorBit) ? MatrixType::ColsAtCompileTime : MatrixType::RowsAtCompileTime,
106  DiagonalVectorPacketLoadMode = (LoadMode == Aligned && ((InnerSize%16) == 0)) ? Aligned : Unaligned
107  };
108  return internal::pmul(m_matrix.template packet<LoadMode>(row, col),
109  m_diagonal.diagonal().template packet<DiagonalVectorPacketLoadMode>(id));
110  }
111 
112  typename MatrixType::Nested m_matrix;
113  typename DiagonalType::Nested m_diagonal;
114 };
115 
118 template<typename Derived>
119 template<typename DiagonalDerived>
122 {
123  return DiagonalProduct<Derived, DiagonalDerived, OnTheRight>(derived(), diagonal.derived());
124 }
125 
128 template<typename DiagonalDerived>
129 template<typename MatrixDerived>
132 {
133  return DiagonalProduct<MatrixDerived, DiagonalDerived, OnTheLeft>(matrix.derived(), derived());
134 }
135 
136 } // end namespace Eigen
137 
138 #endif // EIGEN_DIAGONALPRODUCT_H