Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

### solutions

parents
 %% Cell type:markdown id: tags: # Create these arrays in order array([[1,0,0,0,1], array([[0,0,0,0,0], array([[1, 1, 1, 1, 1], [0,1,0,1,0], [2,0,0,0,0], [1, 2, 3, 4, 5], [0,0,1,0,0], [0,3,0,0,0], [1, 4, 9, 16, 25], [0,1,0,1,0], [0,0,4,0,0], [1, 8,27, 64,125], [1,0,0,0,1]]) [0,0,0,5,0]]) [1,16,81,256,625]]) %% Cell type:markdown id: tags: ### Before you continue There are many ways to create these matrix, following solutions are just one way to create the corresponding matrix. To guarantee fast code, try your best to just use functions provided in numpy. To understand the code, try print out the result of each line or parts of it. %% Cell type:code id: tags: ``` python import numpy as np import time ``` %% Cell type:markdown id: tags: # First one %% Cell type:code id: tags: ``` python # First one, bit smelly solution x = np.eye(5) y = np.fliplr(x) ((x+y)>0).astype(float) print("x: \n", x, "\n") print("y: \n", y, "\n") print("x+y: \n", x+y, "\n") print("(x+y)>0: \n", (x+y)>0, "\n") print("((x+y)>0).astype(float): \n", ((x+y)>0).astype(float), "\n") ``` %% Cell type:markdown id: tags: # Second one %% Cell type:code id: tags: ``` python # Second one x = np.arange(1,6) np.diag(x, k=-1)[1:, 1:] print(f"x: \n {x} \n") print(f"np.diag(x, k=-1): \n{np.diag(x, k=-1)} \n") print(f"np.diag(x, k=-1)[1:, 1:]: \n{np.diag(x, k=-1)[1:, 1:]} \n") ``` %% Cell type:markdown id: tags: # Third one This one is a bit hard, but try breaking it down yourself and try and see if you understand what is going on. There are two solutions, the first one used python lists, while the second one just uses functions from numpy. Try breaking down the solutions down and print them out. I will leave some code below to hint what is going on. %% Cell type:code id: tags: ``` python # Third one # First solution: # (np.array([np.arange(1,6),] * 5).T ** np.arange(5)).T # Second solution (better one): (np.tile(np.arange(1,6), (5,1)).T ** np.arange(5)).T ``` %%%% Output: execute_result array([[ 1, 1, 1, 1, 1], [ 1, 2, 3, 4, 5], [ 1, 4, 9, 16, 25], [ 1, 8, 27, 64, 125], [ 1, 16, 81, 256, 625]], dtype=int32) %% Cell type:markdown id: tags: ### Hints %% Cell type:code id: tags: ``` python x = np.arange(25).reshape(5,5) # Create an array to use for example print(f"x: \n{x}") ``` %% Cell type:code id: tags: ``` python y = np.array([1,2,1,1,0]) print(f"y: \n{y}\n") print(f"x ** y: \n{x ** y}") ```
 %% Cell type:code id: tags: ``` python import numpy as np import time ``` %% Cell type:code id: tags: ``` python def f(x): return x**2 - 3*x + 4 # Python x = np.arange(1e6) y = [] start = time.perf_counter() for i in x: y.append(f(i)) print(time.perf_counter() - start) ``` %% Cell type:code id: tags: ``` python # Numpy goes brrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr x = np.arange(1e6) start = time.perf_counter() y = f(x) print(time.perf_counter() - start) ``` %% Cell type:code id: tags: ``` python ```
 %% Cell type:code id: tags: ``` python import numpy as np ``` %% Cell type:code id: tags: ``` python def intersection_point(A, c): """ The original formula is: A @ r = -c where r is a 2x1 matrix that contains the x and y coordinate of the intersection point. Hence from the formula above we could get: A @ r = -c inv(A) @ A @ r = inv(A) @ -c I @ r = inv(A) @ -c r = inv(A) @ -c where I is the identity matrix """ return np.linalg.inv(A) @ -c ``` %%%% Output: execute_result array([[0.], [1.]]) %% Cell type:code id: tags: ``` python #Example: A = np.array([[1,1], [2,1]]) c = np.array([1, 1]).reshape(2,1) np.dot(np.linalg.inv(A), c) ``` %% Cell type:code id: tags: ``` python def triangle_area(x): A12 = x[:2, :-1] c12 = x[:2, -1] A23 = x[1:3, :-1] c23 = x[1:3, -1] mod_x = np.delete(x, 1, axis=0) A13 = mod_x[:, :-1] c13 = mod_x[:, -1] # Calculate the intersection points A = intersection_point(A12,c12) B = intersection_point(A23,c23) C = intersection_point(A13,c13) # # Check if A, B and C are correct # print(A) # print(B) # print(C) # Calculate the vectors AB = B-A AC = C-A return 0.5 * np.linalg.norm(np.cross(AB, AC)) ``` %% Cell type:code id: tags: ``` python # Test test = np.array([[0, 1, 0], [1, -1, 0], [1, 1, -2]]) print(f"Area of triangle is {triangle_area(test)}") ```
