From 003c4d2647cad78f7fcc487af453612969a711f0 Mon Sep 17 00:00:00 2001 From: wuhanstudio Date: Mon, 11 Mar 2024 17:56:21 +0000 Subject: [PATCH] [Question 3] Add a common mistake --- Question 3.ipynb | 147 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 114 insertions(+), 33 deletions(-) diff --git a/Question 3.ipynb b/Question 3.ipynb index e07d119..f244828 100644 --- a/Question 3.ipynb +++ b/Question 3.ipynb @@ -15,6 +15,7 @@ "metadata": {}, "outputs": [], "source": [ + "import math\n", "import numpy as np" ] }, @@ -28,6 +29,14 @@ "points = np.load('data/points.npy').astype(np.uint8)" ] }, + { + "cell_type": "markdown", + "id": "c1e442a7", + "metadata": {}, + "source": [ + "## Method 1" + ] + }, { "cell_type": "code", "execution_count": 3, @@ -35,7 +44,7 @@ "metadata": {}, "outputs": [], "source": [ - "def compute_rotation_matrix(points, theta):\n", + "def m1_compute_rotation_matrix(points, theta):\n", " \"\"\"\n", " Write a function compute_rotation_matrix(points, theta) to compute the rotation matrix in\n", " homogeneous coordinate system to rotate a shape depicted with 2-dimensional (x,y) coordinates\n", @@ -84,10 +93,59 @@ "metadata": {}, "outputs": [], "source": [ - "rotation_matrices = []\n", + "m1_rotation_matrices = []\n", "\n", "for t in range(0, 365, 5):\n", - " rotation_matrices.append( compute_rotation_matrix(points, t) )" + " m1_rotation_matrices.append( m1_compute_rotation_matrix(points, t) )" + ] + }, + { + "cell_type": "markdown", + "id": "5554bde0", + "metadata": {}, + "source": [ + "## Method 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "087b6ac9", + "metadata": {}, + "outputs": [], + "source": [ + "def m2_compute_rotation_matrix(points, theta):\n", + "\n", + " # Convert angle to radians\n", + " theta_rad = np.deg2rad(theta)\n", + " \n", + " cos_theta = np.cos(theta_rad)\n", + " sin_theta = np.sin(theta_rad)\n", + "\n", + " # Compute center of the shape\n", + " center = np.mean(points, axis=0)\n", + " \n", + " # Construct rotation matrix\n", + " rotation_matrix = np.array([\n", + " [ cos_theta, -sin_theta, -center[0] * cos_theta + center[1] * sin_theta + center[0] ],\n", + " [ sin_theta, cos_theta, -center[0] * sin_theta - center[1] * cos_theta + center[1] ],\n", + " [0, 0, 1]\n", + " ])\n", + " \n", + " return rotation_matrix" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "02ec3b60", + "metadata": {}, + "outputs": [], + "source": [ + "m2_rotation_matrices = []\n", + "\n", + "for t in range(0, 365, 5):\n", + " m2_rotation_matrices.append( m2_compute_rotation_matrix(points, t) )" ] }, { @@ -100,12 +158,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "23967b5d", "metadata": {}, "outputs": [], "source": [ - "np.save('data/question_3_rotation_matrices.npy', rotation_matrices)" + "np.save('data/question_3_rotation_matrices.npy', m1_rotation_matrices)" ] }, { @@ -116,35 +174,42 @@ "## Put students' implementations here" ] }, + { + "cell_type": "markdown", + "id": "06417b95", + "metadata": {}, + "source": [ + "This is one common mistake." + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "id": "b0836f8d", "metadata": {}, "outputs": [], "source": [ "def compute_rotation_matrix(points, theta):\n", - " # Convert points to float64\n", - " points = points.astype(np.float64)\n", - " # Calculate centre\n", - " centre = np.mean(points, axis=0)\n", - " # Compute rotation matrix\n", - " rotation_matrix = np.array([[np.cos(np.radians(theta)), -np.sin(np.radians(theta)), 0],\n", - " [np.sin(np.radians(theta)), np.cos(np.radians(theta)), 0],\n", - " [0, 0, 1]])\n", - " # Translation matrix to origin\n", - " translation_to_origin = np.array([[1, 0, -centre[0]],\n", - " [0, 1, -centre[1]],\n", - " [0, 0, 1]])\n", - " # Translation matrix to original position\n", - " translation_to_centre = np.array([[1, 0, centre[0]],\n", - " [0, 1, centre[1]],\n", - " [0, 0, 1]])\n", - " # Combine transformations with data type float64 \n", - " combined_matrix = np.dot(np.dot(translation_to_centre, rotation_matrix), translation_to_origin).astype(np.float64)\n", - " return combined_matrix\n", "\n", - " return 0" + " # Convert angle to radians\n", + " theta_rad = np.deg2rad(theta)\n", + " \n", + " # Calculate sine and cosine of the angle\n", + " cos_theta = np.cos(theta_rad)\n", + " sin_theta = np.sin(theta_rad)\n", + " \n", + " # Compute center of the shape\n", + " center = np.mean(points, axis=0)\n", + " \n", + " # Translate points to origin\n", + " translated_points = points - center\n", + " \n", + " # Wrong: Construct rotation matrix\n", + " rotation_matrix = np.array([[cos_theta, -sin_theta, center[0]],\n", + " [sin_theta, cos_theta, center[1]],\n", + " [0, 0, 1]])\n", + " \n", + " return rotation_matrix" ] }, { @@ -165,7 +230,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "id": "132d734b", "metadata": { "scrolled": false @@ -175,26 +240,42 @@ "name": "stdout", "output_type": "stream", "text": [ - "73 PASS\n", - "ALL PASS\n" + "0 PASS\n", + "Failed\n" + ] + }, + { + "ename": "AssertionError", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[9], line 9\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(n_pass, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPASS\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m----> 9\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m n_pass \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mlen\u001b[39m(m1_rotation_matrices)\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mALL PASS\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAssertionError\u001b[39;00m:\n", + "\u001b[1;31mAssertionError\u001b[0m: " ] } ], "source": [ "n_pass = 0\n", "for t in range(0, 365, 5):\n", - " if np.allclose(compute_rotation_matrix(points, t), rotation_matrices[int(t / 5)]):\n", + " if np.allclose(compute_rotation_matrix(points, t), m1_rotation_matrices[int(t / 5)]):\n", " n_pass = n_pass + 1\n", "\n", "print(n_pass, \"PASS\")\n", - "assert n_pass == len(rotation_matrices)\n", - "print(\"ALL PASS\")" + "\n", + "try:\n", + " assert n_pass == len(m1_rotation_matrices)\n", + " print(\"ALL PASS\")\n", + "except AssertionError:\n", + " print(\"Failed\")\n", + " raise" ] }, { "cell_type": "code", "execution_count": null, - "id": "0fac308a", + "id": "03e9c423", "metadata": {}, "outputs": [], "source": []