Thursday, September 21, 2006

A Tale of Two Hacks

Years ago I wrote rcenter which has long been superseded by the LIRC project. I finally switched to LIRC myself. I wonder if anyone out there is still using rcenter.

It took me a while to figure out that LIRC does not work with OSS drivers unless Stephen Beahm's midi poll patch has been applied. I decided to switch to the ALSA drivers to avoid this issue. But then I had to set some other module options: snd-emu10k1 enable_ir=1 extin="0x3fc3" extout="0x1fff".

I wrote xmmspipe almost simultaneously, and in contrast, this project isn't obsolete yet. In fact, I recently discovered it has been an official Gentoo Linux package for some time when I received a bugfix for it.

It's times like these when I feel warm and fuzzy. I get to experience first-hand some of the touted benefits of free software. Having the source open means thankfully someone else can solve the problem. Even if I managed to fix the bug myself, I probably would've spent hours doing so.

On the other hand, I wouldn't mind if xmmspipe were obsolete and named pipes came with XMMS by default. I'm reminded of a quote from Doug McIlroy:

This is the Unix philosophy. Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

and one from Rob Pike:

There has been much talk about component architectures, but only one true success: Unix pipes.

Thursday, September 14, 2006

Mental Feats

Michael Curtis (who commented on a previous posting) has written several articles on mental feats involving memory and mathematics.

Coincidently, I too read about the Trachtenberg system (a method for performing mental arithmetic) many years ago.

Reading his site reminded me of a time when I'd relieve boredom during occasions such as high school assemblies by squaring 2-digit numbers in my head using the techniques I had read about.

Today, I'd still use Trachtenberg's method for numbers ending in 5, based on the equation (10 a + 5)^2 = 100 a (a+1) + 25. However I have since found faster methods for other numbers, which I haven't seen described on the web. They only require additions and subtractions, but one has more to memorize.

