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