NumCpp  2.13.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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 {
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 {
76 for (uint32 row = 0; row < inArray.numRows(); ++row)
77 {
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>
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
119
120 switch (inAxis)
121 {
122 case Axis::NONE:
123 {
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 {
134 for (uint32 row = 0; row < inArray.numRows(); ++row)
135 {
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 {
150 for (uint32 row = 0; row < transposedArray.numRows(); ++row)
151 {
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
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
self_type transpose() const
Definition NdArrayCore.hpp:4882
value_type item() const
Definition NdArrayCore.hpp:3022
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
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:46
std::uint32_t uint32
Definition Types.hpp:40