How to use NMatrix’s shortcuts

John Woods merged my last pull request to NMatrix recently and I wanted to write about why we created these shortcuts and what can be done with them.

UPDATE: This post is outdated. I’m writing a guide on NMatrix and will probably reuse some parts of this post. I’ll update with a link eventually.

The need for shortcuts

Originally, the NMatrix shortcuts were written by Daniel Carrera and sent to the mailing list, where I started working with them. Some discussions, decisions and 2 pull requests later, we have a working set of methods to create common matrices (zeros, ones, random, etc) and do some cool stuff (column and linspace) very easily.

I think that there are at least 2 good reasons to have them: MATLAB users are going to feel at home and they allow us to experiment very rapidly. For example, it’s dead simple to create an identify matrix when you need one:

And a random matrix to test some operation:

Shortcuts available

The following list is taken from NMatrix’s wiki.

The method names are mostly from MATLAB. In the future, when more functionality is added, we’ll try to create a more “Ruby-like” feel to the API. But as these shortcuts do very primitive tasks, there isn’t much room to improvement. (prove me wrong, please!)

I’ll create a separate wiki page for the shortcuts to be able to organize and put more information about how to use them. But I want to create a better RDoc documentation for NMatrix – so the full API will probably be referenced there. I’ll post a new article here when something along these lines is done.

Examples

First, let’s try normalizing the columns of a NMatrix.

Unfortunately, slice by reference isn’t working on HEAD, so we can’t make this code a bit smarter (see issue #51).

The other thing that I’ve wanted to do is to use linspace to generate points for plotting sines, cosines, etc. Let me show an example:

These results show something good: we are able to use NMatrix for simple tasks already. Of course, there are problems to be solved, e.g. slice by reference, inversion for all dtypes, eigenvalues. But it’s going somewhere, it’s growing.

I hope that by this time next year we’ll have a very mature linear algebra library (and much more).

Leave a Reply