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