Sub topic 12
Checking Rearrangements with a Computer Algebra System
This chapter assumes you can rearrange formulas by hand using inverse operations, collecting and factoring, clearing fractions, and taking roots. You also need basic Python: defining functions, importing modules, and printing values. Earlier chapters covered the hand methods and the restrictions they carry. Here you use software to catch mistakes in your own work.
What a computer algebra system does
A computer algebra system (CAS) manipulates symbols instead of numbers. If you give it x + x, it returns 2*x, not a decimal. If you give it an equation and a variable, it can return a formula for that variable.
This chapter uses SymPy, a free CAS library for Python. Install it with pip install sympy.
A CAS is a checker, not a replacement for your own derivation. It does not know what your formula means, so it cannot tell you which root is physically sensible. It also solves under generic assumptions unless you state others. You still have to supply the judgment about restrictions.
Solving a formula with `solve`
The function solve(equation, target) returns a list of solutions for the target variable. The list can be empty, have one item, or have several.
from sympy import symbols, Eq, solve
a, b, c, d, x = symbols('a b c d x')
# a*x + b = c*x + d -> (a - c)*x = d - b -> x = (d - b)/(a - c)
print(solve(Eq(a*x + b, c*x + d), x))The result is one expression equivalent to (d - b)/(a - c). SymPy may print it as (-b + d)/(a - c), which is the same thing in a different order.
Two details matter here:
Eq(left, right)builds an equation. The expressionleft == rightwould test whether the two sides are structurally identical and return a PythonTrueorFalse. It would not build an equation.- The answer is a list, even when there is one solution. Use
solve(...)[0]to get the expression itself.
Comparing your answer with the CAS
Suppose you derived a formula by hand. You can compare it with SymPy's answer by subtracting one from the other and simplifying. If the difference simplifies to 0, the two formulas are equivalent.
from sympy import symbols, Eq, solve, simplify, Rational
C, F = symbols('C F')
by_hand = 5 * (F - 32) / 9
by_cas = solve(Eq(F, Rational(9, 5) * C + 32), C)[0]
print(simplify(by_cas - by_hand)) # 0Rational(9, 5) keeps the fraction exact. Writing 9/5 in Python would produce the float 1.8, which can leave tiny rounding residue in the symbolic result.
The same method works for a harder case, such as the parallel resistor formula where the target is in a denominator:
from sympy import symbols, Eq, solve, simplify
R, R1, R2 = symbols('R R1 R2', positive=True)
equation = Eq(1 / R, 1 / R1 + 1 / R2)
by_cas = solve(equation, R2)
by_hand = R * R1 / (R1 - R)
print(len(by_cas)) # 1
print(simplify(by_cas[0] - by_hand)) # 0Here the check passes, so the hand derivation R2 = R*R1/(R1 - R) matches the CAS.
Catching a wrong rearrangement
The check is useful because it fails when you have made an error. A common slip with this formula is to flip a sign and write R*R1/(R - R1).
from sympy import symbols, Eq, solve, simplify
R, R1, R2 = symbols('R R1 R2', positive=True)
correct = solve(Eq(1 / R, 1 / R1 + 1 / R2), R2)[0]
wrong = R * R1 / (R - R1)
print(simplify(correct - wrong) == 0) # False
print(correct.subs({R: 2, R1: 6})) # 3
print(wrong.subs({R: 2, R1: 6})) # -3The wrong formula gives a negative resistance for inputs where the right answer is 3. The numeric substitution makes the error concrete and shows what kind of mistake it was: here, a sign error.
Checking by substitution
A second approach does not rely on you having a hand answer. Substitute the CAS solution back into the original equation. If the left side minus the right side simplifies to zero, the solution satisfies the equation.
from sympy import symbols, Eq, solve, simplify
a, b, c, d, x = symbols('a b c d x')
equation = Eq(a * x + b, c * x + d)
solution = solve(equation, x)[0]
residual = equation.lhs.subs(x, solution) - equation.rhs.subs(x, solution)
print(simplify(residual)) # 0The difference between the two sides is the residual. A residual of zero means the solution satisfies the equation. This check also works on a formula you rearranged by hand: substitute your formula for the target and see whether the residual vanishes.
The workflow looks like this:
Assumptions control which roots you get
A CAS treats a symbol as any complex number unless told otherwise. For a power, that matters. Take the circle area formula A = pi * r**2:
from sympy import symbols, Eq, solve, pi
A = symbols('A', positive=True)
r_any = symbols('r')
r_pos = symbols('r_pos', positive=True)
print(solve(Eq(A, pi * r_any**2), r_any)) # two roots: plus and minus
print(solve(Eq(A, pi * r_pos**2), r_pos)) # one root: the positive oneWith no assumption on r, SymPy returns both sqrt(A)/sqrt(pi) and its negative. Declaring r positive discards the negative root, which has no meaning for a radius.
This is the CAS version of the plus-or-minus step from the chapter on powers and roots. The software will not decide for you. You tell it what the variable represents by declaring the assumption, and the output follows.
| Declaration | Effect on `solve` |
|---|---|
| `symbols('r')` | No assumption. Both roots of an even power appear. |
| `symbols('r', positive=True)` | Negative roots are discarded. Square roots of squares simplify cleanly. |
| `symbols('r', real=True)` | Complex roots are discarded. Both real roots remain. |
| `symbols('n', integer=True)` | Useful for counts, such as the number of compounding periods. |
Restrictions the CAS can hide
The rearranged formula x = (d - b)/(a - c) fails when a = c. solve with generic symbols returns the formula without flagging that. The zero-denominator case is a different problem, not a special case of the formula.
You can see the distinction by substituting numbers before solving:
from sympy import symbols, Eq, solve
x = symbols('x')
print(solve(Eq(2 * x + 1, 5 * x + 4), x)) # [-1]
print(solve(Eq(2 * x + 1, 2 * x + 5), x)) # []In the first equation a = 2 and c = 5, so the formula applies and gives (4 - 1)/(2 - 5) = -1. In the second a = c = 2, so the equation reduces to 1 = 5, which is false for every x. The empty list means no solution exists.
The lesson is to read the generic result as "valid when the denominators in it are nonzero". Look at every denominator in the CAS output and write down the condition that it is not zero. The same applies to the resistor formula, where R1 = R makes the output undefined.
Spot-checking with numbers
Symbolic simplification sometimes fails to reduce a correct difference to 0. The two expressions may be equal but in forms that simplify cannot unify. A numeric spot check is a good fallback: substitute several unrelated values and compare.
from sympy import symbols, Eq, solve
R, R1, R2 = symbols('R R1 R2', positive=True)
by_cas = solve(Eq(1 / R, 1 / R1 + 1 / R2), R2)[0]
by_hand = R * R1 / (R1 - R)
for values in [{R: 2, R1: 6}, {R: 1, R1: 5}, {R: 3, R1: 4}]:
print(by_cas.subs(values) == by_hand.subs(values)) # True each timeUse values that satisfy the restrictions. Avoid special numbers like 0 or 1 for every variable, because accidents like a = c can make a wrong formula look right. Several unrelated values make such an accident unlikely, but they do not prove equality. A symbolic zero difference does.
Common errors
- Forgetting that
solvereturns a list, then trying to do arithmetic on the list. - Using
==whereEqis needed, which builds no equation at all. - Writing
9/5or0.5and getting floats in a symbolic result. UseRational(9, 5). - Trusting the output when the target symbol has no assumptions, then using a negative root as if it were physical.
- Concluding that a formula is wrong because
simplifydid not print0. Test numerically before redoing the algebra. - Treating a generic result as valid for every input, instead of listing the denominators that must be nonzero.
Exercises
Exercise
The formula for the final amount with simple interest is A = P(1 + rt). You rearranged it by hand to r = (A - P)/(Pt). Write SymPy code that confirms your result is equivalent to what solve returns. State one restriction the rearranged formula has.
Show answer
from sympy import symbols, Eq, solve, simplify A, P, r, t = symbols('A P r t') bycas = solve(Eq(A, P (1 + r t)), r)[0] byhand = (A - P) / (P t) print(simplify(bycas - byhand)) # 0 The restriction is that P and t must be nonzero, because the formula divides by Pt. If P = 0 then A must be 0 and r cannot be determined. If t = 0 then A = P and r has no effect on the amount.
Exercise
A student rearranges V = (1/3)pir*2h for r and writes r = sqrt(3V/(pih)). The positive root is chosen because r is a radius. Write SymPy code that solves for r with a suitable assumption and prints a result that you can compare with the student's formula. What does the list contain if r is declared with no assumption?
Show answer
from sympy import symbols, Eq, solve, pi, simplify, sqrt V, h = symbols('V h', positive=True) r = symbols('r', positive=True) result = solve(Eq(V, pi r2 h / 3), r) print(len(result)) # 1 print(simplify(result[0] - sqrt(3 V / (pi h)))) # 0 With no assumption on r, the list contains two roots, one positive and one negative, because r appears squared. The student's choice of the positive root matches declaring r positive. The formula requires h nonzero, since h is in the denominator.