sabato 15 marzo 2014

Octave Cheat Sheet

Octave Cheat Sheet:



I found this very interesting cheat sheet about Octave here:
Source http://altons.github.io/octave/2013/05/05/octave-commands-cheat-sheet/

I think it is very great, but there are couple of commands missing (like PS1, which in my opinion makes your life better!).
So I have taken it and extendend with some commands.

Management & Help

Task
Command
exits software
quit or exit
list variables in session
who
list variables in session with info about type
whos
deallocate variable(s)
clear varname
displays search path
path
Adds path to search
addpath
clear screen
clc
list .m files in dir
what
search for keyword
lookfor
displays help for function
help funname
Parse and execute the contents of FILE
source(".octaverc")
List installed packages
pkg list
Install packages
pkg install [-auto or -noauto] pkg_name.tar.gz
Describe packages
pkg describe pkg_name
Load/Unload package
pkg load/unload pkg_name
Uninstall package
pkg uninstall pkg_name



Shell Commands

Task
Command
Change Linde starter
PS1(‘desired Starter’); %PS1(‘>> ‘);
change working directory to dir
cd dirname
print working directory
Pwd
print directory listing
ls
return value of named environment variable
getenv (string)
execute arbitrary shell command string
system (cmd)
Load the file
 
You will load a file and make it available with a
Varible having same name of the file without extension
Load FILENAME_IN_PWD
Load(‘FILENAME_IN_PWD’);
Save variable into file
 
You will load a file and make it available with a
Varible having same name of the file without extension
save  FILENAME_IN_PWD VARIABLE_NAME;



Special Operators

Definition
Operator
Example
comment
%
% Initialising
wildcard
*
clear p*
array
[ ]
[1 2; 3 4]
Cell Arrays
{ }
p = { 10 , 15 , "rectangle" }; p{1} =10
ranges
start:step:stop (default step=1)
1:10 or 1:10:100
variable assignment
=
A=magic(3);
do not display
;
a=1;



Workflow

Task
Command
Suspend the execution of the program.
pause; or pause(5)
print string to the stout
fprintf("Hello World")



Vectors & Matrices

Rules

· Square brackets delimit literal matrices.
· Commas separate elements on the same row.
· Semicolons separate rows.
· Commas may be replaced by spaces, and
· semicolons may be replaced by one or more newlines.
· Elements of a matrix may be arbitrary expressions, provided that all the dimensions agree.
· Optional: Matrices are denoted by uppercase letters A,B,X etc while vectors by lowercase letters a,b,y etc.
 

Example
Expression
enter a row vector
[ x, y, ... ]
enter a column vector
[ x; y; ... ]
enter a 2×2 matrix
[ w, x; y, z ]
Return a row vector with N linearly spaced elements between BASE and LIMIT
linspace (BASE, LIMIT, N)
Similar to linspace except that the values are logarithmically spaced
logspace(BASE, LIMIT, N)
Higher Dimensional Arrays
B(z,y,z)=1
Sorting arrays
sort(A); or sort(A, 'descend');
Return true if any element in array is non-zero
any(A)
Return true if all element are non-zero
all(A)
Return a vector of indices of a matrix
[i , j] = find(A<0.5)
 
 

 

Array Operations

Task
Function
select elements of a vector
A(i)
select elements of a matrix
A(i,j)
select rows (columns) corresponding to the elements of a range
A(1:2,1); A(1:2,1:5);
select all rows (columns)
A(:,1); A(3,:);
Delete a row/column of a matrix
A(2,:)= [] or A(:,5) = []

 



Linear Algebra

Task
Function
dimensions of the array
size
returns larger dimension
length
allocates array with 0’s
zeros
allocates array with 1’s
ones
transpose array
'
Compute the determinant of A.
det(A)
inverse of matrix
inv
pseudo inverse of matrix
pinv
Calculate Eigenvalues / Eigenvectors of matrix A
eig(A) or [V,L] = eig(A)
Identity matrix
eye
Compute the rank of a Matrix
rank(A)
returns the lower triangular part of A
tril(A)
returns the upper triangular part of A
triu(A)
Create an N-by-N magic square
magic
Compute the dot product of two vectors
dot(A,B)
Compute the vector cross product of two 3-dimensional vectors X and Y
cross(X,Y)
interval range arrays
v = 1:0.1:2
%vector = STARTING_AT:DELTA:ENDING_AT



