Think symbolicallyLesson 04 of 48 · 7 min
Tell SymPy what a symbol means
Use domain assumptions to justify a square-root simplification.
Maths refresher: Square roots and absolute value
The principal square root of a nonnegative real number is nonnegative. Absolute value is a number’s distance from zero.
For , , while itself is . For a positive , .
An identity must hold for all values permitted by its assumptions. SymPy therefore cannot generally replace the square root of x squared by x. Negative real values already provide a counterexample.
With real=True, the expression becomes Abs(x), the nonnegative distance of x from zero. With positive=True, the symbol is strictly positive, so that absolute value reduces to the symbol itself. An unconstrained symbol can represent complex values and needs additional care.
Assumptions belong in the model before simplification. Do not add positivity merely to obtain a shorter answer. Compare the three outputs and substitute a negative real value into the real-symbol expression to see why the distinction matters.
Should sqrt(x**2) simplify the same way for unrestricted, real and positive symbols?
Write a prediction (optional)
Exact symbolic investigation
Select Run Python with the code unchanged.
Your saved draft is open. To use the published code, select Restore example in Tools. Use Recover draft to bring back your code.
Tools
About Python
Python and the course packages can load while you read. Each run starts with fresh user variables; imported modules stay loaded. Stop replaces the worker. Runtime details ↗
Ctrl+Enter or ⌘+Enter runs Python.
Execution error
Technical error details
Your result
Python text output
Compare with the worked example
Executed from the published Python during the site build. Your own run appears above. Build environment ↗
Worked text output
Unrestricted: sqrt(z**2) Real: Abs(x) Assumed p: p Real at -3: 3
Read the starter result
Compare Unrestricted, Real and Assumed p. The three results describe different permitted inputs. Real at -3 checks the negative real case.
Edit and compare
In the line that creates p, replace positive=True with nonnegative=True. Select Run Python and read Assumed p. Next replace nonnegative=True with negative=True and select Run Python again. Explain why the final result needs a minus sign.
Scope. Assumptions are mathematical restrictions supplied by you; SymPy does not establish their real-world truth.
Try a fresh case
A formula must work for any real displacement, including negative values. Which reduction is justified?
Need a hint?
Hint 1
Test and .
Hint 2
Choose a form that is nonnegative in both cases.