Properties

new SparseMatrix([arg][, rows])

Sparse Matrix

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix with array
var mat = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]]);
// create a new sparse matrix with specified max rows
var mat2 = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]], 3);

Parameters

Name Type Optional Description

arg

(Array of Array of Array of number or module:la.SparseMatrix)

Yes

Constructor arguments. There are two ways of constructing:
1. using a nested array of sparse vectors (columns). A sparse vector is a nested array of pairs, first value is index, second is value. Example: [[[0, 2]], [[0, 1], [2, 3]]] has 2 columns. The second non-zero element in second column has a value 3 at index 2,
2. using a sparse matrix (copy constructor).

rows

number

Yes

Maximal number of rows in sparse vector. It is only in combinantion with nested array of vector elements.

Properties

cols

Gives the number of columns of sparse matrix. Type number.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]]);
// check the number of columns in sparse matrix
mat.cols;

rows

Gives the number of rows of sparse matrix. Type number.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]]);
// check the number of rows in sparse matrix
mat.rows;

Methods

at(rowIdx, colIdx) → number

Returns an element of the sparse matrix at the given location.

Example

// import la module
var la = require('qminer').la;
// create a sparse matrix
var mat = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]]);
// get the value at the position (1, 1)
mat.at(1, 1); // returns 3

Parameters

Name Type Optional Description

rowIdx

number

 

Row index (zero based).

colIdx

number

 

Column index (zero based).

Returns

numberB Matrix value.

clear() → module:la.SparseMatrix

Clear content of the matrix and sets its row dimension to -1.

Example

//import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2], [2, -3]], [[1, 1], [3, -2]]]);
// clear the matrix
mat.clear();
Returns

module:la.SparseMatrixB Self. All the content has been cleared.

colNorms() → module:la.Vector

Returns the vector of column norms of sparse matrix.

Example

//import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2], [2, -3]], [[1, 1], [3, -2]]]);
// get the column norms
var norms = mat.colNorms();
Returns

module:la.VectorB Vector of column norms. Ihe i-th value of the return vector is the norm of i-th column of sparse matrix.

frob() → number

Returns the frobenious norm of sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 1], [1, 3]], [[0, 2], [1, 4]]]);
// get the frobenious norm of sparse matrix
var norm = mat.frob(); // returns sqrt(30)
Returns

numberB Frobenious norm of sparse matrix.

frob2() → number

Calculates the frobenious norm squared of the matrix.

Example

// import la module
var la = require('qminer').la;
// create a sparse matrix
var spMat = new la.SparseMatrix([[[0, 1], [1, 5]], [[0, 2], [2, -3]]]);
// get the forbenious norm squared of the sparse matrix
var frob = spMat.frob2();
Returns

numberB Frobenious norm squared.

full() → module:la.Matrix

Returns the dense representation of sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]]);
// create a dense representation of sparse matrix
// returns the dense matrix:
// 2  1
// 0  0
// 0  3
mat.full();
Returns

module:la.MatrixB Dense representation of sparse matrix.

getCol(colIdx) → module:la.SparseVector

Returns the column of the sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a sparse matrix
var mat = new la.SparseMatrix([[[0, 3], [1, 2]], [[1, -2], [3, 4]], [[10, 8]]]);
// get the first column as a vector
var first = mat.getCol(0); // returns the first column of the sparse matrix

Parameter

Name Type Optional Description

colIdx

number

 

The column index (zero based).

Returns

module:la.SparseVectorB Sparse vector corresponding to the colIdx-th column of sparse matrix.

getColSubmatrix(columnIdVec) → module:la.SparseMatrix

Returns a submatrix containing only selected columns. Columns are identified by a vector of ids.

Example

//import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2], [2, -3]], [[1, 1], [3, -2]]]);
// get the submatrix containing the 1, 2 and 4 column
var submat = mat.getColSubmatrix(new la.IntVector([1]));

Parameter

Name Type Optional Description

columnIdVec

module:la.IntVector

 

Integer vector containing selected column ids.

Returns

module:la.SparseMatrixB The submatrix containing the the columns of the original matrix.

load(fin) → module:la.Matrix

