{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this exercise you will be provided with a problem and and asked to construct two solutions. The first solution should be without numpy and the second with numpy. The vectorized forms with numpy should perform magnitudes more quickly."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vector argument: [ 0 1 2 ... 9999997 9999998 9999999]\n",
"Matrix argument: [[ 0 1 2 ... 97 98 99]\n",
" [ 1 2 3 ... 98 99 100]\n",
" [ 2 3 4 ... 99 100 101]\n",
" ...\n",
" [197 198 199 ... 294 295 296]\n",
" [198 199 200 ... 295 296 297]\n",
" [199 200 201 ... 296 297 298]]\n"
]
}
],
"source": [
"arg_length = 10000000\n",
"arg_vector = np.arange(arg_length) # An example vector argument.\n",
"print(f\"Vector argument: {arg_vector}\")\n",
"\n",
"num_rows, num_cols = 200, 100\n",
"arg_matrix = np.array([[i + j for j in range(num_cols)] for i in range(num_rows)]) # An example matrix argument.\n",
"print(f\"Matrix argument: {arg_matrix}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Problem 1: Construct a function which takes a vector, and returns an vector with each element doubled."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def doubler(x):\n",
" \"\"\"Take an array and return an array with each element doubled. This version does not use numpy.\n",
" \n",
" :param x: An array of numbers.\n",
" :return: An array of doubled numbers.\n",
" \"\"\" \n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(doubler(arg_vector)[:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def doubler_numpy(x):\n",
" \"\"\"Take an array and return an array with each element doubled. This version uses numpy.\n",
" \n",
" :param x: An array of numbers.\n",
" :return: An array of doubled numbers.\n",
" \"\"\" \n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(doubler_numpy(arg_vector)[:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Problem 2: Construct a function which takes two vectors and returns the elementwise summation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def adder(a, b):\n",
" \"\"\"Take two arrays and return an array with their elementwise addition. This version does not use numpy.\n",
" \n",
" :param a: An array of numbers.\n",
" :param b: An array of numbers.\n",
" :return: An array of the elementwise addition of a and b.\n",
" \"\"\" \n",
" raise NotImplementedError\n",
" \n",
"start_time = time.time()\n",
"print(adder(arg_vector, arg_vector)[:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def adder_numpy(a, b):\n",
" \"\"\"Take two arrays and return an array with their elementwise addition. This version uses numpy.\n",
" \n",
" :param a: An array of numbers.\n",
" :param b: An array of numbers.\n",
" :return: An array of the elementwise addition of a and b.\n",
" \"\"\"\n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(adder_numpy(arg_vector, arg_vector)[:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Problem 3: Construct a function which takes a matrix and returns the tranpose."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def transposer(A):\n",
" \"\"\"Take an mxn matrix (with m,n > 0) and return a nxm matrix with the transpose.\n",
" \n",
" :param A: A mxn matrix.\n",
" :return: A nxm matrix.\n",
" \"\"\" \n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(transposer(arg_matrix)[0][:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def transposer_numpy(A):\n",
" \"\"\"Take an mxn matrix (with m,n > 0) and return a nxm matrix with the transpose.\n",
" \n",
" :param A: A mxn matrix.\n",
" :return: A nxm matrix.\n",
" \"\"\"\n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(transposer_numpy(arg_matrix)[0][:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Problem 4: Construct a function which takes two matrices and returns their dot product."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dot(A, B):\n",
" \"\"\"Take an mxn matrix and a nxk (with m,n, k > 0) and return a mxr matrix with the transpose.\n",
" \n",
" :param A: A mxn matrix.\n",
" :param B: A nxp matrix.\n",
" :return: A mxp matrix.\n",
" \"\"\" \n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(dot(arg_matrix, arg_matrix.T)[0][:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dot_numpy(A, B):\n",
" \"\"\"Take an mxn matrix and a nxk (with m,n, k > 0) and return a mxr matrix with the transpose.\n",
" \n",
" :param A: A mxn matrix.\n",
" :param B: A nxr matrix.\n",
" :return: A nxr matrix.\n",
" \"\"\" \n",
" raise NotImplementedError\n",
"\n",
"start_time = time.time()\n",
"print(dot_numpy(arg_matrix, arg_matrix.T)[0][:5])\n",
"print(f\"Runtime: {time.time() - start_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}