1 | 'use strict'
|
2 |
|
3 | const DimensionError = require('../../error/DimensionError')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | const latex = require('../../utils/latex')
|
7 |
|
8 | const matrix = load(require('../../type/matrix/function/matrix'))
|
9 | const addScalar = load(require('./addScalar'))
|
10 | const unaryMinus = load(require('./unaryMinus'))
|
11 |
|
12 | const algorithm01 = load(require('../../type/matrix/utils/algorithm01'))
|
13 | const algorithm03 = load(require('../../type/matrix/utils/algorithm03'))
|
14 | const algorithm05 = load(require('../../type/matrix/utils/algorithm05'))
|
15 | const algorithm10 = load(require('../../type/matrix/utils/algorithm10'))
|
16 | const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
|
17 | const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
|
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 | const subtract = typed('subtract', {
|
55 |
|
56 | 'number, number': function (x, y) {
|
57 | return x - y
|
58 | },
|
59 |
|
60 | 'Complex, Complex': function (x, y) {
|
61 | return x.sub(y)
|
62 | },
|
63 |
|
64 | 'BigNumber, BigNumber': function (x, y) {
|
65 | return x.minus(y)
|
66 | },
|
67 |
|
68 | 'Fraction, Fraction': function (x, y) {
|
69 | return x.sub(y)
|
70 | },
|
71 |
|
72 | 'Unit, Unit': function (x, y) {
|
73 | if (x.value === null) {
|
74 | throw new Error('Parameter x contains a unit with undefined value')
|
75 | }
|
76 |
|
77 | if (y.value === null) {
|
78 | throw new Error('Parameter y contains a unit with undefined value')
|
79 | }
|
80 |
|
81 | if (!x.equalBase(y)) {
|
82 | throw new Error('Units do not match')
|
83 | }
|
84 |
|
85 | const res = x.clone()
|
86 | res.value = subtract(res.value, y.value)
|
87 | res.fixPrefix = false
|
88 |
|
89 | return res
|
90 | },
|
91 |
|
92 | 'SparseMatrix, SparseMatrix': function (x, y) {
|
93 | checkEqualDimensions(x, y)
|
94 | return algorithm05(x, y, subtract)
|
95 | },
|
96 |
|
97 | 'SparseMatrix, DenseMatrix': function (x, y) {
|
98 | checkEqualDimensions(x, y)
|
99 | return algorithm03(y, x, subtract, true)
|
100 | },
|
101 |
|
102 | 'DenseMatrix, SparseMatrix': function (x, y) {
|
103 | checkEqualDimensions(x, y)
|
104 | return algorithm01(x, y, subtract, false)
|
105 | },
|
106 |
|
107 | 'DenseMatrix, DenseMatrix': function (x, y) {
|
108 | checkEqualDimensions(x, y)
|
109 | return algorithm13(x, y, subtract)
|
110 | },
|
111 |
|
112 | 'Array, Array': function (x, y) {
|
113 |
|
114 | return subtract(matrix(x), matrix(y)).valueOf()
|
115 | },
|
116 |
|
117 | 'Array, Matrix': function (x, y) {
|
118 |
|
119 | return subtract(matrix(x), y)
|
120 | },
|
121 |
|
122 | 'Matrix, Array': function (x, y) {
|
123 |
|
124 | return subtract(x, matrix(y))
|
125 | },
|
126 |
|
127 | 'SparseMatrix, any': function (x, y) {
|
128 | return algorithm10(x, unaryMinus(y), addScalar)
|
129 | },
|
130 |
|
131 | 'DenseMatrix, any': function (x, y) {
|
132 | return algorithm14(x, y, subtract)
|
133 | },
|
134 |
|
135 | 'any, SparseMatrix': function (x, y) {
|
136 | return algorithm10(y, x, subtract, true)
|
137 | },
|
138 |
|
139 | 'any, DenseMatrix': function (x, y) {
|
140 | return algorithm14(y, x, subtract, true)
|
141 | },
|
142 |
|
143 | 'Array, any': function (x, y) {
|
144 |
|
145 | return algorithm14(matrix(x), y, subtract, false).valueOf()
|
146 | },
|
147 |
|
148 | 'any, Array': function (x, y) {
|
149 |
|
150 | return algorithm14(matrix(y), x, subtract, true).valueOf()
|
151 | }
|
152 | })
|
153 |
|
154 | subtract.toTex = {
|
155 | 2: `\\left(\${args[0]}${latex.operators['subtract']}\${args[1]}\\right)`
|
156 | }
|
157 |
|
158 | return subtract
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | function checkEqualDimensions (x, y) {
|
168 | const xsize = x.size()
|
169 | const ysize = y.size()
|
170 |
|
171 | if (xsize.length !== ysize.length) {
|
172 | throw new DimensionError(xsize.length, ysize.length)
|
173 | }
|
174 | }
|
175 |
|
176 | exports.name = 'subtract'
|
177 | exports.factory = factory
|