Scientific API
chevron down
 

Scientific API

Interface: Scientific

New in SDK 3.0

The Scientific module provides the ability to operate on Float32Arrays with near-native performance.

This module is designed to work with batched sensor data.

No operations will modify provided arrays. If the operation returns a Float32Array, the result will be a newly allocated Float32Array object.

import * as scientific from "scientific";
const arrX = new Float32Array(1,2,3,4,5);

console.log(`Sum=${scientific.sum(arrX)}`)
console.log(`Mean=${scientific.mean(arrX)}`)
console.log(`Max=${scientific.max(arrX)}`)
console.log(`Min=${scientific.min(arrX)}`)

cumsum()

New in SDK 4.2

Calculate the cumulative sum of values in an array, where each value is equal to the sum of all previous values in the array, including the current value.

If called with an empty array, returns an empty array.

This is equivalent to numpy.cumsum(arr).

scientific.cumsum(arr: Float32Array)

Parameters

arr: Float32Array

diff()

New in SDK 4.2

Calculate the difference between values in an array.

If called with an array of length 0 or 1, returns an empty array.

This is equivalent to numpy.diff(arr).

scientific.diff(arr: Float32Array)

Parameters

arr: Float32Array

clip()

New in SDK 4.2

Constrain the values in an array between min and max. If either min or max is undefined, clipping is not performed on the lower and higher edges respectively.

If min and max are both undefined, a TypeError is thrown.

This is equivalent to numpy.clip(arr, min, max).

scientific.clip(arr: Float32Array, min?: number | undefined, max?: number | undefined)

Parameters

arr: Float32Array
(optional) min: number or undefined
(optional) max: number or undefined

range()

Return evenly spaced values within a given interval, similar to Python's or PHP's range() or numpy's arange().

The given interval is interpreted as the half-open range [start, stop). That is, including start but not stop.

Arguments may be integers or floats, although floating point may yield surprising results.

The length of the resulting array is calculated as:

// Allow (0, 1, Infinity) to give the somewhat more correct
// result of [0], rather than [] as the length calculation
// alone would yield.
bool fixup_large_step = sgn(end - begin) == sgn(step);
double min = fixup_large_step ? 1.0 : 0.0;
len = ceil(max(min, (end - begin) / step));

start defaults to 0, and step defaults to 1.

scientific.range(stop: number)

Parameters

stop: number

scientific.range(start: number, stop: number)

Parameters

start: number
stop: number

scientific.range(start: number, stop: number, step: number)

Parameters

start: number
stop: number
step: number

stride()

Creates a copy of the given array, with a given stride, which may be negative. If a stride is not given, defaults to a value of 1. If the stride value is zero or NaN, the function throws. If the stride is a float, truncate (down) into an integer.

scientific.stride(arr: Float32Array, stride?: number | undefined)

Parameters

arr: Float32Array
(optional) stride: number or undefined

std()

Compute the population standard deviation of the elements in the given array. If the array is empty, NaN will be returned. Equivalent to:

Math.sqrt(var(arr))

scientific.std(arr: Float32Array)

Parameters

arr: Float32Array

variance()

Compute the population variance of the given array. If the array is empty, NaN will be returned. Equivalent to:

var avg = mean(arr)
mean(square(sub(arr, avg)))

scientific.variance(arr: Float32Array)

Parameters

arr: Float32Array

argmax()

Returns the index of the maximum value of the given array. If the array is empty, NaN will be returned. If the maximum value appears more than once in the array, the index of the first occurrence will be returned.

scientific.argmax(arr: Float32Array)

Parameters

arr: Float32Array

argmin()

Returns the index of the minimum value of the given array. If the array is empty, NaN will be returned. If the minimum value appears more than once in the array, the index of the first occurrence will be returned.

scientific.argmin(arr: Float32Array)

Parameters

arr: Float32Array

max()

Returns the maximum value of the given array. If the array is empty, -Infinity will be returned. If any value is NaN, NaN will be returned.

scientific.max(arr: Float32Array)

Parameters

arr: Float32Array

min()

Returns the minimum value of the given array. If the array is empty, +Infinity will be returned. If any value is NaN, NaN will be returned.

scientific.min(arr: Float32Array)

Parameters

arr: Float32Array

sum()

Computes the sum of the elements in the given array.

sum = arr[0] + arr[1] + ... + arr[arr.length - 1]

scientific.sum(arr: Float32Array)

Parameters

arr: Float32Array

mean()

Calculates the mean of the given array.

mean = sum(arr) / arr.length

If the array is empty, NaN will be returned.

scientific.mean(arr: Float32Array)

Parameters

arr: Float32Array

div()

Perform a division operation on a Float32Array.

Element-wise division of first array by the second: out[i] = a1[i] / a2[i] Division of each element by a constant:

out[i] = a[i] / c

Division of a constant by each element:

out[i] = c / a[i]

Division by ±0 will result in ±Infinity.

scientific.div(a1: Float32Array, a2: Float32Array)

Parameters

a1: Float32Array
a2: Float32Array

scientific.div(a: Float32Array, c: number)

Parameters

a: Float32Array
c: number

scientific.div(c: number, a: Float32Array)

Parameters

c: number
a: Float32Array

mul()

Perform a multiplication operation on a Float32Array.

Element-wise multiplication of two arrays:

out[i] = a1[i] * a2[i]

Multiplication of each element by a constant:

out[i] = a[i] * c

scientific.mul(a1: Float32Array, a2: Float32Array)

Parameters

a1: Float32Array
a2: Float32Array

scientific.mul(a: Float32Array, c: number)

Parameters

a: Float32Array
c: number

scientific.mul(c: number, a: Float32Array)

Parameters

c: number
a: Float32Array

sub()

Perform a subtraction operation on a Float32Array. Element-wise subtraction of the second array from the first:

out[i] = a1[i] - a2[i]

Subtraction of a constant from each element:

out[i] = a[i] - c

Subtraction of each element from a constant:

out[i] = c - a[i]

scientific.sub(a1: Float32Array, a2: Float32Array)

Parameters

a1: Float32Array
a2: Float32Array

scientific.sub(a: Float32Array, c: number)

Parameters

a: Float32Array
c: number

scientific.sub(c: number, a: Float32Array)

Parameters

c: number
a: Float32Array

add()

Perform an addition operation on a Float32Array.

Element-wise addition of two arrays:

out[i] = a1[i] + a2[i]

Addition of a constant to each element in the array:

out[i] = a[i] + c

scientific.add(a1: Float32Array, a2: Float32Array)

Parameters

a1: Float32Array
a2: Float32Array

scientific.add(a: Float32Array, c: number)

Parameters

a: Float32Array
c: number

scientific.add(c: number, a: Float32Array)

Parameters

c: number
a: Float32Array

sqrt()

Compute the square root of each element of the given array.

out[i] = sqrt(arr[i])

scientific.sqrt(arr: Float32Array)

Parameters

arr: Float32Array

square()

Compute the square of each element of the given array.

out[i] = arr[i] * arr[i]

scientific.square(arr: Float32Array)

Parameters

arr: Float32Array

abs()

Compute the absolute value of each element in the given array. This quantity is also variously referred to as magnitude or modulus (especially in the context of complex numbers).

out[i] = abs(arr[i])
out[i] = Math.sqrt(a.imag[i]*a.imag[i] + a.real[i]*a.real[i])

scientific.abs(arr: Float32Array)

Parameters

arr: Float32Array