subview_elem1_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2010-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 
16 
17 
18 template<typename eT, typename T1>
19 inline
21  {
23  }
24 
25 
26 template<typename eT, typename T1>
29  : m(in_m)
30  , m_ptr(0)
31  , a(in_a)
32  {
34  }
35 
36 
37 
38 template<typename eT, typename T1>
41  : m(in_m)
42  , m_ptr(&in_m)
43  , a(in_a)
44  {
46  }
47 
48 
49 
50 template<typename eT, typename T1>
51 template<typename op_type>
52 inline
53 void
55  {
56  Mat<eT>& m_local = *m_ptr;
57 
58  eT* m_mem = m_local.memptr();
59  const uword m_n_elem = m_local.n_elem;
60 
61  const unwrap_check_mixed<T1> tmp(a.get_ref(), m_local);
62  const umat& aa = tmp.M;
63 
65  (
66  ( aa.is_vec() == false ),
67  "Mat::elem(): given object is not a vector"
68  );
69 
70  const uword* aa_mem = aa.memptr();
71  const uword aa_n_elem = aa.n_elem;
72 
73  uword i,j;
74  for(i=0, j=1; j<aa_n_elem; i+=2, j+=2)
75  {
76  const uword ii = aa_mem[i];
77  const uword jj = aa_mem[j];
78 
79  arma_debug_check( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" );
80 
81  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { m_mem[ii] = val; m_mem[jj] = val; }
82  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { m_mem[ii] += val; m_mem[jj] += val; }
83  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { m_mem[ii] -= val; m_mem[jj] -= val; }
84  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { m_mem[ii] *= val; m_mem[jj] *= val; }
85  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { m_mem[ii] /= val; m_mem[jj] /= val; }
86  }
87 
88  if(i < aa_n_elem)
89  {
90  const uword ii = aa_mem[i];
91 
92  arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" );
93 
94  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { m_mem[ii] = val; }
95  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { m_mem[ii] += val; }
96  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { m_mem[ii] -= val; }
97  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { m_mem[ii] *= val; }
98  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { m_mem[ii] /= val; }
99  }
100  }
101 
102 
103 
104 template<typename eT, typename T1>
105 template<typename op_type, typename T2>
106 inline
107 void
109  {
110  subview_elem1<eT,T1>& t = *this;
111 
112  if(&(t.m) == &(x.m))
113  {
114  arma_extra_debug_print("subview_elem1::inplace_op(): aliasing detected");
115 
116  const Mat<eT> tmp(x);
117 
118  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { t.operator= (tmp); }
119  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { t.operator+=(tmp); }
120  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { t.operator-=(tmp); }
121  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { t.operator%=(tmp); }
122  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { t.operator/=(tmp); }
123  }
124  else
125  {
126  Mat<eT>& t_m_local = *(t.m_ptr);
127  const Mat<eT>& x_m_local = x.m;
128 
129  const unwrap_check_mixed<T1> t_tmp(t.a.get_ref(), t_m_local);
130  const unwrap_check_mixed<T2> x_tmp(x.a.get_ref(), t_m_local);
131 
132  const umat& t_aa = t_tmp.M;
133  const umat& x_aa = x_tmp.M;
134 
136  (
137  ( (t_aa.is_vec() == false) || (x_aa.is_vec() == false) ),
138  "Mat::elem(): given object is not a vector"
139  );
140 
141  const uword* t_aa_mem = t_aa.memptr();
142  const uword* x_aa_mem = x_aa.memptr();
143 
144  const uword t_aa_n_elem = t_aa.n_elem;
145 
146  arma_debug_check( (t_aa_n_elem != x_aa.n_elem), "Mat::elem(): size mismatch" );
147 
148 
149  eT* t_m_mem = t_m_local.memptr();
150  const uword t_m_n_elem = t_m_local.n_elem;
151 
152  const eT* x_m_mem = x_m_local.memptr();
153  const uword x_m_n_elem = x_m_local.n_elem;
154 
155  uword i,j;
156  for(i=0, j=1; j<t_aa_n_elem; i+=2, j+=2)
157  {
158  const uword t_ii = t_aa_mem[i];
159  const uword t_jj = t_aa_mem[j];
160 
161  const uword x_ii = x_aa_mem[i];
162  const uword x_jj = x_aa_mem[j];
163 
165  (
166  (t_ii >= t_m_n_elem) || (t_jj >= t_m_n_elem) || (x_ii >= x_m_n_elem) || (x_jj >= x_m_n_elem),
167  "Mat::elem(): index out of bounds"
168  );
169 
170  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { t_m_mem[t_ii] = x_m_mem[x_ii]; t_m_mem[t_jj] = x_m_mem[x_jj]; }
171  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { t_m_mem[t_ii] += x_m_mem[x_ii]; t_m_mem[t_jj] += x_m_mem[x_jj]; }
172  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { t_m_mem[t_ii] -= x_m_mem[x_ii]; t_m_mem[t_jj] -= x_m_mem[x_jj]; }
173  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { t_m_mem[t_ii] *= x_m_mem[x_ii]; t_m_mem[t_jj] *= x_m_mem[x_jj]; }
174  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { t_m_mem[t_ii] /= x_m_mem[x_ii]; t_m_mem[t_jj] /= x_m_mem[x_jj]; }
175  }
176 
177  if(i < t_aa_n_elem)
178  {
179  const uword t_ii = t_aa_mem[i];
180  const uword x_ii = x_aa_mem[i];
181 
183  (
184  ( (t_ii >= t_m_n_elem) || (x_ii >= x_m_n_elem) ),
185  "Mat::elem(): index out of bounds"
186  );
187 
188  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { t_m_mem[t_ii] = x_m_mem[x_ii]; }
189  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { t_m_mem[t_ii] += x_m_mem[x_ii]; }
190  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { t_m_mem[t_ii] -= x_m_mem[x_ii]; }
191  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { t_m_mem[t_ii] *= x_m_mem[x_ii]; }
192  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { t_m_mem[t_ii] /= x_m_mem[x_ii]; }
193  }
194  }
195  }
196 
197 
198 
199 template<typename eT, typename T1>
200 template<typename op_type, typename T2>
201 inline
202 void
204  {
206 
207  Mat<eT>& m_local = *m_ptr;
208 
209  eT* m_mem = m_local.memptr();
210  const uword m_n_elem = m_local.n_elem;
211 
212  const unwrap_check_mixed<T1> tmp(a.get_ref(), m_local);
213  const umat& aa = tmp.M;
214 
216  (
217  ( aa.is_vec() == false ),
218  "Mat::elem(): given object is not a vector"
219  );
220 
221  const uword* aa_mem = aa.memptr();
222  const uword aa_n_elem = aa.n_elem;
223 
224  const Proxy<T2> P(x.get_ref());
225 
226  arma_debug_check( (aa_n_elem != P.get_n_elem()), "Mat::elem(): size mismatch" );
227 
228  if(P.is_alias(m) == false)
229  {
230  typename Proxy<T2>::ea_type X = P.get_ea();
231 
232  uword i,j;
233  for(i=0, j=1; j<aa_n_elem; i+=2, j+=2)
234  {
235  const uword ii = aa_mem[i];
236  const uword jj = aa_mem[j];
237 
238  arma_debug_check( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" );
239 
240  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { m_mem[ii] = X[i]; m_mem[jj] = X[j]; }
241  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { m_mem[ii] += X[i]; m_mem[jj] += X[j]; }
242  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { m_mem[ii] -= X[i]; m_mem[jj] -= X[j]; }
243  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { m_mem[ii] *= X[i]; m_mem[jj] *= X[j]; }
244  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { m_mem[ii] /= X[i]; m_mem[jj] /= X[j]; }
245  }
246 
247  if(i < aa_n_elem)
248  {
249  const uword ii = aa_mem[i];
250 
251  arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" );
252 
253  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { m_mem[ii] = X[i]; }
254  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { m_mem[ii] += X[i]; }
255  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { m_mem[ii] -= X[i]; }
256  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { m_mem[ii] *= X[i]; }
257  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { m_mem[ii] /= X[i]; }
258  }
259  }
260  else
261  {
262  arma_extra_debug_print("subview_elem1::inplace_op(): aliasing detected");
263 
264  const unwrap_check<typename Proxy<T2>::stored_type> tmp(P.Q, m_local);
265  const Mat<eT>& M = tmp.M;
266 
267  const eT* X = M.memptr();
268 
269  uword i,j;
270  for(i=0, j=1; j<aa_n_elem; i+=2, j+=2)
271  {
272  const uword ii = aa_mem[i];
273  const uword jj = aa_mem[j];
274 
275  arma_debug_check( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" );
276 
277  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { m_mem[ii] = X[i]; m_mem[jj] = X[j]; }
278  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { m_mem[ii] += X[i]; m_mem[jj] += X[j]; }
279  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { m_mem[ii] -= X[i]; m_mem[jj] -= X[j]; }
280  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { m_mem[ii] *= X[i]; m_mem[jj] *= X[j]; }
281  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { m_mem[ii] /= X[i]; m_mem[jj] /= X[j]; }
282  }
283 
284  if(i < aa_n_elem)
285  {
286  const uword ii = aa_mem[i];
287 
288  arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" );
289 
290  if(is_same_type<op_type, op_subview_elem_equ >::value == true) { m_mem[ii] = X[i]; }
291  else if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { m_mem[ii] += X[i]; }
292  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { m_mem[ii] -= X[i]; }
293  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { m_mem[ii] *= X[i]; }
294  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { m_mem[ii] /= X[i]; }
295  }
296  }
297  }
298 
299 
300 
301 //
302 //
303 
304 
305 
306 template<typename eT, typename T1>
307 inline
308 void
310  {
312 
313  inplace_op<op_subview_elem_equ>(val);
314  }
315 
316 
317 
318 template<typename eT, typename T1>
319 inline
320 void
322  {
324 
325  inplace_op<op_subview_elem_equ>(eT(0));
326  }
327 
328 
329 
330 template<typename eT, typename T1>
331 inline
332 void
334  {
336 
337  inplace_op<op_subview_elem_equ>(eT(1));
338  }
339 
340 
341 
342 template<typename eT, typename T1>
343 inline
344 void
346  {
348 
349  inplace_op<op_subview_elem_inplace_plus>(val);
350  }
351 
352 
353 
354 template<typename eT, typename T1>
355 inline
356 void
358  {
360 
361  inplace_op<op_subview_elem_inplace_minus>(val);
362  }
363 
364 
365 
366 template<typename eT, typename T1>
367 inline
368 void
370  {
372 
373  inplace_op<op_subview_elem_inplace_schur>(val);
374  }
375 
376 
377 
378 template<typename eT, typename T1>
379 inline
380 void
382  {
384 
385  inplace_op<op_subview_elem_inplace_div>(val);
386  }
387 
388 
389 
390 //
391 //
392 
393 
394 
395 template<typename eT, typename T1>
396 template<typename T2>
397 inline
398 void
400  {
402 
403  inplace_op<op_subview_elem_equ>(x);
404  }
405 
406 
407 
408 
409 template<typename eT, typename T1>
410 template<typename T2>
411 inline
412 void
414  {
416 
417  (*this).operator_equ(x);
418  }
419 
420 
421 
423 template<typename eT, typename T1>
424 inline
425 void
427  {
429 
430  (*this).operator_equ(x);
431  }
432 
433 
434 
435 template<typename eT, typename T1>
436 template<typename T2>
437 inline
438 void
440  {
442 
443  inplace_op<op_subview_elem_inplace_plus>(x);
444  }
445 
446 
447 
448 template<typename eT, typename T1>
449 template<typename T2>
450 inline
451 void
453  {
455 
456  inplace_op<op_subview_elem_inplace_minus>(x);
457  }
458 
459 
460 
461 template<typename eT, typename T1>
462 template<typename T2>
463 inline
464 void
466  {
468 
469  inplace_op<op_subview_elem_inplace_schur>(x);
470  }
471 
472 
473 
474 template<typename eT, typename T1>
475 template<typename T2>
476 inline
477 void
479  {
481 
482  inplace_op<op_subview_elem_inplace_div>(x);
483  }
484 
485 
486 
487 template<typename eT, typename T1>
488 template<typename T2>
489 inline
490 void
492  {
494 
495  inplace_op<op_subview_elem_equ>(x);
496  }
497 
498 
499 
500 template<typename eT, typename T1>
501 template<typename T2>
502 inline
503 void
505  {
507 
508  inplace_op<op_subview_elem_inplace_plus>(x);
509  }
510 
511 
512 
513 template<typename eT, typename T1>
514 template<typename T2>
515 inline
516 void
518  {
520 
521  inplace_op<op_subview_elem_inplace_minus>(x);
522  }
523 
524 
525 
526 template<typename eT, typename T1>
527 template<typename T2>
528 inline
529 void
531  {
533 
534  inplace_op<op_subview_elem_inplace_schur>(x);
535  }
536 
537 
538 
539 template<typename eT, typename T1>
540 template<typename T2>
541 inline
542 void
544  {
546 
547  inplace_op<op_subview_elem_inplace_div>(x);
548  }
549 
550 
551 
552 //
553 //
554 
555 
556 
557 template<typename eT, typename T1>
558 inline
559 void
561  {
563 
564  const unwrap_check_mixed<T1> tmp1(in.a.get_ref(), actual_out);
565  const umat& aa = tmp1.M;
566 
568  (
569  ( aa.is_vec() == false ),
570  "Mat::elem(): given object is not a vector"
571  );
572 
573  const uword* aa_mem = aa.memptr();
574  const uword aa_n_elem = aa.n_elem;
575 
576  const Mat<eT>& m_local = in.m;
577 
578  const eT* m_mem = m_local.memptr();
579  const uword m_n_elem = m_local.n_elem;
580 
581  const bool alias = (&actual_out == &m_local);
582 
583  arma_extra_debug_warn(alias, "subview_elem1::extract(): aliasing detected");
584 
585  Mat<eT>* tmp_out = alias ? new Mat<eT>() : 0;
586  Mat<eT>& out = alias ? *tmp_out : actual_out;
587 
588  out.set_size(aa_n_elem, 1);
589 
590  eT* out_mem = out.memptr();
591 
592  uword i,j;
593  for(i=0, j=1; j<aa_n_elem; i+=2, j+=2)
594  {
595  const uword ii = aa_mem[i];
596  const uword jj = aa_mem[j];
597 
598  arma_debug_check( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" );
599 
600  out_mem[i] = m_mem[ii];
601  out_mem[j] = m_mem[jj];
602  }
603 
604  if(i < aa_n_elem)
605  {
606  const uword ii = aa_mem[i];
607 
608  arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" );
609 
610  out_mem[i] = m_mem[ii];
611  }
612 
613  if(alias == true)
614  {
615  actual_out = out;
616  delete tmp_out;
617  }
618  }
619 
620 
621 
622 template<typename eT, typename T1>
623 template<typename op_type>
624 inline
625 void
627  {
629 
630  const unwrap<T1> tmp1(in.a.get_ref());
631  const umat& aa = tmp1.M;
632 
634  (
635  ( aa.is_vec() == false ),
636  "Mat::elem(): given object is not a vector"
637  );
638 
639  const uword* aa_mem = aa.memptr();
640  const uword aa_n_elem = aa.n_elem;
641 
642  const unwrap_check< Mat<eT> > tmp2(in.m, out);
643  const Mat<eT>& m_local = tmp2.M;
644 
645  const eT* m_mem = m_local.memptr();
646  const uword m_n_elem = m_local.n_elem;
647 
648  arma_debug_check( (out.n_elem != aa_n_elem), "Mat::elem(): size mismatch" );
649 
650  eT* out_mem = out.memptr();
651 
652  uword i,j;
653  for(i=0, j=1; j<aa_n_elem; i+=2, j+=2)
654  {
655  const uword ii = aa_mem[i];
656  const uword jj = aa_mem[j];
657 
658  arma_debug_check( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" );
659 
660  if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { out_mem[i] += m_mem[ii]; out_mem[j] += m_mem[jj]; }
661  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { out_mem[i] -= m_mem[ii]; out_mem[j] -= m_mem[jj]; }
662  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { out_mem[i] *= m_mem[ii]; out_mem[j] *= m_mem[jj]; }
663  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { out_mem[i] /= m_mem[ii]; out_mem[j] /= m_mem[jj]; }
664  }
665 
666  if(i < aa_n_elem)
667  {
668  const uword ii = aa_mem[i];
669 
670  arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" );
671 
672  if(is_same_type<op_type, op_subview_elem_inplace_plus >::value == true) { out_mem[i] += m_mem[ii]; }
673  else if(is_same_type<op_type, op_subview_elem_inplace_minus>::value == true) { out_mem[i] -= m_mem[ii]; }
674  else if(is_same_type<op_type, op_subview_elem_inplace_schur>::value == true) { out_mem[i] *= m_mem[ii]; }
675  else if(is_same_type<op_type, op_subview_elem_inplace_div >::value == true) { out_mem[i] /= m_mem[ii]; }
676  }
677  }
678 
679 
680 
681 template<typename eT, typename T1>
682 inline
683 void
685  {
687 
688  mat_inplace_op<op_subview_elem_inplace_plus>(out, in);
689  }
690 
691 
692 
693 template<typename eT, typename T1>
694 inline
695 void
697  {
699 
700  mat_inplace_op<op_subview_elem_inplace_minus>(out, in);
701  }
702 
703 
704 
705 template<typename eT, typename T1>
706 inline
707 void
709  {
711 
712  mat_inplace_op<op_subview_elem_inplace_schur>(out, in);
713  }
714 
715 
716 
717 template<typename eT, typename T1>
718 inline
719 void
721  {
723 
724  mat_inplace_op<op_subview_elem_inplace_div>(out, in);
725  }
726 
727 
728 
arma_inline arma_warn_unused bool is_vec() const
returns true if the object can be interpreted as a column or row vector
Definition: Mat_meat.hpp:3824
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
void operator_equ(const subview_elem1< eT, T2 > &x)
void set_size(const uword in_elem)
change the matrix to have user specified dimensions (data is not preserved)
Definition: Mat_meat.hpp:4211
arma_aligned const Base< uword, T1 > & a
void operator*=(const eT val)
arma_inline const derived & get_ref() const
Definition: Base_meat.hpp:22
arma_aligned const Mat< eT > & m
const uword n_elem
number of elements in the matrix (read-only)
Definition: Mat_bones.hpp:31
void operator-=(const eT val)
#define arma_extra_debug_print
Definition: debug.hpp:1118
void fill(const eT val)
static void minus_inplace(Mat< eT > &out, const subview_elem1 &in)
void operator+=(const eT val)
void inplace_op(const eT val)
u32 uword
Definition: typedef.hpp:85
#define arma_extra_debug_warn
Definition: debug.hpp:1119
#define arma_debug_check
Definition: debug.hpp:1084
static void schur_inplace(Mat< eT > &out, const subview_elem1 &in)
static void mat_inplace_op(Mat< eT > &out, const subview_elem1 &in)
static void div_inplace(Mat< eT > &out, const subview_elem1 &in)
static void extract(Mat< eT > &out, const subview_elem1 &in)
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
Dense matrix class.
#define arma_inline
static void plus_inplace(Mat< eT > &out, const subview_elem1 &in)
void operator%=(const subview_elem1< eT, T2 > &x)
arma_aligned Mat< eT > * m_ptr
const Mat< eT > M
Definition: unwrap.hpp:142
arma_inline const Op< subview_elem1< eT, T1 >, op_htrans > t() const
void operator=(const subview_elem1< eT, T2 > &x)
void operator/=(const eT val)


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:59