squeeze.js 1.6 KB

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