class staticla. Vector
Source: ladoc.
The number vector representation. Wraps a C++ array.
Property
Methods
new Vector([arg])
Vector - array of doubles.
Example
var la = require('qminer').la;
// create a new empty vector
var vec = new la.Vector();
// create a new vector
var vec2 = new la.Vector([1, 2, 3]);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
arg |
(Array of number or module:la.Vector) |
Yes |
Constructor arguments. There are two ways of constructing:
|
Property
length
Gives the length of vector. Type number.
Example
var la = require('qminer').la;
// create a new vector
var x = new la.Vector([1, 2, 3]);
// get the length of the vector
var len = x.length; // returns 3
Methods
at(index) → number
Returns element at index.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// get the element at index 1
var el = vec[1];
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
index |
number |
|
Element index (zero-based). |
- Returns
-
numberVector element.
cosine(vec) → number
Returns the cosine between the two vectors.
Example
var la = require('qminer').la;
// create two new vectors
var x = new la.Vector([1, 0]);
var y = new la.Vector([0, 1]);
// calculate the cosine between those two vectors
var num = x.cosine(y); // returns 0
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
vec |
|
Second vector. |
- Returns
-
numberThe cosine between the two vectors.
diag()
Creates a dense diagonal matrix out of the vector.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([4, 5, -1]);
// create a dense matrix with the diagonal equal to vec
var mat = vec.diag();
getMaxIdx() → number
Gets the index of the maximal element.
- Returns
-
numberIndex of the maximal element in the vector. // import la modules var la = require('qminer').la; // create a new vector var vec = new la.Vector([1, 2, 3]); // get the index of the maximum value var idx = vec.getMaxIdx();
inner(vec) → number
Computes the inner product.
Example
var la = require('qminer').la;
// create two new vectors
var x = new la.Vector([1, 2, 3]);
var y = new la.Vector([4, 5, -1]);
// get the inner product of the two vectors
var prod = x.inner(y); // returns 11
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
vec |
|
Other vector. |
- Returns
-
numberInner product between the instance and the other vector.
load(fin) → module:la.Vector
Loads the vector from input stream (binary deserialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create an empty vector
var vec = new la.Vector();
// open a read stream
var fin = fs.openRead('vec.dat');
// load the vector
vec.load(fin);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
fin |
|
Input stream. |
- Returns
-
module:la.VectorSelf. The vector is filled using the input streamfin.
loadascii(fin) → module:la.Vector
Loads the vector from input stream (ascii deserialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create an empty vector
var vec = new la.Vector();
// open a read stream
var fin = fs.openRead('vec.dat');
// load the matrix
vec.loadascii(fin);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
fin |
|
Input stream. |
- Returns
-
module:la.VectorSelf. The vector is filled using the input streamfin.
minus(vec) → module:la.Vector
Vector substraction.
Example
var la = require('qminer').la;
// create two new vectors
var x = new la.Vector([1, 2, 3]);
var y = new la.Vector([4, 5, -1]);
// substract the vectors
var z = x.minus(y);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
vec |
|
Second vector. |
- Returns
-
module:la.VectorThe difference of the instance and the other vector.
multiply(val) → module:la.Vector
Multiplies the vector with a scalar.
Example
var la = require('qminer').la;
// create a new vector
var x = new la.Vector([4, 5, -1]);
// multiply the vector with the scalar 3
var y = x.multiply(3);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
val |
number |
|
Scalar. |
- Returns
-
module:la.VectorProduct of the vector and scalar.
norm() → number
Calculates the norm of the vector.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([4, 5, -1]);
// get the norm of the vector
var norm = vec.norm();
- Returns
-
numberThe norm of the vector.
normalize() → module:la.Vector
Normalizes vector.
Example
var la = require('qminer').la;
// create a new vector
var x = new la.Vector([4, 5, -1]);
// normalize the vector
x.normalize();
- Returns
-
module:la.VectorSelf. The vector is normalized.
outer(vec) → module:la.Matrix
Creates a dense matrix A by multiplying two vectors x and y: A = x * y^T.
Example
var la = require('qminer').la;
// create two new vectors
var x = new la.Vector([1, 2, 3]);
var y = new la.Vector([4, 5]);
// create the outer product of these vectors
var A = x.outer(y); // creates the dense matrix [[4, 5], [8, 10], [12, 15]]
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
vec |
|
Second vector. |
- Returns
-
module:la.MatrixMatrix obtained by the outer product of the instance and second vector.
plus(vec) → module:la.Vector
Vector addition.
Example
var la = require('qminer').la;
// create two new vectors
var x = new la.Vector([1, 2, 3]);
var y = new la.Vector([4, 5, -1]);
// sum the vectors
var z = x.plus(y);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
vec |
|
Second vector. |
- Returns
-
module:la.VectorSum of the instance and the second vector.
print()
Prints the vector on-screen.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// print the vector
// For this example it prints:
// [1, 2, 3]
vec.print();
push(val) → number
Adds an element to the end of the vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// push an element to the vector
vec.push(10);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
val |
number |
|
The element added to the vector. |
- Returns
-
numberThe new length property of the object upon which the method was called.
pushV(vec) → number
Appends a second vector to the first one.
Example
// import la module
var la = require('qminer').la;
// create two new vectors
var vec = new la.Vector([1, 2, 3]);
var vec2 = new la.Vector([4, 5]);
// append the two vectors
vec.pushV(vec2);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
vec |
|
The appended vector. |
- Returns
-
numberThe new length property of the vectors.
put(idx, val) → module:la.Vector
Sets an element in vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// set the first element to 10
vec.put(0, 10);
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
idx |
number |
|
Index (zero based). |
|
val |
number |
|
Element value. |
- Returns
-
module:la.VectorSelf. The values at indexidxhas been changed toval.
save(fout) → module:fs.FOut
Saves the vector as output stream (binary serialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// open write stream
var fout = fs.openWrite('vec.dat');
// save vector and close write stream
vec.save(fout).close();
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
fout |
|
Output stream. |
- Returns
-
module:fs.FOutThe output streamfout.
saveascii(fout) → module:fs.FOut
Saves the vector as output stream (ascii serialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// open write stream
var fout = fs.openWrite('vec.dat');
// save matrix and close write stream
vec.saveascii(fout).close();
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
fout |
|
Output stream. |
- Returns
-
module:fs.FOutThe output streamfout.
shuffle() → module:la.Vector
Randomly reorders the elements of the vector (inplace).
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([-2.0, 1.0, 3.0]);
// shuffle the elements
vec.shuffle();
- Returns
-
module:la.VectorSelf. The elements are randomly reordered.
sort([arg]) → module:la.Vector
Sorts the vector (in place operation).
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([-2.0, 1.0, 3.0]);
// sort ascending
vec.sort(); // sorts to: [-2.0, 1.0, 3.0]
// sort using callback
vec.sort(function(arg1, arg2) { return Math.abs(arg1) - Math.abs(arg2); }); // sorts to: [1.0, -2.0, 3.0]
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
arg |
(module:la~vectorCompareCb or boolean) |
Yes |
Sort callback or a boolean ascend flag. Default is boolean and true. |
- Returns
-
module:la.VectorSelf.
1. Vector sorted in ascending order, ifargis boolean and true.
2. Vector sorted in descending order, ifargis boolean and false.
3. Vector sorted by using the comparator callback, ifargis a module:la~vectorCompareCb.
sortPerm([asc]) → Object
Sorts the vector and returns the sorted vector as well as the permutation.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([-2.0, 1.0, 3.0]);
// sort ascending
var result = vec.sortPerm();
result.vec; // [-2.0, 1.0, 3.0]
result.perm; // permutation index vector
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
asc |
boolean |
Yes |
Sort in ascending order flag. Defaults to |
- Returns
-
ObjectThe objectVectorSortResultcontaining the properties:
VectorSortResult.vec- The sorted vector,
VectorSortResult.perm- Permutation vector, whereVectorSortResult.vec[i] = instanceVector[VectorSortResult.perm[i]].
sparse() → module:la.SparseVector
Creates the sparse vector representation of the vector.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([4, 5, -1]);
// create the sparse representation of the vector
var spVec = vec.sparse();
- Returns
-
module:la.SparseVectorThe sparse vector representation.
spDiag() → module:la.SparseMatrix
Creates a sparse diagonal matrix out of the vector.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([4, 5, -1]);
// create a sparse matrix with the diagonal equal to vec
var mat = vec.spDiag();
- Returns
-
module:la.SparseMatrixDiagonal matrix, where the (i, i)-th element is the i-th element of vector.
splice(start, deleteCount[, ...itemN]) → module:la.Vector
Changes the vector by removing and adding elements.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// splice the vector by removing the last two elements and adding 4, 5
vec.splice(1, 2, 4, 5)// returns vector [1, 4, 5]
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
start |
number |
|
Index at which to start changing the array. |
|
deleteCount |
number |
|
Number of elements to be removed. |
|
itemN |
number |
Yes |
The element(s) to be add to the array. If no elements are given, splice() will only remove elements from the array. Value can be repeated. |
- Returns
-
module:la.VectorSelf. The selected elements are removed/replaced.
subVec(arg) → module:la.Vector
Returns a subvector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// get the subvector of the first two elements
var subvec = vec.subVec([0, 1]);
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
arg |
(Array of number or module:la.IntVector) |
|
Index array or vector. Indices can repeat (zero based). |
- Returns
-
module:la.VectorSubvector, where the i-th element is thearg[i]-th element of the instance.
sum() → number
Sums the elements in the vector.
Example
// import la modules
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// sum all the elements of the vector
var sum = vec.sum();
- Returns
-
numberThe sum of all elements in the instance.
toArray() → 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 = vec.toArray(); // returns an array [1, 2, 3]
- Returns
-
Array of numberA JavaScript array of numbers.
toMat() → module:la.Matrix
Creates a matrix with a single column that is equal to the vector.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([4, 5, -1]);
// create a matrix representation of the vector
var mat = vec.toMat();
- Returns
-
module:la.MatrixThe matrix with a single column that is equal to the instance.
toString() → string
Returns the vector as string.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// create vector as string
vec.toString(); // returns '1, 2, 3'
- Returns
-
stringString representation.
trunc(idx) → module:la.Vector
Deletes elements with sprecific index or more.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// trunc all elements with index 1 or more
vec.trunc(1); // returns vector [1]
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
idx |
number |
|
Index (zero based). |
- Returns
-
module:la.VectorSelf after truncating.
unshift(...args) → number
Adds elements to the beginning of the vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.Vector([1, 2, 3]);
// add two elements to the beggining of the vector
var len = vec.unshift(4, 5); // returns 5
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
args |
number |
|
One or more elements to be added to the vector. Value can be repeated. |
- Returns
-
numberThe new length of vector.