Dec 22, 2009

Qt

It's been a while since I did any GUI development, but these days I've been working on a little project for a friend of mine. I decided to check out Qt, which is the main GUI library used for KDE applications. It's one of the main GUI toolkits for Linux apps that I still hadn't tried and cared to try (I haven't tried Tk, but I'm not really interested in trying it because of aesthetic reasons).

After a while of working with Qt, I have to give it two thumbs up!
Here's some of the highlights:
- "Garbage Collection" - it's not real garbage collection as far as I know, but the memory for GUI widgets is managed within Qt, so you don't have to worry about deleting them. It isn't full garbage collection because you still have to clean up your other stuff, but it's a nice to have.
- The meta-object system. Qt doesn't actually use real C++, it uses a slightly extended version which it converts to standard C++, and then compiles. This gives it the really nice messaging system that it has called slots and signals. A signal is a method that you don't define yourself, you just define a section in your class called signals:
class MyWidget : public QWidget {
...
signals:
void opened();
void closed();
At some point during your program you can emit a signal like this:
emit opened();
This makes your object send out the opened() signal to anyone who cares to listen.
How do you listen then? You use a slot. A slot is just like a regular method, except you can attach a slot to a signal. For example, a button in an app would send out the clicked() signal when you click on it:
connect(myButton, SIGNAL(clicked()), this, SLOT(myButtonPushed()))
This line would make it so that whenever myButton's clicked() signal is sent, the myButtonPushed() method on whoever "this" is would be called. You can even connect signals to other signals, so that when one signal goes off, it triggers another signal. This is handy for creating widgets that have subwidgets, but you don't want to reveal the subwidgets to other classes.
- The tools - You'd think that if there is this meta-compiler that has to run before the C++ compiler, the Makefiles for Qt would be nasty. And they probably are. However Qt provides you with a Makefile-generator so that you don't actually have to touch the darn things. You just go:
qmake -project && qmake
Running this from your code folder sets up a Makefile for your project so that you don't have to.
There is also an IDE for Qt development called Qt Creator. I haven't tried it because I'm not quite ready to stop using Vim, although supposedly Qt Creator has a Vim-mode so it might be interesting to try...unfortunately though it seems that to build Qt Creator you need Qt 4.6, which is not available in the Ubuntu repos just yet. Darn. Oh well, you can just use the qtcreator package. I'll try this out and let you know what happens.
- Aesthetics - the default widgets in Qt are appealing. They don't look like blocks, the fonts are well rendered, and animations are subtle/smooth. I know many developers don't actually care about aesthetics, but I do and this is a big win for me since I'm not very good at making things pretty myself.
- Cross-platform - my code can compile under Windows just fine, and supposedly under Mac as well. In fact Nokia advertises that it can run on Symbian as well, not that I need that very much.
- Cross-language - I'm using C++ for this, but you can write Qt apps in Java (and therefore any JVM language like Scala and JRuby) and Ruby, and probably more but I haven't bothered to check others.

So yeah, if you have the need to write a GUI-based app, give Qt a shot. It sure beats then hell out of GTK!

Dec 14, 2009

GIMP - White Face Outline

A couple people asked me how I created the picture for my current Facebook profile, which looks something like this:


I did it with the GIMP, and it was actually really easy. There's a few steps to follow:
1) Open up your picture in the GIMP (this is an easy step)
2) Go to Filters -> Edge-Detect -> Sobel - I use this one because it generates a lot less noise than the other ones, and it is mostly black and white. The issue is that some features which should be brighter aren't, like lips. You can experiment with some of the other Edge-Detect filters they have in there if you like.
3) Go to Colours -> Threshold. Drag the little arrow around in the histogram (which will appear on a dialog called "Threshold") until you get something you like. At this point as I mentioned in the last step certain features might not stand out, so you might have to do some tinkering with a white pencil to get those features to stand out a bit more (unfortunately I'm not an amazing artist, so the lips in that picture above look kinda wonky).
4) Remove noise. You can do this with the pencil tool to draw black over the noise, or you can use the select tool(s) to select and delete stuff (make sure to set your background colour to black). Do this until you have removed the stuff you don't want.

And tada! You should have an image that looks something like the one above.

Dec 12, 2009

Resetting Your Keyring

For those of you who have forgotten your keyring password and are annoyed at the apps like Empathy and Ubuntu one who insist on using it, you can reset it by deleting your keyrings folder:
1) Go to your Home Folder
2) In the address bar type ~/.gnome2/ (if you don't see the address bar, click the little icon with the paper and pencil that has a tooltip like "Toggle between ...")
3) Delete the folder called keyrings

Next time you run a program that wants to use the keyring you will be asked to type a password. This password doesn't have to be the same as your regular password.

If you are comfortable with a terminal, the command is:
rm -r ~/.gnome2/keyrings

Dec 10, 2009

Folder Bar in Nautilus

I just learned something today. If you go into Nautilus (the file browser for Ubuntu), you can see on the left there is a bar with your home folder, Desktop, Trash, etc. on it. Below that are a list of folders, probably Documents, Music, Ubuntu One (if you're running Karmic). You can drag and drop folders onto that list in order to access them quicker! The random little things you discover.

Dec 8, 2009

Computer Science vs. Economics For Me

A question people have asked me many times is why I went from Computer Science to Economics (although I'll note here that I actually did finish the CS degree instead of just transferring). It's a valid question, and I figure I'll answer it here.

Update: For those of you who are double-majoring in Computer Science and Economics, stay with it! If you go into an economics-oriented field, your computer skills will set you far apart from the hordes of computer-illiterate economics majors that graduate each year.
For those of you only studying economics, the best piece of advice for you is this: learn to use computers. And go beyond Excel, learn something sophisticated like SQL or R which are much more powerful.

And now, back to the article:
Here are my reasons:
1) While I like programming and working with computers, I found that working with them 40+ hours per week drained my enthusiasm for playing around in the off-hours. I would get home from work and not want to go anywhere near code. It was depressing. Now, even after doing stats and math all day long, I have no problem writing code at night when I'm done studying. So therefore I have decided that I would like to keep coding as a hobby, and do something else to earn a living.

2) More of the people I care about are interested in Economics, so they actually want to hear what I have to say about it (which isn't all that much right now, strangely enough). Some of my friends do care about computer stuff, in fact some of them even read this blog (hey guys! *waves*) but the vast majority of my friends and family don't really care too much about programming, computer science, Linux, etc. It's not that it isn't relevant to them, most of them find computers useful in some form or another, but when you compare questions like "why did I get a virus?" to questions like "why did I get laid off?" or "why did my investment portfolio bomb?" there is a much higher level of interest in the latter two. It's also a question of respect, I suspect that economists get a slightly higher level of respect than do programmers (although these days maybe not!).

3) I feel I can make a bigger contribution to the economic world than the computer world. It seems to me like a lot of the people doing things at the top in economics are just wanking around with math and saying that it reflects reality. I think it would be interesting to see what happens when you throw in more computer simulations using AI, or start wondering what happens when you mix computational complexity with game theory. For example, existence/uniqueness of equilibria matter and all, but the ability for a decision-maker/market to find said equilibria in a timely manner matters too.

4) The computer industry is governed more by economics than computer science (technology/computer science does play a fair-sized part mind you, just the part played by economics seems to be bigger). Why is Microsoft so big? Why is Java so popular? The answers to these questions are answered by economics as much as by technology.

Second Update (2015-06-18): It's been 6 years since I wrote this article, and probably 5 or so since that last update. That's enough time for some adventure and experience, which I'll share it here. I did a Master's in Economics, and worked in a number of different jobs: algorithmic trading, a successful Internet marketing startup, my own video game startup (briefly), and I'm now working at YouTube in San Francisco. This adventure is not over - I'm not even 30 years old. I know that in a few years if I decide to leave the Bay Area, I can go pretty much anywhere and not have to worry too much about finding work. There are very few disciplines that can boast this, but computer science is one of them. So here are some reasons why I am back in the tech world:

1) The jobs pay better. A good economist knows that utility increases with income, and therefore the rational choice between the two is to go into technology.

