UNPKG

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