NumCpp  1.0
A C++ implementation of the Python Numpy library
nanstdev.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/Utils/sqr.hpp"
37 
38 #include <algorithm>
39 #include <cmath>
40 
41 namespace nc
42 {
43  //============================================================================
44  // Method Description:
55  template<typename dtype>
57  {
58  STATIC_ASSERT_FLOAT(dtype);
59 
60  switch (inAxis)
61  {
62  case Axis::NONE:
63  {
64  double meanValue = nanmean(inArray, inAxis).item();
65  double sum = 0;
66  double counter = 0;
67  for (auto value : inArray)
68  {
69  if (std::isnan(value))
70  {
71  continue;
72  }
73 
74  sum += utils::sqr(static_cast<double>(value) - meanValue);
75  ++counter;
76  }
77  NdArray<double> returnArray = { std::sqrt(sum / counter) };
78  return returnArray;
79  }
80  case Axis::COL:
81  {
82  const Shape inShape = inArray.shape();
83  NdArray<double> meanValue = nanmean(inArray, inAxis);
84  NdArray<double> returnArray(1, inShape.rows);
85  for (uint32 row = 0; row < inShape.rows; ++row)
86  {
87  double sum = 0;
88  double counter = 0;
89  for (uint32 col = 0; col < inShape.cols; ++col)
90  {
91  if (std::isnan(inArray(row, col)))
92  {
93  continue;
94  }
95 
96  sum += utils::sqr(static_cast<double>(inArray(row, col)) - meanValue[row]);
97  ++counter;
98  }
99  returnArray(0, row) = std::sqrt(sum / counter);
100  }
101 
102  return returnArray;
103  }
104  case Axis::ROW:
105  {
106  NdArray<double> meanValue = nanmean(inArray, inAxis);
107  NdArray<dtype> transposedArray = inArray.transpose();
108  const Shape inShape = transposedArray.shape();
109  NdArray<double> returnArray(1, inShape.rows);
110  for (uint32 row = 0; row < inShape.rows; ++row)
111  {
112  double sum = 0;
113  double counter = 0;
114  for (uint32 col = 0; col < inShape.cols; ++col)
115  {
116  if (std::isnan(transposedArray(row, col)))
117  {
118  continue;
119  }
120 
121  sum += utils::sqr(static_cast<double>(transposedArray(row, col)) - meanValue[row]);
122  ++counter;
123  }
124  returnArray(0, row) = std::sqrt(sum / counter);
125  }
126 
127  return returnArray;
128  }
129  default:
130  {
131  // this isn't actually possible, just putting this here to get rid
132  // of the compiler warning.
133  return NdArray<double>(0);
134  }
135  }
136  }
137 
138 }
StaticAsserts.hpp
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2950
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
nc::sqrt
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:51
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4591
nc::nanstdev
NdArray< double > nanstdev(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanstdev.hpp:56
nc::NdArray< double >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
STATIC_ASSERT_FLOAT
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:44
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::nanmean
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmean.hpp:56
nc::sum
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:48
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
sqr.hpp
nc::utils::sqr
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:45
Types.hpp
nc::Axis::COL
@ COL
nc::isnan
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:52
nanmean.hpp