LUNAR
π
Dashboard
Browse
SW08_Auftrag_EN_Version_1_1
JUPYTER
View Source
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment work SW08 - PYTHON BASICS - Version 1.1\n", "\n", "These are the self-study tasks for the semester week, which you will solve within a week in your JupyterHub environment. After completing your work, download a copy of the Jupyter notebook file locally to your laptop (right-click on the file in the JupyterHub file browser -> Download).\n", "\n", "On ILIAS you will find the corresponding scheduled work assignments every week, where you will upload your solved Jupyter notebook file. After you have submitted your work, you will receive a corresponding sample solution for the work assignment. Your submission will not be corrected. Although the work assignments are marked \"mandatory\", there are no tests in the module. The grades from the tests during the semester count.\n", "\n", "We wish you much success!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "# Math Tasks\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1 - deg2rad\n", "\n", "- Define the **function** ``deg2rad(angle)`` which can convert the angle from **degrees** to **radians**. Use only standard operators (``+,-,...``)\n", "\n", "- Test this" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "180Β° = 3.141592653589793 rad\n" ] } ], "source": [ "import numpy as np\n", "\n", "deg = 180\n", "\n", "def deg2rad(deg):\n", " rad = (deg*np.pi)/180\n", " return rad\n", "\n", "print(f\"{deg}Β° = {deg2rad(deg)} rad\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 2 - rad2deg\n", "\n", "- Define the inverse function `rad2deg(radians)`. Use only standard operators (``+,-,...``)\n", "- Test this" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7853981633974483 rad = 45.0Β°\n" ] } ], "source": [ "rad = np.pi/4\n", "\n", "def rad2deg(rad):\n", " deg = (rad*180)/np.pi\n", " return deg\n", "\n", "print(f\"{rad} rad = {rad2deg(rad)}Β°\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 3 - Ball Surface\n", "\n", "- Define the function ``ball_surface(radius)``, which can calculate and **return** the **surface area of ββa sphere**.\n", "- Give the function a **default value** of `` 5``\n", "- Test this" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Radius (cm): 5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "The surface area of a sphere with r = 5 cm is S_a = 314.2 cmΒ²\n" ] } ], "source": [ "radius = input(\"Radius (cm): \")\n", "\n", "def ball_surface(radius):\n", " surface_area = 4*np.pi*(float(radius)**2)\n", " return surface_area\n", "\n", "print(f\"The surface area of a sphere with r = {radius} cm is S_a = {ball_surface(radius):.1f} cmΒ²\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "# Array tasks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Creation I\n", "\n", "Write a NumPy program to create a **3x3 matrix with values ββbetween 2 and 10**. Assign the array to the variable ``arr_3d``.\n", "\n", "**Tip:**\n", "- ``np.arrange()``\n", "- ``np.reshape()``\n", "\n", "**Result:**\n", "\n", "```\n", "array([[ 2, 3, 4],\n", "[ 5, 6, 7],\n", "[ 8, 9, 10]])\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2 3 4]\n", " [ 5 6 7]\n", " [ 8 9 10]]\n" ] } ], "source": [ "def arr(n,m)->np.ndarray:\n", " array = np.arange(2,11).reshape((n,m))\n", " return array\n", "\n", "arr_3d = arr(3,3)\n", "print(arr_3d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Invert\n", "\n", "- Convert `arr_3d` to a **1D array**.\n", "- Assign the result to the variable `arr_1d`.\n", "\n", "**Result:**\n", "```\n", "array([ 2, 3, 4, 5, 6, 7, 8, 9, 10])\n", "```\n", "\n", "- **Invert** the order of the array (the first element becomes the last).\n", "\n", "\n", "**Result**:\n", "```\n", "array([10, 9, 8, 7, 6, 5, 4, 3, 2])\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10 9 8 7 6 5 4 3 2]\n" ] } ], "source": [ "arr_1d = arr_3d.reshape((9))\n", "\n", "print(arr_1d[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read information\n", "Print the following information from the arrays `arr_3d` and `arr_1d`.\n", "\n", "- Shape\n", "- Number of elements\n", "- Number of dimensions" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr_3d has shape (3, 3), 3 elements, and 2 dimensions.\n", "arr_1d has shape (9,), 9 elements, and 1 dimensions.\n" ] } ], "source": [ "print(f\"arr_3d has shape {arr_3d.shape}, {len(arr_3d)} elements, and {arr_3d.ndim} dimensions.\")\n", "print(f\"arr_1d has shape {arr_1d.shape}, {len(arr_1d)} elements, and {arr_1d.ndim} dimensions.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read values:\n", "\n", "Read the following values ββfrom `arr_1d` **in one statement** (via slicing):\n", "\n", "- `[2,4,6]`\n", "- `[10,9,8,7]`\n", "\n", "Read the following values ββfrom `arr_3d` **in a statement** (via slicing):\n", "\n", "- `[2,3,4]`\n", "- `[2,5,8]`\n", "- `[[6,7],[9,10]]`\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 4 6]\n", "[10 9 8 7]\n", "[2 3 4]\n", "[2 5 8]\n", "[6 7], [ 9 10]\n" ] } ], "source": [ "print(arr_1d[0:5:2])\n", "print(arr_1d[:4:-1])\n", "\n", "print(arr_3d[0])\n", "print(arr_3d[:3,0])\n", "print(\", \".join(map(str, arr_3d[1:3,1:3])))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Arithmetic I\n", "\n", "Create the following array & output it\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1. 1. 1.]\n", " [1. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 1.]\n", " [1. 1. 1. 1. 1.]]\n" ] } ], "source": [ "def arr_5d(n):\n", " arr = np.ones((n,n))\n", " arr[1:-1,1:-1] -= 1\n", " return arr\n", "\n", "print(arr_5d(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array manipulation\n", "Given the following numpy array:\n", "```python\n", "import numpy as np\n", "# --------------------\n", "# given np array\n", "x = np.arange(50).reshape((10,5))\n", "# --------------------\n", "```\n", "\n", "1. **Transpose** the given array and store it in a new variable.\n", "- Print the **shape** of both arrays.\n", "\n", "2. Perform a **matrix multiplication** of the given and the transposed array.\n", "- Store the result as a new variable (`z`).\n", "- Print the **form** of `z`\n", "- Return the **number of dimensions** of `z`\n", "\n", "3. **Reform`z`** into the form ``(10,10,1)``.\n", "- Print the form of `z`\n", "- Print the number of dimensions of `z`\n", "\n", "4. **Type conversion:**\n", "- Print the type of array `z`\n", "- convert the array type to a `float`\n", "- convert the type of the array to a `bool`\n", "- what happens?" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 30 80 130 180 230 280 330 380 430 480]\n", " [ 80 255 430 605 780 955 1130 1305 1480 1655]\n", " [ 130 430 730 1030 1330 1630 1930 2230 2530 2830]\n", " [ 180 605 1030 1455 1880 2305 2730 3155 3580 4005]\n", " [ 230 780 1330 1880 2430 2980 3530 4080 4630 5180]\n", " [ 280 955 1630 2305 2980 3655 4330 5005 5680 6355]\n", " [ 330 1130 1930 2730 3530 4330 5130 5930 6730 7530]\n", " [ 380 1305 2230 3155 4080 5005 5930 6855 7780 8705]\n", " [ 430 1480 2530 3580 4630 5680 6730 7780 8830 9880]\n", " [ 480 1655 2830 4005 5180 6355 7530 8705 9880 11055]] (10, 10)\n", "[[[ 30]\n", " [ 80]\n", " [ 130]\n", " [ 180]\n", " [ 230]\n", " [ 280]\n", " [ 330]\n", " [ 380]\n", " [ 430]\n", " [ 480]]\n", "\n", " [[ 80]\n", " [ 255]\n", " [ 430]\n", " [ 605]\n", " [ 780]\n", " [ 955]\n", " [ 1130]\n", " [ 1305]\n", " [ 1480]\n", " [ 1655]]\n", "\n", " [[ 130]\n", " [ 430]\n", " [ 730]\n", " [ 1030]\n", " [ 1330]\n", " [ 1630]\n", " [ 1930]\n", " [ 2230]\n", " [ 2530]\n", " [ 2830]]\n", "\n", " [[ 180]\n", " [ 605]\n", " [ 1030]\n", " [ 1455]\n", " [ 1880]\n", " [ 2305]\n", " [ 2730]\n", " [ 3155]\n", " [ 3580]\n", " [ 4005]]\n", "\n", " [[ 230]\n", " [ 780]\n", " [ 1330]\n", " [ 1880]\n", " [ 2430]\n", " [ 2980]\n", " [ 3530]\n", " [ 4080]\n", " [ 4630]\n", " [ 5180]]\n", "\n", " [[ 280]\n", " [ 955]\n", " [ 1630]\n", " [ 2305]\n", " [ 2980]\n", " [ 3655]\n", " [ 4330]\n", " [ 5005]\n", " [ 5680]\n", " [ 6355]]\n", "\n", " [[ 330]\n", " [ 1130]\n", " [ 1930]\n", " [ 2730]\n", " [ 3530]\n", " [ 4330]\n", " [ 5130]\n", " [ 5930]\n", " [ 6730]\n", " [ 7530]]\n", "\n", " [[ 380]\n", " [ 1305]\n", " [ 2230]\n", " [ 3155]\n", " [ 4080]\n", " [ 5005]\n", " [ 5930]\n", " [ 6855]\n", " [ 7780]\n", " [ 8705]]\n", "\n", " [[ 430]\n", " [ 1480]\n", " [ 2530]\n", " [ 3580]\n", " [ 4630]\n", " [ 5680]\n", " [ 6730]\n", " [ 7780]\n", " [ 8830]\n", " [ 9880]]\n", "\n", " [[ 480]\n", " [ 1655]\n", " [ 2830]\n", " [ 4005]\n", " [ 5180]\n", " [ 6355]\n", " [ 7530]\n", " [ 8705]\n", " [ 9880]\n", " [11055]]] (10, 10)\n", "<class 'numpy.ndarray'>\n", "[[ 30. 80. 130. 180. 230. 280. 330. 380. 430. 480.]\n", " [ 80. 255. 430. 605. 780. 955. 1130. 1305. 1480. 1655.]\n", " [ 130. 430. 730. 1030. 1330. 1630. 1930. 2230. 2530. 2830.]\n", " [ 180. 605. 1030. 1455. 1880. 2305. 2730. 3155. 3580. 4005.]\n", " [ 230. 780. 1330. 1880. 2430. 2980. 3530. 4080. 4630. 5180.]\n", " [ 280. 955. 1630. 2305. 2980. 3655. 4330. 5005. 5680. 6355.]\n", " [ 330. 1130. 1930. 2730. 3530. 4330. 5130. 5930. 6730. 7530.]\n", " [ 380. 1305. 2230. 3155. 4080. 5005. 5930. 6855. 7780. 8705.]\n", " [ 430. 1480. 2530. 3580. 4630. 5680. 6730. 7780. 8830. 9880.]\n", " [ 480. 1655. 2830. 4005. 5180. 6355. 7530. 8705. 9880. 11055.]]\n", "[[ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]\n", " [ True True True True True True True True True True]]\n" ] } ], "source": [ "x = np.arange(50).reshape((10,5))\n", "#1\n", "t = x.transpose()\n", "#2\n", "z = x@t\n", "print(z, z.shape)\n", "#3\n", "print(z.reshape(10,10,1), z.shape)\n", "#4\n", "print(type(z))\n", "print(z.astype(float))\n", "print(z.astype(bool)) # since the values are different from 0, every value return True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "# Text comprehension + Python\n", "\n", "In a bakery, **three** different types of **dough** $T_1, T_2, T_3$ are made from **four** different\n", "**Types of flour** $M_1, M_2, M_3, M_4$ are produced. From the dough types **three** different\n", "Types of **bread** $B_1, B_2, B_3$ baked.\n", "\n", "The following **tables** show:\n", "\n", "1. How many units of **each type of flour** are needed **per unit of dough** of the different **dough types**\n", "2. How many units of **each type of dough** are needed per **loaf of bread**:\n", "\n", "\n", "\n", "\n", "Let $A$ be the $(4 Γ 3)$ matrix from the **first table (left)** and $B$ be the $(3 Γ 3)$ matrix\n", "from the **second table**.\n", "\n", "## Task\n", "\n", "- Create the two Maritzen $A$ and $B$\n", "\n", "- Calculate $C$ which is the matrix describing the number of units of **flour**\n", "that is needed for a loaf of **bread** of type $B$ : Express the matrix $C$\n", "by $A$ and $B$. **Give reasons for your answer**\n", "\n", "- What shape does $C$ have?\n", "\n", "- What does an arbitrary entry in $C_{ij}$ describe\n" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix A:\n", "[[3 1 1]\n", " [1 3 1]\n", " [2 1 1]\n", " [2 0 4]]\n", "\n", "Matrix B:\n", "[[2 1 2]\n", " [2 1 0]\n", " [0 1 1]]\n", "\n", "AB:\n", "[[8 5 7]\n", " [8 5 3]\n", " [6 4 5]\n", " [4 6 8]]\n", "\n", "AB has a shape (4, 3)\n", "\n", "C describes the number of units of flour that is needed for a loaf of bread\n" ] } ], "source": [ "arr_A = np.array([[3,1,1],[1,3,1],[2,1,1],[2,0,4]])\n", "arr_B = np.array([[2,1,2],[2,1,0],[0,1,1]])\n", "arr_C = arr_A@arr_B\n", "\n", "print(f\"Matrix A:\\n{arr_A}\\n\\nMatrix B:\\n{arr_B}\\n\\nAB:\\n{arr_C}\\n\\nAB has a shape {arr_C.shape}\\n\\nC describes the number of units of flour that is needed for a loaf of bread\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "# Mathematics with arrays\n", "\n", "\n", "## Task 1\n", "Create ``Numpy Arrays`` for all the following matrices. Use **like-named** `variables` for the different matrices.\n", "\n", "\n", "\n", "**EXAMPLE:**\n", "\n", "```python\n", "M = np.array(...)\n", "```\n", "\n", "\n", "\n", "\n", "\n", "**Questions:**\n", "- What shape does `R` have?" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2, 2, 2)\n" ] } ], "source": [ "import math\n", "M = np.array([[1,7],[4,2]])\n", "N = np.array([[6,0],[0,5]])\n", "P = np.array([[-math.pi,-math.pi],[-math.pi,-math.pi]])\n", "Q = np.array([[math.e + math.sqrt(2), math.e],[math.e, math.e + math.sqrt(2)]])\n", "R = np.array([[M,N],[P,Q]])\n", "S = np.array([[M+N,np.zeros((2,2))],[np.zeros((2,2)), P/math.pi]])\n", "\n", "print(R.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2\n", "\n", "Also calculate:\n", "\n", "\n", "\n", "Use only **standard operators**" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "res1 = R+S\n", "res2 = R.transpose()\n", "res3 = M@N\n", "res4 = N@M" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "# Manual Mathematics\n", "\n", "Do the following calculations **by hand**:\n", "\n", "**Given** are the following matrices:\n", "\n", "\n", "\n", "Which of the following **calculations** are **possible**? Calculate them.\n", "Please note that you have to perform **matrix multiplication**.\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- AA can be calculated since the matrix is square: \n", "[1, -6, -4] \n", "[-2, 4, 4] \n", "[-3, 6, 4]\n", "\n", "- AB don't have the same internal factor (3x3 | 2x3);\n", "- AC don't have the same internal factor (3x3 | 1x3);\n", "- AD can be calculated since they have the same internal factor (3x3 | 3x1): \n", "[-7] \n", "[4] \n", "[3] \n", "- BA can be calculated since they have the same internal factor (2x3 | 3x3): \n", "[7, 1, -11] \n", "[0, -8, -4]\n", "- BB don't have the same internal factor (2x3 | 2x3);\n", "- BD can be calculated since they have the same internal factor (2x3 | 3x1): \n", "[-2] \n", "[-10] \n", "- CA can be calculated since they have the same internal factor (1x3 | 3x3): \n", "[5, 0, -6] \n", "- CD can be calculated since they have the same internal factor (1x3 | 3x1): \n", "[1] \n", "- DC can be calculated since they have the same internal factor (3x1 | 1x3): \n", "[-2, 3, -1] \n", "[0, 0, 0] \n", "[6, -9, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "end" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 4 }