la
Source: ladoc.
Linear algebra module.
Example
// import la module
var la = require('qminer').la;
// create a random matrix
var mat = new la.Matrix({ rows: 10, cols: 5, random: true });
// create a vector
var vec = new la.Vector([1, 2, 3, 0, -1]);
// multiply the matrix and vector
var vec2 = mat.multiply(vec);
// calculate the svd decomposition of the matrix
var svd = la.svd(mat, 3);Child classes
Methods
Classes
BoolVector
IntVector
Matrix
SparseMatrix
SparseVector
StrVector
Vector
Methods
cat(nestedArrMat) → module:la.Matrix
Constructs a matrix by concatenating a double-nested array of matrices.
Example
// import la module
var la = require('qminer').la;
// create four matrices and concatenate (2 block columns, 2 block rows)
var la = require('qminer').la;
var A = new la.Matrix([[1,2], [3,4]]);
var B = new la.Matrix([[5,6], [7,8]]);
var C = new la.Matrix([[9,10], [11,12]]);
var D = new la.Matrix([[13,14], [15,16]]);
// create a nested matrix
// returns the matrix:
// 1  2  5  6
// 3  4  7  8
// 9  10 13 14
// 11 12 15 16
var mat = la.cat([[A,B], [C,D]]);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| nestedArrMat | Array of Array of module:la.Matrix | 
 | An array of block rows, where each block row is an array of matrices.
                          For example:  | 
- Returns
- 
                  module:la.MatrixB Concatenated matrix.
conjgrad(A, b[, x][, verbose]) → module:la.Vector
Solves the PSD symmetric system: A x = b, where A is a positive-definite symmetric matrix.
Example
// import la module
var la = require('qminer').la;
// create a positive-definite symmetric matrix
var vecTemp = new la.Vector([1, 2, 3]);
var mat = vecTemp.diag();
// create the right-hand side vector 
var vec = new la.Vector([0.5, 3, -2]);
// solve the PSD symmetric system
var x = la.conjgrad(mat, vec);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| A | 
 | The matrix on the left-hand side of the system. | |
| b | 
 | The vector on the right-hand side of the system. | |
| x | Yes | Current solution. Default is a vector of zeros. | |
| verbose | boolean | Yes | If true, console logs the residuum value. Defaults to  | 
- Returns
- 
                  module:la.VectorB Solution to the system.
copyVecToArray(vec) → Array of number
Copies the vector into a JavaScript array of numbers.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// create a JavaScript array out of vec
var arr = la.copyVecToArray(vec); // returns an array [1, 2, 3]Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| vec | 
 | Copied vector. | 
- Returns
- 
                  Array of numberB A JavaScript array of numbers.
eye(dim) → module:la.Matrix
Returns an dense identity matrix.
Example
// import la module
var la = require('qminer').la;
// generate a dense identity matrix of dimension 5
var id = la.eye(5);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| dim | number | 
 | The dimension of the identity matrix. Must be a positive integer. | 
- Returns
- 
                  module:la.MatrixB Adim-by-dimidentity matrix.
findMaxIdx(X) → Array of number
Returns a JS array of indices idxArray that correspond to the max elements in each column of dense matrix. The resulting array has one element for vector input.
Example
// import la module
var la = require('qminer').la;
// create a dense matrix
var mat = new la.Matrix([[1, 2], [2, 0]]);
// get the indices of the maximum elements in each column of mat
// returns the array:
// [1, 0]
la.findMaxIdx(mat);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| X | 
 | The matrix or vector. | 
- Returns
- 
                  Array of numberB Array of indexes where maximum is found, one for each column.
inverseSVD(mat) → module:la.Matrix
Calculates the inverse matrix with SVD.
Example
// import la module
var la = require('qminer').la;
// create a random matrix
var mat = new la.Matrix({ rows: 5, cols: 5, random: true });
// get the inverse of mat
var inv = la.inverseSVD(mat);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| mat | 
 | The matrix we want to inverse. | 
- Returns
- 
                  module:la.MatrixB The inverse matrix ofmat.
ones(dim) → module:la.Vector
Returns a vector with all entries set to 1.0.
Example
// import la module
var la = require('qminer').la;
// create a 3-dimensional vector with all entries set to 1.0
var vec = la.ones(3);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| dim | number | 
 | Dimension of the vector. | 
- Returns
- 
                  module:la.VectorB Adim-dimensional vector whose entries are set to 1.0.
pdist2(X1, X2) → module:la.Matrix
Computes and returns the pairwise squared euclidean distances between columns of X1 and X2 (mat3[i,j] = ||mat(:,i) - mat2(:,j)||^2).
Example
// import la module
var la = require('qminer').la;
// construct two input matrices
var X1 = new la.Matrix([[1,2], [2,0]]);
var X2 = new la.Matrix([[1,0.5,0],[0,-0.5,-1]]);
// get the pairwise squared distance between the matrices
// returns the matrix:
// 4 6.5 10
// 1 2.5 5
la.pdist2(X1, X2);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| X1 | 
 | First matrix. | |
