def get_celerite_matrices(self, x, diag, *, a=None, U=None, V=None, P=None): dt = self.delta ar, cr, a, b, c, d = self.term.get_coefficients() # Real part cd = cr * dt delta_diag = 2 * np.sum(ar * (cd - np.sinh(cd)) / cd**2) # Complex part cd = c * dt dd = d * dt c2 = c**2 d2 = d**2 c2pd2 = c2 + d2 C1 = a * (c2 - d2) + 2 * b * c * d C2 = b * (c2 - d2) - 2 * a * c * d norm = (dt * c2pd2)**2 sinh = np.sinh(cd) cosh = np.cosh(cd) delta_diag += 2 * np.sum( (C2 * cosh * np.sin(dd) - C1 * sinh * np.cos(dd) + (a * c + b * d) * dt * c2pd2) / norm) new_diag = diag + delta_diag return super().get_celerite_matrices(x, new_diag, a=a, U=U, V=V, P=P)
def get_value(self, tau0): dt = self.delta ar, cr, a, b, c, d = self.term.get_coefficients() # Format the lags correctly tau0 = np.abs(np.atleast_1d(tau0)) tau = tau0[..., None] # Precompute some factors dpt = dt + tau dmt = dt - tau # Real parts: # tau > Delta crd = cr * dt cosh = np.cosh(crd) norm = 2 * ar / crd**2 K_large = np.sum(norm * (cosh - 1) * np.exp(-cr * tau), axis=-1) # tau < Delta crdmt = cr * dmt K_small = K_large + np.sum(norm * (crdmt - np.sinh(crdmt)), axis=-1) # Complex part cd = c * dt dd = d * dt c2 = c**2 d2 = d**2 c2pd2 = c2 + d2 C1 = a * (c2 - d2) + 2 * b * c * d C2 = b * (c2 - d2) - 2 * a * c * d norm = 1.0 / (dt * c2pd2)**2 k0 = np.exp(-c * tau) cdt = np.cos(d * tau) sdt = np.sin(d * tau) # For tau > Delta cos_term = 2 * (np.cosh(cd) * np.cos(dd) - 1) sin_term = 2 * (np.sinh(cd) * np.sin(dd)) factor = k0 * norm K_large += np.sum((C1 * cos_term - C2 * sin_term) * factor * cdt, axis=-1) K_large += np.sum((C2 * cos_term + C1 * sin_term) * factor * sdt, axis=-1) # tau < Delta edmt = np.exp(-c * dmt) edpt = np.exp(-c * dpt) cos_term = (edmt * np.cos(d * dmt) + edpt * np.cos(d * dpt) - 2 * k0 * cdt) sin_term = (edmt * np.sin(d * dmt) + edpt * np.sin(d * dpt) - 2 * k0 * sdt) K_small += np.sum(2 * (a * c + b * d) * c2pd2 * dmt * norm, axis=-1) K_small += np.sum((C1 * cos_term + C2 * sin_term) * norm, axis=-1) mask = tau0 >= dt return K_large * mask + K_small * (~mask)
def evolution_pepo_imag_time(g: float, dt: float, bc: str, dtype: np.dtype, lx: Optional[int] = None, ly: Optional[int] = None) -> Operator: # PEPO for U(dt) ~ U_vert(dt/2) U_bond(dt) U_vert(dt/2) # # half bond operators: # # | | | | # U_bond = A -- A # | | | | # # expm(- H_bond dt) = expm(- (-XX) dt) = expm(dt XX) = cosh(dt) + sinh(dt) XX = A_0 A_0 + A_1 A_1 # with A_0 = (cosh(dt) ** 0.5) * 1 , A_1 = (sinh(dt) ** 0.5) * X # A & B legs: (p,p*,k) A = np.empty([2, 2, 2], dtype=dtype) A = index_update(A, index[:, :, 0], (np.cosh(dt)**0.5) * s0) A = index_update(A, index[:, :, 1], (np.sinh(dt)**0.5) * sx) # expm(- H_vert dt/2) = expm(- (-gZ) dt/2) = expm(g dt/2 Z) u_vert = np.asarray(expm(g * (dt / 2) * sz), dtype=dtype) return _build_evolution_pepo(u_vert, A, bc, lx, ly)
def get_coefficients(self): ar, cr, a, b, c, d = self.term.get_coefficients() # Real componenets crd = cr * self.delta coeffs = [2 * ar * (np.cosh(crd) - 1) / crd**2, cr] # Imaginary coefficients cd = c * self.delta dd = d * self.delta c2 = c**2 d2 = d**2 factor = 2.0 / (self.delta * (c2 + d2))**2 cos_term = np.cosh(cd) * np.cos(dd) - 1 sin_term = np.sinh(cd) * np.sin(dd) C1 = a * (c2 - d2) + 2 * b * c * d C2 = b * (c2 - d2) - 2 * a * c * d coeffs += [ factor * (C1 * cos_term - C2 * sin_term), factor * (C2 * cos_term + C1 * sin_term), c, d, ] return coeffs
def logf(r, sig, p): first = -jnp.sum(r**2) / (2 * sig**2) second = jnp.sum( jnp.log( jnp.sinh(jnp.abs(r[:, None] - r[None, :]))[jnp.tril_indices( p, k=-1)])) return first + second
def ionic_flux_phi(self, j, u, T, phis, phie, cM, cMp, cmax): cs = (cM + cMp) / 2 # cs = self.cstar(cM,cMp,j,T) keff = self.k * np.exp((-self.Ek / R) * ((1 / T) - (1 / Tref))) ans = j - 2 * keff * np.sqrt(u * (cmax - cs) * cs) * np.sinh( (0.5 * F / (R * T)) * (self.over_poten_exp(phis, phie, T, cM, cMp, cmax))) return ans.reshape()
def doublesoliton(x, t, c, x0): # assert c[0] > c[1], "c1 has to be bigger than c[2]" xi0 = (jnp.sqrt(c[0]) / 2 * (x - c[0] * t - x0[0]) ) # switch to moving coordinate frame xi1 = jnp.sqrt(c[1]) / 2 * (x - c[1] * t - x0[1]) part_1 = 2 * (c[0] - c[1]) numerator = c[0] * jnp.cosh(xi1)**2 + c[1] * jnp.sinh(xi0)**2 denominator_1 = (jnp.sqrt(c[0]) - jnp.sqrt(c[1])) * jnp.cosh(xi0 + xi1) denominator_2 = (jnp.sqrt(c[0]) + jnp.sqrt(c[1])) * jnp.cosh(xi0 - xi1) u = part_1 * numerator / (denominator_1 + denominator_2)**2 return u
def transverse_comoving_distance(cosmo, a): r"""Transverse comoving distance in [Mpc/h] for a given scale factor. Parameters ---------- a : array_like Scale factor Returns ------- f_k : ndarray, or float if input scalar Transverse comoving distance corresponding to the specified scale factor. Notes ----- The transverse comoving distance depends on the curvature of the universe and is related to the radial comoving distance through: .. math:: f_k(a) = \left\lbrace \begin{matrix} R_H \frac{1}{\sqrt{\Omega_k}}\sinh(\sqrt{|\Omega_k|}\chi(a)R_H)& \mbox{for }\Omega_k > 0 \\ \chi(a)& \mbox{for } \Omega_k = 0 \\ R_H \frac{1}{\sqrt{\Omega_k}} \sin(\sqrt{|\Omega_k|}\chi(a)R_H)& \mbox{for } \Omega_k < 0 \end{matrix} \right. """ chi = radial_comoving_distance(cosmo, a) if cosmo.k < 0: # Open universe return const.rh / cosmo.sqrtk * np.sinh(cosmo.sqrtk * chi / const.rh) elif cosmo.k > 0: # Closed Universe return const.rh / cosmo.sqrtk * np.sin(cosmo.sqrtk * chi / const.rh) else: return chi
def hyperbolic_sin(x: Array) -> Array: """Hyperbolic sinus transform.""" chex.assert_type(x, float) return jnp.sinh(x)
def ionic_flux(self, u, T, eta, cs, cmax): keff = self.k * np.exp((-self.Ek / R) * ((1 / T) - (1 / Tref))) ans = 2 * keff * np.sqrt(u * (cmax - cs) * cs) * np.sinh( (0.5 * R / (F * T)) * (eta)) return ans.reshape()
def free_energy(L, T): assert L % 2 == 0, "L has to be even." return -jnp.real((0.5 - 1. / L**2) * jnp.log(2) + 0.5 * jnp.log(jnp.sinh(2. / T)) + get_log_Z_sum(L, T) / L**2)
def sinh(x): if isinstance(x, JaxArray): x = x.value return JaxArray(jnp.sinh(x))
def _sinh(x): return jnp.sinh(x)
Atemp = np.transpose(Atemp, (3, 0, 1, 2)) U2, V2 = construct_uv(Atemp, chiM) Atemp = ncon([V1, U2, V2, U1], [[1, 4, -1], [2, 1, -2], [4, 3, -4], [3, 2, -3]]) # extract result print('sizeA = ', Atemp.shape, ', RGstep = ', k + 1) lnz += np.log(ncon([Atemp], [[1, 2, 1, 2]])) / 2**(RGstep + 1) return lnz print('Calculate lnz') lnz = lnz_trg(betaval, chiM, log2L) Tval = 1 / betaval FreeEnergy = -Tval * lnz ##### Compare with exact results (thermodynamic limit) maglambda = 1 / (np.sinh(2 / Tval)**2) N = 1000000 x = np.linspace(0, np.pi, N + 1) y = np.log( np.cosh(2 * betaval) * np.cosh(2 * betaval) + (1 / maglambda) * np.sqrt(1 + maglambda**2 - 2 * maglambda * np.cos(2 * x))) FreeExact = -Tval * ((np.log(2) / 2) + 0.25 * sum(y[1:(N + 1)] + y[:N]) / N) RelFreeErr = abs((FreeEnergy - FreeExact) / FreeExact) print('FreeEnergy= %.5f' % FreeEnergy) print('Exact = ', FreeExact) print('RelFreeErr = ', RelFreeErr) print('Calculate dlnz') dlnz = grad(lnz_trg, argnums=0)(betaval, chiM, log2L) #dlnz = grad(lnz_trg)(betaval) print('dlnz: %.5f' % dlnz)
x0 = 0. xf = 3. nCEs = 8 colors = qual.Plotly # Constrained expressions: myTfc = utfc(n,nC,m,x0=x0,xf=xf) t = myTfc.x H = myTfc.H g = lambda xi,t: np.dot(H(t),xi['xi']) xslow = lambda xi,t: g(xi,t)\ +(3.-t)/3.*(aEll*np.sin(xi['phi'])*np.cos(xi['th'])-g(xi,np.array([0.])))\ +t/3.*(3.+aHyp*np.sinh(xi['v'])*np.cos(xi['psi'])-g(xi,np.array([3.]))) yslow = lambda xi,t: g(xi,t)\ +(3.-t)/3.*(bEll*np.sin(xi['phi'])*np.sin(xi['th'])-g(xi,np.array([0.])))\ +t/3.*(bHyp*np.sinh(xi['v'])*np.sin(xi['psi'])-g(xi,np.array([3.]))) zslow = lambda xi,t: g(xi,t)\ +(3.-t)/3.*(cEll*np.cos(xi['phi'])-g(xi,np.array([0.])))\ +t/3.*((-1.)**step(xi['n'])*cHyp*np.cosh(xi['v'])-g(xi,np.array([3.]))) x = jit(xslow); y = jit(yslow); z = jit(zslow) # Plot conic sections th = np.linspace(0.,2.*np.pi,100) v = np.linspace(0.,3.0,100) matHyp = np.meshgrid(v,th) phi = np.linspace(0.,np.pi,100)
def CpIG(self, T): T = jnp.squeeze(T) return (self.CpIGA + self.CpIGB * (self.CpIGC / T / jnp.sinh(self.CpIGC / T))**2 + self.CpIGD * (self.CpIGE / T / jnp.cosh(self.CpIGE / T))**2)
# segment_prod = utils.copy_docstring( # tf.math.segment_prod, # lambda data, segment_ids, name=None: np.segment_prod) # segment_sum = utils.copy_docstring( # tf.math.segment_sum, # lambda data, segment_ids, name=None: np.segment_sum) sigmoid = utils.copy_docstring(tf.math.sigmoid, lambda x, name=None: scipy_special.expit(x)) sign = utils.copy_docstring(tf.math.sign, lambda x, name=None: np.sign(x)) sin = utils.copy_docstring(tf.math.sin, lambda x, name=None: np.sin(x)) sinh = utils.copy_docstring(tf.math.sinh, lambda x, name=None: np.sinh(x)) softmax = utils.copy_docstring(tf.math.softmax, _softmax) softplus = utils.copy_docstring( tf.math.softplus, lambda x, name=None: np.log1p(np.exp(-np.abs(x))) + np.maximum(x, 0.)) softsign = utils.copy_docstring( tf.math.softsign, lambda features, name=None: np.divide(features, (np.abs(features) + 1))) sqrt = utils.copy_docstring(tf.math.sqrt, lambda x, name=None: np.sqrt(x)) square = utils.copy_docstring(tf.math.square, lambda x, name=None: np.square(x))