$search
00001 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) 00002 // Copyright (C) 2008-2011 Conrad Sanderson 00003 // 00004 // This file is part of the Armadillo C++ library. 00005 // It is provided without any warranty of fitness 00006 // for any purpose. You can redistribute this file 00007 // and/or modify it under the terms of the GNU 00008 // Lesser General Public License (LGPL) as published 00009 // by the Free Software Foundation, either version 3 00010 // of the License or (at your option) any later version. 00011 // (see http://www.opensource.org/licenses for more info) 00012 00013 00016 00017 00018 00019 template<typename T1> 00020 class diagmat_proxy 00021 { 00022 public: 00023 00024 typedef typename T1::elem_type elem_type; 00025 typedef typename get_pod_type<elem_type>::result pod_type; 00026 00027 inline diagmat_proxy(const Base<typename T1::elem_type,T1>& X) 00028 : P ( X.get_ref() ) 00029 , P_is_vec( (P.get_n_rows() == 1) || (P.get_n_cols() == 1) ) 00030 , n_elem ( P_is_vec ? P.get_n_elem() : (std::min)(P.get_n_elem(), P.get_n_rows()) ) 00031 { 00032 arma_extra_debug_sigprint(); 00033 00034 arma_debug_check 00035 ( 00036 (P_is_vec == false) && (P.get_n_rows() != P.get_n_cols()), 00037 "diagmat(): only vectors and square matrices are accepted" 00038 ); 00039 } 00040 00041 00042 arma_inline 00043 elem_type 00044 operator[](const uword i) const 00045 { 00046 if( (Proxy<T1>::prefer_at_accessor == true) || (P_is_vec == false) ) 00047 { 00048 return P.at(i,i); 00049 } 00050 else 00051 { 00052 return P[i]; 00053 } 00054 } 00055 00056 00057 arma_inline 00058 elem_type 00059 at(const uword row, const uword col) const 00060 { 00061 if(row == col) 00062 { 00063 if( (Proxy<T1>::prefer_at_accessor == true) || (P_is_vec == false) ) 00064 { 00065 return P.at(row,row); 00066 } 00067 else 00068 { 00069 return P[row]; 00070 } 00071 } 00072 else 00073 { 00074 return elem_type(0); 00075 } 00076 } 00077 00078 00079 const Proxy<T1> P; 00080 const bool P_is_vec; 00081 const uword n_elem; 00082 }; 00083 00084 00085 00086 template<typename eT> 00087 class diagmat_proxy< Mat<eT> > 00088 { 00089 public: 00090 00091 typedef eT elem_type; 00092 typedef typename get_pod_type<elem_type>::result pod_type; 00093 00094 00095 inline diagmat_proxy(const Mat<eT>& X) 00096 : P(X) 00097 , P_is_vec( (P.n_rows == 1) || (P.n_cols == 1) ) 00098 , n_elem( P_is_vec ? P.n_elem : (std::min)(P.n_elem, P.n_rows) ) 00099 { 00100 arma_extra_debug_sigprint(); 00101 00102 arma_debug_check 00103 ( 00104 (P_is_vec == false) && (P.n_rows != P.n_cols), 00105 "diagmat(): only vectors and square matrices are accepted" 00106 ); 00107 } 00108 00109 00110 arma_inline elem_type operator[] (const uword i) const { return P_is_vec ? P[i] : P.at(i,i); } 00111 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? ( P_is_vec ? P[row] : P.at(row,row) ) : elem_type(0); } 00112 00113 const Mat<eT>& P; 00114 const bool P_is_vec; 00115 const uword n_elem; 00116 }; 00117 00118 00119 00120 template<typename eT> 00121 class diagmat_proxy< Row<eT> > 00122 { 00123 public: 00124 00125 typedef eT elem_type; 00126 typedef typename get_pod_type<elem_type>::result pod_type; 00127 00128 00129 inline diagmat_proxy(const Row<eT>& X) 00130 : P(X) 00131 , P_is_vec(true) 00132 , n_elem(P.n_elem) 00133 { 00134 arma_extra_debug_sigprint(); 00135 } 00136 00137 00138 arma_inline elem_type operator[] (const uword i) const { return P[i]; } 00139 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); } 00140 00141 00142 const Row<eT>& P; 00143 const bool P_is_vec; 00144 const uword n_elem; 00145 }; 00146 00147 00148 00149 template<typename eT> 00150 class diagmat_proxy< Col<eT> > 00151 { 00152 public: 00153 00154 typedef eT elem_type; 00155 typedef typename get_pod_type<elem_type>::result pod_type; 00156 00157 00158 inline diagmat_proxy(const Col<eT>& X) 00159 : P(X) 00160 , P_is_vec(true) 00161 , n_elem(P.n_elem) 00162 { 00163 arma_extra_debug_sigprint(); 00164 } 00165 00166 00167 arma_inline elem_type operator[] (const uword i) const { return P[i]; } 00168 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); } 00169 00170 00171 const Col<eT>& P; 00172 const bool P_is_vec; 00173 const uword n_elem; 00174 }; 00175 00176 00177 00178 template<typename T1> 00179 class diagmat_proxy_check 00180 { 00181 public: 00182 00183 typedef typename T1::elem_type elem_type; 00184 typedef typename get_pod_type<elem_type>::result pod_type; 00185 00186 inline diagmat_proxy_check(const Base<typename T1::elem_type,T1>& X, const Mat<typename T1::elem_type>& out) 00187 : P(X.get_ref()) 00188 , P_is_vec( (P.n_rows == 1) || (P.n_cols == 1) ) 00189 , n_elem( P_is_vec ? P.n_elem : (std::min)(P.n_elem, P.n_rows) ) 00190 { 00191 arma_extra_debug_sigprint(); 00192 arma_ignore(out); 00193 00194 arma_debug_check 00195 ( 00196 (P_is_vec == false) && (P.n_rows != P.n_cols), 00197 "diagmat(): only vectors and square matrices are accepted" 00198 ); 00199 } 00200 00201 00202 arma_inline elem_type operator[] (const uword i) const { return P_is_vec ? P[i] : P.at(i,i); } 00203 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? ( P_is_vec ? P[row] : P.at(row,row) ) : elem_type(0); } 00204 00205 00206 const Mat<elem_type> P; 00207 const bool P_is_vec; 00208 const uword n_elem; 00209 }; 00210 00211 00212 00213 template<typename eT> 00214 class diagmat_proxy_check< Mat<eT> > 00215 { 00216 public: 00217 00218 typedef eT elem_type; 00219 typedef typename get_pod_type<elem_type>::result pod_type; 00220 00221 00222 inline diagmat_proxy_check(const Mat<eT>& X, const Mat<eT>& out) 00223 : P_local ( (&X == &out) ? new Mat<eT>(X) : 0 ) 00224 , P ( (&X == &out) ? (*P_local) : X ) 00225 , P_is_vec( (P.n_rows == 1) || (P.n_cols == 1) ) 00226 , n_elem ( P_is_vec ? P.n_elem : (std::min)(P.n_elem, P.n_rows) ) 00227 { 00228 arma_extra_debug_sigprint(); 00229 00230 arma_debug_check 00231 ( 00232 (P_is_vec == false) && (P.n_rows != P.n_cols), 00233 "diagmat(): only vectors and square matrices are accepted" 00234 ); 00235 } 00236 00237 inline ~diagmat_proxy_check() 00238 { 00239 if(P_local) 00240 { 00241 delete P_local; 00242 } 00243 } 00244 00245 00246 arma_inline elem_type operator[] (const uword i) const { return P_is_vec ? P[i] : P.at(i,i); } 00247 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? ( P_is_vec ? P[row] : P.at(row,row) ) : elem_type(0); } 00248 00249 00250 const Mat<eT>* P_local; 00251 const Mat<eT>& P; 00252 const bool P_is_vec; 00253 const uword n_elem; 00254 }; 00255 00256 00257 00258 template<typename eT> 00259 class diagmat_proxy_check< Row<eT> > 00260 { 00261 public: 00262 00263 typedef eT elem_type; 00264 typedef typename get_pod_type<elem_type>::result pod_type; 00265 00266 inline diagmat_proxy_check(const Row<eT>& X, const Mat<eT>& out) 00267 : P_local ( (&X == reinterpret_cast<const Row<eT>*>(&out)) ? new Row<eT>(X) : 0 ) 00268 , P ( (&X == reinterpret_cast<const Row<eT>*>(&out)) ? (*P_local) : X ) 00269 , P_is_vec(true) 00270 , n_elem (P.n_elem) 00271 { 00272 arma_extra_debug_sigprint(); 00273 } 00274 00275 00276 inline ~diagmat_proxy_check() 00277 { 00278 if(P_local) 00279 { 00280 delete P_local; 00281 } 00282 } 00283 00284 00285 arma_inline elem_type operator[] (const uword i) const { return P[i]; } 00286 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); } 00287 00288 00289 const Row<eT>* P_local; 00290 const Row<eT>& P; 00291 const bool P_is_vec; 00292 const uword n_elem; 00293 }; 00294 00295 00296 00297 00298 00299 00300 template<typename eT> 00301 class diagmat_proxy_check< Col<eT> > 00302 { 00303 public: 00304 00305 typedef eT elem_type; 00306 typedef typename get_pod_type<elem_type>::result pod_type; 00307 00308 inline diagmat_proxy_check(const Col<eT>& X, const Mat<eT>& out) 00309 : P_local ( (&X == reinterpret_cast<const Col<eT>*>(&out)) ? new Col<eT>(X) : 0 ) 00310 , P ( (&X == reinterpret_cast<const Col<eT>*>(&out)) ? (*P_local) : X ) 00311 , P_is_vec(true) 00312 , n_elem (P.n_elem) 00313 { 00314 arma_extra_debug_sigprint(); 00315 } 00316 00317 00318 inline ~diagmat_proxy_check() 00319 { 00320 if(P_local) 00321 { 00322 delete P_local; 00323 } 00324 } 00325 00326 00327 arma_inline elem_type operator[] (const uword i) const { return P[i]; } 00328 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); } 00329 00330 00331 const Col<eT>* P_local; 00332 const Col<eT>& P; 00333 const bool P_is_vec; 00334 const uword n_elem; 00335 }; 00336 00337 00338