📜 ⬆️ ⬇️

Higher Command Line Math - GNU Octave

As I promised, I move from a review of calculator replacement programs to more serious tools. If you remember the diagram from the previous post, then in the second category were the tabular ones: OpenOffice / LibreOffice associates. We can safely skip this batch, since it does not belong to the command line, besides, it is difficult to find a person among Habr's readers who would not understand them. Therefore, I turn immediately to the third category.


Specialized math programs, student level +


  1. GNU Octave .
  2. Scilab .
  3. Maxima .
  4. R
  5. Sage .

In the first place in this list is Octave , and this is not an accident. Researchers from the University of Maryland in the United States conducted a comparative analysis of mathematical calculations using MATLAB, Octave, SciLab and FreeMat in a simple and complex scenario. In the first case, the system of linear equations was solved, and in the second, the finite-difference discretization of the Poisson equation in two-dimensional space. The main conclusion is that GNU Octave handles the tasks better than other open mathematical packages, demonstrating the result (pages 23 and 25) comparable to Matlabovsky .


But first, a little historical context to understand how open source math programs were steeled.


Catch up and overtake MATLAB


It so happened that the commercial programs came running and were the first to stave off a clearing of mathematical calculations. Already in the late 1970s. The creator of the programming language, Cleve Mouler, distributes MATLAB at US universities, and in 1984, together with two companions, rewritten it from Fortran to C and created The MathWorks . It is noteworthy that earlier versions were distributed with open source .


It was, was, and MATLAB , as we know it today, is a high-level PL with support for 2D / 3D graphics, various mathematical functions, an interactive programming environment, numerical calculations and problem solving. External interfaces allow it to integrate with third-party applications and programming languages. More than 1,000,000 engineers and scientists around the world use MATLAB and pay a substantial price for it .


With great delay, open source programs are included in the game. It was only in the 1990s that the mathematical packages GNU Octave, Scilab and competed with the leader of computational programming.


Originally conceived as a software tool for designing a chemical reactor and named after the professor of chemistry Oktav Levenshpil , who taught the author of the mathematical package, Octave designed to replace the difficult Fortran debugging for students of the University of Texas. Version 1.0 was released on February 17, 1994. The project is developing steadily, and in July of this year, Octave 4.0.3 was released. Waiting for ebuilds .




Octave ’s main mission was, and in the foreseeable future most likely will remain, a suitable replacement for MATLAB, just as OpenOffice / LibreOffice replaces MS Office for those who can count a penny. Actually, for this, Octave has a MATLAB compatible syntax and set of functions. Moreover , incompatibility with MATLAB is considered a bug, however software Themis already has a similar precedent, and this is not considered a copyright violation. In this regard, it can be considered Octave software clone . The truth about full compatibility is not yet talked about, but work in this direction does not stop.


Octave written in C++ using the standard template library, has an interactive command interface, supports extensions — dynamically loadable modules in the native language or C, C++, Fortran , etc. Like MATLAB , in algebraic calculations, Octave uses the Basic Line libraries of Algebra Subroutines (BLAS) and Linear Algebra Package (LAPACK).


Installation


Installing Octave in Linux is no different from installing other programs. On Gentoo Linux, we run:


 $ sudo emerge -av octave 

Debianshchik do the same with apt .


 $ sudo aptitude install octave 

For SUSE and Arch, everything is also very simple, but users of the Red Cap and CentOS will have to tinker a bit. Attempting to install Octave slight movement of the brush fails, the package in the repositories could not be found.


 [root@server ~]# yum install octave  : langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. HighAvailability | 4.1 kB 00:00:00 ResilientStorage | 4.1 kB 00:00:00 server | 4.1 kB 00:00:00 vmware-tools | 951 B 00:00:00    octave  . :   

Fortunately, there is a workaround . You must first install the epel-release package.


 [root@server ~]# wget https://dl.fedoraproject.org/pub/epel/7/x86_64/ [root@server ~]# yum localinstall epel-release-6-7.noarch.rpm 

And only after that yum install octave will work.
Finally, everything is ready and the program is installed.


 [root@server ~]# octave GNU Octave, version 3.8.2 Copyright (C) 2014 John W. Eaton and others. This is free software; see the source code for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'. Octave was configured for "x86_64-redhat-linux-gnu". Additional information about Octave is available at http://www.octave.org. Please contribute if you find this software useful. For more information, visit http://www.octave.org/get-involved.html Read http://www.octave.org/bugs.html to learn how to submit bug reports. For information about changes from previous versions, type 'news'. octave:1> 

Matrix operations


Let's not waste time and do operations that can be repeated with the help of bc and awk , which were discussed last time. Let's play a little with matrices.


