2.6. Exercise Solutions#

Solution to Exercise 2.1

import numpy as np

# Exercise 2.1
print("\nExercise 2.1\n------------")

a = np.array([6, 3, 4, -1])
print(f"1. a = {a} \n")

B = np.array([[3, 5, -2], [2, 4, 3], [7, 2, -1]])
print(f"2. B = \n\n {B} \n")

C = np.array([[2, 0, -1, 4], [7, -3, 9, -5]])
print(f"3. C = \n\n {C} \n")

D = np.array([[-4, 4, 2], [7, 5, -3], [5, 1, 6]])
print(f"4. D = \n\n {D} \n")

odd = np.arange(1, 32, 2)
print(f"5. Odd numbers upto and including 31:\n\n {odd} \n")

mult_6 = np.arange(0, 100, 6)
print(f"6. Multiples of 6 between 0 and 100:\n\n {np.arange(0, 100, 6)} \n")

mult_9 = np.arange(999, 899, -9)
print(f"7. Multiples of 9 between 900 and 1000 in reverse order: \n\n {mult_9} \n")

threes = 3 * np.ones((4, 8))
print(f"8. A 4x8 array of 3s: \n\n {threes} \n")

I_10 = np.eye(10)
print(f"9. A 10x10 identity matrix: \n\n {I_10} \n")

Output

Exercise 2.1
------------
1. a = [ 6  3  4 -1] 

2. B = 

 [[ 3  5 -2]
 [ 2  4  3]
 [ 7  2 -1]] 

3. C = 

 [[ 2  0 -1  4]
 [ 7 -3  9 -5]] 

4. D = 

 [[-4  4  2]
 [ 7  5 -3]
 [ 5  1  6]] 

5. Odd numbers upto and including 31:

 [ 1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31] 

6. Multiples of 6 between 0 and 100:

 [ 0  6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96] 

7. Multiples of 9 between 900 and 1000 in reverse order: 

 [999 990 981 972 963 954 945 936 927 918 909 900] 

8. A 4x8 array of 3s: 

 [[3. 3. 3. 3. 3. 3. 3. 3.]
 [3. 3. 3. 3. 3. 3. 3. 3.]
 [3. 3. 3. 3. 3. 3. 3. 3.]
 [3. 3. 3. 3. 3. 3. 3. 3.]] 

9. A 10x10 identity matrix: 

 [[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]] 

Solution to Exercise 2.2

# Exercise 2.2
print("\nExercise 2.2\n------------")
print(f"1. a(3) = {a[2]}")
print(f"2. B(1,2) = {B[0,1]}")
print(f"3. Middle two elements of a = {a[1:3]}")
print(f"4. 3rd column of C = {C[:,2]}")
print(f"5. Last 2 rows and columns of D: \n\n {D[-2:,-2:]} \n")
print(f"6. B with rows in reverse order: \n\n {B[::-1,:]} \n")
print(f"7. 1st and 3rd columns of D: \n\n {D[:,0::2]}")

Output

Exercise 2.2
------------
1. a(3) = 4
2. B(1,2) = 5
3. Middle two elements of a = [3 4]
4. 3rd column of C = [-1  9]
5. Last 2 rows and columns of D: 

 [[ 5 -3]
 [ 1  6]] 

6. B with rows in reverse order: 

 [[ 7  2 -1]
 [ 2  4  3]
 [ 3  5 -2]] 

7. 1st and 3rd columns of D: 

 [[-4  2]
 [ 7 -3]
 [ 5  6]]

Solution to Exercise 2.3

# Exercise 2.3
print("\nExercise 2.3\n------------")
print(f"1. 2a = {2 * a} \n")
print(f"2. B + D = \n\n {B + D} \n")
print(f"3. C^T = \n\n {C.T} \n")
print(f"4. B * D = \n\n {B * D} \n")
print(f"5. DB = \n\n {np.dot(D, B)} \n")
print(f"6. DBB^T = \n\n {np.linalg.multi_dot((D, B, B.T))} \n")
print(f"7. D.^3 = \n\n {D ** 3} \n")
print(f"8. B^4 = \n\n {np.linalg.matrix_power(B, 4)} \n")
print(f"9. det(B) = {np.linalg.det(B)} \n")
print(f"10. inv(D) = \n\n {np.linalg.inv(D)} \n")

Output

Exercise 2.3
------------
1. 2a = [12  6  8 -2] 

2. B + D = 

 [[-1  9  0]
 [ 9  9  0]
 [12  3  5]] 

3. C^T = 

 [[ 2  7]
 [ 0 -3]
 [-1  9]
 [ 4 -5]] 

4. B * D = 

 [[-12  20  -4]
 [ 14  20  -9]
 [ 35   2  -6]] 

5. DB = 

 [[ 10   0  18]
 [ 10  49   4]
 [ 59  41 -13]] 

6. DBB^T = 

 [[ -6  74  52]
 [267 228 164]
 [408 243 508]] 

7. D.^3 = 

 [[-64  64   8]
 [343 125 -27]
 [125   1 216]] 

8. B^4 = 

 [[1308 1598  133]
 [1385 2314  510]
 [1399 1583  452]] 

9. det(B) = 133.0 

10. inv(D) = 

 [[-0.08333333  0.05555556  0.05555556]
 [ 0.14393939  0.08585859 -0.00505051]
 [ 0.04545455 -0.06060606  0.12121212]] 

Solution to Exercise 2.4

# Exercise 2.4
print("\nExercise 2.4\n------------")

new_matrix = np.append(B, D, 1)
print(f"1. D appended to the right of B:\n\n {new_matrix} \n")

new_matrix = np.insert(B, 2, D.T, 1)
print(f"2. D inserted between the 2nd and 3rd columns of B: \n\n {new_matrix} \n")

new_matrix = np.delete(B, 1, 0)
print(f"3. B with the middle row deleted: \n\n {new_matrix} \n")

a= np.sort(a)
print(f"4. a sorted in descending order: {a} \n")

new_matrix = np.reshape(C, (8, 1))
print(f"5. C reshaped into an 8x1 array:\n\n {new_matrix} \n")

Output

Exercise 2.4
------------
1. D appended to the right of B:

 [[ 3  5 -2 -4  4  2]
 [ 2  4  3  7  5 -3]
 [ 7  2 -1  5  1  6]] 

2. D inserted between the 2nd and 3rd columns of B: 

 [[ 3  5 -4  4  2 -2]
 [ 2  4  7  5 -3  3]
 [ 7  2  5  1  6 -1]] 

3. B with the middle row deleted: 

 [[ 3  5 -2]
 [ 7  2 -1]] 

4. a sorted in descending order: [-1  3  4  6] 

5. C reshaped into an 8x1 array:

 [[ 2]
 [ 0]
 [-1]
 [ 4]
 [ 7]
 [-3]
 [ 9]
 [-5]]