Skip to content

LinearAlgebra.js

deployment script via Travis CI edited this page Sep 25, 2018 · 3 revisions

Functions

vector(p0, p1)Array.<number>

creates vector between points p0 and p1 of equal dimensions

vectorAdd(v0, v1)Array.<number>

adds vector or point v0 to vector or point v1

vectorSubtract(v0, v1)Array.<number>

Subtracts vector or point p1 from vector or point p0. Equal to vector(p1, p0)

vectorMultiply(v, n)number

Multiplies a vector with a number.

vectorDivide(v, n)number

Divides a vector by a number.

vectorDotProduct(v0, v1)Array.<number> | void

calculates the dot product of two vectors of equal dimensions

vectorLength(v)number

calculates the length of a vector using the euclidean distance

distance(p0, p1)number

calculates the euclidean distance between two points

innerProductAngle(v0, v1)Array.<number>

calculates the inner inner product angle in rad of two vectors of equal dimensions

shortestDistance(O, P0, P1)number

calculates the shortest distance between a point O and a line, defined through two points p0 and p1

matrixIdentity(n)Array.<Array.<number>>

returns a n*n identity matrix

matrixAugment(M0, M1)Array.<Array.<number>>

creates a matrix that is basically the two input matrices next to each other

matrixSlice(M, [beginRow], [beginColumn], [endRow], [endColumn])Array.<Array.<number>>

removes rows and columns at the end and/or the beginning of a matrix

matrixEliminate(M0, M1)Array.<Array.<number>>

Sorry, i forgot what this does. THat's the problem with personal projects, there is no reason to hurry or to watch your coding style and then you just forget stuff. I don't think this function it is necessary though.I might delete it soon.

isSqareShortCheck(M, [throwIfNot])boolean

Checks if a matrix is n*n. Only checks first row internally, so make sure the matrix is well-formed.

matrixInvert(M)Array.<Array.<number>>

Calculates inverted matrix M⁻¹. Based on a function by Andrew Ippoliti.

vector(p0, p1) ⇒ Array.<number>

creates vector between points p0 and p1 of equal dimensions

Kind: global function
Returns: Array.<number> - the vector from the input points
Throws:

  • Error 'input lengths are not equal' if p0 & p1 have different lengths (= different number of dimensions)
Param Type Description
p0 Array.<number> first point
p1 Array.<number> second point

Example

LinearAlgebra.vector([0, 0], [0, 0]) //is equal to [0, 0]

Example

const
      p0 = [0, 2, 3, 1],
      p1 = [2, 4, 5, 3];
  LinearAlgebra.vector(p0, p1) //is equal to [2, 2, 2, 2]
  LinearAlgebra.vector([1, 2], [3, 4, 5]); //throws an error

vectorAdd(v0, v1) ⇒ Array.<number>

adds vector or point v0 to vector or point v1

Kind: global function
Returns: Array.<number> - sum of the inputs
Throws:

  • Error 'input lengths are not equal' if v0 & v1 have different lengths (= different number of dimensions)

Todo

  • rewrite vector add, substract, multiply and divide with array.map
Param Type Description
v0 Array.<number> first summand
v1 Array.<number> second summand

Example

LinearAlgebra.vectorAdd([0, 1], [2, 3]) //is equal to [2, 4]
  LinearAlgebra.vectorAdd([0], [1, 2, 3]) //throws an error

vectorSubtract(v0, v1) ⇒ Array.<number>

Subtracts vector or point p1 from vector or point p0. Equal to vector(p1, p0)

Kind: global function
Returns: Array.<number> - difference of the inputs
Throws:

  • Error 'input lengths are not equal' if v0 & v1 have different lengths (= different number of dimensions)
Param Type Description
v0 Array.<number> minuend
v1 Array.<number> subtrahend

Example

LinearAlgebra.vectorSubtract([3, 2], [2, 1]) //is equal to [1, 1]
  LinearAlgebra.vectorSubtract([0], [1, 2, 3]) //throws an error

vectorMultiply(v, n) ⇒ number

Multiplies a vector with a number.

Kind: global function
Returns: number - output vector

Param Type Description
v Array.<number> input vector
n number input number

Example

LinearAlgebra.vectorMultiply([1, -2, 0], 2)
  //is equal to [2, -4, 0]

vectorDivide(v, n) ⇒ number

Divides a vector by a number.

Kind: global function
Returns: number - output vector

Param Type Description
v Array.<number> input vector
n number input number

Example

LinearAlgebra.vectorDivide([1, 2, 3], 2)
  //is equal to [0.5, 1, 1.5]

vectorDotProduct(v0, v1) ⇒ Array.<number> | void

calculates the dot product of two vectors of equal dimensions

Kind: global function
Returns: Array.<number> | void - dot product
Throws:

  • Error 'input lengths are not equal' if v0 & v1 have different lengths (= different number of dimensions)
Param Type Description
v0 Array.<number> first vector
v1 Array.<number> second vector

Example

LinearAlgebra.vectorDotProduct([1, 1], [1, -1]) //is 0

vectorLength(v) ⇒ number

calculates the length of a vector using the euclidean distance

Kind: global function
Returns: number - length of the input vector
See: https://en.wikipedia.org/wiki/Euclidean_distance

Param Type Description
v Array.<number> input vector, described through an array

Example

LinearAlgebra.vectorLength([4, 4, -4, 4]) //is 8

distance(p0, p1) ⇒ number

calculates the euclidean distance between two points

Kind: global function
Returns: number - distance between p0 and p1
Throws:

  • Error 'input lengths are not equal' if p0 & p1 have different lengths (= different number of dimensions)
Param Type Description
p0 Array.<number> first point
p1 Array.<number> second point

Example

LinearAlgebra.distance([0, 1, 2, 3], [4, -3, 6, 7]) //is 8
  LinearAlgebra.distance([0, 1], [2]) //throws an error

innerProductAngle(v0, v1) ⇒ Array.<number>

calculates the inner inner product angle in rad of two vectors of equal dimensions

Kind: global function
Returns: Array.<number> - inner product angle
Throws:

  • Error 'input lengths are not equal' if p0 & p1 have different lengths (= different number of dimensions)

See: https://commons.wikimedia.org/wiki/File:Inner-product-angle.png

Param Type Description
v0 Array.<number> first vector
v1 Array.<number> second vector

Example

const v0 = [1, 0], v1 = [0, 1];
  LinearAlgebra.innerProductAngle(v0, v1) === Math.PI/2 //is true

Example

LinearAlgebra.innerProductAngle([0, 1], [2]) //throws an error

shortestDistance(O, P0, P1) ⇒ number

calculates the shortest distance between a point O and a line, defined through two points p0 and p1

Kind: global function
Returns: number - distance between a and the closest point on the line
Throws:

  • Error 'input lengths are not equal' if O, P0 & P1 have different lengths (= different number of dimensions)
Param Type Description
O Array.<number> point where the distance starts
P0 Array.<number> point 1 of the line on which the ending point of the distance lies
P1 Array.<number> point 2 of the line on which the ending point of the distance lies

Example

LinearAlgebra.shortestDistance([0, 0],
                                 [1, 1],
                                 [2, 1]) //is 1
  LinearAlgebra.shortestDistance([1], [2, 3], [4])
  //throws an error

matrixIdentity(n) ⇒ Array.<Array.<number>>

returns a n*n identity matrix

Kind: global function
Returns: Array.<Array.<number>> - identity matrix with n rows and columns

Param Type Description
n number matrix width/height

Example

LinearAlgebra.matrixIdentity(3)
      //is equal to [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

matrixAugment(M0, M1) ⇒ Array.<Array.<number>>

creates a matrix that is basically the two input matrices next to each other

Kind: global function
Returns: Array.<Array.<number>> - augmented matrix M0M1

Param Type Description
M0 Array.<Array.<number>> first input matrix
M1 Array.<Array.<number>> second input matrix

Example

LinearAlgebra.matrixAugment([[1], [3]], [[2], 4])
      //is equal to [[1, 2], [3, 4]]

matrixSlice(M, [beginRow], [beginColumn], [endRow], [endColumn]) ⇒ Array.<Array.<number>>

removes rows and columns at the end and/or the beginning of a matrix

Kind: global function
Returns: Array.<Array.<number>> - sliced matrix M₁

Param Type Default Description
M Array.<Array.<number>> input matrix
[beginRow] number 0 row where the output matrix starts
[beginColumn] number 0 column where the output matrix
[endRow] number M.length row where the output matrix ends
[endColumn] number M[0].length column where the output matrix ends

Example

LinearAlgebra.matrixSlice([[0, 1], [2, 3]])
      //is equal to [[0, 1], [2, 3]]

Example

LinearAlgebra.matrixSlice([[8,8,8],[8,9,8],[8,8,8]], 1, 1, 2, 2)
  //is equal to [[9]]

matrixEliminate(M0, M1) ⇒ Array.<Array.<number>>

Sorry, i forgot what this does. THat's the problem with personal projects, there is no reason to hurry or to watch your coding style and then you just forget stuff. I don't think this function it is necessary though.I might delete it soon.

Kind: global function
Returns: Array.<Array.<number>> - Something. I don't exactly know.
Todo

  • find out what this does and delete it if it's not in use or not useful
Param Type Description
M0 Array.<Array.<number>> first matrix
M1 Array.<Array.<number>> | Array.<number> second matrix

isSqareShortCheck(M, [throwIfNot]) ⇒ boolean

Checks if a matrix is n*n. Only checks first row internally, so make sure the matrix is well-formed.

Kind: global function
Returns: boolean - False if matrix is not sqare and throwIfNot is false. True if it is a sqare matrix.
Throws:

  • Error 'not a sqare matrix' if throwIfNot is true and M is not n*n
Param Type Default Description
M Array.<Array.<number>> matrix to be checked
[throwIfNot] boolean false specifies if exception should be thrown in case of M not being a sqare matrix

matrixInvert(M) ⇒ Array.<Array.<number>>

Calculates inverted matrix M⁻¹. Based on a function by Andrew Ippoliti.

Kind: global function
Returns: Array.<Array.<number>> - inverted Matrix
Throws:

  • Error 'not a square matrix' if number of rows and columns don't match. Only checks first row and assumes all other rows have the same length.

See: http://blog.acipo.com/matrix-inversion-in-javascript/
Author: Andrew Ippoliti

Param Type Description
M Array.<Array.<number>> input matrix