本文整理汇总了Python中sympy.core.function.nfloat函数的典型用法代码示例。如果您正苦于以下问题:Python nfloat函数的具体用法?Python nfloat怎么用?Python nfloat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了nfloat函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_float_handling
def test_float_handling():
def test(e1, e2):
return len(e1.atoms(Float)) == len(e2.atoms(Float))
assert solve(x - 0.5, rational=True)[0].is_Rational
assert solve(x - 0.5, rational=False)[0].is_Float
assert solve(x - S.Half, rational=False)[0].is_Rational
assert solve(x - 0.5, rational=None)[0].is_Float
assert solve(x - S.Half, rational=None)[0].is_Rational
assert test(nfloat(1 + 2*x), 1.0 + 2.0*x)
for contain in [list, tuple, set]:
ans = nfloat(contain([1 + 2*x]))
assert type(ans) is contain and test(list(ans)[0], 1.0 + 2.0*x)
k, v = nfloat({2*x: [1 + 2*x]}).items()[0]
assert test(k, 2*x) and test(v[0], 1.0 + 2.0*x)
assert test(nfloat(cos(2*x)), cos(2.0*x))
assert test(nfloat(3*x**2), 3.0*x**2)
assert test(nfloat(3*x**2, exponent=True), 3.0*x**2.0)
assert test(nfloat(exp(2*x)), exp(2.0*x))
assert test(nfloat(x/3), x/3.0)
assert test(nfloat(x**4 + 2*x + cos(S(1)/3) + 1),
x**4 + 2.0*x + 1.94495694631474)
示例2: test_return_root_of
def test_return_root_of():
f = x ** 5 - 15 * x ** 3 - 5 * x ** 2 + 10 * x + 20
s = list(solveset_complex(f, x))
for root in s:
assert root.func == RootOf
# if one uses solve to get the roots of a polynomial that has a RootOf
# solution, make sure that the use of nfloat during the solve process
# doesn't fail. Note: if you want numerical solutions to a polynomial
# it is *much* faster to use nroots to get them than to solve the
# equation only to get RootOf solutions which are then numerically
# evaluated. So for eq = x**5 + 3*x + 7 do Poly(eq).nroots() rather
# than [i.n() for i in solve(eq)] to get the numerical roots of eq.
assert (
nfloat(list(solveset_complex(x ** 5 + 3 * x ** 3 + 7, x))[0], exponent=False)
== RootOf(x ** 5 + 3 * x ** 3 + 7, 0).n()
)
sol = list(solveset_complex(x ** 6 - 2 * x + 2, x))
assert all(isinstance(i, RootOf) for i in sol) and len(sol) == 6
f = x ** 5 - 15 * x ** 3 - 5 * x ** 2 + 10 * x + 20
s = list(solveset_complex(f, x))
for root in s:
assert root.func == RootOf
s = x ** 5 + 4 * x ** 3 + 3 * x ** 2 + S(7) / 4
assert solveset_complex(s, x) == FiniteSet(*Poly(s * 4, domain="ZZ").all_roots())
# XXX: this comparison should work without converting the FiniteSet to list
# See #7876
eq = x * (x - 1) ** 2 * (x + 1) * (x ** 6 - x + 1)
assert list(solveset_complex(eq, x)) == list(
FiniteSet(
-1,
0,
1,
RootOf(x ** 6 - x + 1, 0),
RootOf(x ** 6 - x + 1, 1),
RootOf(x ** 6 - x + 1, 2),
RootOf(x ** 6 - x + 1, 3),
RootOf(x ** 6 - x + 1, 4),
RootOf(x ** 6 - x + 1, 5),
)
)
示例3: simplify
#.........这里部分代码省略.........
# TODO: Apply different strategies, considering expression pattern:
# is it a purely rational function? Is there any trigonometric function?...
# See also https://github.com/sympy/sympy/pull/185.
def shorter(*choices):
'''Return the choice that has the fewest ops. In case of a tie,
the expression listed first is selected.'''
if not has_variety(choices):
return choices[0]
return min(choices, key=measure)
# rationalize Floats
floats = False
if rational is not False and expr.has(Float):
floats = True
expr = nsimplify(expr, rational=True)
expr = bottom_up(expr, lambda w: w.normal())
expr = Mul(*powsimp(expr).as_content_primitive())
_e = cancel(expr)
expr1 = shorter(_e, _mexpand(_e).cancel()) # issue 6829
expr2 = shorter(together(expr, deep=True), together(expr1, deep=True))
if ratio is S.Infinity:
expr = expr2
else:
expr = shorter(expr2, expr1, expr)
if not isinstance(expr, Basic): # XXX: temporary hack
return expr
expr = factor_terms(expr, sign=False)
# hyperexpand automatically only works on hypergeometric terms
expr = hyperexpand(expr)
expr = piecewise_fold(expr)
if expr.has(BesselBase):
expr = besselsimp(expr)
if expr.has(TrigonometricFunction, HyperbolicFunction):
expr = trigsimp(expr, deep=True)
if expr.has(log):
expr = shorter(expand_log(expr, deep=True), logcombine(expr))
if expr.has(CombinatorialFunction, gamma):
# expression with gamma functions or non-integer arguments is
# automatically passed to gammasimp
expr = combsimp(expr)
if expr.has(Sum):
expr = sum_simplify(expr)
if expr.has(Product):
expr = product_simplify(expr)
from sympy.physics.units import Quantity
from sympy.physics.units.util import quantity_simplify
if expr.has(Quantity):
expr = quantity_simplify(expr)
short = shorter(powsimp(expr, combine='exp', deep=True), powsimp(expr), expr)
short = shorter(short, cancel(short))
short = shorter(short, factor_terms(short), expand_power_exp(expand_mul(short)))
if short.has(TrigonometricFunction, HyperbolicFunction, ExpBase):
short = exptrigsimp(short)
# get rid of hollow 2-arg Mul factorization
hollow_mul = Transform(
lambda x: Mul(*x.args),
lambda x:
x.is_Mul and
len(x.args) == 2 and
x.args[0].is_Number and
x.args[1].is_Add and
x.is_commutative)
expr = short.xreplace(hollow_mul)
numer, denom = expr.as_numer_denom()
if denom.is_Add:
n, d = fraction(radsimp(1/denom, symbolic=False, max_terms=1))
if n is not S.One:
expr = (numer*n).expand()/d
if expr.could_extract_minus_sign():
n, d = fraction(expr)
if d != 0:
expr = signsimp(-n/(-d))
if measure(expr) > ratio*measure(original_expr):
expr = original_expr
# restore floats
if floats and rational is None:
expr = nfloat(expr, exponent=False)
return expr