|
|
Subscribe / Log in / New account

A new release for GNU Octave

Please consider subscribing to LWN

Subscriptions are the lifeblood of LWN.net. If you appreciate this content and would like to see more of it, your subscription will help to ensure that LWN continues to thrive. Please visit this page to join up and keep LWN on the net.

December 15, 2020

This article was contributed by Lee Phillips

On November 26, version 6.1 of GNU Octave, a language and environment for numerical computing, was released. There are several new features and enhancements in this release, including improvements to graphics output, better communication with web services, and over 40 new functions. We will take a look at where Octave fits into the landscape of numerical tools for scientists and engineers, and recount some of its long history.

What is GNU Octave?

In the words of its manual:

GNU Octave is a high-level language primarily intended for numerical computations. It is typically used for such problems as solving linear and nonlinear equations, numerical linear algebra, statistical analysis, and for performing other numerical experiments.

Octave is free software distributed under the GPLv3. The program was first publicly released in 1993; it began as a teaching tool for students in a chemical engineering class. The professors, James B. Rawlings and John G. Ekerdt, tried to have the students use Fortran, but found that they were spending too much time trying to get their programs to compile and run instead of working on the actual substance of their assignments. So the professors created early versions of Octave as an easier alternative. The name is a tribute to the late Octave Levenspiel, a former professor of Octave's principal original author, John W. Eaton. Octave became part of the GNU project in 1997.

To understand Octave's subsequent development requires some awareness of another tool, called MATLAB, which was also created as a Fortran alternative. MATLAB has an interesting origin story: it was created to enable students to use Fortran's linear algebra routines without having to write Fortran programs. Its authors later turned it into a commercial product, which became quite successful. Now this powerful (and expensive) system is in use by millions of engineers and scientists all over the world. This created a market of people doing numerical computing who became used to MATLAB's syntax, but lost access to it after leaving their employers or institutions; beyond that, some people were also interested in free-software alternatives.

There are now several options, at different levels of compatibility. Octave, written in C, C++, and Fortran, soon adopted the goal and policy of being a fully compatible replacement for MATLAB. According to the Octave Wiki, any differences between Octave and MATLAB are considered to be bugs, "in general", and most existing MATLAB scripts will work unmodified when fed to Octave, and vice versa. The exceptions are scripts that exploit extensions to the syntax introduced in Octave, or that use recent features in MATLAB that have not yet been implemented in Octave.

Generations of engineers were raised with MATLAB, and its tendrils extend far, exerting its influence even over the recently developed language Julia, which I wrote about here recently. MATLAB and Octave deliberately confine their attention to numerics, with a particular focus on matrix manipulations, whereas Julia is a true general-purpose language. But Julia, like MATLAB, indexes arrays starting from one rather than zero, and its syntax for dealing with arrays is partly designed to provide the comfort of familiarity to refugees from MATLAB, without attempting to be a clone of the earlier language.

The latest version of Octave is available as a package in Debian unstable, but users of more conservative distributions may have to build the latest release. For example, the latest version available for Ubuntu is 5.2 (released in January 2020). Octave is a large system with many dependencies, and users may desire an alternative to compiling that still gets them a recent version. This page provides a detailed rundown of various methods of installation for several Linux distributions, as well as a detailed guide to compiling Octave. Octave is available for Windows and macOS, and there are packages for both FreeBSD and OpenBSD as well.

Using GNU Octave

When octave is started in the terminal it brings up an interactive prompt. The user can type in expressions, and the results are printed immediately. In other words, operation is similar to gnuplot, Maxima, Julia, and other programs that offer a read-eval-print loop (REPL) style of interaction. The REPL provides tab-completion of function and variable names that works similarly to Bash tab-completion on the command line. It also offers the usual command history and editing through readline.

If one adds the --gui flag when invoking Octave, a graphical environment speedily appears on the screen, instead of a simple REPL prompt. This is replete with the customary inscrutable array of icons whose purpose is revealed in tooltips when they are hovered over, and the standard hierarchical menu bar at the top. One of these menu items promises "help", leading to the inbuilt copy of the manual, as illustrated below. Having a complete, hyperlinked manual locally available is convenient. It allows searching through an index of functions and the content of any particular page, but not through the entire text of the manual.

[Octave manual]