First simple matrix transposition:


 octave:1> A=[1 3 5; 2 4 6] A = 1 3 5 2 4 6 octave:2> A' ans = 1 2 3 4 5 6 

Let's try to solve a system of linear equations:


 x + y + z = 9 2x + 4y - 3z = 1 3x + 6y - 5z = 0 

We drive in the matrix A , the vector b and solve the equation Ax = b in the matrix form


 octave:1> A=[1 1 1; 2 4 -3; 3 6 -5] A = 1 1 1 2 4 -3 3 6 -5 octave:2> b=[9; 1; 0] b = 9 1 0 octave:3> x=A\b x = 7.00000 -1.00000 3.00000 

We find the determinant and the eigenvalues ​​of the matrix.


 octave:4> det (A) ans = -1.00000 octave:5> eig (A) ans = -2.88897 2.76372 0.12525 

Complex numbers are also supported in calculations.


 octave:6> A=[-3 0 2; 1 -1 0; -2 -1 0] A = -3 0 2 1 -1 0 -2 -1 0 octave:7> x=det (A) x = -6 octave:8> y=eig(A) y = -1.00000 + 1.41421i -1.00000 - 1.41421i -2.00000 + 0.00000i 

Functions and Variables


In Octave variables and functions are much easier to create than, for example, in Java or C. In the example of matrices, we have already seen how to declare variables. Creating a new function has the following syntax


 function [res1, res2, ..., resM] = _ (arg1, arg2, ..., argN)   endfunction 

As a rule, a new function is created either in a separate file or in the Octave script file.
before her first call. If you intend to use a custom function in different script files, then, of course, it is preferable to create it in a separate file. In GNU Octave, files with functions have the extension .m and are loaded automatically. The file name must strictly coincide with the function name.

Write a function to solve the quadratic equation ax² + bx + c = 0


 octave:9> function [x1,x2] = quadr(a, b, c) > D = sqrt(b^2-4*a*c); > x1 = (-bD)/(2*a); > x2 = (-b+D)/(2*a); > endfunction octave:10> [y1,y2]=quadr(a, b, c) y1 = 2 y2 = 3 

Graphical interface


Actually, we’ve been here for the math of the command line, but for the time being it’s not clear how to display the function graph. However, there is no secret here - Gnuplot used for these purposes. So you can portray the Lorenz Attractor by installing the additional package odepkg .


  function [vyd] = froessler (vt, vx) vyd = [- ( vx(2) + vx(3) ); vx(1) + 0.2 * vx(2); 0.2 + vx(1) * vx(3) - 5.7 * vx(3)]; endfunction A = odeset ('MaxStep', 1e-1); [t, y] = ode78 (@froessler, [0 70], [0.1 0.3 0.1], A); subplot (2, 2, 1); grid ('on'); plot (t, y(:,1), '-b;f_x(t);', t, y(:,2), '-g;f_y(t);', \ t, y(:,3), '-r;f_z(t);'); subplot (2, 2, 2); grid ('on'); plot (y(:,1), y(:,2), '-b;f_{xyz}(x, y);'); subplot (2, 2, 3); grid ('on'); plot (y(:,2), y(:,3), '-b;f_{xyz}(y, z);'); subplot (2, 2, 4); grid ('on'); plot3 (y(:,1), y(:,2), y(:,3), '-b;f_{xyz}(x, y, z);'); 



The most convenient graphical environment for working with Octave is the QtOctave program. The latter has already stabilized and is included in the package since the release of Octave 4.0 .


What next?


The question may arise: why do we even need open math packages? Office applications are needed by everyone, but far from everyone needs to solve the Poisson equations sitting at home using the Laplace transform. For universities, MATLAB costs much less than for individuals and commercial organizations. Commercial organizations, if need be, will find money, while ordinary people may do mathematics at universities or consider it a bar.


Of course, this is a misconception. Scientific calculations performed using open source software have an additional “level of protection”, because if you wish, anyone can repeat the same calculations and check the validity of the results . The same calculations performed on expensive software partially cut off the possibility of checking the results . The problem is actually much wider (English text) and not only in open or proprietary math programs. It is no secret that scientific journals, as a rule, do not require authors to provide data and methods sufficient to guarantee the repetition of experimental results, model verification. Especially economists and financiers are often guilty of this, simply classifying their data. Verification of calculations and conclusions among a sample of an array of articles with “classified” data gave unexpected results (English text). Science, like software, must be open , which is why open math packages have value for all of society.


Recommended reading


In addition to the last book, the rest of the materials used in the article can be easily found on the Internet. Half of the above links lead to English pages. I would be happy to briefly report what they are talking about or help with the translation.



')

Source: https://habr.com/ru/post/312004/


All Articles