2) The jobs are more diverse. With economics you pretty much have two choices: a large financial institution, or a government. Unless you've built up some other skills elsewhere (business savvy, tech skills, social skills, entrepreneurship) then you've got limited options. With tech, you can do anything, anywhere. Software touches nearly every industry on the planet from farming to physics, and they are all looking for people who know how to make the computer do what they want.

3) It's easy to go from tech to economics; the reverse is not true. It wasn't very hard for me to get accepted to my Master's program in economics simply because I have an engineering background. Since graduate economics is all math and computers anyway, you're actually better off than the people who did their undergraduate degree in econ.

So while I found economics interesting, the only reason I would recommend that someone take economics over computer science is if they really liked economics and really didn't like computer science. Otherwise I'd suggest a major in computer science with either a minor or a double-major in economics.

Dec 4, 2009

Evaluate an expression in Vim

There might be a case when you need to write an expression and don't feel like calculating it yourself. If you're using Vim, then from insert mode you can hit Ctrl+R and then =, and you will then be able to type an expression to be evaluated. So suppose you hit:
<Ctrl+R>=5+2<Enter>
it will type out 7 for you. You can even do more advanced equations like:
cos(3.14159/2)
Unfortunately it is a bit limited, you can't do powers or logs. But it is handy for simple arithmetic.

Dec 3, 2009

Infinite Lists in Scala

One of the pretty cool things about Haskell is the ability to have infinite lists, thanks to Haskell's laziness. You can create a list containing all the Fibonacci numbers, or all the digits of π, or something like that.

Turns out that Scala can do it too, despite not being a lazy language. The main way to do it is with the lazy keyword:
lazy val hello = epicCalculation()

if (some condition){
println(hello) // epicCalculation() is done here
}else{
// don't use hello
// in this execution path, epicCalculation is never called
}
Here we're creating a constant variable called hello and only using it in one of our if branches. That means that if execution never goes into our if block, then the epic calculation is never computed. Pretty handy sometimes! This example isn't great because we can just call epicCalculation inside the if block, but whatever.
Unfortunately one catch is that the variable is evaluated when you pass it to another function, regardless of whether it is used or not:
object Main{
def getValue = {
println("getting value")
5
}
def output(y : Any) = {
println("outputting")
}
def main(args : Array[String]){
lazy val x = getValue
output(x)
}
}
Even though the variable y is not actually used inside output(), the value of x is still evaluated. So it isn't true laziness, but whatever.

We can also build infinite lists using the Stream class. Here's an example of generating a familiar list of numbers (I'd use Fibonacci but it is a bit more complex since each element depends on two previous elements instead of one):

object InfLists extends Application{
def numbers(current : Int) : Stream[Int] =
Stream.cons(current, numbers(current * 2))

// print out 10 numbers, starting with 1
numbers(1).take(10).foreach(println)
}
Yet again another trivial example, but it is kinda neat to be able to do this.

Note that the following does not work:
println(numbers(1) take 10)
Why not? Well here, we're not actually evaluating the values for the first 10 numbers. So when println goes to print the list, it just prints the first element followed by a ?, because it doesn't know what the rest of the list is.

Now if you actually want the Fibonacci series, you can do it like this:
Stream.cons(1, Stream.cons(1, fib.zip(fib.tail).map(f => f._1 + f._2)))
Swiped from here, converted to Scala. If you zip something with its tail, then you get a list of pairs that look like this: (ai, ai + 1). Then if you map that using +, you'll get your result.

Dec 1, 2009

CUSEC 2010

I'm fairly certain that anybody actually reading this blog and is able to go to this is already planning on going, but in the odd case that you are not I would highly recommend you check out CUSEC 2010 if you are at all interested in software development. I've been going for the last few years and it has been quite interesting each time. They've always had some great speakers including Tim Bray, Richard Stallman, Joey Devilla and Zed Shaw who usually give quite informative and inspiring talks.
This year the lineup is equally interesting, with folks like Reg Braithwaite and Matt Knox (guy who wrote some adware in Scheme, read article here).

So if you're interested in this kind of thing, are within reasonable travel distance from Montreal, and can handle the Canadian winter, feel free to come on out this January. The tickets are only $60 for students or $150 for non-students, which is ridiculously cheap when it comes to conferences. Plus Montreal is pretty sweet, so you'd definitely be having a good time.

Nov 20, 2009

Spellcheck in Vim

