We'll explore Sage's linear algebra capabilities. First, let's do some matrix multiplication.
|
|
For equations of the form
Ax=b
Sage uses the same syntax as Matlab when solving for 'x'. 'A\b' is shorthand for A^-1 b. Of course, not all systems of equations have solutions.
Re-evaluate, changing to
b=vector([0,-4,1]) and then
b=Matrix([0,-4],[1,0])
|
|
Let's check our answer.
|
|
Now we'll examine how sage indexes matrices. Note that the coordinate of the first cell in a matrix is defined as [0,0] rather than [1,1]/
|
|
It's easy to screw this up. Here's what happens if we forget and try to retrieve the third entry in the first line with A[1,3].
|
|
Let's change some entries.
|
|
|
|
|
|
We can expand the matrix by defining an entry beyond the index.
|
|
Note that we can't simply add A to the 5x3 zero matrix:
|
|
Note that we can also store a matrix as a sparse matrix, that is, only storing the coordinates and values of the non-zero entries, in order to save memory. However, it still treats the matrix as an m x n matrix and will return the matrix in proper form when asked.
|
|
Unlike in Matlab, we can't expand a matrix by defining an entry beyond its dimensions. For example, if we wish to expand A (with zeros) to a 5x4 matrix with the last entry equal to two and we write A[4,3]=2, it will return an 'index out of range' error.
Instead we have to add rows and columns using the 'stack' and 'augment' commands.
|
|