Hi Oscar,
Thanks for replying!
On Sat, 9 Aug 2025 at 13:26, Gábor Horváth <hungaborhorv...@gmail.com> wrote:
So my questions are:
1) Is the expected behaviour for sympy.solve for the Piecewise case to throw a
NaN comparison exception, or is this a sign of some underlying issue in
sympy.solve for Piecewise functions?
No, that is just a bug.
Ok, thanks. Should I report it in some official way somewhere (if yes,
where and how), or has this email already taken care of the bugreport?
Maybe I should open an issue on https://github.com/sympy/sympy/issues ?
(As I don't fully understand the code behind the Abs rewrite, I don't feel
confident enough to work on this on my own and submit a PR.)
2) Is sympy.solve expected to rewrite Min and Max to Piecewise at some point in
the future? I found some piece of code in solvers/solvers.py:944-957 which does
some sort of rewriting for Abs, but didn't find any for Min or Max.
It probably does make sense to rewrite Min and Max as Piecewise if
they contain the variables of interest.
Ok, thanks. Same question as above: should I open some issue on
https://github.com/sympy/sympy/issues or elsewhere, or this is already
taken care of by this email?
3) From some reading on the sympy docs page, I figured solveset is expected to
be the future solver. Is there a direct way I can apply solveset on a system,
rather than on one equation?
This documentation needs to be changed. The solveset function is not
going to replace solve. It was originally intended as a replacement
for solve but was then misdesigned as something different that cannot
replace solve because it does not provide comparable functionality or
interface.
Ah, I see. This was the page I found first:
https://lidavidm.github.io/sympy/modules/solvers/solveset.html
and only later did I find the official documentation:
https://docs.sympy.org/latest/modules/solvers/solveset.html
Now the two are pretty much (looks to be more than 90%) identical, but the
first has this section that confused me about solveset potentially
replacing solve:
"What will you do with the old solve?
There are still a few things solveset can’t do, which the old solve
can, such as solving non linear multivariate & LambertW type equations.
Hence, it’s not yet a perfect replacement for old solve. The ultimate goal
is to:
Replace solve with solveset once solveset is at least as powerful
as solve, i.e., solveset does everything that solve can do currently, and
eventually rename solveset to solve."
This section is missing from the official documentation page on
docs.sympy.org, but considering google search shows the second page, as
well, it might make sense to remove or rephrase this section there.
BTW, are there some old threads somewhere maybe where such discussions
can be read? I would be curious what you mean by misdesign, and what were
the discussion and decision points around solve an solveset.
There is also mention in the docs about FiniteSet handling tuples
corresponds to a finite subset of points in the multi-dimensional space.
That suggests that solveset was intended to be able to solve multi-variate
equations or even systems. Is that indeed the case, or only some special
cases (like linsolve) can really handle multi-variate equations/systems?
4) If the answer to 3) is yes, is there also a way which provides a stable
output that I can use in a programmatic way?
Only solve with dict=True gives stable output that is suitable for
programmatic use but it is not really designed to handle systems of
the type that you have.
The solutions to the system x = Max(y, 1), y = Max(2, x) are y = x for
x >= 2. Given that the solution set is constrained by an inequality
I'm not sure what form of output you would want for programmatic use.
It is very difficult to make a solve function that can take arbitrary
inputs and give something useful as output, especially handling
underdetermined cases, inequality constraints and so on. However if
you have a specific class of problems like these with min and max it
is a lot easier to make something that handles a more limited case. In
this example you can split the equations into 4 linear systems and
solve those.
In general I think that what you should really want in a situation
like this is something like Mathematica's Reduce function rather than
Solve. Unfortunately SymPy has several versions of solve but nothing
like reduce.
Hm, all are very good points. I have some very crude code to solve these
systems exactly as you suggest, by splitting at the various Min and Max
occurrences, solving the underlying linear system (with linsolve), and
then check if the obtained solutions indeed solve the original system with
Min and Max.
That said, somehow my code is rather slow, especially when there are many
Max occurrences. I feel that sympy.solve is faster when it doesn't fail,
so somehow sympy handles Piecewise functions pretty fast. I guess I'll
have to do some profiling on where I can still gain on performance.
Your comment also made me realize that in my systems there are some
underlying assumptions, as well, as inequalities. For the above system I
quickly checked and the corresponding assumption would be 1<=Min(x,y) and
Max(x,y)<=2. With that assumption the solution becomes unique. I guess
I'll need to handle these assumptions in my code, as well. (sympy.solve
dies with the same NaN comparison error even with the extended
assumptions)
Thanks!
Gábor
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sympy+unsubscr...@googlegroups.com.
To view this discussion visit
https://groups.google.com/d/msgid/sympy/a9299028-4f3a-496a-7ad4-c561c4c6383e%40SamsungLaptop.WORKGROUP.