{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mathematical Tools for Neuroscience (NEU 314)

\n",
"#### Princeton University, Fall 2016

\n",
"#### Jonathan Pillow

\n",
"\n",
"---\n",
"## Homework 1: Linear Algebra & Trichromacy\n",
"#### **Due:** Tuesday, October 4, 2016 at 9:59am\n",
"\n",
"---\n",
"### Formatting Instructions\n",
"- Please prepare your homework submission completely within this notebook file (*.ipynb*). For each problem or sub-problem, please **limit yourself to one Code cell and/or one Markdown cell** as appropriate (switch between them by using the menu at the top, or the shortcuts `Esc + M` for Markdown and `Esc + Y` for Code). Of course, feel free to use as many cells as you like in the course of working on the homework, just clean and condense once you're done!\n",
"\n",
"\n",
"- Comments within code cells can be useful for explaining your code and thought process as necessary (anything following a `#` is a comment in Python; you can quickly (un)comment blocks of code by highlighting it and using the shortcut `Cmd + /`. See the posted solution for Homework 0 for examples of the desired formatting).\n",
"\n",
"\n",
"- Finished homeworks should be **submitted via Piazza** as an attachment to a private message to your AI before the due date/time. Late submissions will be penalized 10% per day. (If unable to upload for whatever reason, you may also email your homework to your AI).\n",
"\n",
"\n",
"- Make sure your AI is able to run your code! That means **using Python 3.5 and only core packages** (i.e. ones used in class or otherwise very common). If you need a more obscure package for a problem, it will be explicitly imported for you in the homework. \n",
"\n",
"\n",
"- Make sure to hit `Kernel -> Restart & Run All` before submitting to catch any final errors! The AIs will be restarting your notebooks before grading them, so all results need to be able to be generated from scratch.\n",
"\n",
"\n",
"- All problems or sub-problems are worth 1pt each unless otherwise marked.\n",
"\n",
"---\n",
"### Using Jupyter Notebooks\n",
"To run a cell and advance to the next cell, press `Shift + Return`\n",
"\n",
"To run a cell without advancing to the next cell, press `Control + Return` \n",
"\n",
"You can find a variety of shortcuts at **Keyboard Shortcuts** in the Help menu above\n",
"\n",
"**If you're confused:** Google and Python are the best of friends! Throw a few words describing your problem into Google and click on the first Stack Overflow link — this will solve 95% of your problems!\n",
"\n",
"If you would simply like to know more about a particular function, press `Shift + Tab` while inside the function to bring up a snippet of documentation; press `Tab` again (while still holding `Shift`) to bring up an even larger box of documentation; a third press of `Tab` will turn the bottom half of your screen into a window with the full documentation for your function (including definitions of the function's inputs, outputs, parameters and their default settings, and often some example code!)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"Before we start let's run some magic commands to automatically save our progress once a second (with `%autosave 1`), force all graphics from the `matplotlib` package to be displayed inline (with `%matplotlib notebook`), and then import some of our favorite packages."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%autosave 1\n",
"%matplotlib notebook\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## Linear Algebra\n",
"\n",
"**1) Linear Projection**\n",
"\n",
"*a.* Create two random 10-component vectors $x$ and $u$ using `np.random.randn()`, and make $u$ into\n",
"a unit vector."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*b.* Compute the linear projection of $x$ onto $u$, that is, the component of $x$ that lies in the 1D\n",
"subspace defined by $u$. Call it `x_proj`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*c.* Compute the component of $x$ that is orthogonal to $u$. Call it `x_orth`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*d.* Are `x_proj` and `x_orth` orthogonal? Give evidence for why or why not."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*e.* Show that the original $x$ is equal to the sum of `x_proj` and `x_orth`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2) Linear Independence**\n",
"\n",
"*a.* Are the vectors $\\begin{bmatrix}6 \\\\ -2\\end{bmatrix}$ and $\\begin{bmatrix}-3 \\\\ 1\\end{bmatrix}$ linearly dependent or linearly independent? (Why or why not?)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*b.* What about the vectors $\\begin{bmatrix}6 \\\\ -2 \\\\ 7\\end{bmatrix}$ and $\\begin{bmatrix}-3 \\\\ 1 \\\\ 7\\end{bmatrix}$? Linearly dependent or linearly independent?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*c.* Write a Python function `checkIfIndependent()` that takes a pair of vectors $u$ and $v$ as input and returns 1 if they are linearly independent and returns 0 if they are linearly dependent. Check that it works correctly when called with the vectors given in *(2a)* and *(2b)* above. **(2 points)**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3) Gram-Schmidt orthogonalization (8 points)**\n",
"\n",
"Gram-Schmidt orthogonalization is a classic method for constructing an orthonormal basis from an arbitrary set of $n$ linearly independent vectors. This method proceeds as follows. For the first vector, rescale it to be a unit vector. For each subsequent vector, orthogonalize it with respect to each basis vector already created (i.e., subtract off the component pointing in the direction of each already-created basis vector), and then rescale it to be a unit vector.\n",
"\n",
"Write your own function `gramSchmidt()` to perform Gram-Schmidt orthogonalization. It\n",
"should take an arbitrary $n \\times m$ matrix as input, and return a matrix $M$ whose columns form\n",
"an orthonormal basis for the same subspace spanned by the columns of the matrix passed\n",
"in. If the input vectors are linearly dependent (which will occur if you get a zero vector after\n",
"orthogonalizing a vector with respect to the previous basis vectors), your function should\n",
"output an error (e.g., `Error: Columns are linearly dependent!` ).\n",
"\n",
"Call your function with a random $3 \\times 2$ matrix, a random $3 \\times 3$ matrix, and a random $3 \\times 4$\n",
"matrix. Check that in the first two cases, the columns of the matrix $M$ passed back are unit\n",
"vectors, and are orthogonal to each other.\n",
"\n",
"What do you get if you matrix multiply the transpose of $M$ times $M$?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:tf]",
"language": "python",
"name": "conda-env-tf-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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}