'How to solve this equation involving an absolute term in sympy?
from sympy import *
x = symbols('x', real=True)
solve(Abs(2 + 36/(x - 2)) - 6)
I've already set real=True, but it still says solving Abs(2 + 36/(x - 2)) when the argument is not real or imaginary.
However, solving Abs(36/(x - 2)) - 6 is fine.
What's the problem?
Solution 1:[1]
Sometimes, dealing with Abs leads to "suprise" moments, like you experienced. For your particular case, a possible way to make it works is to manipulate the expression. In particular, make abs(x / y) = abs(x) / abs(y):
from sympy import *
x = symbols('x', real=True)
expr = Abs(2 + 36/(x - 2)) - 6
expr = expr.simplify()
def repl_Abs(t):
num, den = fraction(t)
return Abs(num) / Abs(den)
solve(expr.replace(Abs, repl_Abs))
# out: [-5/2, 11]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Davide_sd |
