1 from sympy
import Symbol
, exp
, Integer
, Real
, sin
, cos
, log
, Poly
, Lambda
, Function
, I
2 from sympy
.abc
import x
, y
3 from sympy
.core
.sympify
import sympify
, _sympify
, _sympifyit
, SympifyError
5 #from sympy.utilities.pytest import XFAIL
12 assert type(v
) == type(exp(x
))
13 assert str(type(v
)) == str(type(exp(x
)))
16 assert sympify("x") == Symbol("x")
17 assert sympify(" x") == Symbol("x")
18 assert sympify(" x ") == Symbol("x")
27 assert _sympify(a
)== x
**3
28 assert sympify(a
) == x
**3
32 assert sympify("x**3") == x
**3
33 assert sympify("1/2") == Integer(1)/2
35 py
.test
.raises(SympifyError
, "_sympify('x**3')")
36 py
.test
.raises(SympifyError
, "_sympify('1/2')")
45 assert _sympify(a
)**3== x
**3
46 assert sympify(a
)**3 == x
**3
49 def test_sympify_text():
50 assert sympify('some') == Symbol('some')
51 assert sympify('core') == Symbol('core')
53 assert sympify('True') == True
54 assert sympify('False') == False
56 assert sympify('Poly') == Poly
57 assert sympify('sin') == sin
59 def test_sympify_function():
60 assert sympify('factor(x**2-1, x)') == -(1-x
)*(x
+1)
61 assert sympify('sin(pi/2)*cos(pi)') == -Integer(1)
63 def test_sympify_poly():
66 assert _sympify(p
) is p
67 assert sympify(p
) is p
70 # how to effectivelly test for the _sage_() method without having SAGE
72 assert hasattr(x
, "_sage_")
73 assert hasattr(Integer(3), "_sage_")
74 assert hasattr(sin(x
), "_sage_")
75 assert hasattr(cos(x
), "_sage_")
76 assert hasattr(x
**2, "_sage_")
77 assert hasattr(x
+y
, "_sage_")
78 assert hasattr(exp(x
), "_sage_")
79 assert hasattr(log(x
), "_sage_")
87 assert sympify('lambda : 1')==Lambda(x
, 1)
88 assert sympify('lambda x: 2*x')==Lambda(x
, 2*x
)
90 py
.test
.raises(SympifyError
, "_sympify('lambda : 1')")
92 def test_sympify_raises():
93 py
.test
.raises(SympifyError
, sympify
, "fx)")
101 assert _sympify(x
) is x
102 assert _sympify(f
) is f
103 assert _sympify(1) == Integer(1)
104 assert _sympify(0.5) == Real("0.5")
105 assert _sympify(1+1j
) == 1 + I
112 assert _sympify(a
) == Integer(5)
115 py
.test
.raises(SympifyError
, "_sympify('1')")
116 py
.test
.raises(SympifyError
, "_sympify([1,2,3])")
119 def test_sympifyit():
123 @_sympifyit('b', NotImplemented)
127 assert add(x
, 1) == x
+1
128 assert add(x
, 0.5) == x
+Real('0.5')
129 assert add(x
, y
) == x
+y
131 assert add(x
, '1') == NotImplemented
135 def add_raises(a
, b
):
138 assert add_raises(x
, 1) == x
+1
139 assert add_raises(x
, 0.5) == x
+Real('0.5')
140 assert add_raises(x
, y
) == x
+y
142 py
.test
.raises(SympifyError
, "add_raises(x, '1')")
144 def test_int_float():
151 This class is still a float, even though it also implements __int__().
161 This class is still a float, because it implements _sympy_()
178 This class implements both __int__() and __float__(), so it will be
179 treated as Real in SymPy. One could change this behavior, by using
180 float(a) == int(a), but deciding that integer-valued floats represent
181 exact numbers is arbitrary and often not correct, so we do not do it.
182 If, in the future, we decide to do it anyway, the tests for I5b need to
193 This class implements both __int__() and __float__(), but also
194 a _sympy_() method, so it will be Integer.
211 assert sympify(i5
) == 5
212 assert isinstance(sympify(i5
), Integer
)
213 assert sympify(i5b
) == 5
214 assert isinstance(sympify(i5b
), Real
)
215 assert sympify(i5c
) == 5
216 assert isinstance(sympify(i5c
), Integer
)
217 assert abs(sympify(f1_1
) - 1.1) < 1e-5
218 assert abs(sympify(f1_1b
) - 1.1) < 1e-5
219 assert abs(sympify(f1_1c
) - 1.1) < 1e-5
221 assert _sympify(i5
) == 5
222 assert isinstance(_sympify(i5
), Integer
)
223 assert _sympify(i5b
) == 5
224 assert isinstance(_sympify(i5b
), Real
)
225 assert _sympify(i5c
) == 5
226 assert isinstance(_sympify(i5c
), Integer
)
227 assert abs(_sympify(f1_1
) - 1.1) < 1e-5
228 assert abs(_sympify(f1_1b
) - 1.1) < 1e-5
229 assert abs(_sympify(f1_1c
) - 1.1) < 1e-5
232 def test_issue1034():
233 a
= sympify('Integer(4)')
235 assert a
== Integer(4)