Plotting
Task
Command
Example
2-D plot
plot
plot(x,y); plot(x, a+b+c);
Surface plot
surf
surf(theta0, theta1, J);
Contour plot
contour
contour(theta0, theta1, J, logspace(-2, 3, 20))
Specify x-, y-, or z-axis labels for the current axis
[x,y,z]label
xlabel('k lags')
Set a plot window to plot window N
figure
figure;plot(x, a);
close figure window(s)
close
close [***(N),all,all hidden***]
new graphic objects are added to the plot
hold
hold on;plot(x, b);
Clear plot and restore default graphics settings
hold
hold off;
Display a legend for the axes
legend
plot(X,Y);legend('Temperature', 'Gas Demand');
give a title
title
title('myTitle');
export the chart
print
Print –dpng ‘filename.png’



Math Functions

Type
Function
Examples
Sum of elements along dimension DIM
sum
sum([1 2 3 4])
Product of elements along dimension DIM
prod
prod([1 2 3 4)]
Trigonometric
sin, cos, tan
floor((1+tan(1.2)) / 1.2)
Inverse Trigonometric
asin, acos, atan
Natural Logarithm
log
Base 10 Logarithm
log10
log10(100)/log10(10)
Exponentiation
exp
exp(0)
Absolute value
abs
abs(-3)
Square Root
sqrt
sqrt(3^2 + 4^2)
X raised to the Y power
power(X,Y)
power(3,2)
Real part of complex number
real
real(3+5I)
Imaginary part of complex number
imag
imag(3+5I)
Evaluate polynomials
polyval
polyval([2 10.1 0 6],0)
Write formatted polynomial
polyout
polyout([2 -3 1],"x")
Return the largest integer not greater than X
floor
floor(1.9)
Return the smallest integer not less than X
ceil
ceil(3.7)
Return the integer nearest to X
round
round(1.9)
Truncate fractional portion of X and return the integer portion
fix
fix(pi)



Stats Functions

Task
Example
Function
Uniform random numbers btw 0 and 1 (both excluded)
rand
rand(3,5)
Normal(0,1) random numbers
randn
randn(1,3)
Gamma distribution
randg
randg
Exponential distribution
rande
rande(1,10)
Poisson distribution
randp
randp(0.5,3,3)
Min value by column
min
min(A)
Max value by column
max
max(A)



Constants

Name
Expression
Default Variable
ans
Pi
pi
Euler's number
e
Imaginary number
i, j and I
Infinity
inf
Not a Number
NaN
machine precision
eps
true/false
logical 1/0



Logical Operators

Expression
Operator
is greater than
> 
is less than
< 
is greater than or equal to
>=
is less than or equal to
<=
is equal to
==
is not equal to
= or !=
AND with short circuiting
&&
with short circuiting
||
AND
&
OR
|
NOT



Auxiliary Functions

Task
Function
Check a scalar
isscalar(a)
Check a vector
isvector(b)
Check a matrix
ismatrix(b)
is func available
is TAB TAB
Type info
typeinfo(b)



String Functions

Task
Function
Example
Compare 2 strings
strcmp
strcmp("hello","Hello")



Import & Export Data

Task
Command
Example
Read the matrix DATA from a text file
dlmread (FILE, SEP, R0, C0)
dmlread("virus.dat",",",1,1);
Write data to a text file
dlmwrite (FILE, M, DELIM, R, C)
dlmwrite("out.txt",yhat,";",1,1);
Read the csv files
csvread (FILENAME, DLM_OPTS)
csvread("input.csv");
Write csv files
csvwrite (FILENAME, X, DLM_OPTS)
csvwrite("output.csv", yhat);

Defining Functions

Simplest Form

  function name
      body
  end

Example:

  function wakeup
        printf ("\a");
    end

