simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

kron.js (2530B)


      1 import { arraySize as size } from '../../utils/array.js';
      2 import { factory } from '../../utils/factory.js';
      3 var name = 'kron';
      4 var dependencies = ['typed', 'matrix', 'multiplyScalar'];
      5 export var createKron = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed,
      8     matrix,
      9     multiplyScalar
     10   } = _ref;
     11 
     12   /**
     13      * Calculates the kronecker product of 2 matrices or vectors.
     14      *
     15      * NOTE: If a one dimensional vector / matrix is given, it will be
     16      * wrapped so its two dimensions.
     17      * See the examples.
     18      *
     19      * Syntax:
     20      *
     21      *    math.kron(x, y)
     22      *
     23      * Examples:
     24      *
     25      *    math.kron([[1, 0], [0, 1]], [[1, 2], [3, 4]])
     26      *    // returns [ [ 1, 2, 0, 0 ], [ 3, 4, 0, 0 ], [ 0, 0, 1, 2 ], [ 0, 0, 3, 4 ] ]
     27      *
     28      *    math.kron([1,1], [2,3,4])
     29      *    // returns [ [ 2, 3, 4, 2, 3, 4 ] ]
     30      *
     31      * See also:
     32      *
     33      *    multiply, dot, cross
     34      *
     35      * @param  {Array | Matrix} x     First vector
     36      * @param  {Array | Matrix} y     Second vector
     37      * @return {Array | Matrix}       Returns the kronecker product of `x` and `y`
     38      */
     39   return typed(name, {
     40     'Matrix, Matrix': function MatrixMatrix(x, y) {
     41       return matrix(_kron(x.toArray(), y.toArray()));
     42     },
     43     'Matrix, Array': function MatrixArray(x, y) {
     44       return matrix(_kron(x.toArray(), y));
     45     },
     46     'Array, Matrix': function ArrayMatrix(x, y) {
     47       return matrix(_kron(x, y.toArray()));
     48     },
     49     'Array, Array': _kron
     50   });
     51   /**
     52      * Calculate the kronecker product of two matrices / vectors
     53      * @param {Array} a  First vector
     54      * @param {Array} b  Second vector
     55      * @returns {Array} Returns the kronecker product of x and y
     56      * @private
     57      */
     58 
     59   function _kron(a, b) {
     60     // Deal with the dimensions of the matricies.
     61     if (size(a).length === 1) {
     62       // Wrap it in a 2D Matrix
     63       a = [a];
     64     }
     65 
     66     if (size(b).length === 1) {
     67       // Wrap it in a 2D Matrix
     68       b = [b];
     69     }
     70 
     71     if (size(a).length > 2 || size(b).length > 2) {
     72       throw new RangeError('Vectors with dimensions greater then 2 are not supported expected ' + '(Size x = ' + JSON.stringify(a.length) + ', y = ' + JSON.stringify(b.length) + ')');
     73     }
     74 
     75     var t = [];
     76     var r = [];
     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 });