SharedLab 1 / Lab 1 Kyle O'Brien 2018-02-28-180036.sagewsOpen in CoCalc
Lab 1 Math 5 Kyle O'Brien Josh Murphy Phil Cook
# 1. (a)

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


show(A)
show(B)
show(V)

(1234)\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)
(0211110)\displaystyle \left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)
(140)\displaystyle \left(\begin{array}{rrr} 1 & 4 & 0 \end{array}\right)
\left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)

\left(\begin{array}{rrr} 1 & 5 & 8 \\ -91 & 45 & 100 \end{array}\right)


# 1. (b)

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

show(A)
latex(A)
# QQ: The numbers become floating point values.

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


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

show(A)
latex(A)

# RDF: The values have a tenths place precision.

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

# 1c
show(B)
show(B.rref())
latex(B)
latex(B.rref())

(0211110)\displaystyle \left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)
(102120112)\displaystyle \left(\begin{array}{rrr} 1 & 0 & \frac{21}{2} \\ 0 & 1 & -\frac{1}{2} \end{array}\right)
\left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right) \left(\begin{array}{rrr} 1 & 0 & \frac{21}{2} \\ 0 & 1 & -\frac{1}{2} \end{array}\right)
# 1d
I4 = identity_matrix(5)
show(I4)
latex(I4)
(1000010000100001)\displaystyle \left(\begin{array}{rrrr} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right)
\left(\begin{array}{rrrr} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right)
# 2a
# Primivitive arithmatic commands (+/-) are valid iff the operands are matrices of the same dimensions.
# Multiplications of vectors is valid with the "*" symbol and if valid iff the number of columns in the left operands is = the number of rows in the right operand.
A = matrix(QQ, [[1, 2], [3, 4]])
B = matrix(QQ, [[0, 2, -1], [1, 1, 10]])

show(A)
show(B)
latex(A)
latex(B)
show(A + A)
show(A - A)
show(2 * A)
show(A * B)
(1234)\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)
(0211110)\displaystyle \left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)
\left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right) \left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)
(2468)\displaystyle \left(\begin{array}{rr} 2 & 4 \\ 6 & 8 \end{array}\right)
(0000)\displaystyle \left(\begin{array}{rr} 0 & 0 \\ 0 & 0 \end{array}\right)
(2468)\displaystyle \left(\begin{array}{rr} 2 & 4 \\ 6 & 8 \end{array}\right)
(241941037)\displaystyle \left(\begin{array}{rrr} 2 & 4 & 19 \\ 4 & 10 & 37 \end{array}\right)
# 2b
show(B)
B_T = B.transpose()
show(B_T)
latex(B)
latex(B_T)

B_T = B.T
show(B_T)

v = matrix(QQ, [[1], [2], [3]])
show(v)
show(v.transpose())

# The rows of the transpose of B are the columns of B. The colums of transpose of B are the rows of B.
# The transpose of an nX1 vector becomes an 1Xn matrix.

(0211110)\displaystyle \left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)
(0121110)\displaystyle \left(\begin{array}{rr} 0 & 1 \\ 2 & 1 \\ -1 & 10 \end{array}\right)
\left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right) \left(\begin{array}{rr} 0 & 1 \\ 2 & 1 \\ -1 & 10 \end{array}\right)
(0121110)\displaystyle \left(\begin{array}{rr} 0 & 1 \\ 2 & 1 \\ -1 & 10 \end{array}\right)
(123)\displaystyle \left(\begin{array}{r} 1 \\ 2 \\ 3 \end{array}\right)
(123)\displaystyle \left(\begin{array}{rrr} 1 & 2 & 3 \end{array}\right)
# 2c

# i. No

A = matrix(QQ, [[1, 2], [3, 4]])
B = matrix(SR, [[5, 6], [7, 8]])
show(A)
show(B)

show((A * B).transpose())
show(A.transpose() * B.transpose())


