00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_SELFADJOINT_MATRIX_VECTOR_H
00026 #define EIGEN_SELFADJOINT_MATRIX_VECTOR_H
00027
00028 namespace internal {
00029
00030
00031
00032
00033
00034
00035 template<typename Scalar, typename Index, int StorageOrder, int UpLo, bool ConjugateLhs, bool ConjugateRhs>
00036 static EIGEN_DONT_INLINE void product_selfadjoint_vector(
00037 Index size,
00038 const Scalar* lhs, Index lhsStride,
00039 const Scalar* _rhs, Index rhsIncr,
00040 Scalar* res,
00041 Scalar alpha)
00042 {
00043 typedef typename packet_traits<Scalar>::type Packet;
00044 typedef typename NumTraits<Scalar>::Real RealScalar;
00045 const Index PacketSize = sizeof(Packet)/sizeof(Scalar);
00046
00047 enum {
00048 IsRowMajor = StorageOrder==RowMajor ? 1 : 0,
00049 IsLower = UpLo == Lower ? 1 : 0,
00050 FirstTriangular = IsRowMajor == IsLower
00051 };
00052
00053 conj_helper<Scalar,Scalar,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, IsRowMajor), ConjugateRhs> cj0;
00054 conj_helper<Scalar,Scalar,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, !IsRowMajor), ConjugateRhs> cj1;
00055 conj_helper<Scalar,Scalar,NumTraits<Scalar>::IsComplex, ConjugateRhs> cjd;
00056
00057 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, IsRowMajor), ConjugateRhs> pcj0;
00058 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex && EIGEN_LOGICAL_XOR(ConjugateLhs, !IsRowMajor), ConjugateRhs> pcj1;
00059
00060 Scalar cjAlpha = ConjugateRhs ? conj(alpha) : alpha;
00061
00062
00063
00064
00065 const Scalar* EIGEN_RESTRICT rhs = _rhs;
00066 if (rhsIncr!=1)
00067 {
00068 Scalar* r = ei_aligned_stack_new(Scalar, size);
00069 const Scalar* it = _rhs;
00070 for (Index i=0; i<size; ++i, it+=rhsIncr)
00071 r[i] = *it;
00072 rhs = r;
00073 }
00074
00075 Index bound = std::max(Index(0),size-8) & 0xfffffffe;
00076 if (FirstTriangular)
00077 bound = size - bound;
00078
00079 for (Index j=FirstTriangular ? bound : 0;
00080 j<(FirstTriangular ? size : bound);j+=2)
00081 {
00082 register const Scalar* EIGEN_RESTRICT A0 = lhs + j*lhsStride;
00083 register const Scalar* EIGEN_RESTRICT A1 = lhs + (j+1)*lhsStride;
00084
00085 Scalar t0 = cjAlpha * rhs[j];
00086 Packet ptmp0 = pset1<Packet>(t0);
00087 Scalar t1 = cjAlpha * rhs[j+1];
00088 Packet ptmp1 = pset1<Packet>(t1);
00089
00090 Scalar t2 = 0;
00091 Packet ptmp2 = pset1<Packet>(t2);
00092 Scalar t3 = 0;
00093 Packet ptmp3 = pset1<Packet>(t3);
00094
00095 size_t starti = FirstTriangular ? 0 : j+2;
00096 size_t endi = FirstTriangular ? j : size;
00097 size_t alignedStart = (starti) + first_aligned(&res[starti], endi-starti);
00098 size_t alignedEnd = alignedStart + ((endi-alignedStart)/(PacketSize))*(PacketSize);
00099
00100
00101 res[j] += cjd.pmul(internal::real(A0[j]), t0);
00102 res[j+1] += cjd.pmul(internal::real(A1[j+1]), t1);
00103 if(FirstTriangular)
00104 {
00105 res[j] += cj0.pmul(A1[j], t1);
00106 t3 += cj1.pmul(A1[j], rhs[j]);
00107 }
00108 else
00109 {
00110 res[j+1] += cj0.pmul(A0[j+1],t0);
00111 t2 += cj1.pmul(A0[j+1], rhs[j+1]);
00112 }
00113
00114 for (size_t i=starti; i<alignedStart; ++i)
00115 {
00116 res[i] += t0 * A0[i] + t1 * A1[i];
00117 t2 += conj(A0[i]) * rhs[i];
00118 t3 += conj(A1[i]) * rhs[i];
00119 }
00120
00121
00122 const Scalar* EIGEN_RESTRICT a0It = A0 + alignedStart;
00123 const Scalar* EIGEN_RESTRICT a1It = A1 + alignedStart;
00124 const Scalar* EIGEN_RESTRICT rhsIt = rhs + alignedStart;
00125 Scalar* EIGEN_RESTRICT resIt = res + alignedStart;
00126 for (size_t i=alignedStart; i<alignedEnd; i+=PacketSize)
00127 {
00128 Packet A0i = ploadu<Packet>(a0It); a0It += PacketSize;
00129 Packet A1i = ploadu<Packet>(a1It); a1It += PacketSize;
00130 Packet Bi = ploadu<Packet>(rhsIt); rhsIt += PacketSize;
00131 Packet Xi = pload <Packet>(resIt);
00132
00133 Xi = pcj0.pmadd(A0i,ptmp0, pcj0.pmadd(A1i,ptmp1,Xi));
00134 ptmp2 = pcj1.pmadd(A0i, Bi, ptmp2);
00135 ptmp3 = pcj1.pmadd(A1i, Bi, ptmp3);
00136 pstore(resIt,Xi); resIt += PacketSize;
00137 }
00138 for (size_t i=alignedEnd; i<endi; i++)
00139 {
00140 res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i],t1);
00141 t2 += cj1.pmul(A0[i], rhs[i]);
00142 t3 += cj1.pmul(A1[i], rhs[i]);
00143 }
00144
00145 res[j] += alpha * (t2 + predux(ptmp2));
00146 res[j+1] += alpha * (t3 + predux(ptmp3));
00147 }
00148 for (Index j=FirstTriangular ? 0 : bound;j<(FirstTriangular ? bound : size);j++)
00149 {
00150 register const Scalar* EIGEN_RESTRICT A0 = lhs + j*lhsStride;
00151
00152 Scalar t1 = cjAlpha * rhs[j];
00153 Scalar t2 = 0;
00154
00155 res[j] += cjd.pmul(internal::real(A0[j]), t1);
00156 for (Index i=FirstTriangular ? 0 : j+1; i<(FirstTriangular ? j : size); i++)
00157 {
00158 res[i] += cj0.pmul(A0[i], t1);
00159 t2 += cj1.pmul(A0[i], rhs[i]);
00160 }
00161 res[j] += alpha * t2;
00162 }
00163
00164 if(rhsIncr!=1)
00165 ei_aligned_stack_delete(Scalar, const_cast<Scalar*>(rhs), size);
00166 }
00167
00168 }
00169
00170
00171
00172
00173
00174 namespace internal {
00175 template<typename Lhs, int LhsMode, typename Rhs>
00176 struct traits<SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true> >
00177 : traits<ProductBase<SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true>, Lhs, Rhs> >
00178 {};
00179 }
00180
00181 template<typename Lhs, int LhsMode, typename Rhs>
00182 struct SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true>
00183 : public ProductBase<SelfadjointProductMatrix<Lhs,LhsMode,false,Rhs,0,true>, Lhs, Rhs >
00184 {
00185 EIGEN_PRODUCT_PUBLIC_INTERFACE(SelfadjointProductMatrix)
00186
00187 enum {
00188 LhsUpLo = LhsMode&(Upper|Lower)
00189 };
00190
00191 SelfadjointProductMatrix(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs) {}
00192
00193 template<typename Dest> void scaleAndAddTo(Dest& dest, Scalar alpha) const
00194 {
00195 typedef typename Dest::Scalar ResScalar;
00196 typedef typename Base::RhsScalar RhsScalar;
00197 typedef Map<Matrix<ResScalar,Dynamic,1>, Aligned> MappedDest;
00198
00199 eigen_assert(dest.rows()==m_lhs.rows() && dest.cols()==m_rhs.cols());
00200
00201 const ActualLhsType lhs = LhsBlasTraits::extract(m_lhs);
00202 const ActualRhsType rhs = RhsBlasTraits::extract(m_rhs);
00203
00204 Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(m_lhs)
00205 * RhsBlasTraits::extractScalarFactor(m_rhs);
00206
00207 enum {
00208 EvalToDest = (Dest::InnerStrideAtCompileTime==1),
00209 UseRhs = (_ActualRhsType::InnerStrideAtCompileTime==1)
00210 };
00211
00212 internal::gemv_static_vector_if<ResScalar,Dest::SizeAtCompileTime,Dest::MaxSizeAtCompileTime,!EvalToDest> static_dest;
00213 internal::gemv_static_vector_if<RhsScalar,_ActualRhsType::SizeAtCompileTime,_ActualRhsType::MaxSizeAtCompileTime,!UseRhs> static_rhs;
00214
00215 bool freeDestPtr = false;
00216 ResScalar* actualDestPtr;
00217 if(EvalToDest)
00218 actualDestPtr = dest.data();
00219 else
00220 {
00221 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
00222 int size = dest.size();
00223 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
00224 #endif
00225 if((actualDestPtr=static_dest.data())==0)
00226 {
00227 freeDestPtr = true;
00228 actualDestPtr = ei_aligned_stack_new(ResScalar,dest.size());
00229 }
00230 MappedDest(actualDestPtr, dest.size()) = dest;
00231 }
00232
00233 bool freeRhsPtr = false;
00234 RhsScalar* actualRhsPtr;
00235 if(UseRhs)
00236 actualRhsPtr = const_cast<RhsScalar*>(rhs.data());
00237 else
00238 {
00239 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
00240 int size = rhs.size();
00241 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
00242 #endif
00243 if((actualRhsPtr=static_rhs.data())==0)
00244 {
00245 freeRhsPtr = true;
00246 actualRhsPtr = ei_aligned_stack_new(RhsScalar,rhs.size());
00247 }
00248 Map<typename _ActualRhsType::PlainObject>(actualRhsPtr, rhs.size()) = rhs;
00249 }
00250
00251
00252 internal::product_selfadjoint_vector<Scalar, Index, (internal::traits<_ActualLhsType>::Flags&RowMajorBit) ? RowMajor : ColMajor, int(LhsUpLo), bool(LhsBlasTraits::NeedToConjugate), bool(RhsBlasTraits::NeedToConjugate)>
00253 (
00254 lhs.rows(),
00255 &lhs.coeffRef(0,0), lhs.outerStride(),
00256 actualRhsPtr, 1,
00257 actualDestPtr,
00258 actualAlpha
00259 );
00260
00261 if(!EvalToDest)
00262 {
00263 dest = MappedDest(actualDestPtr, dest.size());
00264 if(freeDestPtr) ei_aligned_stack_delete(ResScalar, actualDestPtr, dest.size());
00265 }
00266 if(freeRhsPtr) ei_aligned_stack_delete(RhsScalar, actualRhsPtr, rhs.size());
00267 }
00268 };
00269
00270 namespace internal {
00271 template<typename Lhs, typename Rhs, int RhsMode>
00272 struct traits<SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false> >
00273 : traits<ProductBase<SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false>, Lhs, Rhs> >
00274 {};
00275 }
00276
00277 template<typename Lhs, typename Rhs, int RhsMode>
00278 struct SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false>
00279 : public ProductBase<SelfadjointProductMatrix<Lhs,0,true,Rhs,RhsMode,false>, Lhs, Rhs >
00280 {
00281 EIGEN_PRODUCT_PUBLIC_INTERFACE(SelfadjointProductMatrix)
00282
00283 enum {
00284 RhsUpLo = RhsMode&(Upper|Lower)
00285 };
00286
00287 SelfadjointProductMatrix(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs) {}
00288
00289 template<typename Dest> void scaleAndAddTo(Dest& dest, Scalar alpha) const
00290 {
00291
00292 Transpose<Dest> destT(dest);
00293 SelfadjointProductMatrix<Transpose<const Rhs>, int(RhsUpLo)==Upper ? Lower : Upper, false,
00294 Transpose<const Lhs>, 0, true>(m_rhs.transpose(), m_lhs.transpose()).scaleAndAddTo(destT, alpha);
00295 }
00296 };
00297
00298
00299 #endif // EIGEN_SELFADJOINT_MATRIX_VECTOR_H