Skip to content

Math I: Vectors & Matrices

nnnik edited this page Aug 17, 2018 · 2 revisions

In this section I will discuss methods, that are used for both matrices and vectors, according to maths. All math operations are strongly optimized - however they have to create a new matrix unless you use the outputMatrix parameter to provide an already existing matrix object to store their result. The outputMatrix parameter can always be any of the inputs too.

Multiply matrices/vectors by a factor

outputMatrix := newMat.multiplyByFactor(factor [, outputMatrix])

This multiplies all values in newMat by factor.

  • newMat: The matrix whose values you want to be multiplied
  • factor: The factor all values should be multiplied by
  • outputMatrix: The result of the equation

factor should be a valid number or the method may fail.

The result in outputMatrix will have the same size as the input newMat.

When passing outputMatrix as parameter, it will use the matrix object passed as outputMatrix parameter to store the result. Then: outputMatrix should be a matrix that is just as large as the result of the method. The method will not attempt resizing, but will rather fail if the size is wrong.

outputMatrix can be equal to newMat without any problems.

What the method does in detail is: It takes a value from newMat at the place x, y. Then it multiplies that value by the factor. Finally it writes the result of that multiplication into the outputMatrix at the same place x, y. This is repeated for all values.

Add/Subtract 2 matrices/vectors from another:

outputMatrix := newMat.add(secondMat [, outputMatrix])
outputMatrix := newMat.subtract(secondMat [, outputMatrix])
  • newMat: The matrix that you want to add to or subtract from
  • secondMat: The matrix that you want to add to newMat or subtract from newMat
  • outputMatrix: The result of the equation

secondMat should be a matrix with equal size and height as newMat. If that isn't the case the method will fail. secondMat can also be equal to newMat.

The result in outputMatrix will have the same size as newMat and secondMat.

When passing outputMatrix as parameter, it will use the matrix object passed as outputMatrix parameter to store the result. Then: outputMatrix should be a matrix that is just as large as the result of the method. The method will not attempt resizing, but will rather fail if the size is wrong.

outputMatrix can be equal to newMat or secondMat without causing any problems.