UNPKG

atum

Version:

A Procedural Graph Based Javascript Library

118 lines (111 loc) 3.58 kB
/** * Theses function are used to redistribute data located in the range 0-1 * They take all the data and rearrange them and purturbe them slightly so that * they fit a particular distrubution function. For example you can use these * to push all the data points closer to 1 so that there are few points near 0 * each redistribution function has different properties. * * Properties of these functions * the domain is (0-1) for the range (0-1) * in this range the function is one to one * f(0) == 0 and f(1) == 1 * * @summary Functions used to redistrubute values in the range 0-1 * @class Redist */ "use strict"; /** * The identity function. It returns the input value x * * @export * @function * @param {Number} x The input number in the range [0-1] * @returns {Number} Input value * @memberof Redist */ export function identity(x) { return x; } /** * The inverse fuction. It returns the opposite of the function in the range * from [0-1]. This is simply 1 - x. * * @export * @function * @param {Number} x The input number in the range [0-1] * @returns {Number} The redistributed input value, 1 - x * @memberof Redist */ export function inverse(x) { return 1 - x; } /** * Exponential redistribution function. This function skews the values either * up or down by a particular ammount according the input parameters. The * output distribution will be slight exponential shaped. * * @export * @function * @param {Number} x The input number in the range [0-1] * @param {Number} [amm=1] The strength of the redistribution * @param {Boolean} [inc=true] If you want to increase or decrease the input * @returns {Number} The redistributed input value * @memberof Redist */ export function exp(x, amm = 1, inc = true) { let nom, denom; if (inc) { nom = 1 - Math.exp(-amm * x); denom = 1 - Math.exp(-amm); } else { nom = Math.exp(amm * x) - 1; denom = Math.exp(amm) - 1; } return nom / denom; } // Power Function eg sqrt qubrt /** * Power redistribution function. This function skews values either up or down * by a particular ammount according to the input parameters. The power * distribution also has a slight skew up or down on top of the redistribution. * * @export * @function * @param {Number} x The input number in the range [0-1] * @param {Number} [amm=2] The strength of the redistribution * @param {Boolean} [inc=true] If you want to increase or decrease the input * @param {Boolean} [skewDown=true] If you want to skew the input value down * towards 0, then skewDown=true. If you want to skew the input value up * towards 1, then skewDown=false * @returns {Number} The redistributed input value * @memberof Redist */ export function pow(x, amm = 2, inc = true, skewDown = true) { if (inc) { if (skewDown) { return Math.pow(x, 1 / amm); } else { return 1 - Math.pow(1 - x, amm); } } else { if (skewDown) { return Math.pow(x, amm); } else { return 1 - Math.pow(1 - x, 1 / amm); } } } /** * Turns a continious function and turns it into a discrete function that has * a specific number of bins to but the distribution into. * * @export * @function * @param {Number} x The input number in the range [0-1] * @param {Number} [bins=10] The number of bins for the discrite distribution * @returns {Number} The discretized input value * @memberof Redist */ export function step(x, bins = 10) { return Math.floor(bins * x) / bins; }