Skip to main content

MathUtil

Provides common math utilities.

Index

Constructors

constructor

Methods

staticcompactMatrix

  • compactMatrix(matrix: number[][], testValue?: number): { cols: number[]; row: number }[]
  • Compacts a 2D matrix testing entries against a testValue with a default value of 1 for inclusion. The resulting compacted array only has object hash entries for rows that contain column entries that pass the test. Each entry has a row entry as a number corresponding to a row index and a cols entry which is an array of numbers representing all column indexes that pass the test. This works well for large sparse matrices.


    Parameters

    • matrix: number[][]

      A matrix to compact / compress.

    • testValue: number = 1

      A value to test strict equality to include entry for compaction.

    Returns { cols: number[]; row: number }[]

staticcreate2DArray

  • create2DArray(length?: number, value?: number): number[][]
  • Creates an 2-dimensional array of the given length.


    Parameters

    • length: number = 0

      Array length for x / y dimensions.

    • value: number = 0

      Default value.

    Returns number[][]

staticgetMedian

  • getMedian(values: number[]): number
  • Returns the median / middle value from the given array after sorting numerically. If values length is odd the middle value in the array is returned otherwise if even two middle values are summed then divided by 2.


    Parameters

    • values: number[]

      An Array of numerical values.

    Returns number

staticgetPercent

  • getPercent(value: number, limit: number): number
  • Returns the percent of a given value and limit.


    Parameters

    • value: number

      A value to calculate the percentage against the given limit.

    • limit: number

      A base limit that constrains the value.

    Returns number

statictoFixed

  • toFixed(val: number): number
  • Converts floating point numbers to a fixed decimal length of 3. This saves space and avoids precision issues with serializing / deserializing.


    Parameters

    • val: number

      Any value; only floats are processed.

    Returns number

statictoFixedTraverse

  • toFixedTraverse(data: any): any
  • Performs a naive depth traversal of an object / array. The data structure must not have circular references. The result of the toFixed method is invoked for each leaf or array entry modifying any floating point number in place.


    Parameters

    • data: any

      An object or array.

    Returns any