mathx
arrange()
Returns array of numbers.
Syntax
arrange(start, end, step)Parameters
startNumberstart fromendNumberend tostepNumberincrements
Return value
Array<Number> generated numbers in an array.
Usage example
| |
linspace()
Returns array of numbers.
Syntax
linspace(start, end, count)Parameters
startNumberstart fromendNumberend tocountNumbertotal count of numbers to generate
Return value
Array<Number> generated numbers in an array.
Usage example
| |
meshgrid()
Returns array of numbers array.
Syntax
meshgrid(arr1, arr2)Parameters
arr1Array<Number>arr2Array<Number>
Return value
``Array<Array
Usage example
| |
oscillator()
Since v8.5.5Generates synthetic oscillator samples as [time, value] tuples.
Syntax
oscillator(options)Parameters
optionsObjectoptions.componentsObject[]Required. Oscillator components to sum.amplitudeNumberRequired. Signal amplitude.frequencyHzNumberRequired. Frequency in Hz.phaseRadNumberOptional. Phase offset in radians. Default is0.biasNumberOptional. DC offset. Default is0.
options.timeRangeObjectRequired. Time range object.fromAnyRequired.toAnyRequired.- Supported time literals:
- epoch nanoseconds as number (for example
0,10000000000) - epoch strings with suffix:
s,ms,us,ns(for example"10s","10000ms") - string time expressions such as
"now","now-10s" - RFC3339/RFC3339Nano time strings
- epoch nanoseconds as number (for example
options.sampleNumber|StringOptional.- Number: interpreted as sample count.
- String with
Hz/hzsuffix: interpreted as sample rate (for example"2Hz").
options.noiseNumber|ObjectOptional.- Number: interpreted as noise amplitude.
- Object:
{ amplitude, seed? }whereamplitudeis the noise strength andseedmakes the noise deterministic.
Return value
Array<[time, Number]> generated samples.
Usage example
| |
series()
Since v8.5.5Converts tuple samples into separate time/value arrays.
Syntax
series(samples, options)Parameters
samplesArray<[time, Number]>tuple samples.optionsObjectOptional.xKeyStringkey name for x-axis array. Default is"time".yKeyStringkey name for y-axis array. Default is"value".
Return value
Object containing two arrays. Default shape is { time, value }.
Usage example
const m = require("mathx");
const gen = m.oscillator({
components: [{ amplitude: 1.0, frequencyHz: 0.1 }],
timeRange: { from: "0s", to: "10s" },
sample: 5,
});
const s = m.series(gen);
console.log(s.time.length); // 5
console.log(s.value.length); // 5
const custom = m.series(gen, { xKey: "ts", yKey: "amp" });
console.log(custom.ts.length); // 5
console.log(custom.amp.length); // 5
unzip()
Since v8.5.5Splits tuple samples into two arrays.
Syntax
unzip(samples)Parameters
samplesArray<[x, y]>tuple samples.
Return value
[Array<x>, Array<y>]
Usage example
const m = require("mathx");
const [x, y] = m.unzip([[1, 10], [2, 20], [3, 30]]);
console.log(x); // [1, 2, 3]
console.log(y); // [10, 20, 30]
zip()
Since v8.5.5Combines two arrays into tuple samples.
Syntax
zip(x, y)Parameters
xArray<any>x-axis data array.yArray<any>y-axis data array. Must have the same length asx.
Return value
Array<[x, y]>
Usage example
const m = require("mathx");
const samples = m.zip([1, 2, 3], [10, 20, 30]);
console.log(samples[0]); // [1, 10]
fft()
The fft function performs a Fast Fourier Transform (FFT) on a given dataset.
FFT is used to analyze the frequency components of a signal, making it useful in signal processing and data analysis.
It accepts input in two forms and returns an array of [frequency, amplitude] pairs.
Syntax
fft(times, amplitudes) // time array and amplitude array
fft(timesAndAmplitudes) // array of [time, amplitude]
fft(times, amplitudes)Passes the time values and amplitude values as two separate arrays.fft(timesAndAmplitudes)Passes an array of[time, amplitude]pairs.
When times and amplitudes are provided separately, the two arrays must have the same length.
Return value
Array<[frequency, amplitude]> array of frequency and amplitude pairs.
Usage example
const m = require("mathx");
const gen = m.oscillator({
components: [
{amplitude: 1.0, frequencyHz: 15},
{amplitude: 1.5, frequencyHz: 24},
],
timeRange: {from: 'now', to: 'now+10s'},
sample: "1000Hz",
});
// gen is array of tuple (time, amplitude)
// ffp() accepts array of (time, amplitude)
// and returns array of (frequency, amplitude)
const result = m.fft(gen);
for(i=0; i < result.length; i++) {
console.println(i, result[i][0], result[i][1]);
}sort()
The sort function sorts the elements of an array in ascending order.
It is useful for organizing data or preparing it for further analysis.
Usage example
| |
sum()
The sum function calculates the total sum of all numbers in an array.
It is commonly used in statistical and mathematical computations.
Usage example
| |
cdf()
The cdf function calculates the cumulative distribution function (CDF) for a given dataset x
that is the fraction of the samples less than or equal to q.
It represents the probability that a random variable takes on a value less than or equal to a specified value.
This function is commonly used in statistical analysis and probability theory to understand the distribution of data.
Syntax
cdf(q, x, weights)qNumberxArray<Number>Thexdata must be sorted in increasing order.weightsArray<Number>If weights is not specified then all of the weights are 1. If weights is specified, then length ofxmust equal length ofweights.
Usage example
| |
mean()
The mean function calculates the arithmetic mean (average) of a given array of numbers.
It is computed by summing all the elements in the array and dividing by the total number of elements.
This function is commonly used in statistical analysis to determine the central tendency of a dataset.
Usage example
| |
circularMean()
The circularMean function calculates the mean of angles measured in radians, taking into account the circular nature of angles.
It is particularly useful for datasets where values wrap around, such as angles or time of day.
Optionally, weights can be provided to compute a weighted circular mean.
Usage example
| |
correlation()
The correlation function calculates the Pearson correlation coefficient between two datasets.
It measures the linear relationship between the datasets,
with values ranging from -1 (perfect negative correlation) to 1 (perfect positive correlation).
Optionally, weights can be provided to compute a weighted correlation.
Usage example
| |
covariance()
The covariance function calculates the covariance between two datasets.
Covariance is a measure of how much two random variables vary together.
A positive covariance indicates that the variables tend to increase together,
while a negative covariance indicates that one variable tends to increase as the other decreases.
Usage example
| |
entropy()
The entropy function calculates the Shannon entropy of a probability distribution.
Entropy is a measure of uncertainty or randomness in the distribution.
It is commonly used in information theory and statistics.
Usage example
| |
geometricMean()
The geometricMean function calculates the geometric mean of a given array of positive numbers.
It is computed by multiplying all the elements in the array and then taking the nth root, where n is the total number of elements.
This function is commonly used in financial and statistical analysis to determine the average rate of return or growth.
Usage example
| |
harmonicMean()
The harmonicMean function calculates the harmonic mean of a given array of positive numbers.
It is computed as the reciprocal of the arithmetic mean of the reciprocals of the elements.
This function is particularly useful for datasets involving rates or ratios, such as speeds or densities.
Usage example
| |
median()
The median function calculates the median of a given array of numbers.
The median is the middle value when the numbers are sorted in ascending order.
If the array has an even number of elements, the median is the average of the two middle values.
This function is commonly used in statistical analysis to determine the central value of a dataset.
The input array should be sorted, otherwise it throws exception.
Usage example
| |
medianInterp()
The medianInterp function is same as median except it returns the linear interpolated value.
Usage example
| |
quantile()
The quantile function calculates the quantile of a given dataset for a specified probability.
Quantiles divide the dataset into intervals with equal probabilities, such as quartiles (4 intervals) or percentiles (100 intervals).
This function is useful for understanding the distribution of data.
Syntax
quantile(p, x, weights)pNumberxArray<Number>Thexdata must be sorted in increasing order.weightsArray<Number>If weights is not specified then all of the weights are 1. If weights is specified, then length ofxmust equal length ofweights.
Usage example
| |
quantileInterp()
The quantileInterp function is same as quantile except it returns the linear interpolated value.
Syntax
quantileInterp(p, x, weights)pNumberxArray<Number>Thexdata must be sorted in increasing order.weightsArray<Number>If weights is not specified then all of the weights are 1. If weights is specified, then length ofxmust equal length ofweights.
Usage example
| |
meanStdDev()
The meanStdDev function calculates both the mean and the standard deviation of a given array of numbers.
The mean represents the central tendency, while the standard deviation measures the spread or dispersion of the data.
This function is useful for summarizing datasets in statistical analysis.
Usage example
| |
mode()
The mode function calculates the mode of a given array of numbers.
The mode is the value that appears most frequently in the dataset.
If there are multiple modes, the function may return all of them or handle it based on implementation.
It returns {value: number, count: number}.
Syntax
mode(x, weights)xArray<Number>Thexdata must be sorted in increasing order.weightsArray<Number>If weights is not specified then all of the weights are 1. If weights is specified, then length ofxmust equal length ofweights.
Usage example
| |
moment()
The moment function calculates the nth moment of a dataset about a specified point.
Moments are used in statistics to describe the shape of a distribution, such as skewness (3rd moment) or kurtosis (4th moment).
Usage example
| |
stdDev()
The stdDev function calculates the standard deviation of a given array of numbers.
Standard deviation measures the amount of variation or dispersion in a dataset.
A low standard deviation indicates that the data points are close to the mean, while a high standard deviation indicates greater spread.
Usage example
| |
stdErr()
The stdErr function calculates the standard error of the mean for a given array of numbers.
The standard error measures the accuracy with which a sample mean represents the population mean.
It is computed as the standard deviation divided by the square root of the sample size.
Usage example
| |
linearRegression()
The linearRegression function performs a linear regression analysis on two datasets.
It calculates the best-fit line that minimizes the sum of squared residuals between the observed and predicted values.
This function is commonly used in predictive modeling and trend analysis.
It returns {slope: alpha, intercept: beta} where y = alpha*x + beta.
Usage example
| |