## Properties

cols

rows

## Methods

at(rowIdx, colIdx)
colMaxIdx(colIdx)
colNorms()
diag()
frob()
getCol(colIdx)
getColSubmatrix(intVec)
getRow(rowIdx)
getSubmatrix(minRow, maxRow, minCol, maxCol)
load(fin)

minus(mat)
multiply(arg)
multiplyT(arg)
normalizeCols()
plus(mat)
print()
put(rowIdx, colIdx, arg)
rowMaxIdx(rowIdx)
rowNorms()
save(fout)

setCol(colIdx, vec)
setRow(rowIdx, vec)
solve(vec)
sparse()
toArray()
toMat()
toString()
transpose()

## new Matrix([arg])

Matrix class.

### Example

    // import la module
    var la = require('qminer').la;
    // create new matrix with matrixArg
    var mat = new la.Matrix({"rows": 3, "cols": 2, "random": true}); // creates a 3 x 2 matrix with random values
    // create a new matrix with nested arrays
    var mat2 = new la.Matrix([[1, 7, 4], [-10, 0, 3]]); // creates a 2 x 3 matrix with the designated values

### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
arg | (module:la~matrixArg, Array of Array of number, or module:la.Matrix) | Yes | Constructor arguments. There are three ways of constructing: 1. Using the parameter object module:la~matrixArg, 2. using a nested array of matrix elements (row major). Example: [[1,2],[3,4]] has two rows, the first row is [1,2], 3. using a dense matrix (copy constructor).

## Properties

### cols

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

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
    // get the number of cols
    var colsN = mat.cols; // returns 2

### rows

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

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
    // get the number of rows
    var rowN = mat.rows; // returns 3

## Methods

### at(rowIdx, colIdx) → number

Returns an element of matrix.

#### Example

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

#### Parameters

Name | Type | Optional | Description
--- | --- | --- | ---
rowIdx | number |  | Row index (zero based).
colIdx | number |  | Column index (zero based).

Returns

  `number`B The matrix element.

### colMaxIdx(colIdx) → number

Gives the index of the maximum element in the given column.

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
    // get the row id of the maximum value of the second column
    var maxRow = mat.colMaxIdx(1); // returns the value 2

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
colIdx | number |  | Column index (zero based).

Returns

  `number`B Row index (zero based) of the maximum value in `colIdx`-th column of matrix.

### colNorms() → module:la.Vector

Calculates the matrix column norms.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[3, 4], [4, 15/2]]);
    // get the row norms of the matrix
    var rowNorms = mat.colNorms(); // returns the vector [5, 17/2]

Returns

  `module:la.Vector`B Vector, where the value at i-th index is the norm of the i-th column of matrix.

### diag() → module:la.Vector

Returns the diagonal elements of matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[1, -1, 0], [15, 8, 3], [0, 1, 0]]);
    // call diag function
    var vec = mat.diag(); // returns a vector [1, 8, 0]

Returns

  `module:la.Vector`B Vector containing the diagonal elements.

### frob() → number

Returns the frobenious norm of matrix.

#### Example

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

Returns

  `number`B Frobenious norm of matrix.

### getCol(colIdx) → module:la.Vector

Returns the corresponding column of matrix as vector.

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
    // get the second column of the matrix
    var col = mat.getCol(1);

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
colIdx | number |  | Column index (zero based).

Returns

  `module:la.Vector`B The `colIdx`-th column of matrix.

### getColSubmatrix(intVec) → module:la.Matrix

Gets the submatrix from the column ids.

#### Example

    //import la module
    var la = require('qminer').la;
    // create a random matrix
    var mat = new la.Matrix({ rows: 10, cols: 10, random: true });
    // get the submatrix containing the 1, 2 and 4 column
    var submat = mat.getColSubmatrix(new la.IntVector([0, 1, 3]));

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
intVec | module:la.IntVector |  | The vector containing the column ids.

Returns

  `module:la.Matrix`B The submatrix containing the the columns of the original matrix.

### getRow(rowIdx) → module:la.Vector

Returns the corresponding row of matrix as vector.

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
    // get the first row of the matrix
    var row = mat.getRow(1);

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
rowIdx | number |  | Row index (zero based).

Returns

  `module:la.Vector`B The `rowIdx`-th row of matrix.

