mixin

Funcs

Funcs : Funcs

meta mixin slots pi

Return constant for pi

remainder

Return the remainder or modulo of division

ceil

Return the smallest whole number greater than or equal to val

floor

Return the largest whole number less than or equal to val

round

Returns the nearest whole number to val

exp

Return e raised to val

logE

Return natural logarithm to the base e of val

log10

Return base 10 logarithm of val

pow

Return val raised to the specified power

sqrt

Return square root of val

random

Return random integer within given inclusive range

bitNot

Bitwise not

bitAnd

Bitwise and

bitOr

Bitwise or

bitXor

Bitwise xor

bitShiftr

Bitwise right shift

bitShiftl

Bitwise left shift

acos

Return the arc cosine

asin

Return the arc sine

atan

Return the arc tangent

atan2

Converts rectangular coordinates (x, y) to polar (r, theta)

cos

Return the cosine of angle in radians

cosh

Return the hyperbolic cosine

sin

Return sine of angle in radians

sinh

Return hyperbolic sine

tan

Return tangent of angle in radians

tanh

Return hyperbolic tangent

toDegrees

Convert angle in radians to an angle in degrees

toRadians

Convert angle in degrees to an angle in radians

mean

Fold a sample of numbers into their standard average or arithmetic mean

median

Fold a sample of numbers into their median value which is the middle value of the sorted samples

rootMeanSquareErr

Fold a sample of numbers into their RMSE (root mean square error)

meanBiasErr

Fold a sample of numbers into their MBE (mean bias error)

standardDeviation

Fold a series of numbers into the standard deviation of a sample

quantile

Computes the pth quantile of a list of numbers, according to the specified interpolation method

toMatrix

Convert a general grid to an optimized matrix grid

matrixTranspose

Transpose the given matrix which is any value accepted by toMatrix

matrixDeterminant

Return the determinant as a unitless Number for the given matrix which is any value accepted by toMatrix

matrixInverse

Return the inverse of the given matrix which is any value accepted by toMatrix

matrixAdd

Add two matrices together and return new matrix

matrixSub

Subtract two matrices and return new matrix

matrixMult

Multiply two matrices and return new matrix

matrixFitLinearRegression

Given a matrix of y coordinates and a matrix of multiple x coordinates compute the best fit multiple linear regression equation using the ordinary least squares method

fitLinearRegression

Given a grid of x, y coordinates compute the best fit linear regression equation using the ordinary least squares method

pi () => Number

Return constant for pi: 3.141592653589793

remainder (a: Number, b: Number) => Number

Return the remainder or modulo of division: a % b. Result has same unit as a.

ceil (val: Number) => Number

Return the smallest whole number greater than or equal to val. Result has same unit as val.

floor (val: Number) => Number

Return the largest whole number less than or equal to val. Result has same unit as val.

round (val: Number) => Number

Returns the nearest whole number to val. Result has same unit as val.

exp (val: Number) => Number

Return e raised to val.

logE (val: Number) => Number

Return natural logarithm to the base e of val.

log10 (val: Number) => Number

Return base 10 logarithm of val.

pow (val: Number, exp: Number) => Number

Return val raised to the specified power.

sqrt (val: Number) => Number

Return square root of val.

random (range: Obj?) => Number

Return random integer within given inclusive range. If range is null, then full range of representative integers is assumed.

Examples:

 random()       // random num with no range
 random(0..100) // random num between 0 and 100

bitNot (a: Number) => Number

Bitwise not: ~a

bitAnd (a: Number, b: Number) => Number

Bitwise and: a & b

bitOr (a: Number, b: Number) => Number

Bitwise or: a | b

bitXor (a: Number, b: Number) => Number

Bitwise xor: a ^ b

bitShiftr (a: Number, b: Number) => Number

Bitwise right shift: a >> b

bitShiftl (a: Number, b: Number) => Number

Bitwise left shift: a << b

acos (val: Number) => Number

Return the arc cosine.

asin (val: Number) => Number

Return the arc sine.

atan (val: Number) => Number

Return the arc tangent.

