def _get_I_total(R0, d, t): A = np.ln(R0) B = np.ln(1+ d) mu = 0.5 * A / B I = 0.5 * np.exp( 0.25 * A**2 / B * np.sqrt( np.pi / B)) I = I * np.sqrt(B) * (np.erf(t - mu)- np.erf(-mu)) return I
def _get_I_total(R0, d, t): A = np.ln(R0) B = np.ln(1 + d) mu = 0.5 * A / B I = 0.5 * np.exp(0.25 * A**2 / B * np.sqrt(np.pi / B)) I = I * np.sqrt(B) * (np.erf(t - mu) - np.erf(-mu)) return I
def _black_scholes(self, nopt, price, strike, t, rate, vol): mr = -rate sig_sig_two = vol * vol * 2 P = price S = strike T = t a = log(P / S) b = T * mr z = T * sig_sig_two c = 0.25 * z y = invsqrt(z) w1 = (a - b + c) * y w2 = (a - b - c) * y d1 = 0.5 + 0.5 * erf(w1) d2 = 0.5 + 0.5 * erf(w2) Se = exp(b) * S call = P * d1 - Se * d2 put = call - P + Se return (call, put)
def __init__(self, func=lambda x: np.sin(x), freq=440, vol=1.0, random=False, length=def_length): (pb_freq, pb_bits, pb_chns) = pg.mixer.get_init() self.freq = freq self.multiplier = int(freq * length) #self.multiplier = freq * length self.length = max(1, int(float(pb_freq) / freq * self.multiplier)) self.lin = np.linspace(0.0, self.multiplier, self.length, endpoint=False) #self.lin2 = np.linspace(0.0,m #self.lin = np.linspace(0.0, ) #self.ary = np.sin(self.lin * 2.0 * np.pi) self.ary = func(self.lin * 2 * np.pi) fade_fac = 1. fadeout_len = min(50, int(len(self.lin) * 0.01)) self.fadein = np.arctan(self.lin / fade_fac) / (np.pi / 2.) #self.fadeout= (1. + np.arctan(- ( self.lin - self.lin[-fadeout_len])/fade_fac )/(np.pi/2.)) self.fadein = np.erf(self.lin) self.fadeout = 0.5 - np.erf(self.lin - self.lin[-50]).astype('f2') / 2. self.fadeinout = np.multiply(self.fadein, self.fadeout) self.ary = np.multiply(self.fadeinout, self.ary) norm = abs(1. / self.ary.max()) self.ary = self.ary * norm norm2 = abs(1. / self.ary.min()) self.ary = self.ary * norm2 # If mixer is in stereo mode, double up the array information for # each channel. if pb_chns == 2: self.ary2 = np.repeat(self.ary[..., np.newaxis], 2, axis=1) else: self.ary2 = self.ary if pb_bits == 8: snd_ary = self.ary2 * vol * 127.0 self.sound = pg.sndarray.make_sound(snd_ary.astype(np.uint8) + 128) elif pb_bits == -16: snd_ary = self.ary2 * vol * float((1 << 15) - 1) self.sound = pg.sndarray.make_sound(snd_ary.astype(np.int16)) else: print "Sound playback resolution unsupported (either 8 or -16)." return None
def __init__(self, func = lambda x: np.sin(x ) ,freq=440 , vol=1.0, random=False, length=def_length ): (pb_freq, pb_bits, pb_chns) = pg.mixer.get_init() self.freq=freq self.multiplier = int(freq * length) #self.multiplier = freq * length self.length = max(1, int(float(pb_freq) / freq * self.multiplier)) self.lin = np.linspace(0.0, self.multiplier, self.length, endpoint=False) #self.lin2 = np.linspace(0.0,m #self.lin = np.linspace(0.0, ) #self.ary = np.sin(self.lin * 2.0 * np.pi) self.ary = func(self.lin*2*np.pi) fade_fac=1. fadeout_len = min(50, int(len(self.lin)*0.01 )) self.fadein= np.arctan(self.lin/fade_fac)/(np.pi/2.) #self.fadeout= (1. + np.arctan(- ( self.lin - self.lin[-fadeout_len])/fade_fac )/(np.pi/2.)) self.fadein = np.erf( self.lin ) self.fadeout = 0.5 - np.erf(self.lin- self.lin[-50]).astype('f2')/2. self.fadeinout = np.multiply(self.fadein, self.fadeout) self.ary = np.multiply(self.fadeinout,self.ary) norm = abs(1./self.ary.max()) self.ary = self.ary* norm norm2 = abs(1./self.ary.min()) self.ary = self.ary*norm2 # If mixer is in stereo mode, double up the array information for # each channel. if pb_chns == 2: self.ary2 = np.repeat(self.ary[..., np.newaxis], 2, axis=1) else: self.ary2 = self.ary if pb_bits == 8: snd_ary = self.ary2 * vol * 127.0 self.sound= pg.sndarray.make_sound(snd_ary.astype(np.uint8) + 128) elif pb_bits == -16: snd_ary = self.ary2 * vol * float((1 << 15) - 1) self.sound= pg.sndarray.make_sound(snd_ary.astype(np.int16)) else: print "Sound playback resolution unsupported (either 8 or -16)." return None
def _broaden_wmp_polynomials(E, dopp, n): r"""Evaluate Doppler-broadened windowed multipole curvefit. The curvefit is a polynomial of the form :math:`\frac{a}{E} + \frac{b}{\sqrt{E}} + c + d \sqrt{E} + \ldots` Parameters ---------- E : Real Energy to evaluate at. dopp : Real sqrt(atomic weight ratio / kT) in units of eV. n : Integral Number of components to the polynomial. Returns ------- numpy.ndarray The value of each Doppler-broadened curvefit polynomial term. """ sqrtE = np.sqrt(E) beta = sqrtE * dopp half_inv_dopp2 = 0.5 / dopp**2 quarter_inv_dopp4 = half_inv_dopp2**2 if beta > 6.0: # Save time, ERF(6) is 1 to machine precision. # beta/sqrtpi*exp(-beta**2) is also approximately 1 machine epsilon. erf_beta = 1.0 exp_m_beta2 = 0.0 else: erf_beta = np.erf(beta) exp_m_beta2 = np.exp(-beta**2) # Assume that, for sure, we'll use a second order (1/E, 1/V, const) # fit, and no less. factors = np.zeros(n) factors[0] = erf_beta / E factors[1] = 1.0 / sqrtE factors[2] = (factors[0] * (half_inv_dopp2 + E) + exp_m_beta2 / (beta * np.sqrt(np.pi))) # Perform recursive broadening of high order components. range(1, n-4) # replaces a do i = 1, n=3. All indices are reduced by one due to the # 1-based vs. 0-based indexing. for i in range(1, n - 4): if i != 1: factors[i + 2] = (-factors[i - 2] * (i - 1.0) * i * quarter_inv_dopp4 + factors[i] * (E + (1.0 + 2.0 * i) * half_inv_dopp2)) else: # Although it's mathematically identical, factors[0] will contain # nothing, and we don't want to have to worry about memory. factors[i + 2] = factors[i] * (E + (1.0 + 2.0 * i) * half_inv_dopp2) return factors
def register_element_ops(): binary_op_list = [ ["mul", lambda a, b: a * b], ["add", lambda a, b: a + b], ["sub", lambda a, b: a - b], ["div", lambda a, b: a / (b + 1e-4)], [ "pow", lambda a, b: torch.pow(a, b), lambda a, b: np.power(a, b), ], # no fuson triggered ["max", lambda a, b: torch.max(a, b), lambda a, b: np.maximum(a, b)], ["min", lambda a, b: torch.min(a, b), lambda a, b: np.minimum(a, b)], ] unary_op_list = [ ["erf", lambda x: torch.erf(x), lambda x: np.erf(x)], ["exp", lambda x: torch.exp(x), lambda x: np.exp(x)], ["sin", lambda x: torch.sin(x), lambda x: np.sin(x)], ["cos", lambda x: torch.cos(x), lambda x: np.cos(x)], [ "rand_like", lambda x: torch.rand_like(x), lambda x: np.random.rand(*x.shape) ], ] for split_input, binary_op in itertools.product([True, False], binary_op_list): # Make a copy of ElementBench if len(binary_op) == 2: [op_str, op_pt_func] = binary_op op_np_func = op_pt_func elif len(binary_op) == 3: [op_str, op_pt_func, op_np_func] = binary_op split_str = "split" if split_input else "shared" op_str = split_str + "_" + op_str bm_cls = type("ElementBench_" + op_str, (ElementBench, ), {}) bm_cls.op_str = op_str bm_cls.binary_op_pt_func = op_pt_func bm_cls.binary_op_np_func = op_np_func bm_cls.split_input = split_input benchmark.register_benchmark_class(bm_cls) for split_input, unary_op in itertools.product([True, False], unary_op_list): # Make a copy of ElementBench if len(unary_op) == 2: [op_str, op_pt_func] = unary_op op_np_func = op_pt_func elif len(unary_op) == 3: [op_str, op_pt_func, op_np_func] = unary_op split_str = "split" if split_input else "shared" op_str = split_str + "_" + op_str bm_cls = type("ElementBench_" + op_str, (ElementBench, ), {}) bm_cls.op_str = op_str bm_cls.unary_op_pt_func = op_pt_func bm_cls.unary_op_np_func = op_np_func bm_cls.split_input = split_input benchmark.register_benchmark_class(bm_cls)
def int_local_time(self, t, K): """Explicit form for the integral of the local time between 0 and t """ dJ = self.dJ(0, K) J = self.J(0, K) return dJ * np.exp(-J**2 / (2 * t)) * np.sqrt( 2 / np.pi) * np.sqrt(t) + dJ * J * (np.erf(J / (np.sqrt(2 * t))) - 1)
def _broaden_wmp_polynomials(E, dopp, n): r"""Evaluate Doppler-broadened windowed multipole curvefit. The curvefit is a polynomial of the form :math:`\frac{a}{E} + \frac{b}{\sqrt{E}} + c + d \sqrt{E} + \ldots` Parameters ---------- E : Real Energy to evaluate at. dopp : Real sqrt(atomic weight ratio / kT) in units of eV. n : Integral Number of components to the polynomial. Returns ------- numpy.ndarray The value of each Doppler-broadened curvefit polynomial term. """ sqrtE = np.sqrt(E) beta = sqrtE * dopp half_inv_dopp2 = 0.5 / dopp**2 quarter_inv_dopp4 = half_inv_dopp2**2 if beta > 6.0: # Save time, ERF(6) is 1 to machine precision. # beta/sqrtpi*exp(-beta**2) is also approximately 1 machine epsilon. erf_beta = 1.0 exp_m_beta2 = 0.0 else: erf_beta = np.erf(beta) exp_m_beta2 = np.exp(-beta**2) # Assume that, for sure, we'll use a second order (1/E, 1/V, const) # fit, and no less. factors = np.zeros(n) factors[0] = erf_beta / E factors[1] = 1.0 / sqrtE factors[2] = (factors[0] * (half_inv_dopp2 + E) + exp_m_beta2 / (beta * np.sqrt(np.pi))) # Perform recursive broadening of high order components. range(1, n-4) # replaces a do i = 1, n=3. All indices are reduced by one due to the # 1-based vs. 0-based indexing. for i in range(1, n-4): if i != 1: factors[i+2] = (-factors[i-2] * (i - 1.0) * i * quarter_inv_dopp4 + factors[i] * (E + (1.0 + 2.0 * i) * half_inv_dopp2)) else: # Although it's mathematically identical, factors[0] will contain # nothing, and we don't want to have to worry about memory. factors[i+2] = factors[i]*(E + (1.0 + 2.0 * i) * half_inv_dopp2) return factors
def erf(f=Ellipsis): ''' erf(x) yields a potential function that calculates the error function over the input x. If x is a constant, yields a constant potential function. erf() is equivalent to erf(...), which is just the error function, calculated over its inputs. ''' f = to_potential(f) if is_const_potential(f): return const_potential(np.erf(f.c)) elif is_identity_potential(f): return ErfPotential() else: return compose(ErfPotential(), f)
def expected_improvement(self, fref, f, s): if s > 0.0: if self.MIN[self.nfg]: y = (fref - f)/s else: y = (f - fref)/s cdf = 0.5*(1.0 - np.erf(-y/np.sqrt(2.0))) pdf = 1.0/np.sqrt(2.0*np.pi)*np.exp(-0.5*y**2.0) ei = s*(y*cdf + pdf) else: ei = 0.0 return ei
def grid_coherence(U, res, f0, model='Pierson-Moskowitz'): """ Calculates the coherence time of a resolution cell :param U: wind velocity at reference height :param res: grid size [m] :param f0: carrier frequency [Hz] :param model: Model used, defaults to Pierson-Moskowitz (currently the only one implemented) """ tau_c = 3.29 * const.c / f0 / U / np.sqrt(np.erf(2.688 * res / U**2)) return tau_c
def whrankHamm(q_codes, db_codes, q_feats, fmu, fstd, w_type='ones'): if w_type == 'ones': weights = np.ones_like(q_feats) elif w_type == 'q': weights = np.abs(q_feats) elif w_type == 'std': weights = np.ones_like(q_feats) / fstd elif w_type == 'q_std': weights = np.abs(q_feats) / fstd elif w_type == 'erf': Pr = 0.5 * (1 + q_codes * np.erf((-q_feats-fmu) / (np.sqrt(2)*fstd))) weights = np.log((1 - Pr) / Pr) num1 = q_codes.shape[0] num2 = db_codes.shape[0] distMat = np.zeros((num1, num2)) for i in range(num1): codediff = np.abs(np.tile(q_codes[i], (num2, 1)) - db_codes) / 2 distMat[i] = np.dot(weights[i], codediff.transpose()) return distMat
def __init__(self, alpha=2, Mach=10, epsilon=0.2, b=0.4, phi_t=1/3., alpha_vir=1., Beta=20, ): self.alpha = alpha self.b = b self.epsilon = epsilon self.Mach = Mach self.alpha_vir = alpha_vir self.phi_t = phi_t self.Beta = Beta # eqn19 self.s_t = 0.5 * (2*np.abs(self.alpha) - 1) * self.sigma_s**2 self.C = (np.exp(0.5 * (self.alpha-1) * self.alpha * self.sigma_s**2) / (self.sigma_s * np.sqrt(2*np.pi))) self.N = 2 * (1 + np.erf((2*np.log(self.s_t) + self.sigma_s**2)/(2**1.5*self.sigma_s)) + (2*self.C * np.exp(-self.s_t*self.alpha))/self.alpha)**-1
def value(self, x): return np.erf(flattest(x))
def test_unary_fun(self): import math data = [1., 2., 3.] c = np.array(data) d = np.acos(c / 3) for i, ei in enumerate(d): self.assertEqual(ei, math.acos(data[i] / 3)) d = np.asin(c / 3) for i, ei in enumerate(d): self.assertEqual(ei, math.asin(data[i] / 3)) d = np.atan(c) for i, ei in enumerate(d): self.assertEqual(ei, math.atan(data[i])) d = np.sin(c) for i, ei in enumerate(d): self.assertEqual(ei, math.sin(data[i])) d = np.cos(c) for i, ei in enumerate(d): self.assertEqual(ei, math.cos(data[i])) d = np.tan(c) for i, ei in enumerate(d): self.assertEqual(ei, math.tan(data[i])) d = np.acosh(c) for i, ei in enumerate(d): self.assertEqual(ei, math.acosh(data[i])) d = np.asinh(c) for i, ei in enumerate(d): self.assertEqual(ei, math.asinh(data[i])) d = np.atanh(c / 3.1) for i, ei in enumerate(d): self.assertEqual(ei, math.atanh(data[i] / 3.1)) d = np.sinh(c) for i, ei in enumerate(d): self.assertEqual(ei, math.sinh(data[i])) d = np.cosh(c) for i, ei in enumerate(d): self.assertEqual(ei, math.cosh(data[i])) d = np.tanh(c) for i, ei in enumerate(d): self.assertEqual(ei, math.tanh(data[i])) d = np.ceil(c * 2.7) for i, ei in enumerate(d): self.assertEqual(ei, math.ceil(data[i] * 2.7)) d = np.floor(c * 2.7) for i, ei in enumerate(d): self.assertEqual(ei, math.floor(data[i] * 2.7)) d = np.erf(c) for i, ei in enumerate(d): self.assertEqual(ei, math.erf(data[i])) d = np.erfc(c) for i, ei in enumerate(d): self.assertEqual(ei, math.erfc(data[i])) d = np.exp(c) for i, ei in enumerate(d): self.assertEqual(ei, math.exp(data[i])) d = np.expm1(c) for i, ei in enumerate(d): self.assertEqual(ei, math.expm1(data[i])) d = np.gamma(c) for i, ei in enumerate(d): self.assertEqual(ei, math.gamma(data[i])) d = np.lgamma(c) for i, ei in enumerate(d): self.assertEqual(ei, math.lgamma(data[i])) d = np.log(c) for i, ei in enumerate(d): self.assertEqual(ei, math.log(data[i])) d = np.log10(c) for i, ei in enumerate(d): self.assertEqual(ei, math.log10(data[i])) d = np.log2(c) for i, ei in enumerate(d): self.assertEqual(ei, math.log2(data[i])) d = np.sqrt(c) for i, ei in enumerate(d): self.assertEqual(ei, math.sqrt(data[i])) # slices data = [1., 2., 3.] c = np.array(data + data) d = np.cos(c[::2]) mm = data + data for i, ei in enumerate(d): self.assertEqual(ei, math.cos(mm[2 * i])) # 2d array data = [1., 2., 3.] c = np.array([data, data]) d = np.cos(c) mm = [data, data] for i, ei in enumerate(d): for j, eij in enumerate(ei): self.assertEqual(eij, math.cos(mm[i][j])) # 2d array slices data = [1., 2., 3.] c = np.array([data + data, data + data]) d = np.cos(c[:, ::2]) mm = [data + data, data + data] for i, ei in enumerate(d): for j, eij in enumerate(ei): self.assertEqual(eij, math.cos(mm[i][2 * j]))
def gauss_cdf(z): return 0.5 * (1 + np.erf(z / np.sqrt(2)))
def Phi(x): """ cumulative gaussian fct """ return (np.erf(x * 0.7071067811865475) * 0.5 + 0.5)
def derived_error_function(N, P): alpha = N / P return 1 / 2 * (1 - np.erf((1 + alpha) / math.sqrt(2 * alpha)))
def value(self, x): return np.erf(flattest(x)) def jacobian(self, x, into=None):
def V(x): return - (k/16) * np.exp(-8 * (x ** 2)) - ((e2/(2 * (8 ** 0.5))) * np.sqrt(np.pi)) * (np.erf( (8 ** 0.5)* (x - xb)) - np.erf((8 ** 0.5) *( x + xb)))