la. IntVector
Source: ladoc.
The number vector representation. Wraps a C++ array.
Property
new IntVector([arg])
Vector - array of integers.
Example
var la = require('qminer').la;
// create a new empty vector
var vec = new la.IntVector();
// create a new vector
var vec2 = new la.IntVector([1, 2, 3]);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
arg |
(Array of number or module:la.IntVector) |
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.IntVector([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.IntVector([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
-
number
B Vector element.
getMaxIdx() → number
Gets the index of the maximal element.
- Returns
-
number
B Index of the maximal element in the vector. // import la modules var la = require('qminer').la; // create a new vector var vec = new la.IntVector([1, 2, 3]); // get the index of the maximum value var idx = vec.getMaxIdx();
load(fin) → module:la.IntVector
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.IntVector();
// 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.IntVector
B Self. The vector is filled using the input streamfin
.
loadascii(fin) → module:la.IntVector
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.IntVector();
// 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.IntVector
B Self. The vector is filled using the input streamfin
.
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.IntVector([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
-
number
B The 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.IntVector([1, 2, 3]);
var vec2 = new la.IntVector([4, 5]);
// append the two vectors
vec.pushV(vec2);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
vec |
|
The appended vector. |
- Returns
-
number
B The new length property of the vectors.
put(idx, val) → module:la.IntVector
Sets an element in vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.IntVector([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.IntVector
B Self. The values at indexidx
has 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.IntVector([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.FOut
B The 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.IntVector([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.FOut
B The output streamfout
.
shuffle() → module:la.IntVector
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.IntVector([-2, 1, 3]);
// shuffle the elements
vec.shuffle();
- Returns
-
module:la.IntVector
B Self. The elements are randomly reordered.
splice(start, deleteCount[, ...itemN]) → module:la.IntVector
Changes the vector by removing and adding elements.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.IntVector([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.IntVector
B Self. The selected elements are removed/replaced.
subVec(arg) → module:la.IntVector
Returns a subvector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.IntVector([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.IntVector
B Subvector, 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.IntVector([1, 2, 3]);
// sum all the elements of the vector
var sum = vec.sum();
- Returns
-
number
B The 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 integer vector
var vec = new la.IntVector([1, 2, 3]);
// create a JavaScript array out of vec
var arr = vec.toArray(); // returns an array [1, 2, 3]
- Returns
-
Array of number
B A JavaScript array of integers.
toString() → string
Returns the vector as string.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.IntVector([1, 2, 3]);
// create vector as string
vec.toString(); // returns '1, 2, 3'
- Returns
-
string
B String representation.
trunc(idx) → module:la.IntVector
Deletes elements with sprecific index or more.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.IntVector([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.IntVector
B Self 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.IntVector([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
-
number
B The new length of vector.