kron.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createKron = void 0;
  6. var _array = require("../../utils/array.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'kron';
  9. var dependencies = ['typed', 'matrix', 'multiplyScalar'];
  10. var createKron = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var typed = _ref.typed,
  12. matrix = _ref.matrix,
  13. multiplyScalar = _ref.multiplyScalar;
  14. /**
  15. * Calculates the kronecker product of 2 matrices or vectors.
  16. *
  17. * NOTE: If a one dimensional vector / matrix is given, it will be
  18. * wrapped so its two dimensions.
  19. * See the examples.
  20. *
  21. * Syntax:
  22. *
  23. * math.kron(x, y)
  24. *
  25. * Examples:
  26. *
  27. * math.kron([[1, 0], [0, 1]], [[1, 2], [3, 4]])
  28. * // returns [ [ 1, 2, 0, 0 ], [ 3, 4, 0, 0 ], [ 0, 0, 1, 2 ], [ 0, 0, 3, 4 ] ]
  29. *
  30. * math.kron([1,1], [2,3,4])
  31. * // returns [ [ 2, 3, 4, 2, 3, 4 ] ]
  32. *
  33. * See also:
  34. *
  35. * multiply, dot, cross
  36. *
  37. * @param {Array | Matrix} x First vector
  38. * @param {Array | Matrix} y Second vector
  39. * @return {Array | Matrix} Returns the kronecker product of `x` and `y`
  40. */
  41. return typed(name, {
  42. 'Matrix, Matrix': function MatrixMatrix(x, y) {
  43. return matrix(_kron(x.toArray(), y.toArray()));
  44. },
  45. 'Matrix, Array': function MatrixArray(x, y) {
  46. return matrix(_kron(x.toArray(), y));
  47. },
  48. 'Array, Matrix': function ArrayMatrix(x, y) {
  49. return matrix(_kron(x, y.toArray()));
  50. },
  51. 'Array, Array': _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 ((0, _array.arraySize)(a).length === 1) {
  63. // Wrap it in a 2D Matrix
  64. a = [a];
  65. }
  66. if ((0, _array.arraySize)(b).length === 1) {
  67. // Wrap it in a 2D Matrix
  68. b = [b];
  69. }
  70. if ((0, _array.arraySize)(a).length > 2 || (0, _array.arraySize)(b).length > 2) {
  71. throw new RangeError('Vectors with dimensions greater then 2 are not supported expected ' + '(Size x = ' + JSON.stringify(a.length) + ', y = ' + JSON.stringify(b.length) + ')');
  72. }
  73. var t = [];
  74. var r = [];
  75. return a.map(function (a) {
  76. return b.map(function (b) {
  77. r = [];
  78. t.push(r);
  79. return a.map(function (y) {
  80. return b.map(function (x) {
  81. return r.push(multiplyScalar(y, x));
  82. });
  83. });
  84. });
  85. }) && t;
  86. }
  87. });
  88. exports.createKron = createKron;