NumCpp  2.4.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:
54  template<typename dtype>
56  {
57  STATIC_ASSERT_FLOAT(dtype);
58 
59  switch (inAxis)
60  {
61  case Axis::NONE:
62  {
63  double meanValue = nanmean(inArray, inAxis).item();
64  double sum = 0;
65  double counter = 0;
66  for (auto value : inArray)
67  {
68  if (std::isnan(value))
69  {
70  continue;
71  }
72 
73  sum += utils::sqr(static_cast<double>(value) - meanValue);
74  ++counter;
75  }
76  NdArray<double> returnArray = { std::sqrt(sum / counter) };
77  return returnArray;
78  }
79  case Axis::COL:
80  {
81  const Shape inShape = inArray.shape();
82  NdArray<double> meanValue = nanmean(inArray, inAxis);
83  NdArray<double> returnArray(1, inShape.rows);
84  for (uint32 row = 0; row < inShape.rows; ++row)
85  {
86  double sum = 0;
87  double counter = 0;
88  for (uint32 col = 0; col < inShape.cols; ++col)
89  {
90  if (std::isnan(inArray(row, col)))
91  {
92  continue;
93  }
94 
95  sum += utils::sqr(static_cast<double>(inArray(row, col)) - meanValue[row]);
96  ++counter;
97  }
98  returnArray(0, row) = std::sqrt(sum / counter);
99  }
100 
101  return returnArray;
102  }
103  case Axis::ROW:
104  {
105  NdArray<double> meanValue = nanmean(inArray, inAxis);
106  NdArray<dtype> transposedArray = inArray.transpose();
107  const Shape inShape = transposedArray.shape();
108  NdArray<double> returnArray(1, inShape.rows);
109  for (uint32 row = 0; row < inShape.rows; ++row)
110  {
111  double sum = 0;
112  double counter = 0;
113  for (uint32 col = 0; col < inShape.cols; ++col)
114  {
115  if (std::isnan(transposedArray(row, col)))
116  {
117  continue;
118  }
119 
120  sum += utils::sqr(static_cast<double>(transposedArray(row, col)) - meanValue[row]);
121  ++counter;
122  }
123  returnArray(0, row) = std::sqrt(sum / counter);
124  }
125 
126  return returnArray;
127  }
128  default:
129  {
130  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
131  return {}; // get rid of compiler warning
132  }
133  }
134  }
135 
136 } // namespace nc
StaticAsserts.hpp
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2975
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
nc::sqrt
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:50
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
nc::nanstdev
NdArray< double > nanstdev(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanstdev.hpp:55
nc::NdArray< double >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
STATIC_ASSERT_FLOAT
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:45
nc::nanmean
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmean.hpp:55
nc::sum
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:47
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:46
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
sqr.hpp
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::utils::sqr
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Types.hpp
nc::Axis::COL
@ COL
nc::isnan
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:51
nanmean.hpp