simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

to.js (2492B)


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