$search
00001 // Copyright (C) 2011 NICTA (www.nicta.com.au) 00002 // Copyright (C) 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 eT, typename gen_type> 00020 arma_inline 00021 Gen<eT, gen_type>::Gen(const uword in_n_rows, const uword in_n_cols) 00022 : n_rows(in_n_rows) 00023 , n_cols(in_n_cols) 00024 { 00025 arma_extra_debug_sigprint(); 00026 } 00027 00028 00029 00030 template<typename eT, typename gen_type> 00031 arma_inline 00032 Gen<eT, gen_type>::~Gen() 00033 { 00034 arma_extra_debug_sigprint(); 00035 } 00036 00037 00038 00039 template<typename eT, typename gen_type> 00040 arma_inline 00041 eT 00042 Gen<eT, gen_type>::generate() 00043 { 00044 if(is_same_type<gen_type, gen_ones_full>::value == true) { return eT(1); } 00045 else if(is_same_type<gen_type, gen_zeros >::value == true) { return eT(0); } 00046 else if(is_same_type<gen_type, gen_randu >::value == true) { return eT(eop_aux_randu<eT>()); } 00047 else if(is_same_type<gen_type, gen_randn >::value == true) { return eT(eop_aux_randn<eT>()); } 00048 else { return eT(); } 00049 } 00050 00051 00052 00053 template<typename eT, typename gen_type> 00054 arma_inline 00055 eT 00056 Gen<eT, gen_type>::operator[](const uword i) const 00057 { 00058 if(is_same_type<gen_type, gen_ones_diag>::value == true) 00059 { 00060 return ((i % n_rows) == (i / n_rows)) ? eT(1) : eT(0); 00061 } 00062 else 00063 { 00064 return Gen<eT, gen_type>::generate(); 00065 } 00066 } 00067 00068 00069 00070 template<typename eT, typename gen_type> 00071 arma_inline 00072 eT 00073 Gen<eT, gen_type>::at(const uword row, const uword col) const 00074 { 00075 if(is_same_type<gen_type, gen_ones_diag>::value == true) 00076 { 00077 return (row == col) ? eT(1) : eT(0); 00078 } 00079 else 00080 { 00081 return Gen<eT, gen_type>::generate(); 00082 } 00083 } 00084 00085 00086 00087 template<typename eT, typename gen_type> 00088 inline 00089 void 00090 Gen<eT, gen_type>::apply(Mat<eT>& out) const 00091 { 00092 arma_extra_debug_sigprint(); 00093 00094 // NOTE: we're assuming that the matrix has already been set to the correct size; 00095 // this is done by either the Mat contructor or operator=() 00096 00097 if(is_same_type<gen_type, gen_ones_diag>::value == true) { out.eye(); } 00098 else if(is_same_type<gen_type, gen_ones_full>::value == true) { out.ones(); } 00099 else if(is_same_type<gen_type, gen_zeros >::value == true) { out.zeros(); } 00100 else if(is_same_type<gen_type, gen_randu >::value == true) { out.randu(); } 00101 else if(is_same_type<gen_type, gen_randn >::value == true) { out.randn(); } 00102 } 00103 00104 00105 00106 template<typename eT, typename gen_type> 00107 inline 00108 void 00109 Gen<eT, gen_type>::apply_inplace_plus(Mat<eT>& out) const 00110 { 00111 arma_extra_debug_sigprint(); 00112 00113 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "addition"); 00114 00115 00116 if(is_same_type<gen_type, gen_ones_diag>::value == true) 00117 { 00118 const uword N = (std::min)(n_rows, n_cols); 00119 00120 for(uword i=0; i<N; ++i) 00121 { 00122 out.at(i,i) += eT(1); 00123 } 00124 } 00125 else 00126 { 00127 eT* out_mem = out.memptr(); 00128 const uword n_elem = out.n_elem; 00129 00130 uword i,j; 00131 00132 for(i=0, j=1; j<n_elem; i+=2, j+=2) 00133 { 00134 const eT tmp_i = Gen<eT, gen_type>::generate(); 00135 const eT tmp_j = Gen<eT, gen_type>::generate(); 00136 00137 out_mem[i] += tmp_i; 00138 out_mem[j] += tmp_j; 00139 } 00140 00141 if(i < n_elem) 00142 { 00143 out_mem[i] += Gen<eT, gen_type>::generate(); 00144 } 00145 } 00146 00147 } 00148 00149 00150 00151 00152 template<typename eT, typename gen_type> 00153 inline 00154 void 00155 Gen<eT, gen_type>::apply_inplace_minus(Mat<eT>& out) const 00156 { 00157 arma_extra_debug_sigprint(); 00158 00159 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "subtraction"); 00160 00161 00162 if(is_same_type<gen_type, gen_ones_diag>::value == true) 00163 { 00164 const uword N = (std::min)(n_rows, n_cols); 00165 00166 for(uword i=0; i<N; ++i) 00167 { 00168 out.at(i,i) -= eT(1); 00169 } 00170 } 00171 else 00172 { 00173 eT* out_mem = out.memptr(); 00174 const uword n_elem = out.n_elem; 00175 00176 uword i,j; 00177 00178 for(i=0, j=1; j<n_elem; i+=2, j+=2) 00179 { 00180 const eT tmp_i = Gen<eT, gen_type>::generate(); 00181 const eT tmp_j = Gen<eT, gen_type>::generate(); 00182 00183 out_mem[i] -= tmp_i; 00184 out_mem[j] -= tmp_j; 00185 } 00186 00187 if(i < n_elem) 00188 { 00189 out_mem[i] -= Gen<eT, gen_type>::generate(); 00190 } 00191 } 00192 00193 } 00194 00195 00196 00197 00198 template<typename eT, typename gen_type> 00199 inline 00200 void 00201 Gen<eT, gen_type>::apply_inplace_schur(Mat<eT>& out) const 00202 { 00203 arma_extra_debug_sigprint(); 00204 00205 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise multiplication"); 00206 00207 00208 if(is_same_type<gen_type, gen_ones_diag>::value == true) 00209 { 00210 const uword N = (std::min)(n_rows, n_cols); 00211 00212 for(uword i=0; i<N; ++i) 00213 { 00214 for(uword row=0; row<i; ++row) { out.at(row,i) = eT(0); } 00215 for(uword row=i+1; row<n_rows; ++row) { out.at(row,i) = eT(0); } 00216 } 00217 } 00218 else 00219 { 00220 eT* out_mem = out.memptr(); 00221 const uword n_elem = out.n_elem; 00222 00223 uword i,j; 00224 00225 for(i=0, j=1; j<n_elem; i+=2, j+=2) 00226 { 00227 const eT tmp_i = Gen<eT, gen_type>::generate(); 00228 const eT tmp_j = Gen<eT, gen_type>::generate(); 00229 00230 out_mem[i] *= tmp_i; 00231 out_mem[j] *= tmp_j; 00232 } 00233 00234 if(i < n_elem) 00235 { 00236 out_mem[i] *= Gen<eT, gen_type>::generate(); 00237 } 00238 } 00239 00240 } 00241 00242 00243 00244 00245 template<typename eT, typename gen_type> 00246 inline 00247 void 00248 Gen<eT, gen_type>::apply_inplace_div(Mat<eT>& out) const 00249 { 00250 arma_extra_debug_sigprint(); 00251 00252 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise division"); 00253 00254 00255 if(is_same_type<gen_type, gen_ones_diag>::value == true) 00256 { 00257 const uword N = (std::min)(n_rows, n_cols); 00258 00259 for(uword i=0; i<N; ++i) 00260 { 00261 const eT zero = eT(0); 00262 00263 for(uword row=0; row<i; ++row) { out.at(row,i) /= zero; } 00264 for(uword row=i+1; row<n_rows; ++row) { out.at(row,i) /= zero; } 00265 } 00266 } 00267 else 00268 { 00269 eT* out_mem = out.memptr(); 00270 const uword n_elem = out.n_elem; 00271 00272 uword i,j; 00273 00274 for(i=0, j=1; j<n_elem; i+=2, j+=2) 00275 { 00276 const eT tmp_i = Gen<eT, gen_type>::generate(); 00277 const eT tmp_j = Gen<eT, gen_type>::generate(); 00278 00279 out_mem[i] /= tmp_i; 00280 out_mem[j] /= tmp_j; 00281 } 00282 00283 if(i < n_elem) 00284 { 00285 out_mem[i] /= Gen<eT, gen_type>::generate(); 00286 } 00287 } 00288 00289 } 00290 00291 00292 00293