Last week, we saw how to create an account, project, and Sage worksheet in CoCalc, and some basic operations inside the Sage worksheet — arithmetic, plotting, solving systems of equations, creating and row-reducing matrices. This week, we will focus on two topics:
- Vector arithmetic, and
- Understanding solution sets of systems of linear equations.
Start by going to the project you created last week, and create a new Sage worksheet, as described in last week’s post.
Before we get started, let’s make Sage give us prettier output. Type the following command and press shift-enter:
%typeset_mode True
Vectors
To create a vector named “v” which is equal to the column vector (1,2,3), type:
v=vector([1,2,3])
(Remember to press shift-return.) Typing v by itself and pressing shift-return should show you the vector v:
Even though Sage displays the vector as a row vector, we (and Sage) are going to treat it as a column vector. (As in post 1, click on the screenshot to see a larger version.)
Let’s create a few more vectors and do some vector arithmetic. Type:
w=vector([3,-1,2])
x=vector([1,7])
v+w
3*v
v+x
(Again, remember to press shift-return after each line.)
The output you get should look like this:
In particular, it doesn’t make sense to add v and x, since they have different numbers of entries, and Sage will tell you this with a (somewhat cryptic) error.
All well and good. Sage can also multiply a matrix by a vector:
M = matrix([[1,0,-1],[3,5,3]])
M
M*v
M*w
M*x
The second line is just to get Sage to print out M for you. The product M*x gives an error, as it should: you can’t multiply a 2×3 matrix by a length 2 vector. Your output should look like this:
Check the product M*v by hand, to convince yourself Sage got the product right.
Even better, Sage can solve the matrix equation Mx=b for you, with the command “solve_right”. For example, let’s solve problem 11 in the current edition of Lay-Lay-McDonald’s Linear Algebra:
N = matrix([[1,2,4],[0,1,5],[-2,-4,-3]])
N
b = vector([-2,2,9])
N.solve_right(b)
(The funny syntax — N.solve_right — is because Python, the language underlying Sage, is object-oriented.) Here’s the answer you should get:
Again, even though the answer is displayed as a row vector, read it as a column vector. Check by hand that Nx=b.
Of course, we could do that ourselves by row-reducing. Let’s use Sage to row-reduce the augmented matrix [N|b]:
Nbarb = matrix([[1,2,4,-2],[0,1,5,2],[-2,-4,-3,9]])
Nbarb
Nbarb.rref()
In fact, there’s an even easier way to got an augmented matrix:
N.augment(b)
You can even get straight to the row-reduced form like this:
N.augment(b).rref()
What does solve_right do if there’s no solution? What about if there’s more than one solution? Input an example of each and find out.
Plotting solution sets
There are two ways to describe lines in the plane:
- Implicitly, as the solutions to one equation in two variables. For example, x+2y=3.
- Parametrically, as the set of points swept out as you vary a parameter in the vector equation x=p+tv. For example, (x,y)=(3,0)+t(-2,1).
Sage can plots either. To plot the first kind of equation, use the command:
%var x,y
implicit_plot(x+2*y==3,(x,-5,5),(y,-5,5))
To plot the second kind of equation, use the command:
%var t
p=vector([3,0])
v=vector([-2,1])
parametric_plot(p+t*v,(t,-5,5))
The code we used for the parametric plot is actually needlessly verbose, to get the structure across. Another equivalent version is
%var t
parametric_plot([3-2*t,t],(t,-5,5))
In both cases, the piece (t,-5,5) says we want to see the points where the parameter t runs from -5 to 5. (Since the line is infinite, Sage can’t show the whole line, of course.) In the implicit case, the (x,-5,5) and (y,-5,5) say that the view window should be -5<x
Going back to the implicit plot, recall that varying the number of the right hand side will give parallel lines, with 0 giving a line through the origin. Let’s draw some, in one plot:
implicit_plot(x+2*y==3,(x,-5,5),(y,-5,5))+implicit_plot(x+2*y==0,(x,-5,5),(y,-5,5))+implicit_plot(x+2*y==-2,(x,-5,5),(y,-5,5))+implicit_plot(x+2*y==-1,(x,-5,5),(y,-5,5))
(That’s right — Sage knows how to add plots.)
For planes in 3 dimensions, the commands are similar but with “3d” at the end. To plot the plane x+2y+3z=4 use:
%var x y z
implicit_plot3d(x+2*y+3*z==4, (x,-5,5),(y,-5,5),(z,-5,5))
This plane is the same as the parametric plane (x,y,z)=(4,0,0)+s(-3,0,1)+t(0,-3/2,1), which we can plot with:
%var s t
v = vector([-3,0,1])
w = vector([0,-3/2,1])
p = vector([4,0,0])
parametric_plot3d(p+s*v+t*w,(s,-2,2),(t,-2,2))
Finally, let’s loop back to the matrix algebra. If we let M be the 1×3 matrix [1,2,3] then we’ve been looking at the equation Mx=[4]. The vectors v and w in the parametric form should be solutions to the equation Mx=[0], and p should satisfy Mp=4. Let’s check:
M = matrix([1,2,3])
M*v
M*w
M*p
That’s it for today, but we’ve covered a lot.