NumCpp  2.7.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 
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:
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(), inArray.cend(), 0.0,
63  [](dtype inValue1, dtype inValue2) -> dtype
64  {
65  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
66  }));
67 
68  const auto numberNonNan = static_cast<double>(std::accumulate(inArray.cbegin(), inArray.cend(), 0.0,
69  [](dtype inValue1, dtype inValue2) -> dtype
70  {
71  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
72  }));
73 
74  NdArray<double> returnArray = { sum /= numberNonNan };
75 
76  return returnArray;
77  }
78  case Axis::COL:
79  {
80  const Shape inShape = inArray.shape();
81  NdArray<double> returnArray(1, inShape.rows);
82  for (uint32 row = 0; row < inShape.rows; ++row)
83  {
84  auto sum = static_cast<double>(std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.0,
85  [](dtype inValue1, dtype inValue2) -> dtype
86  {
87  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
88  }));
89 
90  auto numberNonNan = static_cast<double>(std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.0,
91  [](dtype inValue1, dtype inValue2) -> dtype
92  {
93  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
94  }));
95 
96  returnArray(0, row) = sum / numberNonNan;
97  }
98 
99  return returnArray;
100  }
101  case Axis::ROW:
102  {
103  NdArray<dtype> transposedArray = inArray.transpose();
104  const Shape transShape = transposedArray.shape();
105  NdArray<double> returnArray(1, transShape.rows);
106  for (uint32 row = 0; row < transShape.rows; ++row)
107  {
108  auto sum = static_cast<double>(std::accumulate(transposedArray.cbegin(row), transposedArray.cend(row), 0.0,
109  [](dtype inValue1, dtype inValue2) -> dtype
110  {
111  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
112  }));
113 
114  auto numberNonNan = static_cast<double>(std::accumulate(transposedArray.cbegin(row), transposedArray.cend(row), 0.0,
115  [](dtype inValue1, dtype inValue2) -> dtype
116  {
117  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
118  }));
119 
120  returnArray(0, row) = sum / numberNonNan;
121  }
122 
123  return returnArray;
124  }
125  default:
126  {
127  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
128  return {}; // get rid of compiler warning
129  }
130  }
131  }
132 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1216
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
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:46
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmean.hpp:54
std::uint32_t uint32
Definition: Types.hpp:40