comparison armadillo-2.4.4/include/armadillo_bits/diagview_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) 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
14 //! \addtogroup diagview
15 //! @{
16
17
18 template<typename eT>
19 inline
20 diagview<eT>::~diagview()
21 {
22 arma_extra_debug_sigprint();
23 }
24
25
26 template<typename eT>
27 arma_inline
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 {
36 arma_extra_debug_sigprint();
37 }
38
39
40
41 template<typename eT>
42 arma_inline
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 {
51 arma_extra_debug_sigprint();
52 }
53
54
55
56 //! set a diagonal of our matrix using a diagonal from a foreign matrix
57 template<typename eT>
58 inline
59 void
60 diagview<eT>::operator= (const diagview<eT>& x)
61 {
62 arma_extra_debug_sigprint();
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
108 diagview<eT>::operator+=(const eT val)
109 {
110 arma_extra_debug_sigprint();
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
129 diagview<eT>::operator-=(const eT val)
130 {
131 arma_extra_debug_sigprint();
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
150 diagview<eT>::operator*=(const eT val)
151 {
152 arma_extra_debug_sigprint();
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
171 diagview<eT>::operator/=(const eT val)
172 {
173 arma_extra_debug_sigprint();
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
189 //! set a diagonal of our matrix using data from a foreign object
190 template<typename eT>
191 template<typename T1>
192 inline
193 void
194 diagview<eT>::operator= (const Base<eT,T1>& o)
195 {
196 arma_extra_debug_sigprint();
197
198 const unwrap<T1> tmp(o.get_ref());
199 const Mat<eT>& x = tmp.M;
200
201 diagview<eT>& t = *this;
202
203 arma_debug_check
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
239 diagview<eT>::operator+=(const Base<eT,T1>& o)
240 {
241 arma_extra_debug_sigprint();
242
243 const unwrap<T1> tmp(o.get_ref());
244 const Mat<eT>& x = tmp.M;
245
246 diagview<eT>& t = *this;
247
248 arma_debug_check
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
284 diagview<eT>::operator-=(const Base<eT,T1>& o)
285 {
286 arma_extra_debug_sigprint();
287
288 const unwrap<T1> tmp(o.get_ref());
289 const Mat<eT>& x = tmp.M;
290
291 diagview<eT>& t = *this;
292
293 arma_debug_check
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
329 diagview<eT>::operator%=(const Base<eT,T1>& o)
330 {
331 arma_extra_debug_sigprint();
332
333 const unwrap<T1> tmp(o.get_ref());
334 const Mat<eT>& x = tmp.M;
335
336 diagview<eT>& t = *this;
337
338 arma_debug_check
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
374 diagview<eT>::operator/=(const Base<eT,T1>& o)
375 {
376 arma_extra_debug_sigprint();
377
378 const unwrap<T1> tmp(o.get_ref());
379 const Mat<eT>& x = tmp.M;
380
381 diagview<eT>& t = *this;
382
383 arma_debug_check
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
415 //! extract a diagonal and store it as a column vector
416 template<typename eT>
417 inline
418 void
419 diagview<eT>::extract(Mat<eT>& out, const diagview<eT>& in)
420 {
421 arma_extra_debug_sigprint();
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
452 //! X += Y.diag()
453 template<typename eT>
454 inline
455 void
456 diagview<eT>::plus_inplace(Mat<eT>& out, const diagview<eT>& in)
457 {
458 arma_extra_debug_sigprint();
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
488 //! X -= Y.diag()
489 template<typename eT>
490 inline
491 void
492 diagview<eT>::minus_inplace(Mat<eT>& out, const diagview<eT>& in)
493 {
494 arma_extra_debug_sigprint();
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
524 //! X %= Y.diag()
525 template<typename eT>
526 inline
527 void
528 diagview<eT>::schur_inplace(Mat<eT>& out, const diagview<eT>& in)
529 {
530 arma_extra_debug_sigprint();
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
560 //! X /= Y.diag()
561 template<typename eT>
562 inline
563 void
564 diagview<eT>::div_inplace(Mat<eT>& out, const diagview<eT>& in)
565 {
566 arma_extra_debug_sigprint();
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>
597 arma_inline
598 eT&
599 diagview<eT>::operator[](const uword i)
600 {
601 return (*m_ptr).at(i+row_offset, i+col_offset);
602 }
603
604
605
606 template<typename eT>
607 arma_inline
608 eT
609 diagview<eT>::operator[](const uword i) const
610 {
611 return m.at(i+row_offset, i+col_offset);
612 }
613
614
615
616 template<typename eT>
617 arma_inline
618 eT&
619 diagview<eT>::at(const uword i)
620 {
621 return (*m_ptr).at(i+row_offset, i+col_offset);
622 }
623
624
625
626 template<typename eT>
627 arma_inline
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>
637 arma_inline
638 eT&
639 diagview<eT>::operator()(const uword i)
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>
649 arma_inline
650 eT
651 diagview<eT>::operator()(const uword i) const
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>
661 arma_inline
662 eT&
663 diagview<eT>::at(const uword row, const uword col)
664 {
665 return (*m_ptr).at(row+row_offset, row+col_offset);
666 }
667
668
669
670 template<typename eT>
671 arma_inline
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>
681 arma_inline
682 eT&
683 diagview<eT>::operator()(const uword row, const uword col)
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>
693 arma_inline
694 eT
695 diagview<eT>::operator()(const uword row, const uword col) const
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 {
709 arma_extra_debug_sigprint();
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
724 diagview<eT>::zeros()
725 {
726 arma_extra_debug_sigprint();
727
728 (*this).fill(eT(0));
729 }
730
731
732
733 template<typename eT>
734 inline
735 void
736 diagview<eT>::ones()
737 {
738 arma_extra_debug_sigprint();
739
740 (*this).fill(eT(1));
741 }
742
743
744
745 //! @}