comparison armadillo-3.900.4/include/armadillo_bits/subview_each_meat.hpp @ 49:1ec0e2823891

Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author Chris Cannam
date Thu, 13 Jun 2013 10:25:24 +0100
parents
children
comparison
equal deleted inserted replaced
48:69251e11a913 49:1ec0e2823891
1 // Copyright (C) 2012 Conrad Sanderson
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7
8 //! \addtogroup subview_each
9 //! @{
10
11
12 //
13 //
14 // subview_each_common
15
16 template<typename parent, unsigned int mode>
17 inline
18 subview_each_common<parent,mode>::subview_each_common(parent& in_p)
19 : p(in_p)
20 {
21 arma_extra_debug_sigprint();
22 }
23
24
25
26 template<typename parent, unsigned int mode>
27 arma_inline
28 const Mat<typename parent::elem_type>&
29 subview_each_common<parent,mode>::get_mat_ref_helper(const Mat<typename parent::elem_type>& X) const
30 {
31 return X;
32 }
33
34
35
36 template<typename parent, unsigned int mode>
37 arma_inline
38 const Mat<typename parent::elem_type>&
39 subview_each_common<parent,mode>::get_mat_ref_helper(const subview<typename parent::elem_type>& X) const
40 {
41 return X.m;
42 }
43
44
45
46 template<typename parent, unsigned int mode>
47 arma_inline
48 const Mat<typename parent::elem_type>&
49 subview_each_common<parent,mode>::get_mat_ref() const
50 {
51 return get_mat_ref_helper(p);
52 }
53
54
55
56 template<typename parent, unsigned int mode>
57 inline
58 void
59 subview_each_common<parent,mode>::check_size(const Mat<typename parent::elem_type>& A) const
60 {
61 if(arma_config::debug == true)
62 {
63 if(mode == 0)
64 {
65 if( (A.n_rows != p.n_rows) || (A.n_cols != 1) )
66 {
67 arma_stop( incompat_size_string(A) );
68 }
69 }
70 else
71 {
72 if( (A.n_rows != 1) || (A.n_cols != p.n_cols) )
73 {
74 arma_stop( incompat_size_string(A) );
75 }
76 }
77 }
78 }
79
80
81
82 template<typename parent, unsigned int mode>
83 arma_cold
84 inline
85 const std::string
86 subview_each_common<parent,mode>::incompat_size_string(const Mat<typename parent::elem_type>& A) const
87 {
88 std::stringstream tmp;
89
90 if(mode == 0)
91 {
92 tmp << "each_col(): incompatible size; expected " << p.n_rows << "x1" << ", got " << A.n_rows << 'x' << A.n_cols;
93 }
94 else
95 {
96 tmp << "each_row(): incompatible size; expected 1x" << p.n_cols << ", got " << A.n_rows << 'x' << A.n_cols;
97 }
98
99 return tmp.str();
100 }
101
102
103
104 //
105 //
106 // subview_each1
107
108
109
110 template<typename parent, unsigned int mode>
111 inline
112 subview_each1<parent,mode>::~subview_each1()
113 {
114 arma_extra_debug_sigprint();
115 }
116
117
118
119 template<typename parent, unsigned int mode>
120 inline
121 subview_each1<parent,mode>::subview_each1(parent& in_p)
122 : subview_each_common<parent,mode>::subview_each_common(in_p)
123 {
124 arma_extra_debug_sigprint();
125 }
126
127
128
129 template<typename parent, unsigned int mode>
130 template<typename T1>
131 inline
132 void
133 subview_each1<parent,mode>::operator= (const Base<eT,T1>& in)
134 {
135 arma_extra_debug_sigprint();
136
137 parent& p = subview_each_common<parent,mode>::p;
138
139 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
140 const Mat<eT>& A = tmp.M;
141
142 subview_each_common<parent,mode>::check_size(A);
143
144 const eT* A_mem = A.memptr();
145 const uword p_n_rows = p.n_rows;
146 const uword p_n_cols = p.n_cols;
147
148 if(mode == 0) // each column
149 {
150 for(uword i=0; i < p_n_cols; ++i)
151 {
152 arrayops::copy( p.colptr(i), A_mem, p_n_rows );
153 }
154 }
155 else // each row
156 {
157 for(uword i=0; i < p_n_cols; ++i)
158 {
159 arrayops::inplace_set( p.colptr(i), A_mem[i], p_n_rows);
160 }
161 }
162 }
163
164
165
166 template<typename parent, unsigned int mode>
167 template<typename T1>
168 inline
169 void
170 subview_each1<parent,mode>::operator+= (const Base<eT,T1>& in)
171 {
172 arma_extra_debug_sigprint();
173
174 parent& p = subview_each_common<parent,mode>::p;
175
176 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
177 const Mat<eT>& A = tmp.M;
178
179 subview_each_common<parent,mode>::check_size(A);
180
181 const eT* A_mem = A.memptr();
182 const uword p_n_rows = p.n_rows;
183 const uword p_n_cols = p.n_cols;
184
185 if(mode == 0) // each column
186 {
187 for(uword i=0; i < p_n_cols; ++i)
188 {
189 arrayops::inplace_plus( p.colptr(i), A_mem, p_n_rows );
190 }
191 }
192 else // each row
193 {
194 for(uword i=0; i < p_n_cols; ++i)
195 {
196 arrayops::inplace_plus( p.colptr(i), A_mem[i], p_n_rows);
197 }
198 }
199 }
200
201
202
203 template<typename parent, unsigned int mode>
204 template<typename T1>
205 inline
206 void
207 subview_each1<parent,mode>::operator-= (const Base<eT,T1>& in)
208 {
209 arma_extra_debug_sigprint();
210
211 parent& p = subview_each_common<parent,mode>::p;
212
213 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
214 const Mat<eT>& A = tmp.M;
215
216 subview_each_common<parent,mode>::check_size(A);
217
218 const eT* A_mem = A.memptr();
219 const uword p_n_rows = p.n_rows;
220 const uword p_n_cols = p.n_cols;
221
222 if(mode == 0) // each column
223 {
224 for(uword i=0; i < p_n_cols; ++i)
225 {
226 arrayops::inplace_minus( p.colptr(i), A_mem, p_n_rows );
227 }
228 }
229 else // each row
230 {
231 for(uword i=0; i < p_n_cols; ++i)
232 {
233 arrayops::inplace_minus( p.colptr(i), A_mem[i], p_n_rows);
234 }
235 }
236 }
237
238
239
240 template<typename parent, unsigned int mode>
241 template<typename T1>
242 inline
243 void
244 subview_each1<parent,mode>::operator%= (const Base<eT,T1>& in)
245 {
246 arma_extra_debug_sigprint();
247
248 parent& p = subview_each_common<parent,mode>::p;
249
250 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
251 const Mat<eT>& A = tmp.M;
252
253 subview_each_common<parent,mode>::check_size(A);
254
255 const eT* A_mem = A.memptr();
256 const uword p_n_rows = p.n_rows;
257 const uword p_n_cols = p.n_cols;
258
259 if(mode == 0) // each column
260 {
261 for(uword i=0; i < p_n_cols; ++i)
262 {
263 arrayops::inplace_mul( p.colptr(i), A_mem, p_n_rows );
264 }
265 }
266 else // each row
267 {
268 for(uword i=0; i < p_n_cols; ++i)
269 {
270 arrayops::inplace_mul( p.colptr(i), A_mem[i], p_n_rows);
271 }
272 }
273 }
274
275
276
277 template<typename parent, unsigned int mode>
278 template<typename T1>
279 inline
280 void
281 subview_each1<parent,mode>::operator/= (const Base<eT,T1>& in)
282 {
283 arma_extra_debug_sigprint();
284
285 parent& p = subview_each_common<parent,mode>::p;
286
287 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
288 const Mat<eT>& A = tmp.M;
289
290 subview_each_common<parent,mode>::check_size(A);
291
292 const eT* A_mem = A.memptr();
293 const uword p_n_rows = p.n_rows;
294 const uword p_n_cols = p.n_cols;
295
296 if(mode == 0) // each column
297 {
298 for(uword i=0; i < p_n_cols; ++i)
299 {
300 arrayops::inplace_div( p.colptr(i), A_mem, p_n_rows );
301 }
302 }
303 else // each row
304 {
305 for(uword i=0; i < p_n_cols; ++i)
306 {
307 arrayops::inplace_div( p.colptr(i), A_mem[i], p_n_rows);
308 }
309 }
310 }
311
312
313
314 //
315 //
316 // subview_each2
317
318
319
320 template<typename parent, unsigned int mode, typename TB>
321 inline
322 subview_each2<parent,mode,TB>::~subview_each2()
323 {
324 arma_extra_debug_sigprint();
325 }
326
327
328
329 template<typename parent, unsigned int mode, typename TB>
330 inline
331 subview_each2<parent,mode,TB>::subview_each2(parent& in_p, const Base<uword, TB>& in_indices)
332 : subview_each_common<parent,mode>::subview_each_common(in_p)
333 , base_indices(in_indices)
334 {
335 arma_extra_debug_sigprint();
336 }
337
338
339
340 template<typename parent, unsigned int mode, typename TB>
341 inline
342 void
343 subview_each2<parent,mode,TB>::check_indices(const Mat<uword>& indices) const
344 {
345 if(mode == 0)
346 {
347 arma_debug_check( ((indices.is_vec() == false) && (indices.is_empty() == false)), "each_col(): list of indices must be a vector" );
348 }
349 else
350 {
351 arma_debug_check( ((indices.is_vec() == false) && (indices.is_empty() == false)), "each_row(): list of indices must be a vector" );
352 }
353 }
354
355
356
357 template<typename parent, unsigned int mode, typename TB>
358 template<typename T1>
359 inline
360 void
361 subview_each2<parent,mode,TB>::operator= (const Base<eT,T1>& in)
362 {
363 arma_extra_debug_sigprint();
364
365 parent& p = subview_each_common<parent,mode>::p;
366
367 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
368 const Mat<eT>& A = tmp.M;
369
370 subview_each_common<parent,mode>::check_size(A);
371
372 const unwrap_check_mixed<TB> tmp_indices( base_indices.get_ref(), (*this).get_mat_ref() );
373 const Mat<uword>& indices = tmp_indices.M;
374
375 check_indices(indices);
376
377 const eT* A_mem = A.memptr();
378 const uword p_n_rows = p.n_rows;
379 const uword p_n_cols = p.n_cols;
380
381 const uword* indices_mem = indices.memptr();
382 const uword N = indices.n_elem;
383
384 if(mode == 0) // each column
385 {
386 for(uword i=0; i < N; ++i)
387 {
388 const uword col = indices_mem[i];
389
390 arma_debug_check( (col > p_n_cols), "each_col(): index out of bounds" );
391
392 arrayops::copy( p.colptr(col), A_mem, p_n_rows );
393 }
394 }
395 else // each row
396 {
397 for(uword i=0; i < N; ++i)
398 {
399 const uword row = indices_mem[i];
400
401 arma_debug_check( (row > p_n_rows), "each_row(): index out of bounds" );
402
403 for(uword col=0; col < p_n_cols; ++col)
404 {
405 p.at(row,col) = A_mem[col];
406 }
407 }
408 }
409 }
410
411
412
413 template<typename parent, unsigned int mode, typename TB>
414 template<typename T1>
415 inline
416 void
417 subview_each2<parent,mode,TB>::operator+= (const Base<eT,T1>& in)
418 {
419 arma_extra_debug_sigprint();
420
421 parent& p = subview_each_common<parent,mode>::p;
422
423 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
424 const Mat<eT>& A = tmp.M;
425
426 subview_each_common<parent,mode>::check_size(A);
427
428 const unwrap_check_mixed<TB> tmp_indices( base_indices.get_ref(), (*this).get_mat_ref() );
429 const Mat<uword>& indices = tmp_indices.M;
430
431 check_indices(indices);
432
433 const eT* A_mem = A.memptr();
434 const uword p_n_rows = p.n_rows;
435 const uword p_n_cols = p.n_cols;
436
437 const uword* indices_mem = indices.memptr();
438 const uword N = indices.n_elem;
439
440 if(mode == 0) // each column
441 {
442 for(uword i=0; i < N; ++i)
443 {
444 const uword col = indices_mem[i];
445
446 arma_debug_check( (col > p_n_cols), "each_col(): index out of bounds" );
447
448 arrayops::inplace_plus( p.colptr(col), A_mem, p_n_rows );
449 }
450 }
451 else // each row
452 {
453 for(uword i=0; i < N; ++i)
454 {
455 const uword row = indices_mem[i];
456
457 arma_debug_check( (row > p_n_rows), "each_row(): index out of bounds" );
458
459 for(uword col=0; col < p_n_cols; ++col)
460 {
461 p.at(row,col) += A_mem[col];
462 }
463 }
464 }
465 }
466
467
468
469 template<typename parent, unsigned int mode, typename TB>
470 template<typename T1>
471 inline
472 void
473 subview_each2<parent,mode,TB>::operator-= (const Base<eT,T1>& in)
474 {
475 arma_extra_debug_sigprint();
476
477 parent& p = subview_each_common<parent,mode>::p;
478
479 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
480 const Mat<eT>& A = tmp.M;
481
482 subview_each_common<parent,mode>::check_size(A);
483
484 const unwrap_check_mixed<TB> tmp_indices( base_indices.get_ref(), (*this).get_mat_ref() );
485 const Mat<uword>& indices = tmp_indices.M;
486
487 check_indices(indices);
488
489 const eT* A_mem = A.memptr();
490 const uword p_n_rows = p.n_rows;
491 const uword p_n_cols = p.n_cols;
492
493 const uword* indices_mem = indices.memptr();
494 const uword N = indices.n_elem;
495
496 if(mode == 0) // each column
497 {
498 for(uword i=0; i < N; ++i)
499 {
500 const uword col = indices_mem[i];
501
502 arma_debug_check( (col > p_n_cols), "each_col(): index out of bounds" );
503
504 arrayops::inplace_minus( p.colptr(col), A_mem, p_n_rows );
505 }
506 }
507 else // each row
508 {
509 for(uword i=0; i < N; ++i)
510 {
511 const uword row = indices_mem[i];
512
513 arma_debug_check( (row > p_n_rows), "each_row(): index out of bounds" );
514
515 for(uword col=0; col < p_n_cols; ++col)
516 {
517 p.at(row,col) -= A_mem[col];
518 }
519 }
520 }
521 }
522
523
524
525 template<typename parent, unsigned int mode, typename TB>
526 template<typename T1>
527 inline
528 void
529 subview_each2<parent,mode,TB>::operator%= (const Base<eT,T1>& in)
530 {
531 arma_extra_debug_sigprint();
532
533 parent& p = subview_each_common<parent,mode>::p;
534
535 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
536 const Mat<eT>& A = tmp.M;
537
538 subview_each_common<parent,mode>::check_size(A);
539
540 const unwrap_check_mixed<TB> tmp_indices( base_indices.get_ref(), (*this).get_mat_ref() );
541 const Mat<uword>& indices = tmp_indices.M;
542
543 check_indices(indices);
544
545 const eT* A_mem = A.memptr();
546 const uword p_n_rows = p.n_rows;
547 const uword p_n_cols = p.n_cols;
548
549 const uword* indices_mem = indices.memptr();
550 const uword N = indices.n_elem;
551
552 if(mode == 0) // each column
553 {
554 for(uword i=0; i < N; ++i)
555 {
556 const uword col = indices_mem[i];
557
558 arma_debug_check( (col > p_n_cols), "each_col(): index out of bounds" );
559
560 arrayops::inplace_mul( p.colptr(col), A_mem, p_n_rows );
561 }
562 }
563 else // each row
564 {
565 for(uword i=0; i < N; ++i)
566 {
567 const uword row = indices_mem[i];
568
569 arma_debug_check( (row > p_n_rows), "each_row(): index out of bounds" );
570
571 for(uword col=0; col < p_n_cols; ++col)
572 {
573 p.at(row,col) *= A_mem[col];
574 }
575 }
576 }
577 }
578
579
580
581 template<typename parent, unsigned int mode, typename TB>
582 template<typename T1>
583 inline
584 void
585 subview_each2<parent,mode,TB>::operator/= (const Base<eT,T1>& in)
586 {
587 arma_extra_debug_sigprint();
588
589 parent& p = subview_each_common<parent,mode>::p;
590
591 const unwrap_check<T1> tmp( in.get_ref(), (*this).get_mat_ref() );
592 const Mat<eT>& A = tmp.M;
593
594 subview_each_common<parent,mode>::check_size(A);
595
596 const unwrap_check_mixed<TB> tmp_indices( base_indices.get_ref(), (*this).get_mat_ref() );
597 const Mat<uword>& indices = tmp_indices.M;
598
599 check_indices(indices);
600
601 const eT* A_mem = A.memptr();
602 const uword p_n_rows = p.n_rows;
603 const uword p_n_cols = p.n_cols;
604
605 const uword* indices_mem = indices.memptr();
606 const uword N = indices.n_elem;
607
608 if(mode == 0) // each column
609 {
610 for(uword i=0; i < N; ++i)
611 {
612 const uword col = indices_mem[i];
613
614 arma_debug_check( (col > p_n_cols), "each_col(): index out of bounds" );
615
616 arrayops::inplace_div( p.colptr(col), A_mem, p_n_rows );
617 }
618 }
619 else // each row
620 {
621 for(uword i=0; i < N; ++i)
622 {
623 const uword row = indices_mem[i];
624
625 arma_debug_check( (row > p_n_rows), "each_row(): index out of bounds" );
626
627 for(uword col=0; col < p_n_cols; ++col)
628 {
629 p.at(row,col) /= A_mem[col];
630 }
631 }
632 }
633 }
634
635
636
637 //! @}