Skip to content

Commit

Permalink
Merge pull request #2987 from jcrist/master
Browse files Browse the repository at this point in the history
Added support for kwarg "use_array" in lambdify (fixes  #2931)
  • Loading branch information
moorepants committed Mar 14, 2014
2 parents 81e0b89 + 4789c90 commit 819b5a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sympy/utilities/lambdify.py
Expand Up @@ -145,7 +145,8 @@ def _import(module, reload="False"):


@doctest_depends_on(modules=('numpy'))
def lambdify(args, expr, modules=None, printer=None, use_imps=True, dummify=True):
def lambdify(args, expr, modules=None, printer=None, use_imps=True,
dummify=True, use_array=False):
"""
Returns a lambda function for fast calculation of numerical values.
Expand All @@ -167,6 +168,10 @@ def lambdify(args, expr, modules=None, printer=None, use_imps=True, dummify=True
to view the lambdified function or provide "sympy" as the module, you
should probably set dummify=False.
If numpy is installed, the default behavior is to substitute Sympy Matrices
with numpy.matrix. If you would rather have a numpy.array returned,
set use_array=True.
Usage
=====
Expand Down Expand Up @@ -230,6 +235,10 @@ def lambdify(args, expr, modules=None, printer=None, use_imps=True, dummify=True
>>> row = lambdify((x, y), Matrix((x, x + y)).T, modules='sympy')
>>> row(1, 2)
Matrix([[1, 3]])
>>> col = lambdify((x, y), Matrix((x, x + y)), use_array=True)
>>> col(1, 2)
array([[1],
[3]])
Tuple arguments are handled and the lambdified function should
be called with the same type of arguments as were used to create
Expand Down Expand Up @@ -274,6 +283,18 @@ def lambdify(args, expr, modules=None, printer=None, use_imps=True, dummify=True
# might be the reason for irreproducible errors.
modules = ["math", "mpmath", "sympy"]

#If numpy.array should be used instead of numpy.matrix
if use_array:
NUMPY_TRANSLATIONS.update({"Matrix": "array",
"MutableDenseMatrix": "array",
"ImmutableMatrix": "array"})
else:
#Ensures that the translation dict is set back
#to matrix if lambdify was already called
NUMPY_TRANSLATIONS.update({"Matrix": "matrix",
"MutableDenseMatrix": "matrix",
"ImmutableMatrix": "matrix"})
#Attempt to import numpy
try:
_import("numpy")
except ImportError:
Expand Down
14 changes: 14 additions & 0 deletions sympy/utilities/tests/test_lambdify.py
Expand Up @@ -259,6 +259,20 @@ def test_matrix():
assert lambdify(v, J, modules='sympy')(1, 2) == sol
assert lambdify(v.T, J, modules='sympy')(1, 2) == sol

def test_numpy_matrix():
if not numpy:
skip("numpy not installed.")
A = Matrix([[x, x*y], [sin(z) + 4, x**z]])
sol_mat = numpy.matrix([[1, 2], [numpy.sin(3) + 4, 1]])
sol_arr = numpy.array([[1, 2], [numpy.sin(3) + 4, 1]])
#Lambdify array first, to ensure return to matrix as default
f_arr = lambdify((x, y, z), A, use_array=True)(1, 2, 3)
f_mat = lambdify((x, y, z), A)(1, 2, 3)
numpy.testing.assert_allclose(f_mat, sol_mat)
numpy.testing.assert_allclose(f_arr, sol_arr)
#Check that the types are arrays and matrices
assert isinstance(f_mat, numpy.matrix)
assert isinstance(f_arr, numpy.ndarray)

def test_integral():
f = Lambda(x, exp(-x**2))
Expand Down

0 comments on commit 819b5a6

Please sign in to comment.