本文整理汇总了Python中sympy.polys.Poly.terms方法的典型用法代码示例。如果您正苦于以下问题:Python Poly.terms方法的具体用法?Python Poly.terms怎么用?Python Poly.terms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.polys.Poly
的用法示例。
在下文中一共展示了Poly.terms方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: apart_undetermined_coeffs
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import terms [as 别名]
def apart_undetermined_coeffs(P, Q):
"""Partial fractions via method of undetermined coefficients. """
X = numbered_symbols(cls=Dummy)
partial, symbols = [], []
_, factors = Q.factor_list()
for f, k in factors:
n, q = f.degree(), Q
for i in xrange(1, k + 1):
coeffs, q = take(X, n), q.quo(f)
partial.append((coeffs, q, f, i))
symbols.extend(coeffs)
dom = Q.get_domain().inject(*symbols)
F = Poly(0, Q.gen, domain=dom)
for i, (coeffs, q, f, k) in enumerate(partial):
h = Poly(coeffs, Q.gen, domain=dom)
partial[i] = (h, f, k)
q = q.set_domain(dom)
F += h * q
system, result = [], S(0)
for (k,), coeff in F.terms():
system.append(coeff - P.nth(k))
from sympy.solvers import solve
solution = solve(system, symbols)
for h, f, k in partial:
h = h.as_expr().subs(solution)
result += h / f.as_expr() ** k
return result
示例2: trigsimp_groebner
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import terms [as 别名]
#.........这里部分代码省略.........
# - it suffices to show that the projective closure in CP**3 is
# irreducible
# - using the half-angle substitutions, we can express sin(x), tan(x),
# cos(x) as rational functions in tan(x/2)
# - from this, we get a rational map from CP**1 to our curve
# - this is a morphism, hence the curve is prime
#
# Step (2) is trivial.
#
# Step (3) works by adding selected relations of the form
# sin(x + y) - sin(x)*cos(y) - sin(y)*cos(x), etc. Geometric primality is
# preserved by the same argument as before.
def parse_hints(hints):
"""Split hints into (n, funcs, iterables, gens)."""
n = 1
funcs, iterables, gens = [], [], []
for e in hints:
if isinstance(e, (SYMPY_INTS, Integer)):
n = e
elif isinstance(e, FunctionClass):
funcs.append(e)
elif iterable(e):
iterables.append((e[0], e[1:]))
# XXX sin(x+2y)?
# Note: we go through polys so e.g.
# sin(-x) -> -sin(x) -> sin(x)
gens.extend(parallel_poly_from_expr(
[e[0](x) for x in e[1:]] + [e[0](Add(*e[1:]))])[1].gens)
else:
gens.append(e)
return n, funcs, iterables, gens
def build_ideal(x, terms):
"""
Build generators for our ideal. Terms is an iterable with elements of
the form (fn, coeff), indicating that we have a generator fn(coeff*x).
If any of the terms is trigonometric, sin(x) and cos(x) are guaranteed
to appear in terms. Similarly for hyperbolic functions. For tan(n*x),
sin(n*x) and cos(n*x) are guaranteed.
"""
I = []
y = Dummy('y')
for fn, coeff in terms:
for c, s, t, rel in (
[cos, sin, tan, cos(x)**2 + sin(x)**2 - 1],
[cosh, sinh, tanh, cosh(x)**2 - sinh(x)**2 - 1]):
if coeff == 1 and fn in [c, s]:
I.append(rel)
elif fn == t:
I.append(t(coeff*x)*c(coeff*x) - s(coeff*x))
elif fn in [c, s]:
cn = fn(coeff*y).expand(trig=True).subs(y, x)
I.append(fn(coeff*x) - cn)
return list(set(I))
def analyse_gens(gens, hints):
"""
Analyse the generators ``gens``, using the hints ``hints``.
The meaning of ``hints`` is described in the main docstring.
Return a new list of generators, and also the ideal we should
work with.
"""
# First parse the hints
示例3: _solve
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import terms [as 别名]
def _solve(f, *symbols, **flags):
""" Return a checked solution for f in terms of one or more of the symbols."""
check = flags.get('check', True)
if not iterable(f):
if len(symbols) != 1:
soln = None
free = f.free_symbols
ex = free - set(symbols)
if len(ex) == 1:
ex = ex.pop()
try:
# may come back as dict or list (if non-linear)
soln = solve_undetermined_coeffs(f, symbols, ex)
except NotImplementedError:
pass
if not soln is None:
return soln
# find first successful solution
failed = []
for s in symbols:
n, d = solve_linear(f, symbols=[s])
if n.is_Symbol:
return [{n: cancel(d)}]
failed.append(s)
for s in failed:
try:
soln = _solve(f, s, **flags)
except NotImplementedError:
continue
if soln:
return [{s: sol} for sol in soln]
else:
return soln
else:
msg = "No algorithms are implemented to solve equation %s"
raise NotImplementedError(msg % f)
symbol = symbols[0]
# build up solutions if f is a Mul
if f.is_Mul:
result = set()
dens = denoms(f, symbols)
for m in f.args:
soln = _solve(m, symbol, **flags)
result.update(set(soln))
if check:
result = [s for s in result if all(not checksol(den, {symbol: s}, **flags) for den in dens)]
elif f.is_Piecewise:
result = set()
for expr, cond in f.args:
candidates = _solve(expr, *symbols)
if isinstance(cond, bool) or cond.is_Number:
if not cond:
continue
# Only include solutions that do not match the condition
# of any of the other pieces.
for candidate in candidates:
matches_other_piece = False
for other_expr, other_cond in f.args:
if isinstance(other_cond, bool) \
or other_cond.is_Number:
continue
if bool(other_cond.subs(symbol, candidate)):
matches_other_piece = True
break
if not matches_other_piece:
result.add(candidate)
else:
for candidate in candidates:
if bool(cond.subs(symbol, candidate)):
result.add(candidate)
dens = set() # all checking has already been done
else:
# first see if it really depends on symbol and whether there
# is a linear solution
f_num, sol = solve_linear(f, symbols=symbols)
if not symbol in f_num.free_symbols:
return []
elif f_num.is_Symbol:
return [cancel(sol)]
result = False # no solution was obtained
msg = '' # there is no failure message
dens = denoms(f, symbols) # store these for checking later
# Poly is generally robust enough to convert anything to
# a polynomial and tell us the different generators that it
# contains, so we will inspect the generators identified by
# polys to figure out what to do.
poly = Poly(f_num)
if poly is None:
raise ValueError('could not convert %s to Poly' % f_num)
gens = [g for g in poly.gens if g.has(symbol)]
if len(gens) > 1:
#.........这里部分代码省略.........