NumCpp  2.1.0
A C++ implementation of the Python Numpy library
rms.hpp
Go to the documentation of this file.
1 #pragma once
30 
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.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  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
99  return {}; // get rid of compiler warning
100  }
101  }
102  }
103 
104  //============================================================================
105  // Method Description:
114  template<typename dtype>
115  NdArray<std::complex<double>> rms(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
116  {
118 
119  std::complex<double> squareSum = 0.0;
120  const auto function = [&squareSum](std::complex<dtype> value) -> void
121  {
122  squareSum += utils::sqr(complex_cast<double>(value));
123  };
124 
125  switch (inAxis)
126  {
127  case Axis::NONE:
128  {
129  std::for_each(inArray.cbegin(), inArray.cend(), function);
130  NdArray<std::complex<double>> returnArray = { std::sqrt(squareSum / static_cast<double>(inArray.size())) };
131  return returnArray;
132  }
133  case Axis::COL:
134  {
135  NdArray<std::complex<double>> returnArray(1, inArray.numRows());
136  for (uint32 row = 0; row < inArray.numRows(); ++row)
137  {
138  squareSum = std::complex<double>(0.0, 0.0);
139  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
140  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(inArray.numCols()));
141  }
142 
143  return returnArray;
144  }
145  case Axis::ROW:
146  {
147  NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
148  NdArray<std::complex<double>> returnArray(1, transposedArray.numRows());
149  for (uint32 row = 0; row < transposedArray.numRows(); ++row)
150  {
151  squareSum = std::complex<double>(0.0, 0.0);
152  std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
153  returnArray(0, row) = std::sqrt(squareSum / static_cast<double>(transposedArray.numCols()));
154  }
155 
156  return returnArray;
157  }
158  default:
159  {
160  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
161  return {}; // get rid of compiler warning
162  }
163  }
164  }
165 } // namespace nc
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::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4608
nc::NdArray< double >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:214
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::NdArray::numCols
uint32 numCols() const noexcept
Definition: NdArrayCore.hpp:3431
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:4326
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
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
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:3444
nc::Axis::COL
@ COL