185 lines
5.6 KiB
JavaScript
185 lines
5.6 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.createSubtract = void 0;
|
||
|
|
||
|
var _factory = require("../../utils/factory.js");
|
||
|
|
||
|
var _DimensionError = require("../../error/DimensionError.js");
|
||
|
|
||
|
var _algorithm = require("../../type/matrix/utils/algorithm01.js");
|
||
|
|
||
|
var _algorithm2 = require("../../type/matrix/utils/algorithm03.js");
|
||
|
|
||
|
var _algorithm3 = require("../../type/matrix/utils/algorithm05.js");
|
||
|
|
||
|
var _algorithm4 = require("../../type/matrix/utils/algorithm10.js");
|
||
|
|
||
|
var _algorithm5 = require("../../type/matrix/utils/algorithm13.js");
|
||
|
|
||
|
var _algorithm6 = require("../../type/matrix/utils/algorithm14.js");
|
||
|
|
||
|
var name = 'subtract';
|
||
|
var dependencies = ['typed', 'matrix', 'equalScalar', 'addScalar', 'unaryMinus', 'DenseMatrix'];
|
||
|
var createSubtract = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
|
||
|
var typed = _ref.typed,
|
||
|
matrix = _ref.matrix,
|
||
|
equalScalar = _ref.equalScalar,
|
||
|
addScalar = _ref.addScalar,
|
||
|
unaryMinus = _ref.unaryMinus,
|
||
|
DenseMatrix = _ref.DenseMatrix;
|
||
|
// TODO: split function subtract in two: subtract and subtractScalar
|
||
|
var algorithm01 = (0, _algorithm.createAlgorithm01)({
|
||
|
typed: typed
|
||
|
});
|
||
|
var algorithm03 = (0, _algorithm2.createAlgorithm03)({
|
||
|
typed: typed
|
||
|
});
|
||
|
var algorithm05 = (0, _algorithm3.createAlgorithm05)({
|
||
|
typed: typed,
|
||
|
equalScalar: equalScalar
|
||
|
});
|
||
|
var algorithm10 = (0, _algorithm4.createAlgorithm10)({
|
||
|
typed: typed,
|
||
|
DenseMatrix: DenseMatrix
|
||
|
});
|
||
|
var algorithm13 = (0, _algorithm5.createAlgorithm13)({
|
||
|
typed: typed
|
||
|
});
|
||
|
var algorithm14 = (0, _algorithm6.createAlgorithm14)({
|
||
|
typed: typed
|
||
|
});
|
||
|
/**
|
||
|
* Subtract two values, `x - y`.
|
||
|
* For matrices, the function is evaluated element wise.
|
||
|
*
|
||
|
* Syntax:
|
||
|
*
|
||
|
* math.subtract(x, y)
|
||
|
*
|
||
|
* Examples:
|
||
|
*
|
||
|
* math.subtract(5.3, 2) // returns number 3.3
|
||
|
*
|
||
|
* const a = math.complex(2, 3)
|
||
|
* const b = math.complex(4, 1)
|
||
|
* math.subtract(a, b) // returns Complex -2 + 2i
|
||
|
*
|
||
|
* math.subtract([5, 7, 4], 4) // returns Array [1, 3, 0]
|
||
|
*
|
||
|
* const c = math.unit('2.1 km')
|
||
|
* const d = math.unit('500m')
|
||
|
* math.subtract(c, d) // returns Unit 1.6 km
|
||
|
*
|
||
|
* See also:
|
||
|
*
|
||
|
* add
|
||
|
*
|
||
|
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x
|
||
|
* Initial value
|
||
|
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y
|
||
|
* Value to subtract from `x`
|
||
|
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
|
||
|
* Subtraction of `x` and `y`
|
||
|
*/
|
||
|
|
||
|
return typed(name, {
|
||
|
'number, number': function numberNumber(x, y) {
|
||
|
return x - y;
|
||
|
},
|
||
|
'Complex, Complex': function ComplexComplex(x, y) {
|
||
|
return x.sub(y);
|
||
|
},
|
||
|
'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
|
||
|
return x.minus(y);
|
||
|
},
|
||
|
'Fraction, Fraction': function FractionFraction(x, y) {
|
||
|
return x.sub(y);
|
||
|
},
|
||
|
'Unit, Unit': function UnitUnit(x, y) {
|
||
|
if (x.value === null) {
|
||
|
throw new Error('Parameter x contains a unit with undefined value');
|
||
|
}
|
||
|
|
||
|
if (y.value === null) {
|
||
|
throw new Error('Parameter y contains a unit with undefined value');
|
||
|
}
|
||
|
|
||
|
if (!x.equalBase(y)) {
|
||
|
throw new Error('Units do not match');
|
||
|
}
|
||
|
|
||
|
var res = x.clone();
|
||
|
res.value = this(res.value, y.value);
|
||
|
res.fixPrefix = false;
|
||
|
return res;
|
||
|
},
|
||
|
'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
|
||
|
checkEqualDimensions(x, y);
|
||
|
return algorithm05(x, y, this);
|
||
|
},
|
||
|
'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
|
||
|
checkEqualDimensions(x, y);
|
||
|
return algorithm03(y, x, this, true);
|
||
|
},
|
||
|
'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
|
||
|
checkEqualDimensions(x, y);
|
||
|
return algorithm01(x, y, this, false);
|
||
|
},
|
||
|
'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
|
||
|
checkEqualDimensions(x, y);
|
||
|
return algorithm13(x, y, this);
|
||
|
},
|
||
|
'Array, Array': function ArrayArray(x, y) {
|
||
|
// use matrix implementation
|
||
|
return this(matrix(x), matrix(y)).valueOf();
|
||
|
},
|
||
|
'Array, Matrix': function ArrayMatrix(x, y) {
|
||
|
// use matrix implementation
|
||
|
return this(matrix(x), y);
|
||
|
},
|
||
|
'Matrix, Array': function MatrixArray(x, y) {
|
||
|
// use matrix implementation
|
||
|
return this(x, matrix(y));
|
||
|
},
|
||
|
'SparseMatrix, any': function SparseMatrixAny(x, y) {
|
||
|
return algorithm10(x, unaryMinus(y), addScalar);
|
||
|
},
|
||
|
'DenseMatrix, any': function DenseMatrixAny(x, y) {
|
||
|
return algorithm14(x, y, this);
|
||
|
},
|
||
|
'any, SparseMatrix': function anySparseMatrix(x, y) {
|
||
|
return algorithm10(y, x, this, true);
|
||
|
},
|
||
|
'any, DenseMatrix': function anyDenseMatrix(x, y) {
|
||
|
return algorithm14(y, x, this, true);
|
||
|
},
|
||
|
'Array, any': function ArrayAny(x, y) {
|
||
|
// use matrix implementation
|
||
|
return algorithm14(matrix(x), y, this, false).valueOf();
|
||
|
},
|
||
|
'any, Array': function anyArray(x, y) {
|
||
|
// use matrix implementation
|
||
|
return algorithm14(matrix(y), x, this, true).valueOf();
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
/**
|
||
|
* Check whether matrix x and y have the same number of dimensions.
|
||
|
* Throws a DimensionError when dimensions are not equal
|
||
|
* @param {Matrix} x
|
||
|
* @param {Matrix} y
|
||
|
*/
|
||
|
|
||
|
exports.createSubtract = createSubtract;
|
||
|
|
||
|
function checkEqualDimensions(x, y) {
|
||
|
var xsize = x.size();
|
||
|
var ysize = y.size();
|
||
|
|
||
|
if (xsize.length !== ysize.length) {
|
||
|
throw new _DimensionError.DimensionError(xsize.length, ysize.length);
|
||
|
}
|
||
|
}
|