def test_wrapper(self): P = KroghInterpolator(self.xs, self.ys) assert_almost_equal(P(self.test_xs), krogh_interpolate(self.xs, self.ys, self.test_xs)) assert_almost_equal(P.derivative(self.test_xs, 2), krogh_interpolate(self.xs, self.ys, self.test_xs, der=2)) assert_almost_equal( P.derivatives(self.test_xs, 2), krogh_interpolate(self.xs, self.ys, self.test_xs, der=[0, 1]) )
def test_shapes_scalarvalue_derivative(self): P = KroghInterpolator(self.xs,self.ys) n = P.n assert_array_equal(np.shape(P.derivatives(0)), (n,)) assert_array_equal(np.shape(P.derivatives(np.array(0))), (n,)) assert_array_equal(np.shape(P.derivatives([0])), (n,1)) assert_array_equal(np.shape(P.derivatives([0,1])), (n,2))
def test_derivatives_complex(self): # regression test for gh-7381: krogh.derivatives(0) fails complex y x, y = np.array([-1, -1, 0, 1, 1]), np.array([1, 1.0j, 0, -1, 1.0j]) func = KroghInterpolator(x, y) cmplx = func.derivatives(0) cmplx2 = (KroghInterpolator(x, y.real).derivatives(0) + 1j*KroghInterpolator(x, y.imag).derivatives(0)) assert_allclose(cmplx, cmplx2, atol=1e-15)
def test_int_inputs(self): # Check input args are cast correctly to floats, gh-3669 x = [0, 234,468,702,936,1170,1404,2340,3744,6084,8424,13104,60000] offset_cdf = np.array([-0.95, -0.86114777, -0.8147762, -0.64072425, -0.48002351, -0.34925329, -0.26503107, -0.13148093, -0.12988833, -0.12979296, -0.12973574, -0.08582937, 0.05]) f = KroghInterpolator(x, offset_cdf) assert_allclose(abs((f(x) - offset_cdf) / f.derivative(x, 1)), 0, atol=1e-10)
def test_vector(self): xs = [0, 1, 2] ys = np.array([[0, 1], [1, 0], [2, 1]]) P = KroghInterpolator(xs, ys) Pi = [KroghInterpolator(xs, ys[:, i]) for i in xrange(ys.shape[1])] test_xs = np.linspace(-1, 3, 100) assert_almost_equal(P(test_xs), np.rollaxis(np.asarray([p(test_xs) for p in Pi]), -1)) assert_almost_equal( P.derivatives(test_xs), np.transpose(np.asarray([p.derivatives(test_xs) for p in Pi]), (1, 2, 0)) )
def krogh_deriv(x, y, axis=0): return KroghInterpolator(x, y, axis).derivative
def derivatives(f, T0, dT=.001): # evaluate function at Chebyshev zeros as suggested by docs T = T0 + dT * np.cos(np.linspace(0, np.pi, nd)) return KroghInterpolator(T, f(T)).derivatives(T0)
def test_shapes_1d_vectorvalue(self): P = KroghInterpolator(self.xs, np.outer(self.ys, [1])) assert_array_equal(np.shape(P(0)), (1, )) assert_array_equal(np.shape(P([0])), (1, 1)) assert_array_equal(np.shape(P([0, 1])), (2, 1))
def test_shapes_vectorvalue_derivative(self): P = KroghInterpolator(self.xs, np.outer(self.ys, np.arange(3))) n = P.n assert_array_equal(np.shape(P.derivatives(0)), (n, 3)) assert_array_equal(np.shape(P.derivatives([0])), (n, 1, 3)) assert_array_equal(np.shape(P.derivatives([0, 1])), (n, 2, 3))
plt.plot(FaCO, e, 'o', x, f(x), '-') #courbe (FaCO,e) et (x,f(x)) plt.xlabel('FaCO') #nomme l'axe des x puis apres y plt.ylabel('e') plt.title('interpolation_lineaire,e=f(FaCO)') #on nomme le graphique plt.show() #on affiche g = interpolate.interp1d(FaCO, Y) x = np.linspace(0.172, 6.89, 100) y = g(x) plt.plot(FaCO, Y, 'o', x, g(x), '-') plt.xlabel('Y') plt.ylabel('e') plt.title('interpolation_lineaire,Y=f(Faco)') plt.show() f = KroghInterpolator(FaCO, e) #interpolation polynomiale x = np.linspace(0.172, 6.89, 100) y = f(x) plt.plot(FaCO, e, 'o', x, f(x), '-') plt.xlabel('FaCO') plt.ylabel('e') plt.title('interpolation_polynomiale,e=f(FaCO)') plt.show() g = KroghInterpolator(FaCO, Y) x = np.linspace(0.172, 6.89, 100) y = g(x) plt.plot(FaCO, Y, 'o', x, g(x), '-') plt.xlabel('Y') plt.ylabel('e') plt.title('interpolation_polynomiale,Y=f(Faco)')
# 埃尔米特插值 from scipy.interpolate import KroghInterpolator import numpy as np import matplotlib.pyplot as plt def f(x): return x**3 + 1 xs = np.linspace(-5, 5, 100) nodes = np.array([0, 1, 2, 3]) xi = np.array([0, 1, 2, 3]) yi = np.array([1, 2, 9, 28]) interpolant = KroghInterpolator(xi, yi) plt.figure() plt.subplot(121) plt.plot(xs, interpolant(xs), "b--", label="Hermite interpolation") plt.plot(nodes, f(nodes), "ro", label="nodes") # 显示图中的标签 plt.legend(loc=9) # 设定坐标上下限 plt.xlim(-10.5, 10.5) plt.title("$f(x) = x^3 + 1$") # 1行 2列 plt.subplot(122) plt.plot(xs, f(xs), "g--", label="original") plt.plot(xi, f(xi), "ro", label="nodes") plt.legend(loc=9)
def interpolate(x0, x1, m0, m1, t0, t1): x = np.repeat([t0, t1], 2) y = np.ravel(np.dstack(([x0, x1], [m0, m1]))) return KroghInterpolator(x, y)
def test_derivative(self): P = KroghInterpolator(self.xs,self.ys) m = 10 r = P.derivatives(self.test_xs,m) for i in xrange(m): assert_almost_equal(P.derivative(self.test_xs,i),r[i])
def test_lagrange(self): P = KroghInterpolator(self.xs, self.ys) assert_almost_equal(self.true_poly(self.test_xs), P(self.test_xs))
def test_low_derivatives(self): P = KroghInterpolator(self.xs, self.ys) D = P.derivatives(self.test_xs, len(self.xs) + 2) for i in xrange(D.shape[0]): assert_almost_equal(self.true_poly.deriv(i)(self.test_xs), D[i])
def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--Tmin', type=float, default=.002, help='minimum temperature') parser.add_argument('--Ta', type=float, default=.165, help='connection range minimum temperature') parser.add_argument('--Tb', type=float, default=.200, help='connection range maximum temperature') parser.add_argument('--Tmax', type=float, default=.600, help='maximum temperature') parser.add_argument('--nsteps', type=int, default=10**5, help='number of energy density steps') parser.add_argument('--species', choices=['all', 'urqmd', 'id'], default='urqmd', help='HRG species') parser.add_argument('--res-width-off', action='store_false', dest='res_width', help='do not account for finite width of resonances') parser.add_argument('--plot', nargs='?', metavar='FILE', const='<show>', default=False, help='plot EoS instead of printing table') parser.add_argument('--write-bin', metavar='FILE', help='write binary file instead of printing table') args = parser.parse_args() hrg_kwargs = dict(species=args.species, res_width=args.res_width) # split full temperature range into three parts: # low-T (l): Tmin < T < Ta (HRG) # mid-T (m): Ta < T < Tb (connection) # high-T (h): Tb < T < Tmax (lattice) # number of extra temperature points below Tmin and above Tmax # (helps cubic interpolation) nextrapts = 2 # compute low-T (HRG) trace anomaly Tl = T_points(args.Tmin, args.Ta, 1000, extra_low=nextrapts) e3p_T4_l = HRGEOS(Tl, **hrg_kwargs).e3p_T4() # compute mid-T (connection) using an interpolating polynomial that # matches the function values and first several derivatives at the # connection endpoints (Ta, Tb) nd = 5 # use Krogh interpolation near the endpoints to estimate derivatives def derivatives(f, T0, dT=.001): # evaluate function at Chebyshev zeros as suggested by docs T = T0 + dT * np.cos(np.linspace(0, np.pi, nd)) return KroghInterpolator(T, f(T)).derivatives(T0) # use another Krogh interpolation for the connection polynomial # skip (Ta, Tb) endpoints in Tm since they are already in (Tl, Th) Tm = T_points(args.Ta, args.Tb, 1000, extra_low=-1, extra_high=-1) e3p_T4_m = KroghInterpolator( nd * [args.Ta] + nd * [args.Tb], np.concatenate([ derivatives(lambda T: HRGEOS(T, **hrg_kwargs).e3p_T4(), args.Ta), derivatives(e3p_T4_lattice, args.Tb) ]))(Tm) # compute high-T part (lattice) Th = T_points(args.Tb, args.Tmax, 1000, extra_high=nextrapts) e3p_T4_h = e3p_T4_lattice(Th) # join temperature ranges together T = np.concatenate([Tl, Tm, Th]) e3p_T4 = np.concatenate([e3p_T4_l, e3p_T4_m, e3p_T4_h]) # pressure is integral of trace anomaly over temperature starting from some # reference temperature, Eq. (12) in HotQCD paper: # p/T^4(T) = p/T^4(T_0) + \int_{T_0}^T dT (e - 3p)/T^5 delta_p_T4_spline = CubicSpline(T, e3p_T4 / T).antiderivative() p_T4_0 = HRGEOS(T[:1], **hrg_kwargs).p_T4()[0] def compute_p_T4(T): p_T4 = delta_p_T4_spline(T) p_T4 += p_T4_0 return p_T4 p_T4 = compute_p_T4(T) e_T4 = e3p_T4 + 3 * p_T4 if args.plot: plot(T, e3p_T4, p_T4, e_T4, args, hrg_kwargs) return # energy density at original temperature points e_orig = e_T4 * T**4 / HBARC**3 T_orig = T # compute thermodynamic quantities at evenly-spaced temperature points # as required by osu-hydro-pce T = np.linspace(T[nextrapts], T[-nextrapts - 1], args.nsteps) e = CubicSpline(T_orig, e_orig)(T) p = compute_p_T4(T) * T**4 / HBARC**3 # Nc = 3 # Nf = 3 # T = np.linspace(1e-10, 0.7, 10**5) # e = np.pi**2 * 1/30. * (2*(Nc*Nc-1) + 3.5*Nc*Nf) * np.ones(T.shape) * T**4 / HBARC**3 # p = e/3. s = (e + p) / T cs2 = CubicSpline(e, p)(e, nu=1) cstilde2 = p / e if args.write_bin: with open(args.write_bin, 'wb') as f: # for x in [e[0], e[-1], p, s, T]: for x in [T[0], T[-1], p, s, e, cs2, cstilde2]: f.write(x.tobytes()) else: # output table fmt = 4 * '{:24.16e}'
def test_wrapper(self): P = KroghInterpolator(self.xs,self.ys) assert_almost_equal(P(self.test_xs),krogh_interpolate(self.xs,self.ys,self.test_xs)) assert_almost_equal(P.derivative(self.test_xs,2),krogh_interpolate(self.xs,self.ys,self.test_xs,der=2)) assert_almost_equal(P.derivatives(self.test_xs,2),krogh_interpolate(self.xs,self.ys,self.test_xs,der=[0,1]))
def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( '--Tmin', type=float, default=.050, help='minimum temperature' ) parser.add_argument( '--Ta', type=float, default=.165, help='connection range minimum temperature' ) parser.add_argument( '--Tb', type=float, default=.200, help='connection range maximum temperature' ) parser.add_argument( '--Tmax', type=float, default=.500, help='maximum temperature' ) parser.add_argument( '--nsteps', type=int, default=10**5, help='number of energy density steps' ) parser.add_argument( '--species', choices=['all', 'urqmd', 'id'], default='urqmd', help='HRG species' ) parser.add_argument( '--res-width-off', action='store_false', dest='res_width', help='do not account for finite width of resonances' ) parser.add_argument( '--plot', nargs='?', metavar='FILE', const='<show>', default=False, help='plot EoS instead of printing table' ) parser.add_argument( '--write-bin', metavar='FILE', help='write binary file instead of printing table' ) args = parser.parse_args() hrg_kwargs = dict(species=args.species, res_width=args.res_width) # split full temperature range into three parts: # low-T (l): Tmin < T < Ta (HRG) # mid-T (m): Ta < T < Tb (connection) # high-T (h): Tb < T < Tmax (lattice) # number of extra temperature points below Tmin and above Tmax # (helps cubic interpolation) nextrapts = 2 # compute low-T (HRG) trace anomaly Tl = T_points(args.Tmin, args.Ta, 200, extra_low=nextrapts) e3p_T4_l = HRGEOS(Tl, **hrg_kwargs).e3p_T4() # compute mid-T (connection) using an interpolating polynomial that # matches the function values and first several derivatives at the # connection endpoints (Ta, Tb) nd = 5 # use Krogh interpolation near the endpoints to estimate derivatives def derivatives(f, T0, dT=.001): # evaluate function at Chebyshev zeros as suggested by docs T = T0 + dT*np.cos(np.linspace(0, np.pi, nd)) return KroghInterpolator(T, f(T)).derivatives(T0) # use another Krogh interpolation for the connection polynomial # skip (Ta, Tb) endpoints in Tm since they are already in (Tl, Th) Tm = T_points(args.Ta, args.Tb, 100, extra_low=-1, extra_high=-1) e3p_T4_m = KroghInterpolator( nd*[args.Ta] + nd*[args.Tb], np.concatenate([ derivatives(lambda T: HRGEOS(T, **hrg_kwargs).e3p_T4(), args.Ta), derivatives(e3p_T4_lattice, args.Tb) ]) )(Tm) # compute high-T part (lattice) Th = T_points(args.Tb, args.Tmax, 1000, extra_high=nextrapts) e3p_T4_h = e3p_T4_lattice(Th) # join temperature ranges together T = np.concatenate([Tl, Tm, Th]) e3p_T4 = np.concatenate([e3p_T4_l, e3p_T4_m, e3p_T4_h]) # pressure is integral of trace anomaly over temperature starting from some # reference temperature, Eq. (12) in HotQCD paper: # p/T^4(T) = p/T^4(T_0) + \int_{T_0}^T dT (e - 3p)/T^5 delta_p_T4_spline = CubicSpline(T, e3p_T4/T).antiderivative() p_T4_0 = HRGEOS(T[:1], **hrg_kwargs).p_T4()[0] def compute_p_T4(T): p_T4 = delta_p_T4_spline(T) p_T4 += p_T4_0 return p_T4 p_T4 = compute_p_T4(T) e_T4 = e3p_T4 + 3*p_T4 # energy density at original temperature points e_orig = e_T4 * T**4 / HBARC**3 # compute thermodynamic quantities at evenly-spaced energy density points # as required by osu-hydro e = np.linspace(e_orig[nextrapts], e_orig[-nextrapts - 1], args.nsteps) T = CubicSpline(e_orig, T)(e) p = compute_p_T4(T) * T**4 / HBARC**3 s = (e + p)/T Tc = 0.270 tlat = Tc*np.array([0.7, 0.74, 0.78, 0.82, 0.86, 0.9, 0.94, 0.98, 1, 1.02, 1.06, 1.10, 1.14, 1.18, 1.22, 1.26, 1.30, 1.34, 1.38, 1.42, 1.46, 1.5, 2, 2.5, 3, 3.5, 4.0, 4.5, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 80, 100, 200, 300, 400, 500, 600, 800, 1000]) Ilat = np.array([.0104, .0162, .0232, .0318, .0433, .0594, .0859, .1433, 1.0008, 2.078, 2.4309, 2.4837, 2.4309, 2.3426, 2.2342, 2.1145, 1.998, 1.8867, 1.7809, 1.6810, 1.5872, 1.4995, 0.8038, 0.5057, 0.3589, 0.2736, 0.2207, 0.1855, 0.1606, 0.1266, 0.1050, 0.0903, 0.0798, 0.0720, 0.0375, 0.0265, 0.0216, 0.0191, 0.0174, 0.0154, 0.0142, 0.0112, 0.01, 0.0091, 0.0085, 0.0080, 0.0073, 0.0068]) plat = np.array([0.0015, 0.0023, 0.0033, 0.0046, 0.0064, 0.0087, 0.0118, 0.0164, 0.0222, 0.0571, 0.1455, 0.237, 0.325, 0.4074, 0.4837, 0.5539, 0.6181, 0.677, 0.7309, 0.7804, 0.8258, 0.8675, 1.189, 1.3319, 1.4098, 1.4582, 1.491, 1.5149, 1.533, 1.5591, 1.5768, 1.5898, 1.5998, 1.6078, 1.6444, 1.6572, 1.6641, 1.6686, 1.672, 1.6767, 1.68, 1.6887, 1.693, 1.6958, 1.6977, 1.6992, 1.7014, 1.703]) e3p_T4_gluon = CubicSpline(tlat,Ilat) delta_p_T4_spline_gluon = CubicSpline(tlat, Ilat/tlat).antiderivative() p_T4_0_gluon = HRGEOS(tlat[:1], **hrg_kwargs).p_T4()[0] def compute_p_T4_gluon(T): p_T4 = delta_p_T4_spline_gluon(T) p_T4 += p_T4_0_gluon return p_T4 p_T4_gluon = compute_p_T4_gluon(tlat) e_T4_gluon = Ilat + 3*plat # energy density at original temperature points e_orig_gluon = e_T4_gluon * tlat**4 / HBARC**3 # compute thermodynamic quantities at evenly-spaced energy density points # as required by osu-hydro e_gluon = np.linspace(e_orig_gluon[nextrapts], e_orig_gluon[-nextrapts - 1], args.nsteps) T_gluon = CubicSpline(e_orig_gluon, tlat)(e) p_gluon = compute_p_T4_gluon(T) * T_gluon**4 / HBARC**3 s_gluon = (e_gluon + p_gluon)/T_gluon plot(T, e3p_T4, p_T4, e_T4, T_gluon, e3p_T4_gluon, p_T4_gluon, e_T4_gluon, args, hrg_kwargs) return
from scipy.interpolate import KroghInterpolator #from format_data import Formatter import matplotlib.pyplot as plt import time import numpy as np x_values = [1,2,3,4,5] y_values = [6,7,8,9,10] intra_x_values = [6,7,8] poly_func = KroghInterpolator(x_values,y_values) intra_x_values = np.asarray(intra_x_values) intra_y_values = poly_func.__call__(intra_x_values) print(intra_y_values)
def test_shapes_scalarvalue_derivative(self): P = KroghInterpolator(self.xs, self.ys) n = P.n assert_array_equal(np.shape(P.derivatives(0)), (n, )) assert_array_equal(np.shape(P.derivatives([0])), (n, 1)) assert_array_equal(np.shape(P.derivatives([0, 1])), (n, 2))
def test_scalar(self): P = KroghInterpolator(self.xs, self.ys) assert_almost_equal(self.true_poly(7), P(7)) assert_almost_equal(self.true_poly(np.array(7)), P(np.array(7)))
def test_derivative(self): P = KroghInterpolator(self.xs, self.ys) m = 10 r = P.derivatives(self.test_xs, m) for i in range(m): assert_almost_equal(P.derivative(self.test_xs, i), r[i])
def test_high_derivative(self): P = KroghInterpolator(self.xs, self.ys) for i in range(len(self.xs), 2 * len(self.xs)): assert_almost_equal(P.derivative(self.test_xs, i), np.zeros(len(self.test_xs)))
def test_empty(self): P = KroghInterpolator(self.xs, self.ys) assert_array_equal(P([]), [])
def test_low_derivatives(self): P = KroghInterpolator(self.xs,self.ys) D = P.derivatives(self.test_xs,len(self.xs)+2) for i in xrange(D.shape[0]): assert_almost_equal(self.true_poly.deriv(i)(self.test_xs), D[i])
def test_shapes_scalarvalue(self): P = KroghInterpolator(self.xs, self.ys) assert_array_equal(np.shape(P(0)), ()) assert_array_equal(np.shape(P(np.array(0))), ()) assert_array_equal(np.shape(P([0])), (1, )) assert_array_equal(np.shape(P([0, 1])), (2, ))
def test_high_derivative(self): P = KroghInterpolator(self.xs,self.ys) for i in xrange(len(self.xs),2*len(self.xs)): assert_almost_equal(P.derivative(self.test_xs,i), np.zeros(len(self.test_xs)))
def test_shapes_vectorvalue(self): P = KroghInterpolator(self.xs, np.outer(self.ys, np.arange(3))) assert_array_equal(np.shape(P(0)), (3, )) assert_array_equal(np.shape(P([0])), (1, 3)) assert_array_equal(np.shape(P([0, 1])), (2, 3))
def test_shapes_vectorvalue_derivative(self): P = KroghInterpolator(self.xs,np.outer(self.ys,np.arange(3))) n = P.n assert_array_equal(np.shape(P.derivatives(0)), (n,3)) assert_array_equal(np.shape(P.derivatives([0])), (n,1,3)) assert_array_equal(np.shape(P.derivatives([0,1])), (n,2,3))
def test_high_degree_warning(self): with pytest.warns(UserWarning, match="40 degrees provided,"): KroghInterpolator(np.arange(40), np.ones(40))