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.