1 | import { factory } from '../../utils/factory'
|
2 | import { extend } from '../../utils/object'
|
3 | import { createAlgorithm01 } from '../../type/matrix/utils/algorithm01'
|
4 | import { createAlgorithm04 } from '../../type/matrix/utils/algorithm04'
|
5 | import { createAlgorithm10 } from '../../type/matrix/utils/algorithm10'
|
6 | import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
|
7 | import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
8 |
|
9 | const name = 'add'
|
10 | const dependencies = [
|
11 | 'typed',
|
12 | 'matrix',
|
13 | 'addScalar',
|
14 | 'equalScalar',
|
15 | 'DenseMatrix',
|
16 | 'SparseMatrix'
|
17 | ]
|
18 |
|
19 | export const createAdd = 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 |
|
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 | const add = typed(name, extend({
|
61 |
|
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 |
|
81 | return add(matrix(x), matrix(y)).valueOf()
|
82 | },
|
83 |
|
84 | 'Array, Matrix': function (x, y) {
|
85 |
|
86 | return add(matrix(x), y)
|
87 | },
|
88 |
|
89 | 'Matrix, Array': function (x, y) {
|
90 |
|
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 |
|
112 | return algorithm14(matrix(x), y, addScalar, false).valueOf()
|
113 | },
|
114 |
|
115 | 'any, Array': function (x, y) {
|
116 |
|
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 | })
|