UNPKG

4.81 kBJavaScriptView Raw
1'use strict'
2
3const DimensionError = require('../../error/DimensionError')
4
5function 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 // TODO: split function subtract in two: subtract and subtractScalar
20
21 /**
22 * Subtract two values, `x - y`.
23 * For matrices, the function is evaluated element wise.
24 *
25 * Syntax:
26 *
27 * math.subtract(x, y)
28 *
29 * Examples:
30 *
31 * math.subtract(5.3, 2) // returns number 3.3
32 *
33 * const a = math.complex(2, 3)
34 * const b = math.complex(4, 1)
35 * math.subtract(a, b) // returns Complex -2 + 2i
36 *
37 * math.subtract([5, 7, 4], 4) // returns Array [1, 3, 0]
38 *
39 * const c = math.unit('2.1 km')
40 * const d = math.unit('500m')
41 * math.subtract(c, d) // returns Unit 1.6 km
42 *
43 * See also:
44 *
45 * add
46 *
47 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x
48 * Initial value
49 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y
50 * Value to subtract from `x`
51 * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
52 * Subtraction of `x` and `y`
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 // use matrix implementation
114 return subtract(matrix(x), matrix(y)).valueOf()
115 },
116
117 'Array, Matrix': function (x, y) {
118 // use matrix implementation
119 return subtract(matrix(x), y)
120 },
121
122 'Matrix, Array': function (x, y) {
123 // use matrix implementation
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 // use matrix implementation
145 return algorithm14(matrix(x), y, subtract, false).valueOf()
146 },
147
148 'any, Array': function (x, y) {
149 // use matrix implementation
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 * Check whether matrix x and y have the same number of dimensions.
163 * Throws a DimensionError when dimensions are not equal
164 * @param {Matrix} x
165 * @param {Matrix} y
166 */
167function 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
176exports.name = 'subtract'
177exports.factory = factory