NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanmean.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/Functions/max.hpp"
35 #include "NumCpp/NdArray.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  auto sum = static_cast<double>(std::accumulate(inArray.cbegin(), inArray.cend(), 0.0,
64  [](dtype inValue1, dtype inValue2) -> dtype
65  {
66  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
67  }));
68 
69  const auto numberNonNan = static_cast<double>(std::accumulate(inArray.cbegin(), inArray.cend(), 0.0,
70  [](dtype inValue1, dtype inValue2) -> dtype
71  {
72  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
73  }));
74 
75  NdArray<double> returnArray = { sum /= numberNonNan };
76 
77  return returnArray;
78  }
79  case Axis::COL:
80  {
81  const Shape inShape = inArray.shape();
82  NdArray<double> returnArray(1, inShape.rows);
83  for (uint32 row = 0; row < inShape.rows; ++row)
84  {
85  auto sum = static_cast<double>(std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.0,
86  [](dtype inValue1, dtype inValue2) -> dtype
87  {
88  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
89  }));
90 
91  auto numberNonNan = static_cast<double>(std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.0,
92  [](dtype inValue1, dtype inValue2) -> dtype
93  {
94  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
95  }));
96 
97  returnArray(0, row) = sum / numberNonNan;
98  }
99 
100  return returnArray;
101  }
102  case Axis::ROW:
103  {
104  NdArray<dtype> transposedArray = inArray.transpose();
105  const Shape transShape = transposedArray.shape();
106  NdArray<double> returnArray(1, transShape.rows);
107  for (uint32 row = 0; row < transShape.rows; ++row)
108  {
109  auto sum = static_cast<double>(std::accumulate(transposedArray.cbegin(row), transposedArray.cend(row), 0.0,
110  [](dtype inValue1, dtype inValue2) -> dtype
111  {
112  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
113  }));
114 
115  auto numberNonNan = static_cast<double>(std::accumulate(transposedArray.cbegin(row), transposedArray.cend(row), 0.0,
116  [](dtype inValue1, dtype inValue2) -> dtype
117  {
118  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
119  }));
120 
121  returnArray(0, row) = sum / numberNonNan;
122  }
123 
124  return returnArray;
125  }
126  default:
127  {
128  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
129  return {}; // get rid of compiler warning
130  }
131  }
132  }
133 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4841
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:47
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmean.hpp:55
std::uint32_t uint32
Definition: Types.hpp:40