| X2 | 
 | Second matrix. | 
- Returns
- 
                  module:la.MatrixB Matrix withX1.colsrows andX2.colscolumns containing squared euclidean distances.
randi(num[, len]) → (number or la.IntVector)
Returns a randomly selected integer(s) from an array.
Example
// import la module
var la = require('qminer').la;
// generate a random integer between 0 and 10
var number = la.randi(10);
// generate an integer vector containing 5 random integers between 0 and 10
var vec = la.randi(10, 5);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| num | number | 
 | The upper bound of the array. Must be an integer. | 
| len | number | Yes | The number of selected integers. Must be an integer. | 
- Returns
- 
                  (number or la.IntVector)B
 1. Randomly selected integer from the array[0,...,num-1], if no parameters are given.
 2. module:la.IntVector, if parameterlenis given. The vector contains random integers from the array[0,...,num-1](with repetition).
randn([arg1][, arg2]) → (number, module:la.Vector, or module:la.Matrix)
Returns an object with random numbers.
Example
// import la module
var la = require('qminer').la;
// generate a random number
var number = la.randn();
// generate a random vector of length 7
var vector = la.randn(7);
// generate a random matrix with 7 rows and 10 columns
var mat = la.randn(7, 10);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| arg1 | number | Yes | Represents dimension of vector or number of rows in matrix. Must be an integer. | 
| arg2 | number | Yes | Represents number of columns in matrix. Must be an integer. | 
- Returns
- 
                  (number, module:la.Vector, or module:la.Matrix)B
 1. Number, if no parameters are given.
 2. module:la.Vector, if parameterarg1is given.
 3. module:la.Matrix, if parametersarg1andarg2are given.
randPerm(k) → Array of number
Returns a permutation of elements.
Example
// import la module
var la = require('qminer').la;
// create an array/permutation of 5 elements
var perm = la.randPerm(5);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| k | number | 
 | Number of elements to permutate. | 
- Returns
- 
                  Array of numberB A JavaScript array of integers. Represents a permutation ofkelements.
randVariation(n, k) → Array of number
Returns a JavaScript array, which is a sample of integers from an array.
Example
// import la module
var la = require('qminer').la;
// create an array containing 5 integers between 0 and 15
var arr = la.randVariation(15, 5);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| n | number | 
 | The upper bound of the generated array  | 
| k | number | 
 | Length of the sample. Must be smaller or equal to  | 
- Returns
- 
                  Array of numberB The sample ofknumbers from[0,...,n-1], sampled without replacement.
rangeVec(min, max) → module:la.IntVector
Generates an integer vector given range.
Example
// import la module
var la = require('qminer').la;
// create a range vector containing 1, 2, 3
var vec = la.rangeVec(1, 3);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| min | number | 
 | Start value. Should be an integer. | 
| max | number | 
 | End value. Should be an integer. | 
- Returns
- 
                  module:la.IntVectorB Integer range vector.
sparse(rows[, cols]) → module:la.SparseMatrix
Returns a sparse zero matrix.
Example
// import la module
var la = require('qminer').la;
// create a sparse zero matrix with 5 rows and columns
var spMat = la.sparse(5);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| rows | number | 
 | Number of rows of the sparse matrix. | 
| cols | number | Yes | Number of columns of the sparse matrix. Defaults to  | 
- Returns
- 
                  module:la.SparseMatrixB Arows-by-colssparse zero matrix.
speye(dim) → module:la.SparseMatrix
Returns a sparse identity matrix
Example
// import la module
var la = require('qminer').la;
// generate a sparse identity matrix of dimension 5
var spId = la.speye(5);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| dim | number | 
 | The dimension of the identity matrix. Must be a positive integer. | 
- Returns
- 
                  module:la.SparseMatrixB A dim-by-dim identity matrix.
square(x) → (number or module:la.Vector)
Squares the values in vector.
Example
// import la module
var la = require('qminer').la;
// create a vector
var vec = new la.Vector([1, 2, 3]);
// square the values of the vector
// returns the vector containing  the values 1, 4, 9
var sqr = la.square(vec);Parameter
| Name | Type | Optional | Description | 
|---|---|---|---|
| x | (number or module:la.Vector) | 
 | The value/vector. | 
- Returns
- 
                  (number or module:la.Vector)B
 1. Ifxis a number, returns square ofx.
 2. Ifxis a module:la.Vector, returns a module:la.Vector, where the i-th value of the vector is the square ofx[i].
