NumCpp  1.0
A C++ implementation of the Python Numpy library
rms.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/Utils/sqr.hpp"
35 
36 #include <algorithm>
37 #include <cmath>
38 #include <complex>
39 
40 namespace nc
41 {
42  //============================================================================
43  // Method Description:
52  template<typename dtype>
53  NdArray<double> rms(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
54  {
56 
57  double squareSum = 0.0;
58  const auto function = [&squareSum](dtype value) -> void
59  {
60  squareSum += utils::sqr(static_cast<double>(value));
61  };
62 
63  switch (inAxis)
64  {
65  case Axis::NONE:
66  {
67  std::for_each(inArray.cbegin(), inArray.cend(), function);
68  NdArray<double> returnArray = { std::sqrt(squareSum / static_cast<double>(inArray.size())) };
69  return returnArray;
70  }
71  case Axis::COL:
72  {
73  NdArray<double> returnArray(1, inArray.numRows());
74  for (uint32 row = 0; row < inArray.numRows(); ++row)
75  {
76  squareSum = 0.0;
77  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
78  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(inArray.numCols()));
79  }
80 
81  return returnArray;
82  }
83  case Axis::ROW:
84  {
85  NdArray<dtype> transposedArray = inArray.transpose();
86  NdArray<double> returnArray(1, transposedArray.numRows());
87  for (uint32 row = 0; row < transposedArray.numRows(); ++row)
88  {
89  squareSum = 0.0;
90  std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
91  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(transposedArray.numCols()));
92  }
93 
94  return returnArray;
95  }
96  default:
97  {
98  return NdArray<double>(); // get rid of compiler warning
99  }
100  }
101  }
102 
103  //============================================================================
104  // Method Description:
113  template<typename dtype>
114  NdArray<std::complex<double>> rms(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
115  {
117 
118  std::complex<double> squareSum = 0.0;
119  const auto function = [&squareSum](std::complex<dtype> value) -> void
120  {
121  squareSum += utils::sqr(complex_cast<double>(value));
122  };
123 
124  switch (inAxis)
125  {
126  case Axis::NONE:
127  {
128  std::for_each(inArray.cbegin(), inArray.cend(), function);
129  NdArray<std::complex<double>> returnArray = { std::sqrt(squareSum / static_cast<double>(inArray.size())) };
130  return returnArray;
131  }
132  case Axis::COL:
133  {
134  NdArray<std::complex<double>> returnArray(1, inArray.numRows());
135  for (uint32 row = 0; row < inArray.numRows(); ++row)
136  {
137  squareSum = std::complex<double>(0.0, 0.0);
138  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
139  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(inArray.numCols()));
140  }
141 
142  return returnArray;
143  }
144  case Axis::ROW:
145  {
146  NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
147  NdArray<std::complex<double>> returnArray(1, transposedArray.numRows());
148  for (uint32 row = 0; row < transposedArray.numRows(); ++row)
149  {
150  squareSum = std::complex<double>(0.0, 0.0);
151  std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
152  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(transposedArray.numCols()));
153  }
154 
155  return returnArray;
156  }
157  default:
158  {
159  return NdArray<std::complex<double>>(); // get rid of compiler warning
160  }
161  }
162  }
163 }
StaticAsserts.hpp
nc::Axis::NONE
@ NONE
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:38
nc::Axis::ROW
@ ROW
nc::sqrt
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:51
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f) noexcept
Definition: StlAlgorithms.hpp:214
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4591
nc::NdArray< double >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::NdArray::numCols
uint32 numCols() const noexcept
Definition: NdArrayCore.hpp:3415
NdArray.hpp
nc::rms
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rms.hpp:53
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4310
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
nc
Definition: Coordinate.hpp:45
sqr.hpp
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
nc::utils::sqr
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:45
Types.hpp
nc::NdArray::numRows
uint32 numRows() const noexcept
Definition: NdArrayCore.hpp:3428
nc::Axis::COL
@ COL