simple-squiggle

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

squeeze.js (1666B)


      1 import { clone } from '../../utils/object.js';
      2 import { squeeze as arraySqueeze } from '../../utils/array.js';
      3 import { factory } from '../../utils/factory.js';
      4 var name = 'squeeze';
      5 var dependencies = ['typed', 'matrix'];
      6 export var createSqueeze = /* #__PURE__ */factory(name, dependencies, _ref => {
      7   var {
      8     typed,
      9     matrix
     10   } = _ref;
     11 
     12   /**
     13    * Squeeze a matrix, remove inner and outer singleton dimensions from a matrix.
     14    *
     15    * Syntax:
     16    *
     17    *     math.squeeze(x)
     18    *
     19    * Examples:
     20    *
     21    *     math.squeeze([3])           // returns 3
     22    *     math.squeeze([[3]])         // returns 3
     23    *
     24    *     const A = math.zeros(3, 1)    // returns [[0], [0], [0]] (size 3x1)
     25    *     math.squeeze(A)             // returns [0, 0, 0] (size 3)
     26    *
     27    *     const B = math.zeros(1, 3)    // returns [[0, 0, 0]] (size 1x3)
     28    *     math.squeeze(B)             // returns [0, 0, 0] (size 3)
     29    *
     30    *     // only inner and outer dimensions are removed
     31    *     const C = math.zeros(2, 1, 3) // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3)
     32    *     math.squeeze(C)             // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3)
     33    *
     34    * See also:
     35    *
     36    *     subset
     37    *
     38    * @param {Matrix | Array} x      Matrix to be squeezed
     39    * @return {Matrix | Array} Squeezed matrix
     40    */
     41   return typed(name, {
     42     Array: function Array(x) {
     43       return arraySqueeze(clone(x));
     44     },
     45     Matrix: function Matrix(x) {
     46       var res = arraySqueeze(x.toArray()); // FIXME: return the same type of matrix as the input
     47 
     48       return Array.isArray(res) ? matrix(res) : res;
     49     },
     50     any: function any(x) {
     51       // scalar
     52       return clone(x);
     53     }
     54   });
     55 });