Turns out Vim 7 and higher has a built-in spellcheck. To turn it on, do:
:set spell
It's a bit annoying for code, but maybe it will help people spell things properly in their comments ;) It's also really good for LaTeX documents (if you're old school and do them by hand like I do) since those actually do have real writing in them which should definitely be spellchecked.

UPDATE: The suggestions for this spellcheck are quite odd...somehow it suggests for "homoskedasticity" you should put "insecticides". Strange.

Nov 18, 2009

Using sdljava with Scala

After fixing up sdljava, I decided to try it out with Scala. Turns out it works really well!

Here is the code to get a simple window that closes when you click the close button:
import sdljava.SDLMain
import sdljava.video.{SDLVideo, SDLSurface}
import sdljava.event._

object SDLTest{
def main(args: Array[String]){
SDLMain init SDLMain.SDL_INIT_VIDEO

try{
var framebuffer = SDLVideo.setVideoMode(800, 600, 32, SDLVideo.SDL_HWSURFACE)

var ev : SDLEvent = new SDLQuitEvent()
var loop = true

while (loop){
if ((ev = SDLEvent.pollEvent) != null){
if (ev.isInstanceOf[SDLQuitEvent]){
loop = false
}
}

Thread.sleep(1)
}
}finally{
SDLMain.quit()
}
}
}
This looks almost exactly like it does in C (I probably code Scala/Java with a C accent anyway). I might decide to start adding stuff to sdljava so that you don't have as much boilerplate as you do here, or I could even add in some Scala-only stuff to bind functions as callbacks or something. But I probably won't, sorry...

Anyway the hard part is actually getting this junk to compile. If you check out my sdljava repo here you can grab all the files needed for compiling and executing provided you're running 64-bit Linux, otherwise you'll probably have to download and compile the library yourself.
Drop those into the same folder as your Scala file, and do this to compile:
scalac -cp ./sdljava.jar MyFile.scala
That should work fine (if your code doesn't have any errors). The more annoying part is getting it to actually execute. Unfortunately it seems like when you use the actual scala program, it does not pass parameters to java. So you have to call java directly:
java -Djava.library.path=. -cp /path/to/scala/lib/scala-library.jar:./sdljava.jar:. MyClass
This is because sdljava uses native libraries which are not looked for in the classpath, you need to pass them in the java.library.path variable. Scala does not pass this to java when it is executed, so you have to call java manually.

Anyway, this should get you started to whatever SDL program you want!

Nov 17, 2009

Learning Scala: Euler's Method

I've been messing around more with Scala to see some of its more interesting features, two of which are pattern matching and currying. These are really handy in certain situations, and I decided to share them with you today.

The example I'm using is the Euler method for approximating a solution to a differential equation. This is probably not the most intuitive way of doing the Euler method, but it uses a lot of Scala's interesting features and I think it is a good way of illustrating them.

The Euler method works like this. You have a differential equation in the form y' = f(x, y) and an initial value (x0, y0). You calculate the slope at the initial value, which is f(x0, y0). You then take a step of size h in the x-direction following the slope, which brings you to (x0 + h, y0 + h * f(x0, y0) ). This becomes your new point, and you repeat until you are satisfied with the results. A smaller h will lead to more accurate results, but with more computation.

Here is the not-so-intuitive code:
object Euler{
def main(args: Array[String]){
// Use the differential equation y' = x^2 + y^2
val my_de = solve_de((x, y) => Math.pow(x, 2) + Math.pow(y, 2)) _

// Use a step size of 0.1, and do 10 steps
val generator = my_de(0.1, 10)

// try with different initial conditions
println(generator(0, 1))
println(generator(1, 1))

// create another generator with a smaller step size
val precise_generator = my_de(0.05, 20)
println(precise_generator(0, 1))
println(precise_generator(1, 1))
}

def solve_de(func: (Double, Double) => Double)(step : Double, iterations: Int)(x_0: Double, y_0: Double) =
(List(List(), List(x_0, y_0)) /: (0 until iterations))((s, i) => s match {
case List(res, List(x, y)) => {
val fxy = func(x, y)
List(res ::: List(fxy), List(x + step, y + step*fxy))
}
}).head

}
Look first at the definition for solve_de. It has three argument lists! One of them takes a function of type (Double, Double) => Double, also known as a function that takes two Doubles and returns a Double. The second takes a Double and an Int, and the third takes two Doubles.
The first parameter list takes a function representing the differential equation we are approximating, the second takes the parameters for the approximation which are the step size and the number of steps we take, the third takes the initial values.

If you look back at the main function, you'll see that we call solve_de with only one of its argument lists, and a _ at the end! This is called currying - it returns a new function with the first parameter list filled out. We save this as our variable my_de (note we use val, so this variable is immutable) which is a solution generator for our specific differential equation described above. You use the _ at the end to tell Scala that you are only partially applying the solve_de function.

Next, we call my_de with the values 0.1 and 10. This creates another function which solves our differential equation using a step size of 0.1 and 10 steps. We can then call this function with different initial conditions to get different solutions to the differential equation. Each time we call it, it returns a list of 10 points that lie along our solution curve. If we wanted to, we could then plot this curve using some graphing library.
Note: for some reason here you don't have to call my_de with the _ at the end, it is probably for some reason that I do not yet understand.

After that, we create a more precise generator with half the step size. I double the number of steps so that if we were to graph this alongside the first list, they would have the same range of x-values.

The next bit is the solve_de function, which illustrates some of the more interesting features of Scala. First one (which probably isn't that interesting) is that there are no curly brackets around the body of solve_de. If you have a function in Scala that is only one line, you can just write:
def foo(x) = ...
You don't need to include curly brackets.
We have a fold using the /: operator. If you've used inject() in Ruby then you'll know what I'm talking about, otherwise take a look here for a description of what fold (aka reduce1) is. In Scala you can write this:
(0 /: myList)(some function f)
This does a left fold of myList with the initial value 0, using the function f - aka f(f(0, myList[0]), myList[1])....
The initial value is a list that looks like this:
[ [], [x0, y0] ]
The first element of this list is where we will be sticking the approximated values, the second element is our current point.
We fold this list into (0 until iterations), which is a range equivalent to 0..(iterations - 1) in Ruby. This is a very interesting piece of Scala, because it shows some of the fancier features. Scala is a pure object-oriented language so the 0 there is actually an object of class Int. Effectively what we are doing is calling 0.until(iterations), which returns a Range object that we can use fold on. In Scala for a method which only takes one argument, you don't need to put the . or the brackets.
However there is no until() method for Int. Where does this until() come from? Scala has a feature called implicit functions, which are used for implicitly converting one type into another - like the auto-boxing between int and Integer in Java. Little do you know, there is actually a class called RichInt which supplies the until() method, and a bunch of other handy things (you could write 0 to 5 if you like). When you call until() on 0, Scala first looks to see if Int has an until() method. Since it doesn't, it checks to see if there is an implicit conversion for Ints into a class that does have an until() method. Since there is only one such class (RichInt), it automatically replaces your statement with something like toRichInt(0).until(iterations). If there were more than one implicit conversions however, then Scala would give you a compile error and you would have to explicitly provide your conversion. The main difference between this and auto-boxing in Java is that you can provide your own implicit conversions between any classes you like, provided they don't result in ambiguities.

The next step is to provide a function to the fold operator to use for folding. After the => we see
s match {
This matches the variable s (the "accumulator") against a set of patterns. This is another feature of Scala called pattern matching. This example doesn't really do it justice since we only have one pattern here, and it is just so that we can have a nice way of extracting the variables out of s without using head() and tail(). I think I might post something more detailed on pattern matching in the future. Anyway, we use the expression List(res, List(x, y)) to match s, and this extracts out our current accumulated values as res, and the current position into x,y. We can then compute f(x, y) and put it in fxy (this is to save some time in computation) and then return:
[ new res, [ x + step, y + step * f(x, y) ] ]
The new res value is just res with f(x, y) stuck on the end (that's what the ::: operator does, it concatenates two lists).

Two small syntactic things to note:
- There is not a single semicolon in this program. Scala doesn't need the semicolons at the end of lines, although you can include them if you like.
- There are no return keywords in this program, even though we have functions. Scala doesn't require the return keyword, it will insert it where it thinks you are trying to return something.

So I'm not sure if I'd recommend you actually write Euler's method like this, instead you would probably write it something like this in Scala:
def solve_de(func: (Double, Double) => Double)(step : Double, iterations: Int)(x_0: Double, y_0: Double) = {
var x = x_0
var y = y_0
var res = List[Double]()

for (i <- 0 until iterations){
val fxy = func(x, y)
x = x + step
y = y + step * fxy
res = res ::: List(fxy)
}
res
}
However in this case, you wouldn't be able to use all those fun little toys that Scala gives you, so I did it in a different way.

1In Scala fold and reduce are two different things: fold takes an initial element, where reduce uses the first element of the list as the initial element. Reduce will throw an exception if used on an empty list, where fold will just return the initial element. In non-Scala languages, reduce and fold are the same thing.

Nov 16, 2009

Dusting Off sdljava

I've been wanting to use Scala for little graphical apps fairly often these days, however the major issue is that doing any basic graphics operations with Java seems to be a pain in the ass. You have to go through AWT, which is a big mess. In fact, I can't even do a basic plot pixel! (If I'm wrong, please correct me.)

So I looked up sdljava, which is a port of SDL to Java. They pretty much just wrap up the C calls using JNI, and put some nice little objects everywhere. It's a bit cleaner than regular SDL using C.

However after trying it out, I found that it crashed all the time. Which was pretty lame. So I decided to roll up my sleeves and make it work. After a little while, I realized that what was causing it to crash was some pointer tricks generated by SWIG, which is an interface generator for C/C++ code. I fixed up some of these pointer tricks and voila! It works again.

I was initially excited and was planning on notifying the maintainer of sdljava, however since there haven't been any commits to the repo for about 4 years, I figured that it has been abandoned. I forked the CVS repo, converted it to git (since I don't know how to use CVS and don't really feel like learning it) and have put up my own repo of it on Github here if you are interested in checking it out. I don't know if I will be actively repairing things, more just fixing things as they come up (since for the most part the system is complete).

Update: At some point I removed the sdljava repo, forgetting about this link. Sorry about the inconvenience.

Nov 15, 2009

Welcome to Karmic

Last year around this time I did a a little review of Intrepid. I forgot to do one in May about Jaunty, so I'll make up for it by doing one now about Karmic.

There's been some negative press about Karmic, saying it is really buggy and incomplete. Others haven't had too many problems, and say that the negative press is unfair.

One small note: there are bugs with every new operating system release that I have used. Ever since I installed Windows 95, I have seen issues with new operating systems. They don't always work the same as before, they change things, or introduce new bugs, etc. This is normal. Surprisingly enough, I have been impressed with past Ubuntu releases (Feisty and Jaunty come to mind) where things actually upgraded really smoothly - in fact the move from Edgy to Feisty is what really sold me on Ubuntu, it was a really great upgrade. Before that I was still using XP as a primary with Edgy for messing around.

My issues with Karmic haven't been terrible. My network still works, so does my sound. The issues are more with papercut-like things. There are little things that come up that are not giant failures (I remember back in Gutsy when you wanted to install the Nvidia drivers you had to kill the X server - see my post about it) but are annoying little things that you kinda go, "well that's annoying." A small list of them:
  • Flash doesn't recognize mouse clicks - annoying for watching non-autoplay Youtube embeds, or using Google Analytics. A fix can be found here.
  • Suspend/resume and compiz - sometimes when I come back from suspend compiz crashes. Slightly annoying since I'm using Avant Window Navigator which depends on compiz's livelihood. It's not a big deal, it's just annoying to have to go to Preferences->Appearance and start compiz again.
  • Empathy - I posted about this here already so I won't go into details again, I just find that Empathy doesn't really offer anything over Pidgin. This is not a papercut, more just a "why did they do that?"
  • System Tray Icons - sometimes system tray icons look really weird, and there's a bunch that don't make sense. I have one that says my network cable is unplugged, even though it is quite obvious that it's not since I am on the Internet as we speak. Also some of the icons are messed up, they show a solid grey square behind them instead of the gradient image I have for my GNOME panels. Again this one isn't a huge issue, just ugly.
  • Bluetooth Applet - my fix no longer works since they changed the Bluetooth applet. It's nice that they show a system tray icon for Bluetooth, unfortunately there's a bit of confusion. When I right-click, it says "Bluetooth: On" and has a menu item to disable Bluetooth, but when I go into the Preferences panel it says Bluetooth is disabled and has a giant button to turn it on that doesn't actually work for me, when I click it the button goes grey and nothing else happens. Unfortunately the dialog has nothing else than that except an option to stop displaying the system tray icon. So I'm not really sure what to do here, if I fix it at some point then I will blog about it.
  • Wine is weird to install. See my fix to install it.
I'm sure there's more (boot time isn't slower, but feels slower since GNOME seems to take longer to start), but I don't really want to bore people with a list of my problems.

What has gone right with Karmic? Well, not a whole lot I think. It isn't horribly broken, which is nice. Ubuntu One is really useful, and I think the Ubuntu Software Centre is a lot cleaner than the Add/Remove programs that used to be there. Also it upgrades to Firefox 3.5, which is a bit speedier than Firefox 3.0.

All-in-all, I wouldn't say Karmic is a bad release, I just don't really see much point in using it. If you really want Ubuntu One then it is good, but other than that if Jaunty is working for you then you should probably just stick with that.

Nov 14, 2009

Installing Wine in Karmic

Each time you upgrade Ubuntu you have to tweak your Wine repo for the newer version. This is not a huge problem, you can just follow the instructions on Wine's site which are nice and clear.

Karmic seems to be a little weird with this. The issue comes when you want to actually install wine through Synaptic. You can't install the package "wine" anymore, you get an error like this: "wine: Depends: wine1.2 but it is not going to be installed". You can fix this easily by just installing the wine1.2 package, but it is kinda weird that they did not set up the dependencies properly. Oh well.

So in short, to install wine:
1) Follow the instructions to add the Wine repository.
2) Go to System->Administration->Synaptic Package Manager
3) Search for wine1.2
4) Install the wine1.2 package by double clicking it. Accept any dependencies it asks for.
5) Click Apply.

Nov 13, 2009

Accented Characters in Ubuntu

A long time ago I set up my computer so that it was easy to type accents, but with the recent Karmic release the settings are gone and since I forgot how to set it up. Now that I've rediscovered it, I can not only reset it but also blog about it!

So suppose you want to type French characters like é, ç, etc. In Ubuntu, you can activate these with the following steps:
1) Go to System->Preferences->Keyboard
2) Click the Layouts tab
3) Click the button saying Layout Options
4) Click the triangle next to the Compose key position
5) Choose which compose key you want. I use Right Win.

