Commit 3ef58023 authored by Éamonn Murray's avatar Éamonn Murray

Initial set of files and examples

parents
*.swp
__pycache__/
tmp
*.py[cod]
Introduction to Scientific Computing
====================================
This is a collection of various notes and examples I've generated over the
past few years, that will hopefully be of use to students as supplementary
material to their courses.
I periodically add corrections and new content and sections. To ensure you
have the latest version:
- Go to <https://gitlab.com/eamonn.murray/IntroToScientificComputing>
- To download everything with git:
`git clone https://gitlab.com/eamonn.murray/IntroToScientificComputing.git`
- If you've cloned the repository previously and want to ensure you have
the most recent updates type `git pull` within the repository directory.
Contents
--------
There are currently sections on the following topics:
- [Computer Systems](systems/README.md)
- [Binary, Bits and Bytes](systems/README.md#binary-bits-and-bytes)
- [Components](systems/README.md#components)
- [High-Performance Computer Systems](systems/README.md#high-performance-computer-systems)
- [Internal Representation of Data](systems/README.md#internal-representation-of-data)
- [Linux - 1. The Command line](linux1/README.md)
- [Using the Terminal](linux1/README.md#using-the-terminal)
- [Tab Completion](linux1/README.md#tab-completion)
- [Permissions](linux1/README.md#permissions)
- [Reading Files](linux1/README.md#reading-files)
- [The Manual and man Command](linux1/README.md#the-manual-and-man-command)
- [Wildcards](linux1/README.md#wildcards)
- [IO Redirection](linux1/README.md#io-redirection)
- [History](linux1/README.md#history)
- [Other Useful Commands](linux1/README.md#other-useful-commands)
- [Linux - 2. More Advanced Use](linux2/README.md)
- [Editing Files](linux2/README.md#editing-files)
- [The Bash Shell](linux2/README.md#bash-shell)
- [Remote Access](linux2/README.md#remote-access)
- [Linux - 3. Useful Tools](linux3/README.md)
- [Version Control with Git](linux3/README.md#version-control-with-git)
- [Plotting](linux3/README.md#plotting)
- [Visualizing Atomic Structures](linux3/README.md#visualizing-atomic-structures)
- [Document Preparation with Latex](linux3/README.md#document-prepartion-with-latex)
- [Python - 1. The Basics](python1/README.md)
- [Installing Python](python1/README.md#installing-python)
- [Using Python](python1/README.md#using-python)
- [Hello World - Output](python1/README.md#hello-world-output)
- [Variables, Strings and IO](python1/README.md#variables-strings-and-IO)
- [Input](python1/README.md#input)
- [Basic Types](python1/README.md#basic-types)
- [Mathematical Operations](python1/README.md#mathematical-operations)
- [Compound Assignments](python1/README.md#compound-assignments)
- [More Advanced Math - Python Packages](python1/README.md#more-advanced-math-python-packages)
- [Branching - if statements](python1/README.md#branching-if-statements)
- [Python Data Structures - Strings and Lists](python1/README.md#python-data-structures-strings-and-lists)
- [Loops - while and for statements](python1/README.md#loops-while-and-for-statements)
- [More Compound Types - sets, tuples and dicts](python1/README.md#more-compound-types-sets-tuples-and-dicts)
- [Functions](python1/README.md#functions)
- [Modules](python1/README.md#modules)
- [Coding Style](python1/README.md#coding-style)
- [Working with Files](python1/README.md#working-with-files)
- [Python - 2. More Advanced Topics](python2/README.md)
- [List Comprehensions](python2/README.md#list-comprehensions)
- [Scope](python2/README.md#scope)
- [Classes](python2/README.md#classes)
- [Using Arguments in your Python Script](python2/README.md#using-arguments-in-your-python-script)
- [Installing Additional Packages](python2/README.md#installing-additional-packages)
- [Virtual Environments](python2/README.md#virtual-environments)
- [Catching Exceptions](python2/README.md#catching-exceptions)
- [Gotchas](python2/README.md#gotchas)
- [Jupyter](jupyter/README.md)
- [Installation](jupyter/README.md#installation)
- [Starting Jupyter](jupyter/README.md#starting-jupyter)
- [Shortcuts and Cell Types](jupyter/README.md#shortcuts-and-cell-types)
- [Full List of Shortcuts](jupyter/README.md#full-list-of-shortcuts)
- [Numpy](numpy/README.md)
- [Installation](numpy/README.md#installation)
- [Basic Usage](numpy/README.md#basic-usage)
- [A Simple Example](numpy/README.md#a-simple-example)
- [Comparison with Built-In Python](numpy/README.md#comparison-with-built-in-python)
- [Useful Numpy Operations](numpy/README.md#useful-numpy-operations)
- [Matplotlib](matplotlib/README.md)
In addition to the material collected here, there are also separate
repositories with material for:
- [Intro to C++](https://gitlab.com/eamonn.murray/IntroToCplusplus). This also
includes a brief section on Makefiles in the first section.
- [Intro to Mathematica](https://gitlab.com/eamonn.murray/IntroToMathematica)
- The content that was previously in [Intro to Linux](https://gitlab.com/eamonn.murray/IntroToLinux)
has been incorporated into this repository.
Additional Resources
--------------------
### Linux
- There are various useful
[guides at the Linux Documentation Project](http://www.tldp.org/guides.html),
in particular:
- [Introduction to Linux](http://www.tldp.org/LDP/intro-linux/html/index.html)
- [Bash Guide for Beginners](http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html)
### HPC and Scientific Computing
- Once you want to start thinking about more advanced scientific computing,
Victor Eijkhout's free textbook
[Introduction to High-Performance Scientific Computing](http://pages.tacc.utexas.edu/~eijkhout/istc/istc.html)
is an excellent resource.
### Python
- There are many excellent free resources available online, particularly at the
official python website:
- The python wiki: <https://wiki.python.org>. This links to beginner
guides and has other useful information such as recommended books.
- There is also a tutorial at
<https://docs.python.org/3/tutorial/index.html>.
- The official python documentation website:
<https://www.python.org/doc/>. This is particularly useful for finding
how particular functionality should work in the version of python you
have. Throughout the course, when a function is mentioned for the first
time, it will be linked to its official documentation page. Many
functions have options beyond what we will have time to cover so it's
worth checking these to get a better idea of what you can do.
- There are also many good books out there, in particular:
- Al Sweigart, _Automate the Boring Stuff with Python_, No Starch Press
(2015). This is a great introduction to Python language with a focus on
learning how to put python to work doing useful things for you. The
material in the book is free to read online at
<https://automatetheboringstuff.com/>.
- Christian Hill, _Learning Scientific Programming with Python_, Cambridge
University Press (2016). This is an excellent companion to the course
and will give you an alternative view on many of the topics covered
here.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Example Jupyter Notebook\n",
"\n",
"This is a simple example to show you what you can do.\n",
"This is a markdown cell. You can use it to generate a formatted description of what you're doing.\n",
"\n",
"It can parse LaTex style mathematical expressions:$$ \\Sigma_{i=0}^{10} x^2 = 285 $$\n",
"\n",
"If you enclose the expression in `$$` symbols it will be displayed on it's own line. And you can use single `$` symbols for inline equations: $\\alpha + \\beta = \\gamma$.\n",
"\n",
"There are more markdown examples at http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Working%20With%20Markdown%20Cells.html\n",
"\n",
"You can run a selected cell `ctrl+enter`, or run it and go to the next cell with `shift+enter`.\n",
"\n",
"Running a markdown cell, will render the markdown and any equations. You can double click the cell or press enter with the cell selected to go back to the source.\n",
"\n",
"Running a code cell will execute whatever code is in it. This works in the same way as an ipython notebook or using python interactively. You don't need to explicitly print output - the output of the last command in the cell will automatically be displayed. And variables or functions defined in any cell that us run will be available for use in any cell run subsequently."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# This will calculate the sum in the expression above using a list comprehension.\n",
"sum([x**2 for x in range(10)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = 100"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(a)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "raw",
"metadata": {
"collapsed": true
},
"source": [
"# Example Jupyter Notebook\n",
"\n",
"This is a simple example to show you what you can do.\n",
"This is a markdown cell. You can use it to generate a formatted description of what you're doing.\n",
"\n",
"It can parse LaTex style mathematical expressions:$$ \\Sigma_{i=0}^{10} x^2 = 285 $$\n",
"\n",
"If you enclose the expression in `$$` symbols it will be displayed on it's own line. And you can use single `$` symbols for inline equations: $\\alpha + \\beta = \\gamma$.\n",
"\n",
"There are more markdown examples at http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Working%20With%20Markdown%20Cells.html\n",
"\n",
"You can run a selected cell `ctrl+enter`, or run it and go to the next cell with `shift+enter`.\n",
"\n",
"Running a markdown cell, will render the markdown and any equations. You can double click the cell or press enter with the cell selected to go back to the source.\n",
"\n",
"Running a code cell will execute whatever code is in it. This works in the same way as an ipython notebook or using python interactively. You don't need to explicitly print output - the output of the last command in the cell will automatically be displayed. And variables or functions defined in any cell that us run will be available for use in any cell run subsequently."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This will calculate the sum in the expression above using a list comprehension.\n",
"sum([x**2 for x in range(10)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = 100"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(a)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Jupyter
=======
[Full topic list](../README.md)
`jupyter` offers a graphical notebook style interface to `ipython` (a command
shell for python) functioning in a similar way to Mathematica. This offers
many more features than simply using python interactively in a terminal.
Installation
------------
You can install `jupyter` using the python package manager `pip`. If you're
on an Ubuntu or Debian system you can do this system-wide for python3 with
```bash
sudo pip3 install --upgrade pip
sudo pip3 install jupyter
```
If you're using the TSM Linux VM I suggest you do this now.
Starting Jupyter
----------------
You can start `jupyter` by typing ``jupyter-notebook`` in a terminal.
Alternatively, depending on how you installed it, you may need to type
``jupyter notebook``. This will start a jupyter server in the terminal, and
open the server interface as a web page on your default browser that will
allow you to interact with it. An example of what you will see in your browser
when you first start jupyter in an empty folder is shown below: ![jupyter
browser interface](jupyter1.png).
You can do several things from this web interface. If you click the "new"
dropdown menu, you will see options to create a new text file or folder, or to
start a terminal, which gives you a terminal in your browser, followed by a
list of notebooks which can be started, which will likely just list "Python 3"
by default. Click this now to start a new python 3 notebook. The notebook
interface will be started in a new browser tab. How this looks is shown below:
![jupyter notebook](jupyter2.png).
The notebook interface offers many nice features. For example, it
automatically saves a checkpoints your work, will automatically close
brackets, and it can display graphics directly in the notebook. The input and
output is organised around cells, where you can enter commands in a similar
way as with an interactive python session, but you can edit and rearrange
cells easily. To execute the commands contained in a cell, press
``shift+enter`` as the enter key on its own will give you a new line in the
current cell allowing you to enter several lines of code to be executed
together. There are also menu buttons for many operations, such as executing
the current cell. You can hover over them to see what function they perform.
Each notebook you open from the server interface will have its own kernel that
runs in the server. This means that any function or variable definitions or
imports are local to the notebook, and will be available as long as the kernel
is running. When a notebook is saved, you can close the browser tab where it
is running and reopen it again to carry on from where you were. There is a
separate option to stop the kernel for a running notebook from the server
interface page. And if you close the browser interface page, the server will
still be running, so you can usually get back to it by visiting
[http://localhost:8888](http://localhost:8888). To shut down the server, go to
the terminal where you started it, and press ``ctrl+c``.
Shortcuts and Cell Types
------------------------
`jupyter` has many shortcuts for e.g. switching between input cell types, or
selecting, running, merging or deleting cells. You can bring up a list of
these by first pressing `Escape` to ensure you're in command mode (the cursor
will not be active in any cell), and then pressing `h`. It will take a while
to get to know them all, so I suggest trying to remember a few at a time as
you go. You can perform most operations using the mouse and menus so the
shortcuts are not mandatory but will make you more efficient.
There are three types of cell you can switch between:
1. Code (`Y`) - for entering sections of code of whatever language you've
chosen for the notebook. We've installed `jupyter` for python3 but many
other languages are possible.
2. Markdown (`M`) - for entering sections of text, with desired formatting
indicated by some simple rules. LaTex style mathematical expressions are
also supported.
3. Raw (`R`) - raw text with any highlighting or formatting disabled.
There is an example notebook given in the [`Example`](Example) directory that
contains a markdown cell and a few code cells. Launch `jupyter` and open this
to take a look. Try entering some code and markdown cells yourself.
Full List of Shortcuts
----------------------
Note shortcuts are not case sensitive.
- Command Mode (`Esc`)
- `F`: find and replace
- `Ctrl+Shift+F`: open the command palette
- `Ctrl+Shift+P` : open the command palette
- `Enter` : enter edit mode
- `P` : open the command palette
- `Shift+Enter` : run cell, select below
- `Ctrl+Enter` : run selected cells
- `Alt+Enter` : run cell, insert below
- `Y` : to code
- `M` : to markdown
- `R` : to raw
- `1` : to heading 1
- `2` : to heading 2
- `3` : to heading 3
- `4` : to heading 4
- `5` : to heading 5
- `6` : to heading 6
- `K` : select cell above
- `Up` : select cell above
- `Down` : select cell below
- `J` : select cell below
- `Shift+K` : extend selected cells above
- `Shift+Up` : extend selected cells above
- `Shift+Down` : extend selected cells below
- `Shift+J` : extend selected cells below
- `A` : insert cell above
- `B` : insert cell below
- `X` : cut selected cells
- `C` : copy selected cells
- `Shift-V` : paste cells above
- `V` : paste cells below
- `Z` : undo cell deletion
- `D,D` : delete selected cells
- `Shift+M` : merge selected cells, or current cell with cell below if only one cell selected
- `Ctrl+S` : Save and Checkpoint
- `S` : Save and Checkpoint
- `L` : toggle line numbers
- `O` : toggle output of selected cells
- `Shift+O` : toggle output scrolling of selected cells
- `H` : show keyboard shortcuts
- `I`,`I` : interrupt kernel
- `0`,`0` : restart the kernel (with dialogue)
- `Ctrl+V` : Dialogue for paste from system clipboard
- `Esc` : close the pager
- `Q` : close the pager
- `Shift+L` : toggles line numbers in all cells, and persist the setting
- `Shift+Space` : scroll notebook up
- `Space` : scroll notebook down
- Edit Mode (`Enter`)
- `Tab` : code completion or indent
- `Shift+Tab` : tooltip
- `Ctrl+]` : indent
- `Ctrl+[` : dedent
- `Ctrl+A` : select all
- `Ctrl+Z` : undo
- `Ctrl+Shift+Z` : redo
- `Ctrl+Y` : redo
- `Ctrl+Home` : go to cell start
- `Ctrl+Up` : go to cell start
- `Ctrl+End` : go to cell end
- `Ctrl+Down` : go to cell end
- `Ctrl+Left` : go one word left
- `Ctrl+Right` : go one word right
- `Ctrl+Backspace` : delete word before
- `Ctrl+Delete` : delete word after
- `Ctrl+M` : command mode
- `Ctrl+Shift-F` : open the command palette
- `Ctrl+Shift-P` : open the command palette
- `Esc` : command mode
- `Shift+Enter` : run cell, select below
- `Ctrl+Enter` : run selected cells
- `Alt+Enter` : run cell, insert below
- `Ctrl+Shift+Minus` : split cell
- `Ctrl+S` : Save and Checkpoint
- `Down` : move cursor down
- `Up` : move cursor up
This diff is collapsed.
This diff is collapsed.
\documentclass{article}
% Comment lines start with %.
% Latex commands start with a \.
% The first line in a latex document is always \documentclass, with the
% chosen class enclosed in braces. Many options are possible here, such
% as article, report, or book, and each will format the content in different
% ways.
\begin{document}
% The main content of a latex document is enclosed between a \begin{document}
% and a \end{document} command.
Paragraphs are indicated by separating them with an empty line in the source
file.
This paragraph includes some simple math: $f(x)=\frac{1}{2}x^3$.
% Inline math is enclosed betweeen two $ symbols.
\end{document}
\documentclass[12pt,a4paper]{article}
% Additional class options can be given in brackets. Here we set the
% main font size, and the page size.
% This time we include some title information.
\title{A \LaTeX{} example}
\author{\'Eamonn Murray}
\date{\today}
\begin{document}
% The maketitle command will generate a title from the information defined
% in the header above.
\maketitle
% We can use the section command to divide the document into sections.
% subsection, and subsubsection can also be used to further subdivide
% sections.
\section{Introduction}
Equation \ref{geometric_sum} below shows another way to include mathematical
expressions. It is given a label so it can be referred to in this paragraph.
\begin{equation}\label{geometric_sum}
\sum_{i=0}^{\infty} z^k = \frac{1 - z^{n+1}}{1 - z}
\end{equation}
\section{Conclusion}
Conclusions go here.
\end{document}
\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
% We need to include the graphicx package which tells latex how to handle
% graphical files.
\title{A \LaTeX{} example including an image}
\author{\'Eamonn Murray}
\date{\today}
\begin{document}
\maketitle
Here we include the plot generated by gnuplot previously, where we have
fit the function $a + b x + c x^2$ to some example data.
\includegraphics{example-gp.pdf}
Note that what image formats can be used depends on what command you use
to compile the document. When using pdflatex, pdf, png and jpg files can
all be included.
\end{document}
This diff is collapsed.
#!/bin/bash
# Generate an ordered list of words according to frequency in a text file.
tr -d [:punct:] < $1 | tr [:space:] "\n" | sort | uniq -c | sort -n
1.14000 -1.0222391714
1.18000 -1.0322870955
1.22000 -1.0405831395
1.26000 -1.0473470089
1.30000 -1.0527710468
1.34000 -1.0570244671
1.38000 -1.0602563279
1.42000 -1.0625980369
1.46000 -1.0641619648
1.50000 -1.0650570018
1.54000 -1.0653744400
1.58000 -1.0651854957
1.62000 -1.0645640041
1.66000 -1.0635714437
1.70000 -1.0622617837
1.74000 -1.0606823102
1.78000 -1.0588743592
1.82000 -1.0568739730
1.86000 -1.0547124824
1.90000 -1.0524170323
1.94000 -1.0500110419
1.98000 -1.0475146408
2.02000 -1.0449450350
f(x)=a+b*x+c*x**2
a=-1;b=-1;c=0.5;
fit f(x) "example.dat" via a,b,c
set title "Example Gnuplot Plot"
set xlabel "Position (Bohr)"
set ylabel "Energy (Hartree)"
set term pdf
set output "example-gp.pdf"
plot "example.dat" with lines title "Results", f(x) title "Quadratic fit"
Si
5.431
0.00 0.50 0.50
0.50 0.00 0.50
0.50 0.50 0.00
2
Direct
0.125 0.125 0.125
0.875 0.875 0.875
3
H2O molecule. Distances in Angstrom.
O 0.00000 0.00000 0.00000
H 0.75754 0.58708 0.00000
H -0.75754 0.58708 0.00000
Matplotlib
==========
[Full topic list](../README.md)
[Matplotlib](https://matplotlib.org/) is a plotting library for python that
you can use to have your scripts and notebooks automatically generate plots.
It can produce a very wide variety of plots and graphics in different styles
and can be interacted with in many different ways.
There is a page full of tutorials on the official site at
<https://matplotlib.org/users/tutorials.html>.
Installation
------------
If you're using the virtual machine provided as part of the TSM CDT this
will already have been installed for you. Otherwise there are several
different ways to do this. The easiest would be one of the following:
- matplotlib will be available through the package manager on the vast majority of
Linux installations. E.g. on Ubuntu you could do `sudo apt install
python3-matplotlib`.
- You can install it using the python package manager `pip`. E.g. to install
the version for python3 system wide where you have sudo privileges you could
do `sudo pip3 install matplotlib`.
- If you're using a
[virtual environment](../python2/README.md#virtual-environments), and have
it activated, you can install it with simply `pip install matplotlib`.
A First Example with Pyplot
---------------------------
The easiest way to get started with matplotlib is with the pyplot submodule.
This works in a similar way as plotting in Matlab. There's a full tutorial on
using this online at <https://matplotlib.org/users/pyplot_tutorial.html>.
An example showing how to use this to generate a basic plot of sin(x) is given
in `sinxplot.py`:
```python
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
# We set up a numpy array covering the range we want to plot.
xvals = np.linspace(-5, 5, 500)
# Now we use these x-values to generate a series of coordinates to plot with
# whatever function we like. In this case we're using the numpy sin function.
# The plot() function is for plotting lines and points in 2D.
plt.plot(xvals, np.sin(xvals))
# We can add axes labels and a plot title.
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('Matplotlib Test Plot')
# And finally we display our plot.
plt.show()
```
If you run this script you'll see a window appear with your plot. You can
interact with it in various ways via the menu buttons, such as panning and
zooming the plot. There is also a save button which allows you to save the
image to various formats.
A More Complex Example
----------------------
You can combine several plots together and control the appearance as shown
in `multiplot.py`:
```python
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
# This time will make a figure with two subplots arranged vertically.
# First we initialize our figure
plt.figure()
# Again We set up numpy arrays covering the range we want to plot.
xvals1 = np.linspace(-5, 5, 500)
xvals2 = np.linspace(-5, 5, 20)
# This creates a subplot on a notional grid with 2 rows and 1 column and
# selects the 1st plot (top row).
plt.subplot(211)
# Plot both sin(x) with a blue solid line and cos(x) with red dashed line. We
# can do this with a single plot() command.
plt.plot(xvals1, np.sin(xvals1), 'b-', xvals1, np.cos(xvals1), 'r--')
# And add a legend
plt.legend(["sin", "cos"])
# Select the lower plot
plt.subplot(212)
<