comparison armadillo-2.4.4/include/armadillo_bits/Gen_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup Gen
15 //! @{
16
17
18
19 template<typename eT, typename gen_type>
20 arma_inline
21 Gen<eT, gen_type>::Gen(const uword in_n_rows, const uword in_n_cols)
22 : n_rows(in_n_rows)
23 , n_cols(in_n_cols)
24 {
25 arma_extra_debug_sigprint();
26 }
27
28
29
30 template<typename eT, typename gen_type>
31 arma_inline
32 Gen<eT, gen_type>::~Gen()
33 {
34 arma_extra_debug_sigprint();
35 }
36
37
38
39 template<typename eT, typename gen_type>
40 arma_inline
41 eT
42 Gen<eT, gen_type>::generate()
43 {
44 if(is_same_type<gen_type, gen_ones_full>::value == true) { return eT(1); }
45 else if(is_same_type<gen_type, gen_zeros >::value == true) { return eT(0); }
46 else if(is_same_type<gen_type, gen_randu >::value == true) { return eT(eop_aux_randu<eT>()); }
47 else if(is_same_type<gen_type, gen_randn >::value == true) { return eT(eop_aux_randn<eT>()); }
48 else { return eT(); }
49 }
50
51
52
53 template<typename eT, typename gen_type>
54 arma_inline
55 eT
56 Gen<eT, gen_type>::operator[](const uword i) const
57 {
58 if(is_same_type<gen_type, gen_ones_diag>::value == true)
59 {
60 return ((i % n_rows) == (i / n_rows)) ? eT(1) : eT(0);
61 }
62 else
63 {
64 return Gen<eT, gen_type>::generate();
65 }
66 }
67
68
69
70 template<typename eT, typename gen_type>
71 arma_inline
72 eT
73 Gen<eT, gen_type>::at(const uword row, const uword col) const
74 {
75 if(is_same_type<gen_type, gen_ones_diag>::value == true)
76 {
77 return (row == col) ? eT(1) : eT(0);
78 }
79 else
80 {
81 return Gen<eT, gen_type>::generate();
82 }
83 }
84
85
86
87 template<typename eT, typename gen_type>
88 inline
89 void
90 Gen<eT, gen_type>::apply(Mat<eT>& out) const
91 {
92 arma_extra_debug_sigprint();
93
94 // NOTE: we're assuming that the matrix has already been set to the correct size;
95 // this is done by either the Mat contructor or operator=()
96
97 if(is_same_type<gen_type, gen_ones_diag>::value == true) { out.eye(); }
98 else if(is_same_type<gen_type, gen_ones_full>::value == true) { out.ones(); }
99 else if(is_same_type<gen_type, gen_zeros >::value == true) { out.zeros(); }
100 else if(is_same_type<gen_type, gen_randu >::value == true) { out.randu(); }
101 else if(is_same_type<gen_type, gen_randn >::value == true) { out.randn(); }
102 }
103
104
105
106 template<typename eT, typename gen_type>
107 inline
108 void
109 Gen<eT, gen_type>::apply_inplace_plus(Mat<eT>& out) const
110 {
111 arma_extra_debug_sigprint();
112
113 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "addition");
114
115
116 if(is_same_type<gen_type, gen_ones_diag>::value == true)
117 {
118 const uword N = (std::min)(n_rows, n_cols);
119
120 for(uword i=0; i<N; ++i)
121 {
122 out.at(i,i) += eT(1);
123 }
124 }
125 else
126 {
127 eT* out_mem = out.memptr();
128 const uword n_elem = out.n_elem;
129
130 uword i,j;
131
132 for(i=0, j=1; j<n_elem; i+=2, j+=2)
133 {
134 const eT tmp_i = Gen<eT, gen_type>::generate();
135 const eT tmp_j = Gen<eT, gen_type>::generate();
136
137 out_mem[i] += tmp_i;
138 out_mem[j] += tmp_j;
139 }
140
141 if(i < n_elem)
142 {
143 out_mem[i] += Gen<eT, gen_type>::generate();
144 }
145 }
146
147 }
148
149
150
151
152 template<typename eT, typename gen_type>
153 inline
154 void
155 Gen<eT, gen_type>::apply_inplace_minus(Mat<eT>& out) const
156 {
157 arma_extra_debug_sigprint();
158
159 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "subtraction");
160
161
162 if(is_same_type<gen_type, gen_ones_diag>::value == true)
163 {
164 const uword N = (std::min)(n_rows, n_cols);
165
166 for(uword i=0; i<N; ++i)
167 {
168 out.at(i,i) -= eT(1);
169 }
170 }
171 else
172 {
173 eT* out_mem = out.memptr();
174 const uword n_elem = out.n_elem;
175
176 uword i,j;
177
178 for(i=0, j=1; j<n_elem; i+=2, j+=2)
179 {
180 const eT tmp_i = Gen<eT, gen_type>::generate();
181 const eT tmp_j = Gen<eT, gen_type>::generate();
182
183 out_mem[i] -= tmp_i;
184 out_mem[j] -= tmp_j;
185 }
186
187 if(i < n_elem)
188 {
189 out_mem[i] -= Gen<eT, gen_type>::generate();
190 }
191 }
192
193 }
194
195
196
197
198 template<typename eT, typename gen_type>
199 inline
200 void
201 Gen<eT, gen_type>::apply_inplace_schur(Mat<eT>& out) const
202 {
203 arma_extra_debug_sigprint();
204
205 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise multiplication");
206
207
208 if(is_same_type<gen_type, gen_ones_diag>::value == true)
209 {
210 const uword N = (std::min)(n_rows, n_cols);
211
212 for(uword i=0; i<N; ++i)
213 {
214 for(uword row=0; row<i; ++row) { out.at(row,i) = eT(0); }
215 for(uword row=i+1; row<n_rows; ++row) { out.at(row,i) = eT(0); }
216 }
217 }
218 else
219 {
220 eT* out_mem = out.memptr();
221 const uword n_elem = out.n_elem;
222
223 uword i,j;
224
225 for(i=0, j=1; j<n_elem; i+=2, j+=2)
226 {
227 const eT tmp_i = Gen<eT, gen_type>::generate();
228 const eT tmp_j = Gen<eT, gen_type>::generate();
229
230 out_mem[i] *= tmp_i;
231 out_mem[j] *= tmp_j;
232 }
233
234 if(i < n_elem)
235 {
236 out_mem[i] *= Gen<eT, gen_type>::generate();
237 }
238 }
239
240 }
241
242
243
244
245 template<typename eT, typename gen_type>
246 inline
247 void
248 Gen<eT, gen_type>::apply_inplace_div(Mat<eT>& out) const
249 {
250 arma_extra_debug_sigprint();
251
252 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise division");
253
254
255 if(is_same_type<gen_type, gen_ones_diag>::value == true)
256 {
257 const uword N = (std::min)(n_rows, n_cols);
258
259 for(uword i=0; i<N; ++i)
260 {
261 const eT zero = eT(0);
262
263 for(uword row=0; row<i; ++row) { out.at(row,i) /= zero; }
264 for(uword row=i+1; row<n_rows; ++row) { out.at(row,i) /= zero; }
265 }
266 }
267 else
268 {
269 eT* out_mem = out.memptr();
270 const uword n_elem = out.n_elem;
271
272 uword i,j;
273
274 for(i=0, j=1; j<n_elem; i+=2, j+=2)
275 {
276 const eT tmp_i = Gen<eT, gen_type>::generate();
277 const eT tmp_j = Gen<eT, gen_type>::generate();
278
279 out_mem[i] /= tmp_i;
280 out_mem[j] /= tmp_j;
281 }
282
283 if(i < n_elem)
284 {
285 out_mem[i] /= Gen<eT, gen_type>::generate();
286 }
287 }
288
289 }
290
291
292
293
294 //! @}