本文整理汇总了Python中sympy.sqrt方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.sqrt方法的具体用法?Python sympy.sqrt怎么用?Python sympy.sqrt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.sqrt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_construct_lazy
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def test_construct_lazy(self):
# adapted from https://gist.github.com/raddy/bd0e977dc8437a4f8276
spot, strike, vol, dte, rate, cp = sy.symbols('spot strike vol dte rate cp')
T = dte / 260.
N = syNormal('N', 0.0, 1.0)
d1 = (sy.ln(spot / strike) + (0.5 * vol ** 2) * T) / (vol * sy.sqrt(T))
d2 = d1 - vol * sy.sqrt(T)
TimeValueExpr = sy.exp(-rate * T) * (cp * spot * cdf(N)(cp * d1) - cp * strike * cdf(N)(cp * d2))
PriceClass = ts.construct_lazy(TimeValueExpr)
price = PriceClass(spot=210.59, strike=205, vol=14.04, dte=4, rate=.2175, cp=-1)
x = price.evaluate()()
assert price.evaluate()() == x
price.strike = 210
assert x != price.evaluate()()
示例2: main
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def main():
Eprint()
X = (x,y,z) = symbols('x y z',real=True)
(o3d,ex,ey,ez) = Ga.build('e_x e_y e_z',g=[1,1,1],coords=(x,y,z))
A = x*(ey^ez) + y*(ez^ex) + z*(ex^ey)
print('A =', A)
print('grad^A =',(o3d.grad^A).simplify())
print()
f = o3d.mv(1/sqrt(x**2 + y**2 + z**2))
print('f =', f)
print('grad*f =',(o3d.grad*f).simplify())
print()
B = f*A
print('B =', B)
print()
Curl_B = o3d.grad^B
print('grad^B =', Curl_B.simplify())
return
示例3: compute_params_for_ss_release
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def compute_params_for_ss_release(eps, delta):
"""Computes sigma for additive Gaussian noise scaled by smooth sensitivity.
Presently not used. (We proceed via RDP analysis.)
Compute beta, sigma for applying Lemma 2.6 (full version of Nissim et al.) via
Lemma 2.10.
"""
# Rather than applying Lemma 2.10 directly, which would give suboptimal alpha,
# (see http://www.cse.psu.edu/~ads22/pubs/NRS07/NRS07-full-draft-v1.pdf),
# we extract a sufficient condition on alpha from its proof.
#
# Let a = rho_(delta/2)(Z_1). Then solve for alpha such that
# 2 alpha a + alpha^2 = eps/2.
a = scipy.special.ndtri(1 - delta / 2)
alpha = math.sqrt(a**2 + eps / 2) - a
beta = eps / (2 * scipy.special.chdtri(1, delta / 2))
return alpha, beta
#######################################################
# SYMBOLIC-NUMERIC VERIFICATION OF CONDITIONS C5--C6. #
#######################################################
示例4: test_call_sim_with_args
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def test_call_sim_with_args():
a, a_unc, b, b_unc = 3.0, 0.1, 4.0, 0.1
c = f_hypotenuse(a, b)
m1 = PythagorasModel()
data = {'PythagorasData': {'a': a, 'b': b, 'a_unc': a_unc, 'b_unc': b_unc}}
m1.command('run', data=data)
assert m1.registries['outputs']['c'].m == c
assert m1.registries['outputs']['c'].u == UREG.cm
x, y = sympy.symbols('x, y')
z = sympy.sqrt(x * x + y * y)
fx = sympy.lambdify((x, y), z.diff(x))
fy = sympy.lambdify((x, y), z.diff(y))
dz = np.sqrt(fx(a, b) ** 2 * a_unc ** 2 + fy(a, b) ** 2 * b_unc ** 2)
c_unc = c * np.sqrt(m1.registries['outputs'].variance['c']['c'])
LOGGER.debug('uncertainty in c is %g', c_unc)
assert np.isclose(dz, c_unc.item())
c_unc = c * m1.registries['outputs'].uncertainty['c']['c'].to('fraction')
assert np.isclose(dz, c_unc.m.item())
return m1
示例5: test_tensorproduct
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def test_tensorproduct():
a = BosonOp("a")
b = BosonOp("b")
ket1 = TensorProduct(BosonFockKet(1), BosonFockKet(2))
ket2 = TensorProduct(BosonFockKet(0), BosonFockKet(0))
ket3 = TensorProduct(BosonFockKet(0), BosonFockKet(2))
bra1 = TensorProduct(BosonFockBra(0), BosonFockBra(0))
bra2 = TensorProduct(BosonFockBra(1), BosonFockBra(2))
assert qapply(TensorProduct(a, b ** 2) * ket1) == sqrt(2) * ket2
assert qapply(TensorProduct(a, Dagger(b) * b) * ket1) == 2 * ket3
assert qapply(bra1 * TensorProduct(a, b * b),
dagger=True) == sqrt(2) * bra2
assert qapply(bra2 * ket1).doit() == TensorProduct(1, 1)
assert qapply(TensorProduct(a, b * b) * ket1) == sqrt(2) * ket2
assert qapply(Dagger(TensorProduct(a, b * b) * ket1),
dagger=True) == sqrt(2) * Dagger(ket2)
示例6: test_boson_states
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def test_boson_states():
a = BosonOp("a")
# Fock states
n = 3
assert (BosonFockBra(0) * BosonFockKet(1)).doit() == 0
assert (BosonFockBra(1) * BosonFockKet(1)).doit() == 1
assert qapply(BosonFockBra(n) * Dagger(a)**n * BosonFockKet(0)) \
== sqrt(prod(range(1, n+1)))
# Coherent states
alpha1, alpha2 = 1.2, 4.3
assert (BosonCoherentBra(alpha1) * BosonCoherentKet(alpha1)).doit() == 1
assert (BosonCoherentBra(alpha2) * BosonCoherentKet(alpha2)).doit() == 1
assert abs((BosonCoherentBra(alpha1) * BosonCoherentKet(alpha2)).doit() -
exp(-S(1) / 2 * (alpha1 - alpha2) ** 2)) < 1e-12
assert qapply(a * BosonCoherentKet(alpha1)) == \
alpha1 * BosonCoherentKet(alpha1)
示例7: franke_1
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def franke_1(lmbda):
assert -frac(9, 5) <= lmbda <= frac(9, 4)
a = sqrt(frac(9 + 5 * lmbda, 15))
b = sqrt(frac(9 - 4 * lmbda, 15))
c = sqrt(frac(3, 5))
weights, points = concat(
zero(frac(16 * (4 + 5 * lmbda), 9 * (9 + 5 * lmbda))),
pm2([frac(25, 9 * (9 - 4 * lmbda)), b, c]),
pm(
[frac(40, 9 * (9 + 5 * lmbda)), a, 0],
[frac(40 * (1 - lmbda), 9 * (9 - 4 * lmbda)), 0, c],
),
)
weights /= 4
return C2Scheme(f"Franke(1, {lmbda})", weights, points, 5, source)
示例8: franke_3a
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def franke_3a():
a = math.sqrt(5.0 / 9.0 + 2.0 / 63.0 * math.sqrt(70))
b = math.sqrt(5.0 / 9.0 - 2.0 / 63.0 * math.sqrt(70))
weights, points = concat(
pm2(
[0.705065140564012e-1, 0.845927799771709, a],
[0.721121511007611e-1, 0.628901636732253, a],
[0.971492736037507e-1, 0.959681421214621, b],
[0.368549048677049, 0.436030596273468, b],
),
pm(
[0.316049382716049, 0.774596669241483, 0],
[0.188616439798053, 0, a],
[0.258606964371341e-1, 0, b],
),
zero(0.505679012345679),
)
weights /= 4
return C2Scheme("Franke 3a", weights, points, 9, source)
示例9: franke_3b
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def franke_3b():
a = math.sqrt(5.0 / 9.0 + 2.0 / 63.0 * math.sqrt(70))
b = math.sqrt(5.0 / 9.0 - 2.0 / 63.0 * math.sqrt(70))
weights, points = concat(
pm2(
[0.499290623065150e-1, 0.945813739519925, a],
[0.158445182284802, 0.465346624836203, a],
[0.183383788151247, 0.804253925742002, b],
[0.881476523665422e-1, 0.681385892163677, b],
),
pm(
[0.114456375561331, 0.963018409085396, 0.0],
[0.454432513327558, 0.428610143223121, 0.0],
[0.571052809297435e-1, 0.0, a],
[0.414194459963155, 0.0, b],
),
)
weights /= 4
return C2Scheme("Franke 3b", weights, points, 9, source)
示例10: franke_3c
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def franke_3c():
a = math.sqrt(5.0 / 9.0 + 2.0 / 63.0 * math.sqrt(70))
b = math.sqrt(5.0 / 9.0 - 2.0 / 63.0 * math.sqrt(70))
weights, points = concat(
pm2(
[0.494522019130682e-1, 0.949307350001342, a],
[0.163914731881061, 0.458177548931134, a],
[0.265904816944092, 0.774596669241483, b],
),
pm(
[0.113041839046410, 0.967776908976724, 0.0],
[0.479922229600720, 0.417754671502987, 0.0],
[0.471199025241204e-1, 0.0, a],
[0.425447707110548, 0.0, b],
),
zero(-0.481503595164821e-1),
)
weights /= 4
return C2Scheme("Franke 3c", weights, points, 9, source)
示例11: schmid_4
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def schmid_4():
points = numpy.array(
[
[0, (sqrt(3) + sqrt(15)) / 6],
[0, (sqrt(3) - sqrt(15)) / 6],
[+sqrt(15) / 5, (+sqrt(87) - 2 * sqrt(3)) / 15],
[-sqrt(15) / 5, (+sqrt(87) - 2 * sqrt(3)) / 15],
[+sqrt(15) / 5, (-sqrt(87) - 2 * sqrt(3)) / 15],
[-sqrt(15) / 5, (-sqrt(87) - 2 * sqrt(3)) / 15],
]
)
weights = numpy.array(
[
frac(2, 9) - 2 * sqrt(5) / 45,
frac(2, 9) + 2 * sqrt(5) / 45,
frac(5, 36) + 5 * sqrt(29) / 18 / 29,
frac(5, 36) + 5 * sqrt(29) / 18 / 29,
frac(5, 36) - 5 * sqrt(29) / 18 / 29,
frac(5, 36) - 5 * sqrt(29) / 18 / 29,
]
)
return C2Scheme("Schmid 4", weights, points, 4, source)
示例12: _stroud_5_5
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def _stroud_5_5(n, variant_a, symbolic=False):
frac = sympy.Rational if symbolic else lambda a, b: a / b
sqrt = sympy.sqrt if symbolic else math.sqrt
p_m = +1 if variant_a else -1
# r is complex-valued for n >= 3
r = sqrt((n + 2 + p_m * (n - 1) * sqrt(2 * (n + 2))) / (2 * n))
s = sqrt((n + 2 - p_m * sqrt(2 * (n + 2))) / (2 * n))
A = frac(2, n + 2)
B = frac(1, 2 ** n * (n + 2))
data = [(A, [n * [0]]), (B, fsd(n, (r, 1), (s, n - 1)))]
points, weights = untangle(data)
variant = "a" if variant_a else "b"
return Enr2Scheme(f"Stroud Enr2 5-5{variant}", n, weights, points, 5, source)
示例13: stroud_1967_7_4
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def stroud_1967_7_4(n, symbolic=False):
sqrt = sympy.sqrt if symbolic else math.sqrt
assert n >= 3
sqrt2n2 = sqrt(2 * (n + 2))
r1, r2 = [sqrt((n + 2 - p_m * sqrt2n2) / 2) for p_m in [+1, -1]]
g = gamma_n_2(n, symbolic)
A1, A2 = [(n + 2 + p_m * sqrt2n2) / 4 / (n + 2) * g for p_m in [+1, -1]]
s = un.stroud_1967(n)
points = numpy.concatenate([r1 * s.points, r2 * s.points])
weights = numpy.concatenate([A1 * s.weights, A2 * s.weights])
weights *= un.volume_nsphere(n - 1, symbolic) / volume_enr2(n, symbolic)
return Enr2Scheme("Stroud 1967-7 4", n, weights, points, 7, source)
示例14: xiu
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def xiu(n):
points = []
for k in range(n + 1):
pt = []
# Slight adaptation:
# The article has points for the weight 1/sqrt(2*pi) exp(−x**2/2)
# so divide by sqrt(2) to adapt for 1/sqrt(pi) exp(−x ** 2)
for r in range(1, n // 2 + 1):
alpha = (2 * r * k * pi) / (n + 1)
pt += [cos(alpha), sin(alpha)]
if n % 2 == 1:
pt += [(-1) ** k / sqrt(2)]
points.append(pt)
points = numpy.array(points)
weights = numpy.full(n + 1, frac(1, n + 1))
return Enr2Scheme("Xiu", n, weights, points, 2, source)
示例15: cools_haegemans_1
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sqrt [as 别名]
def cools_haegemans_1(n, delta2=1, symbolic=False):
frac = sympy.Rational if symbolic else lambda a, b: a / b
sqrt = sympy.sqrt if symbolic else math.sqrt
assert frac(1, 2) <= delta2
m = 1
w0 = frac(2 * delta2 - 1, 2 * delta2)
w = frac(_mu(2, symbolic) ** m * _mu(0, symbolic) ** (n - m), 2 ** n * delta2 ** m)
data = [
(w0, z(n)),
(w, pm(n * [sqrt(delta2)])),
]
points, weights = untangle(data)
return Enr2Scheme("Cools-Haegemans 1", n, weights, points, 3, _source)