op_sum_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 
21 template<typename T1>
22 inline
23 void
25  {
27 
28  const uword dim = in.aux_uword_a;
29  arma_debug_check( (dim > 1), "sum(): incorrect usage. dim must be 0 or 1");
30 
31  typedef typename T1::elem_type eT;
32 
33  const unwrap_check<T1> tmp(in.m, out);
34  const Mat<eT>& X = tmp.M;
35 
36  const uword X_n_rows = X.n_rows;
37  const uword X_n_cols = X.n_cols;
38 
39  if(dim == 0) // traverse across rows (i.e. find the sum in each column)
40  {
41  out.set_size(1, X_n_cols);
42 
43  eT* out_mem = out.memptr();
44 
45  for(uword col=0; col<X_n_cols; ++col)
46  {
47  out_mem[col] = arrayops::accumulate(X.colptr(col), X_n_rows);
48  }
49  }
50  else // traverse across columns (i.e. find the sum in each row)
51  {
52  out.set_size(X_n_rows, 1);
53 
54  eT* out_mem = out.memptr();
55 
56  for(uword row=0; row<X_n_rows; ++row)
57  {
58  eT val = eT(0);
59 
60  for(uword col=0; col<X_n_cols; ++col)
61  {
62  val += X.at(row,col);
63  }
64 
65  out_mem[row] = val;
66  }
67  }
68  }
69 
70 
71 
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
void set_size(const uword in_elem)
change the matrix to have user specified dimensions (data is not preserved)
Definition: Mat_meat.hpp:4211
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
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
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_sum > &in)
Immediate sum of elements of a matrix along a specified dimension (either rows or columns)...
Definition: op_sum_meat.hpp:24
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
Dense matrix class.
arma_hot static arma_pure eT accumulate(const eT *src, const uword n_elem)
const Mat< eT > M
Definition: unwrap.hpp:142
arma_aligned uword aux_uword_a
storage of auxiliary data, uword format
Definition: Op_bones.hpp:47


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