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.
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- RSS Feeds
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- Readers' Choice Awards
- The Secret Password Is...
- All the articles you talked
2 hours 15 min ago
- All the articles you talked
2 hours 18 min ago
- All the articles you talked
2 hours 19 min ago
6 hours 44 min ago
- Keeping track of IP address
8 hours 35 min ago
- Roll your own dynamic dns
13 hours 48 min ago
- Please correct the URL for Salt Stack's web site
17 hours 16 sec ago
- Android is Linux -- why no better inter-operation
19 hours 15 min ago
- Connecting Android device to desktop Linux via USB
19 hours 44 min ago
- Find new cell phone and tablet pc
20 hours 42 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?