27/09/2011
Sometimes you have to encode reversibly two (or more) values onto a single one. The typical example of a pairing function that encodes two non-negative integers onto a single non-negative integer (therefore a function
) is the Cantor function, instrumental to the demonstration that, for example, the rational can be mapped onto the integers.

In a more pragmatic way, it may be necessary to encode two values onto one for data compression purposes, or to otherwise exploit a protocol that does not allow many distinct values to be sent separately. The simplest way of pairing two integer is to interleave their bits, for example, with something like:
Read the rest of this entry »
8 Comments |
algorithms, bit twiddling, C, hacks, Mathematics, programming, Python | Tagged: Gödel, Gödel Number, Gödel Numbering, Integers, pairing function |
Permalink
Posted by Steven Pigeon
06/09/2011
In a previous installment, I discussed the quality of English in comments, arguing that the quality of comments influences the reader’s judgment on the quality of the code as well.

That’s not the only thing that can make code harder or easier to understand. Today (anyway, at the time of writing), I was working on something where arbitrary-looking constants would constantly come up. I mean, constants that you wouldn’t know where they’re from unless there’s a comment. A clear comment. Let’s see some of those.
Read the rest of this entry »
4 Comments |
algorithms, C, C-plus-plus, C99, Mathematics, programming | Tagged: Comments, Constants, hexagon, Sphere, surface |
Permalink
Posted by Steven Pigeon
30/08/2011
Quite a while ago, I discussed map generation in the classic 80s-era game Tunnels of Doom. After a short correspondence with Kevin Kenney himself (who kindly answered my questions; and I hope he is aware that he contributed to the fascination of great many kids in computer science), I manage to, not exactly reproduce his algorithm, but create a number of fun variations.

Tunnels of Doom Combat Screen.
This raises the question as to how do we encode maps efficiently in the computer’s memory, not only for Tunnels of Doooooom but also for any number of other games.
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, data compression, data structures, embedded programming, hacks, programming | Tagged: Dragon Warrior, Map, Map representation, Quad-Tree, Tunnels of Doom, Wumpus |
Permalink
Posted by Steven Pigeon
16/08/2011
This week, another programming challenge, but this time considerably tougher than the previous.

His Imperial Majesty, Xórgü of House Nand, decrees that the martian imperial court is in need of a new calendar. The current martian calendar counts 668 days, but it accumulates errors. The martian year, according to His Imperial Majesty’s most illustrious astronomers, is in fact 668.60 (martian) days long.
Read the rest of this entry »
13 Comments |
algorithms, hacks, Mathematics, programming | Tagged: Calendar, epagomenal days, leap year, Mars, Martian |
Permalink
Posted by Steven Pigeon
09/08/2011
A few years ago, I posted on my personal web page a number of programming challenges for my friends. This week, I present one of the old challenges: computing luma from RGB triplets using integer arithmetic only.

Read the rest of this entry »
36 Comments |
algorithms, bit twiddling, C, embedded programming, hacks, programming | Tagged: Arithmetic, Integer, Luma, rgb |
Permalink
Posted by Steven Pigeon
26/07/2011
In some optimizations problems, the objective-function is just too complicated to evaluate directly at every iteration, and in this case, we use surrogate functions, functions that mimic most of the properties of the true objective-function, but that is much simpler analytically and/or computationally.

Lately, I have done some thinking about what properties a surrogate function (or surrogate model) should have to be practical.
Read the rest of this entry »
2 Comments |
algorithms, Mathematics, Science | Tagged: Euclidean Space, function, Sphere, Surrogate Function, Vectors |
Permalink
Posted by Steven Pigeon
12/07/2011
When you come from different programming languages like C and C++, where you are used to control very finely what the machine does, learning Python is quite disconcerting, especially when it comes to performance.

Without being exactly an optimization freak, I rather like to use the best algorithms, favoring
algorithms over an
naïve algorithms/implementations, so you can guess my surprise when after replacing the
initial implementation by an
implementation I did not observe the expected speed-up.
Read the rest of this entry »
14 Comments |
algorithms, programming, Python | Tagged: array, Array List, CPython, Linked List, list |
Permalink
Posted by Steven Pigeon
05/07/2011
I’m always playing around with data compression ideas, and I recently came across a particular problem that inspired this week’s entry about implicit coding.

The idea of implicit coding is to exploit some constraint in the data that lets you reconstruct one (or more) value(s) unambiguously from the others. The textbook example of implicit coding is to use a constraint such as a known sum. For example, If you have a right triangle you necessarily have that
Read the rest of this entry »
1 Comment |
algorithms, bit twiddling, data compression, data structures | Tagged: Constrained equations, implicit coding, Pythagoras, rematerialization, triangle |
Permalink
Posted by Steven Pigeon
29/03/2011
Initializing large arrays of data before use is always cumbersome but it seems to be unavoidable.

The first types of solutions fill the array with a value that denotes an empty entry. While this sounds straightforward, the choice of that value is not always easy. For floating points numbers, zero may or may not be a good candidate. If convenient (as a zero floating point value is encoded as binary zeroes filling the variable) it may be difficult in some contexts to use because zero may be valid. Fortunately, there’s always the possibility to initialize the array with NaNs, which can be tested and used correctly (but you need the POSIX functions to do that).
Read the rest of this entry »
6 Comments |
algorithms, C-plus-plus, data structures, programming | Tagged: Aho, array, Bitmap, Hopcroft, Ullman |
Permalink
Posted by Steven Pigeon
22/03/2011
A friend of mine, Arthur, is developing a voxel-based game and found himself having to deal with large volumes of data. Unlike 2½D games where the map is essentially a 2D expanse with occasional relief, his game allows the definition of things in full (discrete) 3D.

To avoid loading the entire map in memory, he made the wise design decision of making his world tile-based, so that it can, simultaneously, reduce the working set as well as having an essentially open map, by loading only a small set of visible world blocks at all time. So together we took a look at compressing these world blocks, and it turned out that we can do a lot with fairly simple algorithms and VLCs.
Read the rest of this entry »
9 Comments |
algorithms, bit twiddling, C-plus-plus, data compression, data structures, Design, programming | Tagged: 2½D, Game, Game Programming, Huffman, Prediction, Prefix Codes, RLE, Tile-Based Map, Tree Structured Codes, Voxel |
Permalink
Posted by Steven Pigeon
/* no comments (part II) */
06/09/2011In a previous installment, I discussed the quality of English in comments, arguing that the quality of comments influences the reader’s judgment on the quality of the code as well.
That’s not the only thing that can make code harder or easier to understand. Today (anyway, at the time of writing), I was working on something where arbitrary-looking constants would constantly come up. I mean, constants that you wouldn’t know where they’re from unless there’s a comment. A clear comment. Let’s see some of those.
Read the rest of this entry »