Loads the sparse matrix from input stream.

Example

// import the modules
var fs = require('qminer').fs;
var la = require('qminer').la;
// create an empty matrix
var mat = new la.SparseMatrix();
// open a read stream ('mat.dat' was previously created)
var fin = fs.openRead('mat.dat');
// load the matrix
mat.load(fin);

Parameter

Name Type Optional Description

fin

module:fs.FIn

 

Input stream.

Returns

module:la.MatrixB Self. The content has been loaded using fin.

minus(mat) → module:la.SparseMatrix

Substraction of two matrices.

Example

// import la module
var la = require('qminer').la;
// create two sparse matrices
var mat = new la.SparseMatrix([[[0, 1], [3, 2]], [[1, -3]]]);
var mat2 = new la.SparseMatrix([[[0, 3]],[[2, 1]]]);
// get the sum of the two matrices
// returns the sum ( insparse form)
// -2    0
//  0   -3
//  0   -1
//  2    0
var diff = mat.minus(mat2);

Parameter

Name Type Optional Description

mat

module:la.SparseMatrix

 

The second sparse matrix.

Returns

module:la.SparseMatrixB The difference of the two sparse matrices.

multiply(arg) → (module:la.Vector or module:la.Matrix)

Multiplies argument with sparse maatrix.

Example

// import la module
var la = require('qminer').la;
// create new sparse matrix
var mat = new la.SparseMatrix([[[0, 2], [3, 5]], [[1, -3]]]);
// create a vector
var vec = new la.Vector([3, -2]);
// multiply the matrix and vector
var vec2 = mat.multiply(vec);

Parameter

Name Type Optional Description

arg

(number, module:la.Vector, module:la.SparseVector, module:la.Matrix, or module:la.SparseMatrix)

 

Multiplication input.

Returns

(module:la.Vector or module:la.Matrix)B
1. module:la.Matrix, if arg is number, module:la.Matrix or module:la.SparseMatrix.
2. module:la.Vector, if arg is module:la.Vector or module:la.SparseVector.

multiplyT(arg) → (module:la.Vector or module:la.Matrix)

Sparse matrix transpose and multiplies with argument.

Example

// import la module
var la = require('qminer').la;
// create new sparse matrix
var mat = new la.SparseMatrix([[[0, 2], [3, 5]], [[1, -3]]]);
// create a dense matrix
var mat2 = new la.Matrix([[0, 1], [2, 3], [4, 5], [-1, 3]]);
// transpose mat and multiply it with mat2
var mat3 = mat.multiplyT(mat2);

Parameter

Name Type Optional Description

arg

(number, module:la.Vector, module:la.SparseVector, module:la.Matrix, or module:la.SparseMatrix)

 

Multiplication input.

Returns

(module:la.Vector or module:la.Matrix)B
1. module:la.Matrix, if arg is number, module:la.Matrix or module:la.SparseMatrix.
2. module:la.Vector, if arg is module:la.Vector or module:la.SparseVector.

nnz() → number

Returns the number of non-zero elements of sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a sparse matrix
var spMat = new la.SparseMatrix([[[0, 1], [1, 5]], [[0, 2], [2, -3]]]);
// get the number of non-zero elements
// returns 4
var nnz = spMat.nnz(); 
Returns

numberB Number of non-zero elements.

normalizeCols() → module:la.SparseMatrix

Normalizes columns of sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2]], [[0, 1], [2, 3]]]);
// normalize matrix columns
// The new matrix elements are:
// 1  0.316227
// 0  0
// 0  0.948683
mat.normalizeCols();
Returns

module:la.SparseMatrixB Self. The columns of the sparse matrix are normalized.

plus(mat) → module:la.SparseMatrix

Addition of two matrices.

Example

// import la module
var la = require('qminer').la;
// create two sparse matrices
var mat = new la.SparseMatrix([[[0, 1], [3, 2]], [[1, -3]]]);
var mat2 = new la.SparseMatrix([[[0, 3]],[[2, 1]]]);
// get the sum of the two matrices
// returns the sum ( insparse form)
// 4    0
// 0   -3
// 0    1
// 2    0
var sum = mat.plus(mat2);

