Code written by Iain
Code associated with my papers might be found on my publications page.
My personal Github profile has a few things on it.
See also my group’s Github profile.
Matlab / Octave utilities
Some Matlab/Octave utility functions: imurray-matlab-1.037.tar.gz, browse, Contents.m. They’re simple functions that at some point I thought were general enough to warrant copying out of whatever project directory I was working in at the time.
I also have a collection of potentially-missing or replacement functions for people without certain toolboxes, or (older versions of) Octave.
If you are interested in neat Matlab utility functions, I recommend Tom Minka’s lightspeed Matlab toolbox. From V2.8 it changed back to a permissive (MIT) license.
Fast numerics
Updated June 2012: I have written mex files to do sum(log(X)) fast and accurately in Matlab. With a bit of tweaking, the C code should work elsewhere too. It works by bit-twiddling the IEEE floating point representation, but these days that is pretty portable. The code is reasonably careful to deal with infinities and NaNs correctly. This should be usable as a general, drop-in replacement for sum(log(X)).
Approximate exp for Matlab/Octave, two different versions with different time/accuracy/memory tradeoffs. They are written in C and could easily be adapted for use in any environment. They are less recommended than the sum(log(X)) code because the approximations make these routines less safe for general use. Updated March 2011: fixed a problem with compiler options that could cause crashes; added an OpenMP version that allows you to use multiple CPUs; added explicit Octave support.
Finally, although not recommended, “Softmax”: C/C++ code to evaluate log(exp(a)+exp(b)) or log(1+exp(-x)) (for x>0) approximately, but more quickly than using standard routines. Uses a bad implementation of a Taylor expansion. Possibly useful somewhere, but I never use this now.
Swendsen–Wang
Swendsen–Wang is a sampling algorithm which is amazing on some Ising model distributions. When I was first working out the details of Swensen–Wang sampling I put a hacked together implementation on the web. I’ve since documented and put up a faster and more general Matlab/mex implementation. The README gives lots more information. You can download an archive containing all of the above: swendsen_wang.tar.gz.
Unsorted / Misc
The following miscellaneous blobs of code are not packaged up very nicely. There’s a small chance something will be useful.
- Product of unigauss experts fitted with Contrastive Divergence. This demo reproduces Figure 2 from Geoff Hinton’s 2002 Neural Computation paper (Figure 1 in the tech report version).
- Derivative with respect to the log of a matrix, from derivatives with respect to the matrix itself. Possibly useful if you want to convert a constrained algorithm into an unconstrained one. Matlab/octave code available for arbitrary and symmetric matrices.
- I’ve written Octave and Matlab wrappers to David MacKay’s macopt, a gradient-only optimiser. The wrappers are on the main site, but will probably require updating to work with recent Octave or Matlab.
- I’ve played a small part in the Dasher project.
- I have Matlab/Octave code (circa 2003) for ML learning for a mixture of multivariate Bernoulli variables using EM. I can’t put it online as it would help certain cheating students too much, but I’m prepared to share it.
- Slice sampling: a simple Matlab/Octave implementation is on this teaching page. You could also look at elliptical slice sampling.
- So you read “numerical recipes” but ignored it and do simple gradient descent in your Matlab/Octave code anyway? “Bold driver” is a reasonable heuristic for picking epsilon that works surprisingly well. On a Gaussian process learning problem I tried it was >10x slower than Carl’s minimize (now part of GPML), so use that or macopt (see above).
- Smallest sphere that encloses a bunch of points. This seems to be a standard exercise: can you solve this with a quadratic program? The answer is in the code, but little explanation of where it came from, so I doubt I’m helping students much. See Convex Optimization, Stephen Boyd and Lieven Vandenberghe.
Miscellaneous scripting hacks:
Before running any of these take a quick look at the source and see if it is what you want. I take no responsibility for any damage, some of these (eg rmx) are potentially very dangerous!
- Mastodon bookmarklets.
- a2ps_utf8 — poor man’s u2ps/gnome-u2ps, makes a2ps work with UTF-8 text files, as long as only symbols from the windows-1250 codepage are used. File to print must be last argument.
- target — file at end of a chain of symlinks. Used by whichreally.
- Pretty print the size of a file at a URL (see docstring for example).
- make_handout produces 3x3 tiles on
A4 paper
from a PDF of input slides using options I like. To tweak the options, replace
a4paper
withletter
in the source and/or change the\includepdf
line after consulting the pdfpages manual. - Three files from my
~/bin
directory that allow me to run the PDF presentation displayer impressive (formerly keyjnote) with the options I like, a big red hand pointer, and that add a feature allowing me to jump to slides by number. - pdfcolorsplit — a Python program that drives pdftk to split a PDF file into color and b/w parts. Useful for printing large documents with a few color pages when color printing is expensive. The script also requires one of Ghostscript (gs) or pdftoppm (which comes with xpdf). This program was inspired by Jeremy Sander’s dvicoloursplit.py. I also found pdfcolour, which didn’t work for me...but I didn’t try very hard.
- GPU monitoring — scripts to help share NVDIA GPU boards on communal machines.
- google_viewer.user.js a user script that makes links to pdfs on all websites appear like this — the original link to the PDF is left unchanged, the added icon allows you to view the document in Google’s online viewer. You can just click the script to install in Google Chrome or in Firefox with Greasemonkey. There are also Greasemonkey equivalents for other browsers.
- More user scripts.
- utf8_to_ascii.c, utf8_to_ascii.pl or utf8_to_ascii.py: simple programs that take a UTF-8 input stream and spits out an ASCII version that will be equivalent as part of an HTML or XML document. The ASCII version will be less efficient, but might be easier or safer to deal with. You can convert back with ascii_to_utf8.py or ascii_to_utf8.pl. For a more powerful solution try the command-line tools that come with uni2ascii.
- eps2fixedbb — add a tight bounding box to eps files (uses gs).
- fai — concise shell loops.
- latex2png — poor man’s tex2im or dvi2bitmap.
- mutt2gmailcsv — mutt mail alias exporter for gmail.
- mycal — wrapper around the BSD cal command so it’s more friendly.
- ncat — cat multiple files with headers giving their names.
- rmx — delete everything but specified files.
- snaffle webpage hierarchies. This is just a wrapper to wget using the options I like to take a mirror of a sub-branch of a webserver.
- sort_executables — recursively reset executable bit of file permissions to a sensible guess. Useful if you have copied files from a FAT file system.
- sortr — randomly permute input lines. Like "sort -R", but without necessarily putting identical lines together.
- spl (Screen PreLoad) — Used to keep pre-loaded instances of slow-starting terminal applications in screen, so that they're instantly available when needed. Commands can automatically be sent at time of attachment, for example to tell the application the current working directory.
- untar — this is a wrapper around utilities for unpacking archives in many formats. It tries to unpack into an intelligent place, not splurging many files into the current directory and not clobbering existing files without your permission. See comments inside the script for more detail. This script has not been that extensively tested. As with everything here, use at your own risk and inspect the source yourself. If you were used to it, you can get the old version, which behaved a bit differently.
- renameall.sh is called by the following to recursively rename all files and directories below the current level: all2lower.sh, all2upper.sh, spaces2hyphens.sh, spaces2underscores.sh. (Nasty code. If/when I rewrite these I’ll probably use os.walk in Python.)
- tex.exp — expect wrapper to (pdf)(la)tex,
meta(post|font), etc. See the source for how to set it up. Makes the programs
stop and quit on first error, and stops “clever” Ctrl-C handing.
None of the normal interaction modes seem to do what I want. Apparently recent
tetex supports a
-halt-on-error
option, the version I have installed does not, which used to lead to much cursing, keyboard bashing and deleting files called.log
. (I discovered later that maybe I should have trained myself to hit Ctrl-D for end-of-input instead of Ctrl-C). - A bibtex wrapper.
- docmatlab — takes an Octave/Matlab function definition line from standard input and turns it into a skeleton for comments (example). I call this from my editor. I find having an easy-to-view list of the sizes of everything greatly helps both while writing code and while working out how to call it months later. (Tweaked Jan. 2009 to match Matlab’s documentation style better).
- lsd — drives
ls(1)
but ignores files, showing only directories. - intersect — print out lines that appear in both of two files.
- difference — print out lines that appear in a first file, but not a second.
- scp_sudo — use like scp, but uses sudo to get root permissions at remote end.
Obsolete
- Mozex for Firefox 1.5 with UTF-8, for editing web forms with your favourite text editor. None of this was written by me, I just combined two branches of the code. I then swapped to It’s All Text! (no longer available), which stopped working after Firefox swapped to Web Extensions.
- xmms — a wrapper to audacious media player that works around bugs so that I can enqueue or play files from the command-line like I used to with xmms. The bugs have now been fixed in the version of audacious2 that ships with Ubuntu Karmic.
- ps2djvu — wrapper around DjVu libre utilities. There was an online service "any2djvu" that did a better job. Now djvudigital has been released, you can run it on your own machine, although due to annoying licensing issues you have to compile the necessary ghostscript driver yourself. Another option that is better than my old shell script is pdf2djvu.
Bad postscript hacks:
- flip.pl — mirroring for transparencies
- flip2.pl — another attempt
- psinvert — invert black and white colors (sometimes)
Quines
Last and definitely least in usefulness, some old quine attempts:
- I wrote these bash scripts a long time ago: quine.sh, quine2.sh, quine3.sh.
- This quite short Python2 quine or quite short Python3 quine attempts to be “pythonic”, fairly short and clear, while making it shorter seems to involve nasty tricks that make it less clear. Sadly including a #! line and a doc string makes a messier Python2 quine, and an even messier Python3 quine.
- And here’s a Octave/Matlab quine. Mike Garrity wrote an explanation.