NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
Functions/interp.hpp
Go to the documentation of this file.
1
28#pragma once
29
31#include "NumCpp/NdArray.hpp"
33
34namespace nc
35{
36 //============================================================================
45 template<typename dtype>
46 constexpr double interp(dtype inValue1, dtype inValue2, double inPercent) noexcept
47 {
48 return utils::interp(inValue1, inValue2, inPercent);
49 }
50
51 //============================================================================
52 // Method Description:
68 template<typename dtype>
70 {
71 // do some error checking first
72 if (inXp.size() != inFp.size())
73 {
74 THROW_INVALID_ARGUMENT_ERROR("inXp and inFp need to be the same size().");
75 }
76
77 if (inX.min().item() < inXp.min().item() || inX.max().item() > inXp.max().item())
78 {
79 THROW_INVALID_ARGUMENT_ERROR("endpoints of inX should be contained within inXp.");
80 }
81
82 // sort the input inXp and inFp data
83 NdArray<uint32> sortedXpIdxs = argsort(inXp);
84 NdArray<dtype> sortedXp(1, inFp.size());
85 NdArray<dtype> sortedFp(1, inFp.size());
86 uint32 counter = 0;
87 for (auto sortedXpIdx : sortedXpIdxs)
88 {
89 sortedXp[counter] = inXp[sortedXpIdx];
90 sortedFp[counter++] = inFp[sortedXpIdx];
91 }
92
93 // get the sorted input inX array indices
94 NdArray<uint32> sortedXIdxs = argsort(inX);
95
96 NdArray<dtype> returnArray(1, inX.size());
97
98 uint32 currXpIdx = 0;
99 uint32 currXidx = 0;
100 while (currXidx < inX.size())
101 {
102 const auto sortedXIdx = sortedXIdxs[currXidx];
103 const auto x = inX[sortedXIdx];
104 const auto xPLow = sortedXp[currXpIdx];
105 const auto xPHigh = sortedXp[currXpIdx + 1];
106 const auto fPLow = sortedFp[currXpIdx];
107 const auto fPHigh = sortedFp[currXpIdx + 1];
108
109 if (xPLow <= x && x <= xPHigh)
110 {
111 const double percent = static_cast<double>(x - xPLow) / static_cast<double>(xPHigh - xPLow);
112 returnArray[sortedXIdx] = utils::interp(fPLow, fPHigh, percent);
113 ++currXidx;
114 }
115 else
116 {
117 ++currXpIdx;
118 }
119 }
120
121 return returnArray;
122 }
123} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
self_type max(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3117
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
value_type item() const
Definition NdArrayCore.hpp:3098
self_type min(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3161
constexpr double interp(double inValue1, double inValue2, double inPercent) noexcept
Definition Utils/interp.hpp:41
Definition Cartesian.hpp:39
NdArray< uint32 > argsort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition argsort.hpp:46
constexpr double interp(dtype inValue1, dtype inValue2, double inPercent) noexcept
Definition Functions/interp.hpp:46
std::uint32_t uint32
Definition Types.hpp:40