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

Sympy overloads maximum numpy function #18801

Open
phofl opened this issue Mar 8, 2020 · 4 comments
Open

Sympy overloads maximum numpy function #18801

phofl opened this issue Mar 8, 2020 · 4 comments

Comments

@phofl
Copy link

phofl commented Mar 8, 2020

Hi,

I have encountered the following bug recently.

The implementation described in #16473 overloads the maximum/minimum functions from numpy. I would like to get the componentwise maximum from two numpy arrays.

import sympy
from sympy import sympify, lambdify, Symbol
import numpy as np

sym_formula = sympify("maximum(a,b)")
fct = lambdify([Symbol("a"), Symbol("b")], sym_formula)

na1 = np.array([1.0, -2.0])
na2 = np.array([-1.0, 2.0])
fct(na1, na2)

With sympy version 1.4 this code block returned np.array([1.0, 2.0]). Since upgrading to sympy version 1.5 sympify interprets the maximum(a,b) function as a.

Is there a workaround for this behaviour?

@sylee957
Copy link
Member

sylee957 commented Mar 8, 2020

You can use Max

from sympy import *
import numpy as np

a, b = symbols('a b')
fct = lambdify([a, b], Max(a, b))

na1 = np.array([1.0, -2.0])
na2 = np.array([-1.0, 2.0])
fct(na1, na2)

@phofl
Copy link
Author

phofl commented Mar 8, 2020

Hi, thanks for the reply. Unfortunately the Max function returns the maximum from both arrays as a scalar, not componentwise. The return value in my example would be 2.0 instead of np.array([1.0, 2.0])

@MANIKTANEJA3
Copy link

@phofl @sylee957
"""
I observed the same anomaly in using the "maximum" function for the two arrays .
Since, numpy allows us to vertically stack the arrays using the "vstack" function and then use "amax" function to find the maximum value in every array ; we can use these two features to compute the correct results .
the following is the numpy code for finding the maximum value of each array and returning the maximum values in the form of an array
"""
import numpy as np
na1=np.array([1.0,-2.0])
na2=np.array([-1.0,2.0])
a=np.vstack((na1,na2))
b=np.amax(a,axis=0)
c=np.amax(a,axis=1)
b
c

output:
array([ 1., 2.])
"""
using the above mentioned logic and making changes in the max function directly or importing amax function from the numpy module will help workaround this behaviour since it is being observed in the updated sympy version
If it is possible to implement the same logic I'd like to have some sort of guidance so that i can make the necessary changes to the library .
"""

@phofl
Copy link
Author

phofl commented Mar 17, 2020

Hi,

unfortunately we do not know the dimension of our arrays beforehand. Your described workaround is a bit complicated in this case, but would work nevertheless. Thank you. We solved the issue temporarily through defining a user defined function usr_maximum. This function applies the np.maximum function for our arrays. That works, but it is not the best solution in my opinion.

I think the issue is not the lambdify function. According to my obersvations sympify is the issue here. It does not recognize the maximum(a,b) function correctly, if you would like to apply the numpy function. One possible solution may be to rename the maximum and minimum function implemented in the issue mentioned above. I do not know, if there is another workaround.

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

No branches or pull requests

3 participants