This diff is collapsed.
 { "cells": [], "metadata": {}, "nbformat": 4, "nbformat_minor": 2 }
 { "cells": [], "metadata": {}, "nbformat": 4, "nbformat_minor": 2 }
 %% Cell type:code id: tags: ``` python import numpy as np ``` %% Cell type:code id: tags: ``` python arr = np.arange(25).reshape(5,5) # <-creates a 5x5 matrix, which values from 0 to 24 arr ``` %%%% Output: execute_result array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) %% Cell type:code id: tags: ``` python # Access element at xth row, yth column arr[3, 2] ``` %%%% Output: execute_result 17 %% Cell type:code id: tags: ``` python # Creating matrix using functions fn = lambda x,y: 10*x + y a = np.fromfunction(fn,(6,6),dtype=np.int) a ``` %%%% Output: execute_result array([[ 0, 1, 2, 3, 4, 5], [10, 11, 12, 13, 14, 15], [20, 21, 22, 23, 24, 25], [30, 31, 32, 33, 34, 35], [40, 41, 42, 43, 44, 45], [50, 51, 52, 53, 54, 55]]) %% Cell type:code id: tags: ``` python x = a[a%2 == 0] # will return a flattened array with only even elements x ``` %%%% Output: execute_result array([ 0, 2, 4, 10, 12, 14, 20, 22, 24, 30, 32, 34, 40, 42, 44, 50, 52, 54]) %% Cell type:code id: tags: ``` python x = a[a%2 == 0] = -1 # which can be assigned a new value based on their common property x ``` %%%% Output: execute_result -1 %% Cell type:code id: tags: ``` python ```
 %% Cell type:markdown id: tags: # Create these arrays in order array([[1,0,0,0,1], array([[0,0,0,0,0], array([[1, 1, 1, 1, 1], [0,1,0,1,0], [2,0,0,0,0], [1, 2, 3, 4, 5], [0,0,1,0,0], [0,3,0,0,0], [1, 4, 9, 16, 25], [0,1,0,1,0], [0,0,4,0,0], [1, 8,27, 64,125], [1,0,0,0,1]]) [0,0,0,5,0]]) [1,16,81,256,625]]) %% Cell type:markdown id: tags: ### Before you continue There are many ways to create these matrix, following solutions are just one way to create the corresponding matrix. To guarantee fast code, try your best to just use functions provided in numpy. To understand the code, try print out the result of each line or parts of it. %% Cell type:code id: tags: ``` python import numpy as np import time ``` %% Cell type:markdown id: tags: # First one %% Cell type:code id: tags: ``` python # First one, bit smelly solution x = np.eye(5) y = np.fliplr(x) ((x+y)>0).astype(float) # Breaking it down print("x: \n", x, "\n") print("y: \n", y, "\n") print("x+y: \n", x+y, "\n") print("(x+y)>0: \n", (x+y)>0, "\n") print("((x+y)>0).astype(float): \n", ((x+y)>0).astype(float), "\n") ``` %% Cell type:markdown id: tags: # Second one %% Cell type:code id: tags: ``` python # Second one x = np.arange(1,6) np.diag(x, k=-1)[1:, 1:] # Breaking it down print(f"x: \n {x} \n") print(f"np.diag(x, k=-1): \n{np.diag(x, k=-1)} \n") print(f"np.diag(x, k=-1)[1:, 1:]: \n{np.diag(x, k=-1)[1:, 1:]} \n") ``` %% Cell type:markdown id: tags: # Third one This one is a bit hard, but try breaking it down yourself and try and see if you understand what is going on. There are two solutions, the first one used python lists, while the second one just uses functions from numpy. Try breaking down the solutions down and print them out. I will leave some code below to hint what is going on. %% Cell type:code id: tags: ``` python # Third one # First solution: # (np.array([np.arange(1,6),] * 5).T ** np.arange(5)).T # Second solution (better one): (np.tile(np.arange(1,6), (5,1)).T ** np.arange(5)).T ``` %%%% Output: execute_result array([[ 1, 1, 1, 1, 1], [ 1, 2, 3, 4, 5], [ 1, 4, 9, 16, 25], [ 1, 8, 27, 64, 125], [ 1, 16, 81, 256, 625]], dtype=int32) %% Cell type:markdown id: tags: ### Hints %% Cell type:code id: tags: ``` python x = np.arange(25).reshape(5,5) # Create an array to use for example print(f"x: \n{x}") ``` %% Cell type:code id: tags: ``` python y = np.array([1,2,1,1,0]) print(f"y: \n{y}\n") print(f"x ** y: \n{x ** y}") ```
 %% Cell type:code id: tags: ``` python import numpy as np import time ``` %% Cell type:code id: tags: ``` python def f(x): return x**2 - 3*x + 4 # Python x = np.arange(1e6) y = [] start = time.perf_counter() for i in x: y.append(f(i)) time_py = time.perf_counter() - start print(f"Time usage for python {time_py}") ``` %% Cell type:code id: tags: ``` python # Numpy x = np.arange(1e6) start = time.perf_counter() y = f(x) time_np = time.perf_counter() - start print(f"Time usage for python {time_np}") print("Numpy goes brrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr") ``` %% Cell type:code id: tags: ``` python ```
 %% Cell type:code id: tags: ``` python import numpy as np ``` %% Cell type:code id: tags: ``` python def intersection_point(A, c): """ The original formula is: A @ r = -c where r is a 2x1 matrix that contains the x and y coordinate of the intersection point. Hence from the formula above we could get: A @ r = -c inv(A) @ A @ r = inv(A) @ -c I @ r = inv(A) @ -c r = inv(A) @ -c where I is the identity matrix """ return np.linalg.inv(A) @ -c ``` %% Cell type:code id: tags: ``` python #Example: A = np.array([[1,1], [2,1]]) c = np.array([1, 1]).reshape(2,1) np.dot(np.linalg.inv(A), c) ``` %%%% Output: execute_result array([[0.], [1.]]) %% Cell type:code id: tags: ``` python def triangle_area(x): A12 = x[:2, :-1] c12 = x[:2, -1] A23 = x[1:3, :-1] c23 = x[1:3, -1] mod_x = np.delete(x, 1, axis=0) A13 = mod_x[:, :-1] c13 = mod_x[:, -1] # Calculate the intersection points A = intersection_point(A12,c12) B = intersection_point(A23,c23) C = intersection_point(A13,c13) # # Check if A, B and C are correct # print(A) # print(B) # print(C) # Calculate the vectors AB = B-A AC = C-A return 0.5 * np.linalg.norm(np.cross(AB, AC)) ``` %% Cell type:code id: tags: ``` python # Test test = np.array([[0, 1, 0], [1, -1, 0], [1, 1, -2]]) print(f"Area of triangle is {triangle_area(test)}") ```
This diff is collapsed.
 %% Cell type:code id: tags: ``` python def fizz_buzz(m): """ Divisible by: 3 : Fizz 5 : Buzz Both: FizzBuzz Neither: Nothing """ F = "Fizz" B = "Buzz" print(0) for i in range(1,m): string = "" div_3 = i % 3 div_5 = i % 5 if div_3 == 0: string += F if div_5 == 0: string += B print(f"{i} {string}") fizz_buzz(20) ```
 %% Cell type:code id: tags: ``` python def countdown(n): while n > 0: yield n n -= 1 return ``` %% Cell type:code id: tags: ``` python c10 = countdown(10)