Passing Params

  function name (arg-list)
        body
    end

Example:

  function wakeup (message)
        printf ("\a%s\n", message);
    end
 
  wakeup ("Rise and shine!");

Return Single Value

  function ret-var = name (arg-list)
        body
    end

Example:

  function retval = avg (v)
        retval = sum (v) / length (v);  
    end

Return Multiple Values

  function [ret-var1,ret-var2,…,ret-varn] = name (arg-list)
        body
    end

Example:

  function [mu,sigma] = basicStat(X)
    mu = mean(X);
    sigma = std(X);
  end



Statements

IF Statement

  if (condition)
       then-body
    elseif (condition)
       elseif-body
    else
       else-body
    end

Example:

   if (rem (x, 2) == 0)
       printf ("x is even\n");
     elseif (rem (x, 3) == 0)
       printf ("x is odd and divisible by 3\n");
     else
       printf ("x is odd\n");
     end

Note that the elseif keyword must not be spelled else if, as is allowed in Fortran. If it is, the space between the else and if will tell Octave to treat this as a new if statement within another if statement's else clause

SWITCH Statement

    switch (X)
       case 1
         do_something ();
       case 2
         do_something_else ();
       otherwise
         do_something_completely_different ();
     end

Example:

     A = 7;
     switch A
       case { 6, 7 }
         printf ("variable is either 6 or 7\n");
       otherwise
         printf ("variable is neither 6 nor 7\n");
     end

One advantage of using the switch statement compared to using if statements is that the labels can be strings

  switch (X)
       case "a string"
         do_something
       ...
     endswitch

WHILE Statement

  while (condition)
       body
     end

Example:

     fib = ones (1, 10);
     i = 3;
     while (i <= 10)
       fib (i) = fib (i-1) + fib (i-2);
       i++;
     end

DO-UNTIL Statement

     do
       body
     until (condition)

Example:

     fib = ones (1, 10);
     i = 2;
     do
       i++;
       fib (i) = fib (i-1) + fib (i-2);
     until (i == 10)

FOR Statement

     for var = expression
       body
     end

Example:

  fib = ones (1, 10);
     for i = 3:10
       fib (i) = fib (i-1) + fib (i-2);
     end

 




Start crash course on Octave

Octave Cheat Sheet:

