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….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s