Although the manual is quite thorough, it concerns itself almost entirely with the Octave language, as it might be used from the Octave REPL or when writing scripts in any text editor. It contains no help for the GUI that has shipped with Octave since version 3.8 as an experimental feature, and fully supported in 4.0.0; neither how to use it or what, if any, additional powers it affords the user. There is also nothing significant in the Octave Wiki.

Fortunately, everything that the GUI offers can be discovered with a bit of experimentation. In the following illustrations, I've reduced the size of the GUI window considerably in order to produce more compact screen shots; in normal operation, the user has much more room. I'm going to use the Octave GUI to solve the following system of equations:

    x + 2y + 3z = 2
    3x + 2y + z = 2
    2x + 2y + 2z = 2

The layout of the environment can be seen below:

[Octave GUI]

On the left is a panel containing three small segments. From top to bottom, these are the file browser; the workspace, showing a list of all currently defined variables, and some information about their types, dimensions, etc.; and the command history.

On the right is the main panel, which can contain different displays, controlled by the tabs along the bottom. The layout can be arranged to suit the taste of the user. Each panel can be resized, and any panel can be split off into a separate window.

The editor is where the user can write and change Octave scripts. It offers syntax highlighting and command completion. As the user types, scrollable boxes appear with all possible completions. The editor also provides a typical debugger, with the ability to set breakpoints and step through the script.

My little three-line script defines a 3x3 matrix of coefficients, stored in the variable a, the vector of the right hand side of the system, stored in b, and finally invokes the Octave function linsolve(), which is supposed to return the solution as a vector of three values for the three variables.

The script in the panel is run by selecting the "run" menu item or by clicking the gear with the yellow triangle. I've done this, and the command tryingOctave has appeared in the command history panel. This is the name I gave to my script. But where is the output?

A new variable, called ans, has appeared in the workspace panel, showing that something has happened. Switching to the command window tab, shown in the next figure, shows the output of each command in the script. This panel is a REPL that behaves the same as invoking Octave from the command line, including the usual readline functionality.

[Command panel]

Above we can see the matrix of coefficients nicely printed, the vector of the right-hand side of the equation system, and the results, calculated by linsolve(), for the three variables.

If the variable editor panel is selected, a double-click on a variable in the workspace displays it in a spreadsheet-like layout as shown below. This provides random access to elements of arrays, which can be directly altered.

[Variable editor]

So it appears that the GUI offers significant functionality, belying its somewhat homely appearance; it amounts to an IDE for Octave.

Here is one more illustration, demonstrating Octave's integrated graphics and another of its built-in functions, fft(), which implements a fast Fourier transform (FFT). The Fourier transform decomposes a periodic function into a sum of sines and cosines of different frequencies, and the FFT is an algorithm for doing so quickly. Of course, we humans can perform Fourier transforms in real time in our heads—literally. The inner ear Fourier transforms sound and sends the different frequencies along separate nerve bundles to the brain.

[FFT example]

To test this routine, I'll choose a function for analysis where the Fourier transform is known analytically: the square wave. In the first line of this script, I've defined one cycle of a square wave in the variable sw, by appending a vector of 64 1s to a vector of 64 0s, to create one period defined by 128 numbers. The second line calculates its FFT and stores it in tsw, which should now contain the spectrum: the amplitudes of the frequencies in the original function. For a real-valued function, the spectrum is stored in elements 2 through N/2 of the result, so we extract them from tsw and plot the result using the bar() function to create a bar graph. Note that Octave's indexing syntax follows MATLAB in using parentheses.

The result, showing the (un-normalized) spectrum, appears in a graph window. Octave has always used gnuplot for plotting, and plans to continue to include that as an option indefinitely. However, recent versions also include an OpenGL plotting backend, which is more integrated with some Octave GUI functions that, for example, can create GUI elements for user interaction with Octave scripts.

In these examples I've used just a couple of the huge number of built-in functions for Octave. In addition to these, there is an official repository containing a vast number of third-party packages covering almost any topic in numerical analysis that one can think of.

The new release

Version 6.1.0 is a major release with a long list of user-visible changes. Many of the changes are specifically for the purpose of improving MATLAB compatibility, including keeping up with changes to MATLAB behavior.

Some of the more interesting enhancements and changes include new functions to facilitate communicating with web services, for use in scripts that analyze data retrieved over the web. There are also several improvements to the numerical nitty-gritty, such as improved random-number generation and numerical integration. This release also brings bug fixes and improvements to graphing, including font smoothing, control over legends, drawing of axes, and placement of superscripts and subscripts in labels. There is also more complete integration of Unicode throughout the system. The package system has been improved, several functions that had been deprecated in the 4.x series have now been removed, and about 40 new ones added.

