SharedLab 1 / Lab 1 Raniel / Nolan.sagewsOpen in CoCalc
# Defining matrices and Vectors
A = matrix (QQ, [[1,2], [3,4]])
B = matrix (QQ, [[0,2,-1], [1, 1, 10]])
V = matrix (QQ, [[1, 4, 0]])
A
B
V
# Displays matrices
show(A)

$\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)$
show(B)

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'B' is not defined
show(V)

$\displaystyle \left(\begin{array}{rrr} 1 & 4 & 0 \end{array}\right)$
# Row reducing the Matrices
A.rref()
latex(A)

[1 0] [0 1] \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)
B_T = B.transpose()
show(B_T)


$\displaystyle \left(\begin{array}{rr} 0 & 1 \\ 2 & 1 \\ -1 & 10 \end{array}\right)$


B_T = B.T
show(B_T)

$\displaystyle \left(\begin{array}{rr} 0 & 1 \\ 2 & 1 \\ -1 & 10 \end{array}\right)$


#C

Q = matrix(QQ,[[4,3],[2,1]])
R = matrix(QQ,[[6,9,3],[4,5,3]])
S = Q*R
show(S.T)

$\displaystyle \left(\begin{array}{rr} 36 & 16 \\ 51 & 23 \\ 21 & 9 \end{array}\right)$

Q.T * R.T

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/structure/element.pyx", line 3486, in sage.structure.element.Matrix.__mul__ (build/cythonized/sage/structure/element.c:22086) return coercion_model.bin_op(left, right, mul) File "sage/structure/coerce.pyx", line 1139, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10736) raise bin_op_exception(op, x, y) TypeError: unsupported operand parent(s) for *: 'Full MatrixSpace of 2 by 2 dense matrices over Rational Field' and 'Full MatrixSpace of 3 by 2 dense matrices over Rational Field'
#I.No because you can't multiply a 2x2 matrix with a 3x2 matrix so this is undefined.
show(Q.T)

$\displaystyle \left(\begin{array}{rr} 4 & 2 \\ 3 & 1 \end{array}\right)$
show(R.T)

$\displaystyle \left(\begin{array}{rr} 6 & 4 \\ 9 & 5 \\ 3 & 3 \end{array}\right)$
show(Q.T+R.T)

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/structure/element.pyx", line 1242, in sage.structure.element.Element.__add__ (build/cythonized/sage/structure/element.c:10884) return coercion_model.bin_op(left, right, add) File "sage/structure/coerce.pyx", line 1139, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10736) raise bin_op_exception(op, x, y) TypeError: unsupported operand parent(s) for +: 'Full MatrixSpace of 2 by 2 dense matrices over Rational Field' and 'Full MatrixSpace of 3 by 2 dense matrices over Rational Field'
#II.

I = matrix(QQ, [[2,3], [4,5]])
W = matrix(QQ,[[2,1],[1,2]])
F = I + W
show(F.T)

$\displaystyle \left(\begin{array}{rr} 4 & 5 \\ 4 & 7 \end{array}\right)$
show(I.T + W.T)


$\displaystyle \left(\begin{array}{rr} 4 & 5 \\ 4 & 7 \end{array}\right)$
show(2*I.T)

$\displaystyle \left(\begin{array}{rr} 4 & 8 \\ 6 & 10 \end{array}\right)$
A = 2 * I
show(A.T)

$\displaystyle \left(\begin{array}{rr} 4 & 8 \\ 6 & 10 \end{array}\right)$



show(A)

$\displaystyle \left(\begin{array}{rr} 4 & 6 \\ 8 & 10 \end{array}\right)$
Ainverse = A.inverse()

show(Ainverse)

$\displaystyle \left(\begin{array}{rr} -\frac{5}{4} & \frac{3}{4} \\ 1 & -\frac{1}{2} \end{array}\right)$
A.det()

-8


A = matrix (QQ, [[1,2], [3,4]])

Ainverse = A.inverse()

show(Ainverse)

$\displaystyle \left(\begin{array}{rr} -2 & 1 \\ \frac{3}{2} & -\frac{1}{2} \end{array}\right)$
A.det()

-2
B = matrix (QQ, [[0,2,-1], [1, 1, 10]])

Binverse = B.inverse()
show(Binverse)
B.det()

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/matrix/matrix_rational_dense.pyx", line 742, in sage.matrix.matrix_rational_dense.Matrix_rational_dense.inverse (build/cythonized/sage/matrix/matrix_rational_dense.c:8617) raise ArithmeticError("self must be a square matrix") ArithmeticError: self must be a square matrix
#B is not invertible because its not a nxn matrix
D= matrix(QQ, [[1,2], [1,2]])
Dinverse = D.inverse()

