Skip to content

Commit 1bfe330

Browse files
committed
Implement np.interp
1 parent 5955d0c commit 1bfe330

File tree

2 files changed

+839
-783
lines changed

2 files changed

+839
-783
lines changed

src/Numpy/Manual/np.math.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Numerics;
45
using System.Runtime.InteropServices;
56
using System.Text;
67
using Numpy;
@@ -165,5 +166,97 @@ public static NDarray gradient(NDarray f, List<double> varargs, int? edge_order
165166
return ToCsharp<NDarray>(py);
166167
}
167168

169+
/// <summary>
170+
/// One-dimensional linear interpolation.<br></br>
171+
///
172+
/// Returns the one-dimensional piecewise linear interpolant to a function
173+
/// with given discrete data points (xp, fp), evaluated at x.<br></br>
174+
///
175+
/// Notes
176+
///
177+
/// Does not check that the x-coordinate sequence xp is increasing.<br></br>
178+
///
179+
/// If xp is not increasing, the results are nonsense.<br></br>
180+
///
181+
/// A simple check for increasing is:
182+
/// </summary>
183+
/// <param name="x">
184+
/// The x-coordinates at which to evaluate the interpolated values.
185+
/// </param>
186+
/// <param name="xp">
187+
/// The x-coordinates of the data points, must be increasing if argument
188+
/// period is not specified.<br></br>
189+
/// Otherwise, xp is internally sorted after
190+
/// normalizing the periodic boundaries with xp = xp % period.
191+
/// </param>
192+
/// <param name="fp">
193+
/// The y-coordinates of the data points, same length as xp.
194+
/// </param>
195+
/// <param name="left">
196+
/// Value to return for x &lt; xp[0], default is fp[0].
197+
/// </param>
198+
/// <param name="right">
199+
/// Value to return for x &gt; xp[-1], default is fp[-1].
200+
/// </param>
201+
/// <param name="period">
202+
/// A period for the x-coordinates.<br></br>
203+
/// This parameter allows the proper
204+
/// interpolation of angular x-coordinates.<br></br>
205+
/// Parameters left and right
206+
/// are ignored if period is specified.
207+
/// </param>
208+
/// <returns>
209+
/// The interpolated values, same shape as x.
210+
/// </returns>
211+
public static NDarray interp(this NDarray x, IReadOnlyCollection<float> xp, IReadOnlyCollection<float> fp, float? left = null, float? right = null, float? period = null)
212+
{
213+
var __self__ = self;
214+
var pyargs = ToTuple(new object[]
215+
{
216+
x,
217+
xp,
218+
fp,
219+
});
220+
var kwargs = new PyDict();
221+
if (left != null) kwargs["left"] = ToPython(left);
222+
if (right != null) kwargs["right"] = ToPython(right);
223+
if (period != null) kwargs["period"] = ToPython(period);
224+
dynamic py = __self__.InvokeMethod("interp", pyargs, kwargs);
225+
return ToCsharp<NDarray>(py);
226+
}
227+
228+
public static float interp(float x, IReadOnlyCollection<float> xp, IReadOnlyCollection<float> fp, float? left = null, float? right = null, float? period = null)
229+
{
230+
var __self__ = self;
231+
var pyargs = ToTuple(new object[]
232+
{
233+
x,
234+
xp,
235+
fp,
236+
});
237+
var kwargs = new PyDict();
238+
if (left != null) kwargs["left"] = ToPython(left);
239+
if (right != null) kwargs["right"] = ToPython(right);
240+
if (period != null) kwargs["period"] = ToPython(period);
241+
dynamic py = __self__.InvokeMethod("interp", pyargs, kwargs);
242+
return ToCsharp<float>(py);
243+
}
244+
245+
public static NDarray interp(this NDarray x, IReadOnlyCollection<float> xp, Complex[] fp, Complex? left = null, Complex? right = null, float? period = null)
246+
{
247+
var __self__ = self;
248+
var pyargs = ToTuple(new object[]
249+
{
250+
x,
251+
xp,
252+
np.array(fp),
253+
});
254+
var kwargs = new PyDict();
255+
if (left != null) kwargs["left"] = ToPython(left);
256+
if (right != null) kwargs["right"] = ToPython(right);
257+
if (period != null) kwargs["period"] = ToPython(period);
258+
dynamic py = __self__.InvokeMethod("interp", pyargs, kwargs);
259+
return ToCsharp<NDarray>(py);
260+
}
168261
}
169262
}

0 commit comments

Comments
 (0)