本文整理匯總了Python中sympy.Pow方法的典型用法代碼示例。如果您正苦於以下問題:Python sympy.Pow方法的具體用法?Python sympy.Pow怎麽用?Python sympy.Pow使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sympy
的用法示例。
在下文中一共展示了sympy.Pow方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _replace_op_func
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [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
示例2: _expand_powers
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def _expand_powers(factors):
"""
Helper function for normal_ordered_form and normal_order: Expand a
power expression to a multiplication expression so that that the
expression can be handled by the normal ordering functions.
"""
new_factors = []
for factor in factors.args:
if (isinstance(factor, Pow)
and isinstance(factor.args[1], Integer)
and factor.args[1] > 0):
for n in range(factor.args[1]):
new_factors.append(factor.args[0])
else:
new_factors.append(factor)
return new_factors
示例3: test_commutator
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def test_commutator():
A = Operator('A')
B = Operator('B')
c = Commutator(A, B)
c_tall = Commutator(A**2, B)
assert str(c) == '[A,B]'
assert pretty(c) == '[A,B]'
assert upretty(c) == u('[A,B]')
assert latex(c) == r'\left[A,B\right]'
sT(c, "Commutator(Operator(Symbol('A')),Operator(Symbol('B')))")
assert str(c_tall) == '[A**2,B]'
ascii_str = \
"""\
[ 2 ]\n\
[A ,B]\
"""
ucode_str = \
u("""\
⎡ 2 ⎤\n\
⎣A ,B⎦\
""")
assert pretty(c_tall) == ascii_str
assert upretty(c_tall) == ucode_str
assert latex(c_tall) == r'\left[\left(A\right)^{2},B\right]'
sT(c_tall, "Commutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))")
示例4: test_differentiable
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [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)
示例5: test_diffify
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [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)
示例6: pow_to_mul
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def pow_to_mul(expr):
if expr.is_Atom or expr.is_Indexed:
return expr
elif expr.is_Pow:
base, exp = expr.as_base_exp()
if exp > 10 or exp < -10 or int(exp) != exp or exp == 0:
# Large and non-integer powers remain untouched
return expr
elif exp == -1:
# Reciprocals also remain untouched, but we traverse the base
# looking for other Pows
return expr.func(pow_to_mul(base), exp, evaluate=False)
elif exp > 0:
return sympy.Mul(*[base]*int(exp), evaluate=False)
else:
# SymPy represents 1/x as Pow(x,-1). Also, it represents
# 2/x as Mul(2, Pow(x, -1)). So we shouldn't end up here,
# but just in case SymPy changes its internal conventions...
posexpr = sympy.Mul(*[base]*(-int(exp)), evaluate=False)
return sympy.Pow(posexpr, -1, evaluate=False)
else:
return expr.func(*[pow_to_mul(i) for i in expr.args], evaluate=False)
示例7: convert_mp
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def convert_mp(mp):
if hasattr(mp, 'mp'):
mp_left = mp.mp(0)
mp_right = mp.mp(1)
else:
mp_left = mp.mp_nofunc(0)
mp_right = mp.mp_nofunc(1)
if mp.MUL() or mp.CMD_TIMES() or mp.CMD_CDOT():
lh = convert_mp(mp_left)
rh = convert_mp(mp_right)
return sympy.Mul(lh, rh, evaluate=False)
elif mp.DIV() or mp.CMD_DIV() or mp.COLON():
lh = convert_mp(mp_left)
rh = convert_mp(mp_right)
return sympy.Mul(lh, sympy.Pow(rh, -1, evaluate=False), evaluate=False)
else:
if hasattr(mp, 'unary'):
return convert_unary(mp.unary())
else:
return convert_unary(mp.unary_nofunc())
示例8: convert_exp
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def convert_exp(exp):
if hasattr(exp, 'exp'):
exp_nested = exp.exp()
else:
exp_nested = exp.exp_nofunc()
if exp_nested:
base = convert_exp(exp_nested)
if isinstance(base, list):
raise Exception("Cannot raise derivative to power")
if exp.atom():
exponent = convert_atom(exp.atom())
elif exp.expr():
exponent = convert_expr(exp.expr())
return sympy.Pow(base, exponent, evaluate=False)
else:
if hasattr(exp, 'comp'):
return convert_comp(exp.comp())
else:
return convert_comp(exp.comp_nofunc())
示例9: _surd_coefficients
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [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)
示例10: sympy
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def sympy(self):
return sympy.Mul(
self.children['numer'], sympy.Pow(self.children['denom'], -1))
示例11: __init__
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def __init__(self, a, b):
super(Pow, self).__init__({'a': a, 'b': b})
示例12: _print_Pow
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def _print_Pow(self, power):
from sympy.simplify.simplify import fraction
b, e = power.as_base_exp()
if power.is_commutative:
if e is S.NegativeOne:
return prettyForm("1")/self._print(b)
n, d = fraction(e)
if n is S.One and d.is_Atom and not e.is_Integer:
return self._print_nth_root(b, e)
if e.is_Rational and e < 0:
return prettyForm("1")/self._print(C.Pow(b, -e, evaluate=False))
return self._print(b)**self._print(e)
示例13: _print_ProductSet
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def _print_ProductSet(self, p):
if len(p.sets) > 1 and not has_variety(p.sets):
from sympy import Pow
return self._print(Pow(p.sets[0], len(p.sets), evaluate=False))
else:
prod_char = u('\xd7')
return self._print_seq(p.sets, None, None, ' %s ' % prod_char,
parenthesize=lambda set: set.is_Union or set.is_Intersection)
示例14: _sympystr
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def _sympystr(self, printer, *args):
from sympy.printing.str import sstr
length = len(self.args)
s = ''
for i in range(length):
if isinstance(self.args[i], (Add, Pow, Mul)):
s = s + '('
s = s + sstr(self.args[i])
if isinstance(self.args[i], (Add, Pow, Mul)):
s = s + ')'
if i != length - 1:
s = s + 'x'
return s
示例15: _getExpression
# 需要導入模塊: import sympy [as 別名]
# 或者: from sympy import Pow [as 別名]
def _getExpression(expr, input_dict):
"""
all the operations is dependent on the conditions
whether all the elements are leafs or only some of them.
Only return expressions and not the individual elements
"""
t = expr.args if len(expr.atoms()) > 1 else [expr]
# print t
# find out the length of the components within this node
t_lengths = np.array(list(map(_expressionLength, t)))
# print(tLengths)
if np.all(t_lengths == 0):
# if all components are leafs, then the node is an expression
input_dict.setdefault(expr, 0)
input_dict[expr] += 1
else:
for i, ti in enumerate(t):
# if the leaf is a singleton, then it is an expression
# else, go further along the tree
if t_lengths[i] == 0:
input_dict.setdefault(ti, 0)
input_dict[ti] += 1
else:
if isinstance(ti, sympy.Mul):
_getExpression(ti, input_dict)
elif isinstance(ti, sympy.Pow):
input_dict.setdefault(ti, 0)
input_dict[ti] += 1