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
nanstdev.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <cmath>
32
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/Core/Types.hpp"
37#include "NumCpp/NdArray.hpp"
38#include "NumCpp/Utils/sqr.hpp"
39
40namespace nc
41{
42 //============================================================================
43 // Method Description:
53 template<typename dtype>
55 {
57
58 switch (inAxis)
59 {
60 case Axis::NONE:
61 {
62 double meanValue = nanmean(inArray, inAxis).item();
63 double sum = 0;
64 double counter = 0;
65 for (auto value : inArray)
66 {
67 if (std::isnan(value))
68 {
69 continue;
70 }
71
72 sum += utils::sqr(static_cast<double>(value) - meanValue);
73 ++counter;
74 }
75 NdArray<double> returnArray = { std::sqrt(sum / counter) };
76 return returnArray;
77 }
78 case Axis::COL:
79 {
80 const Shape inShape = inArray.shape();
83 for (uint32 row = 0; row < inShape.rows; ++row)
84 {
85 double sum = 0;
86 double counter = 0;
87 for (uint32 col = 0; col < inShape.cols; ++col)
88 {
89 if (std::isnan(inArray(row, col)))
90 {
91 continue;
92 }
93
94 sum += utils::sqr(static_cast<double>(inArray(row, col)) - meanValue[row]);
95 ++counter;
96 }
97 returnArray(0, row) = std::sqrt(sum / counter);
98 }
99
100 return returnArray;
101 }
102 case Axis::ROW:
103 {
104 return nanstdev(inArray.transpose(), Axis::COL);
105 }
106 default:
107 {
108 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
109 return {}; // get rid of compiler warning
110 }
111 }
112 }
113
114} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_FLOAT(dtype)
Definition StaticAsserts.hpp:50
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
value_type item() const
Definition NdArrayCore.hpp:3098
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
constexpr dtype sqr(dtype inValue) noexcept
Definition sqr.hpp:42
Definition Cartesian.hpp:40
NdArray< double > nanstdev(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition nanstdev.hpp:54
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
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition nanmean.hpp:54
std::uint32_t uint32
Definition Types.hpp:40