Parameter

Name Type Optional Description

mat

module:la.SparseMatrix

 

The second sparse matrix.

Returns

module:la.SparseMatrixB Sum of the two sparse matrices.

print()

Prints sparse matrix on screen.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var spMat = new la.SparseMatrix([[[0, 1]], [[0, 3], [1, 8]]]);
// print sparse matrix on screen
// each row represents a nonzero element, where first value is row index, second
// value is column index and third value is element value. For this matrix:
// 0  0  1.000000
// 0  1  3.000000
// 1  1  8.000000
spMat.print();

push(spVec) → module:la.SparseMatrix

Attaches a column to the sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse vector
var mat = new la.SparseMatrix([[[0, 2], [3, 5]], [[1, -3]]]);
// create a new vector
var vec = new la.SparseVector([[0, 2], [2, -3]]);
// push the newly created vector to the matrix
// the new matrix is going to be (in sparse form)
// 2    0    2
// 0   -3    0
// 0    0   -3
// 5    0    0
mat.push(vec);

Parameter

Name Type Optional Description

spVec

module:la.SparseVector

 

Attached column as sparse vector.

Returns

module:la.SparseMatrixB Self. The last column is now the added spVec and the number of columns is now bigger by one.

put(rowIdx, colIdx, num) → module:la.SparseMatrix

Puts an element in sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 3], [1, 2]], [[1, -2], [3, 4]], [[10, 8]]]);
// set the value at position (2, 2) to -4
mat.put(2, 2, -4);

Parameters

Name Type Optional Description

rowIdx

number

 

Row index (zero based).

colIdx

number

 

Column index (zero based).

num

number

 

Element value.

Returns

module:la.SparseMatrixB Self. The value at position (rowIdx, colIdx) is changed.

save(fout[, saveMatlab]) → module:fs.FOut

Saves the sparse matrix as output stream.

Example

// import the modules
var fs = require('qminer').fs;
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 1]], [[0, 3], [1, 12]]]);
// open write stream
var fout = fs.openWrite('mat.dat');
// save matrix and close write stream
mat.save(fout).close();

Parameters

Name Type Optional Description

fout

module:fs.FOut

 

Output stream.

saveMatlab

boolean

Yes

If true, saves using matlab three column text format. Otherwise, saves using binary format.

Defaults to false.

Returns

module:fs.FOutB The output stream fout.

setCol(colIdx, spVec) → module:la.SparseMatrix

Sets a column in sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 3], [1, 2]], [[1, -2], [3, 4]], [[10, 8]]]);
// create a new sparse vector to replace the third column
var vec = new la.SparseVector([[0, 3], [2, -5]]);
// set the third column of mat to vec
mat.setCol(2, vec); // returns mat with the third column changed

Parameters

Name Type Optional Description

colIdx

number

 

Column index (zero based).

spVec

module:la.SparseVector

 

The new column sparse vector.

Returns

module:la.SparseMatrixB Self. The colIdx-th column has been replaced with spVec.

setRowDim(rowDim)

Sets the row dimension.

Example

// import the modules
var la = require('qminer').la;
// create an empty matrix
var mat = new la.SparseMatrix();
mat.setRowDim(2);
mat.rows // prints 2

Parameter

Name Type Optional Description

rowDim

number

 

Row dimension.

toString() → string

Returns a string displaying rows, columns and number of non-zero elements of sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 1]], [[0, 2], [1, 8]]]);
// create the string
var text = mat.toString(); // returns 'rows: -1, cols: 2, nnz: 3'
Returns

stringB String displaying row, columns and number of non-zero elements.

transpose() → module:la.SparseMatrix

Returns the transposed sparse matrix.

Example

// import la module
var la = require('qminer').la;
// create a new sparse matrix
var mat = new la.SparseMatrix([[[0, 2], [2, -3]], [[1, 1], [3, -2]]]);
// transpose the sparse matrix
// returns the transposed matrix (in sparse form)
// 2    0   -3    0
// 0    1    0   -2
mat.transpose();
Returns

module:la.SparseMatrixB Transposed sparse matrix.