## Lament of an artist

I recently came upon a tweet by Kathy Sierra. Here it is:

So I went ahead and downloaded the PDF file and read it through. Man did it resonate! Here’s a quote that practically captured the essence:

Mathematics is an art
– Paul Lockhart

Lockhart was describing how the current education in America was eroding the sense of wonder, discovery and thrill of mathematics in children and young adults. First, go download his article Mathematician’s Lament (about 25 pages) and read at least the first and last few pages if you’re in a hurry.

I don’t know much about the American education system, but the way it’s described, it’s bad. And the Singapore education system (at least with regards to math) is similar (well, at least from what I remember when I was studying). The sense of discovery just wasn’t there.

When I was about ten (I think), my parents bought me an encyclopedia set, Childcraft. The first book I went for? The one on mathematics.

I gorged myself on the puzzles, stories, thought experiments in the book. For example, there was that crossing of river puzzle, with a farmer and a sheep and a wolf. There was a story excerpt of Flat World, where Points and Lines meet the 2 dimensional Squares and Pentagons. And there’s a “proof” of the non-existence of vampires. And … wait, what? You want to know the proof?

Ok, suppose vampires suck blood out of victims and the victims become vampires themselves. Suppose there’s only 1 vampire in the beginning. On the first night, he bites one person. By the next day, there are 2 vampires. 2nd night, each vampire bites another person. So there are 4 the next day. I’m sure you can see how this goes… at some point, the doubling vampire population simply overshot the entire world population. But we’re still here, aren’t we? Hence no vampires.

Of course, that proof goes out the window if vampires don’t need to bite every night… or if they can control who to turn into a vampire… ok, moving along…

Oh yes, with this encyclopedia in hand, I learnt about the fun parts of mathematics. I learnt about the magic numbers of 3, 4 and 5 forming a standard 3^2 + 4^2 = 5^2 Pythagorean triangle long before I actually knew what Pythagoras’ Theorem was. I read about how the ancient Greeks were racking their brains for how to get a right angle, and they used a length of rope with evenly spaced knots. They found that a triangle with sides of 3 knot length, 4 knot length and 5 knot length, created a right angle at one of the corners. That was fascinating.

There was also this section on Fibonacci numbers, and a story about rabbits. The algorithm behind the sequence was presented with an interesting back story of an expanding bunny population. I remember in university, my professor mentioned the sequence and I exclaimed, “Fibonacci sequence!”. And my friend, who’s sitting beside me, turned to me and asked, “You know about this?”. Really? A math university student who don’t know about Fibonacci sequences?

I don’t know about you, but for all the talk about Asians being better (at math) (here and here), I think most of us still don’t get to see math as an art, as something beautiful to contemplate. I started out seeing math as fun, then later on as something scientific and not-so-fun. And after my service in the military, I went to university, and suddenly it’s fun again. I guess it took me a fresh perspective to view math once more…

And I think programming is like math. No, I’m not bringing in the meta-blogging type of “This blog topic is something like that” and go off on some far out analogy. I mean programming, like math, requires one to see the abstract, manipulate pieces of information in the mind’s eye, come up with solutions in the ether, and then bring forth the solution into the world.

Right now if you ask me about math, like really hard core math, I’d probably stumble. Partly from a long disuse of the principles, but mostly because unconsciously, I’m trying to capture that sense of wonder again. That requires me to shed some undesirable shackles and discipline, and allow myself to not know everything on the topic. Math fun is back in vogue.

P.S. The next book in the Childcraft encyclopedia I pounced on? The one with stories. I guess I’m a sucker for fiction. *smile*

## Follow up on geodynamo idea

I toyed with the idea of using Earth as part of a gigantic dynamo to generate electricity previously. A few arguments were deliberately left out, to encourage you to think about it, and so that I’ll have something else to talk about here. *smile*

Jonathan didn’t think it will work. His argument was that the support structures in space won’t be able to hold (steady). And xero suggested using humans as generators. Let’s look at both of their comments.

### Side effects of the geodynamo

Having a large part of the structure out in space should mitigate the gravitational pull of Earth. With some thrusters thrown in, the use of light-weight and strong material for the structure, the whole thing should work. I think.

I’m not so much concerned if it’ll work, but what happens if it does work. I’m concerned about the electromagnetic influence on marine life, on living things around the equatorial region. And most of all, I’m concerned if it will affect Earth’s rotational spin, that it will slow it even more.

As programmers, we seldom have to think about the consequences of our creations. We’re more concerned with making it work. The best examples I have right now are the social media tools, such as Twitter. The microblogging platform flourished, with consequences ranging from people complaining it’s a time-waster and productivity-drainer, to people using it as part of their business strategies, to connecting with people they would never have met.

But there’s a difference in enabling something waiting to happen, and something that’s not.

### Mini dynamos. Thousands of them.

Instead of one gigantic monster of a generator, we could have thousands of mini dynamos. Human powered. The idea is to connect gym bikes to power generators. While we’re exercising, all that energy is lost, so we might as well generate some electricity.

I disagree with xero on paying people minimum wages to go exercise. Something gets optimised whenever an incentive is introduced, particularly if it’s monetary… and if you don’t agree with that, go talk to a manager. The ideal case is that people exercise because they want to maintain a healthy lifestyle, and they want to help generate some alternative sources of electricity. Intrinsic motivation versus extrinsic motivation.

On the thought of gym bikes, what about cars? We have many of them on the planet, and their wheels are spinning. And not really doing anything else.

Can we attach some power generating contraption to car wheels? If we’re burning fossil fuels to turn them, we might as well try to salvage something out of it. It wouldn’t totally negate the loss of energy (that’s almost like a perpetual motion machine), but we would waste less of it. It’s like those lights on bicycles that light up when the biker is pedalling.

Let me know what you think. And I still want my free electricity.

[If you’re Chinese, Happy Lunar New Year to you!]

## How can we have free electricity?

I was travelling on the bus. My thoughts were flying around doing their flights of imagination. “It would be wonderful to have electricity available everywhere.” Then came “It would be even more wonderful if it’s free. Or at least cheap.”

Then I wondered, “How can we have free electricity?”

“We would need something to generate it.” And my next immediate thought was “A dynamo!” I was remembering my physics lessons long ago, when we were playing with magnets. One of the interesting things I remembered was a spinning magnet as part of a dynamo to generate electricity.

So, we need an easy way to do rotation, preferably on a big scale. And the biggest thing that rotates? Earth.

I was thinking of building some structure to house some magnetic material along the equator. Earth is spinning at about 465 metres per second. That should count for something. As the Earth rotates, that structure “rubs” another supporting structure above it, using the theory of the dynamo, we’ll get electricity!

I don’t even know if the theory behind it is sound. Assuming it is, what’s going to hold the upper structure? It can’t be supported by the Earth, because that would defeat the purpose, since it will spin along too. Then came my next breakthrough.

Supporting structures in space.

There will be satellites holding up the structure. I thought of having just one single structure, but the mechanics of holding it steady might be tough. Have you ever played that game where you have to hold a rod of metal with a loop, and slowly pass it through a maze of metal wires without touching the wires? You need steady hands.

Ok, that drawing was bad… I hope you get the idea.

Anyway, several separate satellites to hold up the structure might be better. Now that I look at the diagram carefully, I’m not even sure if the structure would hold up… perhaps some super light material…

Assuming this works out fine, I imagine physicists (theory), engineers (building it), mathematicians (calculations) and other scientists would be involved. Programmers too, for the software used to run the satellites and the structure. Biologists too, for the effects of such a large scale of electromagnetic energy on living things near the equator. Particularly the marine biologists, since most of the structure is over the ocean.

Alright, fine. It’s probably not going to work. I’m just saying, what if it works? What if we can get cheap, even free electricity generated this way? What if you can come up with some other way?

What if…

## If you depend on order, use an order by

It was an uneventful morning. All of us were at our computers, softly typing in the noiseless office. Tippity tap, tippity tap… (Except for me, since I was cranking out code to complete a project by a deadline, and was furiously testing the physical limits of the keyboard. But I digress…)

[image by tysmith]

A phone rang, breaking the silence.

“Hello? Yes… Uh huh… REALLY?!”, answered my colleague.

What followed was a whirlwind of exclamations and activity back and forth between two of my colleagues as they discussed the situation. Some description of something in a report wasn’t correct, as reported by a user, and they were tracing the origins of the error. I didn’t find out what the problem was, since I had my own problems to deal with (tap-tap-tap).

