forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
132 lines (112 loc) · 3.85 KB
/
add.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { factory } from '../../utils/factory'
import { extend } from '../../utils/object'
import { createAlgorithm01 } from '../../type/matrix/utils/algorithm01'
import { createAlgorithm04 } from '../../type/matrix/utils/algorithm04'
import { createAlgorithm10 } from '../../type/matrix/utils/algorithm10'
import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
const name = 'add'
const dependencies = [
'typed',
'matrix',
'addScalar',
'equalScalar',
'DenseMatrix',
'SparseMatrix'
]
export const createAdd = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, addScalar, equalScalar, DenseMatrix, SparseMatrix }) => {
const algorithm01 = createAlgorithm01({ typed })
const algorithm04 = createAlgorithm04({ typed, equalScalar })
const algorithm10 = createAlgorithm10({ typed, DenseMatrix })
const algorithm13 = createAlgorithm13({ typed })
const algorithm14 = createAlgorithm14({ typed })
/**
* Add two or more values, `x + y`.
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.add(x, y)
* math.add(x, y, z, ...)
*
* Examples:
*
* math.add(2, 3) // returns number 5
* math.add(2, 3, 4) // returns number 9
*
* const a = math.complex(2, 3)
* const b = math.complex(-4, 1)
* math.add(a, b) // returns Complex -2 + 4i
*
* math.add([1, 2, 3], 4) // returns Array [5, 6, 7]
*
* const c = math.unit('5 cm')
* const d = math.unit('2.1 mm')
* math.add(c, d) // returns Unit 52.1 mm
*
* math.add("2.3", "4") // returns number 6.3
*
* See also:
*
* subtract, sum
*
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to add
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y`
*/
return typed(name, extend({
// we extend the signatures of addScalar with signatures dealing with matrices
'DenseMatrix, DenseMatrix': function (x, y) {
return algorithm13(x, y, addScalar)
},
'DenseMatrix, SparseMatrix': function (x, y) {
return algorithm01(x, y, addScalar, false)
},
'SparseMatrix, DenseMatrix': function (x, y) {
return algorithm01(y, x, addScalar, true)
},
'SparseMatrix, SparseMatrix': function (x, y) {
return algorithm04(x, y, addScalar)
},
'Array, Array': function (x, y) {
// use matrix implementation
return this(matrix(x), matrix(y)).valueOf()
},
'Array, Matrix': function (x, y) {
// use matrix implementation
return this(matrix(x), y)
},
'Matrix, Array': function (x, y) {
// use matrix implementation
return this(x, matrix(y))
},
'DenseMatrix, any': function (x, y) {
return algorithm14(x, y, addScalar, false)
},
'SparseMatrix, any': function (x, y) {
return algorithm10(x, y, addScalar, false)
},
'any, DenseMatrix': function (x, y) {
return algorithm14(y, x, addScalar, true)
},
'any, SparseMatrix': function (x, y) {
return algorithm10(y, x, addScalar, true)
},
'Array, any': function (x, y) {
// use matrix implementation
return algorithm14(matrix(x), y, addScalar, false).valueOf()
},
'any, Array': function (x, y) {
// use matrix implementation
return algorithm14(matrix(y), x, addScalar, true).valueOf()
},
'any, any': addScalar,
'any, any, ...any': function (x, y, rest) {
let result = this(x, y)
for (let i = 0; i < rest.length; i++) {
result = this(result, rest[i])
}
return result
}
}, addScalar.signatures))
})