diagview_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-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>
19 inline
21  {
23  }
24 
25 
26 template<typename eT>
28 diagview<eT>::diagview(const Mat<eT>& in_m, const uword in_row_offset, const uword in_col_offset, const uword in_len)
29  : m(in_m)
30  , m_ptr(0)
31  , row_offset(in_row_offset)
32  , col_offset(in_col_offset)
33  , n_rows(in_len)
34  , n_elem(in_len)
35  {
37  }
38 
39 
40 
41 template<typename eT>
43 diagview<eT>::diagview(Mat<eT>& in_m, const uword in_row_offset, const uword in_col_offset, const uword in_len)
44  : m(in_m)
45  , m_ptr(&in_m)
46  , row_offset(in_row_offset)
47  , col_offset(in_col_offset)
48  , n_rows(in_len)
49  , n_elem(in_len)
50  {
52  }
53 
54 
55 
57 template<typename eT>
58 inline
59 void
61  {
63 
64  diagview<eT>& t = *this;
65 
66  arma_debug_check( (t.n_elem != x.n_elem), "diagview: diagonals have incompatible lengths");
67 
68  Mat<eT>& t_m = *(t.m_ptr);
69  const Mat<eT>& x_m = x.m;
70 
71  if(&t_m != &x_m)
72  {
73  const uword t_n_elem = t.n_elem;
74  const uword t_row_offset = t.row_offset;
75  const uword t_col_offset = t.col_offset;
76 
77  const uword x_row_offset = x.row_offset;
78  const uword x_col_offset = x.col_offset;
79 
80  uword i,j;
81  for(i=0, j=1; j < t_n_elem; i+=2, j+=2)
82  {
83  const eT tmp_i = x_m.at(i + x_row_offset, i + x_col_offset);
84  const eT tmp_j = x_m.at(j + x_row_offset, j + x_col_offset);
85 
86  t_m.at(i + t_row_offset, i + t_col_offset) = tmp_i;
87  t_m.at(j + t_row_offset, j + t_col_offset) = tmp_j;
88  }
89 
90  if(i < t_n_elem)
91  {
92  t_m.at(i + t_row_offset, i + t_col_offset) = x_m.at(i + x_row_offset, i + x_col_offset);
93  }
94  }
95  else
96  {
97  const Mat<eT> tmp = x;
98 
99  (*this).operator=(tmp);
100  }
101  }
102 
103 
104 
105 template<typename eT>
106 inline
107 void
109  {
111 
112  Mat<eT>& t_m = (*m_ptr);
113 
114  const uword t_n_elem = n_elem;
115  const uword t_row_offset = row_offset;
116  const uword t_col_offset = col_offset;
117 
118  for(uword i=0; i<t_n_elem; ++i)
119  {
120  t_m.at( i + t_row_offset, i + t_col_offset) += val;
121  }
122  }
123 
124 
125 
126 template<typename eT>
127 inline
128 void
130  {
132 
133  Mat<eT>& t_m = (*m_ptr);
134 
135  const uword t_n_elem = n_elem;
136  const uword t_row_offset = row_offset;
137  const uword t_col_offset = col_offset;
138 
139  for(uword i=0; i<t_n_elem; ++i)
140  {
141  t_m.at( i + t_row_offset, i + t_col_offset) -= val;
142  }
143  }
144 
145 
146 
147 template<typename eT>
148 inline
149 void
151  {
153 
154  Mat<eT>& t_m = (*m_ptr);
155 
156  const uword t_n_elem = n_elem;
157  const uword t_row_offset = row_offset;
158  const uword t_col_offset = col_offset;
159 
160  for(uword i=0; i<t_n_elem; ++i)
161  {
162  t_m.at( i + t_row_offset, i + t_col_offset) *= val;
163  }
164  }
165 
166 
167 
168 template<typename eT>
169 inline
170 void
172  {
174 
175  Mat<eT>& t_m = (*m_ptr);
176 
177  const uword t_n_elem = n_elem;
178  const uword t_row_offset = row_offset;
179  const uword t_col_offset = col_offset;
180 
181  for(uword i=0; i<t_n_elem; ++i)
182  {
183  t_m.at( i + t_row_offset, i + t_col_offset) /= val;
184  }
185  }
186 
187 
188 
190 template<typename eT>
191 template<typename T1>
192 inline
193 void
195  {
197 
198  const unwrap<T1> tmp(o.get_ref());
199  const Mat<eT>& x = tmp.M;
200 
201  diagview<eT>& t = *this;
202 
204  (
205  ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ),
206  "diagview: given object has incompatible size"
207  );
208 
209  Mat<eT>& t_m = *(t.m_ptr);
210 
211  const uword t_n_elem = t.n_elem;
212  const uword t_row_offset = t.row_offset;
213  const uword t_col_offset = t.col_offset;
214 
215  const eT* x_mem = x.memptr();
216 
217  uword i,j;
218  for(i=0, j=1; j < t_n_elem; i+=2, j+=2)
219  {
220  const eT tmp_i = x_mem[i];
221  const eT tmp_j = x_mem[j];
222 
223  t_m.at( i + t_row_offset, i + t_col_offset) = tmp_i;
224  t_m.at( j + t_row_offset, j + t_col_offset) = tmp_j;
225  }
226 
227  if(i < t_n_elem)
228  {
229  t_m.at( i + t_row_offset, i + t_col_offset) = x_mem[i];
230  }
231  }
232 
233 
234 
235 template<typename eT>
236 template<typename T1>
237 inline
238 void
240  {
242 
243  const unwrap<T1> tmp(o.get_ref());
244  const Mat<eT>& x = tmp.M;
245 
246  diagview<eT>& t = *this;
247 
249  (
250  ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ),
251  "diagview: given object has incompatible size"
252  );
253 
254  Mat<eT>& t_m = *(t.m_ptr);
255 
256  const uword t_n_elem = t.n_elem;
257  const uword t_row_offset = t.row_offset;
258  const uword t_col_offset = t.col_offset;
259 
260  const eT* x_mem = x.memptr();
261 
262  uword i,j;
263  for(i=0, j=1; j < t_n_elem; i+=2, j+=2)
264  {
265  const eT tmp_i = x_mem[i];
266  const eT tmp_j = x_mem[j];
267 
268  t_m.at( i + t_row_offset, i + t_col_offset) += tmp_i;
269  t_m.at( j + t_row_offset, j + t_col_offset) += tmp_j;
270  }
271 
272  if(i < t_n_elem)
273  {
274  t_m.at( i + t_row_offset, i + t_col_offset) += x_mem[i];
275  }
276  }
277 
278 
279 
280 template<typename eT>
281 template<typename T1>
282 inline
283 void
285  {
287 
288  const unwrap<T1> tmp(o.get_ref());
289  const Mat<eT>& x = tmp.M;
290 
291  diagview<eT>& t = *this;
292 
294  (
295  ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ),
296  "diagview: given object has incompatible size"
297  );
298 
299  Mat<eT>& t_m = *(t.m_ptr);
300 
301  const uword t_n_elem = t.n_elem;
302  const uword t_row_offset = t.row_offset;
303  const uword t_col_offset = t.col_offset;
304 
305  const eT* x_mem = x.memptr();
306 
307  uword i,j;
308  for(i=0, j=1; j < t_n_elem; i+=2, j+=2)
309  {
310  const eT tmp_i = x_mem[i];
311  const eT tmp_j = x_mem[j];
312 
313  t_m.at( i + t_row_offset, i + t_col_offset) -= tmp_i;
314  t_m.at( j + t_row_offset, j + t_col_offset) -= tmp_j;
315  }
316 
317  if(i < t_n_elem)
318  {
319  t_m.at( i + t_row_offset, i + t_col_offset) -= x_mem[i];
320  }
321  }
322 
323 
324 
325 template<typename eT>
326 template<typename T1>
327 inline
328 void
330  {
332 
333  const unwrap<T1> tmp(o.get_ref());
334  const Mat<eT>& x = tmp.M;
335 
336  diagview<eT>& t = *this;
337 
339  (
340  ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ),
341  "diagview: given object has incompatible size"
342  );
343 
344  Mat<eT>& t_m = *(t.m_ptr);
345 
346  const uword t_n_elem = t.n_elem;
347  const uword t_row_offset = t.row_offset;
348  const uword t_col_offset = t.col_offset;
349 
350  const eT* x_mem = x.memptr();
351 
352  uword i,j;
353  for(i=0, j=1; j < t_n_elem; i+=2, j+=2)
354  {
355  const eT tmp_i = x_mem[i];
356  const eT tmp_j = x_mem[j];
357 
358  t_m.at( i + t_row_offset, i + t_col_offset) *= tmp_i;
359  t_m.at( j + t_row_offset, j + t_col_offset) *= tmp_j;
360  }
361 
362  if(i < t_n_elem)
363  {
364  t_m.at( i + t_row_offset, i + t_col_offset) *= x_mem[i];
365  }
366  }
367 
368 
369 
370 template<typename eT>
371 template<typename T1>
372 inline
373 void
375  {
377 
378  const unwrap<T1> tmp(o.get_ref());
379  const Mat<eT>& x = tmp.M;
380 
381  diagview<eT>& t = *this;
382 
384  (
385  ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ),
386  "diagview: given object has incompatible size"
387  );
388 
389  Mat<eT>& t_m = *(t.m_ptr);
390 
391  const uword t_n_elem = t.n_elem;
392  const uword t_row_offset = t.row_offset;
393  const uword t_col_offset = t.col_offset;
394 
395  const eT* x_mem = x.memptr();
396 
397  uword i,j;
398  for(i=0, j=1; j < t_n_elem; i+=2, j+=2)
399  {
400  const eT tmp_i = x_mem[i];
401  const eT tmp_j = x_mem[j];
402 
403  t_m.at( i + t_row_offset, i + t_col_offset) /= tmp_i;
404  t_m.at( j + t_row_offset, j + t_col_offset) /= tmp_j;
405  }
406 
407  if(i < t_n_elem)
408  {
409  t_m.at( i + t_row_offset, i + t_col_offset) /= x_mem[i];
410  }
411  }
412 
413 
414 
416 template<typename eT>
417 inline
418 void
420  {
422 
423  // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing;
424  // size setting and alias checking is done by either the Mat contructor or operator=()
425 
426  const Mat<eT>& in_m = in.m;
427 
428  const uword in_n_elem = in.n_elem;
429  const uword in_row_offset = in.row_offset;
430  const uword in_col_offset = in.col_offset;
431 
432  eT* out_mem = out.memptr();
433 
434  uword i,j;
435  for(i=0, j=1; j < in_n_elem; i+=2, j+=2)
436  {
437  const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset );
438  const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset );
439 
440  out_mem[i] = tmp_i;
441  out_mem[j] = tmp_j;
442  }
443 
444  if(i < in_n_elem)
445  {
446  out_mem[i] = in_m.at( i + in_row_offset, i + in_col_offset );
447  }
448  }
449 
450 
451 
453 template<typename eT>
454 inline
455 void
457  {
459 
460  arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "addition");
461 
462  const Mat<eT>& in_m = in.m;
463 
464  const uword in_n_elem = in.n_elem;
465  const uword in_row_offset = in.row_offset;
466  const uword in_col_offset = in.col_offset;
467 
468  eT* out_mem = out.memptr();
469 
470  uword i,j;
471  for(i=0, j=1; j < in_n_elem; i+=2, j+=2)
472  {
473  const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset );
474  const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset );
475 
476  out_mem[i] += tmp_i;
477  out_mem[j] += tmp_j;
478  }
479 
480  if(i < in_n_elem)
481  {
482  out_mem[i] += in_m.at( i + in_row_offset, i + in_col_offset );
483  }
484  }
485 
486 
487 
489 template<typename eT>
490 inline
491 void
493  {
495 
496  arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "subtraction");
497 
498  const Mat<eT>& in_m = in.m;
499 
500  const uword in_n_elem = in.n_elem;
501  const uword in_row_offset = in.row_offset;
502  const uword in_col_offset = in.col_offset;
503 
504  eT* out_mem = out.memptr();
505 
506  uword i,j;
507  for(i=0, j=1; j < in_n_elem; i+=2, j+=2)
508  {
509  const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset );
510  const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset );
511 
512  out_mem[i] -= tmp_i;
513  out_mem[j] -= tmp_j;
514  }
515 
516  if(i < in_n_elem)
517  {
518  out_mem[i] -= in_m.at( i + in_row_offset, i + in_col_offset );
519  }
520  }
521 
522 
523 
525 template<typename eT>
526 inline
527 void
529  {
531 
532  arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "element-wise multiplication");
533 
534  const Mat<eT>& in_m = in.m;
535 
536  const uword in_n_elem = in.n_elem;
537  const uword in_row_offset = in.row_offset;
538  const uword in_col_offset = in.col_offset;
539 
540  eT* out_mem = out.memptr();
541 
542  uword i,j;
543  for(i=0, j=1; j < in_n_elem; i+=2, j+=2)
544  {
545  const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset );
546  const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset );
547 
548  out_mem[i] *= tmp_i;
549  out_mem[j] *= tmp_j;
550  }
551 
552  if(i < in_n_elem)
553  {
554  out_mem[i] *= in_m.at( i + in_row_offset, i + in_col_offset );
555  }
556  }
557 
558 
559 
561 template<typename eT>
562 inline
563 void
565  {
567 
568  arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "element-wise division");
569 
570  const Mat<eT>& in_m = in.m;
571 
572  const uword in_n_elem = in.n_elem;
573  const uword in_row_offset = in.row_offset;
574  const uword in_col_offset = in.col_offset;
575 
576  eT* out_mem = out.memptr();
577 
578  uword i,j;
579  for(i=0, j=1; j < in_n_elem; i+=2, j+=2)
580  {
581  const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset );
582  const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset );
583 
584  out_mem[i] /= tmp_i;
585  out_mem[j] /= tmp_j;
586  }
587 
588  if(i < in_n_elem)
589  {
590  out_mem[i] /= in_m.at( i + in_row_offset, i + in_col_offset );
591  }
592  }
593 
594 
595 
596 template<typename eT>
598 eT&
600  {
601  return (*m_ptr).at(i+row_offset, i+col_offset);
602  }
603 
604 
605 
606 template<typename eT>
608 eT
610  {
611  return m.at(i+row_offset, i+col_offset);
612  }
613 
614 
615 
616 template<typename eT>
618 eT&
620  {
621  return (*m_ptr).at(i+row_offset, i+col_offset);
622  }
623 
624 
625 
626 template<typename eT>
628 eT
629 diagview<eT>::at(const uword i) const
630  {
631  return m.at(i+row_offset, i+col_offset);
632  }
633 
634 
635 
636 template<typename eT>
638 eT&
640  {
641  arma_debug_check( (i >= n_elem), "diagview::operator(): out of bounds" );
642 
643  return (*m_ptr).at(i+row_offset, i+col_offset);
644  }
645 
646 
647 
648 template<typename eT>
650 eT
652  {
653  arma_debug_check( (i >= n_elem), "diagview::operator(): out of bounds" );
654 
655  return m.at(i+row_offset, i+col_offset);
656  }
657 
658 
659 
660 template<typename eT>
662 eT&
664  {
665  return (*m_ptr).at(row+row_offset, row+col_offset);
666  }
667 
668 
669 
670 template<typename eT>
672 eT
673 diagview<eT>::at(const uword row, const uword col) const
674  {
675  return m.at(row+row_offset, row+col_offset);
676  }
677 
678 
679 
680 template<typename eT>
682 eT&
684  {
685  arma_debug_check( ((row >= n_elem) || (col > 0)), "diagview::operator(): out of bounds" );
686 
687  return (*m_ptr).at(row+row_offset, row+col_offset);
688  }
689 
690 
691 
692 template<typename eT>
694 eT
696  {
697  arma_debug_check( ((row >= n_elem) || (col > 0)), "diagview::operator(): out of bounds" );
698 
699  return m.at(row+row_offset, row+col_offset);
700  }
701 
702 
703 
704 template<typename eT>
705 inline
706 void
707 diagview<eT>::fill(const eT val)
708  {
710 
711  Mat<eT>& x = (*m_ptr);
712 
713  for(uword i=0; i<n_elem; ++i)
714  {
715  x.at(i+row_offset, i+col_offset) = val;
716  }
717  }
718 
719 
720 
721 template<typename eT>
722 inline
723 void
725  {
727 
728  (*this).fill(eT(0));
729  }
730 
731 
732 
733 template<typename eT>
734 inline
735 void
737  {
739 
740  (*this).fill(eT(1));
741  }
742 
743 
744 
void operator+=(const eT val)
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
arma_aligned const Mat< eT > & m
static void plus_inplace(Mat< eT > &out, const diagview &in)
X += Y.diag()
static void minus_inplace(Mat< eT > &out, const diagview &in)
X -= Y.diag()
arma_inline const derived & get_ref() const
Definition: Base_meat.hpp:22
const uword n_rows
arma_aligned Mat< eT > * m_ptr
void zeros()
static const uword n_cols
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
void operator-=(const eT val)
#define arma_debug_assert_same_size
Definition: debug.hpp:1086
void operator%=(const Base< eT, T1 > &x)
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
arma_inline eT & at(const uword i)
const uword n_rows
const uword n_elem
u32 uword
Definition: typedef.hpp:85
void operator*=(const eT val)
const uword col_offset
#define arma_debug_check
Definition: debug.hpp:1084
void fill(const eT val)
arma_inline arma_warn_unused eT & at(const uword i)
linear element accessor (treats the matrix as a vector); no bounds check.
Definition: Mat_meat.hpp:3692
arma_aligned const Mat< eT > & m
subview_row< eT > row(const uword row_num)
creation of subview (row vector)
arma_aligned Mat< eT > * m_ptr
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
void operator=(const diagview &x)
set a diagonal of our matrix using a diagonal from a foreign matrix
const uword row_offset
static void schur_inplace(Mat< eT > &out, const diagview &in)
X %= Y.diag()
Dense matrix class.
#define arma_inline
const uword n_elem
static void div_inplace(Mat< eT > &out, const diagview &in)
X /= Y.diag()
void operator/=(const eT val)
arma_inline eT & operator()(const uword i)
static void extract(Mat< eT > &out, const diagview &in)
extract a diagonal and store it as a column vector
Class for storing data required to extract and set the diagonals of a matrix.
arma_inline const Op< diagview< eT >, op_htrans > t() const
arma_inline eT & operator[](const uword i)
subview_col< eT > col(const uword col_num)
creation of subview (column vector)


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