Added a warning when constructing a Matrix without bracket + test modified
[sympy.git] / sympy / core / tests / test_sympify.py
blob07754a7101b9a81d3b56d408432745ea08c48c66
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
4 import py
5 #from sympy.utilities.pytest import XFAIL
8 def test_439():
9 v = sympify("exp(x)")
10 x = Symbol("x")
11 assert v == exp(x)
12 assert type(v) == type(exp(x))
13 assert str(type(v)) == str(type(exp(x)))
15 def test_sympify1():
16 assert sympify("x") == Symbol("x")
17 assert sympify(" x") == Symbol("x")
18 assert sympify(" x ") == Symbol("x")
20 def test_sympify2():
21 class A:
22 def _sympy_(self):
23 return Symbol("x")**3
25 a = A()
27 assert _sympify(a)== x**3
28 assert sympify(a) == x**3
29 assert a == x**3
31 def test_sympify3():
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')")
38 def test_sympify4():
39 class A:
40 def _sympy_(self):
41 return Symbol("x")
43 a = A()
45 assert _sympify(a)**3== x**3
46 assert sympify(a)**3 == x**3
47 assert a == x
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():
64 p = Poly(x**2+x+1, x)
66 assert _sympify(p) is p
67 assert sympify(p) is p
69 def test_sage():
70 # how to effectivelly test for the _sage_() method without having SAGE
71 # installed?
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_")
81 def test_bug496():
82 a_ = sympify("a_")
83 _a = sympify("_a")
85 def test_lambda():
86 x = Symbol('x')
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)")
96 def test__sympify():
97 x = Symbol('x')
98 f = Function('f')
100 # positive _sympify
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
107 class A:
108 def _sympy_(self):
109 return Integer(5)
111 a = A()
112 assert _sympify(a) == Integer(5)
114 # negative _sympify
115 py.test.raises(SympifyError, "_sympify('1')")
116 py.test.raises(SympifyError, "_sympify([1,2,3])")
119 def test_sympifyit():
120 x = Symbol('x')
121 y = Symbol('y')
123 @_sympifyit('b', NotImplemented)
124 def add(a, b):
125 return a+b
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
134 @_sympifyit('b')
135 def add_raises(a, b):
136 return 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():
145 class F1_1(object):
146 def __float__(self):
147 return 1.1
149 class F1_1b(object):
151 This class is still a float, even though it also implements __int__().
153 def __float__(self):
154 return 1.1
156 def __int__(self):
157 return 1
159 class F1_1c(object):
161 This class is still a float, because it implements _sympy_()
163 def __float__(self):
164 return 1.1
166 def __int__(self):
167 return 1
169 def _sympy_(self):
170 return Real(1.1)
172 class I5(object):
173 def __int__(self):
174 return 5
176 class I5b(object):
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
183 be changed.
185 def __float__(self):
186 return 5.0
188 def __int__(self):
189 return 5
191 class I5c(object):
193 This class implements both __int__() and __float__(), but also
194 a _sympy_() method, so it will be Integer.
196 def __float__(self):
197 return 5.0
199 def __int__(self):
200 return 5
202 def _sympy_(self):
203 return Integer(5)
205 i5 = I5()
206 i5b = I5b()
207 i5c = I5c()
208 f1_1 = F1_1()
209 f1_1b = F1_1b()
210 f1_1c = F1_1c()
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)
236 assert a.is_Integer