subtract.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSubtract = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _matAlgo01xDSid = require("../../type/matrix/utils/matAlgo01xDSid.js");
  8. var _matAlgo03xDSf = require("../../type/matrix/utils/matAlgo03xDSf.js");
  9. var _matAlgo05xSfSf = require("../../type/matrix/utils/matAlgo05xSfSf.js");
  10. var _matAlgo10xSids = require("../../type/matrix/utils/matAlgo10xSids.js");
  11. var _matAlgo12xSfs = require("../../type/matrix/utils/matAlgo12xSfs.js");
  12. var _matrixAlgorithmSuite = require("../../type/matrix/utils/matrixAlgorithmSuite.js");
  13. var name = 'subtract';
  14. var dependencies = ['typed', 'matrix', 'equalScalar', 'addScalar', 'unaryMinus', 'DenseMatrix', 'concat'];
  15. var createSubtract = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  16. var typed = _ref.typed,
  17. matrix = _ref.matrix,
  18. equalScalar = _ref.equalScalar,
  19. addScalar = _ref.addScalar,
  20. unaryMinus = _ref.unaryMinus,
  21. DenseMatrix = _ref.DenseMatrix,
  22. concat = _ref.concat;
  23. // TODO: split function subtract in two: subtract and subtractScalar
  24. var matAlgo01xDSid = (0, _matAlgo01xDSid.createMatAlgo01xDSid)({
  25. typed: typed
  26. });
  27. var matAlgo03xDSf = (0, _matAlgo03xDSf.createMatAlgo03xDSf)({
  28. typed: typed
  29. });
  30. var matAlgo05xSfSf = (0, _matAlgo05xSfSf.createMatAlgo05xSfSf)({
  31. typed: typed,
  32. equalScalar: equalScalar
  33. });
  34. var matAlgo10xSids = (0, _matAlgo10xSids.createMatAlgo10xSids)({
  35. typed: typed,
  36. DenseMatrix: DenseMatrix
  37. });
  38. var matAlgo12xSfs = (0, _matAlgo12xSfs.createMatAlgo12xSfs)({
  39. typed: typed,
  40. DenseMatrix: DenseMatrix
  41. });
  42. var matrixAlgorithmSuite = (0, _matrixAlgorithmSuite.createMatrixAlgorithmSuite)({
  43. typed: typed,
  44. matrix: matrix,
  45. concat: concat
  46. });
  47. /**
  48. * Subtract two values, `x - y`.
  49. * For matrices, the function is evaluated element wise.
  50. *
  51. * Syntax:
  52. *
  53. * math.subtract(x, y)
  54. *
  55. * Examples:
  56. *
  57. * math.subtract(5.3, 2) // returns number 3.3
  58. *
  59. * const a = math.complex(2, 3)
  60. * const b = math.complex(4, 1)
  61. * math.subtract(a, b) // returns Complex -2 + 2i
  62. *
  63. * math.subtract([5, 7, 4], 4) // returns Array [1, 3, 0]
  64. *
  65. * const c = math.unit('2.1 km')
  66. * const d = math.unit('500m')
  67. * math.subtract(c, d) // returns Unit 1.6 km
  68. *
  69. * See also:
  70. *
  71. * add
  72. *
  73. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x
  74. * Initial value
  75. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y
  76. * Value to subtract from `x`
  77. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
  78. * Subtraction of `x` and `y`
  79. */
  80. return typed(name, {
  81. 'number, number': function numberNumber(x, y) {
  82. return x - y;
  83. },
  84. 'Complex, Complex': function ComplexComplex(x, y) {
  85. return x.sub(y);
  86. },
  87. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  88. return x.minus(y);
  89. },
  90. 'Fraction, Fraction': function FractionFraction(x, y) {
  91. return x.sub(y);
  92. },
  93. 'Unit, Unit': typed.referToSelf(function (self) {
  94. return function (x, y) {
  95. if (x.value === null) {
  96. throw new Error('Parameter x contains a unit with undefined value');
  97. }
  98. if (y.value === null) {
  99. throw new Error('Parameter y contains a unit with undefined value');
  100. }
  101. if (!x.equalBase(y)) {
  102. throw new Error('Units do not match');
  103. }
  104. var res = x.clone();
  105. res.value = typed.find(self, [res.valueType(), y.valueType()])(res.value, y.value);
  106. res.fixPrefix = false;
  107. return res;
  108. };
  109. })
  110. }, matrixAlgorithmSuite({
  111. SS: matAlgo05xSfSf,
  112. DS: matAlgo01xDSid,
  113. SD: matAlgo03xDSf,
  114. Ss: matAlgo12xSfs,
  115. sS: matAlgo10xSids
  116. }));
  117. });
  118. exports.createSubtract = createSubtract;