NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rms.hpp
Go to the documentation of this file.
1
28#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
39namespace nc
40{
41 //============================================================================
42 // Method Description:
50 template<typename dtype>
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 {
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>
102 {
104
105 std::complex<double> squareSum = 0.;
106 const auto function = [&squareSum](std::complex<dtype> value) -> void
108
109 switch (inAxis)
110 {
111 case Axis::NONE:
112 {
113 std::for_each(inArray.cbegin(), inArray.cend(), function);
115 static_cast<double>(inArray.size())) };
116 return returnArray;
117 }
118 case Axis::COL:
119 {
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
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
constexpr dtype sqr(dtype inValue) noexcept
Definition sqr.hpp:42
Definition Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition Enums.hpp:36
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition rms.hpp:51
std::uint32_t uint32
Definition Types.hpp:40