2 from sympycore
.core
import Expr
, Pair
, heads
, IntegerList
3 from sympycore
.heads
import *
8 def convert(cls
, obj
, typeerror
=True):
9 if isinstance(obj
, cls
):
11 if isinstance(obj
, (int, long, float, complex)):
12 return MyExpr(NUMBER
, obj
)
13 if isinstance(obj
, str):
14 return MyExpr(SYMBOL
, obj
)
16 raise TypeError('Cannot convert type %r to %r' % (type(obj
), cls
.__name
__))
19 def __add__(self
, other
):
20 return Add(self
, self
.convert(other
))
29 return MyExpr(NUMBER
, n
)
31 return MyExpr(SYMBOL
, s
)
39 return MyExpr(TERMS
, d
)
41 def test_equality_number():
42 assert Number(1)==Number(1)
43 assert Number(1)==Number(1L)
47 assert Number(1)==1.0+0j
53 assert Number(1)!=Number(2)
54 assert Number(1)!=Number(2L)
59 def test_equality_symbol():
60 assert Symbol('x')==Symbol('x')
61 assert Symbol('x')=='x'
62 assert 'x'==Symbol('x')
64 assert Symbol('x')!=Symbol('y')
65 assert Symbol('x')!='y'
66 assert 'x'!=Symbol('y')
68 assert MyExpr(SYMBOL
, 'x')==AExpr(SYMBOL
, 'x')
71 x
, y
, z
= map(Symbol
,'xyz')
72 assert MyExpr(POS
,x
)==x
73 assert hash(MyExpr(POS
,x
))==hash(x
)
76 x
, y
, z
= map(Symbol
,'xyz')
77 z
, o
, t
= map(Number
, [0,1,2])
78 assert MyExpr(NEG
, t
)==-2, MyExpr(NEG
, t
)
79 assert hash(MyExpr(NEG
, t
))==hash(-2), `
hash(MyExpr(NEG
, t
)),hash(-2)`
81 def test_equality_add():
82 x
, y
, z
= map(Symbol
,'xyz')
85 assert x
+ y
== (TERMS
, {x
:1, y
:1})
92 assert MyExpr(ADD
,[])==0,`
MyExpr(ADD
,[]).as_lowlevel()`
93 assert MyExpr(ADD
,[x
])==x
94 assert (not not MyExpr(ADD
,[])) == False
96 assert hash(MyExpr(ADD
,[]))==hash(0)
97 assert hash(MyExpr(ADD
,[x
]))==hash(x
)
99 x1
= AExpr(SYMBOL
, 'x')
100 y1
= AExpr(SYMBOL
, 'y')
102 assert AExpr(ADD
,[x1
])==MyExpr(ADD
,[x
])
103 assert AExpr(ADD
,[x1
,y1
])==MyExpr(ADD
,[x
,y
])
104 assert not (AExpr(ADD
,[x1
,y1
])<MyExpr(ADD
,[x
,y
]))
105 assert not (AExpr(ADD
,[x1
,y1
])>MyExpr(ADD
,[x
,y
]))
107 assert (AExpr(ADD
,[x1
,y1
])<=MyExpr(ADD
,[x
,y
]))
108 assert (AExpr(ADD
,[x1
,y1
])>=MyExpr(ADD
,[x
,y
]))
110 def test_equality_mul():
111 x
, y
, z
= map(Symbol
,'xyz')
112 assert (not not MyExpr(MUL
,[])) == True
113 assert MyExpr(MUL
,[])==1
114 assert MyExpr(MUL
,[x
])==x
115 assert hash(MyExpr(MUL
,[]))==hash(1)
116 assert hash(MyExpr(MUL
,[x
]))==hash(x
)
118 def test_equality_term_coeff():
119 x
, y
, z
= map(Symbol
,'xyz')
120 z
, o
, t
= map(Number
, [0,1,2])
121 assert MyExpr(TERM_COEFF
, (x
, 0))==0
122 assert MyExpr(TERM_COEFF
, (x
, 1))==x
123 assert MyExpr(TERM_COEFF
, (o
, 2))==2
124 assert MyExpr(TERM_COEFF
, (x
, -1))==MyExpr(NEG
, x
)
125 assert hash(MyExpr(TERM_COEFF
, (x
, 0)))==hash(0)
126 assert hash(MyExpr(TERM_COEFF
, (x
, 1)))==hash(x
)
127 assert hash(MyExpr(TERM_COEFF
, (o
, 2)))==hash(2)
128 assert hash(MyExpr(TERM_COEFF
, (x
, -1)))==hash(MyExpr(NEG
, x
))
131 def test_equality_term_coeff_dict():
132 x
, y
, z
= map(Symbol
,'xyz')
133 z
, o
, t
= map(Number
, [0,1,2])
134 assert MyExpr(TERM_COEFF_DICT
, {})==0
135 assert MyExpr(TERM_COEFF_DICT
, {x
:0})==0
136 assert MyExpr(TERM_COEFF_DICT
, {x
:1})==x
137 assert MyExpr(TERM_COEFF_DICT
, {x
:2})==MyExpr(TERM_COEFF
, (x
, 2))
138 assert MyExpr(TERM_COEFF_DICT
, {o
:2})==2
140 assert hash(MyExpr(TERM_COEFF_DICT
, {}))==hash(0)
141 assert hash(MyExpr(TERM_COEFF_DICT
, {x
:0}))==hash(0)
142 assert hash(MyExpr(TERM_COEFF_DICT
, {x
:1}))==hash(x
)
143 assert hash(MyExpr(TERM_COEFF_DICT
, {x
:2}))==hash(MyExpr(TERM_COEFF
, (x
, 2)))
144 assert hash(MyExpr(TERM_COEFF_DICT
, {o
:2}))==hash(2)
146 def test_equality_pow():
147 x
, y
, z
= map(Symbol
,'xyz')
148 z
, o
= map(Number
, [0,1])
149 assert MyExpr(POW
, (x
, 0))==1
150 assert MyExpr(POW
, (x
, 1))==x
151 assert MyExpr(POW
, (o
, x
))==1
153 assert hash(MyExpr(POW
, (x
, 0)))==hash(1)
154 assert hash(MyExpr(POW
, (x
, 1)))==hash(x
)
155 assert hash(MyExpr(POW
, (o
, x
)))==hash(1)
157 def test_equality_base_exp_dict():
158 x
, y
, z
= map(Symbol
,'xyz')
159 z
, o
, t
= map(Number
, [0,1,2])
160 assert MyExpr(BASE_EXP_DICT
, {})==1
161 assert MyExpr(BASE_EXP_DICT
, {x
:0})==1
162 assert MyExpr(BASE_EXP_DICT
, {x
:1})==x
163 assert MyExpr(BASE_EXP_DICT
, {x
:2})==MyExpr(POW
, (x
, 2))
164 assert MyExpr(BASE_EXP_DICT
, {o
:2})==1
166 assert hash(MyExpr(BASE_EXP_DICT
, {}))==hash(1)
167 assert hash(MyExpr(BASE_EXP_DICT
, {x
:0}))==hash(1)
168 assert hash(MyExpr(BASE_EXP_DICT
, {x
:1}))==hash(x
)
169 assert hash(MyExpr(BASE_EXP_DICT
, {x
:2}))==hash(MyExpr(POW
, (x
, 2)))
170 assert hash(MyExpr(BASE_EXP_DICT
, {o
:2}))==hash(1)
172 def test_equality_exp_coeff_dict():
173 x
, y
, z
= map(Symbol
,'xyz')
174 assert MyExpr(EXP_COEFF_DICT
, Pair((x
,), {}))==0
175 assert MyExpr(EXP_COEFF_DICT
, Pair((x
,), {IntegerList(0):2}))==2
176 assert MyExpr(EXP_COEFF_DICT
, Pair((x
,), {IntegerList(1):1}))==x
177 assert MyExpr(EXP_COEFF_DICT
, Pair((x
,), {IntegerList(1):2}))==MyExpr(TERM_COEFF
, (x
,2))
178 assert MyExpr(EXP_COEFF_DICT
, Pair((x
,), {IntegerList(2):1}))==MyExpr(POW
, (x
,2))
180 def test_hash_number():
181 assert hash(Number(1))==hash(1)
182 assert hash(Number(-1))==hash(-1)
183 assert hash(Number(1212424))==hash(1212424)
184 assert hash(Number(-1212424))==hash(-1212424)
186 def test_hash_symbol():
187 assert hash(Symbol('x'))==hash('x'),`
hash(Symbol('x')),hash('x')`
188 assert hash(Symbol('y'))==hash('y')
190 def test_hash_dict_data():
191 x
, y
, z
= map(Symbol
,'xyz')
192 assert hash(x
+ y
) == hash((TERMS
, frozenset([(x
,1),(y
,1)])))
194 def test_hash_list_data():
195 l
= map(MyExpr
,[1,2,3])
197 assert e1
.is_writable
198 e2
= MyExpr(MUL
, tuple(l
))
199 assert hash(e1
)==hash(e2
)
200 assert not e1
.is_writable
202 def test_is_writable():
203 n
, m
= map(MyExpr
, [1,2])
204 assert MyExpr(MUL
, [n
,m
]).is_writable
205 assert not MyExpr(MUL
, (n
,m
)).is_writable