It seems that the company I have been working for, keenkong and I have taken our separate paths. It wasn't really anything spectacular, from my perspective it was basically just a little incompatibility. I wasn't overly enjoying it, and that likely affected how I worked.
So now I look to the horizon with a fair bit more uncertainty about the future. I'm feeling fairly neutral about the job ending. I'm not ecstatic but at the same time, I'm happy to have some free time and a chance to take on some more creative pursuits.
As for looking for a new job, I'm not feeling overly motivated to find a new development position. It seems there are enough open-source projects that I'd be interested in contributing to to satisfy any code craving I may have.
Right now my current goal looks to be go back to school full-time and finish that, and see where it takes me. Back to coding? Maybe but unlikely.
So yeah, I will announce here my (likely permanent) retirement from the software development profession, and hope that all of those reading still in it will enjoy it more than I did :)
PS: I will still be writing code in my spare time - I do like coding after all. This blog will continue to get posts about that kind of stuff, just probably not as many Rails/JRuby posts.
Jun 29, 2009
Jun 27, 2009
Using FreeImage in Ubuntu
In my last post I mentioned I wanted to output a visualization of the time it takes to render various parts of my fractal generator. To do this I used FreeImage, so here is a little tutorial on how to use the library in Ubuntu.
To install, just install the libfreeimage-dev package, either through Synaptic or using:
Here's some simple code to output a blue background to a bitmap:
To install, just install the libfreeimage-dev package, either through Synaptic or using:
sudo apt-get install libfreeimage-devOnce you've got that installed, you can link to it from your C/C++ programs.
Here's some simple code to output a blue background to a bitmap:
#include <FreeImage.h>Then to compile:
#include <stdlib.h>
int main(){
FreeImage_Initialise();
atexit(FreeImage_DeInitialise);
// create the bitmap object
FIBITMAP * bitmap = FreeImage_Allocate(200, 200, 32); // allocate a 200x200 pixel image, with 32-bit colour
// create the blue colour
RGBQUAD blue;
blue.rgbBlue = 255;
for (int i = 0; i < 200; i++){
for (int j = 0; j < 200; j++){
// draw a blue pixel at (i, j)
FreeImage_SetPixelColor(bitmap, i, j, &blue);
}
}
// save it as output.bmp
FreeImage_Save(FIF_BMP, bitmap, "output.bmp");
// deallocate memory
FreeImage_Unload(bitmap);
}
g++ image.cpp -o image -lfreeimage
Jun 20, 2009
Julia Sets - Moving the seed
I've been at the fractals again. This time I made it morph:
This is a Julia set again, but instead I am tracing a path through the complex space with the seed value. The path taken in this video is a near-circle centred at 0.12 + 0.74i with an real radius of 0.11 and an imaginary radius of 0.10.
There were a number of other really cool ones, but the problem is that the path they take tends to go into areas which have lots of points that do not fly off into infinity, which means they take a long time to process. That means that I can't have a nice framerate like the video has.
One thing that was kinda neat was that if you flip the sign on the centre of the ellipse for the imaginary component, it flips the patterns displayed on the x-axis (or maybe it's the y-axis). So centred at 0.12 - 0.74i, those swirls are in the top-right and bottom-left instead of the top-left and bottom-right.
A thing I would like to try to make better animations would be to see where the framerates get low. I can probably do this by iterating between -1 and 1 in the real and imaginary components and spit out a time for each computation. My plan will be to spit that out into a greyscale image so that it is easy to see where the slow points are.
This is a Julia set again, but instead I am tracing a path through the complex space with the seed value. The path taken in this video is a near-circle centred at 0.12 + 0.74i with an real radius of 0.11 and an imaginary radius of 0.10.
There were a number of other really cool ones, but the problem is that the path they take tends to go into areas which have lots of points that do not fly off into infinity, which means they take a long time to process. That means that I can't have a nice framerate like the video has.
One thing that was kinda neat was that if you flip the sign on the centre of the ellipse for the imaginary component, it flips the patterns displayed on the x-axis (or maybe it's the y-axis). So centred at 0.12 - 0.74i, those swirls are in the top-right and bottom-left instead of the top-left and bottom-right.
A thing I would like to try to make better animations would be to see where the framerates get low. I can probably do this by iterating between -1 and 1 in the real and imaginary components and spit out a time for each computation. My plan will be to spit that out into a greyscale image so that it is easy to see where the slow points are.
Jun 11, 2009
Actors and Distributed Computing
During my last post I spaced a fair bit and forgot to include a very important part about actors. I will now dedicate a whole post to that important part.
There are two requirements in an actor model: there is a way for an actor to reference another actor, and there is a way for the actor to send a message to that other actor. In a typical sequential OO program, this is done using instruction pointers and memory addresses and all that jazz. While it can also be done this way in an actor model, it is not restricted to this. For example, the way of referencing an actor can be done through the magical thing known as an IP address, and the way of calling an actor could be to send an HTTP request to that IP address. So in effect, many of us have already used the actor model without even knowing it!
The most important part of what I'm trying to say is that with an actor model when given two actors, these two actors may or may not be executing on the same machine.
An interesting thing is that this starts breaking down the definition of "program". A program can consist of several actors running across multiple machines within the same code-base, or can consist of several programs running across multiple machines and communicating with one another. Where does one draw the line? I can assume the Internet will not be referred to as a program, although technically it seems to follow the actor pattern.
There are two requirements in an actor model: there is a way for an actor to reference another actor, and there is a way for the actor to send a message to that other actor. In a typical sequential OO program, this is done using instruction pointers and memory addresses and all that jazz. While it can also be done this way in an actor model, it is not restricted to this. For example, the way of referencing an actor can be done through the magical thing known as an IP address, and the way of calling an actor could be to send an HTTP request to that IP address. So in effect, many of us have already used the actor model without even knowing it!
The most important part of what I'm trying to say is that with an actor model when given two actors, these two actors may or may not be executing on the same machine.
An interesting thing is that this starts breaking down the definition of "program". A program can consist of several actors running across multiple machines within the same code-base, or can consist of several programs running across multiple machines and communicating with one another. Where does one draw the line? I can assume the Internet will not be referred to as a program, although technically it seems to follow the actor pattern.
Jun 10, 2009
Actors
Concurrency is an interesting topic these days. Actually, it has been an interesting topic for a long time. Yet it is strange that many of us still only know of one way to write a concurrent program: using threads. This is not a bad approach - in fact it is the approach that seems to give the most control over how a program executes because it is close to the machine.
There are other models of concurrency that you can use. Being the ignorant boor that I am, I really only know about the actor model, however if you have the interest you can read about others here. I am not going to talk about those today, I will be telling you about actors.
While some may disagree, the main idea behind object-oriented programming is that there are objects which send messages to one another - in most OO languages, this involves calling the other objects' methods. Things like inheritance and encapsulation and all that are secondary - although no less important.
The actor model is the same thing. There are actors (instead of objects) which send messages to one another. The difference between actors and objects is that every actor is always running, and when it sends a message it does not wait for the message recipient to finish processing before it continues to execute. Let's illustrate this with an example:
With actors however, foo will have finished long before bar does. Why? Because after foo makes the call to bar, it just continues on executing in parallel with bar. Pretty neat eh?
That's pretty much all there is to actors. You can try playing around with them yourself by trying out Scala which has built-in support for actors and is not too far away from Java, or you can take a deeper plunge and try out Erlang. Unfortunately I do not have much experience with either language, in fact my experience with actors comes from a research project in university I did using a C++ library for actors. You can probably find libraries that implement the actor model for most modern languages, if you're not interested in learning a new language.
There are other models of concurrency that you can use. Being the ignorant boor that I am, I really only know about the actor model, however if you have the interest you can read about others here. I am not going to talk about those today, I will be telling you about actors.
While some may disagree, the main idea behind object-oriented programming is that there are objects which send messages to one another - in most OO languages, this involves calling the other objects' methods. Things like inheritance and encapsulation and all that are secondary - although no less important.
The actor model is the same thing. There are actors (instead of objects) which send messages to one another. The difference between actors and objects is that every actor is always running, and when it sends a message it does not wait for the message recipient to finish processing before it continues to execute. Let's illustrate this with an example:
method fooIn a traditional OO language like C++, Java, or basically any other OO language that I've worked with, nothing will happen in foo until bar has finished. So foo ends up taking a long time to execute, even though nothing really in foo takes a long time.
call bar
.. do stuff which does not take a long time
end
method bar
.. do stuff that make take a long time
end
With actors however, foo will have finished long before bar does. Why? Because after foo makes the call to bar, it just continues on executing in parallel with bar. Pretty neat eh?
That's pretty much all there is to actors. You can try playing around with them yourself by trying out Scala which has built-in support for actors and is not too far away from Java, or you can take a deeper plunge and try out Erlang. Unfortunately I do not have much experience with either language, in fact my experience with actors comes from a research project in university I did using a C++ library for actors. You can probably find libraries that implement the actor model for most modern languages, if you're not interested in learning a new language.
Jun 8, 2009
Blocking Reddit from Blogger
Over the year and a half or so that I've been writing this blog, a number of my posts have been put onto reddit by their search bot, gst - at least I think it is a search bot, I don't think anybody could have posted as many articles as they have unless they are sitting there reading blogs 24/7 - or maybe it is actually multiple people, who knows. Anyway, some posts were well liked, some were not, but in the end it doesn't really matter to me.
There is a good and a bad side to reddit. The people who like my stuff either just leave (possibly putting a vote up on reddit) or they subscribe and continue reading and occasionally share an intelligent comment. This is welcome. What does bug me is the people who just come in, leave a nasty comment or two like "yer gay" or something and fuck off. These people contribute absolutely nothing and when I'm having a bad day it is not the kind of email I want to come home to.
So anyway, I wrote up a little script to block out reddit traffic. To install this on your Blogger blog, just click the "Layout" tab, go to "Edit HTML" and drop this somewhere outside of a CSS tag:
There is a good and a bad side to reddit. The people who like my stuff either just leave (possibly putting a vote up on reddit) or they subscribe and continue reading and occasionally share an intelligent comment. This is welcome. What does bug me is the people who just come in, leave a nasty comment or two like "yer gay" or something and fuck off. These people contribute absolutely nothing and when I'm having a bad day it is not the kind of email I want to come home to.
So anyway, I wrote up a little script to block out reddit traffic. To install this on your Blogger blog, just click the "Layout" tab, go to "Edit HTML" and drop this somewhere outside of a CSS tag:
<script>This basically just boots them back to reddit. It won't affect them if they copy+paste the URL into their browser, but I figure if they're willing to go through that much effort to read your stuff they actually want to read your stuff and probably aren't going to troll.
if (document.referrer.match(/reddit\./)){
window.location = document.referrer;
}
</script>
Jun 5, 2009
Newton's Iterative Method
Once in a while when you're doing math processing, it is useful to be able to estimate the zeroes of a function (if f(x) = 0, then x is a zero of f). An example is square roots. A square root of n is simply the zero of f(x) = x2 - n. This is a really useful operation.
I'm going to talk about Newton's Iterative Method, which is a way of estimating a square root. I say estimating because since most square roots are irrational numbers, it is impossible to represent them accurately using floating point arithmetic. So we just get as close as we can to it.
There are other ways of calculating square roots. The square root of n can also be expressed as eln(n)/2. However this is slower to calculate.
How does this method work? Well to understand it fully you need to understand calculus, however with square roots the calculus is really simple so you can just take my word for it if you don't know calculus.
Let's try to find the square root of 2. Our function is therefore:
We start off with a guess. Let's say 1. If you want you can start off with any other number except zero (I'll explain why you can't use zero in a bit), but I'm going to start with 1.
What we want to do now is find the linear approximation (first order Taylor series if you want to be more pedantic) of f around the point x = 1. We'll call this point x0 The function, we'll call it T, for a linear approximation is:
Now for the more interesting part. Let's look at a picture of these two functions (the blue line is the x-axis):

We want to find out where the linear approximation hits the x-axis. By setting T to zero and solving for x we get:
When do we stop looping? Well, since we can't get absolute precision, we can stop looping when the xi is within a certain threshold of some error:
There's a few catches. What would happen if we used a guess of zero? Well, we'd end up with a divide-by-zero error. Basically the linear approximation to f would be flat, and never touch the x-axis.
What would happen if we used a negative guess? Well, that leads to a more interesting discussion. Newton's Iterative Method finds local zeroes. The closest zero to a negative number (say -1) is not around 1.41421. It is close to -1.41421, which is the other square root of 2. When there are multiple zeroes to a function, the iterative method will tend toward which ever one it is sloping toward. So if you're trying to find the zero of a function that crosses the x-axis several times, choose your initial guess wisely!
Finally, here's an interesting problem. Try finding the square root of -1 using this method. Try it with different guesses.
1If you don't know what a derivative is, it is the function which tells you the slope of f at any given value of x.
I'm going to talk about Newton's Iterative Method, which is a way of estimating a square root. I say estimating because since most square roots are irrational numbers, it is impossible to represent them accurately using floating point arithmetic. So we just get as close as we can to it.
There are other ways of calculating square roots. The square root of n can also be expressed as eln(n)/2. However this is slower to calculate.
How does this method work? Well to understand it fully you need to understand calculus, however with square roots the calculus is really simple so you can just take my word for it if you don't know calculus.
Let's try to find the square root of 2. Our function is therefore:
f(x) = x2 - 2The derivative1 f′ of this function is 2x.
We start off with a guess. Let's say 1. If you want you can start off with any other number except zero (I'll explain why you can't use zero in a bit), but I'm going to start with 1.
What we want to do now is find the linear approximation (first order Taylor series if you want to be more pedantic) of f around the point x = 1. We'll call this point x0 The function, we'll call it T, for a linear approximation is:
T(x) = f(x0) + f′(x0) * (x - x0)For our function, it looks like this:
T(x) = x02 - 2 + 2x0(x - x0) = 2x0x - x02 - 2At x0 = 1, we have:
T(x) = 2x - 3
Now for the more interesting part. Let's look at a picture of these two functions (the blue line is the x-axis):

We want to find out where the linear approximation hits the x-axis. By setting T to zero and solving for x we get:
x = x0 - (x02 - 2) / 2x0 = 1.5We now use this 1.5 as our new x0, except that we will call it x1. Now we plug that into our formula:
x = x1 - (x12 - 2) / 2x1 = 1.416667Call that x2, plug that in again, we get:
x = x2 - (x22 - 2) / 2x2 = 1.414216We're getting pretty close eh?
When do we stop looping? Well, since we can't get absolute precision, we can stop looping when the xi is within a certain threshold of some error:
while |f(xi)| > εYou can set this ε to whatever you want. A higher value will mean less precision but more speed - however the iterative method moves pretty damn quickly toward the zero so it is not a huge deal.
do iterative method
There's a few catches. What would happen if we used a guess of zero? Well, we'd end up with a divide-by-zero error. Basically the linear approximation to f would be flat, and never touch the x-axis.
What would happen if we used a negative guess? Well, that leads to a more interesting discussion. Newton's Iterative Method finds local zeroes. The closest zero to a negative number (say -1) is not around 1.41421. It is close to -1.41421, which is the other square root of 2. When there are multiple zeroes to a function, the iterative method will tend toward which ever one it is sloping toward. So if you're trying to find the zero of a function that crosses the x-axis several times, choose your initial guess wisely!
Finally, here's an interesting problem. Try finding the square root of -1 using this method. Try it with different guesses.
1If you don't know what a derivative is, it is the function which tells you the slope of f at any given value of x.
Subscribe to:
Posts (Atom)
