SelfadjointProduct.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_SELFADJOINT_PRODUCT_H
00026 #define EIGEN_SELFADJOINT_PRODUCT_H
00027 
00028 /**********************************************************************
00029 * This file implements a self adjoint product: C += A A^T updating only
00030 * half of the selfadjoint matrix C.
00031 * It corresponds to the level 3 SYRK and level 2 SYR Blas routines.
00032 **********************************************************************/
00033 
00034 template<typename Scalar, typename Index, int StorageOrder, int UpLo, bool ConjLhs, bool ConjRhs>
00035 struct selfadjoint_rank1_update;
00036 
00037 template<typename Scalar, typename Index, int UpLo, bool ConjLhs, bool ConjRhs>
00038 struct selfadjoint_rank1_update<Scalar,Index,ColMajor,UpLo,ConjLhs,ConjRhs>
00039 {
00040   static void run(Index size, Scalar* mat, Index stride, const Scalar* vec, Scalar alpha)
00041   {
00042     internal::conj_if<ConjRhs> cj;
00043     typedef Map<const Matrix<Scalar,Dynamic,1> > OtherMap;
00044     typedef typename internal::conditional<ConjLhs,typename OtherMap::ConjugateReturnType,const OtherMap&>::type ConjRhsType;
00045     for (Index i=0; i<size; ++i)
00046     {
00047       Map<Matrix<Scalar,Dynamic,1> >(mat+stride*i+(UpLo==Lower ? i : 0), (UpLo==Lower ? size-i : (i+1)))
00048           += (alpha * cj(vec[i])) * ConjRhsType(OtherMap(vec+(UpLo==Lower ? i : 0),UpLo==Lower ? size-i : (i+1)));
00049     }
00050   }
00051 };
00052 
00053 template<typename Scalar, typename Index, int UpLo, bool ConjLhs, bool ConjRhs>
00054 struct selfadjoint_rank1_update<Scalar,Index,RowMajor,UpLo,ConjLhs,ConjRhs>
00055 {
00056   static void run(Index size, Scalar* mat, Index stride, const Scalar* vec, Scalar alpha)
00057   {
00058     selfadjoint_rank1_update<Scalar,Index,ColMajor,UpLo==Lower?Upper:Lower,ConjRhs,ConjLhs>::run(size,mat,stride,vec,alpha);
00059   }
00060 };
00061 
00062 template<typename MatrixType, typename OtherType, int UpLo, bool OtherIsVector = OtherType::IsVectorAtCompileTime>
00063 struct selfadjoint_product_selector;
00064 
00065 template<typename MatrixType, typename OtherType, int UpLo>
00066 struct selfadjoint_product_selector<MatrixType,OtherType,UpLo,true>
00067 {
00068   static void run(MatrixType& mat, const OtherType& other, typename MatrixType::Scalar alpha)
00069   {
00070     typedef typename MatrixType::Scalar Scalar;
00071     typedef typename MatrixType::Index Index;
00072     typedef internal::blas_traits<OtherType> OtherBlasTraits;
00073     typedef typename OtherBlasTraits::DirectLinearAccessType ActualOtherType;
00074     typedef typename internal::remove_all<ActualOtherType>::type _ActualOtherType;
00075     const ActualOtherType actualOther = OtherBlasTraits::extract(other.derived());
00076 
00077     Scalar actualAlpha = alpha * OtherBlasTraits::extractScalarFactor(other.derived());
00078 
00079     enum {
00080       StorageOrder = (internal::traits<MatrixType>::Flags&RowMajorBit) ? RowMajor : ColMajor,
00081       UseOtherDirectly = _ActualOtherType::InnerStrideAtCompileTime==1
00082     };
00083     internal::gemv_static_vector_if<Scalar,OtherType::SizeAtCompileTime,OtherType::MaxSizeAtCompileTime,!UseOtherDirectly> static_other;
00084 
00085     ei_declare_aligned_stack_constructed_variable(Scalar, actualOtherPtr, other.size(),
00086       (UseOtherDirectly ? const_cast<Scalar*>(actualOther.data()) : static_other.data()));
00087       
00088     if(!UseOtherDirectly)
00089       Map<typename _ActualOtherType::PlainObject>(actualOtherPtr, actualOther.size()) = actualOther;
00090     
00091     selfadjoint_rank1_update<Scalar,Index,StorageOrder,UpLo,
00092                               OtherBlasTraits::NeedToConjugate  && NumTraits<Scalar>::IsComplex,
00093                             (!OtherBlasTraits::NeedToConjugate) && NumTraits<Scalar>::IsComplex>
00094           ::run(other.size(), mat.data(), mat.outerStride(), actualOtherPtr, actualAlpha);
00095   }
00096 };
00097 
00098 template<typename MatrixType, typename OtherType, int UpLo>
00099 struct selfadjoint_product_selector<MatrixType,OtherType,UpLo,false>
00100 {
00101   static void run(MatrixType& mat, const OtherType& other, typename MatrixType::Scalar alpha)
00102   {
00103     typedef typename MatrixType::Scalar Scalar;
00104     typedef typename MatrixType::Index Index;
00105     typedef internal::blas_traits<OtherType> OtherBlasTraits;
00106     typedef typename OtherBlasTraits::DirectLinearAccessType ActualOtherType;
00107     typedef typename internal::remove_all<ActualOtherType>::type _ActualOtherType;
00108     const ActualOtherType actualOther = OtherBlasTraits::extract(other.derived());
00109 
00110     Scalar actualAlpha = alpha * OtherBlasTraits::extractScalarFactor(other.derived());
00111 
00112     enum { IsRowMajor = (internal::traits<MatrixType>::Flags&RowMajorBit) ? 1 : 0 };
00113     
00114     internal::general_matrix_matrix_triangular_product<Index,
00115       Scalar, _ActualOtherType::Flags&RowMajorBit ? RowMajor : ColMajor,   OtherBlasTraits::NeedToConjugate  && NumTraits<Scalar>::IsComplex,
00116       Scalar, _ActualOtherType::Flags&RowMajorBit ? ColMajor : RowMajor, (!OtherBlasTraits::NeedToConjugate) && NumTraits<Scalar>::IsComplex,
00117       MatrixType::Flags&RowMajorBit ? RowMajor : ColMajor, UpLo>
00118       ::run(mat.cols(), actualOther.cols(),
00119             &actualOther.coeffRef(0,0), actualOther.outerStride(), &actualOther.coeffRef(0,0), actualOther.outerStride(),
00120             mat.data(), mat.outerStride(), actualAlpha);
00121   }
00122 };
00123 
00124 // high level API
00125 
00126 template<typename MatrixType, unsigned int UpLo>
00127 template<typename DerivedU>
00128 SelfAdjointView<MatrixType,UpLo>& SelfAdjointView<MatrixType,UpLo>
00129 ::rankUpdate(const MatrixBase<DerivedU>& u, Scalar alpha)
00130 {
00131   selfadjoint_product_selector<MatrixType,DerivedU,UpLo>::run(_expression().const_cast_derived(), u.derived(), alpha);
00132 
00133   return *this;
00134 }
00135 
00136 #endif // EIGEN_SELFADJOINT_PRODUCT_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:27