fn_trace.hpp
Go to the documentation of this file.
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 
16 
17 
19 template<typename T1>
20 inline
22 typename T1::elem_type
24  {
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
49 typename T1::elem_type
51  {
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 
72 template<typename T1, typename T2>
73 inline
75 typename T1::elem_type
77  {
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 
arma_inline const derived & get_ref() const
Definition: Base_meat.hpp:22
const uword n_elem
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
arma_aligned const T1 & m
storage of reference to the operand (eg. a matrix)
Definition: Op_bones.hpp:45
arma_inline arma_warn_unused eT * colptr(const uword in_col)
returns a pointer to array of eTs for a specified column; no bounds check
Definition: Mat_meat.hpp:4000
u32 uword
Definition: typedef.hpp:85
#define arma_debug_check
Definition: debug.hpp:1084
const T1 & A
first operand
Definition: Glue_bones.hpp:44
arma_warn_unused T1::elem_type trace(const Base< typename T1::elem_type, T1 > &X)
Immediate trace (sum of diagonal elements) of a square dense matrix.
Definition: fn_trace.hpp:23
const Mat< eT > M
Definition: unwrap.hpp:32
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
const T2 & B
second operand
Definition: Glue_bones.hpp:45
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
#define arma_warn_unused
#define arma_debug_assert_mul_size
Definition: debug.hpp:1087
Dense matrix class.


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