NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
rms.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
32 #include "NumCpp/NdArray.hpp"
33 #include "NumCpp/Utils/sqr.hpp"
34 
35 #include <algorithm>
36 #include <cmath>
37 #include <complex>
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
51  template<typename dtype>
52  NdArray<double> rms(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
53  {
55 
56  double squareSum = 0.0;
57  const auto function = [&squareSum](dtype value) -> void
58  {
59  squareSum += utils::sqr(static_cast<double>(value));
60  };
61 
62  switch (inAxis)
63  {
64  case Axis::NONE:
65  {
66  std::for_each(inArray.cbegin(), inArray.cend(), function);
67  NdArray<double> returnArray = { std::sqrt(squareSum / static_cast<double>(inArray.size())) };
68  return returnArray;
69  }
70  case Axis::COL:
71  {
72  NdArray<double> returnArray(1, inArray.numRows());
73  for (uint32 row = 0; row < inArray.numRows(); ++row)
74  {
75  squareSum = 0.0;
76  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
77  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(inArray.numCols()));
78  }
79 
80  return returnArray;
81  }
82  case Axis::ROW:
83  {
84  NdArray<dtype> transposedArray = inArray.transpose();
85  NdArray<double> returnArray(1, transposedArray.numRows());
86  for (uint32 row = 0; row < transposedArray.numRows(); ++row)
87  {
88  squareSum = 0.0;
89  std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
90  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(transposedArray.numCols()));
91  }
92 
93  return returnArray;
94  }
95  default:
96  {
97  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
98  return {}; // 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  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
160  return {}; // get rid of compiler warning
161  }
162  }
163  }
164 } // namespace nc
StaticAsserts.hpp
nc::Axis::NONE
@ NONE
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
nc::Axis::ROW
@ ROW
nc::sqrt
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:50
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
nc::NdArray< double >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
nc::NdArray::numCols
uint32 numCols() const noexcept
Definition: NdArrayCore.hpp:3475
NdArray.hpp
nc::rms
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rms.hpp:52
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1487
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:46
nc
Definition: Coordinate.hpp:44
sqr.hpp
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1143
nc::utils::sqr
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Types.hpp
nc::NdArray::numRows
uint32 numRows() const noexcept
Definition: NdArrayCore.hpp:3488
nc::Axis::COL
@ COL