Jun 29, 2009

/(Temporary)? Retirement/

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 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:
sudo apt-get install libfreeimage-dev
Once 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>
#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);
}
Then to compile:
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.

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.

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:
method foo
call bar
.. do stuff which does not take a long time
end

method bar
.. do stuff that make take a long time
end
In 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.
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:
<script>
if (document.referrer.match(/reddit\./)){
window.location = document.referrer;
}
</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.

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:
f(x) = x2 - 2
The 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 - 2
At 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.5
We 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.416667
Call that x2, plug that in again, we get:
x = x2 - (x22 - 2) / 2x2 = 1.414216
We'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)| > ε
do iterative method
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.

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.

Jun 4, 2009

Glassfish, JRuby and Initialization Parameters

Sometimes in your Rails app you may want to pass some variable to your program, via an environment variable or something. With a regular Rails app, you can just do `export VAR_NAME=something` and it will detect it just fine. However if you're deploying in a container-based server like Glassfish or Tomcat, you'll have to pass the config values in a different way because it doesn't seem to pick up the enviroment variables.

You can do this with your warble config. If you haven't already done so, create your warble config file like this:
jruby -S warble config
This will create a file at config/warble.rb in your Rails app. Inside there you'll see all sorts of config settings for the JRuby environment.

What I wanted to do was output the SVN info on the site, so that when we were testing in the Glassfish environment we could see what revision we were working with. So I just had this little snippet of code:
`svn info` =~ /Revision: (\d+)/
revision = $1.to_i
Spit that out on the page somewhere and everyone can see what revision they're working with!

However the hard part is getting this into the WAR. What you have to do is pass it as a parameter in the warble config, like this:
config.webxml.svn_revision = revision
When warble runs, it will output this into the web.xml file within the WAR, which will get passed to your server.

To access this value from within the Rails app, you need something like this:
if defined?(JRUBY_VERSION)
revision = $servlet_context.getInitParameter("svn_revision")
end
If you're only ever running in JRuby then you don't need the if defined? junk.

The $servlet_context object is a Java object that represents the servlet context. I actually don't really know what that is, but a quick search of the docs gives a handy bit of info and the one you want is the getInitParameter method.

Jun 3, 2009

Find What You Like In Your Language(s)

After one has been programming for several years and has messed around with enough languages, you start to notice that you like some languages better than others (and feel the need to tell the world this). This type of feeling is completely subjective and usually based on experiences with the different languages, and is quite different from programmer to programmer. I've met programmers who refuse to use any language that is interpreted or run inside a VM, at the same time I've met programmers who refuse to use any language that doesn't use garbage collection - for completely different reasons (if you must know, the first ones hated wasting the users' time/resources, the second ones hated wasting their own time/resources).

