Skip to content

mathx

Since v8.0.75

arrange()

Returns array of numbers.

Syntax
arrange(start, end, step)
Parameters
  • start Number start from
  • end Number end to
  • step Number increments
Return value

Array<Number> generated numbers in an array.

Usage example
1
2
3
4
5
6
const { arrange } = require('mathx');
arrange(0, 6, 3).forEach((i) => console.log(i))

// 0
// 3
// 6

linspace()

Returns array of numbers.

Syntax
linspace(start, end, count)
Parameters
  • start Number start from
  • end Number end to
  • count Number total count of numbers to generate
Return value

Array<Number> generated numbers in an array.

Usage example
1
2
3
4
5
6
const { linspace } = require('mathx');
linspace(0, 1, 3).forEach((i) => console.log(i));

// 0
// 0.5
// 1

meshgrid()

Returns array of numbers array.

Syntax
meshgrid(arr1, arr2)
Parameters
  • arr1 Array<Number>
  • arr2 Array<Number>
Return value

``Array<Array>` generated numbers in an array of numbers.

Usage example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const { meshgrid } = require('mathx');

const gen = meshgrid([1, 2, 3], [4, 5]);
for(i=0; i < gen.length; i++) {
    console.log(JSON.stringify(gen[i]));
}

// [1,4]
// [1,5]
// [2,4]
// [2,5]
// [3,4]
// [3,5]

oscillator()

Since v8.5.5

Generates synthetic oscillator samples as [time, value] tuples.

Syntax
oscillator(options)
Parameters
  • options Object
  • options.components Object[] Required. Oscillator components to sum.
    • amplitude Number Required. Signal amplitude.
    • frequencyHz Number Required. Frequency in Hz.
    • phaseRad Number Optional. Phase offset in radians. Default is 0.
    • bias Number Optional. DC offset. Default is 0.
  • options.timeRange Object Required. Time range object.
    • from Any Required.
    • to Any Required.
    • 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
  • options.sample Number|String Optional.
    • Number: interpreted as sample count.
    • String with Hz/hz suffix: interpreted as sample rate (for example "2Hz").
  • options.noise Number|Object Optional.
    • Number: interpreted as noise amplitude.
    • Object: { amplitude, seed? } where amplitude is the noise strength and seed makes the noise deterministic.
Return value

Array<[time, Number]> generated samples.

Usage example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const { oscillator } = require('mathx');

const gen = oscillator({
        components: [
                { amplitude: 1.0, frequencyHz: 0.1, phaseRad: 0 },
                { amplitude: 0.5, frequencyHz: 0.05 },
        ],
        timeRange: { from: '0s', to: '10s' },
        sample: '2Hz',
        noise: { amplitude: 0.1, seed: 123 },
});

for (let i = 0; i < gen.length; i++) {
        const t = gen[i][0];
        const v = gen[i][1];
        console.log(t, v.toFixed(2));
}

series()

Since v8.5.5

Converts tuple samples into separate time/value arrays.

Syntax
series(samples, options)
Parameters
  • samples Array<[time, Number]> tuple samples.
  • options Object Optional.
    • xKey String key name for x-axis array. Default is "time".
    • yKey String key 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.5

Splits tuple samples into two arrays.

Syntax
unzip(samples)
Parameters
  • samples Array<[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.5

Combines two arrays into tuple samples.

Syntax
zip(x, y)
Parameters
  • x Array<any> x-axis data array.
  • y Array<any> y-axis data array. Must have the same length as x.
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

1
2
const m = require('mathx');
console.log(m.sort([1.3, 1.2, 1.1])); // [1.1, 1.2, 1.3]

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

1
2
3
const m = require('mathx');
console.log(m.sum([3, 1, 2]));       // 6
console.log(m.sum([1.3, 1.2, 1.1])); // 3.6

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)
  • q Number
  • x Array<Number> The x data must be sorted in increasing order.
  • weights Array<Number> If weights is not specified then all of the weights are 1. If weights is specified, then length of x must equal length of weights.

Usage example

1
2
3
4
5
6
7
const m = require('mathx');
x = [];
for( i=1; i<=100; i++) {
    x.push(i);
}
x = m.sort(x);
console.log(m); // 0.01

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

1
2
3
const m = require('mathx');
console.log(m.mean([1, 2, 3, 4, 5]));  // 3
console.log(m.mean([10, 20, 30]));     // 20

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

1
2
3
4
5
const m = require('mathx');
x = [0, 0.25 * Math.PI, 0.75 * Math.PI];
w = [1, 2, 2.5];
console.log(m.circularMean(x).toFixed(4));     // 0.9553
console.log(m.circularMean(x, w).toFixed(4));  // 1.3704

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

1
2
3
4
5
6
const m = require('mathx');
x = [8, -3, 7, 8, -4];
y = [10, 5, 6, 3, -1];
w = [2, 1.5, 3, 3, 2];
console.log(m.correlation(x, y).toFixed(5));     // 0.61922
console.log(m.correlation(x, y, w).toFixed(5));  // 0.59915

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

1
2
3
4
5
6
7
const m = require('mathx');
x = [8, -3, 7, 8, -4];
y1 = [10, 2, 2, 4, 1];
y2 = [12, 1, 11, 12, 0];
console.log(m.covariance(x, y1).toFixed(4)); // 13.8000
console.log(m.covariance(x, y2).toFixed(4)); // 37.7000
console.log(m.variance(x).toFixed(4));       // 37.7000

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

1
2
3
4
5
const m = require('mathx');
console.log(m.entropy([0.05, 0.1, 0.9, 0.05]).toFixed(4)); // 0.6247
console.log(m.entropy([0.2, 0.4, 0.25, 0.15]).toFixed(4)); // 1.3195
console.log(m.entropy([0.2, 0, 0, 0.5, 0, 0.2, 0.1, 0, 0, 0]).toFixed(4)); // 1.2206
console.log(m.entropy([0, 0, 1, 0]).toFixed(4));           // 0.0000

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

1
2
3
const m = require('mathx');
console.log(m.geometricMean([1, 3, 9]).toFixed(4));  // 3.0000
console.log(m.geometricMean([2, 8, 32]).toFixed(4)); // 8.0000

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

1
2
3
const m = require('mathx');
console.log(m.harmonicMean([1, 2, 4]).toFixed(4));    // 1.7143
console.log(m.harmonicMean([10, 20, 30]).toFixed(4)); // 16.3636

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

1
2
3
const m = require('mathx');
console.log(m.median(m.sort([1, 3, 2, 5, 4])));      // 3
console.log(m.median(m.sort([10, 20, 30, 40, 50]))); // 30

medianInterp()

The medianInterp function is same as median except it returns the linear interpolated value.

Usage example

1
2
3
const m = require('mathx');
console.log(m.medianInterp(m.sort([1, 3, 2, 5, 4])));      // 2.5
console.log(m.medianInterp(m.sort([10, 20, 30, 40, 50]))); // 25

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)
  • p Number
  • x Array<Number> The x data must be sorted in increasing order.
  • weights Array<Number> If weights is not specified then all of the weights are 1. If weights is specified, then length of x must equal length of weights.

Usage example

1
2
3
4
5
const m = require('mathx');
data = m.sort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
console.log(m.quantile(0.25, data)); // 3
console.log(m.quantile(0.5, data));  // 5
console.log(m.quantile(0.74, data)); // 8

quantileInterp()

The quantileInterp function is same as quantile except it returns the linear interpolated value.

Syntax
quantileInterp(p, x, weights)
  • p Number
  • x Array<Number> The x data must be sorted in increasing order.
  • weights Array<Number> If weights is not specified then all of the weights are 1. If weights is specified, then length of x must equal length of weights.

Usage example

1
2
3
4
5
const m = require('mathx');
data = m.sort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
console.log(m.quantileInterp(0.25, data)); // 2.5
console.log(m.quantileInterp(0.5, data));  // 5
console.log(m.quantileInterp(0.74, data)); // 7.4

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

1
2
3
4
5
const m = require('mathx');
data = [1, 2, 3, 4, 5];
result = m.meanStdDev(data);
console.log(result.mean.toFixed(2));   // 3.00
console.log(result.stdDev.toFixed(2)); // 1.58

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)
  • x Array<Number> The x data must be sorted in increasing order.
  • weights Array<Number> If weights is not specified then all of the weights are 1. If weights is specified, then length of x must equal length of weights.

Usage example

1
2
3
4
5
const m = require('mathx');
data = m.sort([1, 2, 2, 3, 4]);
console.log(m.mode(data)); // {value:2, count:2}
data = m.sort([1, 1, 2, 3, 4]);
console.log(m.mode(data)); // {value:1, count:2}

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

1
2
3
4
const m = require('mathx');
data = [1, 2, 3, 4, 5];
console.log(m.moment(2, data).toFixed(4)); // 2.5000
console.log(m.moment(4, data).toFixed(4)); // 6.8000

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

1
2
3
const m = require('mathx');
console.log(m.stdDev([1, 2, 3, 4, 5]).toFixed(4));      // 1.5811
console.log(m.stdDev([10, 20, 30, 40, 50]).toFixed(4)); // 15.8114

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

1
2
3
4
const m = require('mathx');
let stddev = m.stdDev([1, 2, 3, 4, 5]);
let sampleSize = 5;
console.log(m.stdErr(stddev, sampleSize).toFixed(4)); // 0.7071

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

1
2
3
4
5
6
const m = require('mathx');
x = [1, 2, 3, 4, 5];
y = [2, 4, 6, 8, 10];
result = m.linearRegression(x, y);
console.log(result.slope.toFixed(4));     // 2.0000
console.log(result.intercept.toFixed(4)); // 0.0000
Last updated on