Octave, a Free Math Language

Octave, a Free Math Language Very Similar To MATLAB

electronics specials

Do you need a matrix language quick, and one that won’t cost you an arm and a leg? One that’s highly compatible with Matlab?

How about a language that offers all of that, is loaded with supplemental math routines, and can cost you — nothing?

If that sounds enticing, then read on to learn about the popular matrix scripting language Octave.

If you like what you see and want more complete information, check out the GNU Octave Beginner’s Guide.

The image below was made with the Octave mesh command. It shows what’s called a sin(x)/x curve. It also happens to be the intensity graph of a star image as seen through a telescope. It always makes an impressive example for surface plot utilities. The primary graphics utilities of Octave are implemented by the Gnuplot program, detailed in the available manual: Gnuplot 5.0 Reference Manual. Even though Octave doesn’t have defined calls for every option available in the powerful Gnuplot program, it does provide a primitive interface for calling Gnuplot that will let the user impliment any additional features of Gnuplot.

Octave Mesh Plot

Octave Mesh Plot

Unlike the GUI interface of MATLAB, Octave historically has had a simple command line interface (no GUI). But it does have a nice history mechanism allowing one to scroll back to, and if desired modify, previous commands. In fact, the history is maintained even when exiting Octave and re-entering it later. I do notice that in my lastest install of Octave in a Silax Linux distro, Octave does optionally have a GUI interface similar to that of Scilab.

In Windows, Octave isn’t spectacular. It runs in kind of a pseudo-Linux environment, and this makes file access withing Windows an awkward and bit arcane procedure.

The version of Octave reviewed in this article was included in my Debian Etch Linux distribution, and thus was easily installed.

Octave Syntax
Octave is sort of the GNU answer to the commercial language MATLAB. That is, it is a scripting matrix language, and has a syntax that is about 95% compatible with MATLAB. It’s a language designed by engineers, and thus is heavily loaded with routines commonly used by engineers. It has many of the same time series analysis routines, statistics routines, file commands, and plotting commands of the MATLAB language.

Octave even uses the “m” file method of creating functions. That is, user created functions are stored in files with an m extension, and can be called in Octave programs or interactively without explicitly loading them. To a very large degree, Octave can run MATLAB m files, and vice versa.

An example m-file routine definition for Octave is shown below. This particular m-file routine emulates the handy rvals function in the perl PDL math language.

x = rvals(n1<,n2) — return pdl rvals type array or matrix

function b=rvals(varargin)
n1 = varargin{1};
n2 = 0;
if (length(varargin)>1;)
n2 = varargin{2};
endif

if (n2 == 0)
a = 0:(n1-1);
a = (a – floor(n1/2)).2; b = a; else a = ((0:n2-1)-floor(n2/2)).2;
b = ((0:n1-1)-floor(n1/2)).*2; y1 = ones(n1,1)a;
y2 = b’*ones(1,n2);
b = y1+y2;
endif

b = sqrt(b);
endfunction
The rvals m-file function mimics the handy rvals utility in the perl PDL matrix language. Rvals takes one or two arguments which are respectively the dimesion(s) of a vector or matrix. It returns a vector or matrix of that size, with the cell values being the cell radius from the central vector or matrix position.

For example, rvals(5) returns a vector of [-2 -1 0 1 2]. Rvals(5,5) returns a matrix as follows:

2.82843 2.23607 2.00000 2.23607 2.82843
2.23607 1.41421 1.00000 1.41421 2.23607
2.00000 1.00000 0.00000 1.00000 2.00000
2.23607 1.41421 1.00000 1.41421 2.23607
2.82843 2.23607 2.00000 2.23607 2.82843
Notice that the center element is always 0, being a zero distance from then center cell (it IS the center cell). As the cell position gets further from the center cell, it’s value increases by it’s positional radius from center. So whats so need about it? Below is a graph of an rvals(50,50) call.

It turns out that this simple function makes it vary handy for making image masks and functions like the sinx/x curve. The bottom graph is a one line command of: mesh(sin(.5x)./(.5x)). The ./ operator is a scalar divide operator.

sinx/x
sinx/x using rvals
Note that this quickly built sinx/x graph has an anomoly right at the peak of the curve. For a true sinx/x, that point needs to be normalized, because the simple equation plotted had a devide by zero at the center element. However, hopefully the simple way of getting to sin/x using rvals as a starting point shows the value of the routine, and the usefulness of user defined m-files.

Another handy use for rvals might be to constuct a mask for a graphic, as an example. With x=rvals(50,50) and y=(x<20;)*1.0, we use rvals to contruct a circular function and turn it into a circular mask of radius 20. The 1.0 multiplier is used to convert the boolean y matrix into a floating point matrix for the mesh plot.

rvals mask
Circular Mask From Rvals
As many m-file routines can be created as you wish, and if placed in a directory in the octave search path (defined in .octaverc), octave will automatically find them when referenced.

Octave’s math routines only work on scalars, and 1 or 2 dimensional matrices. One can make data structures of more than 2 dimensions, such as a structure that is a collection of 2 dimensional matrices in a 3 dimensional stack, but math routines can only be applied to the two dimensional elements of the data structure.

As is true with MATLAB, Octave math operators default to matrix operations. For example, the following expression does a full matrix multiply of matrices x and y:

z = x * y;
If one wishes to force an operation to be scalar, such as merely multiplying corresponding cells of matrices x and y, the dot modifier must be applied to the multiply operator as follows:

z = x .* y;
Octave functions are declared in the same way as MATLAB functions. It is also possible to have functions that receive a variable number of arguments. Octave uses the varargin array method of passing variable number of arguments to functions. For the user, this means that functions must have a bit of logic to default arguments, then check if values for them were passed in the varargin array.

One small difference between MATLAB and Octave is that comment lines in Octave can start with either the % character or the # character.