UNPKG

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