forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto.js
More file actions
90 lines (74 loc) · 2.32 KB
/
to.js
File metadata and controls
90 lines (74 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict'
function factory (type, config, load, typed) {
const latex = require('../../utils/latex')
const matrix = load(require('../../type/matrix/function/matrix'))
const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
/**
* Change the unit of a value.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.to(x, unit)
*
* Examples:
*
* math.to(math.unit('2 inch'), 'cm') // returns Unit 5.08 cm
* math.to(math.unit('2 inch'), math.unit(null, 'cm')) // returns Unit 5.08 cm
* math.to(math.unit(16, 'bytes'), 'bits') // returns Unit 128 bits
*
* See also:
*
* unit
*
* @param {Unit | Array | Matrix} x The unit to be converted.
* @param {Unit | Array | Matrix} unit New unit. Can be a string like "cm"
* or a unit without value.
* @return {Unit | Array | Matrix} value with changed, fixed unit.
*/
const to = typed('to', {
'Unit, Unit | string': function (x, unit) {
return x.to(unit)
},
'Matrix, Matrix': function (x, y) {
// SparseMatrix does not support Units
return algorithm13(x, y, to)
},
'Array, Array': function (x, y) {
// use matrix implementation
return to(matrix(x), matrix(y)).valueOf()
},
'Array, Matrix': function (x, y) {
// use matrix implementation
return to(matrix(x), y)
},
'Matrix, Array': function (x, y) {
// use matrix implementation
return to(x, matrix(y))
},
'Matrix, any': function (x, y) {
// SparseMatrix does not support Units
return algorithm14(x, y, to, false)
},
'any, Matrix': function (x, y) {
// SparseMatrix does not support Units
return algorithm14(y, x, to, true)
},
'Array, any': function (x, y) {
// use matrix implementation
return algorithm14(matrix(x), y, to, false).valueOf()
},
'any, Array': function (x, y) {
// use matrix implementation
return algorithm14(matrix(y), x, to, true).valueOf()
}
})
to.toTex = {
2: `\\left(\${args[0]}${latex.operators['to']}\${args[1]}\\right)`
}
return to
}
exports.name = 'to'
exports.factory = factory