(1234)\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)
(5678)\displaystyle \left(\begin{array}{rr} 5 & 6 \\ 7 & 8 \end{array}\right)
(19432250)\displaystyle \left(\begin{array}{rr} 19 & 43 \\ 22 & 50 \end{array}\right)
(23313446)\displaystyle \left(\begin{array}{rr} 23 & 31 \\ 34 & 46 \end{array}\right)


# ii. True
show("ii.")
A = matrix(QQ, [[1, 5, 8], [-91, 45, 100]])
B = matrix(QQ, [[0, 76, -9], [101, 678, 666]])

defined = A + B

if defined:
    quantity_transposed = (A + B).T
    sum_of_transposed = (A.T + B.T)
    show(quantity_transposed)
    show(sum_of_transposed)
    latex(quantity_transposed)
    latex(sum_of_transposed)
    show(quantity_transposed == sum_of_transposed)

# iii. True
show("iii.")

c = 10
quantity = c* (A).T
factored = c * A.T

show(quantity)
latex(quantity)
show(factored)
show(quantity == factored)



i.
(333333888555272727)\displaystyle \left(\begin{array}{rrr} -33 & -33 & -33 \\ -8 & -8 & -8 \\ 5 & 5 & 5 \\ 27 & 27 & 27 \end{array}\right)
(16502454120868017212861109135)\displaystyle \left(\begin{array}{rrr} 16 & 50 & -24 \\ 54 & 120 & -86 \\ 80 & 172 & -128 \\ 61 & 109 & -135 \end{array}\right)
False\displaystyle \mathrm{False}
ii.
(110817231766)\displaystyle \left(\begin{array}{rr} 1 & 10 \\ 81 & 723 \\ -1 & 766 \end{array}\right)
(110817231766)\displaystyle \left(\begin{array}{rr} 1 & 10 \\ 81 & 723 \\ -1 & 766 \end{array}\right)
\left(\begin{array}{rr} 1 & 10 \\ 81 & 723 \\ -1 & 766 \end{array}\right) \left(\begin{array}{rr} 1 & 10 \\ 81 & 723 \\ -1 & 766 \end{array}\right)
True\displaystyle \mathrm{True}
iii.
(1091050450801000)\displaystyle \left(\begin{array}{rr} 10 & -910 \\ 50 & 450 \\ 80 & 1000 \end{array}\right)
\left(\begin{array}{rr} 10 & -910 \\ 50 & 450 \\ 80 & 1000 \end{array}\right)
(1091050450801000)\displaystyle \left(\begin{array}{rr} 10 & -910 \\ 50 & 450 \\ 80 & 1000 \end{array}\right)
True\displaystyle \mathrm{True}

# 2d

# i. Yes, this inverse function matches the formula in section 2.1.
A = matrix(QQ, [[1, 2], [3, 4]])
show(A)
inverse_a = A.inverse()
show(inverse_a)

# ii. It gives a determinant. Yes it does match the formula from exam 1.
A.det()

# iii. No, its not a square matrix
B = matrix(QQ, [[0, 2, -1], [1, 1, 10]])
show(B)
#inverse_b = B.inverse()gives an error because its not a square matrix
#show(inverse_b)

# iv.
C = matrix(QQ, [[1, 2], [2, 4]])
show(C)
latex(C)
show(C.det())
#inverse_c = C.inverse()
#show(inverse_c)ZeroDivisionError: input matrix must be nonsingular

# v.
A_T = A.transpose()
A_I = A.inverse()
show(A_T.inverse())
show(A_I.transpose())

