Steve T.
Steve T.

Reputation: 99

How to solve "Absolute values cannot be inverted in the complex domain" error in sympy

I have the following equation:

Eq(5*Abs(4*x+2)+6,56). 

What I am trying to do is solve for x = -3 for the math question 5 |4x+2|+6=56, but I keep getting the

"Absolute values cannot be inverted in the complex domain"

error in sympy.

Is there a way around this?

Upvotes: 3

Views: 700

Answers (1)

Stelios
Stelios

Reputation: 5521

You must specify that x is a real-valued variable. You can do that when you define the variable as follows.

import sympy as sp
x = sp.symbols('x', real = True)
eq = sp.Eq(5*sp.Abs(4*x+2)+6,56)
sol = sp.solve(eq, x)
print(sol)

[-3, 2]

EDIT: The sympy.solveset function can be used instead of sympy.solve. In that case, you need to explicitly state that you are solving over the domain of reals. By doing so, you do not have to define your variable as real.

import sympy as sp
x = sp.symbols('x') # implies that x is complex
eq = sp.Eq(5*sp.Abs(4*x+2)+6,56)
sol = sp.solveset(eq, x, domain=sp.S.Reals)
print(sol)

{-3, 2}

Upvotes: 4

Related Questions