# Octave: A Free, High-Level Language for Mathematics

Issue #39, July 1997

A quick look at a language designed to manipulate matrices and provide other numerical functions.

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.

## Matrix Operations

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  2
```
we can find the matrix product of a and b with the command:
```octave:4 a*b
ans =
7   4
15   8
```
Or 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  8
```
Matrix elements can easily be selected by index, so to get the (1,1) entry of a, type:
```octave:6 a(1,1)
ans=1
```
We 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
3
```
And to select the first row of a type:
```octave:8 a(1,:)
ans =
1  2
```
As 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.37228
```
Alternatively, 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.37228
```
This 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.

## User-Defined Functions

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
hello
```
Obviously, 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
ans = 3
```
The 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;
endfunction
```
Now, 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=3
```
A 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;
endfunction
```
returns multiple values when invoked as:
```octave:18 ns,d]=sumdiff(1,2)
s = 3
d = -1
```
Functions 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.

## Other Capabilities

Although Octave has very strong matrix capabilities, it has many other features as well. For example, it has routines to manipulate polynomials. A polynomial is entered as a vector of coefficients; so, for example, the polynomial x3+3x2+2x-1 can be represented by the vector:

```octave:19 mypoly=[1 3 2 -1]
mypoly =
1   3   2  -1
```

We can then differentiate mypoly using the command:

```octave:20 polyderiv(mypoly)
ans =
3  6  2
```
or integrate it by:
```octave:21 polyinteg(mypoly)
ans =
0.25000   1.00000   1.00000  -1.00000   0.00000
```
Note that Octave uses zero as the constant of integration. We can also evaluate the polynomial at a given value; thus, to find the value of mypoly(2) use the command:
```octave:22 polyval(mypoly,2)
ans = 23
```
If we want the roots of the polynomial, use:
```octave:23 roots(mypoly)
ans =
-1.66236 + 0.56228i
-1.66236 - 0.56228i
0.32472 + 0.00000i
```
Note that Octave is quite happy with complex numbers, even though all the examples I've given have been real. There are also routines to convolve and deconvolve polynomials, form companion matrices and characteristic polynomials, and form a partial fraction representation of the quotient of two matrices.

Other features include functions to solve systems of nonlinear equations, solve differential and differential-algebraic equations, perform quadrature and collocation, as well as statistics, control theory, signal processing, image processing and optimization routines. The manual indicates areas where the developers hope to extend Octave's capabilities.

## Graphics

Octave provides graphics capabilities via the Gnuplot program, which has to be obtained separately. The advantage of this is that Octave supports all the output devices Gnuplot supports, including the Linux terminal, which might be of interest if you have a low-memory system.

Octave provides two low-level graphics functions, gplot and gsplot, that behave almost exactly like the Gnuplot functions plot and splot, and also provides several higher-level plotting functions based on the graphics functions found in Matlab 3.5. Two and three-dimensional plotting commands are also available.

If you are familiar with Gnuplot, then you will probably appreciate the flexibility offered by access to Gnuplot's commands. However, the higher-level commands offered by Octave are very easy to use and you may find you don't have to use the Gnuplot commands at all.

## Conclusions

Octave is a flexible, powerful, easy-to-use, high-level language designed for numerical computations. It comes with a very readable 200+ page user manual, and a help system based around the GNU info system. The main advantage of a high-level language over a language like FORTRAN is that development time is usually considerably shorter using a high-level language. This allows for easy prototyping and experimentation.

Although the documentation doesn't claim that Octave is intended to be a Matlab clone, or Matlab compatible, Octave is probably the most Matlab-like of the freely available high level languages. It's not exactly the same, but I was able to convert a suite of Matlab m-files that perform finite element analysis of the Navier-Stokes equations to Octave very easily.

Octave has many more features than I have described here, but I've provided an overview of its main strengths. If you're looking for a language for numerical work, Octave is certainly an option. I don't think you can directly compare Octave with such languages as RLaB, SciLab and Yorick—they all do different things, and which you choose depends on what you want to do as well as personal preference. My preference is Octave. Malcolm Murphy still wishes that he had discovered jazz before he gave up clarinet lessons at an early age. He considers himself too old (or too lazy) to start again now, so he plays the guitar instead. If you have an uncontrollable urge to send him some e-mail, his address is Malcolm.Murphy@bristol.ac.uk.