After that, you should be able to use the accented characters. You can do this by first pressing your compose key, then your accent (not at the same time!), and then the character. For example to do a é, I would hit the Right Win key, then the ' key, then the e key.

You can do special characters too, like æ, but those ones usually you have to figure out on your own (for those interested, that one is Right Win, a, e).

Nov 12, 2009

sizeof(int) != sizeof(int*)

I've been messing around with something called RUDL, which is supposed to be a more Ruby-like port of SDL than Ruby/SDL. This is a pretty good idea, it worked really well with pygame in my opinion, and so I would like to see this kind of thing in Ruby.

Unfortunately the coders for RUDL have done something that is extremely annoying. In some parts of the code, they assume that ints and pointer types are compatible. This is true in a 32-bit environment, since ints and pointers are both 32-bit, however in a 64-bit environment this is not the case. An int is still 32-bit, but pointers are 64-bit. This means that if you are converting from a pointer to an int back to a pointer (or something of that nature), things will not always behave as you expect.

I've seen this kind of thing before, and the situations in which it arises can be subtle. One time I saw someone using the varargs() system and they passed in 0 as a pointer value. This is fine in a 32-bit system, but with the untyped nature of the varargs stuff gcc was only zeroing the lower 32-bits of the value. So setting the pointer to 0 was not actually setting the pointer to zero, so later on when they did if(!pointer) it didn't work and caused the system to crash.

In RUDL the issue is less subtle. Within SDL's audio mixing system, you can label groups with an integer. So the system is using pointers to index these groups. In a 32-bit world this works fine since there is a 1-to-1 conversion for pointers to ints. Unfortunately in a 64-bit world, your pointers do not have a 1-to-1 conversion, so sometimes you will get conflicts and things will act weird.

So yes, if you can avoid it, do not treat an int like it is a pointer! This will make your code non-portable. And even if you do decide to do this, please heed your compilers warnings! gcc does give out warnings for this scenario, and you should listen to them.

Nov 8, 2009

flot

Recently I've been working with a Javascript library called flot, which does graphs purely in Javascript. It's a pretty handy tool for plotting graphs without resorting to Flash or Java - it's pure Javascript. Not only that, but it is really snappy and looks good! There's a cool example here that shows both the graphing capabilities and interactivity.

It's not as powerful as Flex by any stretch, but unless you need fancy morphing of graphs or things like that flot is just fine for you. And you have the benefit of not having to use Flex/Flash to do it!

Nov 6, 2009

Case Classes in Ruby

After working a bit in Scala and using the case classes and pattern matching, I've found it's pretty nice to have and it would go well in Ruby! So I've whipped up a little something basic, you can check out the Git repo here: http://github.com/robbrit/ruby_case_classes

What are case classes? They're basically simple classes that don't really do much other than store a set of values (possibly, they don't even need to do that). They're good on their own if you have very basic classes that you want to use, for example exceptions.

With my code, you define a case class like this:
case_class :ClassName, BaseClass, <parameters>
The parameters you pass at the end are the fields of the class and when you instantiate the object you can set them:
case_class :Hello, Object, :title
...
h = Hello.new(:title => "some title")
h.title # => "some title"
Unfortunately at the moment you have to specify the name of the field.

Case classes are handy on their own to save a few keystrokes, but where they help a lot more is with pattern matching. Pattern matching comes from functional programming and was adopted by Scala. It is a handy feature to have, so that's why it is here.

To do pattern matching:
case_class :Blah, Object, :title
case_class :Blih, Object, :title
...
blah = Blah.new(:title => "hello")

match blah do
# Type matching
for_case Blih do
...
end

# Guard and type matching
for_case Blah, :title => "hello" do
...
end

# Just guard matching
for_case "_", :title => "hello" do
...
end

# Multiple guards, one of which is a method call
for_case "_", :title => "hello", :to_i => 0 do
...
end

# Match anything - you could put "_" here, but it's optional
for_case do
...
end
end
Here we have four examples of things you can match against. You can match against just a type, you can match against a type with a guard, you can match against just a guard, and you can have a "default" case if it doesn't match anything.
Guards are basic equality conditions that you can use to match against. If the object fits the guards, then the block is executed.
Note there is a fall-through here. The case that is executed is the first one to match the object, later ones that match are not executed.

Some possible improvements:
- match against values instead of just types. For example:
for_case [] do ...
- inequalities instead of strict equalities in guards
- case class new() doesn't need named parameters
Any other suggestions are welcome.

There's one bug (that I know of). It doesn't work at the top-level. So if this is your entire file, it will fail:
include CaseClasses

case_class :Blah, Object
You need to wrap it in some object. I'm probably having a brain-fart or something as to how to fix this, but for now you'll have this limitation. If anybody has a solution around that, feel free to let me know.

Nov 3, 2009

Empathy Fail

The recently released version of Ubuntu, 9.10, comes with a new default messenger program called Empathy, which replaces the venerable Pidgin. This is great, I'm not a huge fan of Pidgin for anything other than simple chat like IRC or Google Talk. It doesn't support video/audio chat, it has issues with synchronizing MSN contact lists (or at least it did, I haven't used Pidgin for MSN since, well it was still called gAIM last time I used MSN with it), and it looks like someone like me designed the GUI for it (it is very bare and plain).

Next, I want to compare it with aMSN, which is what I use for MSN. If it supports simultaneous audio/video chat, then I'm sold (provided there aren't any showstopper bugs). aMSN doesn't do this, so if I want to talk to my parents on the other end of the country (who don't have Skype) it is either with audio or video, but not both.
Also aMSN is done with Tk. While the developers have done a great job making it look decent, it's like putting makeup on an 80 year old. They still look like they're 80. So I'm hoping for something that looks a little more modern.

