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