Exercise - Vectors and Matrices
Exercise - Vectors and Matrices
Vectors
[]
2
→
a=
[]
3
4
;
→
b=
[ ]
2
−1
;
→
c=
[ ] −3
−3
;
→
d=
[ ]
−2
3
;
→
e= 3
5
In [ ]:
→ →
By using the numpy arrays, represent the vectors a to e
In [ ]:
Print the values of some of the vectors to make sure they match the definition:
In [ ]:
→ →
Use NumPy's built-in function linalg.norm to compute the length of the vectors a and e :
In [ ]:
Vector Operations
→ →
Add the vectors a and b :
In [ ]:
In [ ]:
→
→ →
Subtract vector b from a :
In [ ]:
→
Multiply the vector a by some number (scalar):
In [ ]:
→ →
Multiply vectors a and b elementwise:
In [ ]:
→ →
Compute the dot product of a and b by multipling two vectors elementwise and summing the multiples. For
summing, use the NumPy function np.sum :
In [ ]:
→
Apply the last operaton to compute the length of the vector a instead of using the function linalg.norm :
In [ ]:
Matrices
This is part of a broader convention of orderly arranging numbers in a rectangular, table-like structure called a
matrix. A variable containing a vector is represented with a small letter and an arrow above it. A variable
containing a matrix is conventionally represented with a capital letter. For example, M is a matrix containing 12
numbers arranged in a particular way.
[ ]
1 3 −2 4
M3 × 4 = 2 3 −1 −1 3 rows × 4 columns
−1 1 3 4
5 5
In [ ]:
In [26]:
B = np.array([[-1, 1],
[ 3, -3],
[ 4, -2],
[ 5, 5]])
In [27]:
C = np.array([[-1, 3],
[ 3, -1]])
In [28]:
D = np.array([[ 2, 1],
[-1, 2]])
Print the variables containing the matrices to make sure they match the definition:
In [ ]:
Add matrices A
and B
and analyse the result:
In [ ]:
In [ ]:
In [ ]:
Multiplying a Matrix by a Scalar in Python
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: