Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf ·...

20
Introduction Octave Code Comparisons Introduction to GNU/Octave A. Mani 9/1B, Jatin Bagchi Road Kolkata-700029 India Department of Pure Mathematics, C.U. E-Mail: a.mani .cms @gmail .com Homepage: http://www.logicamani.co.cc Software Freedom Day’2011, Kolkata

Transcript of Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf ·...

Page 1: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Introduction to GNU/Octave

A. Mani

9/1B, Jatin Bagchi RoadKolkata-700029 India

Department of Pure Mathematics, C.U.E-Mail: a.mani .cms@gmail .com

Homepage: http://www.logicamani.co.cc

Software Freedom Day’2011, Kolkata

Page 2: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

ABSTRACT

In this talk, I will give a brief introduction to GNU/Octave with someemphasis on migration issues.

Page 3: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Outline

1 Introduction

2 Octave Code

3 Comparisons

Page 4: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Myself

• Research in Mathematics (Algebra, Rough Sets, Algebraic Logic,Logic and Ordered Structures.)

• Teaching and Independent Consultancy in Statistics, R, KDD, FOSSand Specifications.

• FOSS Activism: Ilug-Cal.info (www.ilug-cal.info), Fedora QA,LQ (www.linuxquestions.org), ...

Page 5: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Octave

• is a software for Numeric Computations (not symbolic).• respects the four freedoms of FOSS: to use, to share, to modify andto distribute modifications. Licensed GNU GPL-2, 3

• performs best in GNU/Linux 64-bit, Free BSD.• Extensible scripting language (interpreted) with good graphicssupport.

• Octave is not Naive.

Page 6: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Installation

• Package manager of GNU/Linux distro:Octave-audio, -image, ...• Vast collection of Contributed Packages (Tool boxes):

http://octave.sf.net/.• 725 Page Manual:

http://www.gnu.org/software/octave/doc/interpreter• > pkg install image ∗ .gz• > pkg list• Compilation from source: GNU/Make, GCC (compulsory).

Page 7: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

IDE

• Octave-mode (Emacs), QtOctave, XOctave, OctaveNB, Geany• Octavegtk, fltk, ... (for GUI programs)

Page 8: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Matrices

• > A= [1,2,3;4,3,2;7,2,9], eig(A)

• > b = [3,2,2]′, inv(A) ∗ b, A\b• > lu(A),comp(eye(3)A;B zeros(2,2))• > a = 3 : 6,b = 5 : 9,c = [ab] (concatenation)• > (inv(A′A)) ∗A′ == pinv(A)

• > x = linspace(0,2 ∗ pi ,100);• > y = cos(sin(x)), plot(x ,z ,′ :′

,x ,y ,′−′), grid ,xlabel(′angle′), print(′ab.eps ′,′−deps ′)

Page 9: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Basic Tasks

• > save abc Save Workspace as abc.mat• > save filename vbl .• > load filename Load vbl.• > clear var − name• Function: cast(val , type): int64, uint64,logical, char,double, doublecomplex.

• Define a Class from a Struct: class(< struct >,< name >)

Page 10: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Functions

• use unique names for each function• function [out1,out2, ..] = name(input1, ...)

• < function body >• endfunction• script name = function name• Variable number of arguments functionval = smallest(varargin)• Unnamed arguments

Page 11: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Scripts

• Just put the code in a file : alertscr.m• To run: > scr

• Comments: %, #

• Remember: > what

• Scripts can be ’standalone’.

Page 12: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Solvers

• Polynomial: > c = [2,1,0,6,7,9], roots(c)• Solvers for sets of nonlinear equations• All Standard Optimization Problems• Many More ...

Page 13: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Tool Boxes: Examples

• Fuzzy Computations:fuzzy-logic-toolkit, fl-core• Image Processing: image• Signal Processing: signal• Optimization: optim• Video Processing (via ffmpeg) :video• 3D Visualization: vrml

Page 14: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Other Languages

• FORTRAN, C, C++, Python ... code can be used within Octave• Typically as dynamically linked libraries (shared libraries)• Easy to write C, C++ code that uses libraries of Octave likeliboctave.so

• FORTRAN-77, C: Long Robust Algorithms

Page 15: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Code: Octave vs Matlab

• Some Octave constructs will not work in Matlab• –traditional mode more compatible• %, # for comments. C style increment decrement.• default search paths for file names - octave is better• printf - Octave, fprintf - Matlab• Whitespace [1,2] ′ , Line Breaks: rand(1... rand(1,

• 2)

Page 16: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Code: Octave vs Matlab

• 90% Code compatibility• Many tool boxes have exact equivalents• OR contain the functionality of specific Tool Boxes.• Need a specific Tool Box?• Get it Done if you do not have the time.

Page 17: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Octave vs Scilab

• Scilab-often better, not mlab code compatible• Graphics, Parallel, NN, GA, Speed: Scilab• Support: http://www.scilab.in/Completed_Books• Fuzzy Computation, Optim: Octave• Scilab Image Processing; SIVP Toolboxes• Code Sprints:

http://www.gnu.org/software/octave/sprint.html

Page 18: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

References I

http://www.gnu.org/software/octave

http://octave.sourceforge.net

http://www.scilab.org

Baudin, M,Scilab is not Naive, 2010http://forge.scilab.org/index.php/p

Page 19: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

Software is a form ofKnowledge and Knowledge

ought to be Free!

Page 20: Introduction to GNU/Octave - Indian Statistical Instituteacmsc/SFD2011/octavsfdr9.pdf · IntroductionOctave CodeComparisons Introduction to GNU=Octave A.Mani 9/1B, Jatin Bagchi Road

Introduction Octave Code Comparisons

CHEERS !