Category Archives: Good Math

Simple Lempel-Ziv Compression in Erlang

I decided to do a little bit of something useful with Erlang, both to have some code to show, and to get some sense of what it’s like writing something beyond a completely trivial example. Because the capabilities of Erlang shine when you get into low-level bit oriented things, I thought that writing a bit of data compression code would be make for a good example. I’m going to present it in two parts: first a simple but stupid version of the algorithm; and then the encoding part, which into bit twiddling, and potentially gets more interesting.

I’m going to use a simple version of Lempel-Ziv compression. Before I get into the detail
of how LZ compression works, I’ve got an interesting story about how I learned about it. My first summer in grad school, I was looking for a job. One of my college friends worked for a subsidiary of Travellers insurance, and he got me an internship with them.

Our group (3 of us) worked on programs that ran on salepeoples’ laptops. Since this
was 1991, laptops were still pretty primitive. We had to run in 128K of memory, with the best machines having 10 megabytes of hard disk, and the worst machines having two floppies. So memory use was always a huge issue.

Being an insurance company, naturally things were pretty bureaucratic. They hired me to write a prototype of a new program. I don’t remember the details of what I was supposed to write. But the way things worked, they wanted to build this new tool for the salespeople. But the company wouldn’t let them propose a new project without having staffing for it. So they hired me to work on it. But because they hadn’t proposed the project before they hired me,
I had nothing to do while they proposed it, and worked out their requirements. So I worked for them for a total of about 12 weeks; and it took them about 9 1/2 weeks to get to the point where they had an approved project for me to work on. So I had over two months with nothing
to do. So I kept pestering the other two guys about what I could do to help them.

One of the things they wanted was a way of doing a splash screen. They had a GIF image of the company logo, and they thought it would be nice to be able to splash it on the screen whenever the sales app loaded. But they didn’t have an image viewer that they could call from inside their program. So they asked me to write one. GIF images are encoded using LZ. So I coded that up the LZ decoder to get the bitmap out of a GIF in C, and they were happy. Then I decided, as long as I had an LZ decompressor, I should write an LZ compressor, and then we’d have some generic data compression code. So I went ahead and did that, and added a
nice, effective set of data compression routines to our libraries. But the manager was actually pissed at me: I’d added a feature to our library – data compression – without getting permission. The right way of doing things was to write a proposal, and pass it around the various levels of petty bureaucrats for two months while I sat and twiddled my thumbs on the payroll.

Anyway… Back to compression.

Continue reading

Building up more: from Groups to Rings

If you’re looking at groups, you’re looking at an abstraction of the idea of numbers, to try to reduce it to minimal properties. As I’ve already explained, a group is a set of values with one operation, and which satisfies several simple properties. From that simple structure comes the
basic mathematical concept of symmetry.

Once you understand some of the basics of groups and symmetry, you can move in two directions. You can ask “What happens if I add something?”; or you can ask “What happens if I remove something?”.

You can either add operations – which can lead you to a two-operation structure called a ring; or you can add properties – in which the simplest step leads you to something called an abelian group. When it comes to removing, you can remove properties, which leads you to a simpler structure called a groupoid. Eventually, I’m going to follow both the upward and the downward paths. For now, we’ll start with the upward path, since it’s easier.

Building up from groups, we can progress to rings. A group captures one simple property of
a set of number-like objects. A ring brings us closer to capturing the structure of the system
of numbers. The way that it does this is by adding a second operation. A group has one operation
with symmetric properties; a ring adds a second symmetric operation, with a well-defined relationship
between the two operations.

Continue reading

The Meaning of Division: Quotient Groups

After that nasty diversion into economics and politics, we now return to your
regularly scheduled math blogging. And what a relief! In celebration, today I’ll give
you something short, sweet, and beautiful: quotient groups. To me, this is a shining
example of the beauty of abstract algebra. We’ve abstracted away from numbers to these
crazy group things, and one reward is that we can see what division really means. It’s
more than just a simple bit of arithmetic: division is a way of describing a fundamental
structural relationship that pervades mathematics.

Continue reading

Macros: why they're evil

I’ve gotten both some comments and some e-mail from people in response to my mini-rant about Erlang’s macros. I started replying in comments, but it got long enough that I thought it made sense to promote it up to a top-level post. It’s not really an Erlang issue, but a general language issue, and my opinions concerning it end up getting into some programming language design and philosophy issues. If, like me, you think that things like that are fun, then read on!

Continue reading

Records in Erlang

One of the things I discovered since writing part one of my Erlang introduction is that Erlang has grown a lot over the last few years. For example, the idiom of tagged tuple as a way of creating a record-like structure has been coded into the language. There is, unfortunately, a bit of a catch. They aren’t really added to the language. Instead, there’s a pre-processor in Erlang, and records
are defined by translation in the pre-processor. This to me typifies one of the less attractive
attributes of Erlang: much of Erlang has a very ad-hoc flavor to it. There are important high-level features – like record data and modules – which aren’t really entirely part of the language. Instead, they’re just sort of spliced in however it was easiest for the implementors to cram ’em in. And there are other things that were added to later versions of the language that, while first class, are awkward – like the “fun” prefix for first-class functions.

The effect of that kind of thing is mainly syntactic. The implementation is good enough that
even though things like records are really pre-processor constructs, they feel almost as
if they’re built-ins.

Anyway – let’s take a look at records.

Continue reading

Symmetric Groups and Group Actions

In my last post on group theory, I screwed up a bit in presenting an example. The example was using a pentagram as an illustration of something called a permutation group. Of course, in
my attempt to simplify it so that I wouldn’t need to spend a lot of time explaining it, I messed up. Today I’ll remedy that, by explaining what permutation groups – and their more important cousins, the symmetry groups are, and then using that to describe what a group action is, and how the group-theory definition of symmetry can be applied to things that aren’t groups.

Continue reading

Groups and Symmetry

In the last post, I talked about what symmetry means. A symmetry is an immunity to some kind of transformation. But I left the idea of transformation informal and intuitive. In this post, I’m going
to move towards formalizing it.

Continue reading

What is Symmetry?

escher-tess.jpg

As I said in the last post, in group theory, you strip things down to a simple collection of values and one operation, with four required properties. The result is a simple structure, which completely captures the concept of symmetry. But mathematically, what is symmetry? And how can something as simple and abstract as a group have anything to do with it?

Continue reading

From Sets to Groups: Deep Meaning in Simple Constructs

The point of set theory isn’t just to sit around and twiddle our thumbs about
the various definitions we can heap together. It’s to create a basis on which we
can build and study interesting things. A great example of that is something
called a group. Once we’ve built up sets enough to be able to understand a set of values and an operator, we’re able to build an amazing deep and interesting construction, called a group.

Back when I started this blog, one of the first topics that I
wrote about was group theory. I was just looking back over that
series of posts, and frankly, they sort of stink. I’ve leaned a lot about
how to write for a blog in the time since then, and so I’d like to go back
and rewrite it. I’ve never reposted any of the group theory material, so
it should also be new to most readers.

Continue reading

From Sets to Numbers: Climbing Up to the Rationals

When last we left off, I’d used set theory to show how to construct the natural numbers; and then from the natural numbers, I showed how to construct the integers. Now, we can take the next step in building
up the full tower of numbers, showing how if we’ve got the natural numbers defined in sets, we can define
the rational numbers using sets and our constructed integers.

Continue reading