NumCpp  2.11.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 
30 #include <algorithm>
31 #include <cmath>
32 #include <complex>
33 
35 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/NdArray.hpp"
37 #include "NumCpp/Utils/sqr.hpp"
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
50  template<typename dtype>
51  NdArray<double> rms(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
52  {
54 
55  double squareSum = 0.;
56  const auto function = [&squareSum](dtype value) -> void
57  { squareSum += utils::sqr(static_cast<double>(value)); };
58 
59  switch (inAxis)
60  {
61  case Axis::NONE:
62  {
63  std::for_each(inArray.cbegin(), inArray.cend(), function);
64  NdArray<double> returnArray = { std::sqrt(squareSum / static_cast<double>(inArray.size())) };
65  return returnArray;
66  }
67  case Axis::COL:
68  {
69  NdArray<double> returnArray(1, inArray.numRows());
70  for (uint32 row = 0; row < inArray.numRows(); ++row)
71  {
72  squareSum = 0.;
73  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
74  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(inArray.numCols()));
75  }
76 
77  return returnArray;
78  }
79  case Axis::ROW:
80  {
81  return rms(inArray.transpose(), Axis::COL);
82  }
83  default:
84  {
85  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
86  return {}; // get rid of compiler warning
87  }
88  }
89  }
90 
91  //============================================================================
92  // Method Description:
100  template<typename dtype>
101  NdArray<std::complex<double>> rms(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
102  {
104 
105  std::complex<double> squareSum = 0.;
106  const auto function = [&squareSum](std::complex<dtype> value) -> void
107  { squareSum += utils::sqr(complex_cast<double>(value)); };
108 
109  switch (inAxis)
110  {
111  case Axis::NONE:
112  {
113  std::for_each(inArray.cbegin(), inArray.cend(), function);
114  NdArray<std::complex<double>> returnArray = { std::sqrt(squareSum /
115  static_cast<double>(inArray.size())) };
116  return returnArray;
117  }
118  case Axis::COL:
119  {
120  NdArray<std::complex<double>> returnArray(1, inArray.numRows());
121  for (uint32 row = 0; row < inArray.numRows(); ++row)
122  {
123  squareSum = std::complex<double>(0., 0.);
124  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
125  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(inArray.numCols()));
126  }
127 
128  return returnArray;
129  }
130  case Axis::ROW:
131  {
132  return rms(inArray.transpose(), Axis::COL);
133  }
134  default:
135  {
136  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
137  return {}; // get rid of compiler warning
138  }
139  }
140  }
141 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
size_type size() const noexcept
Definition: NdArrayCore.hpp:4477
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1318
self_type transpose() const
Definition: NdArrayCore.hpp:4837
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3418
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3430
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1626
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:42
Definition: Cartesian.hpp:40
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rms.hpp:51
Axis
Enum To describe an axis.
Definition: Types.hpp:47
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48
std::uint32_t uint32
Definition: Types.hpp:40