Home Manual Reference Source Test Repository
public class | source

MathUtil

Provides common math utilities.

Static Method Summary

Static Public Methods
public static

compactMatrix(matrix: Array<Array<number>>, testValue: *): Array<{row: number, cols: Array<number>}>

Compacts a 2D matrix testing entries against a testValue with a default value of 1 for inclusion.

public static

Creates an 2-dimensional array of the given length.

public static

Returns the median / middle value from the given array after sorting numerically.

public static

getPercent(value: number, limit: number): number

Returns the percent of a given value and limit.

public static

toFixed(val: *): *

Converts floating point numbers to a fixed decimal length of 3.

public static

Performs a naive depth traversal of an object / array.

Static Public Methods

public static compactMatrix(matrix: Array<Array<number>>, testValue: *): Array<{row: number, cols: Array<number>}> source

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.

Params:

NameTypeAttributeDescription
matrix Array<Array<number>>

A matrix to compact / compress.

testValue *

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

Return:

Array<{row: number, cols: Array<number>}>

public static create2DArray(length: number, value: number): Array<Array<number>> source

Creates an 2-dimensional array of the given length.

Params:

NameTypeAttributeDescription
length number

Array length for x / y dimensions.

value number

Default value.

Return:

Array<Array<number>>

public static getMedian(values: Array<number>): number source

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.

Params:

NameTypeAttributeDescription
values Array<number>

An Array of numerical values.

Return:

number

public static getPercent(value: number, limit: number): number source

Returns the percent of a given value and limit.

Params:

NameTypeAttributeDescription
value number

A value to calculate the percentage against the given limit.

limit number

A base limit that constrains the value.

Return:

number

public static toFixed(val: *): * source

Converts floating point numbers to a fixed decimal length of 3. This saves space and avoids precision issues with serializing / deserializing.

Params:

NameTypeAttributeDescription
val *

Any value; only floats are processed.

Return:

*

public static toFixedTraverse(data: object): * source

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.

Params:

NameTypeAttributeDescription
data object

An object or array.

Return:

*