Defining vectors:
$$x=[3,5,7]$$$$y=[2,4,8]$$a.) Create these vectors as np.arrays and compute the following linear combination: $.5x + 2y$
b.) Find the inner product of x and y, ($x \cdot y$)
Consider the two matrices
$$\mathbf{A_{3 \times 4}} = \begin{bmatrix} 2,4,6, 8 \\ 1,-2,4, -8 \\-3, 4, 5, -5 \end{bmatrix} $$$$\mathbf{B_{3 \times 4}} = \begin{bmatrix} 4,3,-1, 1 \\ -8,6,-4,2 \\1,2,2,1 \end{bmatrix} $$c.) Create matrices $\mathbf{A}$ and $\mathbf{B}$ as np.arrays. Confirm the shape of the matrices are 3x4. Calculate $2\mathbf{A}$
d.) Calculate $2\mathbf{A}+.5\mathbf{B}$
e.) Is it possible to compute the matrix multiplication $\mathbf{AB}$. What happens if you try?
f.) Instead, matrix multiply $\mathbf{A^TB}$ where $T$ represents the transpose (Hint: if you have an np.array, x, you can create the transpose by x.T
g.) Fill a 5x3 matrix, $\mathbf{R}$, with random integers. What shape of an identity matrix, $\mathbf{I}$ do we need so that $\mathbf{IM=M}$. Demonstrate computationally.
import numpy as np
#a
x=np.array([[3,5,7]])
y=np.array([[2,4,8]])
print(.5*x + 2*y)
[[ 5.5 10.5 19.5]]
#b
print(np.dot(x,y.T))
[[82]]
#c
A = np.array([[2,4,6, 8], [ 1,-2,4, -8], [-3, 4, 5, -5]])
B = np.array([[4,3,-1, 1], [-8,6,-4,2], [1,2,2,1]])
print(A.shape)
print(B.shape)
print(2*A)
(3, 4) (3, 4) [[ 4 8 12 16] [ 2 -4 8 -16] [ -6 8 10 -10]]
#d
2*A + .5*B
array([[ 6. , 9.5, 11.5, 16.5], [ -2. , -1. , 6. , -15. ], [ -5.5, 9. , 11. , -9.5]])
#e
np.dot(A,B)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/q_/twndxknx06n5v1nllkjtz0km0000gn/T/ipykernel_39808/1898625942.py in <module> 1 #e ----> 2 np.dot(A,B) /opt/anaconda3/lib/python3.9/site-packages/numpy/core/overrides.py in dot(*args, **kwargs) ValueError: shapes (3,4) and (3,4) not aligned: 4 (dim 1) != 3 (dim 0)
Here we get an error because we are trying to multiply 3x4 by 3x4 matrices. We need the inner dimensions to agree. If we take the transpose of A, then we have a 4x3 matrix, which we can multiply by a 3x4 matrix and we will end up with a 4x4 matrix
#f
np.dot(A.T,B)
array([[ -3, 6, -12, 1], [ 36, 8, 12, 4], [ -3, 52, -12, 19], [ 91, -34, 14, -13]])
#g
R = np.random.randint(10, size=(5, 3))
print(R)
#we need a square 5x5 identity matrix
I = np.eye(5)
print(np.dot(I,R))
[[3 9 4] [5 5 0] [5 8 6] [9 9 6] [3 5 8]] [[3. 9. 4.] [5. 5. 0.] [5. 8. 6.] [9. 9. 6.] [3. 5. 8.]]
I
array([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
Consider 3 countries A, B, and C. Country A has 3 airports, country B has 4 airport, and country C has 5 airports. There are international flights between country A and B and between B and C, but no direct flights between A and C. The matrices below represent the number of direct flights between airports in the different countries.
$$\mathbf{AB_{3 \times 4}} = \begin{bmatrix} 2&4&0& 1 \\ 1&0&4& 0 \\0& 2& 7& 3 \end{bmatrix} $$$$\mathbf{BC_{4 \times 5}} = \begin{bmatrix} 4&3&0& 1& 3 \\ 0&6&0&2& 0 \\2&5&1&0&3 \\1&7&0&2&3 \end{bmatrix} $$So for example the first row and first column of matrice $\mathbf{AB}$, which we label $ab_{1,1}=2$ means two flights fly between airport 1 in country A and airport 1 in country B. Likewise the element in the first row and second column of the matrix, $ab_{1,2}=4$, represents the number of flights between airport 1 in country A and airport 2 in country B.
Do a calculation (in python) of these matrices to give the total possible number connections between countries A and C. (Hint, you wish to end up with a 3x5 matrix, representing the total routes between the 3 airports in A and the 5 airports in C). Explain the logic behind this calculation.
A = np.array([[2,4,0, 1], [ 1,0,4, 0], [0, 4, 5, -5]])
B = np.array([[4,3,-1, 1], [-8,6,-4,2], [1,2,2,1]])