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

Added support for kwarg "use_array" in lambdify (fixes #2931) #2987

Merged
merged 6 commits into from
Mar 14, 2014

Conversation

jcrist
Copy link
Member

@jcrist jcrist commented Mar 2, 2014

Added kwarg in lambdify to support return of numpy array instead of matrix in a more user friendly way. Fixes #2931.

@@ -167,6 +167,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
for numpy.matrix. If you would rather have a numpy.array returned,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for>with

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@moorepants
Copy link
Member

I'm for this option. Let's see if any others have a comment. I think this is a good initial solution.

@@ -145,7 +145,7 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line may need to be wrapped to 79 characters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I was a little unsure how much sympy cared about pep 8 compliance, as some other lines were 80+ characters.

@moorepants
Copy link
Member

I'm +1 on this. Anyone else have comments?

@flacjacket
Copy link
Member

This is really handy, looks good to me.

@jcrist
Copy link
Member Author

jcrist commented Mar 8, 2014

Any way we can get this merged? I'd like to start using this fix.

@asmeurer
Copy link
Member

asmeurer commented Mar 9, 2014

Did you forget to push? All the comments where you say "fixed" are not fixed.

Edited doc string for lambdify. Pep 8 formatting fixes.
@jcrist
Copy link
Member Author

jcrist commented Mar 10, 2014

Sorry about that. I'm new to version control, and because I started this on my master branch, things kind of got screwed up when I worked on other patches later. Looks like I accidentally deleted a commit. Everything should be fixed now though. Just waiting for the Travis tests to re-run.

Note: this was already discussed on gitter, but I'm repeating here for others

f_arr = lambdify((x, y, z), A, use_array=True)
f_mat = lambdify((x, y, z), A)
numpy.testing.assert_allclose(f_mat(1, 2, 3), sol_mat)
numpy.testing.assert_allclose(f_arr(1, 2, 3), sol_arr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these tests will work if f_mat and f_arr return matrices or arrays as it just checks whether the entries of the array are the same. It'd probably be a good idea to check whether the type of what f_arr and f_mat return are actually matrices and arrays. Maybe:

isinstance(f_arr, type(sol_arr))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Should be good now. I left in the content checks as a just-in-case.

Now ensures use_array=True returns numpy.ndarray, and
use_array=False returns numpy.matrix.
@jcrist
Copy link
Member Author

jcrist commented Mar 12, 2014

Hmmm, for some reason the tests passed on my machine, even though looking at the code now they shouldn't have. And I checked, I was running the tests on the same git-branch, and had saved beforehand. Any idea why this could happen?

Anyway, should be good now. Hopefully.

@moorepants
Copy link
Member

Ok, this one's good to go.

moorepants added a commit that referenced this pull request Mar 14, 2014
Added support for kwarg "use_array" in lambdify (fixes  #2931)
@moorepants moorepants merged commit 819b5a6 into sympy:master Mar 14, 2014
@moorepants
Copy link
Member

@jcrist Can you add this to the release notes:

https://github.com/sympy/sympy/wiki/Release-Notes-for-0.7.6

@asmeurer
Copy link
Member

You know, thinking about this, and seeing the discussion that has been going on at numpy/numpy#4351, I think I now understand why np.matrix is so bad. We should probably do what is considered to be the best practice in the numpy community, and always use array, and just print matrix multiplication as np.dot in the lambdarepr printer.

@moorepants
Copy link
Member

I like that idea.

@asmeurer
Copy link
Member

#7284. We should do this before the next release, since it implies reverting this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to lambdify to use array instead of matrix
4 participants