Getting help

Octave is quite well documented. The manual, in particular, is well done and complete. Octave has extensive help within the program itself, which is available by typing "help". In addition to the manual, there is an Octave Wiki that contains a FAQ and other useful information.

There are many books available about GNU Octave. These cover the range from general introductions to guides for researchers in specific disciplines. Many of these books feature both Octave and MATLAB, which is an indication of the functional interchangeability of the two systems. There are Discourse forums for general help, for Octave developers, and general community discussions, as well as the help-octave mailing list with archives that go back to 1992, and an #octave IRC channel. In addition, the project maintains a list of firms offering paid support and consulting in the use of Octave.

Advice

I think the appeal of Octave should be apparent from the two examples in the previous section. It is particularly noteworthy that the linear algebra and signal processing routines were ready to use; I didn't have to type any import statements or figure out where things were. This is one way that Octave beats both Python and Julia: so much is just ready to go.

I also didn't need to write any loops or create any dummy variables, due to Octave's powerful array syntax. Just a few succinct statements solved a non-trivial problem in both cases. And when I wanted a plot, I just asked for one. Again, no need to import libraries (or wait for them to pre-compile). A complete manual is at one's fingertips, and everything in the manual can be used immediately, with no fuss and no waiting. Octave provides an impressively efficient path to the solution to a numerical problem.

It is a great time to be a scientist or engineer with an interest in computation and a desire to use free software. There is a wide variety of mature and powerful software available, with options that are general in scope and others that are focused on particular disciplines. This pleasant situation may, however, create a paralysis of choice. Although the software is free, there is an investment of time and mental space in choosing any particular tool over the others; so one wants to choose rationally.

For those who are already trained in MATLAB and who seek a tool for numerical exploration and modeling, Octave is the obvious choice. For those who may need a more general-purpose language, or whose sights are set on performing large-scale simulations, the best choice for most people today is probably Julia. However, Python may appeal to those who want to work in certain areas, such as machine learning, where it is still the most popular language, and where there are highly developed libraries.


Index entries for this article
GuestArticlesPhillips, Lee


(Log in to post comments)

A new release for GNU Octave

Posted Dec 16, 2020 12:15 UTC (Wed) by tchernobog (subscriber, #73595) [Link]

Worth mentioning is also KDE's Cantor support for Octave, which allows for a very nice GUI.

https://cantor.kde.org/

A new release for GNU Octave

Posted Dec 16, 2020 13:18 UTC (Wed) by leephillips (subscriber, #100450) [Link]

That’s an interesting project, thanks for mentioning that.

A new release for GNU Octave

Posted Dec 17, 2020 9:27 UTC (Thu) by bneven (subscriber, #53893) [Link]

Your example system of equations is not linearly independent; (x, 1-2x, x) is a solution for any value of x. Shouldn't Octave warn you about that?

A new release for GNU Octave

Posted Dec 17, 2020 10:02 UTC (Thu) by xyz (subscriber, #504) [Link]

The example given is not representative of the usual way to solve linear systems in Octave.
It is not idiomatic as the usual way would be to use the left division operator, also following the usual notation the matrix is represented with a capital letter:

>> A = [1 2 3; 3 2 1; 2 2 2];
>> b = [2 2 2]';
>> A \ b
warning: matrix singular to machine precision
ans =

0.3333
0.3333
0.3333

As you see Octave is saying that within the used precision the matrix is non-invertible and so the solution is not unique.

Regarding linsolve this is what the first lines have to say about it. (TL;DR linsolve is an advance frontend when used with other options)

>> help linsolve
'linsolve' is a function from the file /home/jamatos/devel/octave/scripts/linear-algebra/linsolve.m

-- X = linsolve (A, B)
-- X = linsolve (A, B, OPTS)
-- [X, R] = linsolve (...)
Solve the linear system 'A*x = b'.

With no options, this function is equivalent to the left division
operator ('x = A \ b') or the matrix-left-divide function
('x = mldivide (A, b)').
....

A new release for GNU Octave

Posted Dec 20, 2020 17:40 UTC (Sun) by leephillips (subscriber, #100450) [Link]

One can certainly check for the uniqueness of solutions in Octave, but that was not important in this brief example. The returned solution is correct, if not unique.


Copyright © 2020, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds