Section B.2 Linear algebra basics
Entering a matrix.
A matrix is entered as a list of rows, where each row is a list of entries.xxxxxxxxxx
A = matrix( [ [1,2,3], [4,5,6], [0,7,8] ] )
print(A)
A
instead of print(A)
to get an echo of your input.Matrix operations.
You can do all the things you think you can with a matrix.xxxxxxxxxx
A.transpose()
xxxxxxxxxx
A.det()
xxxxxxxxxx
A.inverse()
xxxxxxxxxx
A.rref()
Matrix arithmetic.
This all works as you'd expect.xxxxxxxxxx
B = matrix( [ [1,1,1], [2,2,2], [3,3,3] ] )
print(B)
xxxxxxxxxx
A + B
xxxxxxxxxx
A * B
xxxxxxxxxx
7 * B
xxxxxxxxxx
B^2
Vectors.
Sage has avector
object type. Notice all the brackets.
xxxxxxxxxx
v = vector((1,2,3))
print(v)
xxxxxxxxxx
v * A
xxxxxxxxxx
A * v
xxxxxxxxxx
v1 = vector((1,0,-1,1))
v2 = vector((1,1,0,2))
v3 = vector((0,1,1,1))
M = matrix([v1,v2,v3])
print(M)
xxxxxxxxxx
M.transpose()
column_matrix
method in the first place.
xxxxxxxxxx
P = column_matrix([v1,v2,v3])
print(P)
xxxxxxxxxx
P.rref()
rank
.
xxxxxxxxxx
P.rank()
transpose
or column_matrix
methods.
xxxxxxxxxx
matrix([v1,v2,v3]).rank()
xxxxxxxxxx
print(A)
print()
col1 = A.column(0)
col2 = A.column(1)
col3 = A.column(2)
print("col1 = ",col1)
print("col1 = ",col2)
print("col1 = ",col3)
xxxxxxxxxx
b = vector((1,0,-1))
SYS = A.augment(b)
print(SYS)
print()
SYS.rref()
P
is the matrix we made with columns equal to v1
, v2
, and v3
. Notice how we can chain a bunch of computations into one command.
xxxxxxxxxx
w = vector((7,-1,2,2))
P.augment(w).rref()
\begin{equation*}
\left[\begin{array}{ccc|c} \uvec{v}_1 \amp \uvec{v}_2 \amp \uvec{v}_3 \amp \uvec{w} \end{array}\right]
\end{equation*}
then the third row says that this system is inconsistent, so \uvec{w} is not in \Span \{ \uvec{v}_1, \uvec{v}_2, \uvec{v}_3 \}\text{.}
Let's cook up another example where \uvec{w} will definitely be in the span of the \uvec{v} vectors. This time we'll drop the creation of the intermediate w_column
matrix.
xxxxxxxxxx
w = 3 * v1 - 2 * v2 + v3
P.augment(w).rref()
Special forms of matrices.
Zero matrix.xxxxxxxxxx
Z = zero_matrix(3)
print(Z)
I
as a variable name for it, Sage already uses that for the imaginary complex number.)
xxxxxxxxxx
Ident = identity_matrix(3)
print(Ident)
xxxxxxxxxx
7 * Ident
xxxxxxxxxx
x * Ident
xxxxxxxxxx
D = diagonal_matrix([2,3,5])
xxxxxxxxxx
C = matrix([ [1,2], [3,4] ])
block_C = block_matrix([ [C, -C], [C^2, C] ])
print(block_C)
xxxxxxxxxx
print(block_C)
print("First column vector in block_C is ",block_C.column(0))
augment
method. But remember that Sage thinks of vectors as row vectors. While Sage does this right thing if we do M.augment(v)
for matrix M
and vector v
and appends the vector as a new column, it isn't so accommodating with the block_matrix
method, as there is too much possible ambiguity. So we need to explicitly turn our vector into a column matrix.
xxxxxxxxxx
SYS = block_matrix([ [A, column_matrix([b])] ])
print(SYS)
print()
SYS.rref()
1
or 0
, and Sage will figure out the correct size of block to use based on other blocks around them. (But you can only put a 1
where it will definitely be a square block.)
xxxxxxxxxx
M = block_matrix([ [1,matrix([b]),0], [0,A,1], [0,0,A] ])
print(M)
xxxxxxxxxx
M = block_diagonal_matrix([ C, identity_matrix(1), A ])
print(M)
identity_matrix(1)
instead of just 1
, or else Sage would have told us Error: insufficient information to determine dimensions
.)