SelfadjointProduct.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) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SELFADJOINT_PRODUCT_H
11 #define EIGEN_SELFADJOINT_PRODUCT_H
12 
13 
19 #include "../InternalHeaderCheck.h"
20 
21 namespace Eigen {
22 
23 
24 template<typename Scalar, typename Index, int UpLo, bool ConjLhs, bool ConjRhs>
25 struct selfadjoint_rank1_update<Scalar,Index,ColMajor,UpLo,ConjLhs,ConjRhs>
26 {
27  static void run(Index size, Scalar* mat, Index stride, const Scalar* vecX, const Scalar* vecY, const Scalar& alpha)
28  {
29  internal::conj_if<ConjRhs> cj;
30  typedef Map<const Matrix<Scalar,Dynamic,1> > OtherMap;
31  typedef std::conditional_t<ConjLhs,typename OtherMap::ConjugateReturnType,const OtherMap&> ConjLhsType;
32  for (Index i=0; i<size; ++i)
33  {
34  Map<Matrix<Scalar,Dynamic,1> >(mat+stride*i+(UpLo==Lower ? i : 0), (UpLo==Lower ? size-i : (i+1)))
35  += (alpha * cj(vecY[i])) * ConjLhsType(OtherMap(vecX+(UpLo==Lower ? i : 0),UpLo==Lower ? size-i : (i+1)));
36  }
37  }
38 };
39 
40 template<typename Scalar, typename Index, int UpLo, bool ConjLhs, bool ConjRhs>
41 struct selfadjoint_rank1_update<Scalar,Index,RowMajor,UpLo,ConjLhs,ConjRhs>
42 {
43  static void run(Index size, Scalar* mat, Index stride, const Scalar* vecX, const Scalar* vecY, const Scalar& alpha)
44  {
46  }
47 };
48 
49 template<typename MatrixType, typename OtherType, int UpLo, bool OtherIsVector = OtherType::IsVectorAtCompileTime>
51 
52 template<typename MatrixType, typename OtherType, int UpLo>
53 struct selfadjoint_product_selector<MatrixType,OtherType,UpLo,true>
54 {
55  static void run(MatrixType& mat, const OtherType& other, const typename MatrixType::Scalar& alpha)
56  {
57  typedef typename MatrixType::Scalar Scalar;
58  typedef internal::blas_traits<OtherType> OtherBlasTraits;
59  typedef typename OtherBlasTraits::DirectLinearAccessType ActualOtherType;
60  typedef internal::remove_all_t<ActualOtherType> ActualOtherType_;
61  internal::add_const_on_value_type_t<ActualOtherType> actualOther = OtherBlasTraits::extract(other.derived());
62 
63  Scalar actualAlpha = alpha * OtherBlasTraits::extractScalarFactor(other.derived());
64 
65  enum {
66  StorageOrder = (internal::traits<MatrixType>::Flags&RowMajorBit) ? RowMajor : ColMajor,
67  UseOtherDirectly = ActualOtherType_::InnerStrideAtCompileTime==1
68  };
69  internal::gemv_static_vector_if<Scalar,OtherType::SizeAtCompileTime,OtherType::MaxSizeAtCompileTime,!UseOtherDirectly> static_other;
70 
71  ei_declare_aligned_stack_constructed_variable(Scalar, actualOtherPtr, other.size(),
72  (UseOtherDirectly ? const_cast<Scalar*>(actualOther.data()) : static_other.data()));
73 
74  if(!UseOtherDirectly)
75  Map<typename ActualOtherType_::PlainObject>(actualOtherPtr, actualOther.size()) = actualOther;
76 
77  selfadjoint_rank1_update<Scalar,Index,StorageOrder,UpLo,
78  OtherBlasTraits::NeedToConjugate && NumTraits<Scalar>::IsComplex,
79  (!OtherBlasTraits::NeedToConjugate) && NumTraits<Scalar>::IsComplex>
80  ::run(other.size(), mat.data(), mat.outerStride(), actualOtherPtr, actualOtherPtr, actualAlpha);
81  }
82 };
83 
84 template<typename MatrixType, typename OtherType, int UpLo>
85 struct selfadjoint_product_selector<MatrixType,OtherType,UpLo,false>
86 {
87  static void run(MatrixType& mat, const OtherType& other, const typename MatrixType::Scalar& alpha)
88  {
89  typedef typename MatrixType::Scalar Scalar;
90  typedef internal::blas_traits<OtherType> OtherBlasTraits;
91  typedef typename OtherBlasTraits::DirectLinearAccessType ActualOtherType;
92  typedef internal::remove_all_t<ActualOtherType> ActualOtherType_;
93  internal::add_const_on_value_type_t<ActualOtherType> actualOther = OtherBlasTraits::extract(other.derived());
94 
95  Scalar actualAlpha = alpha * OtherBlasTraits::extractScalarFactor(other.derived());
96 
97  enum {
98  IsRowMajor = (internal::traits<MatrixType>::Flags&RowMajorBit) ? 1 : 0,
99  OtherIsRowMajor = ActualOtherType_::Flags&RowMajorBit ? 1 : 0
100  };
101 
102  Index size = mat.cols();
103  Index depth = actualOther.cols();
104 
105  typedef internal::gemm_blocking_space<IsRowMajor ? RowMajor : ColMajor,Scalar,Scalar,
106  MatrixType::MaxColsAtCompileTime, MatrixType::MaxColsAtCompileTime, ActualOtherType_::MaxColsAtCompileTime> BlockingType;
107 
108  BlockingType blocking(size, size, depth, 1, false);
109 
110 
111  internal::general_matrix_matrix_triangular_product<Index,
112  Scalar, OtherIsRowMajor ? RowMajor : ColMajor, OtherBlasTraits::NeedToConjugate && NumTraits<Scalar>::IsComplex,
113  Scalar, OtherIsRowMajor ? ColMajor : RowMajor, (!OtherBlasTraits::NeedToConjugate) && NumTraits<Scalar>::IsComplex,
114  IsRowMajor ? RowMajor : ColMajor, MatrixType::InnerStrideAtCompileTime, UpLo>
115  ::run(size, depth,
116  actualOther.data(), actualOther.outerStride(), actualOther.data(), actualOther.outerStride(),
117  mat.data(), mat.innerStride(), mat.outerStride(), actualAlpha, blocking);
118  }
119 };
120 
121 // high level API
122 
123 template<typename MatrixType, unsigned int UpLo>
124 template<typename DerivedU>
125 EIGEN_DEVICE_FUNC SelfAdjointView<MatrixType,UpLo>& SelfAdjointView<MatrixType,UpLo>
126 ::rankUpdate(const MatrixBase<DerivedU>& u, const Scalar& alpha)
127 {
128  selfadjoint_product_selector<MatrixType,DerivedU,UpLo>::run(_expression().const_cast_derived(), u.derived(), alpha);
129 
130  return *this;
131 }
132 
133 } // end namespace Eigen
134 
135 #endif // EIGEN_SELFADJOINT_PRODUCT_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:847
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
SelfAdjointView & rankUpdate(const MatrixBase< DerivedU > &u, const MatrixBase< DerivedV > &v, const Scalar &alpha=Scalar(1))
internal::traits< SelfAdjointView >::Scalar Scalar
The type of coefficients in this matrix.
@ Lower
Definition: Constants.h:211
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
const unsigned int RowMajorBit
Definition: Constants.h:68
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
typename add_const_on_value_type< T >::type add_const_on_value_type_t
Definition: Meta.h:176
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231
static void run(MatrixType &mat, const OtherType &other, const typename MatrixType::Scalar &alpha)
static void run(MatrixType &mat, const OtherType &other, const typename MatrixType::Scalar &alpha)
static void run(Index size, Scalar *mat, Index stride, const Scalar *vecX, const Scalar *vecY, const Scalar &alpha)
static void run(Index size, Scalar *mat, Index stride, const Scalar *vecX, const Scalar *vecY, const Scalar &alpha)