- \n",
"
- To help you learn some Python essentials and programming skills to enjoy this module.\n", "

- \n",
"
- Complete the notebook below, as follows. Click on a question, then from the menu select Insert -> Insert Cell Below. From the dropdown box (next to the \"stop\" symbol) choose whether you wish to insert 'Code' or 'Markdown'. Save regularly! \n", " \n", "
- Press Shift-Enter to execute the code in a cell.\n", "\n", "
- Submit the .ipynb file via https://somas-uploads.shef.ac.uk/mas212 by 23.59pm on Sun 13th Oct 2019.
*If you are off-campus, you will need to use a VPN to submit.*\n", "\n", " - Your lecturer will mark each question as either 2 (good), 1 (needs revision), 0 (not attempted/wrong).\n", "\n", "
- This is an open-book test, which means you may consult books, notes and internet resources.
**Do not discuss or share your test with anyone**. Copy-and-pasting is**not permitted**.*Please give answers in your own words.*\n", " \n", " - Some of these questions are relatively straightforward, and some are harder (e.g. Q20). Please don't spend any longer than a maximum of 4 hours on this test, as it is for a relatively small amount of credit.\n", "

- \n",
"
- Lecture materials and links on course web page: http://sam-dolan.staff.shef.ac.uk/mas212/\n", "

\n", "

`list`

and `set`

data types.`string`

is similar to a `list`

, and one way in which it is different.\n", "(i) In Python, function arguments are passed

\n", "(ii) Function arguments can be modified within functions.

`for`

loops and lists with the use of arrays and Universal Functions (ufuncs), wherever possible. Universal Functions act on the whole array with a single statement, and they are typically implemented in C (a compiled language). This has the benefit of increasing the speed and efficiency of the code, as well as of reducing the number of lines of code.\n",
"\n",
"Two examples of universal functions are `np.sin()`

and `np.abs`

. The unary operators (+, *, **, etc) are also universal functions.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n", " What is the shape of A*B, if the arrays A and B have shapes (7,3,1) and (3,7,1,4)?

\n", "

\n", "[N.B. The Fibonacci sequence is defined by $f_{k+1} = f_k + f_{k-1}$ with $f_{0} = f_{1} = 1$. The Golden Ratio is $\\frac{1}{2}(\\sqrt{5} + 1)$.]

`expfn(x)`

to calculate $e^x$ by truncating the series expansion \n",
" $$\n",
" e^x = \\sum^{}_{n=0} \\frac{x^n}{n!} \n",
" $$ at $n_{max} = 10$.\n", " Test your function by calculating $e^{i \\pi}$. How accurate is the result?" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The true value of exp(i pi) is -1.\n", "The truncated series gives (-1.0018291040136216+0.006925270707505149j)\n", "The absolute error is (-0.0018291040136215742+0.006925270707505149j)\n" ] } ], "source": [ "def expfn(x):\n", " nmax=10\n", " return sum([x**k/math.factorial(k) for k in range(0, nmax+1)])\n", "y = expfn(1j*math.pi)\n", "print(\"The true value of exp(i pi) is -1.\")\n", "print(\"The truncated series gives \", y)\n", "print(\"The absolute error is \", (y + 1.0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

`\"me.txt\"`

.\n", "

`s`

?`s`

, and how many characters does it have?\n", "

`numpy`

`np.linalg`

, find the eigenvalues of the real symmetric matrix\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 2 & 4 & 1 \\\\ 3 & 1 & -5 \\end{bmatrix} .\n",
"$$\n",
"Verify that the sum of the eigenvalues is zero.\n",
"\n", "

`ciphertext`

using `keytext`

, to recover the melody as a string of letters, A to G.From what piece of music is this melody taken? (