NumCpp  2.1.0
A C++ implementation of the Python Numpy library
nanmedian.hpp
Go to the documentation of this file.
1 #pragma once
30 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/Functions/max.hpp"
37 #include "NumCpp/NdArray.hpp"
38 
39 #include <cmath>
40 #include <vector>
41 
42 namespace nc
43 {
44  //============================================================================
45  // Method Description:
56  template<typename dtype>
58  {
59  STATIC_ASSERT_FLOAT(dtype);
60 
61  switch (inAxis)
62  {
63  case Axis::NONE:
64  {
65  std::vector<dtype> values;
66  for (auto value : inArray)
67  {
68  if (!std::isnan(value))
69  {
70  values.push_back(value);
71  }
72  }
73 
74  const uint32 middle = static_cast<uint32>(values.size()) / 2;
75  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
76  NdArray<dtype> returnArray = { values[middle] };
77 
78  return returnArray;
79  }
80  case Axis::COL:
81  {
82  const Shape inShape = inArray.shape();
83  NdArray<dtype> returnArray(1, inShape.rows);
84  for (uint32 row = 0; row < inShape.rows; ++row)
85  {
86  std::vector<dtype> values;
87  for (uint32 col = 0; col < inShape.cols; ++col)
88  {
89  if (!std::isnan(inArray(row, col)))
90  {
91  values.push_back(inArray(row, col));
92  }
93  }
94 
95  const uint32 middle = static_cast<uint32>(values.size()) / 2;
96  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
97  returnArray(0, row) = values[middle];
98  }
99 
100  return returnArray;
101  }
102  case Axis::ROW:
103  {
104  NdArray<dtype> transposedArray = inArray.transpose();
105  const Shape inShape = transposedArray.shape();
106  NdArray<dtype> returnArray(1, inShape.rows);
107  for (uint32 row = 0; row < inShape.rows; ++row)
108  {
109  std::vector<dtype> values;
110  for (uint32 col = 0; col < inShape.cols; ++col)
111  {
112  if (!std::isnan(transposedArray(row, col)))
113  {
114  values.push_back(transposedArray(row, col));
115  }
116  }
117 
118  const uint32 middle = static_cast<uint32>(values.size()) / 2;
119  stl_algorithms::nth_element(values.begin(), values.begin() + middle, values.end());
120  returnArray(0, row) = values[middle];
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
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4608
nc::NdArray< dtype >
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::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
nc::stl_algorithms::nth_element
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:398
Shape.hpp
nc
Definition: Coordinate.hpp:45
max.hpp
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
DtypeInfo.hpp
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
StlAlgorithms.hpp
Types.hpp
nc::Axis::COL
@ COL
nc::isnan
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:52
nc::nanmedian
NdArray< dtype > nanmedian(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: nanmedian.hpp:57