diff --git a/Chapter One.ipynb b/Chapter One.ipynb new file mode 100644 index 0000000..c153829 --- /dev/null +++ b/Chapter One.ipynb @@ -0,0 +1,313 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Matrix Multiplication" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Variables for tests" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Constants for tests" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "c_x = np.random.randn(1, 1)\n", + "c_y = np.random.randn(1, 1)\n", + "c_z = np.random.randn(1, 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Vectors for test" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "v_x = np.random.randn(1000, 1)\n", + "v_y = np.random.randn(1000, 1)\n", + "v_z = np.random.randn(1000, 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Matrices for test" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "m_x = np.random.randn(1000, 1000)\n", + "m_y = np.random.randn(1000, 1000)\n", + "m_z = np.random.randn(1000, 1000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm Implementation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Algorithm 1.1.1: Dot Product" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def dot_product(x, y):\n", + " c = 0\n", + " \n", + " for i in np.arange(x.size):\n", + " c = c + x[i]*y[i]\n", + " \n", + " return c" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Algorithm 1.1.2: Saxpy" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def saxpy(a, x, y):\n", + " for i in np.arange(x.size):\n", + " y[i] = y[i] + a*x[i]\n", + " \n", + " return y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Testing algorithms 1.1.1 and 1.1.2" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", + "Wall time: 1.49 ms\n" + ] + } + ], + "source": [ + "# Test Algorithm 1.1.1 (Dot Product)\n", + "%time temp = dot_product(v_x, v_y)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 4 ms, sys: 0 ns, total: 4 ms\n", + "Wall time: 3.92 ms\n" + ] + } + ], + "source": [ + "# Test Algorithm 1.1.2 (Saxpy)\n", + "%time temp = saxpy(c_x, v_x, v_y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Matrix-Vector Multiplication and the Gaxpy" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Algorithm 1.1.3: Row-Oriented Gaxpy" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "def row_gaxpy(A, x, y):\n", + " for i in np.arange(y.size):\n", + " for j in np.arange(x.size):\n", + " y[i] = y[i] + A[i, j]*x[j]\n", + " \n", + " return y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Algorithm 1.1.4: Column-Oriented Gaxpy" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "def column_gaxpy(A, x, y):\n", + " for j in np.arange(x.size):\n", + " for i in np.arange(y.size):\n", + " y[i] = y[i] + A[i, j]*x[j]\n", + " \n", + " return y" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "#### Testing algorithms 1.1.3 and 1.1.4" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 1.89 s, sys: 0 ns, total: 1.89 s\n", + "Wall time: 1.89 s\n" + ] + } + ], + "source": [ + "# Test Row-Oriented Gaxpy\n", + "%time temp = row_gaxpy(m_x, v_x, v_y)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 1.96 s, sys: 24 ms, total: 1.98 s\n", + "Wall time: 1.95 s\n" + ] + } + ], + "source": [ + "# Test Column-Oriented Gaxpy\n", + "%time temp = column_gaxpy(m_x, v_x, v_y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [default]", + "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.5.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Chapter Twelve.ipynb b/Chapter Twelve.ipynb new file mode 100644 index 0000000..9bf7645 --- /dev/null +++ b/Chapter Twelve.ipynb @@ -0,0 +1,101 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tensors for test" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Third order" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "t3_x = np.random.randn(10, 10, 10)\n", + "t3_y = np.random.randn(10, 10, 10)\n", + "t3_z = np.random.randn(10, 10, 10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Fourth order" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "t4_x = np.random.randn(10, 10, 10, 10)\n", + "t4_y = np.random.randn(10, 10, 10, 10)\n", + "t4_z = np.random.randn(10, 10, 10, 10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Fifth order" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "t5_x = np.random.randn(10, 10, 10, 10, 10)\n", + "t5_y = np.random.randn(10, 10, 10, 10, 10)\n", + "t5_z = np.random.randn(10, 10, 10, 10, 10)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [default]", + "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.5.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}