Python equation solver (max and min)

How can I resolve an equation of type x * max(x,15) = 10 with python libraries (possibly Sympy)?
max() means the maximum between the given two arguments.
My equations have a more complex form, but I want to solve it in a simplified form.

+5
source share
3 answers

It seems like SymPy can solve this equation if you convert Max to Piecewise .

 In [4]: solve(x*Piecewise((x, x >=15), (15, x < 15)) - 10, x) Out[4]: [2/3] 
+4
source

When I connect your equation to sympy.solve , it gives NotImplementedError, that is, the algorithms for solving it are not implemented (I opened https://github.com/sympy/sympy/issues/10158 for this).

I think that to solve such equations you will need to replace each Max or Min with your arguments and solve each iteration, and then remove the solutions where Max or Min were not really maximal or minimal in your argument.

I will leave the full algorithm to you or to the other responder (or, hopefully, someone implements it in SymPy). Some helpful tips:

  • expr.atoms(Max, Min) will extract all instances of Max and Min from expr .

  • expr.subs(old, new) will return a new expression with replacing old with new in expr .

+1
source

There is no answer to your equation. You assign x=3 , so there is no variable to solve.

 x 3 Max(x, 15) 15 solve(x*Max(x, 15)-10, x) #No variable here [] 

Perhaps you wanted to do this: y*Max(x, 15) = 10

Then it becomes a valid question.

 In [1]: solve(y*Max(x, 15)-10, y) Out[1]: [2/3] 
-1
source

Source: https://habr.com/ru/post/1236324/


All Articles