我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.cosh()。
def quadrf(ev, y): L = ev[1] # length k = ev[4] # quadrupole strength if k == 0: R = drift(ev, y) else: wrzlk = sqrt(abs(k)) Omega = wrzlk*L coshom = cosh(Omega) sinhom = sinh(Omega) cosom = cos(Omega) sinom = sin(Omega) R = array([ [cosom, sinom/wrzlk, 0., 0., 0., 0.], [-wrzlk*sinom, cosom, 0., 0., 0., 0.], [0., 0., coshom, sinhom/wrzlk, 0., 0.], [0., 0., wrzlk*sinhom, coshom, 0., 0.], [0., 0., 0., 0., 1., L/(y**2)], [0., 0., 0., 0., 0., 1.] ]) return R
def quadaf(ev, y): L = ev[1] # length k = ev[4] # quadrupole strength if k == 0: R = drift(ev, y) else: wrzlk = sqrt(abs(k)) Omega = wrzlk*L coshom = cosh(Omega) sinhom = sinh(Omega) cosom = cos(Omega) sinom = sin(Omega) R = array([ [coshom, sinhom/wrzlk, 0., 0., 0., 0], [wrzlk*sinhom, coshom, 0., 0., 0., 0], [0., 0., cosom, sinom/wrzlk, 0., 0], [0., 0., -wrzlk*sinom, cosom, 0., 0], [0., 0., 0., 0., 1., L/(y**2)], [0., 0., 0., 0., 0., 1.] ]) return R
def _setup(self): self.xlimits[:, 0] = -1. self.xlimits[:, 1] = 1. a = self.options['width'] if self.options['func'] == 'cos': self.func = lambda v: np.cos(a * np.pi * v) self.dfunc = lambda v: -a * np.pi * np.sin(a * np.pi * v) elif self.options['func'] == 'exp': self.func = lambda v: np.exp(a * v) self.dfunc = lambda v: a * np.exp(a * v) elif self.options['func'] == 'tanh': self.func = lambda v: np.tanh(a * v) self.dfunc = lambda v: a / np.cosh(a * v) ** 2 elif self.options['func'] == 'gaussian': self.func = lambda v: np.exp(-2. * a * v ** 2) self.dfunc = lambda v: -4. * a * v * np.exp(-2. * a * v ** 2)
def potentialFunction(self, x): naturalPotential = self.De * (1 - np.exp(-self.a * (x - self.center)))**2 + self.energyOffset imaginaryPotential = 0 try: imaginaryPotentialZeros = np.zeros(x.shape) except: if x > self.startOfAbsorbingPotential: imaginaryPotential = -1.0j *self.strength * np.cosh( (np.real(x) - self.mySpace.xMax)**2 / (self.mySpace.Dx*30)**2)**(-2) else: imaginaryPotential = 0 return naturalPotential + imaginaryPotential if self.absorbingPotential: imaginaryPotential = -1.0j *self.strength * np.cosh( (np.real(x) - self.mySpace.xMax)**2 / (self.mySpace.Dx*30)**2)**(-2) imaginaryPotential = np.where(x > self.startOfAbsorbingPotential, imaginaryPotential, imaginaryPotentialZeros) return naturalPotential + imaginaryPotential
def _transform_dense(self, X): non_zero = (X != 0.0) X_nz = X[non_zero] X_step = np.zeros_like(X) X_step[non_zero] = np.sqrt(X_nz * self.sample_interval_) X_new = [X_step] log_step_nz = self.sample_interval_ * np.log(X_nz) step_nz = 2 * X_nz * self.sample_interval_ for j in range(1, self.sample_steps): factor_nz = np.sqrt(step_nz / np.cosh(np.pi * j * self.sample_interval_)) X_step = np.zeros_like(X) X_step[non_zero] = factor_nz * np.cos(j * log_step_nz) X_new.append(X_step) X_step = np.zeros_like(X) X_step[non_zero] = factor_nz * np.sin(j * log_step_nz) X_new.append(X_step) return np.hstack(X_new)
def Sign(**kwargs): ''' Algorithm 1, Pg 12 of BLISS paper o/p: z,c ''' msg, A, S, m, n, sd, q, M, kappa = kwargs['msg'], kwargs['A'], kwargs['S'], kwargs['m'], kwargs['n'], kwargs['sd'], kwargs['q'], kwargs['M'], kwargs['kappa'] m_bar = m + n D = DiscreteGaussianDistributionLatticeSampler(ZZ**m_bar, sd) count = 0 while(True): y = np.array(D()) # m' x 1 reduced_Ay = util.vector_to_Zq(np.matmul(A, y), 2*q) c = hash_iterative(np.array_str(reduced_Ay) + msg, n, kappa) # still not the hash but this is test run b = util.crypt_secure_randint(0, 1) Sc = np.matmul(S,c) z = y + ((-1)**b) * Sc try: exp_term = exp(float(Sc.dot(Sc)) / (2*sd**2)) cosh_term = np.cosh(float(z.dot(Sc)) / (sd**2)) val = exp_term / (cosh_term * M) except OverflowError: print "OF" continue if(random.random() < min(val, 1.0)): break if(count > 10): # beyond 4 rejection sampling iterations are not expected in general raise ValueError("The number of rejection sampling iterations are more than expected") count += 1 return z, c
def gen_samples(self, num_samples): """Generate sample for ML near the snake.""" points = [] # the sample points labels = [] # the labels whichs = [] # the corresponding node for the sample deri_g = [] # the partial derivative to g deri_T = [] # the partial derivative to T counter = 0 assert num_samples % self.length == 0 for i, (v, n) in enumerate(zip(self.vertices, self.normals())): for d in np.linspace(-1, 1, num_samples / self.length): # geometry r = 2 * self.widths[i] * d s = v + r * n l = array([0.5 * (1. - np.tanh(d)), 0.5 * (1. + np.tanh(d))]) points.append(s) labels.append(l) whichs.append(i) # cal derivatives cosh_d = np.cosh(d) deri_g.append(1 / (4 * self.widths[i] * cosh_d * cosh_d)) deri_T.append(d / (2 * self.widths[i] * cosh_d * cosh_d)) counter += 1 if counter == num_samples: return array(points), array(labels), array(whichs), array(deri_g), array(deri_T)
def test_basic_ufuncs(self): # Test various functions such as sin, cos. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d assert_equal(np.cos(x), cos(xm)) assert_equal(np.cosh(x), cosh(xm)) assert_equal(np.sin(x), sin(xm)) assert_equal(np.sinh(x), sinh(xm)) assert_equal(np.tan(x), tan(xm)) assert_equal(np.tanh(x), tanh(xm)) assert_equal(np.sqrt(abs(x)), sqrt(xm)) assert_equal(np.log(abs(x)), log(xm)) assert_equal(np.log10(abs(x)), log10(xm)) assert_equal(np.exp(x), exp(xm)) assert_equal(np.arcsin(z), arcsin(zm)) assert_equal(np.arccos(z), arccos(zm)) assert_equal(np.arctan(z), arctan(zm)) assert_equal(np.arctan2(x, y), arctan2(xm, ym)) assert_equal(np.absolute(x), absolute(xm)) assert_equal(np.angle(x + 1j*y), angle(xm + 1j*ym)) assert_equal(np.angle(x + 1j*y, deg=True), angle(xm + 1j*ym, deg=True)) assert_equal(np.equal(x, y), equal(xm, ym)) assert_equal(np.not_equal(x, y), not_equal(xm, ym)) assert_equal(np.less(x, y), less(xm, ym)) assert_equal(np.greater(x, y), greater(xm, ym)) assert_equal(np.less_equal(x, y), less_equal(xm, ym)) assert_equal(np.greater_equal(x, y), greater_equal(xm, ym)) assert_equal(np.conjugate(x), conjugate(xm))
def test_testUfuncRegression(self): # Tests new ufuncs on MaskedArrays. for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', 'absolute', 'fabs', 'negative', 'floor', 'ceil', 'logical_not', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod', 'hypot', 'arctan2', 'equal', 'not_equal', 'less_equal', 'greater_equal', 'less', 'greater', 'logical_and', 'logical_or', 'logical_xor', ]: try: uf = getattr(umath, f) except AttributeError: uf = getattr(fromnumeric, f) mf = getattr(numpy.ma.core, f) args = self.d[:uf.nin] ur = uf(*args) mr = mf(*args) assert_equal(ur.filled(0), mr.filled(0), f) assert_mask_equal(ur.mask, mr.mask, err_msg=f)
def test_testUfuncs1(self): # Test various functions such as sin, cos. (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d self.assertTrue(eq(np.cos(x), cos(xm))) self.assertTrue(eq(np.cosh(x), cosh(xm))) self.assertTrue(eq(np.sin(x), sin(xm))) self.assertTrue(eq(np.sinh(x), sinh(xm))) self.assertTrue(eq(np.tan(x), tan(xm))) self.assertTrue(eq(np.tanh(x), tanh(xm))) with np.errstate(divide='ignore', invalid='ignore'): self.assertTrue(eq(np.sqrt(abs(x)), sqrt(xm))) self.assertTrue(eq(np.log(abs(x)), log(xm))) self.assertTrue(eq(np.log10(abs(x)), log10(xm))) self.assertTrue(eq(np.exp(x), exp(xm))) self.assertTrue(eq(np.arcsin(z), arcsin(zm))) self.assertTrue(eq(np.arccos(z), arccos(zm))) self.assertTrue(eq(np.arctan(z), arctan(zm))) self.assertTrue(eq(np.arctan2(x, y), arctan2(xm, ym))) self.assertTrue(eq(np.absolute(x), absolute(xm))) self.assertTrue(eq(np.equal(x, y), equal(xm, ym))) self.assertTrue(eq(np.not_equal(x, y), not_equal(xm, ym))) self.assertTrue(eq(np.less(x, y), less(xm, ym))) self.assertTrue(eq(np.greater(x, y), greater(xm, ym))) self.assertTrue(eq(np.less_equal(x, y), less_equal(xm, ym))) self.assertTrue(eq(np.greater_equal(x, y), greater_equal(xm, ym))) self.assertTrue(eq(np.conjugate(x), conjugate(xm))) self.assertTrue(eq(np.concatenate((x, y)), concatenate((xm, ym)))) self.assertTrue(eq(np.concatenate((x, y)), concatenate((x, y)))) self.assertTrue(eq(np.concatenate((x, y)), concatenate((xm, y)))) self.assertTrue(eq(np.concatenate((x, y, x)), concatenate((x, ym, x))))
def test_testUfuncRegression(self): f_invalid_ignore = [ 'sqrt', 'arctanh', 'arcsin', 'arccos', 'arccosh', 'arctanh', 'log', 'log10', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod'] for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', 'absolute', 'fabs', 'negative', 'floor', 'ceil', 'logical_not', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod', 'hypot', 'arctan2', 'equal', 'not_equal', 'less_equal', 'greater_equal', 'less', 'greater', 'logical_and', 'logical_or', 'logical_xor']: try: uf = getattr(umath, f) except AttributeError: uf = getattr(fromnumeric, f) mf = getattr(np.ma, f) args = self.d[:uf.nin] with np.errstate(): if f in f_invalid_ignore: np.seterr(invalid='ignore') if f in ['arctanh', 'log', 'log10']: np.seterr(divide='ignore') ur = uf(*args) mr = mf(*args) self.assertTrue(eq(ur.filled(0), mr.filled(0), f)) self.assertTrue(eqmask(ur.mask, mr.mask))
def cosh(x: Number = 0.0) -> Number: return np.cosh(x)
def tanh(x, y): if y == 0: return (2 / (1 + np.exp(-2*x))) - 1 else: return 1 / ((np.cosh(x))**2)
def poisson_from_positiveK(mean): # solve x/(1 - exp(-x)) == mean def f(x): return x/(1. - np.exp(-x)) def f1(x): return (np.expm1(x) - x)/(2.*np.cosh(x) - 2.) x = solve(mean, f, f1, mean, n=10) return x
def poisson_from_positiveK(mean): # solve x/(1 - exp(-x)) == mean def f(x): return x/(1. - np.exp(-x)) def f1(x): return (np.expm1(x) - x)/(2.*np.cosh(x) - 2.) x = solve_newton(mean, f, f1, mean, n=10) return x
def cosh(v): return v.__class__(numpy.cosh(v))
def cir_int_rt_chf(u, t, k, theta, sigma, r0): r = np.sqrt(k ** 2 - 1j * u * 2 * sigma ** 2) cosh_fun = np.cosh(r * t / 2) sinh_fun = np.sinh(r * t / 2) coth_fun = cosh_fun / sinh_fun a_t_v = np.exp(t * theta * (k ** 2) / (sigma ** 2)) / (cosh_fun + (k / r) * sinh_fun) ** ( 2 * k * theta / (sigma ** 2)) b_t_v = 2 * 1j * u / (k + r * coth_fun) return a_t_v * np.exp(b_t_v * r0)
def dfdx(self, x): sech_aR = 1.0 / np.cosh(self.a * (x - self.R0)) return self.V0*self.a*sech_aR*sech_aR
def d2fdx2(self, x): sech_aR = 1.0 / np.cosh(self.a * (x - self.R0)) tanh_aR = np.tanh(self.a * (x-self.R0)) return -2.0 * self.V0 * self.a * self.a * sech_aR * sech_aR * tanh_aR
def __init__(self, generator: Generator=Autoincrement()): super().__init__(numpy.cosh, generator)
def test_cosh(): fun = lambda x : 3.0 * np.cosh(x) d_fun = grad(fun) check_grads(fun, npr.randn()) check_grads(d_fun, npr.randn())
def integrate_fip(p, v, z, dt, omega2): """ Integrate the equation of motion of the Floating-base Inverted Pendulum. Parameters ---------- p : array, shape=(3,) Initial position. v : array, shape=(3,) Initial velocity. z : array, shape=(3,) ZMP location throughout the integration. dt : scalar Integration step. omega2 : scalar FIP constant. Returns ------- p_next : array, shape=(3,) Position at the end of the integration step. v_next : array, shape=(3,) Velocity at the end of the integration step. Note ---- The Linear Inverted Pendulum Mode (LIPM) is a special case of the FIP, so this function also applies to COP-based controllers. """ omega = sqrt(omega2) a = omega2 * (p - z) + gravity p_next = p + v / omega * sinh(omega * dt) \ + a / omega2 * (cosh(omega * dt) - 1.) v_next = v * cosh(omega * dt) + a / omega * sinh(omega * dt) return p_next, v_next
def evaluate(self, x): return numpy.cosh(x)
def cosh(x): return Apply(Cosh(), x)
def __init__(self, kp, kd, kv, kw, N, u_limit=[100, 100], nn_limit=[100, 100]): """ kp: Proportional feedback gains. kd: Derivative feedback gains. kv: Input-side learning gain (scalar). kw: Output-side learning gain (scalar). N: Number of neurons. u_limit: Limit on total output effort. nn_limit: Limit on NN component feedforward effort. The user must actively set self.learn = True to have the NN start learning. """ self.nstates = 2 * len(kp) self.ncontrols = len(kp) self.nsigs = N self.sig = lambda x: np.concatenate(([1], np.tanh(x))) self.sigp = lambda x: np.tile(1/(np.cosh(x)**2), (self.nsigs+1, 1)) self.set_gains(kp, kd, kv, kw) self.u_limit = np.array(u_limit, dtype=np.float32) self.nn_limit = np.array(nn_limit, dtype=np.float32) self.V = np.zeros((self.nstates+1, self.nsigs)) self.W = np.zeros((self.nsigs+1, self.ncontrols)) self.y = np.zeros(self.ncontrols) self.saturated = False self.learn = False ########################
def test_testUfuncRegression(self): # Tests new ufuncs on MaskedArrays. for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', 'absolute', 'fabs', 'negative', # 'nonzero', 'around', 'floor', 'ceil', # 'sometrue', 'alltrue', 'logical_not', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod', 'hypot', 'arctan2', 'equal', 'not_equal', 'less_equal', 'greater_equal', 'less', 'greater', 'logical_and', 'logical_or', 'logical_xor', ]: try: uf = getattr(umath, f) except AttributeError: uf = getattr(fromnumeric, f) mf = getattr(numpy.ma.core, f) args = self.d[:uf.nin] ur = uf(*args) mr = mf(*args) assert_equal(ur.filled(0), mr.filled(0), f) assert_mask_equal(ur.mask, mr.mask, err_msg=f)
def test_testUfuncRegression(self): f_invalid_ignore = [ 'sqrt', 'arctanh', 'arcsin', 'arccos', 'arccosh', 'arctanh', 'log', 'log10', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod'] for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', 'absolute', 'fabs', 'negative', # 'nonzero', 'around', 'floor', 'ceil', # 'sometrue', 'alltrue', 'logical_not', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod', 'hypot', 'arctan2', 'equal', 'not_equal', 'less_equal', 'greater_equal', 'less', 'greater', 'logical_and', 'logical_or', 'logical_xor']: try: uf = getattr(umath, f) except AttributeError: uf = getattr(fromnumeric, f) mf = getattr(np.ma, f) args = self.d[:uf.nin] with np.errstate(): if f in f_invalid_ignore: np.seterr(invalid='ignore') if f in ['arctanh', 'log', 'log10']: np.seterr(divide='ignore') ur = uf(*args) mr = mf(*args) self.assertTrue(eq(ur.filled(0), mr.filled(0), f)) self.assertTrue(eqmask(ur.mask, mr.mask))
def cosh(self): out = copy.copy(self) out.surface = np.cosh(out.surface) return out
def jac_scalar_scalar(self, x): return np.cosh(x)
def _f1(self, z): """ Calculate function f1 from Hellstrom (1991) """ f1 = np.exp(self._beta*z)*(np.cosh(self._gamma*z) - self._delta*np.sinh(self._gamma*z)) return f1
def _f3(self, z): """ Calculate function f3 from Hellstrom (1991) """ f3 = np.exp(self._beta*z)*(np.cosh(self._gamma*z) + self._delta*np.sinh(self._gamma*z)) return f3
def _f4(self, z): """ Calculate function f4 from Hellstrom (1991) """ A = self._delta*self._beta1 + self._beta2*self._beta12/self._gamma f4 = np.exp(self._beta*z) \ * (self._beta1*np.cosh(self._gamma*z) - A*np.sinh(self._gamma*z)) return f4
def _f5(self, z): """ Calculate function f5 from Hellstrom (1991) """ B = self._delta*self._beta2 + self._beta1*self._beta12/self._gamma f5 = np.exp(self._beta*z) \ * (self._beta2*np.cosh(self._gamma*z) + B*np.sinh(self._gamma*z)) return f5
def _F4(self, z): """ Calculate integral of function f4 from Hellstrom (1991) """ A = self._delta*self._beta1 + self._beta2*self._beta12/self._gamma C = self._beta1*self._beta + A*self._gamma S = - (self._beta1*self._gamma + self._beta*A) denom = (self._beta**2 - self._gamma**2) F4 = np.exp(self._beta*z) / denom \ * (C*np.cosh(self._gamma*z) + S*np.sinh(self._gamma*z)) return F4
def tot_mom(jet_csts): """Jet momentum calculated from constituent 4-vectors.""" E_tot = np.sum(jet_csts['ET'] * np.cosh(jet_csts['eta'])) px_tot = np.sum(jet_csts['ET'] * np.cos(jet_csts['phi'])) py_tot = np.sum(jet_csts['ET'] * np.sin(jet_csts['phi'])) pz_tot = np.sum(jet_csts['ET'] * np.sinh(jet_csts['eta'])) return E_tot, px_tot, py_tot, pz_tot