UNPKG

5.3 kBJavaScriptView Raw
1import { factory } from '../../utils/factory.js';
2import { DimensionError } from '../../error/DimensionError.js';
3import { createAlgorithm01 } from '../../type/matrix/utils/algorithm01.js';
4import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03.js';
5import { createAlgorithm05 } from '../../type/matrix/utils/algorithm05.js';
6import { createAlgorithm10 } from '../../type/matrix/utils/algorithm10.js';
7import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js';
8import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
9var name = 'subtract';
10var dependencies = ['typed', 'matrix', 'equalScalar', 'addScalar', 'unaryMinus', 'DenseMatrix'];
11export var createSubtract = /* #__PURE__ */factory(name, dependencies, (_ref) => {
12 var {
13 typed,
14 matrix,
15 equalScalar,
16 addScalar,
17 unaryMinus,
18 DenseMatrix
19 } = _ref;
20 // TODO: split function subtract in two: subtract and subtractScalar
21 var algorithm01 = createAlgorithm01({
22 typed
23 });
24 var algorithm03 = createAlgorithm03({
25 typed
26 });
27 var algorithm05 = createAlgorithm05({
28 typed,
29 equalScalar
30 });
31 var algorithm10 = createAlgorithm10({
32 typed,
33 DenseMatrix
34 });
35 var algorithm13 = createAlgorithm13({
36 typed
37 });
38 var algorithm14 = createAlgorithm14({
39 typed
40 });
41 /**
42 * Subtract two values, `x - y`.
43 * For matrices, the function is evaluated element wise.
44 *
45 * Syntax:
46 *
47 * math.subtract(x, y)
48 *
49 * Examples:
50 *
51 * math.subtract(5.3, 2) // returns number 3.3
52 *
53 * const a = math.complex(2, 3)
54 * const b = math.complex(4, 1)
55 * math.subtract(a, b) // returns Complex -2 + 2i
56 *
57 * math.subtract([5, 7, 4], 4) // returns Array [1, 3, 0]
58 *
59 * const c = math.unit('2.1 km')
60 * const d = math.unit('500m')
61 * math.subtract(c, d) // returns Unit 1.6 km
62 *
63 * See also:
64 *
65 * add
66 *
67 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x
68 * Initial value
69 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y
70 * Value to subtract from `x`
71 * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
72 * Subtraction of `x` and `y`
73 */
74
75 return typed(name, {
76 'number, number': function numberNumber(x, y) {
77 return x - y;
78 },
79 'Complex, Complex': function ComplexComplex(x, y) {
80 return x.sub(y);
81 },
82 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
83 return x.minus(y);
84 },
85 'Fraction, Fraction': function FractionFraction(x, y) {
86 return x.sub(y);
87 },
88 'Unit, Unit': function UnitUnit(x, y) {
89 if (x.value === null) {
90 throw new Error('Parameter x contains a unit with undefined value');
91 }
92
93 if (y.value === null) {
94 throw new Error('Parameter y contains a unit with undefined value');
95 }
96
97 if (!x.equalBase(y)) {
98 throw new Error('Units do not match');
99 }
100
101 var res = x.clone();
102 res.value = this(res.value, y.value);
103 res.fixPrefix = false;
104 return res;
105 },
106 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
107 checkEqualDimensions(x, y);
108 return algorithm05(x, y, this);
109 },
110 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
111 checkEqualDimensions(x, y);
112 return algorithm03(y, x, this, true);
113 },
114 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
115 checkEqualDimensions(x, y);
116 return algorithm01(x, y, this, false);
117 },
118 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
119 checkEqualDimensions(x, y);
120 return algorithm13(x, y, this);
121 },
122 'Array, Array': function ArrayArray(x, y) {
123 // use matrix implementation
124 return this(matrix(x), matrix(y)).valueOf();
125 },
126 'Array, Matrix': function ArrayMatrix(x, y) {
127 // use matrix implementation
128 return this(matrix(x), y);
129 },
130 'Matrix, Array': function MatrixArray(x, y) {
131 // use matrix implementation
132 return this(x, matrix(y));
133 },
134 'SparseMatrix, any': function SparseMatrixAny(x, y) {
135 return algorithm10(x, unaryMinus(y), addScalar);
136 },
137 'DenseMatrix, any': function DenseMatrixAny(x, y) {
138 return algorithm14(x, y, this);
139 },
140 'any, SparseMatrix': function anySparseMatrix(x, y) {
141 return algorithm10(y, x, this, true);
142 },
143 'any, DenseMatrix': function anyDenseMatrix(x, y) {
144 return algorithm14(y, x, this, true);
145 },
146 'Array, any': function ArrayAny(x, y) {
147 // use matrix implementation
148 return algorithm14(matrix(x), y, this, false).valueOf();
149 },
150 'any, Array': function anyArray(x, y) {
151 // use matrix implementation
152 return algorithm14(matrix(y), x, this, true).valueOf();
153 }
154 });
155});
156/**
157 * Check whether matrix x and y have the same number of dimensions.
158 * Throws a DimensionError when dimensions are not equal
159 * @param {Matrix} x
160 * @param {Matrix} y
161 */
162
163function checkEqualDimensions(x, y) {
164 var xsize = x.size();
165 var ysize = y.size();
166
167 if (xsize.length !== ysize.length) {
168 throw new DimensionError(xsize.length, ysize.length);
169 }
170}
\No newline at end of file