matrix
A Javascript Library to perform basic matrix operations using the functional nature of Javascript
Usage
var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
var A = matrix(a);
Operations
1. Identity
A();
2. Row
A(0);
3. Column
A([], 0);
4. Element
A(1, 2);
5. Range
A([1,2]);
A([],[1,2]);
A([1,2],[1,2]);
A([2,1],[]);
A([],[2,1]);
A([2,1],[2,1]);
6. Size
A.size();
7. Set
A.set(0).to(0);
A.set(1,2).to(10);
A.set([], 0).to(0);
A.set([1,2]).to(4);
A.set([], [1,2]).to(1);
8. Addition
var B = matrix([[3, 4, 5], [6, 7, 8], [9, 10, 11]]);
A.add(B);
9. Subtraction
B.sub(A);
10. Multiplication
A.mul(B);
NOTE: This is not classical matrix multiplication (which is implemented using the prod() method). This simply multiplies together each element in matrix A with the corresponding element in matrix B. If A and B are not the same size, it will produce some NaN results.
11. Division
A.div(B);
12. Product
A.prod(B);
13. Transpose
A.trans();
14. Determinant
var C = matrix([[5, 4, 7], [4, 8, 2], [9, 0, 4]]);
C.det();
15. Inverse
Should be invertible
M = matrix([[1, 3, 3], [1, 4, 3], [1, 3 ,4]]);
M.inv();
16. Merge
Merges two matrices in all directions
M = matrix([[3, 4], [7, 8]]);
M.merge.left([[1, 2], [5, 6]]);
M = matrix([[1, 2], [5, 6]]);
M.merge.right([[3, 4], [7, 8]]);
M = matrix([5, 6, 7, 8]);
M.merge.top([1, 2, 3, 4]);
M = matrix([1, 2, 3 ,4]);
M.merge.bottom([5, 6, 7, 8]);
17. Map
Applies a given function over the matrix, elementwise. Similar to Array.map()
M = matrix([1, 2, 3]);
M.map(x => x*x);
This example shows the arguments provided to the function
M = matrix([[1, 2], [3, 4]]);
M.map((value, pos, mat) => value * pos[1]);
18. Equals
Checks the equality of two matrices and returns a boolean. A matrix is equal to itself.
A = matrix([[1,2],[3,4]]);
A.equals(A);
B = matrix([[3,4], [1,2]]);
A.equals(B);
19. Generate
Generates a matrix with the value passed across the entire matrix or just the diagonal.
matrix.gen(4).size(2,3);
matrix.gen(2).size(2);
matrix.gen(1).diag(3);
matrix.gen(1).diag(3,2);
matrix.gen(2).diag(3,4);