UNPKG

2.49 kBJavaScriptView Raw
1'use strict'
2
3const size = require('../../utils/array').size
4
5function factory (type, config, load, typed) {
6 const matrix = load(require('../../type/matrix/function/matrix'))
7 const multiplyScalar = load(require('../arithmetic/multiplyScalar'))
8 /**
9 * Calculates the kronecker product of 2 matrices or vectors.
10 *
11 * NOTE: If a one dimensional vector / matrix is given, it will be
12 * wrapped so its two dimensions.
13 * See the examples.
14 *
15 * Syntax:
16 *
17 * math.kron(x, y)
18 *
19 * Examples:
20 *
21 * math.kron([[1, 0], [0, 1]], [[1, 2], [3, 4]])
22 * // returns [ [ 1, 2, 0, 0 ], [ 3, 4, 0, 0 ], [ 0, 0, 1, 2 ], [ 0, 0, 3, 4 ] ]
23 *
24 * math.kron([1,1], [2,3,4])
25 * // returns [ [ 2, 3, 4, 2, 3, 4 ] ]
26 *
27 * See also:
28 *
29 * multiply, dot, cross
30 *
31 * @param {Array | Matrix} x First vector
32 * @param {Array | Matrix} y Second vector
33 * @return {Array | Matrix} Returns the kronecker product of `x` and `y`
34 */
35 const kron = typed('kron', {
36 'Matrix, Matrix': function (x, y) {
37 return matrix(_kron(x.toArray(), y.toArray()))
38 },
39
40 'Matrix, Array': function (x, y) {
41 return matrix(_kron(x.toArray(), y))
42 },
43
44 'Array, Matrix': function (x, y) {
45 return matrix(_kron(x, y.toArray()))
46 },
47
48 'Array, Array': _kron
49 })
50
51 return kron
52
53 /**
54 * Calculate the kronecker product of two matrices / vectors
55 * @param {Array} a First vector
56 * @param {Array} b Second vector
57 * @returns {Array} Returns the kronecker product of x and y
58 * @private
59 */
60 function _kron (a, b) {
61 // Deal with the dimensions of the matricies.
62 if (size(a).length === 1) {
63 // Wrap it in a 2D Matrix
64 a = [a]
65 }
66 if (size(b).length === 1) {
67 // Wrap it in a 2D Matrix
68 b = [b]
69 }
70 if (size(a).length > 2 || size(b).length > 2) {
71 throw new RangeError('Vectors with dimensions greater then 2 are not supported expected ' +
72 '(Size x = ' + JSON.stringify(a.length) + ', y = ' + JSON.stringify(b.length) + ')')
73 }
74 let t = []
75 let r = []
76
77 return a.map(function (a) {
78 return b.map(function (b) {
79 r = []
80 t.push(r)
81 return a.map(function (y) {
82 return b.map(function (x) {
83 return r.push(multiplyScalar(y, x))
84 })
85 })
86 })
87 }) && t
88 }
89}
90
91exports.name = 'kron'
92exports.factory = factory