Octave: A Free, High-Level Language for Mathematics
For numerical computing, high level languages offer advantages over more traditional languages, such as FORTRAN or C. Built-in graphics capabilities, automatic variable typing and flexible data structures combine to provide an environment in which it is easy to develop your ideas without having to fight with the language. That's not to say that FORTRAN and C are of no use, just that sometimes you want to make life a bit easier.
Matlab is a one such language. It is available on many platforms (including Linux) and provides powerful facilities for manipulating matrices, as well as other numerical functions. Unfortunately, Matlab is commercial software and wasn't available for Linux until recently (in the last twelve months or so). However, there are other, freely-available alternatives, and Octave is one such alternative.
Superficially, Octave looks very much like Matlab, and the description in its LSM entry reads “GNU Matlab—A numerical matrix mathematics program.” To begin, type octave at the shell prompt, and Octave greets you with its own prompt. Now we can start doing math.
As you might expect, entering and manipulating matrices is one of Octave's strengths. (In order to differentiate Octave commands from the output, the prompt octave:number of the command precedes the commands in the examples below.) We can enter a matrix with the command:
octave:1 a=[1 2 ; 3 4]
Octave then reports the result of the command, namely:
a =
1 2
3 4
To suppress output, simply place a semicolon after the command.
Note that we didn't have to worry about declaring the size or type
of the matrix a, we just type it in and start
working with it. For example, we can get the transpose of
a by typing:
octave:2 a'
ans =
1 3
2 4
Arithmetic operators such as +,
- and * act as matrix
operators unless they are preceded by a period, in which case they
act in an element by element sense. Octave also provides the
forward and backward slash operators that perform matrix right and
left division. Again, these can be used in an element-wise fashion.
So, given another matrix:
octave:3 b=[1 0; 3 2] b = 1 0 3 2we can find the matrix product of a and b with the command:
octave:4 a*b ans = 7 4 15 8Or define the (i,j)th entry of the product is the product of the (i,j) entries in a and b with the command:
octave:5 a.*b ans = 1 0 9 8Matrix elements can easily be selected by index, so to get the (1,1) entry of a, type:
octave:6 a(1,1) ans=1We can select a row or column using the colon operator, exactly as in Matlab. Thus, to select the first column of a type the command:
octave:7 a(:,1) ans = 1 3And to select the first row of a type:
octave:8 a(1,:) ans = 1 2As well as these elementary operations, Octave provides functions that perform higher-level operations, such as finding the eigenvalues of a matrix, by using a command like the following:
octave:9 eig(a) ans = 5.37228 -0.37228Alternatively, you can find the eigenvalues and eigenvectors by giving the following command:
octave:10 [v,d]=eig(a) v = 0.41597 -0.82456 0.90938 0.56577 d = 5.37228 0.00000 0.00000 -0.37228This is a demonstration of one of the main advantages of using a high level language like Octave. Writing a FORTRAN or C program to find the eigenvalues of a matrix would take a lot more time and effort than it does in Octave. Moreover, Octave routines are usually based on well-known, high quality algorithms, so you can have faith in the results.
Octave provides many other matrix routines, which are detailed in the manual and in the on-line help system.
Octave also lets the user define his own functions via the function keyword. A function definition looks like this:
function [output values] = name (input values) sequence of commands endfunction
The input and output values are optional, so it is possible to write a function that takes no arguments and returns no values, such as
octave:11 function hello
printf("hello\n")
endfunction
The printf statement prints the quoted string to
the screen, and the \n is interpreted as a newline. Invoke the
function by typing its name:
octave:12 hello helloObviously, functions that don't take arguments or return values aren't all that useful. To accept arguments, list them after the function name in the following manner:
octave:13 function add(x,y) x+y endfunction octave:14 add(1,2) ans = 3The output came from the statement x+y--if we had ended the line with a semicolon, there wouldn't have been any output from the add command. To assign the output to a variable, define the function in this way:
octave:15 function sum=add(x,y) sum=x+y; endfunctionNow, if we type add(1,2), we get exactly the same result as before. However, by defining an output variable, we can assign the result of the add function to a variable in this way:
octave:16 fred=add(1,2) fred=3A very powerful feature of Octave is the ability to return multiple values from a function. This feature exists in Matlab, but not in FORTRAN. For example, the following function:
octave:17 function [sum,diff]=sumdiff(x,y) sum=x+y; diff=x-y; endfunctionreturns multiple values when invoked as:
octave:18 ns,d]=sumdiff(1,2) s = 3 d = -1Functions can be defined at the keyboard, as we did in these examples, or stored in a file and used again. This lets you build a suite of routines for whatever tasks you want. All you have to do is put the files (identified with a .m suffix) somewhere where Octave can find them. The built-in variable LOADPATH specifies where Octave should look for the .m files. Many of Octave's standard functions are defined in .m files. You can also access user-supplied C++ routines within Octave, although this feature is not yet fully developed.
Octave also provides a full programming language, with flow control and looping constructs, as well as extensive input-output facilities. It is possible to write quite sophisticated programs in Octave, and development time is considerably shorter than you would expect using FORTRAN or C.











This week 5 lucky Members will receive a copy of The Official Ubuntu Server Book by Benjamin Mako Hill and Linux Journal's very own Kyle Rankin. No entry necessary. Check back here early next week to find out who the lucky Online Members are.




Comments
What I like!
I like how it provides a convenient command line interface for solving linear and nonlinear problems numerically! Also great features are: Matrices as fundamental data type and Built-in support for complex numbers.
Thank you!
I used Matlab exclusively in my MSc. never knew that there was a Gnu'able version. Installed from my GNU/Linux's distrobution and taking it for a spin. Thanks :-)
jayeola
Re: Octave: A Free, High-Level Language for Mathematics
Hi: I
Re: Octave: A Free, High-Level Language for Mathematics
Why yes you can. m files work just fine with octave.
Re: Octave: A Free, High-Level Language for Mathematics
If Matlab compatiability is your aim you should definitely also install the octave-forge package available at http://octave.sourceforge.net. This contains
many additional functions for compatiability to Matlab (eg signal processing, sparse matrix, comms, etc).
Regards
David
Re: Octave: A Free, High-Level Language for Mathematics
Hi... I tried to mail this straight to the address given, but Mr Murphy seems to have moved on... so I'll just post here instead...
Hello Mr Murphy,
You don't know me and I live way too far away... but my name is Peter. I'm doing an undergrad I.T. degree and this semester I'm doing a subject on Pattern Recognition (eventually using Neural Networks). The subject demands use of Matlab and for some reason, our I.T. faculty seems to be fairly poor at supporting anything Linux (except for network admin and programming based subjects).
Thanks to your article, I'm up and running with Octave about five minutes after downloading and installing. Basically, I just wanted to thank you for a well-written, explanatory article with useful beginner's examples in it. This has saved me some time that could be far better used getting up and running with the subject content instead.
Just wanted to let you know that your work has NOT gone unappreciated. Again, thank you.
P.Petroff
Post new comment