UNPKG

3.95 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { extend } from '../../utils/object'
3import { createAlgorithm01 } from '../../type/matrix/utils/algorithm01'
4import { createAlgorithm04 } from '../../type/matrix/utils/algorithm04'
5import { createAlgorithm10 } from '../../type/matrix/utils/algorithm10'
6import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
7import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
8
9const name = 'add'
10const dependencies = [
11 'typed',
12 'matrix',
13 'addScalar',
14 'equalScalar',
15 'DenseMatrix',
16 'SparseMatrix'
17]
18
19export const createAdd = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, addScalar, equalScalar, DenseMatrix, SparseMatrix }) => {
20 const algorithm01 = createAlgorithm01({ typed })
21 const algorithm04 = createAlgorithm04({ typed, equalScalar })
22 const algorithm10 = createAlgorithm10({ typed, DenseMatrix })
23 const algorithm13 = createAlgorithm13({ typed })
24 const algorithm14 = createAlgorithm14({ typed })
25
26 /**
27 * Add two or more values, `x + y`.
28 * For matrices, the function is evaluated element wise.
29 *
30 * Syntax:
31 *
32 * math.add(x, y)
33 * math.add(x, y, z, ...)
34 *
35 * Examples:
36 *
37 * math.add(2, 3) // returns number 5
38 * math.add(2, 3, 4) // returns number 9
39 *
40 * const a = math.complex(2, 3)
41 * const b = math.complex(-4, 1)
42 * math.add(a, b) // returns Complex -2 + 4i
43 *
44 * math.add([1, 2, 3], 4) // returns Array [5, 6, 7]
45 *
46 * const c = math.unit('5 cm')
47 * const d = math.unit('2.1 mm')
48 * math.add(c, d) // returns Unit 52.1 mm
49 *
50 * math.add("2.3", "4") // returns number 6.3
51 *
52 * See also:
53 *
54 * subtract, sum
55 *
56 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to add
57 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add
58 * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y`
59 */
60 const add = typed(name, extend({
61 // we extend the signatures of addScalar with signatures dealing with matrices
62
63 'DenseMatrix, DenseMatrix': function (x, y) {
64 return algorithm13(x, y, addScalar)
65 },
66
67 'DenseMatrix, SparseMatrix': function (x, y) {
68 return algorithm01(x, y, addScalar, false)
69 },
70
71 'SparseMatrix, DenseMatrix': function (x, y) {
72 return algorithm01(y, x, addScalar, true)
73 },
74
75 'SparseMatrix, SparseMatrix': function (x, y) {
76 return algorithm04(x, y, addScalar)
77 },
78
79 'Array, Array': function (x, y) {
80 // use matrix implementation
81 return add(matrix(x), matrix(y)).valueOf()
82 },
83
84 'Array, Matrix': function (x, y) {
85 // use matrix implementation
86 return add(matrix(x), y)
87 },
88
89 'Matrix, Array': function (x, y) {
90 // use matrix implementation
91 return add(x, matrix(y))
92 },
93
94 'DenseMatrix, any': function (x, y) {
95 return algorithm14(x, y, addScalar, false)
96 },
97
98 'SparseMatrix, any': function (x, y) {
99 return algorithm10(x, y, addScalar, false)
100 },
101
102 'any, DenseMatrix': function (x, y) {
103 return algorithm14(y, x, addScalar, true)
104 },
105
106 'any, SparseMatrix': function (x, y) {
107 return algorithm10(y, x, addScalar, true)
108 },
109
110 'Array, any': function (x, y) {
111 // use matrix implementation
112 return algorithm14(matrix(x), y, addScalar, false).valueOf()
113 },
114
115 'any, Array': function (x, y) {
116 // use matrix implementation
117 return algorithm14(matrix(y), x, addScalar, true).valueOf()
118 },
119
120 'any, any': addScalar,
121
122 'any, any, ...any': function (x, y, rest) {
123 let result = add(x, y)
124
125 for (let i = 0; i < rest.length; i++) {
126 result = add(result, rest[i])
127 }
128
129 return result
130 }
131 }, addScalar.signatures))
132
133 return add
134})