NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
nanstdev.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31
33#include "NumCpp/Core/Shape.hpp"
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:
52 template<typename dtype>
54 {
56
57 switch (inAxis)
58 {
59 case Axis::NONE:
60 {
61 double meanValue = nanmean(inArray, inAxis).item();
62 double sum = 0;
63 double counter = 0;
64 for (auto value : inArray)
65 {
66 if (std::isnan(value))
67 {
68 continue;
69 }
70
71 sum += utils::sqr(static_cast<double>(value) - meanValue);
72 ++counter;
73 }
74 NdArray<double> returnArray = { std::sqrt(sum / counter) };
75 return returnArray;
76 }
77 case Axis::COL:
78 {
79 const Shape inShape = inArray.shape();
80 NdArray<double> meanValue = nanmean(inArray, inAxis);
81 NdArray<double> returnArray(1, inShape.rows);
82 for (uint32 row = 0; row < inShape.rows; ++row)
83 {
84 double sum = 0;
85 double counter = 0;
86 for (uint32 col = 0; col < inShape.cols; ++col)
87 {
88 if (std::isnan(inArray(row, col)))
89 {
90 continue;
91 }
92
93 sum += utils::sqr(static_cast<double>(inArray(row, col)) - meanValue[row]);
94 ++counter;
95 }
96 returnArray(0, row) = std::sqrt(sum / counter);
97 }
98
99 return returnArray;
100 }
101 case Axis::ROW:
102 {
103 return nanstdev(inArray.transpose(), Axis::COL);
104 }
105 default:
106 {
107 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
108 return {}; // get rid of compiler warning
109 }
110 }
111 }
112
113} // 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:135
self_type transpose() const
Definition NdArrayCore.hpp:4959
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
value_type item() const
Definition NdArrayCore.hpp:3098
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:39
NdArray< double > nanstdev(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition nanstdev.hpp:53
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:45
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition nanmean.hpp:51
std::uint32_t uint32
Definition Types.hpp:40