def get_temporal_interpolation(time_data, value_data, interpolation_degree=1): """ Obtain a symbolic piecewise function that interpolates between values given in ``value_data`` for the intervals defined in ``time_data``, based on a spline interpolation of degree ``interpolation_degree``. If ``interpolation_degree == 0``, the function changes according to step functions. In this case ``time_data`` needs to have one value more than ``value_data``. The values in ``time_data`` and ``value_data`` can be symbols or numeric values. Parameters ---------- time_data : list Sorted list of time values. value_data : list List of values corresponding to the times given in ``time_data``. interpolation_degree : int The degree of the polynomial that interpolates between values. """ t = sympy.symbols("t") if interpolation_degree == 0: if len(time_data) != len(value_data) + 1: raise ValueError("For ``interpolation_degree == 0``, `time_data`` " +\ "needs to have one value more than ``value_data``.") return sympy.Piecewise(*[ (v, (time_data[i] <= t) & ( t < time_data[i+1])) \ for i, v in enumerate(value_data) ]) else: return sympy.interpolating_spline(interpolation_degree, t, time_data, value_data)
def plotArgs(args): if type(args[0]) in (tuple, list): args = list(args) args[0] = sympy.interpolating_spline( 3, sympy.Symbol("x"), list(range(len(args[0]))), args[0], ) return args
def build_spline_lineshape(degree, nodes): """ Spline interpolated line shape """ from sympy import interpolating_spline, symarray from sympy.abc import x from sympy.utilities.lambdify import lambdify, lambdastr coeffs = symarray("c", len(nodes)) spl = interpolating_spline(degree, x, nodes, coeffs) f = lambdify([x, coeffs], spl, "tensorflow") @atfi.function def spline_lineshape(m, ampl_re, ampl_im): return atfi.complex(f(m, ampl_re), f(m, ampl_im)) return spline_lineshape
def test_temporal_interpolation(self): self.assertRaises(ValueError, get_temporal_interpolation, [0, 1], [0, 1], interpolation_degree=0) t = sympy.symbols("t") p = sympy.Piecewise((1, (0 <= t) & (t < 1)), (2, (1 <= t) & (t < 2))) p2 = get_temporal_interpolation([0, 1, 2], [1, 2], 0) assert (p.subs(t, 0.5) == p2.subs(t, 0.5)) assert (p.subs(t, 1.5) == p2.subs(t, 1.5)) assert (str(p) == str(p2)) p = sympy.interpolating_spline(1, t, [0, 1, 2], [1, 2, 3]) p2 = get_temporal_interpolation([0, 1, 2], [1, 2, 3], 1) assert (p.subs(t, 0.5) == p2.subs(t, 0.5)) assert (p.subs(t, 1.5) == p2.subs(t, 1.5)) assert (str(p) == str(p2))
def interpolating_spline(x): return diffify(sympy.interpolating_spline(x))
def __init__(self, dk, n, nh, isf, nsf, xsf, zsf, cfuns, chfuns, hfuns, order, sys): assert len(cfuns) == len(hfuns) == n + 1 if nsf > 1: assert len(chfuns) == nh + 1 assert order != 0 self.dk = dk self.n = n self.nh = nh assert isf == 0 self.isf = isf self.nsf = nsf assert len(xsf) == len(zsf) == nsf self.xsf = xsf self.zsf = zsf self.order = order self.sys = sys self.xdom = 2.0 * sp.pi / self.dk # Express swd coordinates using application coordinates (x,y,z,t) self.xswd, self.yswd, self.zswd, self.tswd = sys.app2swd() self.cfuns = cfuns if self.nsf == 1: self.chfuns = [] for j in range(nh + 1): gamh = sp.exp(- 2 * j * self.dk * (-self.zsf[0])) cjh = self.cfuns[j].scale(gamh) self.chfuns.append(cjh) else: self.chfuns = chfuns self.hfuns = hfuns if self.order < 0: self.Zfun = self.Zfun_exact self.Zhfun = self.Zhfun_exact else: self.Zfun = self.Zfun_exact_taylor self.Zhfun = self.Zhfun_exact_taylor if self.isf == 0: if self.nsf == 0: self.f_zfloor = sp.interpolating_spline(1, x, [0.0, self.xdom], [1.0, 1.0]) # Floor above z=0 elif self.nsf == 1: self.f_zfloor = sp.interpolating_spline(1, x, [0.0, self.xdom], [self.zsf[0], self.zsf[0]]) elif self.nsf > 1: self.f_zfloor = sp.interpolating_spline(1, x, self.xsf, self.zsf) self.f_zfloor_dzdx = sp.diff(self.f_zfloor, x) fun_phi_a = 0 fun_elev = 0 for j in range(n + 1): cj = self.cfuns[j].fun(self.tswd) fun_phi_a += cj * self.Xfun(j) * self.Zfun(j) hj = self.hfuns[j].fun(self.tswd) fun_elev += hj * self.Xfun(j) fun_phi_b = 0 for j in range(nh + 1): chj = self.chfuns[j].fun(self.tswd) fun_phi_b += chj * self.Xfun(j) * self.Zhfun(j) fun_phi = fun_phi_a + fun_phi_b fun_str = fun_phi_a - fun_phi_b self.f_phi = sp.re(fun_phi) self.f_stream = sp.im(fun_str) self.f_phi_t = sp.re(sp.diff(fun_phi, t)) self.f_phi_x = sp.re(sp.diff(fun_phi, x)) self.f_phi_y = sp.re(sp.diff(fun_phi, y)) self.f_phi_z = sp.re(sp.diff(fun_phi, z)) self.f_phi_tx = sp.re(sp.diff(fun_phi, t, x)) self.f_phi_ty = sp.re(sp.diff(fun_phi, t, y)) self.f_phi_tz = sp.re(sp.diff(fun_phi, t, z)) self.f_phi_xx = sp.re(sp.diff(fun_phi, x, x)) self.f_phi_xy = sp.re(sp.diff(fun_phi, x, y)) self.f_phi_xz = sp.re(sp.diff(fun_phi, x, z)) self.f_phi_yy = sp.re(sp.diff(fun_phi, y, y)) self.f_phi_yz = sp.re(sp.diff(fun_phi, y, z)) self.f_phi_zz = sp.re(sp.diff(fun_phi, z, z)) self.f_elev = sp.re(fun_elev) self.f_elev_t = sp.re(sp.diff(fun_elev, t)) self.f_elev_x = sp.re(sp.diff(fun_elev, x)) self.f_elev_y = sp.re(sp.diff(fun_elev, y)) self.f_elev_xx = sp.re(sp.diff(fun_elev, x, x)) self.f_elev_xy = sp.re(sp.diff(fun_elev, x, y)) self.f_elev_yy = sp.re(sp.diff(fun_elev, y, y))
def mPRR3(t): return interpolating_spline(1, t, times3, mPRR3s)
def mGI(t): return interpolating_spline(1, t, times2, mGIs)
def mTOC1(t): return interpolating_spline(1, t, times1, mTOC1s)