Even with my earphones on, I could still vaguely figure out what’s going on. It’s not that they’re loud (although there’s that…), just that I’m aware of my surroundings. The problem boiled down to a `select` statement.

Let’s say the database table looked like this:

```create table product
(
product_id char(8) not null,
effective_date datetime not null,
product_description varchar(50) not null
)
alter table product primary key (product_id, effective_date)
```

Based on the narrative I gave, and the structure of the table, you should reasonably be able to figure out the issue. No?

Alright, so the product description in a report wasn’t correct. There’s an effective date column, so I would think the record with the latest effective date was more relevant. What if the `select` statement had a `where` clause only on the product_id?

```select product_id, product_description
from product
where product_id = 'PROD0001'
```

Based on the table structure and the primary key, there’s every reason to believe that there were multiple records with the same product ID. Why the original programmer failed to take note of this is beyond me…

In the case where there were 2 or more records with product ID as “PROD0001”, the default order was product ID ascending, then effective date ascending (according to the primary key). What happened to the description based on the latest effective date? It’s right at the bottom of the result set. What was required? The description based on the latest effective date.

Since the default was to use the record at the top of the result set, the description based on the earliest effective date was used. Hence the error.

There were multiple records for the same product IDs. The reason this problem didn’t occur was that the description was the same for the respective IDs. Until now.

My colleagues ordered the results by effective date in descending order, and all was well.

There were other instances where the result set of an unordered `select` statement came out fine, until the order was different. The data could have, by coincidence, been inserted in the correct order, hence the retrieval automatically had that order. The data could have, because of a primary key, been automatically ordered when retrieved. But that’s no excuse to depend on the default ordering.

If you depend on the order in your results, use the order by clause.

## I hate server migrations

Some time ago, the backend Unix server had to be upgraded from 32-bit to 64-bit architecture. All the C and C++ programs had to be recompiled and tested. What were the changes specifically targeted at?

The `int`s and the `long`s.

The programs were connecting to the Sybase database using some of the Sybase libraries. And some of the libraries were obsolete, requiring more changes. Specifically, stuff about DBINT and CS_INT (used for binding database values to program variables). I’m not going to talk about the technical details of the migration.

The point is that the programs were written with DBINTs in mind. Then a creative programmer some time in the past decided to use CS_INT instead of DBINT, thus creating a dissonance in the system of programs. The creativity was splattered here and there, and there lies the difficulty.

The migration changes typically involved casting or changing an `int` variable to a `long` variable. Due to the, uhm, lazy incompetence of the programmers of yore, the programs were written in lax mode. Meaning all sorts of compilation errors were allowed to pass by the then lenient 32-bit compiler.

The new 64-bit compiler, however, would have none of that. It threw out a long list of errors that would have choked the printer were it to be printed. Suddenly, function declarations, strict type casting and the like were back in fashion.

In particular, the libraries supporting CS_INT were no longer available or not supported. I had to translate CS_INT to DBINT for the programs, in addition to the compilation errors. If there was ever a time for being consistently wrong, this was it.

If all the programs were using DBINT, the changes might be many, but they’d be similar. When some of the programs used DBINT, and others used CS_INT, I had to deal with two sets of error corrections. And both types of casting errors needed to be dealt with differently.

Sometimes, tedium is better than frustration. I’m just glad that was over.

## Weaving in the crowd

The crowds in shopping malls have always frustrated me. I’m not the type to meander around the corridors looking for bargains or sales. If I want to buy something, usually I already know what’s the item and where I can get it.

So I’ll arrive at the entrance of the shopping mall, and I know where the shop that has my desired item. It’s a straightforward point A to point B thing. But there are so many obstacles! I feel like I’m running an obstacle course, dodging one after another, stopping to let one pass, speeding up to avoid another. What are these obstacles?

My fellow human beings.

Judging from where I am, the right side of the corridor seems packed. So I move straight, and encounter a slowly moving elder. That’s easy, and I swerve around quickly to encounter…

The family of five. You never really know what kids will do. I’ve had experiences where I would be walking and minding my own business, and then out of nowhere two screaming children would materialise on my left and sprint in front of me to my right. So I swerve a bit more to my left to encounter…

