-
Notifications
You must be signed in to change notification settings - Fork 0
LinearAlgebra.js
-
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
- matrixEliminate(M0, M1)
- matrixInvert(M)
-
Calculates inversed matrix M^⁻¹. Based on a function by Andrew Ippoliti.
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 erroradds 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 errorSubtracts 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], [1, 0]) //is equal to [2, 1]
LinearAlgebra.vectorSubtract([0], [1, 2, 3]) //throws an errorMultiplies 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]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]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 0calculates 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 8calculates 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 errorcalculates 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 trueExample
LinearAlgebra.innerProductAngle([0, 1], [2]) //throws an errorcalculates 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 errorKind: global function
| Param | Type |
|---|---|
| M0 | Array.<Array.<number>> |
| M1 |
Array.<Array.<number>> | Array.<number>
|
Calculates inversed matrix M^⁻¹. Based on a function by Andrew Ippoliti.
Kind: global function
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 |
|---|---|
| M | Array.<Array.<number>> |
Attention: don't edit these files directly! They are auto-generated and pushed by Travis CI. Add your changes in the jsdoc comments in the code files.