本文整理汇总了Python中mpmath.workprec函数的典型用法代码示例。如果您正苦于以下问题:Python workprec函数的具体用法?Python workprec怎么用?Python workprec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了workprec函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _eval_evalf
def _eval_evalf(self, prec):
# The default code is insufficient for polar arguments.
# mpmath provides an optional argument "r", which evaluates
# G(z**(1/r)). I am not sure what its intended use is, but we hijack it
# here in the following way: to evaluate at a number z of |argument|
# less than (say) n*pi, we put r=1/n, compute z' = root(z, n)
# (carefully so as not to loose the branch information), and evaluate
# G(z'**(1/r)) = G(z'**n) = G(z).
from sympy.functions import exp_polar, ceiling
from sympy import Expr
import mpmath
z = self.argument
znum = self.argument._eval_evalf(prec)
if znum.has(exp_polar):
znum, branch = znum.as_coeff_mul(exp_polar)
if len(branch) != 1:
return
branch = branch[0].args[0]/I
else:
branch = S(0)
n = ceiling(abs(branch/S.Pi)) + 1
znum = znum**(S(1)/n)*exp(I*branch / n)
# Convert all args to mpf or mpc
try:
[z, r, ap, bq] = [arg._to_mpmath(prec)
for arg in [znum, 1/n, self.args[0], self.args[1]]]
except ValueError:
return
with mpmath.workprec(prec):
v = mpmath.meijerg(ap, bq, z, r)
return Expr._from_mpmath(v, prec)
开发者ID:moorepants,项目名称:sympy,代码行数:34,代码来源:hyper.py
示例2: _eval_evalf
def _eval_evalf(self, prec):
from mpmath import mp, workprec
from sympy import Expr
z = self.args[0]._to_mpmath(prec)
with workprec(prec):
res = mp.airybi(z, derivative=1)
return Expr._from_mpmath(res, prec)
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:7,代码来源:bessel.py
示例3: _eval_evalf
def _eval_evalf(self, prec):
z = self.argument._to_mpmath(prec)
ap = [a._to_mpmath(prec) for a in self.ap]
bp = [b._to_mpmath(prec) for b in self.bq]
with mpmath.workprec(prec):
res = mpmath.hyper(ap, bp, z, eliminate=False)
return Expr._from_mpmath(res, prec)
开发者ID:skirpichev,项目名称:diofant,代码行数:7,代码来源:hyper.py
示例4: apply
def apply(self, z, evaluation):
"%(name)s[z__]"
args = z.get_sequence()
if len(args) != self.nargs:
return
# if no arguments are inexact attempt to use sympy
if len([True for x in args if Expression("InexactNumberQ", x).evaluate(evaluation).is_true()]) == 0:
expr = Expression(self.get_name(), *args).to_sympy()
result = from_sympy(expr)
# evaluate leaves to convert e.g. Plus[2, I] -> Complex[2, 1]
result = result.evaluate_leaves(evaluation)
else:
prec = min_prec(*args)
with mpmath.workprec(prec):
mpmath_args = [sympy2mpmath(x.to_sympy()) for x in args]
if None in mpmath_args:
return
try:
result = self.eval(*mpmath_args)
result = from_sympy(mpmath2sympy(result, prec))
except ValueError, exc:
text = str(exc)
if text == "gamma function pole":
return Symbol("ComplexInfinity")
else:
raise
except ZeroDivisionError:
return
except SpecialValueError, exc:
return Symbol(exc.name)
开发者ID:sitelmi,项目名称:Mathics,代码行数:33,代码来源:arithmetic.py
示例5: apply_N
def apply_N(self, prec, evaluation):
'N[E, prec_]'
prec = get_precision(prec, evaluation)
if prec is not None:
with workprec(prec):
return Real(mpmath2gmpy(mpmath.e))
开发者ID:cjiang,项目名称:Mathics,代码行数:7,代码来源:exptrig.py
示例6: apply
def apply(self, z, evaluation):
'%(name)s[z__]'
args = z.get_sequence()
if len(args) != self.nargs:
return
# if no arguments are inexact attempt to use sympy
if all(not x.is_inexact() for x in args):
result = Expression(self.get_name(), *args).to_sympy()
result = self.prepare_mathics(result)
result = from_sympy(result)
# evaluate leaves to convert e.g. Plus[2, I] -> Complex[2, 1]
result = result.evaluate_leaves(evaluation)
else:
prec = min_prec(*args)
with mpmath.workprec(prec):
mpmath_args = [sympy2mpmath(x.to_sympy()) for x in args]
if None in mpmath_args:
return
try:
result = self.eval(*mpmath_args)
result = from_sympy(mpmath2sympy(result, prec))
except ValueError, exc:
text = str(exc)
if text == 'gamma function pole':
return Symbol('ComplexInfinity')
else:
raise
except ZeroDivisionError:
return
except SpecialValueError, exc:
return Symbol(exc.name)
开发者ID:kirpit,项目名称:Mathics,代码行数:34,代码来源:arithmetic.py
示例7: _eval_evalf
def _eval_evalf(self, prec):
m = self.args[0]
if m.is_Integer and m.is_nonnegative:
m = m._to_mpmath(prec)
with workprec(prec):
res = mp.eulernum(m)
return Expr._from_mpmath(res, prec)
开发者ID:skirpichev,项目名称:diofant,代码行数:8,代码来源:numbers.py
示例8: _eval_evalf
def _eval_evalf(self, prec):
from mpmath import mp, workprec
from sympy import Expr
a = self.args[0]._to_mpmath(prec)
z = self.args[1]._to_mpmath(prec)
with workprec(prec):
res = mp.gammainc(a, z, mp.inf)
return Expr._from_mpmath(res, prec)
开发者ID:vprusso,项目名称:sympy,代码行数:8,代码来源:gamma_functions.py
示例9: real_power
def real_power(x, y):
x = g_mpf(x)
y = g_mpf(y)
prec = min(x.getprec(), y.getprec())
with workprec(prec):
x = gmpy2mpmath(x)
y = gmpy2mpmath(y)
return mpmath2gmpy(x ** y)
开发者ID:cjiang,项目名称:Mathics,代码行数:8,代码来源:numbers.py
示例10: _eval_evalf
def _eval_evalf(self, prec):
"""Evaluate this complex root to the given precision."""
with workprec(prec):
g = self.poly.gen
if not g.is_Symbol:
d = Dummy('x')
func = lambdify(d, self.expr.subs({g: d}), "mpmath")
else:
func = lambdify(g, self.expr, "mpmath")
try:
interval = self.interval
except DomainError:
return super()._eval_evalf(prec)
while True:
if self.is_extended_real:
a = mpf(str(interval.a))
b = mpf(str(interval.b))
if a == b:
root = a
break
x0 = mpf(str(interval.center))
else:
ax = mpf(str(interval.ax))
bx = mpf(str(interval.bx))
ay = mpf(str(interval.ay))
by = mpf(str(interval.by))
x0 = mpc(*map(str, interval.center))
if ax == bx and ay == by:
root = x0
break
try:
root = findroot(func, x0)
# If the (real or complex) root is not in the 'interval',
# then keep refining the interval. This happens if findroot
# accidentally finds a different root outside of this
# interval because our initial estimate 'x0' was not close
# enough. It is also possible that the secant method will
# get trapped by a max/min in the interval; the root
# verification by findroot will raise a ValueError in this
# case and the interval will then be tightened -- and
# eventually the root will be found.
if self.is_extended_real:
if (a <= root <= b):
break
elif (ax <= root.real <= bx and ay <= root.imag <= by
and (interval.ay > 0 or interval.by < 0)):
break
except (ValueError, UnboundLocalError):
pass
self.refine()
interval = self.interval
return ((Float._new(root.real._mpf_, prec) if not self.is_imaginary else 0) +
I*Float._new(root.imag._mpf_, prec))
开发者ID:skirpichev,项目名称:diofant,代码行数:57,代码来源:rootoftools.py
示例11: _eval_evalf
def _eval_evalf(self, prec):
"""Evaluate this complex root to the given precision. """
with workprec(prec):
g = self.poly.gen
if not g.is_Symbol:
d = Dummy('x')
func = lambdify(d, self.expr.subs(g, d))
else:
func = lambdify(g, self.expr)
interval = self._get_interval()
if not self.is_real:
# For complex intervals, we need to keep refining until the
# imaginary interval is disjunct with other roots, that is,
# until both ends get refined.
ay = interval.ay
by = interval.by
while interval.ay == ay or interval.by == by:
interval = interval.refine()
while True:
if self.is_real:
x0 = mpf(str(interval.center))
else:
x0 = mpc(*map(str, interval.center))
try:
root = findroot(func, x0, verify=False)
# If the (real or complex) root is not in the 'interval',
# then keep refining the interval. This happens if findroot
# accidentally finds a different root outside of this
# interval because our initial estimate 'x0' was not close
# enough.
if self.is_real:
a = mpf(str(interval.a))
b = mpf(str(interval.b))
if a == b:
root = a
break
if not (a < root < b):
raise ValueError("Root not in the interval.")
else:
ax = mpf(str(interval.ax))
bx = mpf(str(interval.bx))
ay = mpf(str(interval.ay))
by = mpf(str(interval.by))
if ax == bx and ay == by:
root = ax + S.ImaginaryUnit*by
break
if not (ax < root.real < bx and ay < root.imag < by):
raise ValueError("Root not in the interval.")
except ValueError:
interval = interval.refine()
continue
else:
break
return Float._new(root.real._mpf_, prec) + I*Float._new(root.imag._mpf_, prec)
开发者ID:tanmaysahay94,项目名称:sympy,代码行数:57,代码来源:rootoftools.py
示例12: apply_N
def apply_N(self, k, precision, evaluation):
'N[AiryBiZero[k_Integer], precision_]'
prec = get_precision(precision, evaluation)
k_int = k.get_int_value()
with mpmath.workprec(prec):
result = mpmath2sympy(mpmath.airybizero(k_int), prec)
return from_sympy(result)
开发者ID:fleeaway,项目名称:Mathics,代码行数:9,代码来源:specialfunctions.py
示例13: _eval_evalf
def _eval_evalf(self, prec):
from mpmath import mp, workprec
from sympy import Expr
if all(x.is_number for x in self.args):
a = self.args[0]._to_mpmath(prec)
z = self.args[1]._to_mpmath(prec)
with workprec(prec):
res = mp.gammainc(a, 0, z)
return Expr._from_mpmath(res, prec)
else:
return self
开发者ID:gamechanger98,项目名称:sympy,代码行数:11,代码来源:gamma_functions.py
示例14: gmpy2mpmath
def gmpy2mpmath(value):
if isinstance(value, mpcomplex):
return value.to_mpmath()
else:
if get_type(value) != 'f':
value = g_mpf(value)
with workprec(value.getprec()):
value = str(g_mpf(value))
if value and value[0] == '-':
return -mp_mpf(value[1:])
else:
return mp_mpf(value)
开发者ID:cjiang,项目名称:Mathics,代码行数:12,代码来源:numbers.py
示例15: apply_inexact
def apply_inexact(self, n, k, evaluation):
'Binomial[n_?InexactNumberQ, k_?NumberQ]'
with workprec(min_prec(n, k)):
n = gmpy2mpmath(n.value)
k = gmpy2mpmath(k.value)
result = mpmath.binomial(n, k)
try:
result = mpmath2gmpy(result)
except SpecialValueError, exc:
return Symbol(exc.name)
number = Number.from_mp(result)
return number
开发者ID:cjiang,项目名称:Mathics,代码行数:13,代码来源:combinatorial.py
示例16: _eval_evalf
def _eval_evalf(self, prec):
# Note: works without this function by just calling
# mpmath for Legendre polynomials. But using
# the dedicated function directly is cleaner.
from mpmath import mp, workprec
from sympy import Expr
n = self.args[0]._to_mpmath(prec)
m = self.args[1]._to_mpmath(prec)
theta = self.args[2]._to_mpmath(prec)
phi = self.args[3]._to_mpmath(prec)
with workprec(prec):
res = mp.spherharm(n, m, theta, phi)
return Expr._from_mpmath(res, prec)
开发者ID:glyg,项目名称:sympy,代码行数:13,代码来源:spherical_harmonics.py
示例17: apply
def apply(self, z, evaluation):
'%(name)s[z__]'
args = z.numerify(evaluation).get_sequence()
mpmath_function = self.get_mpmath_function(args)
result = None
# if no arguments are inexact attempt to use sympy
if all(not x.is_inexact() for x in args):
result = Expression(self.get_name(), *args).to_sympy()
result = self.prepare_mathics(result)
result = from_sympy(result)
# evaluate leaves to convert e.g. Plus[2, I] -> Complex[2, 1]
return result.evaluate_leaves(evaluation)
elif mpmath_function is None:
return
if not all(isinstance(arg, Number) for arg in args):
return
if any(arg.is_machine_precision() for arg in args):
# if any argument has machine precision then the entire calculation
# is done with machine precision.
float_args = [arg.round().get_float_value(permit_complex=True) for arg in args]
if None in float_args:
return
result = self.call_mpmath(mpmath_function, float_args)
if isinstance(result, (mpmath.mpc, mpmath.mpf)):
if mpmath.isinf(result) and isinstance(result, mpmath.mpc):
result = Symbol('ComplexInfinity')
elif mpmath.isinf(result) and result > 0:
result = Expression('DirectedInfinity', Integer(1))
elif mpmath.isinf(result) and result < 0:
result = Expression('DirectedInfinity', Integer(-1))
elif mpmath.isnan(result):
result = Symbol('Indeterminate')
else:
result = Number.from_mpmath(result)
else:
prec = min_prec(*args)
d = dps(prec)
args = [Expression('N', arg, Integer(d)).evaluate(evaluation) for arg in args]
with mpmath.workprec(prec):
mpmath_args = [x.to_mpmath() for x in args]
if None in mpmath_args:
return
result = self.call_mpmath(mpmath_function, mpmath_args)
if isinstance(result, (mpmath.mpc, mpmath.mpf)):
result = Number.from_mpmath(result, d)
return result
开发者ID:poke1024,项目名称:Mathics,代码行数:51,代码来源:arithmetic.py
示例18: apply_inexact
def apply_inexact(self, z, evaluation):
'%(name)s[z_Real|z_Complex?InexactNumberQ]'
with workprec(z.get_precision()):
z = gmpy2mpmath(z.value)
try:
result = self.eval(z)
except ValueError, exc:
text = str(exc)
if text == 'gamma function pole':
return Symbol('ComplexInfinity')
else:
raise
except ZeroDivisionError:
return
开发者ID:mikexstudios,项目名称:Mathics,代码行数:15,代码来源:arithmetic.py
示例19: apply_N
def apply_N(self, k, precision, evaluation):
'N[AiryBiZero[k_Integer], precision_]'
try:
d = get_precision(precision, evaluation)
except PrecisionValueError:
return
if d is None:
p = machine_precision
else:
p = _prec(d)
k_int = k.get_int_value()
with mpmath.workprec(p):
result = mpmath.airybizero(k_int)
return Number.from_mpmath(result, d)
开发者ID:Piruzzolo,项目名称:Mathics,代码行数:18,代码来源:specialfunctions.py
示例20: apply_inexact
def apply_inexact(self, z, evaluation):
'%(name)s[z_Real|z_Complex?InexactNumberQ]'
prec = z.get_precision()
with mpmath.workprec(prec):
z = sympy2mpmath(z.to_sympy())
if z is None:
return
try:
result = self.eval(z)
result = mpmath2sympy(result, prec)
except ValueError, exc:
text = str(exc)
if text == 'gamma function pole':
return Symbol('ComplexInfinity')
else:
raise
except ZeroDivisionError:
return
开发者ID:bwright,项目名称:Mathics,代码行数:19,代码来源:arithmetic.py
注:本文中的mpmath.workprec函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论