atan2 (y: Number, x: Number) => Number

Converts rectangular coordinates (x, y) to polar (r, theta).

cos (val: Number) => Number

Return the cosine of angle in radians.

cosh (val: Number) => Number

Return the hyperbolic cosine.

sin (val: Number) => Number

Return sine of angle in radians.

sinh (val: Number) => Number

Return hyperbolic sine.

tan (val: Number) => Number

Return tangent of angle in radians.

tanh (val: Number) => Number

Return hyperbolic tangent.

toDegrees (val: Number) => Number

Convert angle in radians to an angle in degrees.

toRadians (val: Number) => Number

Convert angle in degrees to an angle in radians.

mean (val: Obj?, acc: Obj?) => Obj? <foldOn:Number>

Fold a sample of numbers into their standard average or arithmetic mean. This function is the same as core::avg. Nulls values are ignored. Return null if no values.

Example:

[2, 4, 5, 3].fold(mean)

median (val: Obj?, acc: Obj?) => Obj? <foldOn:Number>

Fold a sample of numbers into their median value which is the middle value of the sorted samples. If there are an even number of sample, then the median is the mean of the middle two. Null values are ignored. Return null if no values.

Example:

[2, 4, 5, 3, 1].fold(median)

rootMeanSquareErr (val: Obj?, acc: Obj?, nDegrees: Number) => Obj? <foldOn:Number>

Fold a sample of numbers into their RMSE (root mean square error). The RMSE function determines the RMSE between a sample set and its mean using the n-degrees of freedom RMSE:

RMBE = sqrt( Σ(xᵢ - median)² ) / (n - nDegrees)

Examples:

samples.fold(rootMeanSquareErr)         // unbiased zero degrees of freedom
samples.fold(rootMeanSquareErr(_,_,1))  // 1 degree of freedom

meanBiasErr (val: Obj?, acc: Obj?, nDegrees: Number) => Obj? <foldOn:Number>

Fold a sample of numbers into their MBE (mean bias error). The MBE function determines the MBE between a sample set and its mean:

MBE = Σ(xᵢ - median) / (n - nDegrees)

Examples:

samples.fold(meanBiasErr)         // unbiased zero degrees of freedom
samples.fold(meanBiasErr(_,_,1))  // 1 degree of freedom

standardDeviation (val: Obj?, acc: Obj?) => Obj? <foldOn:Number>

Fold a series of numbers into the standard deviation of a sample:

s = sqrt(Σ (xᵢ - mean)² / (n-1))

Example:

[4, 2, 5, 8, 6].fold(standardDeviation)

quantile (percent: Number, method: Str) => Obj?

Computes the pth quantile of a list of numbers, according to the specified interpolation method. The value p must be a number between 0.0 to 1.0.

  • linear (default): Interpolates proportionally between the two closest values
  • nearest: Rounds to the nearest data point
  • lower: Rounds to the nearest lower data point
  • higher: Rounds to the nearest higher data point
  • midpoint: Averages two nearest values

Usage:

[1,2,3].fold(quantile(p, method))

Examples:

[10,10,10,25,100].fold(quantile(0.7 )) => 22 //default to linear
[10,10,10,25,100].fold(quantile(0.7, "nearest")) => 25
[10,10,10,25,100].fold(quantile(0.7, "lower")) => 10
[10,10,10,25,100].fold(quantile(0.7, "higher")) => 25
[10,10,10,25,100].fold(quantile(0.7, "linear")) => 22 //same as no arg
[10,10,10,25,100].fold(quantile(0.7, "midpoint")) => 17.5

Detailed Logic:

 p: percentile (decimal 0-1)
 n: list size
 rank: p * (n-1) // this is the index of the percentile in your list
 // if rank is an integer, return list[rank]
 // if rank is not an integer, interpolate via one of the above methods (illustrated below in examples)

 [1,2,3,4,5].percentile(0.5) => 3 // rank=2 is an int so we can index[2] directly

 [10,10,10, 25, 100].percentile(0.7, method)
   rank = (0.7 * 4) => 2.8

   //adjust rank based on method
   nearest =  index[3]                // => 25
   lower =    index[2]                // => 10
   higher =   index[3]                // => 25

   //or interpolate for these methods

   //takes the 2 closest indices and calculates midpoint
   midpoint = (25-10)/2 + 10          // => 17.5

   //takes the 2 closest indices and calculates weighted average
   linear =   (0.2 * 10) + (0.8 * 25) // => 22

