Friday, 27 May 2011

Literal walks

This post follows an idea two friends of mine and I (all from Bow Cottage) had in our effort to combine our respective talents of arts, design and computing: literal walks.

The idea follows transcendental walks which I remember reading about on the Lunchtime Playground blog: convert any number - preferably a transcendental number - into a quaternary (base 4) number, and then use the digits 0-3 to guide a north/south/east/west random walk in the cartesian plane. The results are surprising.

Literal walks take the idea to the alphabet level: take a text (any text), transfer it in lowercase letters and delete all non-letter symbols, and then use the sequence of letters to guide a sort-of-random walk with 26 directions corresponding to the 26 letters.

Here's the Mathematica code implementing the idea:


And here are some examples. First, the word 'Mississippi':


The first paragraph of Moby Dick:


A take on Magritte's 'This is not a pipe' (this is not a line):


The March Hare:


Finally, my literal walk signature:


The black dot is the starting point - the gray one, the end point. The thicker line on the top left of the black dot is actually two canceling lines coming from 'h' and 'u'.

I'm not fully satisfied with the code I've come up with - for one thing, it contains a Do loop. But I don't have enough time to change it. Suggestions welcome.

Thursday, 26 May 2011

Comp Maths Bow

It's been some time since the last post - it seems like I'm contributing to the statistics of young dying blogs.

No need for me to explain the reasons, except maybe to say that I should change the name of the blog to 'Comp Maths East End' or 'Comp Maths Bow'. The Central Line is no longer transporting me from one side of London to the other everyday. But I still live close enough to it, so I won't change anything. It's too much of a hassle anyway.

The first post from Bow will come soon enough.

Monday, 7 February 2011

Pub quiz with Mathematica

Busy with work so only a quick entry this week.

A few weeks ago, while preparing for our regular monday pub quiz, our leader in useful-only-on-monday-knowledge asked us the following question: How many capital cities start and end with the same letter? I didn't know the answer and couldn't be bothered to look at wikipedia, so I queried Mathematica instead:

Select[CountryData["Countries", "CapitalCity"][[All, 1]], (StringTake[#, 1] // ToLowerCase) === StringTake[#, -1] &]

This is putting Mathematica's online database to a good use I thought. The result turns out to be

{"AndorraLaVella", "SaintJohns", "TheSettlement", "Avarua", "Asmara", "AddisAbaba", "Accra", "SaintGeorges", "Astana", "Abuja", "Oslo", "Warsaw", "SaintDenis", "Apia", "Ankara", "Tashkent"}

So the answer is 16 - sixteen cities that start and end with the same letter. (The Settlement, by the way, is the capital of the Territory of Christmas Island - another quiz question.)

Maybe I should tell the quiz master about Mathematica or WolframAlpha. Or maybe not.

Wednesday, 26 January 2011

Inserting computer code in LaTeX documents

After my post on lgrind, I searched a bit more for a good syntax highlighting utility I could use for inserting computer code into webpages and LaTeX documents. Good use of time, as I discovered that lgrind is a bit disappointing - it really pales in comparison with pygments and minted.

Pygments is a simple python-based command-line utility that transforms raw computer code into syntax-highlighted code in html, LaTeX and other formats. Much better as I said than lgrind - the output in fact is nearly perfect.

To install pygments on Mac OS 10.6, simply type

sudo easy_install pygments

After that, 'pygmentize' your code, say a C++ code into LaTeX, by typing

pygmentize -o mycode.tex mycode.cpp

With this, you only get the bare highlighted code; to get the full, compilable LaTeX document containing the highlighted code, type

pygmentize -O full -o mycode.tex mycode.cpp

More information, including the list of recognized computer languages, can be found on the pygments website.

With only pygments installed, you still have the job of inserting syntax-highlighted code into your LaTeX notes or presentations. Wouldn't it be simpler if LaTeX did the highlighting itself?

Install minted, a simple LaTeX package, and the job is done. Download the minted directory from ctan and run the makefile by typing

make

inside the minted directory. Then move the style (.sty) file and the documentation (.pdf) somewhere inside your LaTeX tree.

To use minted, use the following template:

\documentclass{article}
\usepackage{minted}
...
\begin{document}
...
\begin{minted}{matlab}
Your code here
end{minted}
...
\end{document}

Instead of Matlab, as above, use any of the programming languages recognised by pygments.

Note that minted is not a stand-alone package: it relies on pygments to generate the syntax-highlighted code and includes it when pygments' job is done. Not a perfect solution (the compilation can be a bit slow), but still quite a very good one.

Tuesday, 18 January 2011

lgrind on Mac

lgrind is a pretty-printer and syntax highlighting utility for computer code. You take some computer code, feed it to lgrind, and you get a publishable code (in html, latex, etc.) with formatted and colored syntax. Very useful for including code in presentations and lecture notes.

I tried to install lgrind from ctan on my Mac but couldn't compile the source code without errors. Fortunately, a bit of Googling led me to a set of Mac binaries courtesy of the JHL Development Blog.

The installation is simple:

1. Download and decompress the JHL's binaries
2. Put the executable file lgrind in your bin directory
3. Put the lgrind.sty file in your latex tree (available from ctan)
4. Download the lgrinddef file from ctan and put it where lgrind.sty is
5. Create a symbolic link in /usr/share/ pointing to your latex tree with

ln -s ~/Library/texmf texmf

Saturday, 15 January 2011

Network drives on Octave

Developing a new course always brings some surprises. Mine last week was to discover (during the very first tutorial session!) that the execution path of Octave doesn't seem to get updated automatically for networked and mounted drives - at least not on Windows machines. This means that you create a new m-file, then try to run it, but receive an error message saying that the m-file doesn't exist. Type 'ls' and you see the m-file is there. Type

exist('filename.m','file')

and Octave tells you the file is there. But try to run it and it's not seen. Quite an annoying bug.

Solution #1: Quit and restart Octave. Not much of a solution - my students weren't impressed.

Solution #2: Remove current directory from path, then add it again. Not more impressive.

Solution #3: Type

path(path);

every time a new m-file is created to refresh the path. This is the only (semi) sensible solution I've found so far.

Is this a Windows bug or an Octave one? I'm inclined to blame Windows of course but who knows? I couldn't find much information about this bug. Seems like we may have to buy Matlab licenses after all...