NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanmean.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 #include <cmath>
32 
35 #include "NumCpp/Core/Shape.hpp"
36 #include "NumCpp/Core/Types.hpp"
37 #include "NumCpp/Functions/max.hpp"
38 #include "NumCpp/NdArray.hpp"
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  auto sum = static_cast<double>(std::accumulate(inArray.cbegin(),
63  inArray.cend(),
64  0.,
65  [](dtype inValue1, dtype inValue2) -> dtype {
66  return std::isnan(inValue2) ? inValue1
67  : inValue1 + inValue2;
68  }));
69 
70  const auto numberNonNan =
71  static_cast<double>(std::accumulate(inArray.cbegin(),
72  inArray.cend(),
73  0.,
74  [](dtype inValue1, dtype inValue2) -> dtype
75  { return std::isnan(inValue2) ? inValue1 : inValue1 + 1; }));
76 
77  NdArray<double> returnArray = { sum /= numberNonNan };
78 
79  return returnArray;
80  }
81  case Axis::COL:
82  {
83  const Shape inShape = inArray.shape();
84  NdArray<double> returnArray(1, inShape.rows);
85  for (uint32 row = 0; row < inShape.rows; ++row)
86  {
87  auto sum = static_cast<double>(
88  std::accumulate(inArray.cbegin(row),
89  inArray.cend(row),
90  0.,
91  [](dtype inValue1, dtype inValue2) -> dtype
92  { return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2; }));
93 
94  auto numberNonNan =
95  static_cast<double>(std::accumulate(inArray.cbegin(row),
96  inArray.cend(row),
97  0.,
98  [](dtype inValue1, dtype inValue2) -> dtype {
99  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
100  }));
101 
102  returnArray(0, row) = sum / numberNonNan;
103  }
104 
105  return returnArray;
106  }
107  case Axis::ROW:
108  {
109  return nanmean(inArray.transpose(), Axis::COL);
110  }
111  default:
112  {
113  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
114  return {}; // get rid of compiler warning
115  }
116  }
117  }
118 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:50
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1318
self_type transpose() const
Definition: NdArrayCore.hpp:4837
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1626
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4464
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
Definition: Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition: Types.hpp:47
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmean.hpp:54
std::uint32_t uint32
Definition: Types.hpp:40