Learning Mathematica Basics—Algebraic Operations I

Solve each of the following problems using Mathematica exclusively.

1.  Mathematica deals with like terms in the standard way (by combining them), but the results it gives are not in what we usually think of as standard form.  To see this ask Mathematica to add together the following two polynomials:

       5x^2 - 12x + 15          and           4x^3 + 8x^2 - x - 2
       
(No special command here, just put a plus sign between them.)

2.   As you can see (I assume) Mathematica does not list the terms in descending order of degree as you were probably taught in algebra, but rather in ascending order, as you would list them if you were listing the first several terms of an infinite series.  Most people find this a little quirky (me included) but get used to it—Wolfram don't plan on changing this as far as I know.

Anyway, let's move on to something more interesting!  What about some of those tedious tasks you were required to do in algebra, like finding the products of polynomials?  Enter the following command into Mathematica, and hit ENTER:

(y^3+2y^2-7y+11)(3y^4-12y^3+6y^2-6y+1)

3.   Did Mathematica do anything?  I guess "Not much!" would be the answer to that question.  Apart from rearranging the polynomials in "canonical form" everything looks pretty much the same.  Again, get used to this!  Mathematica will not multiply out a product in factored form unless explicitly told to do so!  It assumes that factored form may very well be exactly what you wanted to keep the expression in, and that you wouldn't appreciate the change.  (If you think about this a little you'll realize that this is true in a multitude of situations.)  So how do we force the issue?  Wrap an Expand function around your previous output and try again.

4.   That's more like it!  Mathematica can expand polynomial products extraordinarily quickly—if you don't agree, try racing against it on paper on the previous example.

Now what about the opposite of what we've just been doing—factoring?  The command for this is Factor.  Ask for the following polynomial to be factored:

              6x^8 -19x^7 -34x^6 + 145x^5 - 391x^4 + 1174x^3 - 691x^2 - 130x - 1680
              
(Be very careful entering it since it's so long!)

5.   Almost instantaneously you should have gotten a product of 6 factors, the last two of which are non-factorable quadratic factors.

O.K.  So we can add (and subtract), multiply, and factor polynomials.  We haven't looked at division yet.  Ask Mathematica to take the quotient of the following two polynomials:

             x^2 + 5x + 6         divided by           x^2 - 4

6.   Mathematica should have done nothing, (except the canonical form business again.)  Tell Mathematica to Simplify the last result.

7.   That's more like it!  Mathematica apparently factored and cancelled to produce this result.  Now ask Mathematica to Simplify the quotient of:

             x^3 + 5x + 6         divided by           x^2 - 4
             
(Notice that the first one is now cubed, not squared anymore.)

8.   Nothing this time!  No change!  Why not?  Well what would you have done if you'd been given this to do on paper?  You can't factor the top, right?  And even if you could, what are the real chances of it actually cancelling a factor on the bottom?  The kind of reducing of fractions that you probably got used to in your algebra class is actually rarely useful in reality—seldom do randomaly chosen numerators and denominators have matching factors that can then be cacelled.

Do you remember what we do when cancelling fails?  Long division, right?  Ask for the syntax of the PolynomialDivision command.

9.   Now try using this new command to redo problem 7.

10.   Read over the syntax again from problem 8 to make sure you understand what this answer means—x is the result of the division (the quotient), and 9x+6 is the remainder.

Anyway, that covers the standard four operations in algebra, plus factoring.  Let's move on.

Another skill that you developed in algebra was manipulating fractions—common denominators, etc.  Ask Mathematica to add the fractions 2/3 and 1/2.

11.   Now try adding the algebraic fractions 1/x and 2/x^2 in the same way.

12.   Whoops!  It wouldn't do this one!  Just as with the Expand commmand we learned earlier, Mathematica will not automatically combine algebraic fractions.  We have to tell it that we want this to be done, and the command we need it Together.  Do problem 11 again, but use this new command to make it work properly.

13.   You should have gotten an LCD that time.  Try combining these two fractions:

       3/(x^2 - x - 6)  +  x/(x^2 - 9)

14.  Pretty impressive—it automatically  factored both denominators, decided on the LCD, and combined the two fractions—a big time-saver if you have to do this very often.  Notice that the denominator in the previous result is left in factored form.  What if you'd like to have Mathematica multiply out the three factors?  The command you need is ExpandDenominator.  Apply it to the previous output.

15.   So why did we have to use ExpandDenominator instead of just Expand?  Well find out!  Try applying a plain Expand function to the output from problem 13 and see what happens.

16.   Apparently all that Expand does to a fraction is split the numerator up term by term, each with the LCD left in the denominator.  There are occasions when this might be a useful operation—integration comes to mind—so I guess it's good that it's built into the program.

Speaking of integration, do you remember how to integrate a fraction like: (x+3)/(x^2 - 25) ?  If you recall, the trick was to break the problem apart using  a procedure called partial fractions.  Mathematica's command to do this is called Apart—like the opposite of Together.  Tell Mathematica to use this new command to do a partial fraction expansion of the fraction just mentioned.

17.  If you still remember doing these by hand, you'll recognize how much effort you just saved.

To see how our next topic works I need you to set the value of x to be 5.  Issue the command x=5.

18.   Now define a new variable called poly to be x^2+3x+7.  (Do not use delayed assignment.)

19.   You should have expected what happened!  Since we didn't use delayed assignment, the value of 5 was used in place of x, and now poly has been "permanently" given a value of 47.

Let's fix this.

a)  Remove the value of x with the command x=.
b)  Redefine poly as you did in problem 18, but use delayed assignment (:=).

20.   Now what do we do if we wish to evaluate poly at, say, x=3?  It basically takes a pair of commands:

x=3
poly

Go ahead and enter them.

21.   You should have gotten 25.  Ask Mathematica what poly is again.  (Type poly and hit ENTER.)

22.   It looks like poly is currently keeping a value of 25 until we change the value of x.  Repeat the steps in problem 20 again, but this time use a value of 7 for x.

23.   poly is now 77, right?  Do you see an underlying problem with the way we've been doing things since problem 19?  As soon as x was given a value, poly assumed a numerical value also.  In truth poly is really no longer a genuine polynomial, but rather just an ordinary number (25 or 77 so far.)

This doesn't really hold with the true spirit of substitution.  What we would really like to do is find out what poly becomes when x=3, say, but leave the actual value of poly as its original definition, so that if we ask for poly again we would get its full formula, not just a number.

Substitution is possible in this manner in Mathematica.  Let's see how!

Define poly2 to be y^3-5y+6.

(No need for delayed assignment—y hasn't been used yet anyway.)

24.   Now enter the command poly2/.y->3

(The -> is formed using a minus sign followed by a greater than sign.)

25.   You should have gotten 18.  Now ask for poly2 again.

26.   Aha!  poly2 is still a genuine polynomial with a formula.  The substitution letting y become 3 was purely temporary, affecting only the command it was conatined in.  In fact to verify this further, ask Mathematica what y currently is.

27.  Unless you entered y= "something or other" earlier this session, you should have simply gotten y back again as the result.

/.               means "use the replacement rule"
y->3          is the replacement rule.

y is only set equal to 3 within the expression preceeding the /. and its value in memory is never changed.

To finish up with, substitute 11 for y into poly2 using the same command-form as in problem 24.