NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
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();
81 NdArray<double> meanValue = nanmean(inArray, inAxis);
82 NdArray<double> returnArray(1, inShape.rows);
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
self_type transpose() const
Definition: NdArrayCore.hpp:4882
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
value_type item() const
Definition: NdArrayCore.hpp:3022
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
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
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:49
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmean.hpp:54
std::uint32_t uint32
Definition: Types.hpp:40