linear-conversion
Linear conversion class for linear-converter
Install
npm i linear-conversion
Basic usage
var Decimal = ;var lc = Decimal;var LinearConversion = lc; // using linear-presets@1.xvar temp = PRESETStemperature; var celsiusToFahrenheit = tempcelsiusToFahrenheit; celsiusToFahrenheit; // => new Decimal('77')
See CodePen example for a quick interactive intro.
A simpler (although less flexible) setup is possible using linear-converter-to-go:
var lc = ;var LinearConversion = lc; // notice that in this case, the presets for common units are bundled// linear-presets@3.x or highervar temp = lcPRESETStemperature;
Conversion inversion
var fahrenheitToCelsius = celsiusToFahrenheit; fahrenheitToCelsius; // => 25 (as decimal)
Conversion composition
var celsiusToKelvin = tempcelsiusToKelvin;var kelvinToFahrenheit = celsiusToKelvin; kelvinToFahrenheit; // => 68 (as decimal)
Custom conversions
Custom conversions are achieved by passing an array with 2 scales, each of those an array with 2 values. For example, [[0, 1], [0, 2]] means that 0 and 1 in the first scale map to 0 and 2 in the second scale respectively; in short, it multiplies by 2. Any linear conversion can be described that way:
// f(x) = ax + b0 1 b a+b; // => ax + b1/a -b/a b+1 0; // => ax + b
For an arbitrary f(x) = ax + b, any [[x1, x2], [f(x1), f(x2)]] is a valid preset.
More examples:
// degrees to radians0 180 0 MathPI; // => 4 * Math.PI / 3 // f(x) = 3x0 1/3 0 1; // => 15 // f(x) = -2x - 460 1 -46 -48; // => 0
Coefficients
// f(x) = 2x + 1var doublePlus1 = 0 1 1 3; doublePlus1; // => 2doublePlus1; // => 1 // f(x) = ax + bvar timesAPlusB = x1 x2 ; timesAPlusB; // => atimesAPlusB; // => b
Preset equivalence
var eq = 1 5 3 -9; eq; // => true (both f(x) = -3x + 6) var notEq = 0 1 0 2; // f(x) = 2x notEq; // => false (new one is f(x) = 3x)
Arbitrary precision
Arbitrary precision support is provided via linear-arbitrary-precision. See all available adapters.
var doublePlusPoint1 = 0 01 01 03; // without arbitrary precision adaptersdoublePlusPoint1; // => 1.9999999999999998 // with arbitrary precision adaptersdoublePlusPoint1; // => 2
See CodePen example.
See more
Related projects
- linear-converter: flexible linear converter with built in conversions for common units.
- rescale: rescales a point given two scales.
- scale: scales normalised data.
- normalise: normalise data to [0, 1].
- rescale-util: rescale utilities.