## Methods

static

### 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: `[[m_11, m_12], [m_21, m_22]]` is used to construct a matrix where the (i,j)-th block submatrix is `m_ij`.

Returns

`module:la.Matrix`B Concatenated matrix.

static

### 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

module:la.Vector

The vector on the right-hand side of the system.

x

module:la.Vector

Yes

Current solution. Default is a vector of zeros.

verbose

boolean

Yes

If true, console logs the residuum value.

Defaults to `false`.

Returns

`module:la.Vector`B Solution to the system.

static

### 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

module:la.Vector

Copied vector.

Returns

`Array of number`B A JavaScript array of numbers.

static

### 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.Matrix`B A `dim`-by-`dim` identity matrix.

static

### 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 number`B Array of indexes where maximum is found, one for each column.

static

### 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

module:la.Matrix

The matrix we want to inverse.

Returns

`module:la.Matrix`B The inverse matrix of `mat`.

static

### 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.Vector`B A `dim`-dimensional vector whose entries are set to 1.0.

static

### 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

module:la.Matrix

First matrix.

X2

module:la.Matrix

Second matrix.

Returns

`module:la.Matrix`B Matrix with `X1.cols` rows and `X2.cols` columns containing squared euclidean distances.

static

### 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 parameter `len` is given. The vector contains random integers from the array `[0,...,num-1]` (with repetition).

static

### 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 parameter `arg1` is given.
3. module:la.Matrix, if parameters `arg1` and `arg2` are given.

static

### 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 number`B A JavaScript array of integers. Represents a permutation of `k` elements.

static

### 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 `[0,...,n-1]`. Must be an integer.

k

number

Length of the sample. Must be smaller or equal to `n`.

Returns

`Array of number`B The sample of `k` numbers from `[0,...,n-1]`, sampled without replacement.

static

### 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.IntVector`B Integer range vector.

static

### 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 `rows`.

Returns

`module:la.SparseMatrix`B A `rows`-by-`cols` sparse zero matrix.

static

### 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.SparseMatrix`B A dim-by-dim identity matrix.

static

### 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. If `x` is a number, returns square of `x`.
2. If `x` is a module:la.Vector, returns a module:la.Vector, where the i-th value of the vector is the square of `x[i]`.

static

### 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 `rows`.

Returns

`module:la.Matrix`B A `rows`-by-`cols` dense 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

module:la.Matrix

The matrix.

tol

number

Yes

The tolerance number.

Defaults to `1e-6`.

Returns

`Object`B A JSON object `qrRes` which 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 `json` have the following properties:

Name Type Optional Description

iter

number

Yes

The number of iterations used for the algorithm.

Defaults to `100`.

tol

number

Yes

The tolerance number.

Defaults to `1e-6`.

callback

function()

Yes

The callback function, that takes the error parameters (err) and the result parameter (res). Only for the asynchronous function.

Returns

`Object`B The JSON object `svdRes` which 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

inner

### 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 If `boolVectorCompareCb(arg1, arg2)` is less than 0 or false, sort `arg1` to a lower index than `arg2`, i.e. `arg1` comes first.

inner

### 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 If `intVectorCompareCb(arg1, arg2)` is less than 0 or false, sort `arg1` to a lower index than `arg2`, i.e. `arg1` comes first.

inner

### 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 `false`.

inner

### 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 If `strVectorCompareCb(arg1, arg2)` is less than 0 or false, sort `arg1` to a lower index than `arg2`, i.e. `arg1` comes first.

inner

### 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 If `vectorCompareCb(arg1, arg2)` is less than 0 or false, sort `arg1` to a lower index than `arg2`, i.e. `arg1` comes first.