toMatrix (obj: Obj, opts: Dict) => Grid

Convert a general grid to an optimized matrix grid. Matrixs are two dimensional grids of Numbers. Columns are named "v0", "v1", "v2", etc. Grid meta is preserved, but not column meta. Numbers in the resulting matrix are unitless; any units passed in are stripped.

The following options are supported:

  • nullVal (Number): replace null values in the grid with this value
  • naVal (Number): replace NA values in the grid with this value
  toMatrix(grid, {nullVal: 0, naVal: 0})

To create a sparse or initialized matrix you can pass a Dict with the the following tags (all required)

toMatrix({rows:10, cols: 1000, init: 0})

matrixTranspose (m: Obj) => Grid

Transpose the given matrix which is any value accepted by toMatrix.

matrixDeterminant (m: Obj) => Number

Return the determinant as a unitless Number for the given matrix which is any value accepted by toMatrix. The matrix must be square.

matrixInverse (m: Obj) => Grid

Return the inverse of the given matrix which is any value accepted by toMatrix.

matrixAdd (a: Obj, b: Obj) => Grid

Add two matrices together and return new matrix. The parameters may be any value supported toMatrix. Matrices must have the same dimensions.

matrixSub (a: Obj, b: Obj) => Grid

Subtract two matrices and return new matrix. The parameters may be any value supported toMatrix. Matrices must have the same dimensions.

matrixMult (a: Obj, b: Obj) => Grid

Multiply two matrices and return new matrix. The parameters may be any value supported toMatrix. Matrix a column count must match matrix b row count.

matrixFitLinearRegression (y: Obj, x: Obj) => Grid

Given a matrix of y coordinates and a matrix of multiple x coordinates compute the best fit multiple linear regression equation using the ordinary least squares method. Both y and x may be any value accepted by toMatrix.

The resulting linear equation for r X coordinates is:

yᵢ = bias + b₁xᵢ₁ + b₂xᵢ₂ +...+ bᵣxᵢᵣ

The equation is returned as a grid. The grid meta:

  • bias: bias or zero coefficient which is independent of any of the x factors
  • r2: R² coefficient of determination as a number between 1.0 (perfect correlation) and 0.0 (no correlation)
  • r: the square root of R², referred to as the correlation coefficient
  • rowCount: the number of rows of data used in the correlation For each X factor there is a row with the following tags:
  • b: the correlation coefficient for the given X factor

fitLinearRegression (grid: Grid, opts: Dict?) => Dict

Given a grid of x, y coordinates compute the best fit linear regression equation using the ordinary least squares method. The first column of the grid is used for x and the second column is y. Any rows without a Number for both x and y are skipped. Any special Numbers (infinity/NaN) are skipped.

Options:

  • x: column name to use for x if not first column
  • y: column name to use for y if not second column

The resulting linear equation is:

yᵢ = mxᵢ + b

The equation is returned as a dictionary with these keys:

  • m: slope of the best fit regression line
  • b: intercept of the best fit regression line
  • r2: R² coefficient of determination as a number between 1.0 (perfect correlation) and 0.0 (no correlation)
  • xmin: minimum value of x variable in sample data
  • xmax: maximum value of x variable in sample data
  • ymin: minimum value of y variable in sample data
  • ymax: maximum value of y variable in sample data

Also see matrixFitLinearRegression to compute a multiple linear regression.

Example:

data: [{x:1, y:2},
       {x:2, y:4},
       {x:4, y:4},
       {x:6, y:5}].toGrid
 fitLinearRegression(data)

 >>> {m:0.4915, b: 2.1525, r2: 0.7502}

Haxall 4.0.5 ∙ 24-Feb-2026 14:33 EST