UNPKG

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