Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-2.4.4/include/armadillo_bits/fn_trace.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-2010 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2008-2010 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 fn_trace | |
15 //! @{ | |
16 | |
17 | |
18 //! Immediate trace (sum of diagonal elements) of a square dense matrix | |
19 template<typename T1> | |
20 inline | |
21 arma_warn_unused | |
22 typename T1::elem_type | |
23 trace(const Base<typename T1::elem_type,T1>& X) | |
24 { | |
25 arma_extra_debug_sigprint(); | |
26 | |
27 typedef typename T1::elem_type eT; | |
28 | |
29 const Proxy<T1> A(X.get_ref()); | |
30 | |
31 arma_debug_check( (A.get_n_rows() != A.get_n_cols()), "trace(): matrix must be square sized" ); | |
32 | |
33 const uword N = A.get_n_rows(); | |
34 eT val = eT(0); | |
35 | |
36 for(uword i=0; i<N; ++i) | |
37 { | |
38 val += A.at(i,i); | |
39 } | |
40 | |
41 return val; | |
42 } | |
43 | |
44 | |
45 | |
46 template<typename T1> | |
47 inline | |
48 arma_warn_unused | |
49 typename T1::elem_type | |
50 trace(const Op<T1, op_diagmat>& X) | |
51 { | |
52 arma_extra_debug_sigprint(); | |
53 | |
54 typedef typename T1::elem_type eT; | |
55 | |
56 const diagmat_proxy<T1> A(X.m); | |
57 | |
58 const uword N = A.n_elem; | |
59 | |
60 eT val = eT(0); | |
61 | |
62 for(uword i=0; i<N; ++i) | |
63 { | |
64 val += A[i]; | |
65 } | |
66 | |
67 return val; | |
68 } | |
69 | |
70 | |
71 //! speedup for trace(A*B), where the result of A*B is a square sized matrix | |
72 template<typename T1, typename T2> | |
73 inline | |
74 arma_warn_unused | |
75 typename T1::elem_type | |
76 trace(const Glue<T1, T2, glue_times>& X) | |
77 { | |
78 arma_extra_debug_sigprint(); | |
79 | |
80 typedef typename T1::elem_type eT; | |
81 | |
82 const unwrap<T1> tmp1(X.A); | |
83 const unwrap<T2> tmp2(X.B); | |
84 | |
85 const Mat<eT>& A = tmp1.M; | |
86 const Mat<eT>& B = tmp2.M; | |
87 | |
88 arma_debug_assert_mul_size(A, B, "matrix multiply"); | |
89 | |
90 arma_debug_check( (A.n_rows != B.n_cols), "trace(): matrix must be square sized" ); | |
91 | |
92 const uword N1 = A.n_rows; | |
93 const uword N2 = A.n_cols; | |
94 eT val = eT(0); | |
95 | |
96 for(uword i=0; i<N1; ++i) | |
97 { | |
98 const eT* B_colmem = B.colptr(i); | |
99 eT acc = eT(0); | |
100 | |
101 for(uword j=0; j<N2; ++j) | |
102 { | |
103 acc += A.at(i,j) * B_colmem[j]; | |
104 } | |
105 | |
106 val += acc; | |
107 } | |
108 | |
109 return val; | |
110 } | |
111 | |
112 | |
113 | |
114 //! @} |