The giddy teenagers, who just emerged from one of those trendy fashion stores. Don’t go near them, because they could suddenly point their fingers somewhere, and go “Oh look, ZARA!” and run enthusiastically toward said store. You need a wide buffer area to improve your chances of dodging their stampede. That, and their unforeseeable and inexplicable fits of high-pitched laughter. So swerving, I encounter…

The young parents with their 6 month old baby in a stroller. This one’s easy, because they will usually move very slow. The stroller also restricts their range of angular movement, so if it’s pointed one way, that’s the direction the parents are moving towards. Easy to predict the point they’ll reach in say, 2 seconds, which is the amount of time I have before I reach them. By which time, I’ll encounter…

The bored boyfriend. This one can be easy or hard, depending on where said boyfriend decides to wait out his girlfriend’s retail rampage in the store. If he decides not to be a free model for the store, he will move out a little onto the corridor. At which traffic will then flow around him and make my path that much harder. This one’s a bit more considerate, choosing to stand close to the store instead. Simple to pass through, and I’ll encounter…

The sentinels. They are a bunch of friends, laughing and talking. Quite ok actually, if not for the fact that their preferred battle formation is a single file moving horizontally. This isn’t a CSI search and comb; you don’t need to spread out into a line. If you want to maximise face time, the line is the worst formation. A circle is much better, like around a camp fire for example (haven’t they heard of complete graphs?). The worst thing is they’ll block a lot of the corridor, leaving you with very little maneuverability. They’re like the sentinels of some hidden treasure, who found an intruder, and are inexorably moving forward to crush the invader… Moving sharply to my left, I avoid them and finally reached my destination. *whew*

The above story was fictional. Well, I didn’t meet the characters all in one trip anyway… but they’re real. This is my long-winded way of saying, sometimes even if you know the destination, there are many unforeseen variables. Such as a software project. Ok, that analogy is a little weird…

I want to point out that, in order for me to successfully navigate around all the colourful characters who so innocently obstruct my path of least resistance, I have to read them. I have to read their body movement, posture and facial expressions to decipher what their likely directions of movement are.

That takes some practice, because you need to know that the person slowing down in front of you might change direction, or even stop completely. Of course, you will need to detect that the person is in fact slowing down…

I used to get angry because of the people blocking me. Why are they moving so slow? In time, I’ve learnt to control my anger. I’ve even turned it into a game of sorts. Here’s how you can play too.

At a moderately populated shopping mall, decide on a destination, preferably one that’s on the same level as your starting point. Your goal is to reach that destination at a more or less constant speed. Say your walking speed is 2 metres per second. You can speed up a little, or slow down a little, but you may not break into a run or stop completely.

This should test your knowledge of human behaviour, how good your reflexes are, whether you can adjust your route on the fly based on real-time data and so on. It also trains you to be more aware of your surroundings, your body position and posture, the length of your stride and so on.

I have 2 killer moves to help you: the shoulder slant and the side step. In the shoulder slant, you swivel one of your shoulders forward and the other backward. This way, you’ll be able to squeeze through some “cracks” in the obstacle run. Because sometimes, there’s never a good time, so when an opening appears, take it. With practice, you’ll learn which shoulder to swivel forward too, depending on situations (swivelling the left shoulder forward means the left foot is more natural to use for the forward leg).

The side step is particularly useful for when the idiot in front of you who don’t know where to go, decides to stop completely. At this point, you must rapidly decide if you want to go to the left or right of this confused and lost person. And then do a side step. Basically you open up your legs sideways. So instead of moving your leg forward in a normal step, you move it to the side.

You need to decide quickly because your momentum will carry you forward, and you don’t want to knock down the fella, right? Because of your forward momentum, there’s almost non-existent side-ways momentum. Thus if you move to the left, you must exert pressure on your right leg to “bounce” to the left. It takes a little practice…

Also, if you decide to go right, and your left foot is in front of you, then the natural movement is to move your right foot to the right in a wide split so as to avoid the now stationary person in front of you. Similarly for the decision to go left.

What if you decide to go right, and your right foot is in front? Uh, try to avoid this situation… Don’t try any fancy dance moves or you might entangle your legs and fall down. Just stop (and you’ll lose the game, but it’s better than public embarrassment). With some practice, you’ll learn to move in the direction based on the length of your stride and where your feet are.

