flatten.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { flatten as flattenArray } from '../../utils/array.js';
  2. import { factory } from '../../utils/factory.js';
  3. var name = 'flatten';
  4. var dependencies = ['typed', 'matrix'];
  5. export var createFlatten = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. matrix
  9. } = _ref;
  10. /**
  11. * Flatten a multidimensional matrix into a single dimensional matrix.
  12. * A new matrix is returned, the original matrix is left untouched.
  13. *
  14. * Syntax:
  15. *
  16. * math.flatten(x)
  17. *
  18. * Examples:
  19. *
  20. * math.flatten([[1,2], [3,4]]) // returns [1, 2, 3, 4]
  21. *
  22. * See also:
  23. *
  24. * concat, resize, size, squeeze
  25. *
  26. * @param {Matrix | Array} x Matrix to be flattened
  27. * @return {Matrix | Array} Returns the flattened matrix
  28. */
  29. return typed(name, {
  30. Array: function Array(x) {
  31. return flattenArray(x);
  32. },
  33. Matrix: function Matrix(x) {
  34. var flat = flattenArray(x.toArray());
  35. // TODO: return the same matrix type as x (Dense or Sparse Matrix)
  36. return matrix(flat);
  37. }
  38. });
  39. });