FUN WITH LINUX

First steps with GNU Octave (1) - Modulo-Fun

11 November 2014

GNU Octave is some kind of programming language for mathematical use cases. It’s mostly compatible with MATLAB and it’s Open-Source.

I just started to get familiar with GNU Octave and i really like it. Here i record my “first steps” and post my scripts..

Print out multiplication -and additiontable:

#!/usr/bin/octave -qf

function modtable (m)

printf("+ ");
for i = 0:m-1
printf("%0.2d ",i)
endfor
printf("\n",i)

for i = 0:m-1
printf("%0.2d|",i);
        for j = 0:m-1
                g = mod( (j+i),m);
                if( (j+i) < m )
                        printf("%0.2d ",j+i);
                else
                        printf("%0.2d ",g);
                endif
        endfor
        printf("\n");
endfor

printf("\n");

printf("* ");
for i = 0:m-1
printf("%0.2d ",i)
endfor

printf("\n");

for i = 0:m-1
printf("%0.2d|",i);
        for j = 0:m-1
                g = mod( (j*i),m);
                if( (j*i) < m )
                        printf("%0.2d ",j*i);
                else
                        printf("%0.2d ",g);
                endif
        endfor
        printf("\n");
endfor
endfunction


if(nargin != 1)
        printf("usage: %s \n",program_name());
        exit();
endif

arg_list = argv();
modtable(str2num(arg_list{1}))

Fun with recursion(calculating gcd with euklid):

#!/usr/bin/octave -qf

function retval = gcd_euklid(a,b)
        retval = 0;
        retmod = mod(a,b);
        retdiv = a/b;
        if(retmod != 0)
                retval = gcd_euklid(b,retmod);
        else
                retval = b;
                return;
        endif
endfunction

if(nargin != 2)
        printf("usage: %s \n",program_name());
        exit();
endif

arg_list = argv();
printf("%d \n",gcd_euklid(str2num(arg_list{1}),str2num(arg_list{2})));

rot13-algorithm:

#!/usr/bin/octave -qf

function retval = getcharnumber (char)
        alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXY";

        for i=1:length(alphabet)
                if( alphabet(i) == char)
                        retval = i;
                        return;
                endif
        endfor

        retval = -1;
        return;
endfunction

function rot13 (m)
        alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXY";

        for i=1:length(m)
                printf("%c",alphabet(mod(getcharnumber(m(i))+13,26)))
        endfor
        printf("\n");
endfunction


if(nargin != 1)
        printf("usage: %s \n",program_name());
        exit();
endif

arg_list = argv();
rot13(toupper(arg_list{1}))
[ Mathematics  Octave  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti