10 #ifndef EIGEN_SELFADJOINT_MATRIX_VECTOR_H
11 #define EIGEN_SELFADJOINT_MATRIX_VECTOR_H
23 template<
typename Scalar,
typename Index,
int StorageOrder,
int UpLo,
bool ConjugateLhs,
bool ConjugateRhs,
int Version=Specialized>
24 struct selfadjoint_matrix_vector_product;
26 template<
typename Scalar,
typename Index,
int StorageOrder,
int UpLo,
bool ConjugateLhs,
bool ConjugateRhs,
int Version>
27 struct selfadjoint_matrix_vector_product
30 static EIGEN_DONT_INLINE
void run(
32 const Scalar* lhs, Index lhsStride,
33 const Scalar* _rhs, Index rhsIncr,
37 typedef typename packet_traits<Scalar>::type Packet;
38 typedef typename NumTraits<Scalar>::Real RealScalar;
39 const Index PacketSize =
sizeof(Packet)/
sizeof(Scalar);
42 IsRowMajor = StorageOrder==
RowMajor ? 1 : 0,
43 IsLower = UpLo ==
Lower ? 1 : 0,
44 FirstTriangular = IsRowMajor == IsLower
47 conj_helper<Scalar,Scalar,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, IsRowMajor), ConjugateRhs> cj0;
48 conj_helper<Scalar,Scalar,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, !IsRowMajor), ConjugateRhs> cj1;
49 conj_helper<Scalar,Scalar,NumTraits<Scalar>::IsComplex, ConjugateRhs> cjd;
51 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, IsRowMajor), ConjugateRhs> pcj0;
52 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, !IsRowMajor), ConjugateRhs> pcj1;
54 Scalar cjAlpha = ConjugateRhs ?
conj(alpha) : alpha;
59 ei_declare_aligned_stack_constructed_variable(Scalar,rhs,size,rhsIncr==1 ? const_cast<Scalar*>(_rhs) : 0);
62 const Scalar* it = _rhs;
63 for (Index i=0; i<size; ++i, it+=rhsIncr)
67 Index bound = (std::max)(Index(0),size-8) & 0xfffffffe;
71 for (Index j=FirstTriangular ? bound : 0;
72 j<(FirstTriangular ? size : bound);j+=2)
74 register const Scalar* EIGEN_RESTRICT A0 = lhs + j*lhsStride;
75 register const Scalar* EIGEN_RESTRICT A1 = lhs + (j+1)*lhsStride;
77 Scalar t0 = cjAlpha * rhs[j];
78 Packet ptmp0 = pset1<Packet>(t0);
79 Scalar t1 = cjAlpha * rhs[j+1];
80 Packet ptmp1 = pset1<Packet>(t1);
83 Packet ptmp2 = pset1<Packet>(t2);
85 Packet ptmp3 = pset1<Packet>(t3);
87 size_t starti = FirstTriangular ? 0 : j+2;
88 size_t endi = FirstTriangular ? j : size;
89 size_t alignedStart = (starti) + internal::first_aligned(&res[starti], endi-starti);
90 size_t alignedEnd = alignedStart + ((endi-alignedStart)/(PacketSize))*(PacketSize);
97 res[j] += cj0.pmul(A1[j], t1);
98 t3 += cj1.pmul(A1[j], rhs[j]);
102 res[j+1] += cj0.pmul(A0[j+1],t0);
103 t2 += cj1.pmul(A0[j+1], rhs[j+1]);
106 for (
size_t i=starti; i<alignedStart; ++i)
108 res[i] += t0 * A0[i] + t1 * A1[i];
109 t2 +=
conj(A0[i]) * rhs[i];
110 t3 +=
conj(A1[i]) * rhs[i];
114 const Scalar* EIGEN_RESTRICT a0It = A0 + alignedStart;
115 const Scalar* EIGEN_RESTRICT a1It = A1 + alignedStart;
116 const Scalar* EIGEN_RESTRICT rhsIt = rhs + alignedStart;
117 Scalar* EIGEN_RESTRICT resIt = res + alignedStart;
118 for (
size_t i=alignedStart; i<alignedEnd; i+=PacketSize)
120 Packet A0i = ploadu<Packet>(a0It); a0It += PacketSize;
121 Packet A1i = ploadu<Packet>(a1It); a1It += PacketSize;
122 Packet Bi = ploadu<Packet>(rhsIt); rhsIt += PacketSize;
123 Packet Xi = pload <Packet>(resIt);
125 Xi = pcj0.pmadd(A0i,ptmp0, pcj0.pmadd(A1i,ptmp1,Xi));
126 ptmp2 = pcj1.pmadd(A0i, Bi, ptmp2);
127 ptmp3 = pcj1.pmadd(A1i, Bi, ptmp3);
128 pstore(resIt,Xi); resIt += PacketSize;
130 for (
size_t i=alignedEnd; i<endi; i++)
132 res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i],t1);
133 t2 += cj1.pmul(A0[i], rhs[i]);
134 t3 += cj1.pmul(A1[i], rhs[i]);
137 res[j] += alpha * (t2 + predux(ptmp2));
138 res[j+1] += alpha * (t3 + predux(ptmp3));
140 for (Index j=FirstTriangular ? 0 : bound;j<(FirstTriangular ? bound : size);j++)
142 register const Scalar* EIGEN_RESTRICT A0 = lhs + j*lhsStride;
144 Scalar t1 = cjAlpha * rhs[j];
148 for (Index i=FirstTriangular ? 0 : j+1; i<(FirstTriangular ? j : size); i++)
150 res[i] += cj0.pmul(A0[i], t1);
151 t2 += cj1.pmul(A0[i], rhs[i]);
153 res[j] += alpha * t2;
165 template<
typename Lhs,
int LhsMode,
typename Rhs>
166 struct traits<SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true> >
167 : traits<ProductBase<SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true>, Lhs, Rhs> >
171 template<
typename Lhs,
int LhsMode,
typename Rhs>
172 struct SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true>
173 :
public ProductBase<SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true>, Lhs, Rhs >
175 EIGEN_PRODUCT_PUBLIC_INTERFACE(SelfadjointProductMatrix)
181 SelfadjointProductMatrix(
const Lhs& lhs,
const Rhs& rhs) : Base(lhs,rhs) {}
183 template<
typename Dest>
void scaleAndAddTo(Dest& dest, Scalar alpha)
const
185 typedef typename Dest::Scalar ResScalar;
186 typedef typename Base::RhsScalar RhsScalar;
187 typedef Map<Matrix<ResScalar,Dynamic,1>,
Aligned> MappedDest;
189 eigen_assert(dest.rows()==m_lhs.rows() && dest.cols()==m_rhs.cols());
191 typename internal::add_const_on_value_type<ActualLhsType>::type lhs = LhsBlasTraits::extract(m_lhs);
192 typename internal::add_const_on_value_type<ActualRhsType>::type rhs = RhsBlasTraits::extract(m_rhs);
194 Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(m_lhs)
195 * RhsBlasTraits::extractScalarFactor(m_rhs);
198 EvalToDest = (Dest::InnerStrideAtCompileTime==1),
199 UseRhs = (_ActualRhsType::InnerStrideAtCompileTime==1)
202 internal::gemv_static_vector_if<ResScalar,Dest::SizeAtCompileTime,Dest::MaxSizeAtCompileTime,!EvalToDest> static_dest;
203 internal::gemv_static_vector_if<RhsScalar,_ActualRhsType::SizeAtCompileTime,_ActualRhsType::MaxSizeAtCompileTime,!UseRhs> static_rhs;
205 ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
206 EvalToDest ? dest.data() : static_dest.data());
208 ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,rhs.size(),
209 UseRhs ?
const_cast<RhsScalar*
>(rhs.data()) : static_rhs.data());
213 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
214 int size = dest.size();
215 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
217 MappedDest(actualDestPtr, dest.size()) = dest;
222 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
223 int size = rhs.size();
224 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
226 Map<typename _ActualRhsType::PlainObject>(actualRhsPtr, rhs.size()) = rhs;
230 internal::selfadjoint_matrix_vector_product<Scalar, Index, (internal::traits<_ActualLhsType>::Flags&
RowMajorBit) ?
RowMajor :
ColMajor,
int(LhsUpLo), bool(LhsBlasTraits::NeedToConjugate), bool(RhsBlasTraits::NeedToConjugate)>::run
233 &lhs.coeffRef(0,0), lhs.outerStride(),
240 dest = MappedDest(actualDestPtr, dest.size());
245 template<
typename Lhs,
typename Rhs,
int RhsMode>
246 struct traits<SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false> >
247 : traits<ProductBase<SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false>, Lhs, Rhs> >
251 template<
typename Lhs,
typename Rhs,
int RhsMode>
252 struct SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false>
253 :
public ProductBase<SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false>, Lhs, Rhs >
255 EIGEN_PRODUCT_PUBLIC_INTERFACE(SelfadjointProductMatrix)
261 SelfadjointProductMatrix(
const Lhs& lhs,
const Rhs& rhs) : Base(lhs,rhs) {}
263 template<
typename Dest>
void scaleAndAddTo(Dest& dest, Scalar alpha)
const
266 Transpose<Dest> destT(dest);
267 SelfadjointProductMatrix<Transpose<const Rhs>, int(RhsUpLo)==
Upper ?
Lower :
Upper,
false,
268 Transpose<const Lhs>, 0,
true>(m_rhs.transpose(), m_lhs.transpose()).scaleAndAddTo(destT, alpha);
274 #endif // EIGEN_SELFADJOINT_MATRIX_VECTOR_H