NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
stdev.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <cmath>
32#include <complex>
33
34#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37#include "NumCpp/Utils/sqr.hpp"
38
39namespace nc
40{
41 //============================================================================
42 // Method Description:
51 template<typename dtype>
53 {
55
56 double meanValue = 0.;
57 double sum = 0.;
58
59 const auto function = [&sum, &meanValue](dtype value) -> void
60 { sum += utils::sqr(static_cast<double>(value) - meanValue); };
61
62 switch (inAxis)
63 {
64 case Axis::NONE:
65 {
66 meanValue = mean(inArray, inAxis).item();
67 std::for_each(inArray.cbegin(), inArray.cend(), function);
68
69 NdArray<double> returnArray = { std::sqrt(sum / inArray.size()) };
70 return returnArray;
71 }
72 case Axis::COL:
73 {
74 NdArray<double> meanValueArray = mean(inArray, inAxis);
75 NdArray<double> returnArray(1, inArray.numRows());
76 for (uint32 row = 0; row < inArray.numRows(); ++row)
77 {
78 meanValue = meanValueArray[row];
79 sum = 0.;
80 std::for_each(inArray.cbegin(row), inArray.cend(row), function);
81
82 returnArray(0, row) = std::sqrt(sum / inArray.numCols());
83 }
84
85 return returnArray;
86 }
87 case Axis::ROW:
88 {
89 return stdev(inArray.transpose(), Axis::COL);
90 }
91 default:
92 {
93 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
94 return {}; // get rid of compiler warning
95 }
96 }
97 }
98
99 //============================================================================
100 // Method Description:
109 template<typename dtype>
110 NdArray<std::complex<double>> stdev(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
111 {
113
114 std::complex<double> meanValue(0., 0.);
115 std::complex<double> sum(0., 0.);
116
117 const auto function = [&sum, &meanValue](std::complex<dtype> value) -> void
118 { sum += utils::sqr(complex_cast<double>(value) - meanValue); };
119
120 switch (inAxis)
121 {
122 case Axis::NONE:
123 {
124 meanValue = mean(inArray, inAxis).item();
125 std::for_each(inArray.cbegin(), inArray.cend(), function);
126
127 NdArray<std::complex<double>> returnArray = { std::sqrt(sum / static_cast<double>(inArray.size())) };
128 return returnArray;
129 }
130 case Axis::COL:
131 {
132 NdArray<std::complex<double>> meanValueArray = mean(inArray, inAxis);
133 NdArray<std::complex<double>> returnArray(1, inArray.numRows());
134 for (uint32 row = 0; row < inArray.numRows(); ++row)
135 {
136 meanValue = meanValueArray[row];
137 sum = std::complex<double>(0., 0.);
138 std::for_each(inArray.cbegin(row), inArray.cend(row), function);
139
140 returnArray(0, row) = std::sqrt(sum / static_cast<double>(inArray.numCols()));
141 }
142
143 return returnArray;
144 }
145 case Axis::ROW:
146 {
147 NdArray<std::complex<double>> meanValueArray = mean(inArray, inAxis);
148 NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
149 NdArray<std::complex<double>> returnArray(1, transposedArray.numRows());
150 for (uint32 row = 0; row < transposedArray.numRows(); ++row)
151 {
152 meanValue = meanValueArray[row];
153 sum = std::complex<double>(0., 0.);
154 std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
155
156 returnArray(0, row) = std::sqrt(sum / static_cast<double>(transposedArray.numCols()));
157 }
158
159 return returnArray;
160 }
161 default:
162 {
163 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
164 return {}; // get rid of compiler warning
165 }
166 }
167 }
168} // 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:4524
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
self_type transpose() const
Definition: NdArrayCore.hpp:4882
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3465
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3477
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
value_type item() const
Definition: NdArrayCore.hpp:3022
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 > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:52
NdArray< double > stdev(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: stdev.hpp:52
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
std::uint32_t uint32
Definition: Types.hpp:40