NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanmedian.hpp
Go to the documentation of this file.
1 #pragma once
29 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/Functions/max.hpp"
36 #include "NumCpp/NdArray.hpp"
37 
38 #include <cmath>
39 #include <vector>
40 
41 namespace nc
42 {
43  //============================================================================
44  // Method Description:
54  template<typename dtype>
56  {
57  STATIC_ASSERT_FLOAT(dtype);
58 
59  switch (inAxis)
60  {
61  case Axis::NONE:
62  {
63  std::vector<dtype> values;
64  for (auto value : inArray)
65  {
66  if (!std::isnan(value))
67  {
68  values.push_back(value);
69  }
70  }
71 
72  const uint32 middle = static_cast<uint32>(values.size()) / 2;
73  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
74  NdArray<dtype> returnArray = { values[middle] };
75 
76  return returnArray;
77  }
78  case Axis::COL:
79  {
80  const Shape inShape = inArray.shape();
81  NdArray<dtype> returnArray(1, inShape.rows);
82  for (uint32 row = 0; row < inShape.rows; ++row)
83  {
84  std::vector<dtype> values;
85  for (uint32 col = 0; col < inShape.cols; ++col)
86  {
87  if (!std::isnan(inArray(row, col)))
88  {
89  values.push_back(inArray(row, col));
90  }
91  }
92 
93  const uint32 middle = static_cast<uint32>(values.size()) / 2;
94  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
95  returnArray(0, row) = values[middle];
96  }
97 
98  return returnArray;
99  }
100  case Axis::ROW:
101  {
102  NdArray<dtype> transposedArray = inArray.transpose();
103  const Shape inShape = transposedArray.shape();
104  NdArray<dtype> returnArray(1, inShape.rows);
105  for (uint32 row = 0; row < inShape.rows; ++row)
106  {
107  std::vector<dtype> values;
108  for (uint32 col = 0; col < inShape.cols; ++col)
109  {
110  if (!std::isnan(transposedArray(row, col)))
111  {
112  values.push_back(transposedArray(row, col));
113  }
114  }
115 
116  const uint32 middle = static_cast<uint32>(values.size()) / 2;
117  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
118  returnArray(0, row) = values[middle];
119  }
120 
121  return returnArray;
122  }
123  default:
124  {
125  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
126  return {}; // get rid of compiler warning
127  }
128  }
129  }
130 } // 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
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:397
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
NdArray< dtype > nanmedian(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmedian.hpp:55
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:49
std::uint32_t uint32
Definition: Types.hpp:40