Let n be the 2-digit number to be squared. Then if n lies in the range:

  1. 0-25: Memorize these answers.
  2. 25-50: Work out how far n is from 50 and how far n is from 25. Then the answer is 100(n - 25) + (50 - n)^2.
  3. 50-75: Compute 100((n-50) + 25) + (n-50)^2. (This is also Trachtenberg's method for squaring fifty-somethings.)
  4. 75-100: Compute 100(100 - 2(100-n)) + (100-n)^2

While I'm at it, I'll record a method for finding square roots (of squares of 2-digit numbers):

  1. Remove the last two digits of the square. Then the first digit of the answer is the largest digit whose square is less than this number.
  2. The last digit of the square tells us what the last digit of the answer could be. If it is 0 or 5, then so is the last digit of the answer and we are done, otherwise:
  3. Let the first digit of the answer is a. Compare the square with the square of 10a+5. If larger, then the last digit of the answer is between 6 and 9, and if smaller, it is between 1 and 4. Luckily, in base 10, the squares of 1 to 4 have distinct last digits. Also the squares of a and (10-a) end in the same digit, so it is now easy to determine the last digit of the answer.

The corresponding algorithm for cube roots is much simpler, because the cube of each digit has a distinct last digit (and similarly with other odd powers).

Friday, August 18, 2006

Slideshows in Firefox

Like many geeks, I frequently use text-based interfaces where normal people use GUIs, with a sense of smug self-satisfaction. Instead of a WYSIAYG word processor I use typesetting software like LaTeX. No fancy website creators for me, I use gvim to edit HTML files. Spreadsheets? I keep data in flat text files and write scripts to process them.

How about presentations? I had used MagicPoint for a few, but I wasn't completely satisfied. For instance, equations were fiddly: I had to write a script that would run TeX to render the equations to encapsulated PostScript and embed the resulting image in the slideshow. I briefly thought about writing my own program. Very briefly. Then I thought about exploiting existing programs instead.

I had come across PinPoint which uses GIMP to produce great-looking slides from a few lines. GIMP was designed to manipulate and display text and images, and is scriptable. But for live presentations, and for certain features I wanted, other programs or scripts would be needed, requiring a fair amount of work.

An idea hit me. MagicPoint can convert slides to HTML. How difficult would it be to modify things slightly so that presentations can be done in a web browser? After all, web browsers also manipulate and display text and images from a simple language. Not only that, they were designed to show different pages in succession. They are also ubiquitous.

One would just need to display pages in fullscreen, and perhaps using Javascript, have certain keypresses cause certain actions such as changing slides and triggering animations and other effects. Soon after experimenting with this, I discovered I was definitely not the first to think about web-based presentations.

The Opera browser has long had a slide show feature (the Opera Show Format), but unfortunately it is not supported by other browsers. I want it to work on Firefox.

Luckily, an alternative, the S5 project, has surfaced, which creates slideshows from a few lines of XHTML, and should work on any standards-compliant browser.

S5 was just what I was looking for. Webpages can contain images, text, visual effects, animations, and so on, and in theory S5 presentations should be able to as well.

MathML in S5

I want to display equations via MathML, but at present one cannot simply embed MathML (or SVG) and change the MIME type of S5 slides accordingly, though a fix exists and will be released.

As a workaround, I use ASCIIMathML. Perhaps this is a good thing. I had intended to put LaTeX style equations in the middle of the HTML and use itex2mml to convert it to MathML, but since ASCIIMathML converts to MathML on-the-fly using JavaScript, I can skip the compilation step. (Other tools to convert human-friendly text to MathML are blahtex, TexToMathML, and TtM.)

There's still the matter of getting the equations to display on Firefox. Until the STIX Fonts are ready, extra mathematical fonts have to be manually installed.

Also, for months now, MathML does not display correctly on certain Linux systems, though a workaround exists [also described here]. And for some reason, S5 is extremely slow on my Debian system, but runs fine on the Windows build of Firefox.

Monday, July 17, 2006

Mnemonic Major Systems

I discovered two things when I tried using the net to brush up on mnemonic major systems (aka phonetic number systems). (If you have no idea what they are read the article before continuing!) Firstly, the only encoding from digits to sounds I've seen online so far is the one published by Harry Lorayne. Secondly, it turned out I didn't need any brushing up at all. The mapping I learnt many years ago was so easy to remember that, not only can I still recall it, but the memory is so strong I cannot use Lorayne's system without confusion.

I came across the major system I use in a book by Jean Hugard. I argue that it is more natural and easier to learn. There were only a few rules to remember:

1,2,3 correspond to the consonants l,n,m respectively. This is easy to remember since the letters require 1,2, and 3 strokes to write. If you happen to know the British sign language alphabet, observe that you place 1, 2 or 3 fingers on the other hand's palm when signing l, n and m respectively.

Some mappings are based on the way you pronounce digits in English. 4 is r (think “fourrr”), 5 is f or v (think “five”). 0 is s or z (think “zero”).

Then there are the digits that look like letters. 6 is b or p, 7 is t, th or d. 9 is k or g. With sufficiently bad handwriting (or fonts), 6 and b are indistinguishable, as are 9 and g (or q, which in English is always pronounced starting with a k or g sound, and always as a k sound in several European languages). 7 and T are also similar.

The only rule I never liked much was the one for 8 (which is not a problem since being the odd one out makes it easy to remember!): “Eight” sounds like “aitch”, which hopefully helps you remember that the sh, ch (and j) sounds correspond to 8.

Digit Consonant(s) Reason
1 l strokes
2 n strokes
3 m strokes
4 r sound
5 f, v sound
6 b, p shape
7 d, t, th shape
8 ch, j, sh special
9 k, g, q shape
0 s, z sound

I also mostly prefer the mapping from playing cards to words as presented by Hugard. Although I dislike the aces been treated specially and agree with Lorayne assigning the names of the suits to the jacks, I believe thinking KH as a groom and QH as a bride for example is easier to learn.

However, there is at least one practical benefit to Lorayne's system. By Benford's law, it is more likely that a number one wishes to memorize begins with a 1, and it is easier to think of a word starting with d, t or th than to think of one starting with l.

Friday, May 26, 2006

Firefox and Manpages

I prefer to read manpages in a web browser rather than in a terminal or a text editor. Konqueror formats manpages very nicely [screenshots here], but I use Firefox, and do not use KDE.

I wish there were a Firefox extension to handle manpages in a similar way to Konqueror. Instead, I use man2html. I setup a Smart Keyword so that typing man firefox for example retrieves the manpage for Firefox.

Unfortunately I feel its output is not as pleasing as Konqueror's. As a stopgap measure, I use the Greasemonkey Firefox extension to add some CSS to make the pages more to my liking.

  • script.js: Greasemonkey script that activates every time man2html is invoked on localhost. Adds a CSS file to the page.
  • man2html.css: CSS file that changes the look of the manpages.

Tuesday, April 11, 2006

Web Syndication and Atom Feeds

Due to popular demand ;-) I added a web feed for my blog. [Much to my surprise I discovered today that there is at least one occasional reader of this blog besides me. Thanks for your kind words.]

