Search

Friends

Atomspheric CO2 (PPM)

Archives

Blather

Uptime verified by Wormly.com

17 March 2007

Ruby + Matrices = Nonstop Fun!

I've had a look at a few different things for stuffing around with matrix arithmetic. It's the sort of thing my degree has reduced me to. I've looked at Excel and R. I already know about Stata. At uni they think Matlab is pretty good. But I've found something better than all of them. Ruby. It's totally sweet.

require 'matrix'
Q = Matrix[[20, 30, 43],
           [20, 25, 10],
           [20, 10, 5]]
assets = Matrix[[10], [20]]
price = Matrix[[5, 10]]
portfolio = Q.inv * assets
cost = price * portfolio

Or some leet OLS. Oh yeah.

portfolio = (Q.t*Q).inv*Q.t*assets

Awesome eh? It probably seems more nifty to programmers and those who've tried matrices in Excel. Nasty. I even wrote a little Ruby library to make printing out the matrices prettier. Although probably if I count that extra time my average productivity drops quite a bit.

Ruby is pretty good all round. Kind of slow in spots. But that won't stop it from taking over the world pretty soon.

Comments

  1. I am reading the poignant guide.

    Currently, I still think Ruby is silly at points, like Constants, $globals, @instance and @@class — why that distintion? Doesn’t Ruby have scope? And I’m not sure what symbols are, still. But maybe I’ll find them wonderful, given time.

    Maybe I’m just a Blub programmer now, looking at another language and going “why?”, eschewing that which I don’t understand.

    Wil / 1:07am / 18 March 2007

  2. Are you asking about their usefulness or their syntax?

    Constants are kind of cheating form of configuration that are immutable and you probably need to get stuff done.

    Globals are definitely cheating, evil (because they’re mutable) and should be abolished even though nearly every language has them.

    @instance variables are at the heart of OOP and the @ is there to make like easier for programmers.

    @@class variables are the same as static variables in Java. I don’t really like them but understand why they might be useful. It seems that if you start storing information in the class it’s not really OOP anymore. Conceptually it’s uglier, but maybe that doesn’t matter.

    Symbols are just good and are the Ruby thing which should be in every language. Instead of using strings to label objects in your code, you can use symbols. Strings are the wrong thing to use but since everyone started using hashes for everything some sort of key was needed. In Ruby strings are only data and you don’t need to create them just to label things internally. In practice maybe it isn’t that different. It’s one less character to type each time. It’s a bit faster to compile. Code highlighting is better. Debuggers probably would like it and can give you nicer information because they know which bits of the program are for computer consumption and which bits are for human consumption. Conceptually it’s just better.

    I think Ruby took some of Python’s sensibleness a step further. It’s not a lot better than Python. But better enough that I don’t know why people would keep using Python. Probably it’s biggest drawback is its “Perlishness”. There are so many nifty little tricks in it that a lot of code becomes indecipherable. Although, unlike Perl, so far Ruby seems to have attracted sociable coders.

    Ryan / 12:59am / 19 March 2007

  3. But can you use symbols anywhere you’d otherwise use a string — as long as the text data is simple and conforms to the limits of variable names? Like the key for a hash?

    I’d find it impractical to use full strings for that every time. hashy[“flurp”] seems cumbersome compared to hashy.flurp.

    Wil / 3:09am / 19 March 2007

  4. Oh, in case you’re wondering, my programming thinking is based on Javascript and C-family-ish things. I’m trying to use Ruby, and later on C#, to expand my rather narrow horizon.

    Wil / 3:11am / 19 March 2007

  5. Symbols are always valid as hash keys so dogs[:bitzer] is fine. You can also use it to switch around the order of function parameters, like in Python.

    do_something(:second => "Bar", :first => "Foo")
    

    Ryan / 9:53am / 19 March 2007

  6. Now that’s something I don’t quite follow.

    Wil / 8:19am / 20 March 2007

  7. def pow(base, exponent)
        return base ^ exponent;
    end
    
    pow(:exponent => 2, :base => 5)

    Is that better? The function call gives you what you’d expect.

    Ryan / 6:52pm / 21 March 2007

  8. It is understanded.

    I’m not sure if allowing this invites good programming, though. It feels like goto. :)

    But so in this case, the :symbol is not a weird “string” of sorts, but a reference to an argument with :symbol’s name. Does that extend to variables or just arguments?

    I still don’t fully comprehend symbols, but I’m sure that will come to me when I start doing some thingies in R.

    Willem / 7:06pm / 21 March 2007

  9. Symbols are often just weird strings, but sometimes they’re a bit smarter than that.

    R? As in R-cran? Or Ruby?

    Ryan / 8:23pm / 21 March 2007

  10. R as in Ruby. :)

    I’ve never heard of this thing called R-cran.

    Wil / 8:19am / 22 March 2007

  11. R is based on S – one of those little jokes that computer folk enjoy. It’s kind of like Matlab. A programming language for mathematics of all sorts.

    Ryan / 12:21am / 23 March 2007

  12. I’ve never heard of S either. Ahem. Of course, I do know about brainfuck and malebolge etcetera.

    But thanks for your clarifications. :)

    Willem / 1:01am / 23 March 2007

Leave a comment

Markdown

0.097 seconds