Octave is an easy programming language used for solving machine learning problems.
I am transcripting here the lesson I am following on Standford from Andrew Ng.
I think that this script is a perfect crash course:

 GNU Octave, version 3.2.4  
 Copyright (C) 2009 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 "i686-pc-mingw32".  
 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/help-wanted.html  
 Report bugs to <bug@octave.org> (but first, please read  
 http://www.octave.org/bugs.html to learn how to write a helpful report).  
 For information about changes from previous versions, type `news'.  
 octave-3.2.4.exe:1> % here I put comment;  
 octave-3.2.4.exe:1> %first remove the line starter...  
 octave-3.2.4.exe:1> PS1('>> ');  
 >> PS1(' -- ');  
  -- % I will go back to the >> line starter  
  -- PS1('>> ');  
 >>   
 >>   
 >> % basic operators  
 >> 10+3  
 ans = 13  
 >> 4-2  
 ans = 2  
 >> 5*10  
 ans = 50  
 >> 50/5  
 ans = 10  
 >> 2^3  
 ans = 8  
 >>   
 >>   
 >> % logic operators  
 >> 1==2  
 ans = 0  
 >> % 0 is false, 1 is true  
 >> 1==1  
 ans = 1  
 >> 1!=2  
 ans = 1  
 >> 1~=2  
 ans = 1  
 >> 1~=1  
 ans = 0  
 >> 1 && 1  
 ans = 1  
 >> 1 && 1 % and operator  
 ans = 1  
 >> 1 || 1 % or operator  
 ans = 1  
 >> 0 || 0  
 ans = 0  
 >> 1 || 2  
 ans = 1  
 >> 1 && 2  
 ans = 1  
 >> % better than to say:  
 >> % 0 is false; != 0 is true  
 >> 1 && -1  
 ans = 1  
 >> % 0 is false; != 0 is true, by default is represented by 1  
 >>  
 >>  
 > % variable assignment  
 >> a=3  
 a = 3  
 >> % suppress the output return line  
 >> a=3;  
 >>  
 >>  
 >> % display...  
 >> a = pi;  
 >> a  
 a = 3.1416  
 >> format long  
 >> a  
 a = 3.14159265358979  
 >> format short  
 >> a  
 a = 3.1416  
 >> disp(a);  
  3.1416  
 >> format long  
 >> disp(a);  
  3.14159265358979  
 >> disp(sprintf('only 2 decimals: %0.2f', a))  
 only 2 decimals: 3.14  
 >> disp(sprintf('only 5 decimals: %0.5f', a))  
 only 5 decimals: 3.14159  
 >> disp(sprintf('only 10 decimals: %0.10f', a))  
 only 10 decimals: 3.1415926536  
 >>  
 >>  
 >>  
 >>  
 >> % matrix  
 >>  
 >> A= [1 2; 3 4; 5 6]  
 A =  
   1  2  
   3  4  
   5  6  
 >> v = [1 2 3 ]  
 v =  
   1  2  3  
 >> % v is an array  
 >> % v has dimension 1 x 3  
 >> % let  
 >> % let's see now a vector, so a v having dimensions 3 x 1  
 >> v = [1;2;3]  
 v =  
   1  
   2  
   3  
 >> % now interval range arrays  
 >> v = 1:0.1:2  
 v =  
  Columns 1 through 3:  
   1.00000000000000  1.10000000000000  1.20000000000000  
  Columns 4 through 6:  
   1.30000000000000  1.40000000000000  1.50000000000000  
  Columns 7 through 9:  
   1.60000000000000  1.70000000000000  1.80000000000000  
  Columns 10 and 11:  
   1.90000000000000  2.00000000000000  
 >> %meta syntax is:  
 >> % vector = STARTING_AT:DELTA:ENDING_AT  
 >> v = 1:6  
 v =  
   1  2  3  4  5  6  
 >> % by default the delta is 1  
 >> %known matrixes  
 >>  
 >> w = ones(1,3)  
 w =  
   1  1  1  
 >> w = zeros(1,3)  
 w =  
   0  0  0  
 >> %random  
 >> r = rand(3,3)  
 r =  
   0.567402857515203  0.301184329285262  0.830303110034579  
   0.703828527889656  0.800885232762887  0.876086413236985  
   0.340103401896943  0.675342891105335  0.242842578966916  
 >> r = rand(3,3)  
 r =  
   0.4699204825787972  0.4124515864382686  0.9827575401589607  
   0.0118059148057477  0.4695877000100873  0.6805667208195990  
   0.9936553157929338  0.9727665315772230  0.1624513014665614  
 >> r = rand(3,3)  
 r =  
   0.17186568773859429  0.22565708518331024  0.28727929514480505  
   0.91982345102655927  0.77380397661275846  0.00322060012954977  
   0.42991791381680561  0.25561768241919586  0.62438590998652621  
 >> % some more complex stuff  
 >>  
 >>  
 >>  
 >>  
 >> g = -6 + sqrt(10)  
 g = -2.83772233983162  
 >> rand(1,1000)  
 >> g = -6 + sqrt(10)*randn(1,10000)  
 >> hist(g)  
 >> % here comes out a histogram window...  
 >> hist(g,50)  
 >> % here it comes out a histogram window with 50 columns  
 >>  
 >>  
 >>  
 >> % generate the identity matrix  
 >> I = eye(3);  
 >> I  
 I =  
 Diagonal Matrix  
   1  0  0  
   0  1  0  
   0  0  1  
 >>  
 >>  
 >> % helper  
 >> help eye  
 >> % here it comes out a helper description of the function...  
 >>  
 >>  
 >> A = [1,2;3,4;5,6]  
 A =  
   1  2  
   3  4  
   5  6  
 >> sz = size(A) %get the dimensions of the matrix  
 sz =  
   3  2  
 >> size(sz)  
 ans =  
   1  2  
 >> size(A,1) %get the nr of rows of the matrix  
 ans = 3  
 >> size(A,2) %get the nr of cols of the matrix  
 ans = 2  
 >> v = [1,2,3,4]  
 v =  
   1  2  3  4  
 >> length(v) %get the max dimension  
 ans = 4  
 >> length(A)  
 ans = 3  
 >> who %results below come from a different session.... values probably wrong... it just gives an idea  
 Variables in the current scope:  
 A  ans sz  v  
 >> whos %results below come from a different session.... values probably wrong... it just gives an idea  
 Variables in the current scope:  
  Attr Name    Size  
  ==== ====    ====  
     A      3x2  
     ans     9x1  
     sz     1x2  
     v      1x4  
 Total is 41 elements using 328 bytes  
 >>  
 >> A(3,2) % gives you the element in position: row 3, col 2  
 ans = 6  
 >> A(3,:) % gives you the elements in position: row 3, complete row  
 ans =  
   5  6  
 >> A(:,2) % gives you the elements in position: col 2, complete column  
 ans =  
   2  
   4  
   6  
 >> A([1 3], :) % gives you the elements in position: row 1 and 3, complete row  
 ans =  
   1  2  
   5  6  
 >> A(:, 2) = [10;11;12] % assign complete column  
 A =  
   1  10  
   3  11  
   5  12  
 >> A = [A, [100; 101; 102]] % extend the matrix, adding a new column  
 A =  
    1  10  100  
    3  11  101  
    5  12  102  
 >> size(A)  
 ans =  
   3  3  
 >> A(:) % transform a matrix into a vector  
 ans =  
    1  
    3  
    5  
   10  
   11  
   12  
   100  
   101  
   102  
 >>  
 >>  
 >> A = [1,2;3,4;5,6]  
 A =  
   1  2  
   3  4  
   5  6  
 >> B= [11 12; 13 14; 15 16]  
 B =  
   11  12  
   13  14  
   15  16  
 >> C = [A B] % put two matrix one beside the other  
 C =  
   1  2  11  12  
   3  4  13  14  
   5  6  15  16  
 >> C = [A; B] % put two matrix one on top of the other  
 C =  
   1  2  
   3  4  
   5  6  
   11  12  
   13  14  
   15  16  
 >>  

martedì 11 marzo 2014

Oracle: log DML errors






When you perform a massive insert or update generating errors, it is very hard to recognize which are the records which are generating the errors.

The topic in this post is about performing a DML statement and storing the wrong error in an additional table rather than crashing the statement itself.


Suppose you run:
INSERT INTO dest
SELECT *
FROM   source;

SELECT *
       *
ERROR at line 2:
ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


SQL>

But you would like:
INSERT INTO dest
SELECT *
FROM   source
LOG ERRORS INTO err$_dest ('INSERT') REJECT LIMIT UNLIMITED;

99998 rows created.

SQL>



How to do this?
First build the "Error table", which is a clone of your destination table, having no format for columns (every column is varchar).

LOG ERRORS [INTO [schema.]table] [('simple_expression')] [REJECT LIMIT integer|UNLIMITED]


Example:
-- Create the error logging table.
BEGIN
  DBMS_ERRLOG.create_error_log (dml_table_name => 'dest');
END;
/

PL/SQL procedure successfully completed.

SQL>


Which generate:

SQL> DESC err$_dest
 Name                              Null?    Type
 --------------------------------- -------- --------------
 ORA_ERR_NUMBER$                            NUMBER
 ORA_ERR_MESG$                              VARCHAR2(2000)
 ORA_ERR_ROWID$                             ROWID
 ORA_ERR_OPTYP$                             VARCHAR2(2)
 ORA_ERR_TAG$                               VARCHAR2(2000)
 ID                                         VARCHAR2(4000)
 CODE                                       VARCHAR2(4000)
 DESCRIPTION                                VARCHAR2(4000)

SQL>




At this point you can invoke the insert in the way above.
Update and merge and code for a complete example are in the link below (source of this post).


Source:
http://www.oracle-base.com/articles/10g/dml-error-logging-10gr2.php