UNPKG

3.64 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { createAlgorithm02 } from '../../type/matrix/utils/algorithm02'
3import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03'
4import { createAlgorithm07 } from '../../type/matrix/utils/algorithm07'
5import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11'
6import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12'
7import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
8import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
9
10const name = 'dotDivide'
11const dependencies = [
12 'typed',
13 'matrix',
14 'equalScalar',
15 'divideScalar',
16 'DenseMatrix'
17]
18
19export const createDotDivide = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, divideScalar, DenseMatrix }) => {
20 const algorithm02 = createAlgorithm02({ typed, equalScalar })
21 const algorithm03 = createAlgorithm03({ typed })
22 const algorithm07 = createAlgorithm07({ typed, DenseMatrix })
23 const algorithm11 = createAlgorithm11({ typed, equalScalar })
24 const algorithm12 = createAlgorithm12({ typed, DenseMatrix })
25 const algorithm13 = createAlgorithm13({ typed })
26 const algorithm14 = createAlgorithm14({ typed })
27
28 /**
29 * Divide two matrices element wise. The function accepts both matrices and
30 * scalar values.
31 *
32 * Syntax:
33 *
34 * math.dotDivide(x, y)
35 *
36 * Examples:
37 *
38 * math.dotDivide(2, 4) // returns 0.5
39 *
40 * a = [[9, 5], [6, 1]]
41 * b = [[3, 2], [5, 2]]
42 *
43 * math.dotDivide(a, b) // returns [[3, 2.5], [1.2, 0.5]]
44 * math.divide(a, b) // returns [[1.75, 0.75], [-1.75, 2.25]]
45 *
46 * See also:
47 *
48 * divide, multiply, dotMultiply
49 *
50 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Numerator
51 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Denominator
52 * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x ./ y`
53 */
54 const dotDivide = typed(name, {
55
56 'any, any': divideScalar,
57
58 'SparseMatrix, SparseMatrix': function (x, y) {
59 return algorithm07(x, y, divideScalar, false)
60 },
61
62 'SparseMatrix, DenseMatrix': function (x, y) {
63 return algorithm02(y, x, divideScalar, true)
64 },
65
66 'DenseMatrix, SparseMatrix': function (x, y) {
67 return algorithm03(x, y, divideScalar, false)
68 },
69
70 'DenseMatrix, DenseMatrix': function (x, y) {
71 return algorithm13(x, y, divideScalar)
72 },
73
74 'Array, Array': function (x, y) {
75 // use matrix implementation
76 return dotDivide(matrix(x), matrix(y)).valueOf()
77 },
78
79 'Array, Matrix': function (x, y) {
80 // use matrix implementation
81 return dotDivide(matrix(x), y)
82 },
83
84 'Matrix, Array': function (x, y) {
85 // use matrix implementation
86 return dotDivide(x, matrix(y))
87 },
88
89 'SparseMatrix, any': function (x, y) {
90 return algorithm11(x, y, divideScalar, false)
91 },
92
93 'DenseMatrix, any': function (x, y) {
94 return algorithm14(x, y, divideScalar, false)
95 },
96
97 'any, SparseMatrix': function (x, y) {
98 return algorithm12(y, x, divideScalar, true)
99 },
100
101 'any, DenseMatrix': function (x, y) {
102 return algorithm14(y, x, divideScalar, true)
103 },
104
105 'Array, any': function (x, y) {
106 // use matrix implementation
107 return algorithm14(matrix(x), y, divideScalar, false).valueOf()
108 },
109
110 'any, Array': function (x, y) {
111 // use matrix implementation
112 return algorithm14(matrix(y), x, divideScalar, true).valueOf()
113 }
114 })
115
116 return dotDivide
117})