本文整理汇总了Python中sympy.polys.Poly.cancel方法的典型用法代码示例。如果您正苦于以下问题:Python Poly.cancel方法的具体用法?Python Poly.cancel怎么用?Python Poly.cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.polys.Poly
的用法示例。
在下文中一共展示了Poly.cancel方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cancel
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def cancel(f, *symbols):
"""Cancel common factors in a given rational function.
Given a quotient of polynomials, performing only gcd and quo
operations in polynomial algebra, return rational function
with numerator and denominator of minimal total degree in
an expanded form.
For all other kinds of expressions the input is returned in
an unchanged form. Note however, that 'cancel' function can
thread over sums and relational operators.
Additionally you can specify a list of variables to perform
cancelation more efficiently using only those symbols.
>>> from sympy import *
>>> x,y = symbols('xy')
>>> cancel((x**2-1)/(x-1))
1 + x
>>> cancel((x**2-y**2)/(x-y), x)
x + y
>>> cancel((x**2-2)/(x+sqrt(2)))
x - 2**(1/2)
"""
return Poly.cancel(f, *symbols)
示例2: simplify
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def simplify(expr):
"""Naively simplifies the given expression.
Simplification is not a well defined term and the exact strategies
this function tries can change in the future versions of SymPy. If
your algorithm relies on "simplification" (whatever it is), try to
determine what you need exactly - is it powsimp(), or radsimp(),
or together(), or something else? And use this particular function
directly, because those are well defined and thus your algorithm
will be robust.
"""
expr = Poly.cancel(powsimp(expr))
return together(expr.expand())
示例3: simplify
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def simplify(expr):
"""Naively simplifies the given expression.
Simplification is not a well defined term and the exact strategies
this function tries can change in the future versions of SymPy. If
your algorithm relies on "simplification" (whatever it is), try to
determine what you need exactly - is it powsimp()? radsimp()?
together()?, logcombine()?, or something else? And use this particular
function directly, because those are well defined and thus your algorithm
will be robust.
"""
expr = Poly.cancel(powsimp(expr))
expr = powsimp(together(expr.expand()), combine='exp', deep=True)
if expr.could_extract_minus_sign():
n, d = expr.as_numer_denom()
if d != 0:
expr = -n/(-d)
return expr
示例4: hypersimp
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def hypersimp(f, k):
"""Given combinatorial term f(k) simplify its consecutive term ratio
ie. f(k+1)/f(k). The input term can be composed of functions and
integer sequences which have equivalent representation in terms
of gamma special function.
The algorithm performs three basic steps:
(1) Rewrite all functions in terms of gamma, if possible.
(2) Rewrite all occurrences of gamma in terms of products
of gamma and rising factorial with integer, absolute
constant exponent.
(3) Perform simplification of nested fractions, powers
and if the resulting expression is a quotient of
polynomials, reduce their total degree.
If f(k) is hypergeometric then as result we arrive with a
quotient of polynomials of minimal degree. Otherwise None
is returned.
For more information on the implemented algorithm refer to:
[1] W. Koepf, Algorithms for m-fold Hypergeometric Summation,
Journal of Symbolic Computation (1995) 20, 399-417
"""
f = sympify(f)
g = f.subs(k, k+1) / f
g = g.rewrite(gamma)
g = expand_func(g)
g = powsimp(g, deep=True, combine='exp')
if g.is_rational_function(k):
return Poly.cancel(g, k)
else:
return None
示例5: splitter
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def splitter(p):
for y in V:
if not p.has_any_symbols(y):
continue
if derivation(y) is not S.Zero:
c, q = p.as_poly(y).as_primitive()
q = q.as_basic()
h = gcd(q, derivation(q), y)
s = quo(h, gcd(q, q.diff(y), y), y)
c_split = splitter(c)
if s.as_poly(y).degree == 0:
return (c_split[0], q * c_split[1])
q_split = splitter(Poly.cancel((q, s), *V))
return (c_split[0]*q_split[0]*s, c_split[1]*q_split[1])
else:
return (S.One, p)
示例6: ratint
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def ratint(f, x, **flags):
"""Performs indefinite integration of rational functions.
Given a field :math:`K` and a rational function :math:`f = p/q`,
where :math:`p` and :math:`q` are polynomials in :math:`K[x]`,
returns a function :math:`g` such that :math:`f = g'`.
>>> from sympy.integrals.rationaltools import ratint
>>> from sympy.abc import x
>>> ratint(36/(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2), x)
(12*x + 6)/(x**2 - 1) + 4*log(x - 2) - 4*log(x + 1)
References
==========
.. [Bro05] M. Bronstein, Symbolic Integration I: Transcendental
Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70
See Also
========
sympy.integrals.integrals.Integral.doit
ratint_logpart, ratint_ratpart
"""
if type(f) is not tuple:
p, q = f.as_numer_denom()
else:
p, q = f
p, q = Poly(p, x, composite=False, field=True), Poly(q, x, composite=False, field=True)
coeff, p, q = p.cancel(q)
poly, p = p.div(q)
result = poly.integrate(x).as_expr()
if p.is_zero:
return coeff*result
g, h = ratint_ratpart(p, q, x)
P, Q = h.as_numer_denom()
P = Poly(P, x)
Q = Poly(Q, x)
q, r = P.div(Q)
result += g + q.integrate(x).as_expr()
if not r.is_zero:
symbol = flags.get('symbol', 't')
if not isinstance(symbol, Symbol):
t = Dummy(symbol)
else:
t = symbol.as_dummy()
L = ratint_logpart(r, Q, x, t)
real = flags.get('real')
if real is None:
if type(f) is not tuple:
atoms = f.atoms()
else:
p, q = f
atoms = p.atoms() | q.atoms()
for elt in atoms - set([x]):
if not elt.is_real:
real = False
break
else:
real = True
eps = S(0)
if not real:
for h, q in L:
eps += RootSum(
q, Lambda(t, t*log(h.as_expr())), quadratic=True)
else:
for h, q in L:
R = log_to_real(h, q, x, t)
if R is not None:
eps += R
else:
eps += RootSum(
q, Lambda(t, t*log(h.as_expr())), quadratic=True)
result += eps
return coeff*result
示例7: ratint
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def ratint(f, x, **flags):
"""Performs indefinite integration of rational functions.
Given a field K and a rational function f = p/q, where p and q
are polynomials in K[x], returns a function g such that f = g'.
>>> from sympy.integrals.rationaltools import ratint
>>> from sympy.abc import x
>>> ratint(36/(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2), x)
-4*log(1 + x) + 4*log(-2 + x) - (6 + 12*x)/(1 - x**2)
References
==========
.. [Bro05] M. Bronstein, Symbolic Integration I: Transcendental
Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70
"""
if type(f) is not tuple:
p, q = f.as_numer_denom()
else:
p, q = f
p, q = Poly(p, x), Poly(q, x)
c, p, q = p.cancel(q)
poly, p = p.div(q)
poly = poly.to_field()
result = c*poly.integrate(x).as_basic()
if p.is_zero:
return result
g, h = ratint_ratpart(p, q, x)
P, Q = h.as_numer_denom()
P = Poly(P, x)
Q = Poly(Q, x)
q, r = P.div(Q)
result += g + q.integrate(x).as_basic()
if not r.is_zero:
symbol = flags.get('symbol', 't')
if not isinstance(symbol, Symbol):
t = Dummy(symbol)
else:
t = symbol
L = ratint_logpart(r, Q, x, t)
real = flags.get('real')
if real is None:
if type(f) is not tuple:
atoms = f.atoms()
else:
p, q = f
atoms = p.atoms() \
| q.atoms()
for elt in atoms - set([x]):
if not elt.is_real:
real = False
break
else:
real = True
eps = S(0)
if not real:
for h, q in L:
eps += RootSum(Lambda(t, t*log(h.as_basic())), q)
else:
for h, q in L:
R = log_to_real(h, q, x, t)
if R is not None:
eps += R
else:
eps += RootSum(Lambda(t, t*log(h.as_basic())), q)
result += eps
return result
示例8: heurisch
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
#.........这里部分代码省略.........
terms.add(erf(sqrt(-M[a])*x))
elif g.is_Pow:
if g.exp.is_Rational and g.exp.q == 2:
M = g.base.match(a*x**2 + b)
if M is not None and M[b].is_positive:
if M[a].is_positive:
terms.add(asinh(sqrt(M[a]/M[b])*x))
elif M[a].is_negative:
terms.add(asin(sqrt(-M[a]/M[b])*x))
else:
terms |= set(hints)
for g in set(terms):
terms |= components(g.diff(x), x)
V = _symbols('x', len(terms))
mapping = dict(zip(terms, V))
rev_mapping = {}
for k, v in mapping.iteritems():
rev_mapping[v] = k
def substitute(expr):
return expr.subs(mapping)
diffs = [ substitute(g.diff(x)) for g in terms ]
denoms = [ g.as_numer_denom()[1] for g in diffs ]
denom = reduce(lambda p, q: lcm(p, q, V), denoms)
numers = [ Poly.cancel(denom * g, *V) for g in diffs ]
def derivation(h):
return Add(*[ d * h.diff(v) for d, v in zip(numers, V) ])
def deflation(p):
for y in V:
if not p.has_any_symbols(y):
continue
if derivation(p) is not S.Zero:
c, q = p.as_poly(y).as_primitive()
return deflation(c)*gcd(q, q.diff(y))
else:
return p
def splitter(p):
for y in V:
if not p.has_any_symbols(y):
continue
if derivation(y) is not S.Zero:
c, q = p.as_poly(y).as_primitive()
q = q.as_basic()
h = gcd(q, derivation(q), y)
s = quo(h, gcd(q, q.diff(y), y), y)
c_split = splitter(c)
if s.as_poly(y).degree == 0:
return (c_split[0], q * c_split[1])
示例9: trim
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def trim(f, *symbols, **flags):
"""Cancel common factors in a given formal rational expression.
Given an arbitrary expression, map all functional components
to temporary symbols, rewriting this expression to rational
function form and perform cancelation of common factors.
When given a rational function or a list of symbols discards
all functional components, then this procedure is equivalent
to cancel().
Note that this procedure can thread over composite objects
like big operators, matrices, relational operators etc. It
can be also called recursively (to change this behaviour
unset 'recursive' flag).
>>> from sympy import *
>>> x,y = symbols('xy')
>>> f = Function('f')
>>> trim((f(x)**2+f(x))/f(x))
1 + f(x)
>>> trim((x**2+x)/x)
1 + x
Recursively simplify expressions:
>>> trim(sin((f(x)**2+f(x))/f(x)))
sin(1 + f(x))
"""
f = sympify(f)
if isinstance(f, Relational):
return Relational(trim(f.lhs, *symbols, **flags),
trim(f.rhs, *symbols, **flags), f.rel_op)
#elif isinstance(f, Matrix):
# return f.applyfunc(lambda g: trim(g, *symbols, **flags))
else:
recursive = flags.get('recursive', True)
def is_functional(g):
return not (g.is_Atom or g.is_number) \
and (not symbols or g.has(*symbols))
def components(g):
result = set()
if is_functional(g):
if g.is_Add or g.is_Mul:
args = []
for h in g.args:
h, terms = components(h)
result |= terms
args.append(h)
g = g.__class__(*args)
elif g.is_Pow:
if recursive:
base = trim(g.base, *symbols, **flags)
else:
base = g.base
if g.exp.is_Rational:
if g.exp.is_Integer:
if g.exp is S.NegativeOne:
h, terms = components(base)
return h**S.NegativeOne, terms
else:
h = base
else:
h = base**Rational(1, g.exp.q)
g = base**g.exp
else:
if recursive:
h = g = base**trim(g.exp, *symbols, **flags)
else:
h = g = base**g.exp
if is_functional(h):
result.add(h)
else:
if not recursive:
result.add(g)
else:
g = g.__class__(*[trim(h, *symbols,
**flags) for h in g.args])
if is_functional(g):
result.add(g)
return g, result
if f.is_number or not f.has_any_symbols(*symbols):
return f
#.........这里部分代码省略.........
示例10: apart
# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import cancel [as 别名]
def apart(f, z, **flags):
"""Compute partial fraction decomposition of a rational function.
Given a rational function 'f', performing only gcd operations
over the algebraic closue of the initial field of definition,
compute full partial fraction decomposition with fractions
having linear denominators.
For all other kinds of expressions the input is returned in an
unchanged form. Note however, that 'apart' function can thread
over sums and relational operators.
Note that no factorization of the initial denominator of 'f' is
needed. The final decomposition is formed in terms of a sum of
RootSum instances. By default RootSum tries to compute all its
roots to simplify itself. This behaviour can be however avoided
by seting the keyword flag evaluate=False, which will make this
function return a formal decomposition.
>>> from sympy import *
>>> x,y = symbols('xy')
>>> apart(y/(x+2)/(x+1), x)
y/(1 + x) - y/(2 + x)
>>> apart(1/(1+x**5), x, evaluate=False)
RootSum(Lambda(_a, -1/5/(x - _a)*_a), x**5 + 1, x)
For more information on the implemented algorithm refer to:
[1] M. Bronstein, B. Salvy, Full partial fraction decomposition
of rational functions, in: M. Bronstein, ed., Proceedings
ISSAC '93, ACM Press, Kiev, Ukraine, 1993, pp. 157-160.
"""
if not f.has(z):
return f
f = Poly.cancel(f, z)
P, Q = f.as_numer_denom()
if not Q.has(z):
return f
partial, r = div(P, Q, z)
f, q, U = r / Q, Q, []
u = Function('u')(z)
a = Symbol('a', dummy=True)
for k, d in enumerate(poly_sqf(q, z)):
n, b = k + 1, d.as_basic()
U += [ u.diff(z, k) ]
h = together(Poly.cancel(f*b**n, z) / u**n)
H, subs = [h], []
for j in range(1, n):
H += [ H[-1].diff(z) / j ]
for j in range(1, n+1):
subs += [ (U[j-1], b.diff(z, j) / j) ]
for j in range(0, n):
P, Q = together(H[j]).as_numer_denom()
for i in range(0, j+1):
P = P.subs(*subs[j-i])
Q = Q.subs(*subs[0])
P, Q = Poly(P, z), Poly(Q, z)
G = poly_gcd(P, d)
D = poly_quo(d, G)
B, g = poly_half_gcdex(Q, D)
b = poly_rem(P * poly_quo(B, g), D)
numer = b.as_basic()
denom = (z-a)**(n-j)
expr = numer.subs(z, a) / denom
partial += RootSum(Lambda(a, expr), D, **flags)
return partial