And that’s some 1000+ words to tell you to be observant of human behaviour, to be conscious of your surroundings and to be aware of your body position and posture.

## Viruses on Windows, Macs and Unix

The most powerful force in the universe is compound interest.
– Albert Einstein

Einstein may not have said that, but compounding is really powerful. I’ll get to the point in a bit, so just read on…

There’s this story I read about a rich man employing a young lady to count his fortune. She took 6 days to complete the task, and the result was that the man was 42 million dollars rich. The man then asked her how she wanted to be paid.

[For the purposes of the story, 100 pennies equal a dollar.]

The young woman asked to be paid 2 pennies for her 1st day. Then pay her the amount paid the previous day, multiplied by itself, for the next day. So her 2nd day costs 2 * 2 = 4 pennies. Her 3rd day costs 4 * 4 = 16 pennies. And so on till her 6th day.

The rich man thought, “Such a foolish girl!”, and promptly agreed.

So for her 4th day, she had 16 * 16 = 256 pennies. Her 5th day costs 256 * 256 = 65536 pennies. And her 6th day? 65536 * 65536 = 4294967296 pennies.

Wait a minute! 4294967296 pennies is just over 42 million dollars! And so the young woman took all of the rich man’s money. The end.

The point is, small things can add up (or in the clever woman’s case, multiply up). What’s that got to do with computer viruses? What is the primary ability a computer virus needs? To spread to as many computers as possible.

[For the purposes of the following discussion, “computer virus” encompasses all the bad things coded by a human being that could happen on a computer. That should cover viruses, worms, hacks and so on…]

This is why I find people’s reactions to the “susceptibility” of Windows machines towards computer viruses, … confusing. They might say that Macs don’t have this problem, or Unix machines have that security clamped down. There will be rabid fans supporting their favourite operating system.

The thing is, I’m sure there are computer viruses on Macs and Unix. Why is there a lack of mass destruction and mayhem on those platforms? My answer might be deflating for those supporters.

There simply aren’t that many people using those platforms.

As far as I know, Windows is used by most people on a computer. The path of least resistance for a virus writer is to target Windows. And he won! For a while at least… then another outbreak, then fixed and so on.

Each “win” sort of amplifies the “susceptibility” of Windows. Virus writers get a little bolder, a little more creative. People get scared, news stories (in the early days) sort of “glorifies” the damage done, and the difference in platforms got a little wider (even if it’s just people’s perceptions).

Bit by bit, Windows come under fire for things such as the blue screen of death, the ease with which an attacker disrupts, and poor security models. I believe it’s just a scaling factor. Web browsers are now targeted, and that means the operating system doesn’t matter as much anymore.

This is why I find it amusing whenever I encounter what’s known as an Apple fanboy. The praises showered on Apple products for their beauty and elegance. Granted, that’s true. It’s when they also show their disdain for Windows that’s amusing. Why such a strong emotion?

I admit right now. I don’t really have overwhelming love for Windows. Hey I’ve got an iPhone! I just find it useful for me. I like using .NET because it allows me to do what I want quickly and easily (I have a friend who “eewws” at the mention of .NET …).

And I’ve only been seriously wounded by computer viruses a couple of times in my entire life of using computers (probably protected by my positive aura). So I’m offering another reason, and drawing a broad generalisation in the process… Mac and Unix users are generally fairly competent with computers. They are designers, so using image editors is second nature to them. They are system administrators, and let’s face it, if you can do command line stuff, you’re competent.

It’s the not-so-competent users that get hit by computer viruses. Broadly speaking of course, and I don’t know if it’s true, so this is just my conjecture. Where are most of those users? On Windows machines.

So based on small reasons, a twist of fate here and there, and compounding all that, and Windows seem to be riddled with security loopholes, wide open for any attack. But I don’t see it that way.

## Bloggers Calendar 2009

About a month ago, I was invited to participate in a project that’s never been done in Singapore before: A calendar for 2009 featuring bloggers in Singapore. It turned out well, and so yeah, the rest of this is going to sound narcissistic…

Still here? Ok, I’m not going into the details of how this came about. The summary is that a few people organised it and the lot of us went to a studio to get our photos taken. I’m born in June, and I’m featured on that month together with another June baby. Here’s me:

The pretty lady has an equally pretty handle: clicktokill. You do not want to mess with her…

I bought 6 hard copies of the calendar, each month printed on glossy photographic material (I don’t know the technical term for it…) with a cover page. For the life of me, I can’t remember why I ordered 6… Thus I’ve got a couple extra, so if you’re interested (really?), I can send one to you (email me with your address) even if you’re not in Singapore (I’m feeling generous).

Or you can head on over to the web version which was done by Darran (he did a great job on that). Then you can download a zip file with the images yourself. Have a look at the other bloggers while you’re there!

And special thanks to Willy from LiveStudios, who’s the photographer capturing us on film. If you’re in Singapore and looking for photographers, you know who to ask for.

## How useful is wisdom of the crowd?

I checked out the entries on Wikipedia and found there’s a book named The Wisdom of Crowds by James Surowiecki and another entry on just wisdom of the crowd (note the “s”). The idea is that groups of individuals are able to provide better solutions, answers or opinions than a single individual.

[image by Michael Jay]

Now Surowiecki stated 4 criteria needed for it to work:

• Diversity of opinion
• Independence
• Decentralisation
• Aggregation

The first two points are somewhat countered by herd behaviour, where people are (easily) influenced by other people. Based on my observations of people around me, and my personal experience, I found that it’s challenging to come up with your own original idea, thought or opinion. Then it’s also challenging to defend that originality against questioning by other people, or self doubt.

The other two points seem related. The more you decentralise, the more you need to aggregate better. It’s like specialists and generalists (using the terms loosely here). The more you specialise in something, the more work you need to tap on other fields of knowledge.

Aannyway, I was thinking about independent thought, the phrase I heard that’s “wisdom of the crowd” and crowdsourcing (though after reading its meaning, it’s not what I wanted). I was thinking if you’re working on something really hard, would the crowd give you better answers? Or would you (or an individual knowledgeable in that field) be a better choice?

Previously, I talked about using heuristics to match image textures. I was using a few tests to determine if a given texture image matched any of the textures in a sample set. This is like using wisdom of the crowd. No one individual test could match with a reasonably high enough accuracy rate. Let’s compare my matching algorithm with the criteria above.

I’ve got a few different tests, so “diversity of opinion” is covered. I’ve got “independence” because each test doesn’t rely on another. I’ve got “decentralisation” because each test is an “expert” on the particular test algorithm used. And I’ve got “aggregation” when I used some kind of average of the tests’ results.

What happens if the problem is so hard, so unheard of, that you don’t know whose opinion to seek?

Diversity dilutes the final (if any) solution if unrelated opinions are brought on. And for an uncommon problem, independent thinkers might get swayed by the first seemingly correct answer (thus following the herd), simply because the problem is so hard. And there’s not much expert knowledge if the problem is so rare (there goes “decentralisation”).

And whose answer are you going to trust, or how are you going to consolidate answers (aggregation)? When answers are uncertain, bias comes into play. A person’s reputation, your feelings towards a person, the way a person presented his solution.

Remember how I used weights on the results of my texture matching algorithm? Not all opinions are created equal…

Of course, all this could be another barber’s paradox, because there’s no such problem! *smile*

## Remember to change the year too

There were occasions when I was to increment or decrement some date value by one month. For example, “200806” to “200807”.

No, I don’t have any disaster-insider-programmer stories to share. Although there was this fellow who forgot to change the year which resulted in a financial fiasco…

Anyway, this is more like a reminder to myself. Take care when January and December are involved. Change the year when going from say “200812” to “200901”, and also “200801” to “200712”.

If you’re bouncing from one date to another by multiple months, you could try storing the year and the month into one variable holding total months. So “200806” becomes 2008 * 12 + 6 = 24102 months. Moving 15 months ahead, we have 24117 months, which is 2009 * 12 + 9. So it’s “200909” finally.

Of course, if your standard code libraries have an inbuilt function, just use that. For .NET, it’s

```DateTime.Now.AddMonths(15).ToString("yyyyMM");
```

Here’s something to think about: Using the algorithm above, what year and month is 24120 months?