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