NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanstdev.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 #include "NumCpp/Utils/sqr.hpp"
36 
37 #include <algorithm>
38 #include <cmath>
39 
40 namespace nc
41 {
42  //============================================================================
43  // Method Description:
53  template<typename dtype>
55  {
56  STATIC_ASSERT_FLOAT(dtype);
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  NdArray<double> meanValue = nanmean(inArray, inAxis);
105  NdArray<dtype> transposedArray = inArray.transpose();
106  const Shape inShape = transposedArray.shape();
107  NdArray<double> returnArray(1, inShape.rows);
108  for (uint32 row = 0; row < inShape.rows; ++row)
109  {
110  double sum = 0;
111  double counter = 0;
112  for (uint32 col = 0; col < inShape.cols; ++col)
113  {
114  if (std::isnan(transposedArray(row, col)))
115  {
116  continue;
117  }
118 
119  sum += utils::sqr(static_cast<double>(transposedArray(row, col)) - meanValue[row]);
120  ++counter;
121  }
122  returnArray(0, row) = std::sqrt(sum / counter);
123  }
124 
125  return returnArray;
126  }
127  default:
128  {
129  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
130  return {}; // get rid of compiler warning
131  }
132  }
133  }
134 
135 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
value_type item() const
Definition: NdArrayCore.hpp:2931
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:44
Definition: Coordinate.hpp:45
NdArray< double > nanstdev(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanstdev.hpp:54
Axis
Enum To describe an axis.
Definition: Types.hpp:46
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