UNPKG

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