NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanpercentile.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <string>
33 #include <vector>
34 
38 #include "NumCpp/Core/Shape.hpp"
39 #include "NumCpp/Core/Types.hpp"
44 #include "NumCpp/NdArray.hpp"
46 
47 namespace nc
48 {
49  //============================================================================
50  // Method Description:
61  template<typename dtype>
63  double inPercentile,
64  Axis inAxis = Axis::NONE,
65  const std::string& inInterpMethod = "linear")
66  {
67  STATIC_ASSERT_FLOAT(dtype);
68 
69  switch (inAxis)
70  {
71  case Axis::NONE:
72  {
73  std::vector<double> arrayCopy;
74  arrayCopy.reserve(inArray.size());
75  for (auto value : inArray)
76  {
77  if (!isnan(value))
78  {
79  arrayCopy.push_back(static_cast<double>(value));
80  }
81  }
82 
83  if (arrayCopy.empty())
84  {
85  NdArray<double> returnArray = { constants::nan };
86  return returnArray;
87  }
88 
89  return percentile(NdArray<double>(arrayCopy.data(),
90  static_cast<typename NdArray<dtype>::size_type>(arrayCopy.size()),
91  false),
92  inPercentile,
93  Axis::NONE,
94  inInterpMethod);
95  }
96  case Axis::COL:
97  {
98  const Shape inShape = inArray.shape();
99 
100  NdArray<double> returnArray(1, inShape.rows);
101  for (uint32 row = 0; row < inShape.rows; ++row)
102  {
103  NdArray<double> outValue = nanpercentile(NdArray<dtype>(&inArray.front(row), inShape.cols),
104  inPercentile,
105  Axis::NONE,
106  inInterpMethod);
107 
108  if (outValue.size() == 1)
109  {
110  returnArray[row] = outValue.item();
111  }
112  else
113  {
114  returnArray[row] = constants::nan;
115  }
116  }
117 
118  return returnArray;
119  }
120  case Axis::ROW:
121  {
122  return nanpercentile(inArray.transpose(), inPercentile, Axis::COL, inInterpMethod);
123  }
124  default:
125  {
126  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
127  return {}; // get rid of compiler warning
128  }
129  }
130 
131  return {}; // get rid of compiler warning
132  }
133 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:45
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4457
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2661
value_type item() const
Definition: NdArrayCore.hpp:2809
uint32 size_type
Definition: NdArrayCore.hpp:88
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
const double nan
NaN.
Definition: Constants.hpp:44
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:47
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:49
NdArray< double > nanpercentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: nanpercentile.hpp:62
NdArray< double > percentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: percentile.hpp:65
std::uint32_t uint32
Definition: Types.hpp:40