to.js (2691B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createTo = void 0; 7 8 var _factory = require("../../utils/factory.js"); 9 10 var _algorithm = require("../../type/matrix/utils/algorithm13.js"); 11 12 var _algorithm2 = require("../../type/matrix/utils/algorithm14.js"); 13 14 var name = 'to'; 15 var dependencies = ['typed', 'matrix']; 16 var createTo = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 17 var typed = _ref.typed, 18 matrix = _ref.matrix; 19 var algorithm13 = (0, _algorithm.createAlgorithm13)({ 20 typed: typed 21 }); 22 var algorithm14 = (0, _algorithm2.createAlgorithm14)({ 23 typed: typed 24 }); 25 /** 26 * Change the unit of a value. 27 * 28 * For matrices, the function is evaluated element wise. 29 * 30 * Syntax: 31 * 32 * math.to(x, unit) 33 * 34 * Examples: 35 * 36 * math.to(math.unit('2 inch'), 'cm') // returns Unit 5.08 cm 37 * math.to(math.unit('2 inch'), math.unit(null, 'cm')) // returns Unit 5.08 cm 38 * math.to(math.unit(16, 'bytes'), 'bits') // returns Unit 128 bits 39 * 40 * See also: 41 * 42 * unit 43 * 44 * @param {Unit | Array | Matrix} x The unit to be converted. 45 * @param {Unit | Array | Matrix} unit New unit. Can be a string like "cm" 46 * or a unit without value. 47 * @return {Unit | Array | Matrix} value with changed, fixed unit. 48 */ 49 50 return typed(name, { 51 'Unit, Unit | string': function UnitUnitString(x, unit) { 52 return x.to(unit); 53 }, 54 'Matrix, Matrix': function MatrixMatrix(x, y) { 55 // SparseMatrix does not support Units 56 return algorithm13(x, y, this); 57 }, 58 'Array, Array': function ArrayArray(x, y) { 59 // use matrix implementation 60 return this(matrix(x), matrix(y)).valueOf(); 61 }, 62 'Array, Matrix': function ArrayMatrix(x, y) { 63 // use matrix implementation 64 return this(matrix(x), y); 65 }, 66 'Matrix, Array': function MatrixArray(x, y) { 67 // use matrix implementation 68 return this(x, matrix(y)); 69 }, 70 'Matrix, any': function MatrixAny(x, y) { 71 // SparseMatrix does not support Units 72 return algorithm14(x, y, this, false); 73 }, 74 'any, Matrix': function anyMatrix(x, y) { 75 // SparseMatrix does not support Units 76 return algorithm14(y, x, this, true); 77 }, 78 'Array, any': function ArrayAny(x, y) { 79 // use matrix implementation 80 return algorithm14(matrix(x), y, this, false).valueOf(); 81 }, 82 'any, Array': function anyArray(x, y) { 83 // use matrix implementation 84 return algorithm14(matrix(y), x, this, true).valueOf(); 85 } 86 }); 87 }); 88 exports.createTo = createTo;