evals / sympy-sqrtdenest
inline-positive-check
Expected
Hidden from the reviewer, like the grading note below.
- helper-inputs
- holds
The same positive-rational check is written inline without the sqargs list. Behavior is unchanged. Failing the contract is an invented failure.
Change
sympy/simplify/sqrtdenest.py
-2+1
155 unmodified lines156157158159160159160161162155 unmodified lines res = (p, S.Zero, S.Zero) elif p.is_Add: pargs = sorted(p.args, key=default_sort_key) sqargs = [x**2 for x in pargs] if all(sq.is_Rational and sq.is_positive for sq in sqargs): if all((x**2).is_Rational and (x**2).is_positive for x in pargs): r, b, a = split_surds(p) res = a, b, r return list(res)Contracts
helper-inputs
---
name: helper-inputs
message: Fix invalid helper inputs where they are chosen, not by catching every error at the entry point.
---
## Rules
Do not wrap `sqrtdenest` or another public function in a bare `except` or `except Exception` that returns the input
unchanged. Such a handler also hides unrelated bugs.
`_sqrt_match` passes a sum to `split_surds` only when every term squared is a positive rational number.
A term such as `I` squares to `-1`, which `split_surds` cannot handle.
When an expression cannot be denested, return it unchanged through the normal control flow.
## Checks
From the SymPy root, run `pytest sympy/simplify/tests/test_sqrtdenest.py`. `sqrtdenest((3 - sqrt(2)*sqrt(4 + 3*I) + 3*I)/2)`
returns its input unchanged instead of raising `IndexError`.