### getSubmatrix(minRow, maxRow, minCol, maxCol) → module:la.Matrix

Gets the submatrix from the column ids.

#### Example

    //import la module
    var la = require('qminer').la;
    // create a random matrix
    var mat = new la.Matrix({ rows: 10, cols: 10, random: true });
    // get the submatrix containing from the position (1, 2) to (7, 4)
    var submat = mat.getSubmatrix(1, 7, 2, 4);

#### Parameters

Name | Type | Optional | Description
--- | --- | --- | ---
minRow | number |  | The minimum row index.
maxRow | number |  | The maximum row index.
minCol | number |  | The minimum column index.
maxCol | number |  | The maximum column index.

Returns

  `module:la.Matrix`B The submatrix of the original matrix.

### load(fin) → module:la.Matrix

Loads the 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.Matrix();
    // open a read stream ('mat.dat' is pre-saved)
    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.Matrix`B Self. It is made out of the input stream `fin`.

### minus(mat) → module:la.Matrix

Substracts two matrices.

#### Example

    // import la module
    var la = require('qminer').la;
    // create two matrices
    var mat = new la.Matrix([[1, 2], [-1, 5]]);
    var mat2 = new la.Matrix([[1, -1], [3, 2]]);
    // substract the matrices
    // the return matrix is
    //  0   3
    // -4   3
    var diff = mat.minus(mat2);

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
mat | module:la.Matrix |  | The second matrix.

Returns

  `module:la.Matrix`B The difference of the matrices.

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

Right-hand side multiplication of matrix with parameter.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[1, 2], [-1, 5]]);
    // create a new vector
    var vec = new la.Vector([1, -1]);
    //multiply mat and vec
    var vec2 = mat.multiply(vec); // returns vector [-1, -6]

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
arg | (number, module:la.Vector, module:la.SparseVector, module:la.Matrix, or module:la.SparseMatrix) |  | Multiplication input. Supports scalar, vector and matrix input.

Returns

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

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

Matrix transpose and right-hand side multiplication of matrix with parameter.

#### Example

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

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
arg | (number, module:la.Vector, module:la.SparseVector, module:la.Matrix, or module:la.SparseMatrix) |  | Multiplication input. Supports scalar, vector and matrix input.

Returns

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

### normalizeCols() → module:la.Matrix

Normalizes each column of matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[3, 4], [4, 15/2]]);
    // normalize the columns of the matrix
    // the matrix is going to be of the form:
    // 3/5     8/17
    // 4/5    15/17
    mat.normalizeCols();

Returns

  `module:la.Matrix`B Self. The columns of matrix are normalized.

### plus(mat) → module:la.Matrix

Adds two matrices.

#### Example

    // import la module
    var la = require('qminer').la;
    // create two matrices
    var mat = new la.Matrix([[1, 2], [-1, 5]]);
    var mat2 = new la.Matrix([[1, -1], [3, 2]]);
    // add the matrices
    // the return matrix is
    // 2   1
    // 2   7
    var sum = mat.plus(mat2);

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
mat | module:la.Matrix |  | The second matrix.

Returns

  `module:la.Matrix`B The sum of the matrices.

### print()

Prints the matrix on-screen.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[1, 2], [3, 4]]);
    // print the matrix
    // each row represents a row in the matrix. For this example:
    // 1  2
    // 3  4
    mat.print();

### put(rowIdx, colIdx, arg) → module:la.Matrix

Sets an element or a block of matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    var arg = new la.Matrix([[10, 11], [12, 13]]);
    mat.put(0, 1, arg);
    // updates the matrix to
    // 1  10  11
    // 4  12  13
    // 7   8   9

#### Parameters

Name | Type | Optional | Description
--- | --- | --- | ---
rowIdx | number |  | Row index (zero based).
colIdx | number |  | Column index (zero based).
arg | (number or module:la.Matrix) |  | A number or a matrix. If arg is of type module:la.Matrix, it gets copied, where the argument's upper left corner, `arg.at(0,0)`, gets copied to position (`rowIdx`, `colIdx`).

Returns

  `module:la.Matrix`B Self. The (`rowIdx`, `colIdx`) value/block is changed.

### rowMaxIdx(rowIdx) → number

Gives the index of the maximum element in the given row.

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
    // get the column id of the maximum value of the second row
    var maxRow = mat.rowMaxIdx(1); // returns the value 0

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
rowIdx | number |  | Row index (zero based).

Returns

  `number`B Column index (zero based) of the maximum value in the `rowIdx`-th row of matrix.

### rowNorms() → module:la.Vector

Calculates the matrix row norms.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[3, 4], [4, 15/2]]);
    // get the row norms of the matrix
    var rowNorms = mat.rowNorms(); // returns the vector [5, 17/2]

Returns

  `module:la.Vector`B Vector, where the value at i-th index is the norm of the i-th row of matrix.

### save(fout) → module:fs.FOut

Saves the matrix as output stream.

#### Example

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

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
fout | module:fs.FOut |  | Output stream.

Returns

  `module:fs.FOut`B The output stream `fout`.

### setCol(colIdx, vec) → module:la.Matrix

Sets the column of the matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a matrix
    var mat = new la.Matrix([[1, -3, 2], [9, 2, -4],  [-2, 3, 3]]);
    // create a vector
    var vec = new la.Vector([-3, 2, 2]);
    // set the first column of the matrix with the vector
    // the changed matrix is now
    // -3   -3    2
    //  2    2   -4
    //  2    3    3
    mat.setCol(0, vec);

#### Parameters

Name | Type | Optional | Description
--- | --- | --- | ---
colIdx | number |  | Column index (zero based).
vec | module:la.Vector |  | The new column of matrix.

Returns

  `module:la.Matrix`B Self. The `colIdx`-th column is changed.

### setRow(rowIdx, vec) → module:la.Matrix

Sets the row of matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a matrix
    var mat = new la.Matrix([[1, -3, 2], [9, 2, -4],  [-2, 3, 3]]);
    // create a vector
    var vec = new la.Vector([-3, 2, 2]);
    // set the first row of the matrix with the vector
    // the changed matrix is now
    // -3    2    2
    //  9    2   -4
    // -2    3    3
    mat.setRow(0, vec);

#### Parameters

Name | Type | Optional | Description
--- | --- | --- | ---
rowIdx | number |  | Row index (zero based).
vec | module:la.Vector |  | The new row of matrix.

Returns

  `module:la.Matrix`B Self. The `rowIdx`-th row is changed.

### solve(vec) → module:la.Vector

Solves the linear system.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var M = new la.Matrix([[1, 2], [-1, -5]]);
    // create a new vector
    var b = new la.Vector([-1, -6]);
    // solve the linear system M*x = b
    var x = M.solve(b); // returns vector [1, -1]

#### Parameter

Name | Type | Optional | Description
--- | --- | --- | ---
vec | module:la.Vector |  | The right-hand side of the equation.

Returns

  `module:la.Vector`B The solution of the linear system.

### sparse() → module:la.SparseMatrix

Transforms the matrix from dense to sparse format.

#### Example

    // import la module
    var la = require('qminer').la;
    // create the matrix
    var mat = new la.Matrix([[1, 2], [0, 3], [-4, 0]]);
    // transform the matrix into the sparse form
    var spMat = mat.sparse();

Returns

  `module:la.SparseMatrix`B Sparse column matrix representation of dense matrix.

### toArray() → Array of Array of number

Copies the matrix into a JavaScript array of arrays of numbers.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[1, 2], [3, 4]]);
    // create a JavaScript array out of matrix
    var arr = mat.toArray(); // returns an array [[1, 2], [3, 4]]

Returns

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

### toMat() → module:la.Matrix

Returns a copy of the matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a random matrix
    var mat = new la.Matrix({ rows: 5, cols: 4, random: true });
    // create a copy of the matrix
    var copy = mat.toMat();

Returns

  `module:la.Matrix`B Matrix copy.

### toString() → string

Returns the matrix as string.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a new matrix
    var mat = new la.Matrix([[1, 2], [3, 5]]);
    // get matrix as string
    var text = mat.toString(); // returns `1 2 \n3 5 \n\n`

Returns

  `string`B Dense matrix as string.

### transpose() → module:la.Matrix

Transposes the matrix.

#### Example

    // import la module
    var la = require('qminer').la;
    // create a matrix
    var mat = new la.Matrix([[2, -5], [3, 1]]);
    // transpose the matrix
    // the return matrix is
    //  2   3
    // -5   1
    var trans = mat.transpose();

Returns

  `module:la.Matrix`B Transposed matrix.