test_sympify.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. from sympy.core.add import Add
  2. from sympy.core.containers import Tuple
  3. from sympy.core.function import (Function, Lambda)
  4. from sympy.core.mul import Mul
  5. from sympy.core.numbers import (Float, I, Integer, Rational, pi, oo)
  6. from sympy.core.power import Pow
  7. from sympy.core.singleton import S
  8. from sympy.core.symbol import Symbol
  9. from sympy.functions.elementary.complexes import Abs
  10. from sympy.functions.elementary.exponential import exp
  11. from sympy.functions.elementary.miscellaneous import sqrt
  12. from sympy.functions.elementary.trigonometric import (cos, sin)
  13. from sympy.logic.boolalg import (false, Or, true, Xor)
  14. from sympy.matrices.dense import Matrix
  15. from sympy.parsing.sympy_parser import null
  16. from sympy.polys.polytools import Poly
  17. from sympy.printing.repr import srepr
  18. from sympy.sets.fancysets import Range
  19. from sympy.sets.sets import Interval
  20. from sympy.abc import x, y
  21. from sympy.core.sympify import (sympify, _sympify, SympifyError, kernS,
  22. CantSympify, converter)
  23. from sympy.core.decorators import _sympifyit
  24. from sympy.external import import_module
  25. from sympy.testing.pytest import raises, XFAIL, skip, warns_deprecated_sympy
  26. from sympy.utilities.decorator import conserve_mpmath_dps
  27. from sympy.geometry import Point, Line
  28. from sympy.functions.combinatorial.factorials import factorial, factorial2
  29. from sympy.abc import _clash, _clash1, _clash2
  30. from sympy.external.gmpy import HAS_GMPY
  31. from sympy.sets import FiniteSet, EmptySet
  32. from sympy.tensor.array.dense_ndim_array import ImmutableDenseNDimArray
  33. import mpmath
  34. from collections import defaultdict, OrderedDict
  35. from mpmath.rational import mpq
  36. numpy = import_module('numpy')
  37. def test_issue_3538():
  38. v = sympify("exp(x)")
  39. assert v == exp(x)
  40. assert type(v) == type(exp(x))
  41. assert str(type(v)) == str(type(exp(x)))
  42. def test_sympify1():
  43. assert sympify("x") == Symbol("x")
  44. assert sympify(" x") == Symbol("x")
  45. assert sympify(" x ") == Symbol("x")
  46. # issue 4877
  47. assert sympify('--.5') == 0.5
  48. assert sympify('-1/2') == -S.Half
  49. assert sympify('-+--.5') == -0.5
  50. assert sympify('-.[3]') == Rational(-1, 3)
  51. assert sympify('.[3]') == Rational(1, 3)
  52. assert sympify('+.[3]') == Rational(1, 3)
  53. assert sympify('+0.[3]*10**-2') == Rational(1, 300)
  54. assert sympify('.[052631578947368421]') == Rational(1, 19)
  55. assert sympify('.0[526315789473684210]') == Rational(1, 19)
  56. assert sympify('.034[56]') == Rational(1711, 49500)
  57. # options to make reals into rationals
  58. assert sympify('1.22[345]', rational=True) == \
  59. 1 + Rational(22, 100) + Rational(345, 99900)
  60. assert sympify('2/2.6', rational=True) == Rational(10, 13)
  61. assert sympify('2.6/2', rational=True) == Rational(13, 10)
  62. assert sympify('2.6e2/17', rational=True) == Rational(260, 17)
  63. assert sympify('2.6e+2/17', rational=True) == Rational(260, 17)
  64. assert sympify('2.6e-2/17', rational=True) == Rational(26, 17000)
  65. assert sympify('2.1+3/4', rational=True) == \
  66. Rational(21, 10) + Rational(3, 4)
  67. assert sympify('2.234456', rational=True) == Rational(279307, 125000)
  68. assert sympify('2.234456e23', rational=True) == 223445600000000000000000
  69. assert sympify('2.234456e-23', rational=True) == \
  70. Rational(279307, 12500000000000000000000000000)
  71. assert sympify('-2.234456e-23', rational=True) == \
  72. Rational(-279307, 12500000000000000000000000000)
  73. assert sympify('12345678901/17', rational=True) == \
  74. Rational(12345678901, 17)
  75. assert sympify('1/.3 + x', rational=True) == Rational(10, 3) + x
  76. # make sure longs in fractions work
  77. assert sympify('222222222222/11111111111') == \
  78. Rational(222222222222, 11111111111)
  79. # ... even if they come from repetend notation
  80. assert sympify('1/.2[123456789012]') == Rational(333333333333, 70781892967)
  81. # ... or from high precision reals
  82. assert sympify('.1234567890123456', rational=True) == \
  83. Rational(19290123283179, 156250000000000)
  84. def test_sympify_Fraction():
  85. try:
  86. import fractions
  87. except ImportError:
  88. pass
  89. else:
  90. value = sympify(fractions.Fraction(101, 127))
  91. assert value == Rational(101, 127) and type(value) is Rational
  92. def test_sympify_gmpy():
  93. if HAS_GMPY:
  94. if HAS_GMPY == 2:
  95. import gmpy2 as gmpy
  96. elif HAS_GMPY == 1:
  97. import gmpy
  98. value = sympify(gmpy.mpz(1000001))
  99. assert value == Integer(1000001) and type(value) is Integer
  100. value = sympify(gmpy.mpq(101, 127))
  101. assert value == Rational(101, 127) and type(value) is Rational
  102. @conserve_mpmath_dps
  103. def test_sympify_mpmath():
  104. value = sympify(mpmath.mpf(1.0))
  105. assert value == Float(1.0) and type(value) is Float
  106. mpmath.mp.dps = 12
  107. assert sympify(
  108. mpmath.pi).epsilon_eq(Float("3.14159265359"), Float("1e-12")) == True
  109. assert sympify(
  110. mpmath.pi).epsilon_eq(Float("3.14159265359"), Float("1e-13")) == False
  111. mpmath.mp.dps = 6
  112. assert sympify(
  113. mpmath.pi).epsilon_eq(Float("3.14159"), Float("1e-5")) == True
  114. assert sympify(
  115. mpmath.pi).epsilon_eq(Float("3.14159"), Float("1e-6")) == False
  116. mpmath.mp.dps = 15
  117. assert sympify(mpmath.mpc(1.0 + 2.0j)) == Float(1.0) + Float(2.0)*I
  118. assert sympify(mpq(1, 2)) == S.Half
  119. def test_sympify2():
  120. class A:
  121. def _sympy_(self):
  122. return Symbol("x")**3
  123. a = A()
  124. assert _sympify(a) == x**3
  125. assert sympify(a) == x**3
  126. assert a == x**3
  127. def test_sympify3():
  128. assert sympify("x**3") == x**3
  129. assert sympify("x^3") == x**3
  130. assert sympify("1/2") == Integer(1)/2
  131. raises(SympifyError, lambda: _sympify('x**3'))
  132. raises(SympifyError, lambda: _sympify('1/2'))
  133. def test_sympify_keywords():
  134. raises(SympifyError, lambda: sympify('if'))
  135. raises(SympifyError, lambda: sympify('for'))
  136. raises(SympifyError, lambda: sympify('while'))
  137. raises(SympifyError, lambda: sympify('lambda'))
  138. def test_sympify_float():
  139. assert sympify("1e-64") != 0
  140. assert sympify("1e-20000") != 0
  141. def test_sympify_bool():
  142. assert sympify(True) is true
  143. assert sympify(False) is false
  144. def test_sympyify_iterables():
  145. ans = [Rational(3, 10), Rational(1, 5)]
  146. assert sympify(['.3', '.2'], rational=True) == ans
  147. assert sympify({"x": 0, "y": 1}) == {x: 0, y: 1}
  148. assert sympify(['1', '2', ['3', '4']]) == [S(1), S(2), [S(3), S(4)]]
  149. @XFAIL
  150. def test_issue_16772():
  151. # because there is a converter for tuple, the
  152. # args are only sympified without the flags being passed
  153. # along; list, on the other hand, is not converted
  154. # with a converter so its args are traversed later
  155. ans = [Rational(3, 10), Rational(1, 5)]
  156. assert sympify(('.3', '.2'), rational=True) == Tuple(*ans)
  157. def test_issue_16859():
  158. class no(float, CantSympify):
  159. pass
  160. raises(SympifyError, lambda: sympify(no(1.2)))
  161. def test_sympify4():
  162. class A:
  163. def _sympy_(self):
  164. return Symbol("x")
  165. a = A()
  166. assert _sympify(a)**3 == x**3
  167. assert sympify(a)**3 == x**3
  168. assert a == x
  169. def test_sympify_text():
  170. assert sympify('some') == Symbol('some')
  171. assert sympify('core') == Symbol('core')
  172. assert sympify('True') is True
  173. assert sympify('False') is False
  174. assert sympify('Poly') == Poly
  175. assert sympify('sin') == sin
  176. def test_sympify_function():
  177. assert sympify('factor(x**2-1, x)') == -(1 - x)*(x + 1)
  178. assert sympify('sin(pi/2)*cos(pi)') == -Integer(1)
  179. def test_sympify_poly():
  180. p = Poly(x**2 + x + 1, x)
  181. assert _sympify(p) is p
  182. assert sympify(p) is p
  183. def test_sympify_factorial():
  184. assert sympify('x!') == factorial(x)
  185. assert sympify('(x+1)!') == factorial(x + 1)
  186. assert sympify('(1 + y*(x + 1))!') == factorial(1 + y*(x + 1))
  187. assert sympify('(1 + y*(x + 1)!)^2') == (1 + y*factorial(x + 1))**2
  188. assert sympify('y*x!') == y*factorial(x)
  189. assert sympify('x!!') == factorial2(x)
  190. assert sympify('(x+1)!!') == factorial2(x + 1)
  191. assert sympify('(1 + y*(x + 1))!!') == factorial2(1 + y*(x + 1))
  192. assert sympify('(1 + y*(x + 1)!!)^2') == (1 + y*factorial2(x + 1))**2
  193. assert sympify('y*x!!') == y*factorial2(x)
  194. assert sympify('factorial2(x)!') == factorial(factorial2(x))
  195. raises(SympifyError, lambda: sympify("+!!"))
  196. raises(SympifyError, lambda: sympify(")!!"))
  197. raises(SympifyError, lambda: sympify("!"))
  198. raises(SympifyError, lambda: sympify("(!)"))
  199. raises(SympifyError, lambda: sympify("x!!!"))
  200. def test_issue_3595():
  201. assert sympify("a_") == Symbol("a_")
  202. assert sympify("_a") == Symbol("_a")
  203. def test_lambda():
  204. x = Symbol('x')
  205. assert sympify('lambda: 1') == Lambda((), 1)
  206. assert sympify('lambda x: x') == Lambda(x, x)
  207. assert sympify('lambda x: 2*x') == Lambda(x, 2*x)
  208. assert sympify('lambda x, y: 2*x+y') == Lambda((x, y), 2*x + y)
  209. def test_lambda_raises():
  210. raises(SympifyError, lambda: sympify("lambda *args: args")) # args argument error
  211. raises(SympifyError, lambda: sympify("lambda **kwargs: kwargs[0]")) # kwargs argument error
  212. raises(SympifyError, lambda: sympify("lambda x = 1: x")) # Keyword argument error
  213. with raises(SympifyError):
  214. _sympify('lambda: 1')
  215. def test_sympify_raises():
  216. raises(SympifyError, lambda: sympify("fx)"))
  217. class A:
  218. def __str__(self):
  219. return 'x'
  220. with warns_deprecated_sympy():
  221. assert sympify(A()) == Symbol('x')
  222. def test__sympify():
  223. x = Symbol('x')
  224. f = Function('f')
  225. # positive _sympify
  226. assert _sympify(x) is x
  227. assert _sympify(1) == Integer(1)
  228. assert _sympify(0.5) == Float("0.5")
  229. assert _sympify(1 + 1j) == 1.0 + I*1.0
  230. # Function f is not Basic and can't sympify to Basic. We allow it to pass
  231. # with sympify but not with _sympify.
  232. # https://github.com/sympy/sympy/issues/20124
  233. assert sympify(f) is f
  234. raises(SympifyError, lambda: _sympify(f))
  235. class A:
  236. def _sympy_(self):
  237. return Integer(5)
  238. a = A()
  239. assert _sympify(a) == Integer(5)
  240. # negative _sympify
  241. raises(SympifyError, lambda: _sympify('1'))
  242. raises(SympifyError, lambda: _sympify([1, 2, 3]))
  243. def test_sympifyit():
  244. x = Symbol('x')
  245. y = Symbol('y')
  246. @_sympifyit('b', NotImplemented)
  247. def add(a, b):
  248. return a + b
  249. assert add(x, 1) == x + 1
  250. assert add(x, 0.5) == x + Float('0.5')
  251. assert add(x, y) == x + y
  252. assert add(x, '1') == NotImplemented
  253. @_sympifyit('b')
  254. def add_raises(a, b):
  255. return a + b
  256. assert add_raises(x, 1) == x + 1
  257. assert add_raises(x, 0.5) == x + Float('0.5')
  258. assert add_raises(x, y) == x + y
  259. raises(SympifyError, lambda: add_raises(x, '1'))
  260. def test_int_float():
  261. class F1_1:
  262. def __float__(self):
  263. return 1.1
  264. class F1_1b:
  265. """
  266. This class is still a float, even though it also implements __int__().
  267. """
  268. def __float__(self):
  269. return 1.1
  270. def __int__(self):
  271. return 1
  272. class F1_1c:
  273. """
  274. This class is still a float, because it implements _sympy_()
  275. """
  276. def __float__(self):
  277. return 1.1
  278. def __int__(self):
  279. return 1
  280. def _sympy_(self):
  281. return Float(1.1)
  282. class I5:
  283. def __int__(self):
  284. return 5
  285. class I5b:
  286. """
  287. This class implements both __int__() and __float__(), so it will be
  288. treated as Float in SymPy. One could change this behavior, by using
  289. float(a) == int(a), but deciding that integer-valued floats represent
  290. exact numbers is arbitrary and often not correct, so we do not do it.
  291. If, in the future, we decide to do it anyway, the tests for I5b need to
  292. be changed.
  293. """
  294. def __float__(self):
  295. return 5.0
  296. def __int__(self):
  297. return 5
  298. class I5c:
  299. """
  300. This class implements both __int__() and __float__(), but also
  301. a _sympy_() method, so it will be Integer.
  302. """
  303. def __float__(self):
  304. return 5.0
  305. def __int__(self):
  306. return 5
  307. def _sympy_(self):
  308. return Integer(5)
  309. i5 = I5()
  310. i5b = I5b()
  311. i5c = I5c()
  312. f1_1 = F1_1()
  313. f1_1b = F1_1b()
  314. f1_1c = F1_1c()
  315. assert sympify(i5) == 5
  316. assert isinstance(sympify(i5), Integer)
  317. assert sympify(i5b) == 5.0
  318. assert isinstance(sympify(i5b), Float)
  319. assert sympify(i5c) == 5
  320. assert isinstance(sympify(i5c), Integer)
  321. assert abs(sympify(f1_1) - 1.1) < 1e-5
  322. assert abs(sympify(f1_1b) - 1.1) < 1e-5
  323. assert abs(sympify(f1_1c) - 1.1) < 1e-5
  324. assert _sympify(i5) == 5
  325. assert isinstance(_sympify(i5), Integer)
  326. assert _sympify(i5b) == 5.0
  327. assert isinstance(_sympify(i5b), Float)
  328. assert _sympify(i5c) == 5
  329. assert isinstance(_sympify(i5c), Integer)
  330. assert abs(_sympify(f1_1) - 1.1) < 1e-5
  331. assert abs(_sympify(f1_1b) - 1.1) < 1e-5
  332. assert abs(_sympify(f1_1c) - 1.1) < 1e-5
  333. def test_evaluate_false():
  334. cases = {
  335. '2 + 3': Add(2, 3, evaluate=False),
  336. '2**2 / 3': Mul(Pow(2, 2, evaluate=False), Pow(3, -1, evaluate=False), evaluate=False),
  337. '2 + 3 * 5': Add(2, Mul(3, 5, evaluate=False), evaluate=False),
  338. '2 - 3 * 5': Add(2, Mul(-1, Mul(3, 5,evaluate=False), evaluate=False), evaluate=False),
  339. '1 / 3': Mul(1, Pow(3, -1, evaluate=False), evaluate=False),
  340. 'True | False': Or(True, False, evaluate=False),
  341. '1 + 2 + 3 + 5*3 + integrate(x)': Add(1, 2, 3, Mul(5, 3, evaluate=False), x**2/2, evaluate=False),
  342. '2 * 4 * 6 + 8': Add(Mul(2, 4, 6, evaluate=False), 8, evaluate=False),
  343. '2 - 8 / 4': Add(2, Mul(-1, Mul(8, Pow(4, -1, evaluate=False), evaluate=False), evaluate=False), evaluate=False),
  344. '2 - 2**2': Add(2, Mul(-1, Pow(2, 2, evaluate=False), evaluate=False), evaluate=False),
  345. }
  346. for case, result in cases.items():
  347. assert sympify(case, evaluate=False) == result
  348. def test_issue_4133():
  349. a = sympify('Integer(4)')
  350. assert a == Integer(4)
  351. assert a.is_Integer
  352. def test_issue_3982():
  353. a = [3, 2.0]
  354. assert sympify(a) == [Integer(3), Float(2.0)]
  355. assert sympify(tuple(a)) == Tuple(Integer(3), Float(2.0))
  356. assert sympify(set(a)) == FiniteSet(Integer(3), Float(2.0))
  357. def test_S_sympify():
  358. assert S(1)/2 == sympify(1)/2 == S.Half
  359. assert (-2)**(S(1)/2) == sqrt(2)*I
  360. def test_issue_4788():
  361. assert srepr(S(1.0 + 0J)) == srepr(S(1.0)) == srepr(Float(1.0))
  362. def test_issue_4798_None():
  363. assert S(None) is None
  364. def test_issue_3218():
  365. assert sympify("x+\ny") == x + y
  366. def test_issue_19399():
  367. if not numpy:
  368. skip("numpy not installed.")
  369. a = numpy.array(Rational(1, 2))
  370. b = Rational(1, 3)
  371. assert (a * b, type(a * b)) == (b * a, type(b * a))
  372. def test_issue_4988_builtins():
  373. C = Symbol('C')
  374. vars = {'C': C}
  375. exp1 = sympify('C')
  376. assert exp1 == C # Make sure it did not get mixed up with sympy.C
  377. exp2 = sympify('C', vars)
  378. assert exp2 == C # Make sure it did not get mixed up with sympy.C
  379. def test_geometry():
  380. p = sympify(Point(0, 1))
  381. assert p == Point(0, 1) and isinstance(p, Point)
  382. L = sympify(Line(p, (1, 0)))
  383. assert L == Line((0, 1), (1, 0)) and isinstance(L, Line)
  384. def test_kernS():
  385. s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))'
  386. # when 1497 is fixed, this no longer should pass: the expression
  387. # should be unchanged
  388. assert -1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) == -1
  389. # sympification should not allow the constant to enter a Mul
  390. # or else the structure can change dramatically
  391. ss = kernS(s)
  392. assert ss != -1 and ss.simplify() == -1
  393. s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))'.replace(
  394. 'x', '_kern')
  395. ss = kernS(s)
  396. assert ss != -1 and ss.simplify() == -1
  397. # issue 6687
  398. assert (kernS('Interval(-1,-2 - 4*(-3))')
  399. == Interval(-1, Add(-2, Mul(12, 1, evaluate=False), evaluate=False)))
  400. assert kernS('_kern') == Symbol('_kern')
  401. assert kernS('E**-(x)') == exp(-x)
  402. e = 2*(x + y)*y
  403. assert kernS(['2*(x + y)*y', ('2*(x + y)*y',)]) == [e, (e,)]
  404. assert kernS('-(2*sin(x)**2 + 2*sin(x)*cos(x))*y/2') == \
  405. -y*(2*sin(x)**2 + 2*sin(x)*cos(x))/2
  406. # issue 15132
  407. assert kernS('(1 - x)/(1 - x*(1-y))') == kernS('(1-x)/(1-(1-y)*x)')
  408. assert kernS('(1-2**-(4+1)*(1-y)*x)') == (1 - x*(1 - y)/32)
  409. assert kernS('(1-2**(4+1)*(1-y)*x)') == (1 - 32*x*(1 - y))
  410. assert kernS('(1-2.*(1-y)*x)') == 1 - 2.*x*(1 - y)
  411. one = kernS('x - (x - 1)')
  412. assert one != 1 and one.expand() == 1
  413. assert kernS("(2*x)/(x-1)") == 2*x/(x-1)
  414. def test_issue_6540_6552():
  415. assert S('[[1/3,2], (2/5,)]') == [[Rational(1, 3), 2], (Rational(2, 5),)]
  416. assert S('[[2/6,2], (2/4,)]') == [[Rational(1, 3), 2], (S.Half,)]
  417. assert S('[[[2*(1)]]]') == [[[2]]]
  418. assert S('Matrix([2*(1)])') == Matrix([2])
  419. def test_issue_6046():
  420. assert str(S("Q & C", locals=_clash1)) == 'C & Q'
  421. assert str(S('pi(x)', locals=_clash2)) == 'pi(x)'
  422. locals = {}
  423. exec("from sympy.abc import Q, C", locals)
  424. assert str(S('C&Q', locals)) == 'C & Q'
  425. # clash can act as Symbol or Function
  426. assert str(S('pi(C, Q)', locals=_clash)) == 'pi(C, Q)'
  427. assert len(S('pi + x', locals=_clash2).free_symbols) == 2
  428. # but not both
  429. raises(TypeError, lambda: S('pi + pi(x)', locals=_clash2))
  430. assert all(set(i.values()) == {null} for i in (
  431. _clash, _clash1, _clash2))
  432. def test_issue_8821_highprec_from_str():
  433. s = str(pi.evalf(128))
  434. p = sympify(s)
  435. assert Abs(sin(p)) < 1e-127
  436. def test_issue_10295():
  437. if not numpy:
  438. skip("numpy not installed.")
  439. A = numpy.array([[1, 3, -1],
  440. [0, 1, 7]])
  441. sA = S(A)
  442. assert sA.shape == (2, 3)
  443. for (ri, ci), val in numpy.ndenumerate(A):
  444. assert sA[ri, ci] == val
  445. B = numpy.array([-7, x, 3*y**2])
  446. sB = S(B)
  447. assert sB.shape == (3,)
  448. assert B[0] == sB[0] == -7
  449. assert B[1] == sB[1] == x
  450. assert B[2] == sB[2] == 3*y**2
  451. C = numpy.arange(0, 24)
  452. C.resize(2,3,4)
  453. sC = S(C)
  454. assert sC[0, 0, 0].is_integer
  455. assert sC[0, 0, 0] == 0
  456. a1 = numpy.array([1, 2, 3])
  457. a2 = numpy.array(list(range(24)))
  458. a2.resize(2, 4, 3)
  459. assert sympify(a1) == ImmutableDenseNDimArray([1, 2, 3])
  460. assert sympify(a2) == ImmutableDenseNDimArray(list(range(24)), (2, 4, 3))
  461. def test_Range():
  462. # Only works in Python 3 where range returns a range type
  463. assert sympify(range(10)) == Range(10)
  464. assert _sympify(range(10)) == Range(10)
  465. def test_sympify_set():
  466. n = Symbol('n')
  467. assert sympify({n}) == FiniteSet(n)
  468. assert sympify(set()) == EmptySet
  469. def test_sympify_numpy():
  470. if not numpy:
  471. skip('numpy not installed. Abort numpy tests.')
  472. np = numpy
  473. def equal(x, y):
  474. return x == y and type(x) == type(y)
  475. assert sympify(np.bool_(1)) is S(True)
  476. try:
  477. assert equal(
  478. sympify(np.int_(1234567891234567891)), S(1234567891234567891))
  479. assert equal(
  480. sympify(np.intp(1234567891234567891)), S(1234567891234567891))
  481. except OverflowError:
  482. # May fail on 32-bit systems: Python int too large to convert to C long
  483. pass
  484. assert equal(sympify(np.intc(1234567891)), S(1234567891))
  485. assert equal(sympify(np.int8(-123)), S(-123))
  486. assert equal(sympify(np.int16(-12345)), S(-12345))
  487. assert equal(sympify(np.int32(-1234567891)), S(-1234567891))
  488. assert equal(
  489. sympify(np.int64(-1234567891234567891)), S(-1234567891234567891))
  490. assert equal(sympify(np.uint8(123)), S(123))
  491. assert equal(sympify(np.uint16(12345)), S(12345))
  492. assert equal(sympify(np.uint32(1234567891)), S(1234567891))
  493. assert equal(
  494. sympify(np.uint64(1234567891234567891)), S(1234567891234567891))
  495. assert equal(sympify(np.float32(1.123456)), Float(1.123456, precision=24))
  496. assert equal(sympify(np.float64(1.1234567891234)),
  497. Float(1.1234567891234, precision=53))
  498. # The exact precision of np.longdouble, npfloat128 and other extended
  499. # precision dtypes is platform dependent.
  500. ldprec = np.finfo(np.longdouble(1)).nmant + 1
  501. assert equal(sympify(np.longdouble(1.123456789)),
  502. Float(1.123456789, precision=ldprec))
  503. assert equal(sympify(np.complex64(1 + 2j)), S(1.0 + 2.0*I))
  504. assert equal(sympify(np.complex128(1 + 2j)), S(1.0 + 2.0*I))
  505. lcprec = np.finfo(np.longcomplex(1)).nmant + 1
  506. assert equal(sympify(np.longcomplex(1 + 2j)),
  507. Float(1.0, precision=lcprec) + Float(2.0, precision=lcprec)*I)
  508. #float96 does not exist on all platforms
  509. if hasattr(np, 'float96'):
  510. f96prec = np.finfo(np.float96(1)).nmant + 1
  511. assert equal(sympify(np.float96(1.123456789)),
  512. Float(1.123456789, precision=f96prec))
  513. #float128 does not exist on all platforms
  514. if hasattr(np, 'float128'):
  515. f128prec = np.finfo(np.float128(1)).nmant + 1
  516. assert equal(sympify(np.float128(1.123456789123)),
  517. Float(1.123456789123, precision=f128prec))
  518. @XFAIL
  519. def test_sympify_rational_numbers_set():
  520. ans = [Rational(3, 10), Rational(1, 5)]
  521. assert sympify({'.3', '.2'}, rational=True) == FiniteSet(*ans)
  522. def test_sympify_mro():
  523. """Tests the resolution order for classes that implement _sympy_"""
  524. class a:
  525. def _sympy_(self):
  526. return Integer(1)
  527. class b(a):
  528. def _sympy_(self):
  529. return Integer(2)
  530. class c(a):
  531. pass
  532. assert sympify(a()) == Integer(1)
  533. assert sympify(b()) == Integer(2)
  534. assert sympify(c()) == Integer(1)
  535. def test_sympify_converter():
  536. """Tests the resolution order for classes in converter"""
  537. class a:
  538. pass
  539. class b(a):
  540. pass
  541. class c(a):
  542. pass
  543. converter[a] = lambda x: Integer(1)
  544. converter[b] = lambda x: Integer(2)
  545. assert sympify(a()) == Integer(1)
  546. assert sympify(b()) == Integer(2)
  547. assert sympify(c()) == Integer(1)
  548. class MyInteger(Integer):
  549. pass
  550. if int in converter:
  551. int_converter = converter[int]
  552. else:
  553. int_converter = None
  554. try:
  555. converter[int] = MyInteger
  556. assert sympify(1) == MyInteger(1)
  557. finally:
  558. if int_converter is None:
  559. del converter[int]
  560. else:
  561. converter[int] = int_converter
  562. def test_issue_13924():
  563. if not numpy:
  564. skip("numpy not installed.")
  565. a = sympify(numpy.array([1]))
  566. assert isinstance(a, ImmutableDenseNDimArray)
  567. assert a[0] == 1
  568. def test_numpy_sympify_args():
  569. # Issue 15098. Make sure sympify args work with numpy types (like numpy.str_)
  570. if not numpy:
  571. skip("numpy not installed.")
  572. a = sympify(numpy.str_('a'))
  573. assert type(a) is Symbol
  574. assert a == Symbol('a')
  575. class CustomSymbol(Symbol):
  576. pass
  577. a = sympify(numpy.str_('a'), {"Symbol": CustomSymbol})
  578. assert isinstance(a, CustomSymbol)
  579. a = sympify(numpy.str_('x^y'))
  580. assert a == x**y
  581. a = sympify(numpy.str_('x^y'), convert_xor=False)
  582. assert a == Xor(x, y)
  583. raises(SympifyError, lambda: sympify(numpy.str_('x'), strict=True))
  584. a = sympify(numpy.str_('1.1'))
  585. assert isinstance(a, Float)
  586. assert a == 1.1
  587. a = sympify(numpy.str_('1.1'), rational=True)
  588. assert isinstance(a, Rational)
  589. assert a == Rational(11, 10)
  590. a = sympify(numpy.str_('x + x'))
  591. assert isinstance(a, Mul)
  592. assert a == 2*x
  593. a = sympify(numpy.str_('x + x'), evaluate=False)
  594. assert isinstance(a, Add)
  595. assert a == Add(x, x, evaluate=False)
  596. def test_issue_5939():
  597. a = Symbol('a')
  598. b = Symbol('b')
  599. assert sympify('''a+\nb''') == a + b
  600. def test_issue_16759():
  601. d = sympify({.5: 1})
  602. assert S.Half not in d
  603. assert Float(.5) in d
  604. assert d[.5] is S.One
  605. d = sympify(OrderedDict({.5: 1}))
  606. assert S.Half not in d
  607. assert Float(.5) in d
  608. assert d[.5] is S.One
  609. d = sympify(defaultdict(int, {.5: 1}))
  610. assert S.Half not in d
  611. assert Float(.5) in d
  612. assert d[.5] is S.One
  613. def test_issue_17811():
  614. a = Function('a')
  615. assert sympify('a(x)*5', evaluate=False) == Mul(a(x), 5, evaluate=False)
  616. def test_issue_8439():
  617. assert sympify(float('inf')) == oo
  618. assert x + float('inf') == x + oo
  619. assert S(float('inf')) == oo
  620. def test_issue_14706():
  621. if not numpy:
  622. skip("numpy not installed.")
  623. z1 = numpy.zeros((1, 1), dtype=numpy.float64)
  624. z2 = numpy.zeros((2, 2), dtype=numpy.float64)
  625. z3 = numpy.zeros((), dtype=numpy.float64)
  626. y1 = numpy.ones((1, 1), dtype=numpy.float64)
  627. y2 = numpy.ones((2, 2), dtype=numpy.float64)
  628. y3 = numpy.ones((), dtype=numpy.float64)
  629. assert numpy.all(x + z1 == numpy.full((1, 1), x))
  630. assert numpy.all(x + z2 == numpy.full((2, 2), x))
  631. assert numpy.all(z1 + x == numpy.full((1, 1), x))
  632. assert numpy.all(z2 + x == numpy.full((2, 2), x))
  633. for z in [z3,
  634. numpy.int64(0),
  635. numpy.float64(0),
  636. numpy.complex64(0)]:
  637. assert x + z == x
  638. assert z + x == x
  639. assert isinstance(x + z, Symbol)
  640. assert isinstance(z + x, Symbol)
  641. # If these tests fail, then it means that numpy has finally
  642. # fixed the issue of scalar conversion for rank>0 arrays
  643. # which is mentioned in numpy/numpy#10404. In that case,
  644. # some changes have to be made in sympify.py.
  645. # Note: For future reference, for anyone who takes up this
  646. # issue when numpy has finally fixed their side of the problem,
  647. # the changes for this temporary fix were introduced in PR 18651
  648. assert numpy.all(x + y1 == numpy.full((1, 1), x + 1.0))
  649. assert numpy.all(x + y2 == numpy.full((2, 2), x + 1.0))
  650. assert numpy.all(y1 + x == numpy.full((1, 1), x + 1.0))
  651. assert numpy.all(y2 + x == numpy.full((2, 2), x + 1.0))
  652. for y_ in [y3,
  653. numpy.int64(1),
  654. numpy.float64(1),
  655. numpy.complex64(1)]:
  656. assert x + y_ == y_ + x
  657. assert isinstance(x + y_, Add)
  658. assert isinstance(y_ + x, Add)
  659. assert x + numpy.array(x) == 2 * x
  660. assert x + numpy.array([x]) == numpy.array([2*x], dtype=object)
  661. assert sympify(numpy.array([1])) == ImmutableDenseNDimArray([1], 1)
  662. assert sympify(numpy.array([[[1]]])) == ImmutableDenseNDimArray([1], (1, 1, 1))
  663. assert sympify(z1) == ImmutableDenseNDimArray([0.0], (1, 1))
  664. assert sympify(z2) == ImmutableDenseNDimArray([0.0, 0.0, 0.0, 0.0], (2, 2))
  665. assert sympify(z3) == ImmutableDenseNDimArray([0.0], ())
  666. assert sympify(z3, strict=True) == 0.0
  667. raises(SympifyError, lambda: sympify(numpy.array([1]), strict=True))
  668. raises(SympifyError, lambda: sympify(z1, strict=True))
  669. raises(SympifyError, lambda: sympify(z2, strict=True))
  670. def test_issue_21536():
  671. #test to check evaluate=False in case of iterable input
  672. u = sympify("x+3*x+2", evaluate=False)
  673. v = sympify("2*x+4*x+2+4", evaluate=False)
  674. assert u.is_Add and set(u.args) == {x, 3*x, 2}
  675. assert v.is_Add and set(v.args) == {2*x, 4*x, 2, 4}
  676. assert sympify(["x+3*x+2", "2*x+4*x+2+4"], evaluate=False) == [u, v]
  677. #test to check evaluate=True in case of iterable input
  678. u = sympify("x+3*x+2", evaluate=True)
  679. v = sympify("2*x+4*x+2+4", evaluate=True)
  680. assert u.is_Add and set(u.args) == {4*x, 2}
  681. assert v.is_Add and set(v.args) == {6*x, 6}
  682. assert sympify(["x+3*x+2", "2*x+4*x+2+4"], evaluate=True) == [u, v]
  683. #test to check evaluate with no input in case of iterable input
  684. u = sympify("x+3*x+2")
  685. v = sympify("2*x+4*x+2+4")
  686. assert u.is_Add and set(u.args) == {4*x, 2}
  687. assert v.is_Add and set(v.args) == {6*x, 6}
  688. assert sympify(["x+3*x+2", "2*x+4*x+2+4"]) == [u, v]