So now I'm trying out Empathy to see why it is so much better. It supports video/audio chat, which is great. First step - it offers to import my Pidgin accounts. Nice! I'll try that. Unfortunately it doesn't work, and I really have no idea why.
I also can't seem to add any accounts to it other than ones that don't require a password - namely IRC. This is because when I create the account it asks for my keyring password which I don't know, since I don't use Ubuntu's keyring. Since I didn't provide the keyring password, the thing decides it doesn't want to create the account. UPDATE: If you need help resetting your keyring, you can follow my instructions here.

In effect, Empathy isn't much more to me than an IRC client. I'm sorry, I'll just continue to use Pidgin.

Nov 2, 2009

Stats App Revisited

A while back I mentioned that I was working on an addon for OpenOffice called ooregress, which adds some more advanced statistics functionality to OpenOffice Calc.

Unfortunately when I wrote it, it was for OpenOffice 2.4. I think they changed the API for OpenOffice 3.0, so my code no longer works.

Rather than try to repair my code, I decided to port the thing to Javascript so that I could release it as an easily accessible website. The issue with the original version was that it was written in JRuby so you would need JRuby actually installed to run it, and it needed the development files for OpenOffice installed on the machine (not to mention OpenOffice).

This one is in a website, so all you need installed is a supported web browser. Since this is a personal project, IE is not a supported web browser :P I am currently supporting Firefox, Chrome and Safari - I can probably support Opera too, I'd just have to install it.

A temporary link is here, although I won't guarantee that this will always be active. I've got it hosted on my SheevaPlug, which I turn off sometimes. UPDATE: The site is now statsan.com, and I've called it StatSan for STATiStical ANalysis.

I'm planning on putting all the code as open source (GPL) so that anybody can host their own version of it should you want to, I'll put up the link once I get something set up - I've got a local git repo of it, it's just a matter of creating something on GitHub and pushing everything over.

Anyway, why am I writing this? Well, a few reasons. First, my stats classes at school have required various stats software, including Excel, EViews and Stata. I've also tried R. For the most part, these programs fall into one or more of the following categories: tedious, hard to use, expensive, not cross-platform. I want to make something that is more streamlined, easy to use, free, accessible and portable. You don't get much more accessible and portable than JavaScript, since the vast majority of people have something installed that supports it to some degree or another (however I guess this isn't 100% portable since I'm not supporting IE).
There is one downside: JavaScript is slow. I'm sure if you were running a regression on 20 variables with hundreds of thousands of observations, it would make your computer cry. This is an issue that I've been thinking about a bit and will hopefully come up with some solution for (if that native code plugin that Google is talking about ever comes out, that would be awesome!).

So if you have to do stats calculations, feel free to check out this app and let me know what you think. If you find any bugs or come up with any features you'd like, feel free to let me know.

Oct 31, 2009

Javascript Dataset Speedup

I've been working on a little project and I noticed it was loading very very slowly when working with large datasets (large being ~6000 numbers, so it is "large"). After doing some digging, I realized that it was because I was rendering my objects as Javascript objects:
<%= @workspace.as_js %>
Since the objects had some very large arrays in them, it took quite some time for the browser to parse that Javascript and load it in.

However, the weird part was then when I'd use Javascript code to process these arrays once they were loaded, it was nice and speedy.

The solution was simple: don't put the objects as Javascript. Parsing it is slow.
Initially I decided I would put the data in some raw form inside a <div> and then process the innerHTML. However that also turned out to affect the rendering time of the page (surprise surprise).

My final solution was to load the data from an AJAX action. This way the rendering time of the page is snappy, and there is not a lot of Javascript to parse.

The raw format is pretty simple. It is a set of name-value pairs separated by pipes (|), and each element is separated by colons - as I write this it might make more sense to use a URL formatting with & and =. The datasets themselves are arrays, so the name is preceded by a @ and is stored in the vars subfield of the object that I'm loading in. Here's the code:
<script type = "text/javascript">
var workspace = {vars: {}};
$(function(){
$.get('data_set_url', {},
function(response){
$.each(response.split("|"), function(i, obj){
if (obj.substring(0, 1) == "@"){
// dataset
obj = obj.substring(1).split(":");

workspace.vars[obj[0]] = $.map(obj[1].split(","), function(obj2, i2){
return parseFloat(obj2);
});
}else{
obj = obj.split(":");
workspace[obj[0]] = obj[1];
}
});
}
);
});
</script>
Even with the AJAX, this turns out to be much faster than putting the data as Javascript (or JSON), or putting the raw data in the HTML.

Oct 25, 2009

Scalability Presentation Slides

Someone requested that I put up my slides for the scalability presentation I did the other day, they are available here.

Unfortunately they are only the slides, the presentation was not recorded. If anybody has questions about a certain page, feel free to ask here. I'd prefer if people ask via comments, so that others can see the answers if they have the same question.

Oct 20, 2009

Scalability Presentation

I'm giving a presentation at Bishop's University in Sherbrooke on Friday at noon if anybody is interested. It's called "Managing Millions: Tips and techniques for building high traffic web applications." It will talk about some of the problems that we faced back when I was working on a high traffic website like various database bottlenecks, issues faced when using cache, etc. plus the solutions we used on how to fix them.

Some of it will cover some techniques that we researched but didn't really employ, so I won't be going into as much detail on those. For example, doing asynchronous processing (aka offline processing) which in hindsight I don't think we didn't do as much as we should have and the stuff we did do wasn't very elegantly handled - cron jobs are decent but when you aren't sure how long the script will take you have to do some hacks to make sure that you don't have too many running at once, etc. There's better tools out there for this.

Oct 10, 2009

Dual Head and Sauerbraten

Normally when I play fullscreen games in Linux it is really annoying, since they tend to display in the middle of the displayable area: that is, half of the game on one monitor, and the other half of the game on the other monitor. It's slightly annoying, although I just disable one of my monitors when I go into a fullscreen game.

However I discovered today with Sauerbraten that it seems to map itself across the entire viewable area. So I end up having the game spread fully across both monitors. It's actually pretty sweet, and if you have dual head and Linux set up, I suggest you try it out.

Oct 7, 2009

Multiple Inheritance in Scala vs. C++

I've made an interesting discovery today on how Scala's type system is slightly more awesome than C++'s type system. It has to do with traits.

There's a C++ project I'm working on where I'm pulling out some shared functionality from a few classes and putting it into another class that will be used like a mixin (one reason to learn a language with mixins - you get better ideas when you go back to languages that don't have them). It's fairly simple: I have a base class Creature, and I have a number of classes which inherit from Creature to define some specific behaviour. Two examples are Giraffe and Tiger. These two subclasses have very different habits when it comes to how they find their food, however there's some basic functionality involved in the mechanics of eating and starvation and stuff that is common. So I put that basic functionality into a class called Hungry:
class Hungry{
...
public:
void act();
};
In the act() method, it makes the creature slightly more hungry and if it is too hungry, it makes the creature die. Unfortunately, the Hungry class has no idea what die() is. So I could have Hungry inherit from Creature as well, however that will cause diamond problems when we start getting into many different traits for different creatures. The next step was to try putting die() as a pure virtual method in Hungry. Unfortunately that didn't work unless I explicitly defined die() in both Giraffe and Tiger, which would be annoying since die() is specified in Creature and works just fine. With that, the code ends up like this:
class Hungry{
...
public:
...
void act() {
if (--hunger <= 0)
die();
}

virtual void die() = 0;
...
};

class Giraffe : public Creature, public Hungry{
...
void die() { Creature::die(); }
...
};
This is really a minor detail, but slightly annoying. Shouldn't it be able to figure out that there is one concrete implementation of die() in Giraffe and Tiger?

Turns out that in Scala, it does figure this out:
class Creature{
def die(){
...
}
}

trait Hungry{
def act() = {
hunger -= 1
if (hunger <= 0)
die
}

def die // abstract method

var hunger : Int = 0
}

