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

Eliminates unconditional conversion of numpy arrays of length 1 #13927

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 6 additions & 6 deletions sympy/core/sympify.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False,
except AttributeError:
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):
try:
Expand All @@ -313,12 +319,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
5 changes: 5 additions & 0 deletions sympy/core/tests/test_sympify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -602,3 +603,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]) == Array([1])