1 /////////////////////////////////////////////////////////////////////////////// 2 // // 3 // The Template Matrix/Vector Library for C++ was created by Mike Jarvis // 4 // Copyright (C) 1998 - 2016 // 5 // All rights reserved // 6 // // 7 // The project is hosted at https://code.google.com/p/tmv-cpp/ // 8 // where you can find the current version and current documention. // 9 // // 10 // For concerns or problems with the software, Mike may be contacted at // 11 // mike_jarvis17 [at] gmail. // 12 // // 13 // This software is licensed under a FreeBSD license. The file // 14 // TMV_LICENSE should have bee included with this distribution. // 15 // It not, you can get a copy from https://code.google.com/p/tmv-cpp/. // 16 // // 17 // Essentially, you can use this software however you want provided that // 18 // you include the TMV_LICENSE file in any distribution that uses it. // 19 // // 20 /////////////////////////////////////////////////////////////////////////////// 21 22 23 #ifndef TMV_DiagMatrixArithFunc_H 24 #define TMV_DiagMatrixArithFunc_H 25 26 #include "tmv/TMV_BaseDiagMatrix.h" 27 #include "tmv/TMV_VectorArithFunc.h" 28 29 #define CT std::complex<T> 30 31 namespace tmv { 32 33 // y (+)= alpha * A * x 34 template <bool add, typename T, typename Ta, typename Tx> 35 void MultMV( 36 const T alpha, const GenDiagMatrix<Ta>& A, 37 const GenVector<Tx>& x, VectorView<T> y); 38 39 // B += alpha * A 40 template <typename T, typename Ta> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,DiagMatrixView<T> B)41 inline void AddMM( 42 const T alpha, const GenDiagMatrix<Ta>& A, DiagMatrixView<T> B) 43 { AddVV(alpha,A.diag(),B.diag()); } 44 45 template <typename T, typename Ta> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,MatrixView<T> B)46 inline void AddMM( 47 const T alpha, const GenDiagMatrix<Ta>& A, MatrixView<T> B) 48 { AddVV(alpha,A.diag(),B.diag()); } 49 50 // C = alpha * A + beta * B 51 template <typename T, typename Ta, typename Tb> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,const T beta,const GenDiagMatrix<Tb> & B,DiagMatrixView<T> C)52 inline void AddMM( 53 const T alpha, const GenDiagMatrix<Ta>& A, 54 const T beta, const GenDiagMatrix<Tb>& B, DiagMatrixView<T> C) 55 { AddVV(alpha,A.diag(),beta,B.diag(),C.diag()); } 56 57 template <typename T, typename Ta, typename Tb> 58 void AddMM( 59 const T alpha, const GenDiagMatrix<Ta>& A, 60 const T beta, const GenMatrix<Tb>& B, MatrixView<T> C); 61 62 template <typename T, typename Ta, typename Tb> AddMM(const T alpha,const GenMatrix<Ta> & A,const T beta,const GenDiagMatrix<Tb> & B,MatrixView<T> C)63 inline void AddMM( 64 const T alpha, const GenMatrix<Ta>& A, 65 const T beta, const GenDiagMatrix<Tb>& B, MatrixView<T> C) 66 { AddMM(beta,B,alpha,A,C); } 67 68 // C (+)= alpha * A * B 69 template <typename T, typename Ta> 70 void MultEqMM( 71 const T alpha, 72 const GenDiagMatrix<Ta>& A, MatrixView<T> B); 73 74 template <bool add, typename T, typename Ta, typename Tb> 75 void MultMM( 76 const T alpha, const GenDiagMatrix<Ta>& A, const GenMatrix<Tb>& B, 77 MatrixView<T> C); 78 79 template <bool add, typename T, typename Ta, typename Tb> MultMM(const T alpha,const GenMatrix<Ta> & A,const GenDiagMatrix<Tb> & B,MatrixView<T> C)80 inline void MultMM( 81 const T alpha, const GenMatrix<Ta>& A, const GenDiagMatrix<Tb>& B, 82 MatrixView<T> C) 83 { MultMM<add>(alpha,B,A.transpose(),C.transpose()); } 84 85 template <bool add, typename T, typename Ta, typename Tb> MultMM(const T alpha,const GenDiagMatrix<Ta> & A,const GenDiagMatrix<Tb> & B,DiagMatrixView<T> C)86 inline void MultMM( 87 const T alpha, const GenDiagMatrix<Ta>& A, const GenDiagMatrix<Tb>& B, 88 DiagMatrixView<T> C) 89 { MultMV<add>(alpha,A,B.diag(),C.diag()); } 90 91 template <bool add, typename T, typename Ta, typename Tb> ElemMultMM(const T alpha,const GenDiagMatrix<Ta> & A,const GenDiagMatrix<Tb> & B,DiagMatrixView<T> C)92 inline void ElemMultMM( 93 const T alpha, const GenDiagMatrix<Ta>& A, const GenDiagMatrix<Tb>& B, 94 DiagMatrixView<T> C) 95 { ElemMultVV<add>(alpha,A.diag(),B.diag(),C.diag()); } 96 97 template <typename T> 98 class DiagMatrixComposite : public GenDiagMatrix<T> 99 { 100 public: DiagMatrixComposite()101 inline DiagMatrixComposite() : inst() {} DiagMatrixComposite(const DiagMatrixComposite<T> &)102 inline DiagMatrixComposite(const DiagMatrixComposite<T>&) : inst() {} ~DiagMatrixComposite()103 virtual inline ~DiagMatrixComposite() {} 104 105 protected: 106 ConstVectorView<T> cdiag() const; 107 108 private: 109 mutable auto_ptr<const DiagMatrix<T> > inst; 110 }; 111 112 // Specialize allowed complex combinations: 113 template <bool add, typename T, typename Ta, typename Tx> MultMV(const T alpha,const GenDiagMatrix<Ta> & A,const GenVector<Tx> & x,VectorView<CT> y)114 inline void MultMV( 115 const T alpha, const GenDiagMatrix<Ta>& A, 116 const GenVector<Tx>& x, VectorView<CT> y) 117 { MultMV<add>(CT(alpha),A,x,y); } 118 119 template <typename T, typename Ta> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,DiagMatrixView<CT> B)120 inline void AddMM( 121 const T alpha, const GenDiagMatrix<Ta>& A, DiagMatrixView<CT> B) 122 { AddMM(CT(alpha),A,B); } 123 124 template <typename T, typename Ta> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,MatrixView<CT> B)125 inline void AddMM( 126 const T alpha, const GenDiagMatrix<Ta>& A, MatrixView<CT> B) 127 { AddMM(CT(alpha),A,B); } 128 129 template <typename T, typename Ta, typename Tb> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,const T beta,const GenDiagMatrix<Tb> & B,DiagMatrixView<CT> C)130 inline void AddMM( 131 const T alpha, const GenDiagMatrix<Ta>& A, 132 const T beta, const GenDiagMatrix<Tb>& B, DiagMatrixView<CT> C) 133 { AddMM(CT(alpha),A,CT(beta),B,C); } 134 135 template <typename T, typename Ta, typename Tb> AddMM(const T alpha,const GenDiagMatrix<Ta> & A,const T beta,const GenMatrix<Tb> & B,MatrixView<CT> C)136 inline void AddMM( 137 const T alpha, const GenDiagMatrix<Ta>& A, 138 const T beta, const GenMatrix<Tb>& B, MatrixView<CT> C) 139 { AddMM(CT(alpha),A,CT(beta),B,C); } 140 141 template <typename T, typename Ta, typename Tb> AddMM(const T alpha,const GenMatrix<Ta> & A,const T beta,const GenDiagMatrix<Tb> & B,MatrixView<CT> C)142 inline void AddMM( 143 const T alpha, const GenMatrix<Ta>& A, 144 const T beta, const GenDiagMatrix<Tb>& B, MatrixView<CT> C) 145 { AddMM(CT(alpha),A,CT(beta),B,C); } 146 147 template <typename T, typename Ta> MultEqMM(const T alpha,const GenDiagMatrix<Ta> & A,MatrixView<CT> B)148 inline void MultEqMM(const T alpha, 149 const GenDiagMatrix<Ta>& A, MatrixView<CT> B) 150 { MultEqMM(CT(alpha),A,B); } 151 152 template <bool add, typename T, typename Ta, typename Tb> MultMM(const T alpha,const GenDiagMatrix<Ta> & A,const GenMatrix<Tb> & B,MatrixView<CT> C)153 inline void MultMM( 154 const T alpha, const GenDiagMatrix<Ta>& A, const GenMatrix<Tb>& B, 155 MatrixView<CT> C) 156 { MultMM<add>(CT(alpha),A,B,C); } 157 158 template <bool add, typename T, typename Ta, typename Tb> MultMM(const T alpha,const GenMatrix<Ta> & A,const GenDiagMatrix<Tb> & B,MatrixView<CT> C)159 inline void MultMM( 160 const T alpha, const GenMatrix<Ta>& A, const GenDiagMatrix<Tb>& B, 161 MatrixView<CT> C) 162 { MultMM<add>(CT(alpha),A,B,C); } 163 164 template <bool add, typename T, typename Ta, typename Tb> MultMM(const T alpha,const GenDiagMatrix<Ta> & A,const GenDiagMatrix<Tb> & B,DiagMatrixView<CT> C)165 inline void MultMM( 166 const T alpha, const GenDiagMatrix<Ta>& A, const GenDiagMatrix<Tb>& B, 167 DiagMatrixView<CT> C) 168 { MultMM<add>(CT(alpha),A,B,C); } 169 170 template <bool add, typename T, typename Ta, typename Tb> ElemMultMM(const T alpha,const GenDiagMatrix<Ta> & A,const GenDiagMatrix<Tb> & B,DiagMatrixView<CT> C)171 inline void ElemMultMM( 172 const T alpha, const GenDiagMatrix<Ta>& A, const GenDiagMatrix<Tb>& B, 173 DiagMatrixView<CT> C) 174 { ElemMultMM<add>(CT(alpha),A,B,C); } 175 176 // Specialize disallowed complex combinations: 177 template <bool add, typename T, typename Ta, typename Tb> MultMV(const CT,const GenDiagMatrix<Ta> &,const GenVector<Tb> &,VectorView<T>)178 inline void MultMV( 179 const CT , const GenDiagMatrix<Ta>& , 180 const GenVector<Tb>& , VectorView<T> ) 181 { TMVAssert(TMV_FALSE); } 182 183 template <typename T, typename Ta, typename Tb> AddMM(const CT,const GenDiagMatrix<Ta> &,const CT,const GenDiagMatrix<Ta> &,DiagMatrixView<T>)184 inline void AddMM( 185 const CT , const GenDiagMatrix<Ta>& , 186 const CT , const GenDiagMatrix<Ta>& , DiagMatrixView<T> ) 187 { TMVAssert(TMV_FALSE); } 188 189 template <typename T, typename Ta, typename Tb> AddMM(const CT,const GenDiagMatrix<Ta> &,const CT,const GenMatrix<Tb> &,MatrixView<T>)190 inline void AddMM( 191 const CT , const GenDiagMatrix<Ta>& , 192 const CT , const GenMatrix<Tb>& , MatrixView<T> ) 193 { TMVAssert(TMV_FALSE); } 194 195 template <typename T, typename Ta, typename Tb> AddMM(const CT,const GenMatrix<Ta> &,const CT,const GenDiagMatrix<Tb> &,MatrixView<T>)196 inline void AddMM( 197 const CT , const GenMatrix<Ta>& , 198 const CT , const GenDiagMatrix<Tb>& , MatrixView<T> ) 199 { TMVAssert(TMV_FALSE); } 200 201 template <bool add, typename T, typename Ta, typename Tb> MultMM(const CT,const GenDiagMatrix<Ta> &,const GenMatrix<Tb> &,MatrixView<T>)202 inline void MultMM( 203 const CT , const GenDiagMatrix<Ta>& , const GenMatrix<Tb>& , 204 MatrixView<T> ) 205 { TMVAssert(TMV_FALSE); } 206 207 template <bool add, typename T, typename Ta, typename Tb> MultMM(const CT,const GenMatrix<Ta> &,const GenDiagMatrix<Tb> &,MatrixView<T>)208 inline void MultMM( 209 const CT , const GenMatrix<Ta>& , const GenDiagMatrix<Tb>& , 210 MatrixView<T> ) 211 { TMVAssert(TMV_FALSE); } 212 213 template <bool add, typename T, typename Ta, typename Tb> MultMM(const CT,const GenDiagMatrix<Ta> &,const GenDiagMatrix<Tb> &,DiagMatrixView<T>)214 inline void MultMM( 215 const CT , const GenDiagMatrix<Ta>& , const GenDiagMatrix<Tb>& , 216 DiagMatrixView<T> ) 217 { TMVAssert(TMV_FALSE); } 218 219 template <bool add, typename T, typename Ta, typename Tb> ElemMultMM(const CT,const GenDiagMatrix<Ta> &,const GenDiagMatrix<Tb> &,DiagMatrixView<T>)220 inline void ElemMultMM( 221 const CT , const GenDiagMatrix<Ta>& , const GenDiagMatrix<Tb>& , 222 DiagMatrixView<T> ) 223 { TMVAssert(TMV_FALSE); } 224 225 } 226 227 #undef CT 228 229 #endif 230