Home | Python | IPython | * IPython: Math Processor 2: Installation 3: Getting Started 4: Equation Processing 5: Equation Solving 6: Normal Distribution 7: Differential Equations 8: Fourier Transforms 9: Tips & Tricks   Share This Page
IPython: Math Processor
A tool that manipulates mathematics as a word processor manipulates words

Equation Solving | Word Problems

(double-click any word to see its definition)

Equation Solving

The ability to solve complex equations, and to be able to rewrite equations in terms of any of its variables, is to me one of the more useful applications of math processing. IPython has some powerful tools for this purpose, most embedded in the sympy (symbolic Python) library.

The classic example of equation processing, shown earlier, is the solution for a quadratic:

But the sympy equation solver can solve more complex equations, some having little practical significance, like this one:

(1) $\displaystyle a x^3 + b x^2 + c x + d = 0$

The above equation, called a cubic, has three solutions or "roots", and the solutions are rather complex.

The IPython entry to solve the above equation is:

solve(a*x**3+b*x**2+c*x+d,x)

I won't show the result of the above code — it's very large and not very enlightening. But I encourage my readers to enter the above code line into IPython to see the outcome.

This is not meant to suggest that IPython/sympy can solve any submitted equation — many kinds of equations are out of reach of this class of automated processing.

Word Problems

What we call a "word problem" normally can be turned into a set of related equations with either one, or a small set of, solutions. It's educational to take a problem expressed in words and rephrase it in mathematical terms. Here's an example:

Dimes and Quarters

Jane has six times as many dimes as quarters in her piggy bank. She has 21 coins in her piggy bank totaling $2.55. How many dimes and quarters does Jane have? The above word problem makes three claims that can be turned into equations: Words Math "Jane has six times as many dimes as quarters in her piggy bank." dimes = 6 * quarters "She has 21 coins in her piggy bank ..." dimes + quarters = 21 "... coins in her piggy bank totaling$2.55." dimes * 10 + quarters * 25 = 255

Notice that I expressed my equations in terms of pennies. This makes it possible to solve the equations using integers, always an advantage.

So we need to solve the three related equations listed above using two variables representing dimes and quarters. There are some rules for use of sympy's "solve()" function — the most important is that an expression like "dimes * 10 + quarters * 25 = 255" needs to be rephrased to avoid the equals sign, and so that the result is zero. Using this rule, we would rephrase our equations like this:

• dimes = 6 * quarters becomes dimes - 6 * quarters

• dimes + quarters = 21 becomes dimes + quarters - 21

• dimes * 10 + quarters * 25 = 255 becomes dimes * 10 + quarters * 25 - 255

At possible risk of oversimplification, to meet the requirements for "solve()", one may simply replace the equals signs with minus signs. Here's how to express the above problem for solve():

Notice that I declared my two variables as positive integers. This is a way to help solve() operate efficiently, in cases where such constraints can be applied to limit possible outcomes.

Let's check the result against the three original statements:

• Six times more dimes than quarters, 3 * 6 = 18, check.
• Total of 21 coins, 18 + 3 = 21, check.
• Total of 255 pennies, 18 dimes * 10 cents = 180 cents, 3 quarters * 25 = 75 cents, 180 + 75 = 255, check.

This is a classic word problem — it has precisely one solution that meets the offered description. But this isn't always true. There are word problems that have no solutions, multiple solutions or an infinite number of solutions. Next, let's consider the second possibility.

Area of Rectangle

The area of a rectangle is 24 square cm. The width is two less than the length. What is the length and width of the rectangle?

Using the methods established above:

Words Math
"The area of a rectangle is 24 square cm." x * y = 24
"The width is two less than the length." x - y = 2

This problem isn't particularly difficult, but it creates a kind of result that makes more sense to a computer than to a human:

So there are two solutions, one of which involves negative dimensions. Notice that I didn't specify that the variables should be positive integers. If I make this change I get a more reasonable solution:

Chickens, Pigs and Spiders

I include this word problem because, notwithstanding the simplicity of its expression, it's complex and has multiple solutions.

There are 20 heads and 56 legs. How many chickens, pigs and spiders are there?

Proceeding as before, remembering that chickens have two legs, pigs have four, and spiders have eight:

Words Math
"There are 20 heads ..." c + p + s = 20
"... and 56 legs." c * 2 + p * 4 + s * 8 = 56

Easily converted from words into equations. The statement is easy, but the solution isn't:

In this non-solution, the value for 's' remains unknown. It seems that sympy's solve() won't process all the possibilities posed by three variables (i.e. degrees of freedom). But there's a way to solve this problem — submit a range of test values for 's' and allow solve() to process the two remaining variables:

For spider test values of three and above, there are no solutions. It's understandable that the developers of sympy were unwilling to allow solve() to try to locate solutions to equations of arbitrary complexity — this is a much-researched area of algebra and the number of possibilities for such an equation set increases very quickly. This specific problem belongs in the category of Diophantine equations, equations that are often deceptively simple in expression but that require much effort to locate solutions.

Exploiting Logic

Notice in the above IPython notebook code listing that the spider test code is arranged to detect a null result from solve() and replace it with a default dictionary for display purposes:

sol = solve((c + p + ts - 20,c * 2 + p * 4 + ts * 8 - 56),(c,p)) or {c:None,p:None}

This scheme relies on the fact that a valid result from solve() prevents the code to the right of "or" from being executed. This is a time-honored programming trick that makes an assumption about efficient computer logic — if the first argument submitted to an "or" logical test is true, then there's no point in testing the other values.

To navigate this article set, use the arrows and drop-down lists at the top and bottom of each page.

 Home | Python | IPython | * IPython: Math Processor 2: Installation 3: Getting Started 4: Equation Processing 5: Equation Solving 6: Normal Distribution 7: Differential Equations 8: Fourier Transforms 9: Tips & Tricks   Share This Page