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...

Wednesday, 15 December 2010

Faking Hirst with four lines of code

I don't know anyone who's not grateful that computers have replaced humans for performing tasks like doing arithmetic calculations or searching for bits of information. Few think, however, that humans can be replaced for creating art. I have yet to see a computer-generated piece selling for millions.

But how difficult is it to mimic art pieces on a computer? Are pieces worthless because they can easily be replicated? And who gets the credit (or the money) for a computer-generated piece - the programmer or the computer?

Here are two examples to put things in perspective. First, my replica of Damien Hirst's polka-dot-LSD painting, obtained with only a few Mathematica lines:


The advantage of using the computer here is obvious: I can generate endless variations of Hirst's piece with different colors at random at the touch of 'Shift-Enter'. How's that for simplicity and productivity?

Needless to say perhaps, I'm not a big fan of Hirst, and being able to generate what he sold (or copied) for £43,300 doesn't help me appreciate what he does.

Next, a piece of Bridget Riley I saw described this morning by Adrian Searle on the Guardian website:


Searle says about the piece: "I can't see the system of drawing at all... It took about a week, a week to actually execute... What you're watching is your own perception."

I'm not sure about my own perception - even less about Mathematica's - but I know that with a bit of fine-tuning and luck (the output is random after all), I can get close to the original, again with only a few lines of code.

Does that mean that Riley's circles are trivial? Well no. She puts a lot of time on her pieces, as shown in the Guardian video, and it does take some work for us humans to come up with something that looks random yet nice and original at the same time.

Riley has also done in the past some remarkable pieces on optical illusion and geometry. For those in London, you can see some of her works at the National Gallery until May 2011.

This is a just a preview of the underestimated art of using computers for doing art. Have a look at the Computer Arts Society website for more (historical) examples. I'll post more of my own computer art in the future, though none in the next two weeks because of Christmas vacations.