Linear Equations in Two Variables, Version B

Kenneth Levasseur
Mathematical Sciences
UMass Lowell
Lowell, MA 01854
Kenneth_Levasseur@uml.edu

This is the second of a series of notebooks that  develop a function in various ways

You're not always given a slope and a point when you want the equation of a line.  In this notebook we will use Mathematica's pattern matching capabilities to define EquationOfLine for a variety of different  forms of input.   

First the simplest case.  Given the slope and y intercept the equation is easy to write down.

EquationOfLine[m_,b_]:=(y==m x +b);

With many programming languages, the definition above pretty much covers all possible calls to EquationOfLine with two parameters.  If we gave the second argument a pair, that pair would be used in place of b above.  But in the definition below, we specify that if the second argument is a pair, then we treat the situation differently and use the definition that we used in Notebook A.

EquationOfLine[m_,{s_,t_}]:=Simplify[(y-t)==m(x-s)];

Now we can build on the previous definition to handle two points.  Given {s,t} and {u,v} the strategy is to compute the slope and then use the slope-point form again.  So EquationOfLine with two points uses EquationOfLine with a slope and a point.

EquationOfLine[{s_,t_},{u_,v_}]:=EquationOfLine[((v-t)/(u-s)),{s,t}]

Oops!  One case needs to be take care of.  What if the x coordinates of the two points are identical.  Then the definition above won't work.  Do you see why?  The following definition is considered simpler by Mathematica and will be considered first in assessing which definition to use.  This will be clear when we ask Mathematica for all it knows about EquationOfLine.

EquationOfLine[{s_,t_},{s_,v_}]:=(x==s);

?EquationOfLine

You can see above that for different patterns of input, the function EquationOfLine will do different things; but the result is always an equation.

Usage Statement

Here is a "usage statement" for EquationOfLine.   This would be important if this function is intended for other users.

EquationOfLine::usage="EquationOfLine[a,b] returns the equation of the line relating variables x and y where a and b are either \n
		a slope and intercept\n
		a slope and point  or \n
		two points point and point.";

Generally if you want to know how to use a function, the first thing you should look at is the usage statement, which you can get this way:

?EquationOfLine

Another nice thing about this usage statement:  If you click immediately to the right of EquationOfLine in the cell below, and then press Ctrl-⇧-K  (Mac users: ⌘-⇧-K) you will get a "template" of a typical input.

EquationOfLine

Tests

Testing your function is important.  You should try to "break it" by trying to think of inputs that other users might try.

EquationOfLine[5,-1]

EquationOfLine[2,{1,-3}]

EquationOfLine[{1,5},{1,-3}]

EquationOfLine[0,{1,5}]

EquationOfLine[{1,5},{4,-3}]

Does it work for symbolic input?  It appears to work fine.  Here are a few examples

EquationOfLine[m,{7,0}]

EquationOfLine[-1,{c,-2c}]

EquationOfLine[{(Subscript[a, 1]),(Subscript[a, 2])},{(Subscript[b, 1]),(Subscript[b, 2])}]

Limitations?

Are there still some other inputs that produce incorrect results or errors?  I know of one, but it is a subtle error.  If you know of a limitation that you haven't fixed, you should document it; i. e. write down what the problem is so that you can plan to fix it later and others can anticipate the problem.