{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This homework covers the material in Lessons 3 & 4. It is due **Thurday, June 15**. Please submit your **answers as a PDF**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1\\. Modify the LU method (without pivoting) to work **in-place**. That is, it should not allocate any new memory for L or U, but instead overwrite A." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def LU(A):\n", " U = np.copy(A)\n", " m, n = A.shape\n", " L = np.eye(n)\n", " for k in range(n-1):\n", " for j in range(k+1,n):\n", " L[j,k] = U[j,k]/U[k,k]\n", " U[j,k:n] -= L[j,k] * U[k,k:n]\n", " return L, U" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2\\. Modify our LU method from class to add pivoting, as described in the lesson. *Hint: the swap method below will be useful* " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def swap(a,b):\n", " temp = np.copy(a)\n", " a[:] = b\n", " b[:] = temp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3\\. For each of the following sets of dimensions, either\n", " - give the dimensions of the output of an operation on A and B, **or** \n", " - answer *incompatible* if the dimensions are incompatible according to the [numpy rules of broadcasting](https://docs.scipy.org/doc/numpy-1.10.0/user/basics.broadcasting.html)." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " a. A (2d array): 3 x 3\n", " B (1d array): 1\n", "\n", " b. A (2d array): 2 x 1\n", " B (3d array): 6 x 4 x 2\n", "\n", " c. A (2d array): 5 x 4\n", " B (1d array): 4\n", "\n", " d. A (3d array): 32 x 64 x 8\n", " B (3d array): 32 x 1 x 8\n", "\n", " e. A (3d array): 64 x 1\n", " B (3d array): 32 x 1 x 16\n", "\n", " f. A (3d array): 32 x 64 x 2\n", " B (3d array): 32 x 1 x 8" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "4\\. Write how this matrix would be stored in compressed row format:\n", "\n", "\\begin{pmatrix}\n", " 1 & & & & -2 & -3 \\\\\n", " & 3 & & & & -9 \\\\\n", " & & & -7 & 4 & \\\\ \n", " -1 & 2 & & 7 & & \\\\\n", " -3 & & & 26 & &\n", " \\end{pmatrix}" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }