{ "cells": [ { "cell_type": "markdown", "id": "38805e6c", "metadata": {}, "source": [ "\n", "### Our goal\n", "\n", "Let's renew our acquaintance with the Python programming language, and begin to get to know the NumPy library and Jupyter notebooks." ] }, { "cell_type": "markdown", "id": "b52aed69", "metadata": {}, "source": [ "### A parametric definition of a line\n", "\n", "Let's suppose that we have two distinct points in the plane:\n", "\n", "* Let $\\vec{p_0} = (x_0, y_0)$.\n", "* Let $\\vec{p_1} = (x_1, y_1)$.\n", "\n", "For all $t$ such that $0.0 \\leq t \\leq 1.0$ it is the case that the points returned by the function $\\vec{p}(t)$ lie on the line segment defined by $\\vec{p_0}$ and $\\vec{p_1}$:\n", "\n", "* $\\vec{p}(t) = \\vec{p_0} + t \\cdot (\\vec{p_1} - \\vec{p_0})$\n", "\n", "If $t < 0.0$ or $t > 1.0$, then $\\vec{p}(t)$ is a point on the line that passes through $\\vec{p_0}$ and $\\vec{p_1}$ but does not lie between those two points.\n" ] }, { "cell_type": "markdown", "id": "0d9c314a", "metadata": {}, "source": [ "### Exercise\n", "\n", "Write code that creates $N$ points on a line segment bounded by two points $(x_0, y_0)$ and $(x_1, y_1)$ and stores this data in a NumPy array.\n", "\n", "* Create a variable $N$. Assign to it any positive integer value that you choose.\n", "* Create variables to represent the coordinates of the two points. Assign to these variables values in the interval $[-1.0, +1.0]$. Select any values in that range that you like, so long as they define distinct points.\n", "* Write a loop to generate the points. Inside the loop, generate random values of the parameter $t$. Draw the values of $t$ from a uniform distribution of random numbers in the range $0.0$ to $1.0$.\n", "* Define function and write comments as appropriate to make concise, easy to understand code.\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "30590ca5", "metadata": {}, "outputs": [], "source": [ "# Here's how to get the numpy functions.\n", "import numpy as np\n", "\n", "# Learn what kinds of vector arithmetic you can do\n", "# with NumPy arrays.\n", "\n", "# Learn what kinds of functions NumPy provides for\n", "# generating random numbers." ] }, { "cell_type": "code", "execution_count": 7, "id": "2a5fd04b", "metadata": {}, "outputs": [], "source": [ "# Here's how to define a function.\n", "\n", "def makePoints( n ):\n", " # This function returns dummy data.\n", " # This is just a placeholder. \n", " \n", " # Create a list of lists.\n", " listOfPoints = [[0.0, 0.0], [1.0,1.0]]\n", " \n", " # From the list create a NumPy array.\n", " return np.array( listOfPoints )" ] }, { "cell_type": "code", "execution_count": 8, "id": "2d9a32a1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.]\n", "[1. 1.]\n" ] } ], "source": [ "if __name__ == '__main__':\n", " N = 12\n", " data = makePoints(N)\n", " \n", " for p in data:\n", " print( p )\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "eb29b3be", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }