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