Package Exports
- linear-conversion
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (linear-conversion) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
linear-conversion
Linear conversion class for linear-converter
Install
npm i linear-conversionBasic usage
var temp = require('linear-presets-temperature');
var LinearConversion = require('linear-conversion')(require('floating-adapter'));
var celsiusToFahrenheit = new LinearConversion(temp.celsiusToFahrenheit);
celsiusToFahrenheit.convert(25); // => 77See CodePen example for a quick interactive intro.
Conversion inversion
var fahrenheitToCelsius = celsiusToFahrenheit.invert();
fahrenheitToCelsius.convert(77); // => 25Conversion composition
var celsiusToKelvin = new LinearConversion(temp.celsiusToKelvin);
var kelvinToFahrenheit = celsiusToKelvin.invert().compose([celsiusToFahrenheit]);
kelvinToFahrenheit.convert(293.15); // => 68Custom conversions
Custom conversions are easily 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 + b
(new LinearConversion([[0, 1], [b, a+b]])).convert(x); // => ax + b
(new LinearConversion([[1/a, -b/a], [b+1, 0]])).convert(x); // => ax + bFor an arbitrary f(x) = ax + b, any [[x1, x2], [f(x1), f(x2)]] is a valid preset.
More examples:
// degrees to radians
(new LinearConversion([[0, 180], [0, Math.PI]])).convert(240); // => 4 * Math.PI / 3
// f(x) = 3x
(new LinearConversion([[0, 1/3], [0, 1]])).convert(5); // => 15
// f(x) = -2x - 46
(new LinearConversion([[0, 1], [-46, -48]])).convert(-23); // => 0Coefficients
Creating presets from a given function is trivial; to find the function from a given preset, two methods are provided: getCoefficientA and getCoefficientB.
// f(x) = 2x + 1
var doublePlus1 = new LinearConversion([[0, 1], [1, 3]]);
doublePlus1.getCoefficientA(); // => 2
doublePlus1.getCoefficientB(); // => 1
// f(x) = ax + b
var timesAPlusB = new LinearConversion([[x1, x2], [f(x1), f(x2)]]);
timesAPlusB.getCoefficientA(); // => a
timesAPlusB.getCoefficientB(); // => bArbitrary precision
Arbitrary precision support is provided via linear-arbitrary-precision. See all available adapters.
var doublePlusPoint1 = new LinearConversion([[0, 0.1], [0.1, 0.3]]);
// without arbitrary precision adapters
doublePlusPoint1.getCoefficientA(); // => 1.9999999999999998
// with arbitrary precision adapters
doublePlusPoint1.getCoefficientA(); // => 2See 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.