Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct sympify for numpy arrays of size 1 #13926

Merged
merged 2 commits into from Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions sympy/core/sympify.py
Expand Up @@ -303,6 +303,15 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False,
except AttributeError:
pass

if not strict:
# Put numpy array conversion _before_ float/int, see
# <https://github.com/sympy/sympy/issues/13924>.
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):
try:
Expand All @@ -313,12 +322,6 @@ 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,
Expand Down
9 changes: 9 additions & 0 deletions sympy/core/tests/test_sympify.py
Expand Up @@ -602,3 +602,12 @@ 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():
if not numpy:
skip("numpy not installed.")

a = sympify(numpy.array([1]))
assert isinstance(a, ImmutableDenseNDimArray)
assert a[0] == 1