reshape.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createReshape = void 0;
  6. var _array = require("../../utils/array.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'reshape';
  9. var dependencies = ['typed', 'isInteger', 'matrix'];
  10. var createReshape = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var typed = _ref.typed,
  12. isInteger = _ref.isInteger;
  13. /**
  14. * Reshape a multi dimensional array to fit the specified dimensions
  15. *
  16. * Syntax:
  17. *
  18. * math.reshape(x, sizes)
  19. *
  20. * Examples:
  21. *
  22. * math.reshape([1, 2, 3, 4, 5, 6], [2, 3])
  23. * // returns Array [[1, 2, 3], [4, 5, 6]]
  24. *
  25. * math.reshape([[1, 2], [3, 4]], [1, 4])
  26. * // returns Array [[1, 2, 3, 4]]
  27. *
  28. * math.reshape([[1, 2], [3, 4]], [4])
  29. * // returns Array [1, 2, 3, 4]
  30. *
  31. * const x = math.matrix([1, 2, 3, 4, 5, 6, 7, 8])
  32. * math.reshape(x, [2, 2, 2])
  33. * // returns Matrix [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
  34. *
  35. * math.reshape([1, 2, 3, 4], [-1, 2])
  36. * // returns Matrix [[1, 2], [3, 4]]
  37. *
  38. * See also:
  39. *
  40. * size, squeeze, resize
  41. *
  42. * @param {Array | Matrix | *} x Matrix to be reshaped
  43. * @param {number[]} sizes One dimensional array with integral sizes for
  44. * each dimension. One -1 is allowed as wildcard,
  45. * which calculates this dimension automatically.
  46. *
  47. * @return {* | Array | Matrix} A reshaped clone of matrix `x`
  48. *
  49. * @throws {TypeError} If `sizes` does not contain solely integers
  50. * @throws {DimensionError} If the product of the new dimension sizes does
  51. * not equal that of the old ones
  52. */
  53. return typed(name, {
  54. 'Matrix, Array': function MatrixArray(x, sizes) {
  55. return x.reshape(sizes, true);
  56. },
  57. 'Array, Array': function ArrayArray(x, sizes) {
  58. sizes.forEach(function (size) {
  59. if (!isInteger(size)) {
  60. throw new TypeError('Invalid size for dimension: ' + size);
  61. }
  62. });
  63. return (0, _array.reshape)(x, sizes);
  64. }
  65. });
  66. });
  67. exports.createReshape = createReshape;