Log Watching

03/11/2009

Very often, you have to keep an eye on a log, or maybe more than one log, and a couple of other things while a long-term simulation is running. The GNU/Linux distributions offer the program watch that allows the periodical execution of a command in the current interactive shell. While watch is convenient, you still have the problem of displaying the needed information in a terminal geometry aware way. Turns out, there are tools to query the terminal geometry and we can use them to write simple, effective, well displayed scripts.

telescope-small

So let us see how we can make BASH somewhat aware of the terminal it runs in.

Read the rest of this entry »


Of staircases and textbooks

27/10/2009

This week, I’m talking you about a little identity that crops up often in the study of algorithms and which isn’t found in formula compendia—anyway, none that I have. I’m talking about this function:

\displaystyle C_{n,a}=\sum_{i=1}^n i a^i

This is a variation on the Gabriel’s Staircase function that does not have an infinite number of terms. Let us solve it without supposing that 0<a<1.

Read the rest of this entry »


Code Style: Vertical vs Horizontal?

20/10/2009

The only difference between coding styles and religion discussions is that coding styles have claimed fewer victims—at least until now. A few post back I discussed color schemes, and this week I’ll be discussing code geometry for enhanced clarity.

fish-on-stilts

Read the rest of this entry »


Cargo Cult Programming (part 1)

13/10/2009

Programmers aren’t always the very rational beings they please themselves to believe. Very often, we close our eyes and take decisions based on what we think we know, and based on what have been told by more or less reliable sources. Such as, for example, taking red-black trees rather than AVL trees because they are faster, while not being able to quite justify in the details why it must be so. Programming using this kind of decision I call cargo cult programming.

cargo

Originally, I wanted to talk about red-black vs. AVL trees and how they compare, but I’ll rather talk about the STL std::map that is implemented using red-black trees with G++ 4.2, and std::unordered_map, a hash-table based container introduced in TR1.

Read the rest of this entry »


Generating Random Sequences (part I)

29/09/2009

Every once in a while, we need a random sequence. Whether to test a data structure’s performance or to run probabilistic unit tests, the provided rand primitive from your favorite programming language has several limitations. First, it’s been known for a while that if most implementations of the C standard library rand() function are not very random, despite being “good enough” in great many cases. Second, and more importantly, it does not allow you to easily control the period nor to generate a permutation on 0\ldots n-1, for example.

dice

There are many methods of generating pseudo-random number sequences. Not all exhibit the same properties and, accordingly, a method may be more useful in one case and perfectly useless in another. High quality pseudo-random number generation is a notoriously hard endeavor, but there is a number of very simple algorithms that will get you out of trouble for certain specific tasks. Let us consider, for example, the example where the pseudo-random generator must generate the numbers in 0\ldots{}n-1 exactly once, in a random order, of course, in exactly n draws.

Read the rest of this entry »


What’s a Good Color Scheme?

15/09/2009

Although EMACS is growing old, filled with passé idiosyncrasies, and rather complicated to tweak, I grew accustomed to it and is now my main editor. I use it for shell scripting, C++, HTML, even. But to use EMACS properly, or a least make it enjoyable a little bit, one must do a fair bit of configuration. Key bindings to match modern keyboards, adding the macros you’re using the most, and, finally, adjusting the color scheme so that your eyes do not bleed after a few hours of work.

yeux-saignants

But what is a good color scheme?

Read the rest of this entry »


Short PWD in BASH Prompts

01/09/2009

It’s not uncommon to have inordinately deep directory hierarchy on your computer, especially if you’re like me and you like to give significant names to your directory. For example, if you’re using a 80×25 terminal, the location /home/steven/download/Album Photo/2009/06 jui/20-22/21-008-St-Georges-de-la-Malbaie will cause your shell prompt to wrap around the shell window quite messily. Of course, you could show only the last directory’s name, say 21-008-St-Georges-de-la-Malbaie to continue with my previous example, but that’s a bit terse, especially if you end up on a directory whose name is unexpectedly short.

The correct solution, may be, is to arrange the prompt to show adaptively long parts of the current working directory up to a given limit, and abstract parts of the path using, say, ..., and make sure the result is legible. For example, /home/steven/download/Album Photo/2009/06 jui/20-22/21-008-St-Georges-de-la-Malbaie, using a maximum prompt length of 50 would get shortened to .../06 jui/20-22/21-008-St-Georges-de-la-Malbaie, which is already much shorter yet retained its legibility and meaning.

Read the rest of this entry »


Filtering Noise (Part I)

25/08/2009

If you own a car, you probably noticed that the speedometer needle’s position varies but relatively slowly, regardless of how the car actually accelerates or decelerates. Unless your speedometer is some variation on the eddy current meter, maybe the noise from the speed sensor isn’t filtered analogically but numerically by the dashboard’s computer.

Ford_Mondeo_MK3_ST220_-_Speedometer_(light)

Let us have a look at how this filtering could be done.

Read the rest of this entry »


CA_COSMO

18/08/2009

Like me, you’re probably dealing with more than a single language at work and on a daily basis. If you’re from the US, it is not unlikely that you are speaking Spanish as well. Here, in Québec, we have English and french coexisting both at work and in our daily lives. Working at a computer, it means that I have two installed locales, one for the US Keyboard Layout and one for the Canada keyboard (formerly known as “Canadian French”) and I cycle between them constantly.

Typist

The fact that Gnome and Windows are smart enough to assign a keyboard locale per application or window allows me to chat in French in Xchat while coding using the US keyboard layout in emacs without explicitly switching the keyboard each time I switch windows. I’ve been doing this for so long I don’t even think of it. There are moment, though, where this model breaks a the seams.

Read the rest of this entry »


Cool, Baby, Cool

11/08/2009

Not all computer cases are equally well designed. That, I’m sure, you know. The thing you may not know, is just how badly designed certain cases are. Not only are they hard to service, they can be detrimental to your hardware! I recently had to change the hard drive from my backup box and ended up changing the whole kit altogether because the old machine did not recognize the new 500GB hard drive. So I reused my old Compaq Presario 6400nx computer, which does recognize the 500GB hard drive, but makes it run very hot. I mean, very hot.

6400nx-hot-small

The new drive, although almost idling except for the nightly backup scripts and whatnots, got to 50°C. Even if 50°C is within the manufacturer’s expected operating parameters (10 to 55°C, or similar), that’s about 20°C more than my main workstation’s hard drive that runs around 30–35°C. That got me worried because it is well known that high drive temperature shortens its life considerably. So I decided to solve the problem using tie-wraps—no, not à la McGyver.

Read the rest of this entry »