class Giraffe extends Creature with Hungry{
override def act() = {
super[Hungry].act
}
}
This works perfectly. It is type-safe too, if you remove the "extends Creature" from Giraffe, it will give you an error saying that you haven't defined die.

I'm confused, why can't C++ do this? This doesn't really have anything to do with the fact that Scala is using a trait, because if you made Hungry into a class and allowed Scala to inherit from multiple classes, it wouldn't be any different. The fact of the matter is that Giraffe and Tiger do have a die() method, but the C++ compiler isn't recognizing this and saying that they are abstract classes.

Oct 6, 2009

Multiple Method Inheritance in Scala

I had a little issue today that likely crops up once in a while when using Scala's traits. I have a class with a particular method foo(), and a trait with a method of the same name. When I created a derived class that used this trait, I didn't know how to specify which version of foo() I wanted to call. Let's take a look at our picture:
class Blah{
def foo(){
}
}

trait Tleh{
def foo(){
}
}

class Dingle extends Blah with Tleh{
override def foo(){
super.foo // this calls Tleh.foo
}
}
As I mentioned in the comment there, using super.foo calls the trait's version of foo(). But what if you want to call Blah's version of foo()?

It's actually pretty simple, and a bit intuitive if you know a bit of Scala:
class Dingle extends Blah with Tleh{
override def foo(){
super.foo // calls Tleh.foo
super[Blah].foo // calls Blah.foo
super[Tleh].foo // calls Tleh.foo
}
}
This is pretty simple, but it took me a little while to figure it out since there doesn't seem to be any mention of this in the docs I found!

Sep 29, 2009

The Cat's In The Bag

Time for another random cat picture:

Sep 23, 2009

Plumbers and Lawyers

I heard an interesting joke the other day in one of my economics classes:

A lawyer one day has a problem with his sink, and has to call in a plumber to fix it. The plumber shows up, fixes it in 20 minutes, and says, "that'll be $200."

"What?", replied the lawyer, "you only worked for 20 minutes! That means it is $600/hour! I'm a lawyer and I only charge $250/hour."

"I understand your pain," said the plumber, "I used to be a lawyer too."

Sep 22, 2009

Compiz Effects Speed

I found this little tutorial here which tells you how to make Compiz's effects look much nicer, just by upping the refresh rate to that of your monitor.

And well, it really really works. Everything runs much more smoothly - of course you'd need a video card that can handle it, but it will probably work on most decent ones these days.

My question is, why does Compiz default to 50Hz when the average monitor runs much higher than that?

Sep 20, 2009

Prices of Different Liquids

I stumbled across this post through a friend today, it is very interesting. I decided to actually dig a little deeper and see if these things are as they say they are.

I was unable to find a price on human blood, penicillin, and the 3M PF-5030 (which is a refrigerant), but I got a price on everything else. Note that all of this is done using CAD and prices as of today (Sept. 20, 2009).

Crude Oil:
Using the price from here (Bloomberg, converting USD to CAD using Google) of $76.63 CAD per barrel, and the volume of a barrel (Wikipedia) of 158 987 mL you get $0.00048/mL.

By the time that gets to your car, if we use $1.00/L which is what it seems to be hovering at in Quebec, that's $0.001/mL.

Bottled Water:
This one I guesstimated the price of a bottle of water at around $1.60 for a 500mL bottle, so that makes $0.0032/mL - 6.67 times the price of the crude oil.

Red Bull:
Also guesstimated at $2.99 for a 250mL can, giving $0.012/mL.

Vodka:
Using the price of the most expensive one at the SAQ called Belvedere (SAQ) which gives $42.75 for a 750mL bottle we get $0.057/mL.

Finally (since I skipped a bunch) we have HP #45, which is their normal black ink. The price is $42.77 (NCIX) for a 42mL cartridge, giving $1.02/mL.

So printer ink is 1020 times more expensive than the gas you put in your car. Yikes!

Note: These numbers are not 100% accurate since some of these have taxes factored in and others do not.

Aug 28, 2009

Interfacing between JRuby and Scala

One of the major features of JVM languages is their interoperability. It's really easy to call Java code from a JRuby script, and (while slightly less easy) you can run a Ruby script from within a Java program.

However, how difficult is it to interface between two non-Java JVM languages? It turns out that it isn't really that hard at all! For this post, we'll talk about how to interface between JRuby and Scala, it's really rather simple.

So here's our scenario. We are writing our main app in Scala, because Scala is faster than Ruby. However there are some situations where we don't want to have to recompile the whole thing to make changes, and we want the code to be nice and easy to understand for people who don't know Scala (admittedly, Scala is not a good first language as it is rather complex). We're going to embed some Ruby scripts within our app.

Here's a basic Scala outline in ScalaTest.scala:
import javax.script._   // import Java's scripting API
import java.io.FileReader

object ScalaTest extends Application {
var engine = (new ScriptEngineManager).getEngineByName("jruby")

engine.eval(new FileReader("test.rb"))
}
And test.rb:
puts "Hello from Ruby!"
Before we attempt to run this though, we'll need to get both JRuby, and the JRuby engine. You can grab JRuby from their website (as of this writing the latest version is 1.3.1), just extract that and grab jruby.jar from the lib folder. As for the JRuby Engine, you'll have to grab the big engine tarball from the Java site and extract jruby-engine.jar from the jruby/build folder. Once you have those put them in the same folder as your code, then compile and run:
scalac ScalaTest.scala
scala -cp .:jruby-engine.jar:jruby.jar ScalaTest
What you should see is "Hello from Ruby!" pop up on the screen.

So this is pretty cool, but how do we get information between Scala and Ruby? That part can be a little bit tricky since the type systems in Scala and Ruby are very different. However for our example here, we will stick to simple things. Here is our new Scala code:
import javax.script._
import java.io.FileReader

object ScalaTest extends Application {
val engine = (new ScriptEngineManager).getEngineByName("jruby")

val name = Console.readLine("What is your name?\n")

// assign a variable in the engine
engine.put("name", name)

// cast the result of the execution into a string
val colour = engine.eval(new FileReader("test.rb")).asInstanceOf[String]

Console.print(name + ", your favourite colour is ")

// this part is unnecessary, but let's have some fun with colours :)
Console.print(
if (colour.toLowerCase == "blue")
Console.BLUE
else if (colour.toLowerCase == "red")
Console.RED
else if (colour.toLowerCase == "green")
Console.GREEN
else
""
)

Console.println(colour + Console.RESET + ".")
}
And the Ruby code:
# the name variable is passed in as $name, which is a String
puts $name + ", what is your favourite colour?"
gets.strip
While this is a trivial example, we could use this to embed all sorts of functionality within our application. In fact, this isn't even limited to JRuby. By changing the getEngineByName call to say, "Javascript", we can execute Javascript code instead of Ruby code. Or any other JVM language included in that big engine tarball, including Scheme, Jython, and Groovy to name a few. You can even use Java as a scripting language, although you have to jump through a few hoops since Java doesn't support global variables.

Aug 16, 2009

Lisp and Code Mutation

So exams for summer courses are done, and I have no job. I do have a small contract, but other than that I have plenty of time on my hands. So I'm working on a tiny little idea I had (if you've been following this blog for any amount of time I tend to get a fair few of those).

One thing I've always found interesting about Lisp (for this article we can assume I'm talking about Common Lisp) is the homoiconicity. Well I don't know enough of the language to comment on its practical nature - I've been having a nightmare of a time getting into it, documentation online is not great and there seems to be a lot of simple things (like string padding) that aren't there, or at least not that I can find - some of the things you can do with the language are downright amazing. Ruby likes to boast that it can do metaprogramming, but it is nowhere near as powerful as what Lisp can do.

