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.

x2=∣x∣(x∈R)\sqrt{x^2}=|x|\quad(x\in\mathbb R)
Worked example

For x=−3x=-3, x2=9=3\sqrt{x^2}=\sqrt9=3, while xx itself is −3-3. For a positive xx, ∣x∣=x|x|=x.

Maths prerequisite map ↗

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.

See the mathematics
-4-2024-4.5-2.2502.254.5xy
-4-2024-4.5-2.2502.254.5xy
x2=∣x∣(x∈R)\sqrt{x^2}=|x|\quad(x\in\mathbb R)
  • ∣x∣\left|{x}\right|
  • xx
Starter reference. The distance from zero is nonnegative. For negative real inputs, the two curves differ.

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.

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.

Compare with the worked example

Executed from the published Python during the site build. Your own run appears above. Build environment ↗

Unrestricted
z2\sqrt{z^{2}}
Real
∣x∣\left|{x}\right|
Assumed p
pp
Real at -3
33
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.

Return to the editor

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?

Choose an answer
Need a hint?
Hint 1

Test x=−1x=-1 and x=1x=1.

Hint 2

Choose a form that is nonnegative in both cases.

Your notesNotes save in this browser when storage is available.