MOAB
4.9.3pre
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2009 Gael Guennebaud <[email protected]> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_SELFADJOINT_PRODUCT_H 00011 #define EIGEN_SELFADJOINT_PRODUCT_H 00012 00013 /********************************************************************** 00014 * This file implements a self adjoint product: C += A A^T updating only 00015 * half of the selfadjoint matrix C. 00016 * It corresponds to the level 3 SYRK and level 2 SYR Blas routines. 00017 **********************************************************************/ 00018 00019 namespace Eigen { 00020 00021 00022 template<typename Scalar, typename Index, int UpLo, bool ConjLhs, bool ConjRhs> 00023 struct selfadjoint_rank1_update<Scalar,Index,ColMajor,UpLo,ConjLhs,ConjRhs> 00024 { 00025 static void run(Index size, Scalar* mat, Index stride, const Scalar* vecX, const Scalar* vecY, const Scalar& alpha) 00026 { 00027 internal::conj_if<ConjRhs> cj; 00028 typedef Map<const Matrix<Scalar,Dynamic,1> > OtherMap; 00029 typedef typename internal::conditional<ConjLhs,typename OtherMap::ConjugateReturnType,const OtherMap&>::type ConjLhsType; 00030 for (Index i=0; i<size; ++i) 00031 { 00032 Map<Matrix<Scalar,Dynamic,1> >(mat+stride*i+(UpLo==Lower ? i : 0), (UpLo==Lower ? size-i : (i+1))) 00033 += (alpha * cj(vecY[i])) * ConjLhsType(OtherMap(vecX+(UpLo==Lower ? i : 0),UpLo==Lower ? size-i : (i+1))); 00034 } 00035 } 00036 }; 00037 00038 template<typename Scalar, typename Index, int UpLo, bool ConjLhs, bool ConjRhs> 00039 struct selfadjoint_rank1_update<Scalar,Index,RowMajor,UpLo,ConjLhs,ConjRhs> 00040 { 00041 static void run(Index size, Scalar* mat, Index stride, const Scalar* vecX, const Scalar* vecY, const Scalar& alpha) 00042 { 00043 selfadjoint_rank1_update<Scalar,Index,ColMajor,UpLo==Lower?Upper:Lower,ConjRhs,ConjLhs>::run(size,mat,stride,vecY,vecX,alpha); 00044 } 00045 }; 00046 00047 template<typename MatrixType, typename OtherType, int UpLo, bool OtherIsVector = OtherType::IsVectorAtCompileTime> 00048 struct selfadjoint_product_selector; 00049 00050 template<typename MatrixType, typename OtherType, int UpLo> 00051 struct selfadjoint_product_selector<MatrixType,OtherType,UpLo,true> 00052 { 00053 static void run(MatrixType& mat, const OtherType& other, const typename MatrixType::Scalar& alpha) 00054 { 00055 typedef typename MatrixType::Scalar Scalar; 00056 typedef internal::blas_traits<OtherType> OtherBlasTraits; 00057 typedef typename OtherBlasTraits::DirectLinearAccessType ActualOtherType; 00058 typedef typename internal::remove_all<ActualOtherType>::type _ActualOtherType; 00059 typename internal::add_const_on_value_type<ActualOtherType>::type actualOther = OtherBlasTraits::extract(other.derived()); 00060 00061 Scalar actualAlpha = alpha * OtherBlasTraits::extractScalarFactor(other.derived()); 00062 00063 enum { 00064 StorageOrder = (internal::traits<MatrixType>::Flags&RowMajorBit) ? RowMajor : ColMajor, 00065 UseOtherDirectly = _ActualOtherType::InnerStrideAtCompileTime==1 00066 }; 00067 internal::gemv_static_vector_if<Scalar,OtherType::SizeAtCompileTime,OtherType::MaxSizeAtCompileTime,!UseOtherDirectly> static_other; 00068 00069 ei_declare_aligned_stack_constructed_variable(Scalar, actualOtherPtr, other.size(), 00070 (UseOtherDirectly ? const_cast<Scalar*>(actualOther.data()) : static_other.data())); 00071 00072 if(!UseOtherDirectly) 00073 Map<typename _ActualOtherType::PlainObject>(actualOtherPtr, actualOther.size()) = actualOther; 00074 00075 selfadjoint_rank1_update<Scalar,Index,StorageOrder,UpLo, 00076 OtherBlasTraits::NeedToConjugate && NumTraits<Scalar>::IsComplex, 00077 (!OtherBlasTraits::NeedToConjugate) && NumTraits<Scalar>::IsComplex> 00078 ::run(other.size(), mat.data(), mat.outerStride(), actualOtherPtr, actualOtherPtr, actualAlpha); 00079 } 00080 }; 00081 00082 template<typename MatrixType, typename OtherType, int UpLo> 00083 struct selfadjoint_product_selector<MatrixType,OtherType,UpLo,false> 00084 { 00085 static void run(MatrixType& mat, const OtherType& other, const typename MatrixType::Scalar& alpha) 00086 { 00087 typedef typename MatrixType::Scalar Scalar; 00088 typedef internal::blas_traits<OtherType> OtherBlasTraits; 00089 typedef typename OtherBlasTraits::DirectLinearAccessType ActualOtherType; 00090 typedef typename internal::remove_all<ActualOtherType>::type _ActualOtherType; 00091 typename internal::add_const_on_value_type<ActualOtherType>::type actualOther = OtherBlasTraits::extract(other.derived()); 00092 00093 Scalar actualAlpha = alpha * OtherBlasTraits::extractScalarFactor(other.derived()); 00094 00095 enum { 00096 IsRowMajor = (internal::traits<MatrixType>::Flags&RowMajorBit) ? 1 : 0, 00097 OtherIsRowMajor = _ActualOtherType::Flags&RowMajorBit ? 1 : 0 00098 }; 00099 00100 Index size = mat.cols(); 00101 Index depth = actualOther.cols(); 00102 00103 typedef internal::gemm_blocking_space<IsRowMajor ? RowMajor : ColMajor,Scalar,Scalar, 00104 MatrixType::MaxColsAtCompileTime, MatrixType::MaxColsAtCompileTime, _ActualOtherType::MaxColsAtCompileTime> BlockingType; 00105 00106 BlockingType blocking(size, size, depth, 1, false); 00107 00108 00109 internal::general_matrix_matrix_triangular_product<Index, 00110 Scalar, OtherIsRowMajor ? RowMajor : ColMajor, OtherBlasTraits::NeedToConjugate && NumTraits<Scalar>::IsComplex, 00111 Scalar, OtherIsRowMajor ? ColMajor : RowMajor, (!OtherBlasTraits::NeedToConjugate) && NumTraits<Scalar>::IsComplex, 00112 IsRowMajor ? RowMajor : ColMajor, UpLo> 00113 ::run(size, depth, 00114 &actualOther.coeffRef(0,0), actualOther.outerStride(), &actualOther.coeffRef(0,0), actualOther.outerStride(), 00115 mat.data(), mat.outerStride(), actualAlpha, blocking); 00116 } 00117 }; 00118 00119 // high level API 00120 00121 template<typename MatrixType, unsigned int UpLo> 00122 template<typename DerivedU> 00123 SelfAdjointView<MatrixType,UpLo>& SelfAdjointView<MatrixType,UpLo> 00124 ::rankUpdate(const MatrixBase<DerivedU>& u, const Scalar& alpha) 00125 { 00126 selfadjoint_product_selector<MatrixType,DerivedU,UpLo>::run(_expression().const_cast_derived(), u.derived(), alpha); 00127 00128 return *this; 00129 } 00130 00131 } // end namespace Eigen 00132 00133 #endif // EIGEN_SELFADJOINT_PRODUCT_H