Anyway, enough on the impressions of the language (I'm a language snob, I know). Let's get to this idea. A friend of mine who is doing her master's at University of Calgary in swarm theory was out visiting about a month ago for a conference on genetic algorithms and evolutionary computing called GECCO. So of course, we ended up talking about the subject. She said that with much of the current research, what they do with genetic algorithms is they put a set of inputs into a program, and then let the program find an "optimal" version of those variables. I thought this was interesting, but limited. See, the variables will change, but the code? Nope, it will stay fixed. The algorithm doesn't change, just the variables within it. Now when working with large neural networks this isn't bad at all, but sometimes maybe the program is the thing that should be changed and not the inputs.

So it got me thinking. In Lisp, because of homoiconicity, code is data and therefore can be manipulated as data. So what if you wrote a program that took as an input another program and a specification, and would alter the program in random ways and check if the program still matched the specification? It would be very interesting to see what would come out.

An even more interesting thing is this: what if the specification was incomplete? If it is too incomplete, the program might not even work anymore. However if the specification just checks the important bits, it's possible that a program outputted by the program re-writer could do some interesting extra things outside of the specification. Most likely not, but it could happen.

Finally as a closing note, there will be programmers who will point out that you could write a program re-writer in any language. It's just the nice thing about Lisp is that you can read in and parse the program by going (read input), which returns an s-expression representing the code that was just read in. It is trivially simple. Then on top of that you can manipulate this s-expression all you like, and then output it to a new file by going (pprint sexp out) and you have an output which is still Lisp. So the whole time, you're working with a single language and there is no real need for any intermediate forms.

Aug 14, 2009

Actor Simulation Synchronization Issues

A while back I did up a post about using Scala actors in a synchronized simulation. I'm hoping to use this to model a simple economy, however I've run into a major problem and I'm trying to find a solution to it - which is why it took so long for me to write a follow-up to that last post.

The way the other simulation worked is by using synchronized time steps. Normally this is good because it means that actors which take varying amounts of time to process can all finish their job before the next time step starts. This also helps when you decide to scale and use a distributed system, you will end up having slower machines and network delay, but can still maintain synchronization because the faster machines will either do more work, or will wait for the slower machines.

Synchronization in my simulation was done using the Act and DoneAct messages. When an agent receives an Act message, it means that the next time step is starting and that they should begin processing. Then when they are done, they send a DoneAct message to the simulation driver to say that they have finished processing. The problem with this is that some agents may send messages to other agents that are finished. There is the scenario that an agent, A, sends a message to another agent, B, and then A sends the DoneAct message to the simulator. However B may well still be processing away the message, which may in turn affect agent C, which may then wrap around and affect agent A. Effectively this could turn into an infinite loop, and due to the nasty halting problem there is not really any general way of detecting this.

I have a solution, however I am not sure if it will work. Basically agents cannot communicate with one another if they are using the ! approach (asynchronous messaging). They can use the ?! method (synchronous method) where when agent A sends a message to agent B, agent A will wait for agent B to return before continuing execution (basically the same as a method call in traditional OO languages like C++ or Java). However in the case where agent A wants to send an asynchronous message to agent B, it can do so by sending a Delayed() object to the simulator, which will then send the message to B at the next time step.

Basically I'm thinking out loud here, hoping that by explaining things here it might give me some insights on how to go about building this thing. If anybody is interested in this type of thing and wants to give their two cents, feel free to comment :)

Aug 13, 2009

Confessions on Retirement

Back in June I announced that I would no longer be working as a programmer, at least temporarily.

That is not entirely true, and over the last little while I've come up with a few scenarios where I will be writing code:
  • Open-source software - I still have no problem with submitting patches to open-source software, or creating my own open-source works. While I don't believe that users of open-source software should be required to give back to the community, I do think that if I'm using the fruits of all their hard work, it's only fair that I give something back now and then. But that's a personal philosophy, and I will not tell others to do the same.
  • Coding for myself - this has a bit of overlap with the previous one, since contributing to open-source helps fulfill some personal obligation or whatever you want to call it, or it helps improve the tools that I use every day, so you can say that any open-source contribution I make is basically coding for myself anyway. However this point is more for non-open-source projects I may undertake. For example, if I should decide to start my own business that uses some kind of software, it's likely that I will be writing some code. Should the healthiness of the business depend on others not having the software, then I will likely keep it proprietary. However patches and tweaks I make to open-source software that I use would be contributed back (well, they'd have to be if it were a GPL'd library).
  • Small Contracts - this is the main confession I have to make. I don't like coding sites for other people's business, mainly because they usually aren't a business I'm really all that interested in, nor do I have much stake in the gains of the business. However at the moment I am a student. Other people have money; I do not. So small contracts are a good way to get a few bucks on the side to help pay for food and rent. Since people giving out small contracts usually don't care too much whether you like their work or not provided you do a good job, there isn't really any issues with my actual sentiments towards the work.
So those are my confessions, sorry for the misleading post saying that I was retiring from coding - a few people who know me expressed shock at that announcement. I still am sorta retired since I won't be doing any full-time jobs in software (unless it is for myself), however I'm not totally out of the biz.

Aug 1, 2009

Ubuntu's Hibernate Won't Wake Up

UPDATE (Feb. 14, 2010): This is not a way to get hibernate working on Ubuntu. This is a way to make your computer able to boot after you've accidentally put it into hibernate and it won't wake up anymore.

So I made the mistake today of clicking "Hibernate" from the shut down menu. It went into hibernate just fine, just a few hours later when I wanted to reboot it didn't work. I just got to a black screen with a flashing cursor in the top left. I figured it was just being slow, so I went to make breakfast. Came back, still in the same state.

After digging through forums under Windows and trying to find a good way to fix my problem, I ended up with the following solution, if you ever find yourself in this situation:

If you're using GRUB1 (if you haven't installed Ubuntu from scratch since Oct. 09)
1) Hit Esc if you need to access the boot menu, otherwise it should pop up normally.
2) Go to the normal Ubuntu item (usually the one that is selected by default) and press "e".
3) Go to the line that starts with "kernel" using the arrow keys and press "e".
4) Remove the "quiet" and "splash" from the end, and add "no_console_suspend".
5) Press Enter, and then "b".

With GRUB2:
1) Hit Esc if you need to access the boot menu, otherwise it should pop up normally.
2) Go to the normal Ubuntu item (usually the one that is selected by default) and press "e".
3) Go to the line that starts with "kernel" using the arrow keys and press "End".
4) Remove the "quiet" and "splash" from the end, and add "no_console_suspend".
5) Press Ctrl+x to boot.

This should get you back into Ubuntu, however it will not be resumed from the previous state.

Do not use hibernate with Linux unless you're either certain it will work, or you know how to fix it if it doesn't work. I personally have never seen it work on any distribution in the 5-6 years I've been using Linux. You'll end up with far more headaches than it's worth.

UPDATE (Sept. 5, 2009): I have used suspend, it works fine.

Jul 31, 2009

Rocketfish Bluetooth Dongle in Ubuntu

UPDATE: This does not work in Karmic as the Bluetooth settings dialog has been changed. This will still work in Jaunty if you have not yet upgraded.

I recently bought a Rocketfish Bluetooth dongle for my computer so that I can see about hooking up the Wiimotes to it. Unfortunately it didn't work out of the box, but it was detected by lsusb. Here's how to get it working.

First, see if it is detected when you plug it in. The little light should be on, and if you go into a terminal (Applications->Accessories->Terminal) and type:
lsusb
You should see a "Primax Electronics, Ltd" and/or "Broadcom Corp." (I get 3 of these) in there. If you've gotten this far, good. Otherwise it is possible that your adapter is broken, try testing it on another computer.

Second, you need to reset the device, not sure why. In the terminal type:
sudo hciconfig hci0 reset
sudo hcitool dev
This should print out a list that looks like this:
Devices:
hci0 00:02:76:17:91:42
This should make the device work.

If you go into System->Preferences->Bluetooth you should now be able to set up your Bluetooth devices. Click the "+" button, which will go through the little wizard. On the "Device search" page, wait a second while it detects the devices (theres a few little usability issues in the Bluetooth thing, supposedly the fixes are in for Karmic) and you should see your other Bluetooth device(s) pop up in the box. After that theres usually some kind of pairing system that you'll need to do, it depends on the device you're trying to connect. I tried with my cell phone and you have to enter the pairing code on your cell before it lets you connect the two.

Jul 30, 2009

Gmail password recovery

After reading this post on password recovery, it turns out that if you don't log into your secondary email address, it could expire. This is pretty obvious, however we don't really think about this very often. It also poses a security risk, since if say your old account expires, but someone else comes along and registers it, they can do a password reset on your current email account. And if they control your current email account, they pretty much control your online identity.

