Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesFergusson committed Nov 10, 2020
2 parents 8073608 + ad0c30b commit 63f0a12
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
52 changes: 13 additions & 39 deletions 02_PythonBasics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -402,24 +402,11 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pi is: 3.14\n",
"a, b, c\n",
"The complex number (1+2j) is formed from the real part 1.0 and the imaginary part 2.0.\n",
"Coordinates: 37.24N, -115.81W\n",
"list element 0: 1; list element 3: cat\n",
"1,234,567,890\n"
]
}
],
"outputs": [],
"source": [
"pi = 3.14159265\n",
"list = [1,2,3,'cat',True,3.1415]\n",
Expand Down Expand Up @@ -447,22 +434,9 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pi is: 3.14\n",
"a, b, c\n",
"The complex number (1+2j) is formed from the real part 1.0 and the imaginary part 2.0.\n",
"Coordinates: 37.24N, -115.81W\n",
"list element 0: 1; list element 3: cat\n",
"1,234,567,890\n"
]
}
],
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pi = 3.14159265\n",
"list = [1,2,3,'cat',True,3.1415]\n",
Expand Down Expand Up @@ -1028,7 +1002,7 @@
"for n in range(5):\n",
" print(n) \n",
"\n",
"print('\\nrange2') b\n",
"print('\\nrange2') \n",
"for n in range(5,10):\n",
" print(n) \n",
"\n",
Expand Down Expand Up @@ -1190,7 +1164,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Here `A=[]` first allocates an empty list to `A` <b>when the function is created</b> not when it is run. Now whenever you edit `A` it's storded at the same address so is kept for subsequent calls to the function. So as we have pointed `a` and `c` to the same list editing one edits both.\n",
"Here `A=[]` first allocates an empty list to `A` <b>when the function is created</b> not when it is run. Now whenever you edit `A` it's stored at the same address so is kept for subsequent calls to the function. So as we have pointed `a` and `c` to the same list editing one edits both.\n",
"\n",
"The following would fix this behaviour:"
]
Expand Down Expand Up @@ -1338,11 +1312,11 @@
"metadata": {},
"outputs": [],
"source": [
"list = [-1,-2,-3,-4,-5,2,3,4,5,6,7,8]\n",
"list.sort()\n",
"print(list)\n",
"list = sorted(list,key=lambda x: abs(x))\n",
"print(list)"
"list1 = [-1,-2,-3,-4,-5,2,3,4,5,6,7,8]\n",
"list1.sort()\n",
"print(list1)\n",
"list1 = sorted(list1,key=lambda x: abs(x))\n",
"print(list1)"
]
},
{
Expand Down
55 changes: 50 additions & 5 deletions 04_Advanced_Python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
" return 6.67e-11 * self.mass / dist\n",
"\n",
" \n",
"earth = CelObj(6e24, [0,0,0], [3e4,0,0])\n",
"earth = CelObj1(6e24, [0,0,0], [3e4,0,0])\n",
"print(earth.mass)\n",
"print(earth.position)\n",
"print(earth.velocity)\n",
Expand Down Expand Up @@ -504,7 +504,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"So far so trivial. This isn't very generic as we had to specify the arguments for the function. We could generalise it with:|"
"So far so trivial. This isn't very generic as we had to specify the arguments for the function. We could generalise it with:"
]
},
{
Expand Down Expand Up @@ -691,13 +691,58 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Another useful decorator would be one to time how long specific functions take.\n",
"Another useful decorator would be one to time how long specific functions take. In numpy there are some built in decorators like `np.vectorize` which modify simple functions to take vector input. My solution for notebook 3 used the explicit version of it on my function `intmono`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"...\n",
"\n",
"# Vectorise just converts a scalar function to one that accepts vectors\n",
"# Needed for numpy.fromfunction\n",
"tmp = np.vectorize(intmono)\n",
"\n",
"# Creates an array with entries calculated from the function which takes array indexes as input\n",
"M = np.fromfunction(tmp,(6,6))\n",
"\n",
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"here we could have used `@np.vectorize` on the definition of `intmono` instead."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@np.vectorize\n",
"def intmono(n,m):\n",
" \n",
"...\n",
"\n",
"# Creates an array with entries calculated from the function which takes array indexes as input\n",
"M = np.fromfunction(intmono,(6,6))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise:\n",
"Design a decorator to time how long functions take to run and output it as a string.\n",
"You will need commands from the builtin module `time`\n",
"\n",
"Another is to `cache` results for a function. This just memorises the output for a specific input so if it's called again the function just returns the answer from memory. This can help with performance but is a bit beyond this course, example given in solutions."
"Another is to `cache` results for a function. This just memorises the output for a specific input so if it's called again the function just returns the answer from memory. This can help with performance but is a bit beyond this course, an example given in solutions."
]
},
{
Expand Down Expand Up @@ -735,7 +780,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.7.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 63f0a12

Please sign in to comment.