NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Functions/interp.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Utils/interp.hpp"
33 
34 #include <string>
35 
36 namespace nc
37 {
38  //============================================================================
47  template<typename dtype>
48  constexpr double interp(dtype inValue1, dtype inValue2, double inPercent) noexcept
49  {
50  return utils::interp(inValue1, inValue2, inPercent);
51  }
52 
53  //============================================================================
54  // Method Description:
71  template<typename dtype>
72  NdArray<dtype> interp(const NdArray<dtype>& inX, const NdArray<dtype>& inXp, const NdArray<dtype>& inFp)
73  {
74  // do some error checking first
75  if (inXp.size() != inFp.size())
76  {
77  THROW_INVALID_ARGUMENT_ERROR("inXp and inFp need to be the same size().");
78  }
79 
80  if (inX.min().item() < inXp.min().item() || inX.max().item() > inXp.max().item())
81  {
82  THROW_INVALID_ARGUMENT_ERROR("endpoints of inX should be contained within inXp.");
83  }
84 
85  // sort the input inXp and inFp data
86  NdArray<uint32> sortedXpIdxs = argsort(inXp);
87  NdArray<dtype> sortedXp(1, inFp.size());
88  NdArray<dtype> sortedFp(1, inFp.size());
89  uint32 counter = 0;
90  for (auto sortedXpIdx : sortedXpIdxs)
91  {
92  sortedXp[counter] = inXp[sortedXpIdx];
93  sortedFp[counter++] = inFp[sortedXpIdx];
94  }
95 
96  // sort the input inX array
97  NdArray<dtype> sortedX = sort(inX);
98 
99  NdArray<dtype> returnArray(1, inX.size());
100 
101  uint32 currXpIdx = 0;
102  uint32 currXidx = 0;
103  while (currXidx < sortedX.size())
104  {
105  if (sortedXp[currXpIdx] <= sortedX[currXidx] && sortedX[currXidx] <= sortedXp[currXpIdx + 1])
106  {
107  const double percent = static_cast<double>(sortedX[currXidx] - sortedXp[currXpIdx]) /
108  static_cast<double>(sortedXp[currXpIdx + 1] - sortedXp[currXpIdx]);
109  returnArray[currXidx++] = utils::interp(sortedFp[currXpIdx], sortedFp[currXpIdx + 1], percent);
110  }
111  else
112  {
113  ++currXpIdx;
114  }
115  }
116 
117  return returnArray;
118  }
119 } // namespace nc
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2975
nc::interp
constexpr double interp(dtype inValue1, dtype inValue2, double inPercent) noexcept
Definition: Functions/interp.hpp:48
Error.hpp
nc::utils::interp
constexpr double interp(double inValue1, double inValue2, double inPercent) noexcept
Definition: Utils/interp.hpp:43
nc::NdArray::max
NdArray< dtype > max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2996
nc::NdArray< dtype >
interp.hpp
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
nc
Definition: Coordinate.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::sort
NdArray< dtype > sort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sort.hpp:47
nc::argsort
NdArray< uint32 > argsort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: argsort.hpp:47
nc::NdArray::min
NdArray< dtype > min(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:3053