Monday, December 22, 2008

We need a Better Matlab!

I have used Matlab in some of my projects (mostly in university and just when I had to do that). I really don’t enjoy it. I think the worst problem with Matlab is the language itself. Poor design of the language, from function definition and typing mechanism to OO support and lack of namespaces, makes it an unpleasant choice for a computer scientist(it may seem very nice from a non-computer-scientist view). Of course it has some nice features including an exhaustive scientific library and efficient support for matrix and vector operations. Unfortunately, the Matlab library is completely based on the awful programming language. Because of the poor language constructs, matrices are overused in library design. Despite the mentioned weaknesses, Matlab is vastly used in scientific world, that’s because you cannot find any other single alternative in scientific programming.

I have found some ongoing works to make a Matlab replacement, Scilab and Octave are more or less a clone of Matlab (which means they are not a better Matlab), SciPy is a scientific python library (and just a library) which seems pretty good to me.

Based on my experience, a scientific environment at least must have the following components to be called a better Matlab:

1) A better programming language, by better I mean powerful but simple. As the language is used by non-computer scientists, simplicity is a must for the scientific programming language.

2) An excellent library, of course an excellent language is almost useless without a proper library.

3) Efficient support for matrix and vector operations. Maybe hardware accelerated operations or support for parallel processing (e.g. support for SMPs or use the power of GPU SIMD instructions) may be helpful.

4) A simple and useful distributed processing platform. As scientific programs are usually CPU intensive, a built-in Cluster Computing platform will greatly help scientists.

5) A better Development Environment

For the programming language, we must choose to use a compiled or scripting language, of course scripting languages are simpler to learn, understand and use and more appropriate for scientific experience and simulations. Moreover we have the following options:

1) Design a programming language from scratch for scientific purposes, it has the benefit that it could completely fit to the scientific computing requirements, but it has a huge cost and is very risky.

2) Use an existing language, there are many programming languages around. We can easily choose one of them; among several programming languages which I have studied, I prefer python and ruby. Although python has a more matured library, ruby design is more consistent. I will discuss later how ruby would be fitted to the needs.

3) Choose an existing platform with multiple programming languages, e.g. .Net or Java with a mixture of compiled-scripting languages; I don’t like this option for many reasons including its complexity.

So, why ruby is a good choice for our goal? There are many reasons to be considered.

1) Ruby is not designed for scientific computation but its flexible nature made it fully DSL-able. There are already some proofed examples of success of ruby in specific domains; Ruby on Rails is an excellent example. As an example of adaptation, consider the scalar expansion capability. Ruby does not support scalar expansion (e.g. 2 * [1, 2, 3, 4]), but this feature could be easily added to the language so as to become native to the language! Here is some lines of code which do that:

class Fixnum

alias_method(:old_mult, :*)

def *(a)

if(a.is_a?(Array))

a.collect { |i| self * i }

else

self.old_mult(a)

end

end

end

This is the job that needs AOP in many other languages to be done, even in many languages like java it can’t be done at all.

2) Ruby is simple, it could easily be learned by non computer scientist and students, and it has very simple OO semantics. Moreover, it is strongly and dynamically typed, its dynamic typing semantic made it simple to use and its strong typing nature prevents many programming errors.

3) Ruby has a wonderful implementation called JRuby which runs over JVM. This is an excellent option because you can easily use all of available java libraries out of the box. Using some simple wrappers, many scientific java libraries could be ported to ruby. Also some parts of new libraries may be implemented in java for efficiency. Additionally, both NetBeans and eclipse (through RadRails plug-in) support ruby. As the IDEs are extendable they could easily be modified to make a better Matlab development environment.

As many other java fans I love ruby because it is very consistence and clean (just like java). It might be some other good choices.