NumCpp  1.0
A C++ implementation of the Python Numpy library
nanmean.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/Functions/max.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 sum = static_cast<double>(std::accumulate(inArray.cbegin(), inArray.cend(), 0.0,
65  [](dtype inValue1, dtype inValue2) -> dtype
66  {
67  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
68  }));
69 
70  const double numberNonNan = static_cast<double>(std::accumulate(inArray.cbegin(), inArray.cend(), 0.0,
71  [](dtype inValue1, dtype inValue2) -> dtype
72  {
73  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
74  }));
75 
76  NdArray<double> returnArray = { sum /= numberNonNan };
77 
78  return returnArray;
79  }
80  case Axis::COL:
81  {
82  const Shape inShape = inArray.shape();
83  NdArray<double> returnArray(1, inShape.rows);
84  for (uint32 row = 0; row < inShape.rows; ++row)
85  {
86  double sum = static_cast<double>(std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.0,
87  [](dtype inValue1, dtype inValue2) -> dtype
88  {
89  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
90  }));
91 
92  double numberNonNan = static_cast<double>(std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.0,
93  [](dtype inValue1, dtype inValue2) -> dtype
94  {
95  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
96  }));
97 
98  returnArray(0, row) = sum / numberNonNan;
99  }
100 
101  return returnArray;
102  }
103  case Axis::ROW:
104  {
105  NdArray<dtype> transposedArray = inArray.transpose();
106  const Shape transShape = transposedArray.shape();
107  NdArray<double> returnArray(1, transShape.rows);
108  for (uint32 row = 0; row < transShape.rows; ++row)
109  {
110  double sum = static_cast<double>(std::accumulate(transposedArray.cbegin(row), transposedArray.cend(row), 0.0,
111  [](dtype inValue1, dtype inValue2) -> dtype
112  {
113  return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2;
114  }));
115 
116  double numberNonNan = static_cast<double>(std::accumulate(transposedArray.cbegin(row), transposedArray.cend(row), 0.0,
117  [](dtype inValue1, dtype inValue2) -> dtype
118  {
119  return std::isnan(inValue2) ? inValue1 : inValue1 + 1;
120  }));
121 
122  returnArray(0, row) = sum / numberNonNan;
123  }
124 
125  return returnArray;
126  }
127  default:
128  {
129  // this isn't actually possible, just putting this here to get rid
130  // of the compiler warning.
131  return NdArray<double>(0);
132  }
133  }
134  }
135 }
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4591
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::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::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
Shape.hpp
nc
Definition: Coordinate.hpp:45
max.hpp
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
DtypeInfo.hpp
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
Types.hpp
nc::Axis::COL
@ COL