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 4To 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 4Arithmetic 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") endfunctionThe 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.
- Give new life to old phones and tablets with these tips!
- Readers' Choice Awards 2014 Poll
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- Memory Ordering in Modern Microprocessors, Part I
- [<Megashare>] Watch Mrs Brown's Boys Movie Online Full Movie HD 2014
- Source Code Scanners for Better Code