本文整理汇总了Python中sympy.core.add.Add._from_args方法的典型用法代码示例。如果您正苦于以下问题:Python Add._from_args方法的具体用法?Python Add._from_args怎么用?Python Add._from_args使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.core.add.Add
的用法示例。
在下文中一共展示了Add._from_args方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _eval_rewrite_as_Add
# 需要导入模块: from sympy.core.add import Add [as 别名]
# 或者: from sympy.core.add.Add import _from_args [as 别名]
def _eval_rewrite_as_Add(self, *args, **kwargs):
"""return Eq(L, R) as L - R. To control the evaluation of
the result set pass `evaluate=True` to give L - R;
if `evaluate=None` then terms in L and R will not cancel
but they will be listed in canonical order; otherwise
non-canonical args will be returned.
Examples
========
>>> from sympy import Eq, Add
>>> from sympy.abc import b, x
>>> eq = Eq(x + b, x - b)
>>> eq.rewrite(Add)
2*b
>>> eq.rewrite(Add, evaluate=None).args
(b, b, x, -x)
>>> eq.rewrite(Add, evaluate=False).args
(b, x, b, -x)
"""
L, R = args
evaluate = kwargs.get('evaluate', True)
if evaluate:
# allow cancellation of args
return L - R
args = Add.make_args(L) + Add.make_args(-R)
if evaluate is None:
# no cancellation, but canonical
return _unevaluated_Add(*args)
# no cancellation, not canonical
return Add._from_args(args)
示例2: factor_terms
# 需要导入模块: from sympy.core.add import Add [as 别名]
# 或者: from sympy.core.add.Add import _from_args [as 别名]
def factor_terms(expr, radical=False):
"""Remove common factors from terms in all arguments without
changing the underlying structure of the expr. No expansion or
simplification (and no processing of non-commutatives) is performed.
If radical=True then a radical common to all terms will be factored
out of any Add sub-expressions of the expr.
Examples
========
>>> from sympy import factor_terms, Symbol
>>> from sympy.abc import x, y
>>> factor_terms(x + x*(2 + 4*y)**3)
x*(8*(2*y + 1)**3 + 1)
>>> A = Symbol('A', commutative=False)
>>> factor_terms(x*A + x*A + x*y*A)
x*(y*A + 2*A)
"""
expr = sympify(expr)
is_iterable = iterable(expr)
if not isinstance(expr, Basic) or expr.is_Atom:
if is_iterable:
return type(expr)([factor_terms(i, radical=radical) for i in expr])
return expr
if expr.is_Function or is_iterable or not hasattr(expr, 'args_cnc'):
args = expr.args
newargs = tuple([factor_terms(i, radical=radical) for i in args])
if newargs == args:
return expr
return expr.func(*newargs)
cont, p = expr.as_content_primitive(radical=radical)
list_args, nc = zip(*[ai.args_cnc() for ai in Add.make_args(p)])
list_args = list(list_args)
nc = [((Dummy(), Mul._from_args(i)) if i else None) for i in nc]
ncreps = dict([i for i in nc if i is not None])
for i, a in enumerate(list_args):
if nc[i] is not None:
a.append(nc[i][0])
a = Mul._from_args(a) # gcd_terms will fix up ordering
list_args[i] = gcd_terms(a, isprimitive=True)
# cancel terms that may not have cancelled
p = Add._from_args(list_args) # gcd_terms will fix up ordering
p = gcd_terms(p, isprimitive=True).xreplace(ncreps)
return _keep_coeff(cont, p)
示例3: do
# 需要导入模块: from sympy.core.add import Add [as 别名]
# 或者: from sympy.core.add.Add import _from_args [as 别名]
def do(expr):
from sympy.concrete.summations import Sum
from sympy.simplify.simplify import factor_sum
is_iterable = iterable(expr)
if not isinstance(expr, Basic) or expr.is_Atom:
if is_iterable:
return type(expr)([do(i) for i in expr])
return expr
if expr.is_Pow or expr.is_Function or \
is_iterable or not hasattr(expr, 'args_cnc'):
args = expr.args
newargs = tuple([do(i) for i in args])
if newargs == args:
return expr
return expr.func(*newargs)
if isinstance(expr, Sum):
return factor_sum(expr, radical=radical, clear=clear, fraction=fraction, sign=sign)
cont, p = expr.as_content_primitive(radical=radical, clear=clear)
if p.is_Add:
list_args = [do(a) for a in Add.make_args(p)]
# get a common negative (if there) which gcd_terms does not remove
if all(a.as_coeff_Mul()[0].extract_multiplicatively(-1) is not None
for a in list_args):
cont = -cont
list_args = [-a for a in list_args]
# watch out for exp(-(x+2)) which gcd_terms will change to exp(-x-2)
special = {}
for i, a in enumerate(list_args):
b, e = a.as_base_exp()
if e.is_Mul and e != Mul(*e.args):
list_args[i] = Dummy()
special[list_args[i]] = a
# rebuild p not worrying about the order which gcd_terms will fix
p = Add._from_args(list_args)
p = gcd_terms(p,
isprimitive=True,
clear=clear,
fraction=fraction).xreplace(special)
elif p.args:
p = p.func(
*[do(a) for a in p.args])
rv = _keep_coeff(cont, p, clear=clear, sign=sign)
return rv
示例4: factor_terms
# 需要导入模块: from sympy.core.add import Add [as 别名]
# 或者: from sympy.core.add.Add import _from_args [as 别名]
def factor_terms(expr):
"""Remove common factors from terms in all arguments without
changing the underlying structure of the expr. No expansion or
simplification (and no processing of non-commutative) is performed.
**Examples**
>>> from sympy import factor_terms, Symbol
>>> from sympy.abc import x, y
>>> factor_terms(x + x*(2 + 4*y)**3)
x*(8*(2*y + 1)**3 + 1)
>>> A = Symbol('A', commutative=False)
>>> factor_terms(x*A + x*A + x*y*A)
x*(y*A + 2*A)
"""
expr = sympify(expr)
if iterable(expr):
return type(expr)([factor_terms(i) for i in expr])
if not isinstance(expr, Basic) or expr.is_Atom:
return expr
if expr.is_Function:
return expr.func(*[factor_terms(i) for i in expr.args])
cont, p = expr.as_content_primitive()
list_args, nc = zip(*[ai.args_cnc(clist=True) for ai in Add.make_args(p)])
list_args = list(list_args)
nc = [((Dummy(), Mul._from_args(i)) if i else None) for i in nc]
ncreps = dict([i for i in nc if i is not None])
for i, a in enumerate(list_args):
if nc[i] is not None:
a.append(nc[i][0])
a = Mul._from_args(a) # gcd_terms will fix up ordering
list_args[i] = gcd_terms(a, isprimitive=True)
# cancel terms that may not have cancelled
p = Add._from_args(list_args) # gcd_terms will fix up ordering
p = gcd_terms(p, isprimitive=True).subs(ncreps) # exact subs could be used here
return _keep_coeff(cont, p)
示例5: factor_terms
# 需要导入模块: from sympy.core.add import Add [as 别名]
# 或者: from sympy.core.add.Add import _from_args [as 别名]
def factor_terms(expr, radical=False, clear=False, fraction=False):
"""Remove common factors from terms in all arguments without
changing the underlying structure of the expr. No expansion or
simplification (and no processing of non-commutatives) is performed.
If radical=True then a radical common to all terms will be factored
out of any Add sub-expressions of the expr.
If clear=False (default) then coefficients will not be separated
from a single Add if they can be distributed to leave one or more
terms with integer coefficients.
If fraction=True (default is False) then a common denominator will be
constructed for the expression.
Examples
========
>>> from sympy import factor_terms, Symbol, Mul, primitive
>>> from sympy.abc import x, y
>>> factor_terms(x + x*(2 + 4*y)**3)
x*(8*(2*y + 1)**3 + 1)
>>> A = Symbol('A', commutative=False)
>>> factor_terms(x*A + x*A + x*y*A)
x*(y*A + 2*A)
When ``clear`` is False, a rational will only be factored out of an
Add expression if all terms of the Add have coefficients that are
fractions:
>>> factor_terms(x/2 + 1, clear=False)
x/2 + 1
>>> factor_terms(x/2 + 1, clear=True)
(x + 2)/2
This only applies when there is a single Add that the coefficient
multiplies:
>>> factor_terms(x*y/2 + y, clear=True)
y*(x + 2)/2
>>> factor_terms(x*y/2 + y, clear=False) == _
True
See Also
========
gcd_terms, sympy.polys.polytools.terms_gcd
"""
expr = sympify(expr)
is_iterable = iterable(expr)
if not isinstance(expr, Basic) or expr.is_Atom:
if is_iterable:
return type(expr)([factor_terms(i,
radical=radical,
clear=clear,
fraction=fraction) for i in expr])
return expr
if expr.is_Pow or expr.is_Function or is_iterable or not hasattr(expr, 'args_cnc'):
args = expr.args
newargs = tuple([factor_terms(i,
radical=radical,
clear=clear,
fraction=fraction) for i in args])
if newargs == args:
return expr
return expr.func(*newargs)
cont, p = expr.as_content_primitive(radical=radical)
if p.is_Add:
list_args = [gcd_terms(a,
isprimitive=True,
clear=clear,
fraction=fraction) for a in Add.make_args(p)]
p = Add._from_args(list_args) # gcd_terms will fix up ordering
elif p.args:
p = p.func(
*[factor_terms(a, radical, clear, fraction) for a in p.args])
p = gcd_terms(p,
isprimitive=True,
clear=clear,
fraction=fraction)
return _keep_coeff(cont, p, clear=clear)
示例6: factor_terms
# 需要导入模块: from sympy.core.add import Add [as 别名]
# 或者: from sympy.core.add.Add import _from_args [as 别名]
def factor_terms(expr, radical=False, clear=False):
"""Remove common factors from terms in all arguments without
changing the underlying structure of the expr. No expansion or
simplification (and no processing of non-commutatives) is performed.
If radical=True then a radical common to all terms will be factored
out of any Add sub-expressions of the expr.
If clear=False (default) then coefficients will not be separated
from a single Add if they can be distributed to leave one or more
terms with integer coefficients.
Examples
========
>>> from sympy import factor_terms, Symbol, Mul, primitive
>>> from sympy.abc import x, y
>>> factor_terms(x + x*(2 + 4*y)**3)
x*(8*(2*y + 1)**3 + 1)
>>> A = Symbol('A', commutative=False)
>>> factor_terms(x*A + x*A + x*y*A)
x*(y*A + 2*A)
When clear is False, a fraction will only appear factored out of an
Add expression if all terms of the Add have coefficients that are
fractions:
>>> factor_terms(x/2 + 1, clear=False)
x/2 + 1
>>> factor_terms(x/2 + 1, clear=True)
(x + 2)/2
This only applies when there is a single Add that the coefficient
multiplies:
>>> factor_terms(x*y/2 + y, clear=True)
y*(x + 2)/2
>>> factor_terms(x*y/2 + y, clear=False) == _
True
"""
expr = sympify(expr)
is_iterable = iterable(expr)
if not isinstance(expr, Basic) or expr.is_Atom:
if is_iterable:
return type(expr)([factor_terms(i, radical=radical, clear=clear) for i in expr])
return expr
if expr.is_Pow or expr.is_Function or is_iterable or not hasattr(expr, 'args_cnc'):
args = expr.args
newargs = tuple([factor_terms(i, radical=radical, clear=clear) for i in args])
if newargs == args:
return expr
return expr.func(*newargs)
cont, p = expr.as_content_primitive(radical=radical)
list_args, nc = zip(*[ai.args_cnc() for ai in Add.make_args(p)])
list_args = list(list_args)
nc = [((Dummy(), Mul._from_args(i)) if i else None) for i in nc]
ncreps = dict([i for i in nc if i is not None])
for i, a in enumerate(list_args):
if nc[i] is not None:
a.append(nc[i][0])
a = Mul._from_args(a) # gcd_terms will fix up ordering
list_args[i] = gcd_terms(a, isprimitive=True, clear=clear)
# cancel terms that may not have cancelled
p = Add._from_args(list_args) # gcd_terms will fix up ordering
p = gcd_terms(p, isprimitive=True, clear=clear).xreplace(ncreps)
return _keep_coeff(cont, p, clear=clear)