Error in lines 2-2 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/matrix/matrix_rational_dense.pyx", line 751, in sage.matrix.matrix_rational_dense.Matrix_rational_dense.inverse (build/cythonized/sage/matrix/matrix_rational_dense.c:8723) return self._invert_flint() File "sage/matrix/matrix_rational_dense.pyx", line 654, in sage.matrix.matrix_rational_dense.Matrix_rational_dense._invert_flint (build/cythonized/sage/matrix/matrix_rational_dense.c:8453) raise ZeroDivisionError("input matrix must be nonsingular") ZeroDivisionError: input matrix must be nonsingular

A = matrix (QQ, [[1,2], [3,4]])
show(A)


$\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)$
show(A.T)

$\displaystyle \left(\begin{array}{rr} 1 & 3 \\ 2 & 4 \end{array}\right)$






show(A.T)

$\displaystyle \left(\begin{array}{rr} 1 & 3 \\ 2 & 4 \end{array}\right)$


Ainverse = A.inverse()
show(Ainverse)


$\displaystyle \left(\begin{array}{rr} -2 & 1 \\ \frac{3}{2} & -\frac{1}{2} \end{array}\right)$
show(Ainverse.T)

$\displaystyle \left(\begin{array}{rr} -2 & \frac{3}{2} \\ 1 & -\frac{1}{2} \end{array}\right)$

C = A.T
show(C.inverse())

$\displaystyle \left(\begin{array}{rr} -2 & \frac{3}{2} \\ 1 & -\frac{1}{2} \end{array}\right)$


