divide.js 2.7 KB

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