From 09d4984f7680417d12554a7fcf8b5f11c387adc6 Mon Sep 17 00:00:00 2001 From: Shikhar Makhija Date: Mon, 15 Jan 2018 01:18:41 +0530 Subject: [PATCH 1/2] Fixes issue #13924 Changes made as suggested --- sympy/core/sympify.py | 15 ++++++++------- sympy/core/tests/test_sympify.py | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/sympy/core/sympify.py b/sympy/core/sympify.py index 74a04d2c3d9b..d15d4fbfa2b5 100644 --- a/sympy/core/sympify.py +++ b/sympy/core/sympify.py @@ -303,6 +303,14 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False, except AttributeError: pass + if iterable(a): + try: + return type(a)([sympify(x, locals=locals, convert_xor=convert_xor, + rational=rational) for x in a]) + except TypeError: + # Not all iterables are rebuildable with their type. + pass + if not isinstance(a, string_types): for coerce in (float, int): try: @@ -319,13 +327,6 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False, except AttributeError: pass - if iterable(a): - try: - return type(a)([sympify(x, locals=locals, convert_xor=convert_xor, - rational=rational) for x in a]) - except TypeError: - # Not all iterables are rebuildable with their type. - pass if isinstance(a, dict): try: return type(a)([sympify(x, locals=locals, convert_xor=convert_xor, diff --git a/sympy/core/tests/test_sympify.py b/sympy/core/tests/test_sympify.py index ae176ee5f87d..576e481da938 100644 --- a/sympy/core/tests/test_sympify.py +++ b/sympy/core/tests/test_sympify.py @@ -602,3 +602,7 @@ def equal(x, y): def test_sympify_rational_numbers_set(): ans = [Rational(3, 10), Rational(1, 5)] assert sympify({'.3', '.2'}, rational=True) == FiniteSet(*ans) + + +def test_issue_13924(): + assert S(1)*numpy.array([1]) == [1] From c3427741fa509f7d9d5949b9db7f2dc2e0d8e1d5 Mon Sep 17 00:00:00 2001 From: Shikhar Makhija Date: Mon, 15 Jan 2018 02:00:46 +0530 Subject: [PATCH 2/2] Code Restructured Handling for numpy arrays --- sympy/core/sympify.py | 25 ++++++++++++------------- sympy/core/tests/test_sympify.py | 3 ++- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/sympy/core/sympify.py b/sympy/core/sympify.py index d15d4fbfa2b5..4589f9c0801f 100644 --- a/sympy/core/sympify.py +++ b/sympy/core/sympify.py @@ -303,13 +303,11 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False, except AttributeError: pass - if iterable(a): - try: - return type(a)([sympify(x, locals=locals, convert_xor=convert_xor, - rational=rational) for x in a]) - except TypeError: - # Not all iterables are rebuildable with their type. - pass + try: + from ..tensor.array import Array + return Array(a.flat, a.shape) # works with e.g. NumPy arrays + except AttributeError: + pass if not isinstance(a, string_types): for coerce in (float, int): @@ -321,12 +319,13 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False, if strict: raise SympifyError(a) - try: - from ..tensor.array import Array - return Array(a.flat, a.shape) # works with e.g. NumPy arrays - except AttributeError: - pass - + if iterable(a): + try: + return type(a)([sympify(x, locals=locals, convert_xor=convert_xor, + rational=rational) for x in a]) + except TypeError: + # Not all iterables are rebuildable with their type. + pass if isinstance(a, dict): try: return type(a)([sympify(x, locals=locals, convert_xor=convert_xor, diff --git a/sympy/core/tests/test_sympify.py b/sympy/core/tests/test_sympify.py index 576e481da938..a5f1c73bb3f9 100644 --- a/sympy/core/tests/test_sympify.py +++ b/sympy/core/tests/test_sympify.py @@ -13,6 +13,7 @@ from sympy.core.compatibility import exec_, HAS_GMPY, PY3 from sympy.sets import FiniteSet, EmptySet from sympy.tensor.array.dense_ndim_array import ImmutableDenseNDimArray +from sympy.tensor.array import Array from sympy.external import import_module import mpmath @@ -605,4 +606,4 @@ def test_sympify_rational_numbers_set(): def test_issue_13924(): - assert S(1)*numpy.array([1]) == [1] + assert S(1)*numpy.array([1]) == Array([1])