divide.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createDivide = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _object = require("../../utils/object.js");
  8. var _matAlgo11xS0s = require("../../type/matrix/utils/matAlgo11xS0s.js");
  9. var _matAlgo14xDs = require("../../type/matrix/utils/matAlgo14xDs.js");
  10. var name = 'divide';
  11. var dependencies = ['typed', 'matrix', 'multiply', 'equalScalar', 'divideScalar', 'inv'];
  12. var createDivide = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  13. var typed = _ref.typed,
  14. matrix = _ref.matrix,
  15. multiply = _ref.multiply,
  16. equalScalar = _ref.equalScalar,
  17. divideScalar = _ref.divideScalar,
  18. inv = _ref.inv;
  19. var matAlgo11xS0s = (0, _matAlgo11xS0s.createMatAlgo11xS0s)({
  20. typed: typed,
  21. equalScalar: equalScalar
  22. });
  23. var matAlgo14xDs = (0, _matAlgo14xDs.createMatAlgo14xDs)({
  24. typed: typed
  25. });
  26. /**
  27. * Divide two values, `x / y`.
  28. * To divide matrices, `x` is multiplied with the inverse of `y`: `x * inv(y)`.
  29. *
  30. * Syntax:
  31. *
  32. * math.divide(x, y)
  33. *
  34. * Examples:
  35. *
  36. * math.divide(2, 3) // returns number 0.6666666666666666
  37. *
  38. * const a = math.complex(5, 14)
  39. * const b = math.complex(4, 1)
  40. * math.divide(a, b) // returns Complex 2 + 3i
  41. *
  42. * const c = [[7, -6], [13, -4]]
  43. * const d = [[1, 2], [4, 3]]
  44. * math.divide(c, d) // returns Array [[-9, 4], [-11, 6]]
  45. *
  46. * const e = math.unit('18 km')
  47. * math.divide(e, 4.5) // returns Unit 4 km
  48. *
  49. * See also:
  50. *
  51. * multiply
  52. *
  53. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Numerator
  54. * @param {number | BigNumber | Fraction | Complex | Array | Matrix} y Denominator
  55. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x / y`
  56. */
  57. return typed('divide', (0, _object.extend)({
  58. // we extend the signatures of divideScalar with signatures dealing with matrices
  59. 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(x, y) {
  60. // TODO: implement matrix right division using pseudo inverse
  61. // https://www.mathworks.nl/help/matlab/ref/mrdivide.html
  62. // https://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html
  63. // https://stackoverflow.com/questions/12263932/how-does-gnu-octave-matrix-division-work-getting-unexpected-behaviour
  64. return multiply(x, inv(y));
  65. },
  66. 'DenseMatrix, any': function DenseMatrixAny(x, y) {
  67. return matAlgo14xDs(x, y, divideScalar, false);
  68. },
  69. 'SparseMatrix, any': function SparseMatrixAny(x, y) {
  70. return matAlgo11xS0s(x, y, divideScalar, false);
  71. },
  72. 'Array, any': function ArrayAny(x, y) {
  73. // use matrix implementation
  74. return matAlgo14xDs(matrix(x), y, divideScalar, false).valueOf();
  75. },
  76. 'any, Array | Matrix': function anyArrayMatrix(x, y) {
  77. return multiply(x, inv(y));
  78. }
  79. }, divideScalar.signatures));
  80. });
  81. exports.createDivide = createDivide;