So if you're using Gmail, maybe it is a good idea to update your alternate email, and make sure that you can still log into it. It turns out that my alternate email was my Bishop's email, which is no longer available. The instructions for updating your alternate email are here: http://mail.google.com/support/bin/answer.py?hl=en&answer=6566

One thing I noticed is you can put your phone number in to SMS your password reset code to you. Does anybody know if this is a security risk or not?

Jul 28, 2009

Using Scala Actors and Case Classes

When I posted about actors last month I didn't really provide many code examples. That's mainly because I didn't really know a good actor library for the languages that I like to use. So I decided to dig a bit deeper into Scala to use the built-in actor library. It's pretty good!

So before I get started, I'll describe how it works. You have these classes that inherit from Actor. In these classes you define an act() method. This is similar to the run() method in the Java Thread class.
When an actor receives a message, it goes into the actor's mailbox. You can check the mailbox with the receive() method.

How are we going to represent these messages? Well, a rather convenient approach is to use a feature of Scala: case classes. These are lightweight classes which basically just have a constructor and some public attributes. Kinda like structs from C, but with a constructor and inheritance.

So for my example, I'm coding a basic simulation environment. We want this simulation to be multi-threaded, so that we can take advantage of multiple CPUs in a machine. And we've decided to use Scala actors for it. This will be an agent-based simulation, so we will obviously need an Agent class. We will also have a Simulator class which drives the simulation.

Let's start with two types of messages. For each time step, the Simulator sends an Act message to each Agent. When the agent is done processing, it sends a DoneAct message back to the Simulator saying it is done. Since the number of Agents is fixed, let's just use a counter to keep track of the finished agents.

Here is the Simulator class:
class Simulator extends Actor {
// need to override start to tell all our agents to start as well
override def start() : Actor = {
agents.foreach(agent => agent.start)
return super.start()
}

// something to add agents
def add(agent : Agent) = agents += agent

def act(){
// loop indefinitely
loop {
// check the mailbox
receive {
case Act => {
// this keeps track of how many agents we are waiting for
agents_left = agents.size

// the binary ! operator is what is used to send a message
agents.foreach(agent => agent ! Act)
}
case DoneAct = {
agents_left -= 1

// if we've gotten messages back from all agents, let's start
// the next simulation step
if (agents_left == 0)
this ! Act
}
}
}
}

var agents_left = 0
var agents = new LinkedList[Agent]()
}
Our Agent class:
class Agent(simulator : Simulator) extends Actor {
def act() {
loop {
receive {
case Act => {
// do some actions
simulator ! DoneAct
}
}
}
}
}
This class is a lot simpler, mainly because we haven't actually defined anything to do yet.

For completeness, here are the Event classes:
abstract case class Event()

case class Act() extends Event
case class DoneAct() extends Event
And a main function:
object Sim {
def main(args : Array[String]) {
var simulator = new Simulator()

// add a bunch of agents
simulator.add(new Agent(simulator))
simulator.add(new Agent(simulator))
simulator.add(new Agent(simulator))
simulator.add(new Agent(simulator))

// start the simulator, and send it an Act message
simulator.start
simulator ! Act
}
}
So this simulation is pretty basic. In fact, it's pretty useless. It doesn't actually do anything. However this gives us a framework for creating a simulation. Let's tweak the agent class a little:
abstract class Agent(simulator : Simulator) extends Actor {
def act() {
loop {
receive {
case Act => perform()
}
}
}

def perform() {
// do nothing
}
}
We can now use our Agent class as a base class for other agents. I will end this post now, however next time I will make some subclasses of Agent to actually do something.

Jul 18, 2009

Fractal Time Analysis Results

In my last post about fractals, I wrote up about how I wanted to analyze which seeds would take a long time to render and which would be fine.

The process goes like this:
real, im = -1, -1
times[IMG_SIZE][IMG_SIZE]
x, y = 0, 0

while x < IMG_SIZE
y = 0
while y < IMG_SIZE
render

times[x][y] = processing time
y++
x++

normalize times // min will be 0, max will be 255
output to bmp
So what you end up with is a greyscale image. The brighter it is, the longer it took to render.

The result is...well, amazing. Like really. See for yourself:



Does that look familiar to anyone?

I used a fairly small image size here (it was even smaller before). Problem is that rendering time is O(n2), so it takes 4 times as long when I double the size - originally I wanted to have an 800x800 image - turns out that'd take like a week for the thing to process! I might try tweaking the code to be multithreaded, that would probably half the time on my machine.

Jul 17, 2009

Penumbra

I just heard about this game called Penumbra through Slashdot (you may have seen it there too). It's a survival horror game, which is probably what you could classify System Shock and Bioshock as. Since I liked both of those games - although I didn't play System Shock all the way through because it ended up just crashing all the time - I decided to buy this one.

It's only $5 USD. No taxes, no shipping, that's it. The only catch is that you have to buy it this weekend. After you pay you get a download link, it's about 850MB.

Even better, it has native Linux support. Since I hate having to reboot to play games (it's one of the reasons I don't play many games anymore) this is a huge bonus.

I've decided that stuff like this is a much better deterrent to piracy than DRM or any of the other restrictive junk they've got out there. Since $60 is retardedly expensive for a game, why pay it when you can just download it for free? There is a bit of a guilt thing that the programmers aren't getting their pay, but $60 is more than I'm willing to pay. $5 though? That's peanuts. I'm willing to give the game a go for that cheap, even without trying the demo (which is something they have, by the way).
I'd be willing to pay up to maybe $20-30 for a game. I bought Crayon Physics which works really well under Wine, and it was about $20. Turned out to be a really interesting game.

So yeah, if you like survival horror, go out and grab this one! It's only $5!

Jul 12, 2009

The Value of Twitter

A while back I wrote a post about Twitter and how I didn't really like it. This was mainly from a personal perspective, where it was just me and my friends on Twitter and people would just tweet about random stuff.

These days however I've been using Twitter as an advertising tool for the Pirate Party of Canada and it is really working well. I tweet about a meeting or an announcement, and people retweet it, and the news gets spread really quickly. Compare this to Facebook (well Facebook now has a more Twitter-esque model on the front-page but we don't really have a Pirate Party page on the site other than the group) where you can't really broadcast a message easily to lot's of followers. Well, you can send a message to all the people in a group, but there are a lot of people that the message doesn't apply to and it's probably annoying for some people in say, Vancouver to always be receiving messages like "there's a meetup in Saint John, NB". Whereas on Twitter you can just ignore the tweet.

So in summary, Twitter is a pretty good communication tool for a one-to-many broadcast. In case you hadn't figured that one out already :)

Jul 9, 2009

Kitties and CPU Fans

A lot of us like kittens, they like to play and run around and chase things - just earlier my kitten was sitting in the screen doorway watching a squirrel, it was pretty funny.

They also like fans on computers. Things moving! Oh my god! So they stick their little claws in there trying to kill your fans. Try to keep them from doing this, the ends of their claws can splinter and while it may not hurt them very much (at least it doesn't seem to, he just keeps doing it) one thing you don't want is little bits of claw firing into your system.

Recently my computer started making a lot of noise from the fans. I figured that it was the heat, it is July after all. However I noticed that the fan on the side of my case wasn't working, and the CPU fan was working extra hard - way harder than normal.

I decided to open up and take a look. Sure enough the case fan had enough fur around the spinning part to slow it down to the stopping point, but here's what was worse - the CPU fan:


That is only about 10% dust!

Jul 7, 2009

Disable Notifications in Jaunty

For those of you like me who can't stand the new notifications in Jaunty (for me they are extremely distracting, like when a window pops up on top of what you are typing into) there is a nice little tweak to get rid of them described here.

For those who don't want to go and read it, here is the short form:
cd /usr/share/dbus-1/services
sudo mv org.freedesktop.Notifications.service org.freedesktop.Notifications.service.disabled
Then restart (I think you only need to restart X for this, so just log out and log back in again).

Jul 1, 2009

Arrr - Pirate Party of Canada

With the recent success of the Swedish Pirate Party, these kinds of parties are popping up all over the world. At the moment I am helping revive the Pirate Party of Canada which can hopefully run in an election sometime soon!

So if you are interested in protecting yourself from another C-61, join up and give us a hand!

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.