IntegersVector
IntegersVector is an one dimensional array or a vector of 32-bit integers.
The following scripts assume that you have imported the IntegersVector
object
from the package and set up the threads as explained in getting started.
#
Constructors MethodsThese methods are used to create new IntegersVector
s.
note
Random constructors are in the random section.
// Create an IntegersVector from a given JavaScript arrayconst a = new IntegersVector([1, 2, 3, 4, 5]);console.log(a.data); // [1, 2, 3, 4, 5]
// Create an IntegersVector filled with zeros of the specified lengthconst b = IntegersVector.newWithZeros(5);console.log(b.data); // [0, 0, 0, 0, 0]
// Create an IntegersVector filled with ones of the specified lengthconst c = IntegersVector.newWithOnes(5);console.log(c.data); // [1, 1, 1, 1, 1]
// Create an IntegersVector filled with given element of the specified lengthconst d = IntegersVector.newWithElement(5, 2);console.log(d.data); // [2, 2, 2, 2, 2]
// Create an IntegersVector of specified length calling the given function// without any parameters at every elementconst e = IntegersVector.newWithSimpleFunc(5, () => Math.floor(Math.random() * 10));console.log(e.data); // [5, 2, 3, 8, 1]
// Create an IntegersVector of specified length calling the given function with// the index as the only parameter for every elementconst f = IntegersVector.newWithFunc(5, i => i * i);console.log(f.data); // [0, 1, 4, 9, 16]
#
Interop MethodsSome handy methods to work with the array.
const a = new IntegersVector([1, 2, 3]);
// Both toJSON and data return a JavaScript array representation of the// IntegersVectorconsole.log(a.toJSON()); // [1, 2, 3]console.log(a.data); // [1, 2, 3]
// This returns the data and metadata about the IntegersVectorconsole.log(a.toString());// "[1, 2, 3], shape=[3], strides=[1], layout=CFcf (0xf), const ndim=1"
// It returns clone of the IntegersVectorconst b = a.clone();console.log(b.data); // [1, 2, 3]
#
Utility MethodsBasic getters and setters.
const x = new IntegersVector([1, 2, 3]);
// Get the length of the arrayconsole.log(x.len()); // 3
// Get the shape of the arrayconsole.log(x.shape()); // [3]
// Set the given value at the specified indexx.set(1, 7);
// Get the value at the specified indexconsole.log(x.get(1)); // 7
// Swap the values at the specified indicesx.swap(0, 2);console.log(x.data); // [3, 7, 1]
More complex methods used to manipulate the IntegersVector
.
note
Each of these methods has two versions. The "pure" version returns the result of performing the operation while the "impure" version actually changes the array.
append -> appended
,
extend -> extended
,
insert -> inserted
,
splice -> spliced
const a = new IntegersVector([1, 2, 3]);const b = new IntegersVector([4, 5, 6]);
// Reverse the IntegersVectorconsole.log(a.reversed().data);// [3, 2, 1]
// Append an element to the IntegersVectorconsole.log(a.appended(7).data);// [1, 2, 3, 7]
// Extend the IntegersVector with anotherconsole.log(a.extended(b).data);// [1, 2, 3, 4, 5, 6]
// Insert the given element at the specified indexconsole.log(a.inserted(1, 7).data);// [1, 7, 2, 3]
// Removes an element from the specified indexconst [spliced, element] = a.spliced(1);console.log(spliced.data, element);// [1, 3] 2
#
Iteration MethodsThese methods allow you to perform element-wise operations on the vector.
const a = new IntegersVector([1, 2, 3]);
const b = a.map(x => x * 3); console.log(b.data); // [3, 6, 9]
a.forEach(x => console.log(x));// 1// 2// 3
a.transform(x => x * x);console.log(a.data); // [1, 4, 9]
#
Math MethodsMethods to perform simple mathematical operations on the array.
const a = new IntegersVector([1, 2, 3]);const b = new IntegersVector([4, 5, 6]);
// Perform element-wise addition of two IntegersVectorsconsole.log(a.add(b).data); // [5, 7, 9]
// Perform element-wise subtraction of two IntegersVectorsconsole.log(a.sub(b).data); // [-3, -3, -3]
// Perform element-wise multiplication of two IntegersVectorsconsole.log(a.mul(b).data); // [4, 10, 18]
// Perform element-wise division of two IntegersVectorsconsole.log(b.div(a).data); // [4, 2, 2]
// Return the addition or product of the IntegersVectorconsole.log(a.sum()); // 6console.log(b.product()); // 120
// Efficiently perform in-place element-wise scaled addition of two IntegersVectorsa.scaledAdd(2, b);console.log(a.data); // [9, 12, 15]
#
Statistical MethodsMethods to perform basic statistical operations.
const a = new IntegersVector([1, 2, 3]);
// Return the minimum element in the arrayconsole.log(a.min()); // 1
// Return the minimum element in the arrayconsole.log(a.max()); // 3
// Return the mean of all the elements in the arrayconsole.log(a.mean()); // 2
#
Random MethodsMethods that involve randomness.
const a = IntegersVector.newWithRandom(5);console.log(a.data);// [-280599903, -1829359186, 7232578, -2034069497, -1238556954]
const b = a.sample(3);console.log(b.data);// [-1238556954, -280599903, 7232578]