zeros(rows[, cols]) → module:la.Matrix
Returns a dense zero matrix.
Example
// import la module
var la = require('qminer').la;
// create a sparse zero matrix with 5 rows and 3 columns
var mat = la.zeros(5, 3);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| rows | number | 
 | Number of rows of the matrix. | 
| cols | number | Yes | Number of columns of the matrix. Defaults to  | 
- Returns
- 
                  module:la.MatrixB Arows-by-colsdense zero matrix.
qr(mat[, tol]) → Object
Computes the QR decomposition.
Example
// import la module
var la = require('qminer').la;
// create a random matrix
var mat = new la.Matrix({ rows: 10, cols: 5, random: true });
// calculate the QR decomposition of mat
var qrRes = la.qr(mat);Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| mat | 
 | The matrix. | |
| tol | number | Yes | The tolerance number. Defaults to  | 
- Returns
- 
                  ObjectB A JSON objectqrReswhich contains the decomposition matrices:qrRes.Q- The orthogonal matrix Q of the QR decomposition. Type module:la.Matrix.qrRes.R- The upper triangular matrix R of the QR decomposition. Type module:la.Matrix.
svd(mat, k[, json][, callback]) → Object
Computes the truncated SVD decomposition.
Examples
Asynchronous function
// import the modules
var la = require('qminer').la;
// create a random matrix
var A = new la.Matrix({ rows: 10, cols: 5, random: true });
// set the parameters for the calculation
var k = 2; // number of singular vectors
var param = { iter: 1000, tol: 1e-4 };
// calculate the svd
la.svd(A, k, param, function (err, result) {
   if (err) { console.log(err); }
   // successful calculation
   var U = result.U;
   var V = result.V;
   var s = result.s;
});Synchronous function
// import the modules
var la = require('qminer').la;
// create a random matrix
var A = new la.Matrix({ rows: 10, cols: 5, random: true });
// set the parameters for the calculation
var k = 2; // number of singular vectors
var param = { iter: 1000, tol: 1e-4 };
// calculate the svd
var result = la.svd(A, k, param);
// successful calculation
var U = result.U;
var V = result.V;
var s = result.s;Parameters
| Name | Type | Optional | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| mat | 
 | The matrix. | |||||||||||||
| k | number | 
 | The number of singular vectors to be computed. | ||||||||||||
| json | Object | Yes | The JSON object. Values in  
 | ||||||||||||
| callback | function() | Yes | The callback function, that takes the error parameters (err) and the result parameter (res). Only for the asynchronous function. | 
- Returns
- 
                  ObjectB The JSON objectsvdReswhich contains the SVD decomposition USV^T matrices:svdRes.U- The dense matrix of the decomposition. Type module:la.Matrix.svdRes.V- The dense matrix of the decomposition. Type module:la.Matrix.svdRes.s- The vector containing the singular values of the decomposition. Type module:la.Vector.
Abstract types
boolVectorCompareCb(arg1, arg2) → (number or boolean)
Vector sort comparator callback.
Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| arg1 | boolean | 
 | First argument. | 
| arg2 | boolean | 
 | Second argument. | 
- Returns
- 
                  (number or boolean)B IfboolVectorCompareCb(arg1, arg2)is less than 0 or false, sortarg1to a lower index thanarg2, i.e.arg1comes first.
intVectorCompareCb(arg1, arg2) → (number or boolean)
Vector sort comparator callback.
Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| arg1 | number | 
 | First argument. | 
| arg2 | number | 
 | Second argument. | 
- Returns
- 
                  (number or boolean)B IfintVectorCompareCb(arg1, arg2)is less than 0 or false, sortarg1to a lower index thanarg2, i.e.arg1comes first.
matrixArg Object
Matrix constructor parameter object.
Properties
| Name | Type | Optional | Description | 
|---|---|---|---|
| rows | number | 
 | Number of rows. | 
| cols | number | 
 | Number of columns. | 
| random | boolean | Yes | Generate a random matrix with entries sampled from a uniform [0,1] distribution. If set to false, a zero matrix is created. Defaults to  | 
strVectorCompareCb(arg1, arg2) → (number or boolean)
Vector sort comparator callback.
Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| arg1 | string | 
 | First argument. | 
| arg2 | string | 
 | Second argument. | 
- Returns
- 
                  (number or boolean)B IfstrVectorCompareCb(arg1, arg2)is less than 0 or false, sortarg1to a lower index thanarg2, i.e.arg1comes first.
vectorCompareCb(arg1, arg2) → (number or boolean)
Vector sort comparator callback.
Parameters
| Name | Type | Optional | Description | 
|---|---|---|---|
| arg1 | number | 
 | First argument. | 
| arg2 | number | 
 | Second argument. | 
- Returns
- 
                  (number or boolean)B IfvectorCompareCb(arg1, arg2)is less than 0 or false, sortarg1to a lower index thanarg2, i.e.arg1comes first.