本文整理汇总了Python中sage.symbolic.ring.SR.operands方法的典型用法代码示例。如果您正苦于以下问题:Python SR.operands方法的具体用法?Python SR.operands怎么用?Python SR.operands使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.symbolic.ring.SR
的用法示例。
在下文中一共展示了SR.operands方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: simplify_sqrt_real
# 需要导入模块: from sage.symbolic.ring import SR [as 别名]
# 或者: from sage.symbolic.ring.SR import operands [as 别名]
def simplify_sqrt_real(expr):
r"""
Simplify ``sqrt`` in symbolic expressions in the real domain.
EXAMPLES:
Simplifications of basic expressions::
sage: from sage.manifolds.utilities import simplify_sqrt_real
sage: simplify_sqrt_real( sqrt(x^2) )
abs(x)
sage: assume(x<0)
sage: simplify_sqrt_real( sqrt(x^2) )
-x
sage: simplify_sqrt_real( sqrt(x^2-2*x+1) )
-x + 1
sage: simplify_sqrt_real( sqrt(x^2) + sqrt(x^2-2*x+1) )
-2*x + 1
This improves over Sage's
:meth:`~sage.symbolic.expression.Expression.canonicalize_radical`,
which yields incorrect results when ``x < 0``::
sage: forget() # removes the assumption x<0
sage: sqrt(x^2).canonicalize_radical()
x
sage: assume(x<0)
sage: sqrt(x^2).canonicalize_radical() # wrong output
x
sage: sqrt(x^2-2*x+1).canonicalize_radical() # wrong output
x - 1
sage: ( sqrt(x^2) + sqrt(x^2-2*x+1) ).canonicalize_radical() # wrong output
2*x - 1
Simplification of nested ``sqrt``'s::
sage: forget() # removes the assumption x<0
sage: simplify_sqrt_real( sqrt(1 + sqrt(x^2)) )
sqrt(abs(x) + 1)
sage: assume(x<0)
sage: simplify_sqrt_real( sqrt(1 + sqrt(x^2)) )
sqrt(-x + 1)
sage: simplify_sqrt_real( sqrt(x^2 + sqrt(4*x^2) + 1) )
-x + 1
Again, :meth:`~sage.symbolic.expression.Expression.canonicalize_radical`
fails on the last one::
sage: (sqrt(x^2 + sqrt(4*x^2) + 1)).canonicalize_radical() # wrong output
x + 1
"""
from sage.symbolic.ring import SR
from sage.functions.other import sqrt
# 1/ Search for the sqrt's in expr
sexpr = str(expr)
if 'sqrt(' not in sexpr: # no sqrt to simplify
return expr
if 'D[' in sexpr:
return expr #!# the code below is not capable of simplifying
# expressions with symbolic derivatives denoted by Pynac
# symbols of the type D[0]
# Lists to store the positions of all the top-level sqrt's in sexpr:
pos_sqrts = [] # position of first character, i.e. 's' of 'sqrt(...)'
pos_after = [] # position of character immediatelty after 'sqrt(...)'
the_sqrts = [] # the sqrt sub-expressions in sexpr, i.e. 'sqrt(...)'
pos_max = len(sexpr) - 6
pos = 0
while pos < pos_max:
if sexpr[pos:pos+5] == 'sqrt(':
pos_sqrts.append(pos)
parenth = 1
scan = pos+5
while parenth != 0:
if sexpr[scan] == '(': parenth += 1
if sexpr[scan] == ')': parenth -= 1
scan += 1
the_sqrts.append( sexpr[pos:scan] )
pos_after.append(scan)
pos = scan
else:
pos += 1
# 2/ Search for sub-sqrt's:
for i in range(len(the_sqrts)):
argum = the_sqrts[i][5:-1] # the sqrt argument
if 'sqrt(' in argum:
simpl = simplify_sqrt_real(SR(argum))
the_sqrts[i] = 'sqrt(' + str(simpl) + ')'
# 3/ Simplifications of the sqrt's
new_expr = "" # will contain the result
pos0 = 0
for i, pos in enumerate(pos_sqrts):
# radcan is called on each sqrt:
x = SR(the_sqrts[i])
argum = x.operands()[0] # the argument of sqrt
den = argum.denominator()
if not (den == 1): # the argument of sqrt is a fraction
# NB: after #19312 (integrated in Sage 6.10.beta7), the above
# cannot be written as
# if den != 1!:
#.........这里部分代码省略.........