simple-squiggle

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

matrixFromColumns.js (2505B)


      1 import { factory } from '../../utils/factory.js';
      2 var name = 'matrixFromColumns';
      3 var dependencies = ['typed', 'matrix', 'flatten', 'size'];
      4 export var createMatrixFromColumns = /* #__PURE__ */factory(name, dependencies, _ref => {
      5   var {
      6     typed,
      7     matrix,
      8     flatten,
      9     size
     10   } = _ref;
     11 
     12   /**
     13    * Create a dense matrix from vectors as individual columns.
     14    * If you pass row vectors, they will be transposed (but not conjugated!)
     15    *
     16    * Syntax:
     17    *
     18    *    math.matrixFromColumns(...arr)
     19    *    math.matrixFromColumns(col1, col2)
     20    *    math.matrixFromColumns(col1, col2, col3)
     21    *
     22    * Examples:
     23    *
     24    *    math.matrixFromColumns([1, 2, 3], [[4],[5],[6]])
     25    *    math.matrixFromColumns(...vectors)
     26    *
     27    * See also:
     28    *
     29    *    matrix, matrixFromRows, matrixFromFunction, zeros
     30    *
     31    * @param {... Array | Matrix} cols Multiple columns
     32    * @return { number[][] | Matrix } if at least one of the arguments is an array, an array will be returned
     33    */
     34   return typed(name, {
     35     '...Array': function Array(arr) {
     36       return _createArray(arr);
     37     },
     38     '...Matrix': function Matrix(arr) {
     39       return matrix(_createArray(arr.map(m => m.toArray())));
     40     } // TODO implement this properly for SparseMatrix
     41 
     42   });
     43 
     44   function _createArray(arr) {
     45     if (arr.length === 0) throw new TypeError('At least one column is needed to construct a matrix.');
     46     var N = checkVectorTypeAndReturnLength(arr[0]); // create an array with empty rows
     47 
     48     var result = [];
     49 
     50     for (var i = 0; i < N; i++) {
     51       result[i] = [];
     52     } // loop columns
     53 
     54 
     55     for (var col of arr) {
     56       var colLength = checkVectorTypeAndReturnLength(col);
     57 
     58       if (colLength !== N) {
     59         throw new TypeError('The vectors had different length: ' + (N | 0) + ' ≠ ' + (colLength | 0));
     60       }
     61 
     62       var f = flatten(col); // push a value to each row
     63 
     64       for (var _i = 0; _i < N; _i++) {
     65         result[_i].push(f[_i]);
     66       }
     67     }
     68 
     69     return result;
     70   }
     71 
     72   function checkVectorTypeAndReturnLength(vec) {
     73     var s = size(vec);
     74 
     75     if (s.length === 1) {
     76       // 1D vector
     77       return s[0];
     78     } else if (s.length === 2) {
     79       // 2D vector
     80       if (s[0] === 1) {
     81         // row vector
     82         return s[1];
     83       } else if (s[1] === 1) {
     84         // col vector
     85         return s[0];
     86       } else {
     87         throw new TypeError('At least one of the arguments is not a vector.');
     88       }
     89     } else {
     90       throw new TypeError('Only one- or two-dimensional vectors are supported.');
     91     }
     92   }
     93 });