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

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

\n",
"#### Jonathan Pillow

\n",
"\n",
"---\n",
"## Homework 0: Python Warm-up\n",
"#### Due: it’s not due\n",
"\n",
"Here are some warm-up problems to help bring you up to speed on using Python. Use the Notebooks from lecture (available online) to help if you get stuck!\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",
"## Variable assignment and indexing\n",
"\n",
"**1)**\n",
"Create the following matrix using NumPy and assign it to the variable $A$:\n",
"\n",
"$$A = \\begin{bmatrix}1 & 3 \\\\ 5 & 7 \\\\ 9 & 11 \\end{bmatrix}$$\n",
"\n",
"Access the element $i = 1$, $j = 0$ (that is, the element in the second row and first column currently set to 5 — remember, Python has zero indexing so $i=1$ means the *second* row!), and change it to 33.\n",
"\n",
"Since you'll need to use the NumPy package, we've imported it for you below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2)**\n",
"Create a new matrix $B$ that is the transpose of $A$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3)**\n",
"Create $C$ that has two copies of $A$ stacked on top of each other.\n",
"\n",
"(*Hint:* the NumPy function `np.vstack()` may be useful!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**4)**\n",
"Extract the second column of $C$ and assign it to a variable $x$.\n",
"\n",
"*Note:* Is $x$ still a 2 dimensional vector, i.e. a column vector? If not, can you create a new variable $x_{\\text{column}}$ where the values of $x$ are stored as a column vector? (the function `np.reshape()` may be useful!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**5)**\n",
"Make a vector $y$ that has the squared elements of $x$ in it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**6)**\n",
"Create $D$ that has two copies of $A$ concatenated side-by-side.\n",
"\n",
"(*Hint:* the NumPy function `np.hstack()` may be useful!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**7)**\n",
"Create $z$ that is the matrix multiplication of $C^T$ times $y$.\n",
"\n",
"(*Hint:* Remember you can use the ampersand symbol @ to matrix multiply two NumPy arrays)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**8)**\n",
"Create a new variable named $A$ that contains the integers 0 through 11 arranged in a $3 \\times 4$ matrix (arranged with integers 0 through 3 in the first column, 4 to 7 in the second column, and 8 to 11 in the last).\n",
"\n",
"*a)* First, do this the “dumb” way by setting the integers in nested NumPy array manually (e.g. `np.array([[1,2],[3,4]])` )\n",
"\n",
"*b)* Then, do it the “smart” way using the Numpy functions `np.arange()` and `np.reshape()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**9)** Create a time vector $t$ that goes from 0 to 100 in increments of 5 using `np.arange()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**10)** Create a vector $q$ whose elements are equal to the corresponding elements of $t$ raised\n",
"to the power of 2, then multiplied by 3."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**11)**\n",
"Create a $5 \\times 5$ matrix of all-1’s using `np.ones()`, a $10 \\times 5$ matrix of all-0’s using `np.zeros()`, and a $10 \\times 10$ identity matrix using `np.eye()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## Plotting\n",
"\n",
"The dominant way to plot in Python is with the package *matplotlib*, which we import for you below.\n",
"\n",
"There are also a variety of ways Python can render images. One of the main advantages of the Notebooks is the ability to render images *inline*, that is, within the notebook itself rather than in an additional pop-up window. We activate that inline functionality here by calling the notebook *magic* function `%matplotlib notebook` for you below.\n",
"\n",
"---\n",
"**12)**\n",
"Using the matplotlib function `plt.plot()`, plot a sine wave over the interval $[0, 4\\pi]$. First, make a vector $t$ that goes from 0 to $4\\pi$ in 200 even increments (try using `np.linspace()` here). Then make a plot of $\\sin(t)$ vs. $t$. Label your $x$ and $y$-axes using `plt.xlabel()` and `plt.ylabel()`, and give your plot a title using title."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"from matplotlib import pyplot as plt\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**13)**\n",
"Plot the vectors (3, 1) and (2, 5), and (−2, 3) as line segments extending from the origin to the vector endpoint, all on the same set of axes.\n",
"\n",
"(You can try using `plt.arrow()` if you want to get fancy and add arrow heads!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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
}