Skip to content

Commit

Permalink
Merge pull request sympy#11666 from ArifAhmed1995/core
Browse files Browse the repository at this point in the history
sympify : Added support for translating basic numpy datatypes
  • Loading branch information
asmeurer authored Mar 24, 2017
2 parents 035b2d6 + 0f3b831 commit 7a34d3b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
17 changes: 17 additions & 0 deletions sympy/core/sympify.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False,
else:
return a

#Support for basic numpy datatypes
if type(a).__module__ == 'numpy':
import numpy as np
if np.isscalar(a):
if not isinstance(a, np.floating):
return sympify(np.asscalar(a))
else:
try:
from sympy.core.numbers import Float
prec = np.finfo(a).nmant
a = str(list(np.reshape(np.asarray(a),
(1, np.size(a)))[0]))[1:-1]
return Float(a, precision=prec)
except NotImplementedError:
raise SympifyError('Translation for numpy float : %s '
'is not implemented' % a)

try:
return converter[cls](a)
except KeyError:
Expand Down
49 changes: 48 additions & 1 deletion sympy/core/tests/test_sympify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sympy import (Symbol, exp, Integer, Float, sin, cos, log, Poly, Lambda,
Function, I, S, sqrt, srepr, Rational, Tuple, Matrix, Interval, Add, Mul,
Function, I, S, N, sqrt, srepr, Rational, Tuple, Matrix, Interval, Add, Mul,
Pow, Or, true, false, Abs, pi, Range)
from sympy.abc import x, y
from sympy.core.sympify import sympify, _sympify, SympifyError, kernS
Expand All @@ -11,6 +11,7 @@
from sympy.abc import _clash, _clash1, _clash2
from sympy.core.compatibility import exec_, HAS_GMPY, PY3
from sympy.sets import FiniteSet, EmptySet
from sympy.external import import_module

import mpmath

Expand Down Expand Up @@ -512,6 +513,52 @@ def test_sympify_set():
assert sympify(set()) == EmptySet()


def test_numpy():
from sympy.utilities.pytest import skip
np = import_module('numpy')


def equal(x, y):
return x == y and type(x) == type(y)

if not np:
skip('numpy not installed.Abort numpy tests.')

assert sympify(np.bool_(1)) is S(True)
assert equal(
sympify(np.int_(1234567891234567891)), S(1234567891234567891))
assert equal(sympify(np.intc(1234567891)), S(1234567891))
assert equal(
sympify(np.intp(1234567891234567891)), S(1234567891234567891))
assert equal(sympify(np.int8(-123)), S(-123))
assert equal(sympify(np.int16(-12345)), S(-12345))
assert equal(sympify(np.int32(-1234567891)), S(-1234567891))
assert equal(
sympify(np.int64(-1234567891234567891)), S(-1234567891234567891))
assert equal(sympify(np.uint8(123)), S(123))
assert equal(sympify(np.uint16(12345)), S(12345))
assert equal(sympify(np.uint32(1234567891)), S(1234567891))
assert equal(
sympify(np.uint64(1234567891234567891)), S(1234567891234567891))
assert equal(sympify(np.float32(1.123456)), Float(1.123456, precision=24))
assert equal(sympify(np.float64(1.1234567891234)),
Float(1.1234567891234, precision=53))
assert equal(sympify(np.complex64(1 + 2j)), S(1.0 + 2.0*I))
assert equal(sympify(np.complex128(1 + 2j)), S(1.0 + 2.0*I))

try:
assert equal(sympify(np.float96(1.123456789)),
Float(1.123456789, precision=80))
except AttributeError: #float96 does not exist on all platforms
pass

try:
assert equal(sympify(np.float128(1.123456789123)),
Float(1.123456789123, precision=80))
except AttributeError: #float128 does not exist on all platforms
pass


@XFAIL
def test_sympify_rational_numbers_set():
ans = [Rational(3, 10), Rational(1, 5)]
Expand Down

0 comments on commit 7a34d3b

Please sign in to comment.