latex(A_T.inverse())
(1234)\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)
(213212)\displaystyle \left(\begin{array}{rr} -2 & 1 \\ \frac{3}{2} & -\frac{1}{2} \end{array}\right)
-2
(0211110)\displaystyle \left(\begin{array}{rrr} 0 & 2 & -1 \\ 1 & 1 & 10 \end{array}\right)
(1224)\displaystyle \left(\begin{array}{rr} 1 & 2 \\ 2 & 4 \end{array}\right)
\left(\begin{array}{rr} 1 & 2 \\ 2 & 4 \end{array}\right)
0\displaystyle 0
(232112)\displaystyle \left(\begin{array}{rr} -2 & \frac{3}{2} \\ 1 & -\frac{1}{2} \end{array}\right)
(232112)\displaystyle \left(\begin{array}{rr} -2 & \frac{3}{2} \\ 1 & -\frac{1}{2} \end{array}\right)
\left(\begin{array}{rr} -2 & \frac{3}{2} \\ 1 & -\frac{1}{2} \end{array}\right)
# 3

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", aspectratio=1)
Q3 = plot(M*u1,color="purple", aspectratio=1)
Q4 = plot(M*u2,color="purple", aspectratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

# 3a: The blue vectors are the original vectors and the purple arrows are the vectors that undergone a linear transformeration by A.
# 3b: M tranforms vector x by reflecting about the x2 axis.

# 3c

reset()
M1 = 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(M1*e1, color="purple", aspect_ratio=1)
Q2 = plot(M1*e2, color="purple", aspect_ratio=1)
Q3 = plot(M1*u1,color="purple", aspect_ratio=1)
Q4 = plot(M1*u2,color="purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

# The transformation reflects the entries of the entered vector about the x1 = x2 axis.
# 3d

reset()
M2 = 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(M2*e1, color="purple", aspect_ratio=1)
Q2 = plot(M2*e2, color="purple", aspect_ratio=1)
Q3 = plot(M2*u1,color="purple", aspect_ratio=1)
Q4 = plot(M2*u2,color="purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

# The transformation M2 reflects the entries of the entered vector about the x2 = -x1 axis.
# 3e

reset()
M3 = 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(M3*e1, color="purple", aspect_ratio=1)
Q2 = plot(M3*e2, color="purple", aspect_ratio=1)
Q3 = plot(M3*u1,color="purple", aspect_ratio=1)
Q4 = plot(M3*u2,color="purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)

# M3 transforms vectors in R^2 by projecting them onto the x1 axis.
# 3f

reset()
M4 = 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(M4*e1, color="purple", aspect_ratio=1)
Q2 = plot(M4*e2, color="purple", aspect_ratio=1)
Q3 = plot(M4*u1,color="purple", aspect_ratio=1)
Q4 = plot(M4*u2,color="purple", aspect_ratio=1)
show(P1+Q1, P2+Q2)
show(P3+Q3, P4+Q4)


# M4 transforms vectors in R^2 by a ...
# 3g

reset()

theta = pi / 18
R = matrix(RR, [[cos(theta), sin(theta)], [-sin(theta), cos(theta)]])
M4 = matrix(RR,[[0, 2],[4, 0]])

e1 = vector(RR, [1,0])
e2 = vector(RR, [0,1])
u1 = vector(RR, [-2,1])
u2 = vector(RR, [-2, -1])
P1 = plot(e1,color="blue", aspect_ratio=1)
P2 = plot(e2,color="red", aspect_ratio=1)
P3 = plot(u1,color="teal", aspect_ratio=1)
P4 = plot(u2,color="black", aspect_ratio=1)
Q1 = plot(R*e1, color="purple", aspect_ratio=1)
Q2 = plot(R*e2, color="orange", aspect_ratio=1)
Q3 = plot(R*u1,color="cyan", aspect_ratio=1)
Q4 = plot(R*u2,color="grey", aspect_ratio=1)
show(P1+Q1+P2+Q2+P3+Q3+P4+Q4)

# R transforms vectors in R^2 by rotating clockwise by theta radians.
# 3h (new)

reset()

theta = pi / 4
R = matrix(RR, [[cos(theta), sin(theta)], [-sin(theta), cos(theta)]])
M2 = matrix(RR,[[0,-1],[-1, 0]])

M2_R = M2 * R
R_M2 = R * M2

#show(M2_R)
#show(R_M2)

e1 = vector(RR, [1,0])
e2 = vector(RR, [0,1])
u1 = vector(RR, [-2,1])
u2 = vector(RR, [-2, -1])
P1 = plot(e1,color="blue", aspect_ratio=1)
P2 = plot(e2,color="red", aspect_ratio=1)
P3 = plot(u1,color="teal", aspect_ratio=1)
P4 = plot(u2,color="black", aspect_ratio=1)
Q1 = plot(R_M2*e1, color="purple", aspect_ratio=1)
Q2 = plot(R_M2*e2, color="orange", aspect_ratio=1)
Q3 = plot(R_M2*u1,color="cyan", aspect_ratio=1)
Q4 = plot(R_M2*u2,color="grey", aspect_ratio=1)
show(P1+Q1+P2+Q2+P3+Q3+P4+Q4)

#3i
theta = pi / 2
R = matrix(SR, [[cos(theta), sin(theta)], [-sin(theta), cos(theta)]])

show(R * R)

(cos(θ)2sin(θ)22cos(θ)sin(θ)2cos(θ)sin(θ)cos(θ)2sin(θ)2)\displaystyle \left(\begin{array}{rr} \cos\left(\theta\right)^{2} - \sin\left(\theta\right)^{2} & 2 \, \cos\left(\theta\right) \sin\left(\theta\right) \\ -2 \, \cos\left(\theta\right) \sin\left(\theta\right) & \cos\left(\theta\right)^{2} - \sin\left(\theta\right)^{2} \end{array}\right)
\left(\begin{array}{rr} \cos\left(\theta\right)^{2} - \sin\left(\theta\right)^{2} & 2 \, \cos\left(\theta\right) \sin\left(\theta\right) \\ -2 \, \cos\left(\theta\right) \sin\left(\theta\right) & \cos\left(\theta\right)^{2} - \sin\left(\theta\right)^{2} \end{array}\right)
# 3h (old)

def graph_transformation(transformation):
    e1 = vector(RR,[1,0])
    e2 = vector(RR,[0,1])
    u1 = vector(RR, [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(transformation*e1, color="purple", aspect_ratio=1)
    Q2 = plot(transformation*e2, color="purple", aspect_ratio=1)
    Q3 = plot(transformation*u1,color="purple", aspect_ratio=1)
    Q4 = plot(transformation*u2,color="purple", aspect_ratio=1)
    show(transformation)
    show(P1+Q1, P2+Q2)
    show(P3+Q3, P4+Q4)

theta = pi / 4
M2 = matrix(RR,[[0,-1],[-1, 0]])
R = matrix(RR, [[cos(theta), sin(theta)], [-sin(theta), cos(theta)]])
transformations = [M2 * R, R * M2, R * R]

for transformation in transformations:
    graph_transformation(transformation)


# Note: M2 reflects the entries of the entered vector about the x2 = -x1 axis.
# Note: R transforms vectors in R^2 by rotating about the x2 = -x1 axis.

# M2 * R : The tranformation reflects vectors first by rotating about the x2 = -x1 axis and then reflecting about the x2 = -x1 axis.
# R * M2 : The tranformation reflects vectors first by reflecting about the x2 = -x1 axis then rotating about the x2 = -x1 axis.

#3i

# R * R  : [[-1, 0], [0, -1]]. Refection through the line x2 = -x1


#reset()
(0.7071067811865480.7071067811865480.7071067811865480.707106781186548)\displaystyle \left(\begin{array}{rr} 0.707106781186548 & -0.707106781186548 \\ -0.707106781186548 & -0.707106781186548 \end{array}\right)
(0.7071067811865480.7071067811865480.7071067811865480.707106781186548)\displaystyle \left(\begin{array}{rr} -0.707106781186548 & -0.707106781186548 \\ -0.707106781186548 & 0.707106781186548 \end{array}\right)
(0.0000000000000001.000000000000001.000000000000000.000000000000000)\displaystyle \left(\begin{array}{rr} 0.000000000000000 & 1.00000000000000 \\ -1.00000000000000 & 0.000000000000000 \end{array}\right)

# 4
reset()

# Standard matrix calculated by hand.
standard_T = matrix(QQ, [[1, -1], [1, 1]])
latex(standard_T)
show(standard_T)

a, b = var('a, b')
# T_symbolic(a,b)=[a+b, a-b] # I think melvin entered the rows in the wrong order.
T_symbolic(a,b)=[a-b, a+b] # Corresponding to example T.

show(T_symbolic)
latex(T_symbolic)

T = linear_transformation(QQ^2, QQ^2, T_symbolic)
A = T.matrix(side='right')
show(A)

if standard_T == A:
    show("We're Correct")
    show(T.matrix(side='left')) # What happens when we multiple from the left?
    latex(T.matrix(side='left'))

    # Answer: We get the result transposed.
    show(A.transpose() == T.matrix(side='left')) # True


\left(\begin{array}{rr} 1 & -1 \\ 1 & 1 \end{array}\right)
(1111)\displaystyle \left(\begin{array}{rr} 1 & -1 \\ 1 & 1 \end{array}\right)
(a,b)  (ab,a+b)\displaystyle \left( a, b \right) \ {\mapsto} \ \left(a - b,\,a + b\right)
\left( a, b \right) \ {\mapsto} \ \left(a - b,\,a + b\right)
(1111)\displaystyle \left(\begin{array}{rr} 1 & -1 \\ 1 & 1 \end{array}\right)
We're Correct
(1111)\displaystyle \left(\begin{array}{rr} 1 & 1 \\ -1 & 1 \end{array}\right)
\left(\begin{array}{rr} 1 & 1 \\ -1 & 1 \end{array}\right)
True\displaystyle \mathrm{True}
computer_by_hand = matrix(QQ, [[2, -5], [0, 0], [1, 4], [0, 1]])
show(computer_by_hand)
latex(computer_by_hand)
(25001401)\displaystyle \left(\begin{array}{rr} 2 & -5 \\ 0 & 0 \\ 1 & 4 \\ 0 & 1 \end{array}\right)
\left(\begin{array}{rr} 2 & -5 \\ 0 & 0 \\ 1 & 4 \\ 0 & 1 \end{array}\right)
# 5
reset()

computed_by_hand = matrix(QQ, [[2, -5], [0, 0], [1, 4], [0, 1]])
show(computed_by_hand)

J_symbolic(x1, x2)=[2*x1 - 5*x2, 0, x1 + 4*x2, x2]
J = linear_transformation(QQ^2, QQ^4, J_symbolic)
B = J.matrix(side='right')

show(J_symbolic)
show(B)

latex(J_symbolic)
latex(B)

# The top matrix is what I calculated and the matrix B is via the algorithm.


(25001401)\displaystyle \left(\begin{array}{rr} 2 & -5 \\ 0 & 0 \\ 1 & 4 \\ 0 & 1 \end{array}\right)
(x1,x2)  (2x15x2,0,x1+4x2,x2)\displaystyle \left( x_{1}, x_{2} \right) \ {\mapsto} \ \left(2 \, x_{1} - 5 \, x_{2},\,0,\,x_{1} + 4 \, x_{2},\,x_{2}\right)
(25001401)\displaystyle \left(\begin{array}{rr} 2 & -5 \\ 0 & 0 \\ 1 & 4 \\ 0 & 1 \end{array}\right)
\left( x_{1}, x_{2} \right) \ {\mapsto} \ \left(2 \, x_{1} - 5 \, x_{2},\,0,\,x_{1} + 4 \, x_{2},\,x_{2}\right) \left(\begin{array}{rr} 2 & -5 \\ 0 & 0 \\ 1 & 4 \\ 0 & 1 \end{array}\right)