def J(x, p=p, extra_par={}): old_p = p.copy() p.update(extra_par) assert x >= 0 assert x <= p["L1"] wJ = sqrt((p["mJ"]) / p["DJ"]) wA = sqrt((p["mA"]) / p["DA"]) result = p["K"] * exp(wJ * (x - p["L1"])) / 2.0 * (1 - wA / wJ * tanh(wA * p["d"])) * ( 1 / (cosh(wJ * p["L1"]) + wA / wJ * tanh(wA * p["d"]) * sinh(wJ * p["L1"])) - 1.0 / (p["r"] * p["d"] * exp(-p["a"] * p["L1"])) ) + p["K"] * exp(-wJ * (x - p["L1"])) / 2.0 * (1 + wA / wJ * tanh(wA * p["d"])) * ( 1 / (cosh(wJ * p["L1"]) + wA / wJ * tanh(wA * p["d"]) * sinh(wJ * p["L1"])) - 1.0 / (p["r"] * p["d"] * exp(-p["a"] * p["L1"])) ) p.update(old_p) return result
def G(l, p=p, extra_par={}): old_p = p.copy() p.update(extra_par) wJ = sqrt((p["mJ"] + l) / p["DJ"]) wA = sqrt((p["mA"] + l) / p["DA"]) result = exp(l * p["t1"]) * (cosh(wJ * p["L1"]) + wA / wJ * tanh(wA * p["d"]) * sinh(wJ * p["L1"])) p.update(old_p) return result
def calc_fnu_mp(nu_M, a=0.707, p=0.3, A=1): """ Find f(nu) using equation A19 from Zhu et al. A19 is used to get the mass function, f(nu). Find A using int_0^inf fnu dnu = 1. They find 0.129. We find A=0.322? # need to integrate fnu from 0 -> inf and set equal to 1, which sets A. res2,_ = scipy.integrate.quad(calc_fnu, 0, np.inf) print res2 """ # normalization constant is A, assume 1 for now. an2 = a*nu_M return A*mp.sqrt(2 * an2 / pi) * (1 + an2**-p) * mp.exp(-0.5*an2)
def intA(p=p, extra_par={}): old_p = p.copy() p.update(extra_par) wJ = sqrt((p["mJ"]) / p["DJ"]) wA = sqrt((p["mA"]) / p["DA"]) result = ( sinh(wA * p["d"]) / wA / cosh(wA * p["d"]) * ( 1 / (cosh(wJ * p["L1"]) + wA / wJ * tanh(wA * p["d"]) * sinh(wJ * p["L1"])) - 1.0 / (p["r"] * p["d"] * exp(-p["a"] * p["L1"])) ) ) p.update(old_p) return result
def A(x, p=p, extra_par={}): old_p = p.copy() p.update(extra_par) assert x >= p["L1"] assert x <= p["L1"] + p["d"] wJ = sqrt((p["mJ"]) / p["DJ"]) wA = sqrt((p["mA"]) / p["DA"]) result = ( cosh(wA * (p["L1"] + p["d"] - x)) / cosh(wA * p["d"]) * ( 1 / (cosh(wJ * p["L1"]) + wA / wJ * tanh(wA * p["d"]) * sinh(wJ * p["L1"])) - 1.0 / (p["r"] * p["d"] * exp(-p["a"] * p["L1"])) ) ) p.update(old_p) return result
def objective(self, z, X, y): (N, _) = X.shape w = np.add(self.S.dot(z), self.m) # O(d^2) sigmoid = lambda x: float(1 / (1 + mpmath.exp(-float(x)))) # val = 0 grad = np.zeros((self.D, 1), dtype=float) for i in range(N): if sparse.issparse(X): x = X[i, :] else: x = np.array([X[i, :]]) t = x.dot(w) f = sigmoid(t) if sparse.issparse(X): grad = np.add(grad, (x * (y[i] - f)).toarray().T) else: grad = np.add(grad, np.array(x * (y[i] - f)).T) S_diag = self.S.diagonal().reshape((self.D, 1)) L = w / (np.multiply(S_diag, S_diag) + np.multiply(self.m, self.m)) grad -= L return 0, grad
def objective(self, z, X, y): (N,_) = X.shape w = np.add(self.S.dot(z), self.m) # O(d^2) sigmoid = lambda x: float(1/(1+mpmath.exp(-float(x)))) # val = 0 grad = np.zeros((self.D, 1), dtype=float) for i in range(N): if sparse.issparse(X): x = X[i, :] else: x = np.array([X[i, :]]) t = x.dot(w) f = sigmoid(t) if sparse.issparse(X): grad = np.add(grad, (x * (y[i] - f)).toarray().T) else: grad = np.add(grad, np.array(x * (y[i] -f)).T) S_diag = self.S.diagonal().reshape((self.D,1)) L = w / (np.multiply(S_diag, S_diag) + np.multiply(self.m, self.m)) grad -= L return 0, grad
def cramerVonMises(x, y): try: x = sorted(x); y = sorted(y); pool = x + y; ps, pr = _sortRank(pool) rx = array ( [ pr[ind] for ind in [ ps.index(element) for element in x ] ] ) ry = array ( [ pr[ind] for ind in [ ps.index(element) for element in y ] ] ) n = len(x) m = len(y) i = array(range(1, n+1)) j = array(range(1, m+1)) u = n * sum ( power( (rx - i), 2 ) ) + m * sum ( power((ry - j), 2) ) t = u / (n*m*(n+m)) - (4*n*m-1) / (6 * (n+m)) Tmu = 1/6 + 1 / (6*(n+m)) Tvar = 1/45 * ( (m+n+1) / power((m+n),2) ) * (4*m*n*(m+n) - 3*(power(m,2) + power(n,2)) - 2*m*n) / (4*m*n) t = (t - Tmu) / power(45*Tvar, 0.5) + 1/6 if t < 0: return -1 elif t <= 12: a = 1-mp.nsum(lambda x : ( mp.gamma(x+0.5) / ( mp.gamma(0.5) * mp.fac(x) ) ) * mp.power( 4*x + 1, 0.5 ) * mp.exp ( - mp.power(4*x + 1, 2) / (16*t) ) * mp.besselk(0.25 , mp.power(4*x + 1, 2) / (16*t) ), [0,100] ) / (mp.pi*mp.sqrt(t)) return float(mp.nstr(a,3)) else: return 0 except Exception as e: print e return -1
def F(x, p=p): return (1 - p['b'] * mp.exp(-x * p['t2'])) * mp.exp(x * p['t1']) \ * (mp.cosh(mp.sqrt((p['mJ'] + x)/p['DJ']) * p['L1']) \ + mp.sinh(mp.sqrt((p['mJ'] + x)/p['DJ']) * p['L1']) \ * mp.sqrt((p['mJ'] + x)/p['DJ']) / mp.sqrt((p['mA'] + x)/p['DA']) \ * mp.coth(mp.sqrt((p['mA'] + x)/p['DA']) * p['d'])) - p['a']
def _f(x): return mp.gamma(g+1)* \ np.power(x/mpf(2), -g) * \ mp.exp(1j*x) * mp.besselj(g, x)
def count_roots(p): """ Counts the number of roots of the polynomial object 'p' on the interior of the unit ball using an integral. """ return mp.quad(lambda z: mp.exp(1.0j * z) * p(mp.exp(1.0j * z)), [0., 2 * np.pi]) / (2 * np.pi)
def N(x, l, p=p): '''Incomplete!!''' return 2. * mp.sinh(sqrt((p['mJ'] + l)/p['DJ']) * p['L1']) \ * (mp.exp(mp.sqrt((p['mA'] + l)/p['DA']) * x) + \ mp.exp(mp.sqrt((p['mA'] + l)/p['DA']) * (2*p['L2'] - x)))
def voigt_slow(a, u): with mp.workdps(581): z = mp.mpc(u, a) result = mp.exp(-z * z) * mp.erfc(-1j * z) return result.real
def M(x, l, p=p): return mp.exp(mp.sqrt((p['mJ'] + l)/p['DJ']) * x) + \ ((1 - p['b'] * mp.exp(-x * p['t2'])) * mp.exp(x * p['t1']) - p['a'] * mp.exp(mp.sqrt((p['mJ'] + l)/p['DJ']) * p['L1'])) / \ ((1 - p['b'] * mp.exp(-x * p['t2'])) * mp.exp(x * p['t1']) - p['a'] * mp.exp(- mp.sqrt((p['mJ'] + l)/p['DJ']) * p['L1'])) \ * mp.exp(- mp.sqrt((p['mJ'] + l)/p['DJ']) * x)
from numpy import inf #from math import factorial from math import sqrt from math import pi from sympy import mpmath from scipy.integrate import quad from GaussHermiteQuadrature import GaussHermiteQuadrature as gaussquad #def H(n, x): # return hermval(x, [0]*n+[1]) #def hermite_weight(n, x): # return pow(2,n-1)*factorial(n)*sqrt(pi)/(pow(n,2)*pow(H(n-1, x), 2)) mu = 1 sigma = 2 sigmoid = lambda x: 1 / (1 + mpmath.exp(-x)) polynomial = lambda x: 2 * x**6 + 4 * x**9 + x + 2 exponential = lambda x: mpmath.exp(x) normalpdf = lambda x: 1 / (sqrt(2 * pi) * sigma) * mpmath.exp(-(x - mu)**2 / (2 * sigma**2)) #N = 10 #x,u = hermgauss(N) #z = x # # #hquad = 0 #for i in range(N): # w = hermite_weight(N, x[i]) # hquad += w * polynomial(sqrt(2)*sigma*x[i]+mu) #hquad /= sqrt(pi)
if __name__ == '__main__': p = np.poly1d([1, 0, 0, 0, 1]) # use mayavi if possible. # otherwise, fall back to matplotlib. if use_mayavi: plot_both_mayavi(p) nroot_real_mayavi(5) nroot_imag_mayavi(5) else: plot_both_matplotlib(p) nroot_real_matplotlib(5) nroot_imag_matplotlib(5) # All the different integration examples. f1 = lambda z: z.conjugate() f2 = lambda z: mp.exp(z) c1 = lambda t: mp.cos(t) + 1.0j * mp.sin(t) c2 = lambda t: t + 1.0j * t c3 = lambda t: t c4 = lambda t: 1 + 1.0j * t c5 = lambda t: mp.cos(t) + 1.0j + 1.0j * mp.sin(t) print "z conjugate counterclockwise along the unit ball starting and ending at 1." print contour_int(f1, c1, 0, 2 * np.pi) print "z conjugate along a straight line from 0 to 1+1j." print contour_int(f1, c2, 0, 1) print "z conjugate along the real axis from 0 to 1, then along the line from 1 to 1+1j." print contour_int(f1, c3, 0, 1) + contour_int(f1, c4, 0, 1) print "z conjugate along the unit ball centered at 1j from 0 to 1+1j." print contour_int(f1, c5, -np.pi / 2, 0.) print "e^z counterclockwise along the unit ball starting and ending at 1." print contour_int(f2, c1, 0, 2 * np.pi)
def precise(freq_, age_, log_a_0_): return float(mp.power(alpha + age_, r + freq_) * mp.exp(log_a_0_))
from numpy import inf #from math import factorial from math import sqrt from math import pi from sympy import mpmath from scipy.integrate import quad from GaussHermiteQuadrature import GaussHermiteQuadrature as gaussquad #def H(n, x): # return hermval(x, [0]*n+[1]) #def hermite_weight(n, x): # return pow(2,n-1)*factorial(n)*sqrt(pi)/(pow(n,2)*pow(H(n-1, x), 2)) mu = 1 sigma = 2 sigmoid = lambda x : 1/(1+mpmath.exp(-x)) polynomial = lambda x : 2*x**6+4*x**9+x+2 exponential = lambda x : mpmath.exp(x) normalpdf = lambda x : 1 / (sqrt(2*pi)*sigma) * mpmath.exp(-(x-mu)**2/(2*sigma**2)) #N = 10 #x,u = hermgauss(N) #z = x # # #hquad = 0 #for i in range(N): # w = hermite_weight(N, x[i]) # hquad += w * polynomial(sqrt(2)*sigma*x[i]+mu) #hquad /= sqrt(pi) #