[PATCH Discussion] Fix solveset_real crash with Abs + trig (issue #28213)

25 views
Skip to first unread message

pushkar sharma

unread,
Jul 7, 2025, 9:46:15 AMJul 7
to sympy

Hi everyone,

I'm Pushkar, and this is my first time contributing to SymPy. I'm still learning my way around the codebase and contribution process, so please let me know if anything I’m doing is not quite right.

I came across SymPy issue #28213, where calling solveset_real on an expression involving Abs and trigonometric functions raises a ValueError, even though all variables are declared real.

Here’s a minimal reproducible example that fails:

```

from sympy import Abs, sin, cos, symbols
from sympy.solvers.solveset import solveset_real

t1, t2, t3, u1, u2, u3 = symbols('t1 t2 t3 u1 u2 u3', real=True)
expr = sin(t1)*cos(t3) + sin(t2)*sin(t3)*cos(t1) - (
    sin(t2)*sin(u3)*cos(t1)*cos(t2)/Abs(cos(t2)) + cos(u3)*Abs(sin(t1))
)
solveset_real(expr, u3)

```

This raises:

```

ValueError: Absolute values cannot be inverted in the complex domain.

```

I looked into it and noticed that _solve_trig1 in solveset_real is falling back to solveset_complex in cases involving Abs, even though the domain is real. To fix this, I modified _solve_abs so that instead of raising a ValueError when inequalities fail, it now returns a ConditionSet fallback. 


My Fix:

I modified _solve_abs to handle NotImplementedError during inequality solving by wrapping the fallback in a ConditionSet, like this:

 
```

def _solve_abs(f, symbol, domain):
    ...
    if not (f_p.is_zero or f_q.is_zero):
        ...
        try:
            q_pos_cond = solve_univariate_inequality(...)
            ...
            return Union(sols_q_pos, sols_q_neg)
        except NotImplementedError:
            return ConditionSet(symbol, Eq(f, 0), domain)
    else:
        return ConditionSet(symbol, Eq(f, 0), domain)
```


Test Results:

After applying the patch, I ran:

```
python -m pytest -s sympy/solvers/tests/test_solveset.py
```

and got the following:

  • 180 tests passed

  •  2 failed (but they are unrelated to this patchtest_solve_trig and test_issue_19050)

  • 2 skipped, 1 deselected, 13 xfailed (as expected)

I checked the failures, and they seem to stem from existing limitations in solving sin(x) + cos(x) exactly, not related to my changes.

I'm happy to open a PR if this approach seems reasonable. But since I’m new, I wanted to get feedback here first. Would love any suggestions or corrections.

Thanks,
Pushkar

 







Oscar Benjamin

unread,
Jul 7, 2025, 1:32:11 PMJul 7
to sy...@googlegroups.com
Hi Pushkar,

I can't say whether the details of your proposed fix are reasonable as
I have not looked through the relevant code in solveset.

What I can say is that if things were properly designed and
implemented then I don't think that there would be any circumstances
in which solveset_real ends up calling solveset_complex. I suspect
that the real fix here is not catching any exception but rather
working through carefully to see how this happens and ensuring that
real solvers are kept separate from the complex ones.

Oscar
> --
> 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+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/sympy/1cea363f-e0fe-4196-89e3-9c9bd954616dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages