Lament of an artist

I recently came upon a tweet by Kathy Sierra. Here it is:
Mathematician's Lament tweet from Kathy Sierra

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!

Geodynamo structure

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.

Geodynamo space supporting structure

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.

Metal zapper game
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…)

Dramatically lit office phone
[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 ints and the longs.

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.