NumCpp  2.5.1
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:
55  template<typename dtype>
57  {
58  STATIC_ASSERT_FLOAT(dtype);
59 
60  switch (inAxis)
61  {
62  case Axis::NONE:
63  {
64  std::vector<dtype> values;
65  for (auto value : inArray)
66  {
67  if (!std::isnan(value))
68  {
69  values.push_back(value);
70  }
71  }
72 
73  const uint32 middle = static_cast<uint32>(values.size()) / 2;
74  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
75  NdArray<dtype> returnArray = { values[middle] };
76 
77  return returnArray;
78  }
79  case Axis::COL:
80  {
81  const Shape inShape = inArray.shape();
82  NdArray<dtype> returnArray(1, inShape.rows);
83  for (uint32 row = 0; row < inShape.rows; ++row)
84  {
85  std::vector<dtype> values;
86  for (uint32 col = 0; col < inShape.cols; ++col)
87  {
88  if (!std::isnan(inArray(row, col)))
89  {
90  values.push_back(inArray(row, col));
91  }
92  }
93 
94  const uint32 middle = static_cast<uint32>(values.size()) / 2;
95  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
96  returnArray(0, row) = values[middle];
97  }
98 
99  return returnArray;
100  }
101  case Axis::ROW:
102  {
103  NdArray<dtype> transposedArray = inArray.transpose();
104  const Shape inShape = transposedArray.shape();
105  NdArray<dtype> returnArray(1, inShape.rows);
106  for (uint32 row = 0; row < inShape.rows; ++row)
107  {
108  std::vector<dtype> values;
109  for (uint32 col = 0; col < inShape.cols; ++col)
110  {
111  if (!std::isnan(transposedArray(row, col)))
112  {
113  values.push_back(transposedArray(row, col));
114  }
115  }
116 
117  const uint32 middle = static_cast<uint32>(values.size()) / 2;
118  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
119  returnArray(0, row) = values[middle];
120  }
121 
122  return returnArray;
123  }
124  default:
125  {
126  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
127  return {}; // get rid of compiler warning
128  }
129  }
130  }
131 } // 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:4483
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4830
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:56
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:51
std::uint32_t uint32
Definition: Types.hpp:40