reset()
M = matrix(RR,[[-1,0],[0,1]])
e1 = vector(QQ,[1,0])
e2 = vector(QQ,[0,1])
u1 = vector(QQ,[1,2])
u2 = vector(QQ, [-2,-1])
P1 = plot(e1,color="blue",aspect_ratio=1)
P2 = plot(e2,color="blue",aspect_ratio=1)
P3 = plot(u1,color="blue",aspect_ratio=1)
P4 = plot(u2,color="blue",aspect_ratio=1)
Q1 = plot(M*e1, color = "purple", aspect_ratio=1)
Q2 = plot(M*e2, color = "purple", aspect_ratio=1)
Q3 = plot(M*u1, color = "purple", aspect_ratio=1)
Q4 = plot(M*u2, color = "purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)



reset()
M = matrix(RR,[[0,1],[1,0]])
e1 = vector(QQ,[1,0])
e2 = vector(QQ,[0,1])
u1 = vector(QQ,[1,2])
u2 = vector(QQ, [-2,-1])
P1 = plot(e1,color="blue",aspect_ratio=1)
P2 = plot(e2,color="blue",aspect_ratio=1)
P3 = plot(u1,color="blue",aspect_ratio=1)
P4 = plot(u2,color="blue",aspect_ratio=1)
Q1 = plot(M*e1, color = "purple", aspect_ratio=1)
Q2 = plot(M*e2, color = "purple", aspect_ratio=1)
Q3 = plot(M*u1, color = "purple", aspect_ratio=1)
Q4 = plot(M*u2, color = "purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

reset()
M = matrix(RR,[[0,-1],[-1,0]])
e1 = vector(QQ,[1,0])
e2 = vector(QQ,[0,1])
u1 = vector(QQ,[1,2])
u2 = vector(QQ, [-2,-1])
P1 = plot(e1,color="blue",aspect_ratio=1)
P2 = plot(e2,color="blue",aspect_ratio=1)
P3 = plot(u1,color="blue",aspect_ratio=1)
P4 = plot(u2,color="blue",aspect_ratio=1)
Q1 = plot(M*e1, color = "purple", aspect_ratio=1)
Q2 = plot(M*e2, color = "purple", aspect_ratio=1)
Q3 = plot(M*u1, color = "purple", aspect_ratio=1)
Q4 = plot(M*u2, color = "purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

reset()
M = matrix(RR,[[1,0],[0,0]])
e1 = vector(QQ,[1,0])
e2 = vector(QQ,[0,1])
u1 = vector(QQ,[1,2])
u2 = vector(QQ, [-2,-1])
P1 = plot(e1,color="blue",aspect_ratio=1)
P2 = plot(e2,color="blue",aspect_ratio=1)
P3 = plot(u1,color="blue",aspect_ratio=1)
P4 = plot(u2,color="blue",aspect_ratio=1)
Q1 = plot(M*e1, color = "purple", aspect_ratio=1)
Q2 = plot(M*e2, color = "purple", aspect_ratio=1)
Q3 = plot(M*u1, color = "purple", aspect_ratio=1)
Q4 = plot(M*u2, color = "purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)


reset()
M = matrix(RR,[[0,2],[4,0]])
e1 = vector(QQ,[1,0])
e2 = vector(QQ,[0,1])
u1 = vector(QQ,[1,2])
u2 = vector(QQ, [-2,-1])
P1 = plot(e1,color="blue",aspect_ratio=1)
P2 = plot(e2,color="blue",aspect_ratio=1)
P3 = plot(u1,color="blue",aspect_ratio=1)
P4 = plot(u2,color="blue",aspect_ratio=1)
Q1 = plot(M*e1, color = "purple", aspect_ratio=1)
Q2 = plot(M*e2, color = "purple", aspect_ratio=1)
Q3 = plot(M*u1, color = "purple", aspect_ratio=1)
Q4 = plot(M*u2, color = "purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)


reset()
theta = pi/2
R = matrix(RR,[[cos(theta),sin(theta)],[-sin(theta),cos(theta)]])
e1 = vector(QQ,[1,0])
e2 = vector(QQ,[0,1])
u1 = vector(QQ,[1,2])
u2 = vector(QQ, [-2,-1])
P1 = plot(e1,color="blue",aspect_ratio=1)
P2 = plot(e2,color="blue",aspect_ratio=1)
P3 = plot(u1,color="blue",aspect_ratio=1)
P4 = plot(u2,color="blue",aspect_ratio=1)
Q1 = plot(R*e1, color = "purple", aspect_ratio=1)
Q2 = plot(R*e2, color = "purple", aspect_ratio=1)
Q3 = plot(R*u1, color = "purple", aspect_ratio=1)
Q4 = plot(R*u2, color = "purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

# part 3h
M2 = matrix(QQ,[[0,-1],[-1,0]])
C = M2*R
show(C)

$\displaystyle \left(\begin{array}{rr} 1.00000000000000 & 0.000000000000000 \\ 0.000000000000000 & -1.00000000000000 \end{array}\right)$
D = R*M2
show(D)

$\displaystyle \left(\begin{array}{rr} -1.00000000000000 & 0.000000000000000 \\ 0.000000000000000 & 1.00000000000000 \end{array}\right)$

E = R*R
show(E)

$\displaystyle \left(\begin{array}{rr} -1.00000000000000 & 0.000000000000000 \\ 0.000000000000000 & -1.00000000000000 \end{array}\right)$


#Part 4

reset()
a, b = var('a, b')
T_symbolic(a,b) = [a+b, a-b]
show(T_symbolic)
T = linear_transformation(QQ^2, QQ^2, T_symbolic)
A = T.matrix(side='right')
show(A)

$\displaystyle \left( a, b \right) \ {\mapsto} \ \left(a + b,\,a - b\right)$
$\displaystyle \left(\begin{array}{rr} 1 & 1 \\ 1 & -1 \end{array}\right)$

reset()
a, b = var('a, b')
T_symbolic(a,b) = [a+b, a-b]
show(T_symbolic)
T = linear_transformation(QQ^2, QQ^2, T_symbolic)
A = T.matrix(side='left')
show(A)

$\displaystyle \left( a, b \right) \ {\mapsto} \ \left(a + b,\,a - b\right)$
$\displaystyle \left(\begin{array}{rr} 1 & 1 \\ 1 & -1 \end{array}\right)$
# Part 5

A = matrix (QQ, [[1,2], [3,4]])

A

[1 2] [3 4]


A = matrix (RR, [[1,2], [3,4]])



[1.00000000000000 2.00000000000000] [3.00000000000000 4.00000000000000]
show(A)
latex(A)

$\displaystyle \left(\begin{array}{rr} 1.00000000000000 & 2.00000000000000 \\ 3.00000000000000 & 4.00000000000000 \end{array}\right)$
\left(\begin{array}{rr} 1.00000000000000 & 2.00000000000000 \\ 3.00000000000000 & 4.00000000000000 \end{array}\right)


A = matrix (RDF, [[1,2], [3,4]])

show(A)

$\displaystyle \left(\begin{array}{rr} 1.0 & 2.0 \\ 3.0 & 4.0 \end{array}\right)$
latex(A)

\left(\begin{array}{rr} 1.0 & 2.0 \\ 3.0 & 4.0 \end{array}\right)



Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'RREF' is not defined

B = matrix (QQ, [[0,2,-1], [1, 1, 10]])
B.rref()

[ 1 0 21/2] [ 0 1 -1/2]
X= B.rref()

latex(X)

\left(\begin{array}{rrr} 1 & 0 & \frac{21}{2} \\ 0 & 1 & -\frac{1}{2} \end{array}\right)

x = identity_matrix(4)
latex(x)

\left(\begin{array}{rrrr} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right)