new SparseVector([arg][, dim])

Sparse Vector.

Example

``````// import la module
var la = require('qminer').la;
// create new sparse vector with arrays
var spVec = new la.SparseVector([[0, 1], [2, 3], [3, 6]]); // sparse vector [1, 0, 3, 6]
// create new sparse vector with dim
var spVec2 = new la.SparseVector([[0, 1], [2, 3], [3, 6]], 5); // largest index (zero based) is 4``````

Parameters

Name Type Optional Description

arg

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

Yes

Constructor arguments. There are two ways of constructing:
1. Using a nested array of vector elements. Example: `[[0, 2],[2, 3]]` has two nonzero values, first value is 2 at position 0, second value is 3 at position 2,
2. using a sparse vector (copy constructor).

dim

number

Yes

Maximum length of sparse vector. It is only in combinantion with nested array of vector elements.

Properties

dim

Returns the dimension of sparse vector. Type `number`.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector and designate the dimension of the vector
var vec = new la.SparseVector([[0, 2], [3, 1], [7, 5], [11, 4]], 15);
// get the dimension of the sparse vector
// returns 15
var dim = vec.dim;``````

nnz

Returns the number of non-zero values. Type `number`.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var vec = new la.SparseVector([[0, 2], [3, 1], [7, 5], [11, 4]]);
// check the number of nonzero values in sparse vector
// returns 4
var nonz = vec.nnz;``````

Methods

at(idx) → number

Returns an element of the sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a sparse vector
var vec = new la.SparseVector([[0, 1], [3, 2], [4, -5]]);
// get the value at the position 3
vec.at(3); // returns the value 2``````

Parameter

Name Type Optional Description

idx

number

Index (zero based).

Returns

`number`B Sparse vector element.

full() → module:la.Vector

Returns the dense vector representation of the sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var vec = new la.SparseVector([[0, 2], [3, 1], [7, 5], [11, 4]]);
// create a dense representation of the vector
var dense = vec.full();``````
Returns

`module:la.Vector`B The dense vector representation.

idxVec() → module:la.Vector

Returns a dense vector of indices (zero based) of non-zero elements of sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sprase vector
var vec = new la.SparseVector([[0, 2], [3, 1], [7, 5], [11, 4]]);
// get the non-zero indeces of the sparse vector
var idxVec = vec.idxVec();``````
Returns

`module:la.Vector`B A dense vector of indeces.

inner(arg) → number

Returns the inner product of the parameter and the sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create two vectors, one sparse and one dense
var sparse = new la.SparseVector([[0, 1], [3, 2], [4, -5]]);
var dense = new la.Vector([3, -4, 2, 0.5, -1]);
// get the inner product of the vectors
sparse.inner(dense); // returns the value 9``````

Parameter

Name Type Optional Description

arg

The inner product input.

Returns

`number`B The inner product of the two vectors.

multiply(num) → module:la.SparseVector

Multiplies the sparse vector with a scalar.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var spVec = new la.SparseVector([[0, 1], [2, 3], [3, 6]]);
// multiply sparse vector with scalar 3.14
var spVec2 = spVec.multiply(3.14); // returns sparse vector [3.14, 0, 9.42, 18.84]``````

Parameter

Name Type Optional Description

num

number

The scalar.

Returns

`module:la.SparseVector`B The product of `num` and sparse vector.

norm() → number

Returns the norm of sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var vec = new la.SparseVector([[0, 2], [3, 1], [7, 5], [11, 4]]);
// get the norm of the vector
var norm = vec.norm();``````
Returns

`number`B Norm of sparse vector.

normalize() → module:la.SparseVector

Normalizes the sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var spVec = new la.SparseVector([[0, 1], [2, 3], [3, 6]]);
// normalize the sparse vector
spVec.normalize();``````
Returns

`module:la.SparseVector`B Self. The vector is normalized.

print()

Prints the sparse vector on-screen.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var spVec = new la.SparseVector([[0, 1], [2, 3]]);
// print sparse vector
spVec.print(); // shows on-screen [(0, 1), (2, 3)]``````

put(idx, num) → module:la.SparseVector

Puts a new element in sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var vec = new la.SparseVector([[0, 1], [3, 2], [4, -5]]);
// set the new values at position 2
vec.put(2, -4);``````

Parameters

Name Type Optional Description

idx

number

Index (zero based).

num

number

Input value.

Returns

`module:la.SparseVector`B Self. It puts/changes the values with the index `idx` to the value `num`.

sum() → number

Returns the sum of all values in sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var vec = new la.SparseVector([[0, 1], [3, 2], [4, -5]]);
// get the sum of the values in the vector
vec.sum(); // returns -2``````
Returns

`number`B The sum of all values in sparse vector.

toString() → string

Returns the string representation.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var spVec = new la.SparseVector([[0, 1], [2, 3]]);
// get the string representation of the vector
spVec.toString(); // returns the string '[(0, 1), (2, 3)]'``````
Returns

`string`B The string representation of the sparse vector.

valVec() → module:la.Vector

Returns a dense vector of values of non-zero elements of sparse vector.

Example

``````// import la module
var la = require('qminer').la;
// create a new sparse vector
var vec = new la.SparseVector([[0, 2], [3, 1], [7, 5], [11, 4]]);
// get the non-zero values of the sparse vector
var valVec = vec.valVec();``````
Returns

`module:la.Vector`B A dense vector of values.