I'll share my story with you. I like to program in Ruby - in case you haven't noticed at this point. However, I also still like to program in C++. Anybody who has seen both of these languages (or have even met people who are fans of either language) can be completely boggled as to why, myself included. They are so completely different that it is almost surprising to say they are both imperative languages (you can get more different by going into functional languages like FORTH, but we won't go there right now).

However I've realized there are three things that I like about coding - freedom, elegance, and speed. It's really difficult to find a language where you can have all three of these while still having the available libraries for me to make games or web apps in (the only apps that I really like to program it seems). Freedom is present in both C++ and Ruby - it is the idea that there are many ways to solve a given problem. This is what really turned me off Java1 and to a lesser extent, Python2; and makes me pick C++ over C.
It is the other two that these two languages are at odds with. You don't really get faster than C++ - unless you're a whiz with assembly, which I am not. C might be faster, but I loves me the STL and the Boost. With C++ you have access to OO solutions and the security of a rigid type system (although I have to say I was amazed by the wonderful combination of flexibility and security in Haskell's type system). It seems like if I can write code that is really fast, not too much else matters - for example I ditched the std::complex class in favour of three doubles for my fractals implementation, it was a speed increase of like 10x which basically made it go from taking several seconds per frame to pretty much real-time. It's probably because g++ decided to put those doubles as floating point registers or something, where the complex objects were just being stored in RAM. I could probably find out for sure by doing an ASM dump of the code, but it's late and I'm going to sleep soon. Maybe I'll post on it some other time. Anyway, it finally occurred to me to upload this (a few months late), so take a look:


ASIDE: After watching this video I realized that this has been converted from screen capture to Ogg/Vorbis to AVI to FLV - if you want the code to watch it yourself, you can grab it here. It requires SDL and a C++ compiler.

At the same time though, Ruby is expressive. I can say what I want to say easily (more so than with pretty much every other language I've used - maybe I still have my head in the box and need to branch out more) and elegantly. It is the aesthetics of the language which appeals to me at a higher level. One thing that annoyed me when using Python was when I wanted to do string processing - Ruby's regex handling is amazing, especially when coupled with the OO aspect. And long method chains seem to have a much nicer flow to them than something like foo1(foo2(foo3(x))), so long as you don't have too many blocks thrown in. And finally, the fact that EVERYTHING is an object, and EVERYTHING works like an object (sorry Python, you're nice and all but things like len(str) annoyed me from this perspective), and finally EVERYTHING is modifiable. While Ruby's monkey-patching can be a royal pain in the ass even when used by people who know what they are doing, it is basically the language designer's way of saying, "I don't know everything. Have at 'er." I respect that a lot and think that it is a great way to make the language evolve.

So, what is it that you like?

1Note that I am not saying Java is bad - I know plenty of good programmers who I respect that love Java (and/or Python for that matter). I'm just saying that its way of doing things are not really the way I like doing things, I find myself fighting it more often than not.
2I actually kinda like Python. I respect it because it does what it wants to do really well - basically saying there is one way to do something, and not getting in your way while you do it. A thing that I love and hate about Python is that it removes the need for the thought, "I can think of multiple, equally good ways of doing this, but which is the most elegant?" With Python you just do whatever works. It bugs me and relieves me at the same time because sometimes I can think of a solution that is more "elegant", but not really any better (and possibly worse).

Jun 1, 2009

KDE, revisited

The other day I posted about me trying out KDE, and my first impressions of it. I've been using it a bit since then to see how I like it.

Initially it blew me away, since the default look and feel is much nicer than GNOME. It's not grey and brown anymore, there are smooth animations and fading effects, etc. etc. It feels much more modern and appeals on a more aesthetic level.

However after I started using it more, there was just a few too many things that annoyed me for me to really want to keep using it. I will probably try and see how to fix some of the problems but for now it is easier just to stick with GNOME.

Here's a list of the annoyances I've had. Note that a lot of them I could probably fix by either searching through their settings panels, or scouring the Internet. However it is just easier for me to switch back to GNOME, which is already pretty much the way I like it.
  • Panel widget sizing - the widgets on your panels seem to have no maximum size, so they just expand to fill up the whole panel. This is really really annoying, because the little cashew thing on the right hand side overlaps the widget that is furthest to the right. Also if you like to use programs like Avant Window Navigator (AWN) like I do, there is no need for the "task manager" widget that shows the open windows. However if you remove it, the other widgets will expand to take up the entire space. Nothing like a clock widget that is 300 pixels across. I ended up having to split the one panel into two, with a huge gap in the middle. Kinda broke the aesthetic aspect there.
    This was something that I did try to look up online how to fix. Apparently there is a "Spacer Widget" that you can get that just takes up empty space, however it was not available through their auto-download thing and I didn't really feel like searching for it. On top of that it feels like a rather ugly way of handling this.
  • Cashews. The little cashew thing is for tweaking settings. It's nice to have it right there in front of you the first time you try KDE because that way you have easy access to make your tweaks. However after you make your tweaks, they are still there in the way of what you are trying to do. A good alternative would be that you right-click on the panel and go to "Settings". Oh wait, they do that. So why would they have the cashew thing?
  • Desktop - Where did all the files on my desktop go? I put lots of files on my desktop either for quick and easy access, or for reminders of a program that I'm supposed to check out - although it is surprisingly easy to forget the things that are right in front of you all the time.
  • The menu - I like the favourites part. And the search feature. However I have counters: with a dock there isn't really a need for the favourites list, because you just put your favourite programs on the dock. And the search feature seems to only be useful for me since the damn menus are a bit more of a pain to navigate! With old-school menus I just need to mouse over things and they open. If I open the wrong menu, it is trivially easy to go back to another one since I can still see it (and am likely still looking at it). Instead with KDE, it is a bit more work to actually have to navigate to the menu, so you just type it in the search field instead. I suppose some people may prefer this over the GNOME menus but I'm not sure if I do.

Anyway these are the little things here and there that I didn't like so far. I'll probably switch back and forth between KDE and GNOME until I get KDE working the way I want it, or when I finally get completely annoyed and stick it out with the less nice aesthetics of GNOME.