I experimented with web feeds when I first heard about the Live Bookmarks feature of Firefox. It wasn't so long ago, and the process was straightforward: I created a few RSS 0.91 files, and filled them with bookmarks, each entry using a title, description and link tag.

Now I find that RSS has forked into two different formats, confusingly named “RSS 1.0” and “RSS 2.0”. (In fact, there are more versions of RSS.) A third contender, Atom has surfaced.

It seems Atom 1.0 is the most fashionable choice, so I wrote a script to generate an Atom feed for this blog.

Atom is more complicated than RSS 0.91. Dates must be included and have to be in RFC 3339 format. Unfortunately the date command in Debian stable is too old to support the --rfc-3339 switch, so instead I used

date +%Y-%m-%dT%H:%M:%S%z $* | sed 's/..$/:&/'

Some sort of ID is also required, and I'm not sure exactly what the purpose of this is. The specifications refer to some Internationalized Resource Identifier as defined by RFC 3987 but that's one standard too many for now. At the moment I use the relevant URL as the ID.

Ideally the mime-type of an Atom file should be served as “application/atom+xml”, which in Apache can be accomplished by adding

AddType application/atom+xml .atom

in a configuration file.

In a HTML file, one should add something like:

<link rel="alternate" type="application/atom+xml" title="Title" href="http://some.url/something.atom">

and perhaps also lines like:

<a href="http://some.url/something.atom"><img src="http://some.url/feed-icon-16x16.png" alt="Site Feed" height="16" width="16" /></a> <a href="http://some.url/something.atom">Site Feed</a>

where the feed icons can be downloaded from feedicons.com.

Monday, April 10, 2006

Git and Cogito

Somehow, I have managed to avoid revision control systems until recently. I set it up for a typical reason: I wanted to be able to work on my thesis from different computers without worrying too much about keeping different copies in sync. Also, it's nice to be able to keep track of my progress. I don't expect to ever revert to an older version, but seeing a detailed log of changes makes me feel I've accomplished something.

Perhaps it's a good thing I put off learning about version control for so long. Back when I first thought about using it, CVS was the most popular open-source choice, Subversion was immature, and Linus Torvalds didn't use any system for the Linux kernel.

Now a new generation of source control tools has materialized, and are being improved rapidly. Development of the Linux kernel has benefitted ever since Linus started used revision control systems years ago.

So it seems it is a better time than ever to delve into version control. The old systems were plagued with various flaws and annoyances. The latest systems resolve these issues, but I wouldn't know. I never used the older systems so I have no idea what the problems were.

Things have gone smoothly for me with Cogito (basically some shell scripts based around Linus Torvalds' git tool). Even though it's designed to be a distributed source code management tool, it works well for one person editing a few files.

The only problem is that I can't easily host the repository on my Debian-stable server since git is too new, though Debian backports exist. I could just use rsync, but this method is deprecated and highly discouraged. [Also explained in this git tutorial; search for “rsync”.]