Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
if u == ZERO: return -v
if u == v: return ZERO
if u == -v or v == -u: return ZERO
elif op == '*':
if u == ZERO or v == ZERO: return ZERO
if u == ONE: return v
if v == ONE: return u
if u == v: return u ** 2
elif op == '/':
if u == ZERO: return ZERO
if v == ZERO: return Expr('Undefined')
if u == v: return ONE
if u == -v or v == -u: return ZERO
elif op == '**':
if u == ZERO: return ZERO
if v == ZERO: return ONE
if u == ONE: return ONE
if v == ONE: return u
elif op == 'log':
if u == ONE: return ZERO
else: raise ValueError("Unknown op: " + op)
## If we fall through to here, we can not simplify further
return Expr(op, *args)
def d(y, x):
"Differentiate and then simplify."
return simp(diff(y, x))
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
#_______________________________________________________________________________
# Utilities for doctest cases
# These functions print their arguments in a standard order
# to compensate for the random order in the standard representation
def ppsubst(s):
"""Print substitution s"""
ppdict(s)
def ppdict(d):
"""Print the dictionary d.
Prints a string representation of the dictionary
with keys in sorted order according to their string
representation: {a: A, d: D, ...}.
>>> ppdict({'m': 'M', 'a': 'A', 'r': 'R', 'k': 'K'})
{'a': 'A', 'k': 'K', 'm': 'M', 'r': 'R'}
>>> ppdict({z: C, y: B, x: A})
{x: A, y: B, z: C}
"""
def format(k, v):
return "%s: %s" % (repr(k), repr(v))
ditems = d.items()
ditems.sort(key=str)
k, v = ditems[0]
dpairs = format(k, v)
for (k, v) in ditems[1:]:
dpairs += (', ' + format(k, v))
print '{%s}' % dpairs
def ppset(s):
"""Print the set s.
>>> ppset(set(['A', 'Q', 'F', 'K', 'Y', 'B']))
set(['A', 'B', 'F', 'K', 'Q', 'Y'])
>>> ppset(set([z, y, x]))
set([x, y, z])
"""
slist = list(s)
slist.sort(key=str)
print 'set(%s)' % slist