Octave: In-built functions (P1)

Octave similar to other language support inbuilt functions. Few of inbuilt functions are listed with examples below.

 

  1. who: Displays all variables currently in memory.
    • Example:
      • Step 1: m1 = [1 2;3 4] This creates a m1 variable in memory and assigns 2×2 matrix.
      • Step 2: m2 = m1 .^2. This results in a matrix where each element of matrix is squared [1 4; 9 16] and result is assigned to m2.
      • Step 3: m1 + m2. Matrix addition[2 6; 12 20]
      • who(). This function display all variables in memory. Variables displayed are “ans”,”m1” &”m2”
  2. clear: Clears variable(s) from memory.
    • Example:
      • Step 1: m1 = [1 2;3 4] This creates a m1 variable in memory and assigns 2×2 matrix.
      • Step 2: m2 = m1 .^2. This results in a matrix where each element of matrix is squared [1 4; 9 16] and result is assigned to m2.
      • Step 3: m1 + m2. Matrix addition[2 6; 12 20]
      • who(). This function display all variables in memory. Variables displayed are “ans”,”m1” &”m2”
      • clear m1. This clears variable m1 from memory
      • clear() to remove all variables from memory.
  3. pwd(): Present Working Directory
    • Example:
      • Step 1: pwd() This results in current working directory. “C:\Users\<User Name>” is default working directory.
  4. cd(): Change Directory:
    • Example:
      • Step 1: pwd() This results in current working directory. “C:\Users\<User Name>” is default working directory.
      • Step 2: cd(“D://Readiness//Data Science//Machine Learning//Octave Tutorials”) will change present working directory to new path.
      • Step 3: pwd(). This displays new modified path of present working director
  • Note:
    • Octave is case sensitive. “who” is different from “WHO” or “Who”
    • “ans” is a default variable that is created by Octave runtime to store result of any computation. It stores only last result of any computation and is overwritten by next operation. Value of result of computation can be accessed by “ans” variable.
Advertisements

Octave: Basic Math operations

Mathematical Operations:

  • Addition: “+” operator is overloaded for different types of data.
    • Simple addition: 3+2 = 5
    • Matrix addition: [1 0; 0 1]+[1 0;0 1] = [2 0;0 2]
    • Complex addition: (1+2i)+(2+3i) = 3 + 5i
    • Strings can not be concatenated. “Hi “+” world” would result in error: operator +: non-conformant arguments (op1 is 1×2 and op2 is 1×5).
      • But if “Hello ”+” World” works but with unexpected result in 108 188 219 222 219 132. Result clearly shows that matrix addition of numbers (characters converted ascii value) is done by octave.
      • Similarly “A” + “B” results in 131. (Same is above both A and B are converted to their ASCII value and added.)
  • Subtraction: “-“. Similar to “+”, operator is a overloaded operator supporting different data types.
    • Simple subtraction: 3-2 = 1
    • Matrix subtraction: [1 0;0 1] – [1 0; 0 1] = [0 0; 0 0]
    • Complex subtraction: (1+2i) – (3+1i) = –2 +1i
    • As “+” operator does not string concatenation but matrix addition, in Octave “-“ operator can be used on strings (though usefulness of such operation is doubtful).
      • “Hi” – “Hi” would result in 0 0 a 1×2 matrix.
  • Multiplication: “*” operator is again overloaded.
    • Simple multiplication: 3*2 = 6
    • Matrix multiplication: [1 0;0 1] * [1 0; 0 1] = [1 0; 0 1]. Matrix multiplication of identity matrices.
      • Matrix multiplication is only possible, if matrices are conformant for multiplication. A matrix of 3×2 can be multiplied with 2×1 or 2×2 or 2×3 or 2xN matrix.
        • Example: [1 2; 3 4; 5 6] * [1 0 0; 1 1 0] is (3×2 * 2×3 = 3×3) equal to [3 2 0; 7 4 0; 11 6 0].
      • But if dimensionalities are different and are not conformant for multiplication Octave runtime throws error.
        • Example: [1 2 3;4 5 6; 7 8 9]*[1 0;0 1] results in error. error: operator *. nonconformant arguments (op1 is 3×3 and op2 is 2×2)
    • It is clear from addition and subtraction operations that there is no pure character operations in Octave but it does convert them to corresponding matrices and perform a matrix operations
  • Division: “/” operator is again overloaded.
  • Simple division: 3/2 = 1.5000
  • Matrix division: [1 0;0 1] / [1 0; 0 1] = [1 0; 0 1].
  • Steps to perform matrix division:
    • Get divisor matrix
    • Compute determinant of matrix and check if it is non zero.
    • If it is non zero, compute inverse of divisor matrix.
    • multiply inverse of matrix with numerator matrix to compute multiplication.
    • To prove the point, similar to 3 / 3 = 1 in scalar number, m1 / m1 = identity in matrix where m1 is a matrix.
      • So [1 2; 3 4]  / [1 2; 3 4] =[1 0; 0 1]

So, this completes basic operators (addition, subtraction, multiplication and division). As can be seen from above example Octave is heavily oriented towards matrix computations.

Next post to deal with remaining operators….

Octave – Introduction

Octave is an “Open Source Alternative” for widely used MATLAB. Octave can be programmed to solve numerical computations along with graphical representation data. Octave is generally used for solving ML problems.

Download link for Octave http://www.gnu.org/software/octave/.

Octave as detailed in article is compatible with MATLAB http://wiki.octave.org/FAQ#Porting_programs_from_Matlab_to_Octave and programs written in Octave can be ported to MATLAB.

Other tools in similar space are

Octave is a command line utility but there is a GUI tool that can be used along with Octave (similar to RStudio for R). To download click http://www.malinc.se/math/octave/mainen.php.

There is also a Cloud version of Octave https://www.verbosus.com/?lang=en (Ofcourse did not try it yet.. will try it and update with my findings)….

So, lets get our hands dirty..

Guru