def survey_fisher(self, params, dx=1e-6): """ Create a fisher matrix using params, based on the overarching survey parameters. Parameters ---------- params: list List of parameters names to be included in fisher matrix dx: float The dx used for calculating numerical derivatives """ def _take_deriv(p2, p2name, p1, p1name): return deriv(deriv_like, p1, dx=dx, args=(p2, [p1name, p2name], self.zl, self.zs, self.dTc, self.cosmo_truths['Om0'], self.cosmo_truths['Ok'], self.cosmo_truths['Ode0'], self.cosmo_truths['w0'], self.cosmo_truths['wa'], self.cosmo_truths['h'], self.P)) fisher_matrix = np.zeros((len(params), len(params))) for i in range(len(params)): for j in range(len(params)): if i == j: fisher_matrix[i][j] = .5*np.abs(deriv(deriv_like, self.cosmo_truths[params[i]], dx=dx, args=(None, [params[i]], self.zl, self.zs, self.dTc, self.cosmo_truths['Om0'], self.cosmo_truths[ 'Ok'], self.cosmo_truths['Ode0'], self.cosmo_truths['w0'], self.cosmo_truths['wa'], self.cosmo_truths['h'], self.P), n=2)) else: fisher_matrix[i][j] = .5*deriv(_take_deriv, self.cosmo_truths[params[i]], dx=dx, args=(params[i], self.cosmo_truths[params[j]], params[j])) self.fisher_matrix = Fisher( data=fisher_matrix, params=params, name=self.name, cosmo_truths=self.cosmo_truths)
def bessel_jn_data(seed, times, noise_scales, order=1): from scipy.special import jn from scipy.misc import derivative as deriv import numpy as np np.random.seed(seed) A0 = np.array([[0., 1.0], [0., 0.0]]) A1 = np.array([[0., 0.0], [1., 0.0]]) A2 = np.array([[0.0, 0.0], [0.0, 1.0]]) def g1(t): return order**2 / t**2 - 1 def g2(t): return -1. / t X = np.column_stack( (jn(order, times), deriv(lambda z: jn(order, z), x0=times, dx=1e-6))) Y = X.copy() for k, s in enumerate(noise_scales): Y[:, k] += np.random.normal(scale=s, size=times.size) res_obj = { "time": times, "Y": Y, "X": X, "As": [A0, A1, A2], "Gs": [g1(times), g2(times)] } return res_obj
def y_prime(t, theta, v0): """Return the derivative of y at t t -- time in seconds theta -- launch angle in radians v0 -- total initial velocity in m/s """ return deriv(y, t, args=(theta, v0), dx=1e-5)
def anharmoniccvib(T): k2 = 0.782 k3 = -34.1 k4 = 34.88 def logZ(T): foo = lambda x: exp(( -k2*x*x -k3*x*x*x - k4*(x**4) ) / T ) return log(quad(foo,-Inf,Inf)[0]) energy = lambda x: x**2 * deriv(logZ,x,dx=1e-6) #return R*( 1/2. + deriv(energy,T,dx=1e-6 )) # 1/2 from kinetic energy #from perturbation theory return R * (1 - (k*T/1.6e-19) * 9. / 4 * k4 / (k2**2))
def deriv(self, x, n=1): '''Returns the nth derivative of the function at x.''' if len(num.shape(x)) < 1: scalar = True else: scalar = False xs = num.atleast_1d(x) f = lambda x: self.__call__(x)[0] res = deriv(f, xs, dx=self.scale/100., n=n) if scalar: return res[0] else: return res
def deriv(self, x, n=1): '''Returns the nth derivative of the function at x.''' if len(num.shape(x)) < 1: scalar = True else: scalar = False xs = num.atleast_1d(x) f = lambda x: self.__call__(x)[0] res = deriv(f, xs, dx=self.scale / 100., n=n) if scalar: return res[0] else: return res
def __init__(self, f=None, f_prime=None, f_pprime=None): #if no function is provided set f and its derivatives to return zero if f == None: zero_func = lambda t: 0 self.f = zero_func self.f_prime = zero_func self.f_pprime = zero_func else: self.f = f if f_prime == None: #numerically differentiate f if a function isn't given self.f_prime = lambda t: deriv(f, t, dx=1e-6, n=1) else: self.f_prime = f_prime if f_pprime == None: #numerically take second derivative of f if function isn't given self.f_pprime = lambda t: deriv(f, t, dx=1e-6, n=2) else: self.f_pprime = f_pprime
import numpy as np from gpode.examples import DataLoader import matplotlib.pyplot as plt from scipy.special import jn from scipy.misc import derivative as deriv order = 2 tt = np.linspace(0.5, 10., 10) bd = DataLoader.load("bessel jn", 11, tt, [0.1, 0.05], order=order) fig = plt.figure() ax = fig.add_subplot(111) td = np.linspace(tt[0], tt[-1], 100) ax.plot(td, jn(order, td), 'k-', alpha=0.2) ax.plot(td, deriv(lambda z: jn(order, z), x0=td, dx=1e-6), 'k-.', alpha=0.2) ax.plot(bd["time"], bd["Y"], 's') plt.show()
#This block is responsable for asking to the user the needed initial data. so = float(input('Insert the value of the initial position: ')) vo = float(input('Insert the value of the initial speed: ')) a = float(input('Insert the value of the initial acceleration: ')) print("\n") #Defining the position of the object as a function of time.(Equation-1.0) def S(t): return so + vo * t + 0.5 * a * t**2 t = sy.Symbol('t') print("Result of the first derivative: ") print(deriv(S, t)) print(sy.diff(S(t), t)) print("-----------------------------------\n") print("Result of the second derivative: ") print(deriv(S, t, n=2)) print(sy.diff(S(t), t, 2)) print("-----------------------------------\n") #Defining the velocity of the object as the derivative of the first equation.(Equation-1.1) def V(t): return deriv(S, t) ''' The following structure solves the roots of both quadratic and linear equations
def F_xx_scal(a,z,c): def f_a(cpr): return F_scal(a,z,cpr) return deriv(f_a,c,dx= 1e-6,n=2)
def aprimefunc_xx_scal(a,z,c): def aprimefunc_a(cpr): return aprimefunc_scal(a,z,cpr) return deriv(aprimefunc_a,c,dx= 1e-6,n=2)
def V(t): return deriv(S, t)
ax.plot(_tt, _yy, 'k-', alpha=0.5) ax.plot(m.data.time, mgr, 'o') for i in [0, 1]: xi_cm, xi_ccov = m._get_xi_conditional(i) sd = np.sqrt(np.diag(xi_ccov)) fig = plt.figure() ax = fig.add_subplot(111) _tt = np.linspace(tt[0], tt[-1], 100) if i == 0: _yy = jn(2, _tt) else: _yy = deriv(lambda z: jn(2, z), x0=_tt, dx=1e-6) ax.plot(_tt, _yy, 'k-', alpha=0.2) for _i, t in enumerate(tt): ax.plot([t, t], [xi_cm[_i] - sd[_i], xi_cm[_i] + sd[_i]], 'k-', alpha=0.5) ax.plot(m.data.time, xi_cm, 'o') ax.plot(m.data.time, m.data.Y[:, i], 's') fig3 = plt.figure() ax = fig3.add_subplot(111) X = [] SIGMAS = [] G1s = []
def Derivative(self, function, x0, dx, n): return deriv(function, x0, 1.0, n)
def _take_deriv(p2, p2name, p1, p1name): return deriv(deriv_like, p1, dx=dx, args=(p2, [p1name, p2name], self.zl, self.zs, self.dTc, self.cosmo_truths['Om0'], self.cosmo_truths['Ok'], self.cosmo_truths['Ode0'], self.cosmo_truths['w0'], self.cosmo_truths['wa'], self.cosmo_truths['h'], self.P))
def diff_nr(self): def differentiand(z): return self.g_plus(z) / self.N(z) return self.Gamma_L * deriv(differentiand, 0, dx=1.e-6)