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

Solve with Piecewise gives nan expressions #20385

Open
oscarbenjamin opened this issue Nov 5, 2020 · 1 comment
Open

Solve with Piecewise gives nan expressions #20385

oscarbenjamin opened this issue Nov 5, 2020 · 1 comment

Comments

@oscarbenjamin
Copy link
Contributor

Using solve to solve a Piecewise equation where the solutions mat or may not exist depending on a parameter of the equation leads to nan results:

In [9]: eq2 = Piecewise((x, Ne(y, 0)), (x+1, True))                                                                                                           

In [10]: eq2                                                                                                                                                  
Out[10]: 
⎧  x    for y  0
⎨                
⎩x + 1  otherwise

In [11]: solve(eq2, x)                                                                                                                                        
Out[11]: 
⎡⎧-1   for y = 00   for y  0⎤
⎢⎨              , ⎨              ⎥
⎣⎩nan  otherwise  ⎩nan  otherwise⎦

In principle those could be combined to give something like

In [15]: Piecewise((-1, Eq(y, 0)), (0, True))                                                                                                                 
Out[15]:-1  for y = 0
⎨             
⎩0   otherwise

However in general that can't work because the number of solutions might be different for different cases e.g.:

In [12]: eq2 = Piecewise((x, Ne(y, 0)), (x**2-1, True))                                                                                                       

In [13]: eq2                                                                                                                                                  
Out[13]: 
⎧  x     for y  0
⎪                 
⎨ 2               
⎪x  - 1  otherwise
⎩                 

In [14]: solve(eq2, x)                                                                                                                                        
Out[14]: 
⎡⎧-1   for y = 00   for y  01   for y = 0⎤
⎢⎨              , ⎨              , ⎨              ⎥
⎣⎩nan  otherwise  ⎩nan  otherwise  ⎩nan  otherwise⎦

When there are no Piecewise then solve returns solutions for generic parameter values e.g.:

In [16]: solve(y*x - 1, x)                                                                                                                                    
Out[16]:1⎤
⎢─⎥
⎣y⎦

In this case the solution exists only if y is non-zero. Following the same reasoning solve should discard all Eq(...) cases in a Piecewise to include only cases that hold for generic values of the parameters.

It would be good to represent solution sets that are conditional on the parameter values but it is not clear how that could be done within the solve API. I suppose that the above nan results are an attempt to guard against incorrect solutions but it doesn't seem like a very useful approach. We should in any case document what the expected behaviour should be.

The same issue affects solveset and nonlinsolve:

In [23]: eq2 = Piecewise((x, Ne(y, 0)), (x**2-1, True))                                                                                                       

In [24]: eq2                                                                                                                                                  
Out[24]: 
⎧  x     for y  0
⎪                 
⎨ 2               
⎪x  - 1  otherwise
⎩                 

In [25]: solveset(eq2, x)                                                                                                                                     
Out[25]: ∅

In [26]: eq3 = Piecewise((x, Eq(y, 0)), (x**2-1, True))                                                                                                       

In [27]: eq3                                                                                                                                                  
Out[27]: 
⎧  x     for y = 0
⎪                 
⎨ 2               
⎪x  - 1  otherwise
⎩                 

In [28]: solveset(eq3, x)                                                                                                                                     
Out[28]: {-1, 0, 1}
@oscarbenjamin
Copy link
Contributor Author

See also #16861

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

1 participant