本文整理汇总了Python中sympy.Add方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.Add方法的具体用法?Python sympy.Add怎么用?Python sympy.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sympy_to_grim
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def sympy_to_grim(expr, **kwargs):
import sympy
assert isinstance(expr, sympy.Expr)
if expr.is_Integer:
return Expr(int(expr))
if expr.is_Symbol:
return Expr(symbol_name=expr.name)
if expr is sympy.pi:
return Pi
if expr is sympy.E:
return ConstE
if expr is sympy.I:
return ConstI
if expr.is_Add:
args = [sympy_to_grim(x, **kwargs) for x in expr.args]
return Add(*args)
if expr.is_Mul:
args = [sympy_to_grim(x, **kwargs) for x in expr.args]
return Mul(*args)
if expr.is_Pow:
args = [sympy_to_grim(x, **kwargs) for x in expr.args]
b, e = args
return Pow(b, e)
raise NotImplementedError("converting %s to Grim", type(expr))
示例2: _print_Sum
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _print_Sum(self, expr):
if len(expr.limits) == 1:
tex = r"\sum_{%s=%s}^{%s} " % \
tuple([ self._print(i) for i in expr.limits[0] ])
else:
def _format_ineq(l):
return r"%s \leq %s \leq %s" % \
tuple([self._print(s) for s in (l[1], l[0], l[2])])
tex = r"\sum_{\substack{%s}} " % \
str.join('\\\\', [ _format_ineq(l) for l in expr.limits ])
if isinstance(expr.function, Add):
tex += r"\left(%s\right)" % self._print(expr.function)
else:
tex += self._print(expr.function)
return tex
示例3: _print_Product
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _print_Product(self, expr):
if len(expr.limits) == 1:
tex = r"\prod_{%s=%s}^{%s} " % \
tuple([ self._print(i) for i in expr.limits[0] ])
else:
def _format_ineq(l):
return r"%s \leq %s \leq %s" % \
tuple([self._print(s) for s in (l[1], l[0], l[2])])
tex = r"\prod_{\substack{%s}} " % \
str.join('\\\\', [ _format_ineq(l) for l in expr.limits ])
if isinstance(expr.function, Add):
tex += r"\left(%s\right)" % self._print(expr.function)
else:
tex += self._print(expr.function)
return tex
示例4: _sympystr
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _sympystr(self, print_obj):
if len(self.terms) == 0:
return ZERO_STR
self = self._with_sorted_terms()
s = ''
for coef, pdop in self.terms:
coef_str = print_obj.doprint(coef)
pd_str = print_obj.doprint(pdop)
if coef == S(1):
s += pd_str
elif coef == S(-1):
s += '-' + pd_str
else:
if isinstance(coef, Add):
s += '(' + coef_str + ')*' + pd_str
else:
s += coef_str + '*' + pd_str
s += ' + '
s = s.replace('+ -', '- ')
s = s[:-3]
return s
示例5: _replace_op_func
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _replace_op_func(e, variable):
if isinstance(e, Operator):
return OperatorFunction(e, variable)
if e.is_Number:
return e
if isinstance(e, Pow):
return Pow(_replace_op_func(e.base, variable), e.exp)
new_args = [_replace_op_func(arg, variable) for arg in e.args]
if isinstance(e, Add):
return Add(*new_args)
elif isinstance(e, Mul):
return Mul(*new_args)
else:
return e
示例6: _eval_expand_expectation
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _eval_expand_expectation(self, **hints):
A = self.args[0]
if isinstance(A, Add):
# <A + B> = <A> + <B>
return Add(*(Expectation(a, self.is_normal_order).expand(expectation=True) for a in A.args))
if isinstance(A, Mul):
# <c A> = c<A> where c is a commutative term
A = A.expand()
cA, ncA = A.args_cnc()
return Mul(Mul(*cA), Expectation(Mul._from_args(ncA), self.is_normal_order).expand())
if isinstance(A, Integral):
# <∫adx> -> ∫<a>dx
func, lims = A.function, A.limits
new_args = [Expectation(func, self.is_normal_order).expand()]
for lim in lims:
new_args.append(lim)
return Integral(*new_args)
return self
示例7: _eval_expand_tensorproduct
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _eval_expand_tensorproduct(self, **hints):
"""Distribute TensorProducts across addition."""
args = self.args
add_args = []
stop = False
for i in range(len(args)):
if isinstance(args[i], Add):
for aa in args[i].args:
tp = TensorProduct(*args[:i] + (aa,) + args[i + 1:])
if isinstance(tp, TensorProduct):
tp = tp._eval_expand_tensorproduct()
add_args.append(tp)
break
if add_args:
return Add(*add_args)
else:
return self
示例8: _normal_ordered_form_terms
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _normal_ordered_form_terms(expr, independent=False, recursive_limit=10,
_recursive_depth=0):
"""
Helper function for normal_ordered_form: loop through each term in an
addition expression and call _normal_ordered_form_factor to perform the
factor to an normally ordered expression.
"""
new_terms = []
for term in expr.args:
if isinstance(term, Mul):
new_term = _normal_ordered_form_factor(
term, recursive_limit=recursive_limit,
_recursive_depth=_recursive_depth, independent=independent)
new_terms.append(new_term)
elif isinstance(term, Expectation):
term = Expectation(normal_ordered_form(term.args[0]), term.is_normal_order)
new_terms.append(term)
else:
new_terms.append(term)
return Add(*new_terms)
示例9: test_differentiable
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def test_differentiable():
a = Function(name="a", grid=Grid((10, 10)))
e = Function(name="e", grid=Grid((10, 10)))
assert isinstance(1.2 * a.dx, Mul)
assert isinstance(e + a, Add)
assert isinstance(e * a, Mul)
assert isinstance(a * a, Pow)
assert isinstance(1 / (a * a), Pow)
addition = a + 1.2 * a.dx
assert isinstance(addition, Add)
assert all(isinstance(a, Differentiable) for a in addition.args)
addition2 = a + e * a.dx
assert isinstance(addition2, Add)
assert all(isinstance(a, Differentiable) for a in addition2.args)
示例10: test_diffify
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def test_diffify():
a = Function(name="a", grid=Grid((10, 10)))
e = Function(name="e", grid=Grid((10, 10)))
assert isinstance(diffify(sympy.Mul(*[1.2, a.dx])), Mul)
assert isinstance(diffify(sympy.Add(*[a, e])), Add)
assert isinstance(diffify(sympy.Mul(*[e, a])), Mul)
assert isinstance(diffify(sympy.Mul(*[a, a])), Pow)
assert isinstance(diffify(sympy.Pow(*[a*a, -1])), Pow)
addition = diffify(sympy.Add(*[a, sympy.Mul(*[1.2, a.dx])]))
assert isinstance(addition, Add)
assert all(isinstance(a, Differentiable) for a in addition.args)
addition2 = diffify(sympy.Add(*[a, sympy.Mul(*[e, a.dx])]))
assert isinstance(addition2, Add)
assert all(isinstance(a, Differentiable) for a in addition2.args)
示例11: indexify
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def indexify(self, indices=None, lshift=False, subs=None):
"""Create a types.Indexed from the current object."""
if indices is not None:
return Indexed(self.indexed, *indices)
# Substitution for each index (spacing only used in own dimension)
subs = subs or {}
subs = [{**{d.spacing: 1, -d.spacing: -1}, **subs} for d in self.dimensions]
# Add halo shift
shift = self._size_nodomain.left if lshift else tuple([0]*len(self.dimensions))
# Indices after substitutions
indices = [sympy.sympify((a - o + f).xreplace(s)) for a, o, f, s in
zip(self.args, self.origin, shift, subs)]
indices = [i.xreplace({k: sympy.Integer(k) for k in i.atoms(sympy.Float)})
for i in indices]
return self.indexed[indices]
示例12: _add_question_or_entity
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _add_question_or_entity(context, p, q, is_question):
"""Generates entity or question for adding p + q."""
value = p.value + q.value
if is_question:
template = random.choice([
'{p} + {q}',
'{p}+{q}',
'Work out {p} + {q}.',
'Add {p} and {q}.',
'Put together {p} and {q}.',
'Sum {p} and {q}.',
'Total of {p} and {q}.',
'Add together {p} and {q}.',
'What is {p} plus {q}?',
'Calculate {p} + {q}.',
'What is {p} + {q}?',
])
return example.Problem(
question=example.question(context, template, p=p, q=q),
answer=value)
else:
return composition.Entity(
context=context,
value=value,
description='Let {self} = {p} + {q}.',
p=p, q=q)
示例13: _surd_coefficients
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def _surd_coefficients(sympy_exp):
"""Extracts coefficients a, b, where sympy_exp = a + b * sqrt(base)."""
sympy_exp = sympy.simplify(sympy.expand(sympy_exp))
def extract_b(b_sqrt_base):
"""Returns b from expression of form b * sqrt(base)."""
if isinstance(b_sqrt_base, sympy.Pow):
# Just form sqrt(base)
return 1
else:
assert isinstance(b_sqrt_base, sympy.Mul)
assert len(b_sqrt_base.args) == 2
assert b_sqrt_base.args[0].is_rational
assert isinstance(b_sqrt_base.args[1], sympy.Pow) # should be sqrt.
return b_sqrt_base.args[0]
if sympy_exp.is_rational:
# Form: a.
return sympy_exp, 0
elif isinstance(sympy_exp, sympy.Add):
# Form: a + b * sqrt(base)
assert len(sympy_exp.args) == 2
assert sympy_exp.args[0].is_rational
a = sympy_exp.args[0]
b = extract_b(sympy_exp.args[1])
return a, b
else:
# Form: b * sqrt(base).
return 0, extract_b(sympy_exp)
示例14: expanded_signs_and_terms
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def expanded_signs_and_terms(self):
"""Returns a list of arguments, plus any sub-arguments from sub-adds.
E.g., if this op is `Add(Add(2, Neg(3)), Mul(4, 5), 1)`, then will return
`[(True, 2), (False, 3), (True, Mul(4, 5)), (True, 1)]` (the arguments of
the inner add have been extracted).
"""
示例15: __init__
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Add [as 别名]
def __init__(self, *args):
super(Add, self).__init__(args)