concat.js (4481B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createConcat = void 0; 7 8 var _is = require("../../utils/is.js"); 9 10 var _object = require("../../utils/object.js"); 11 12 var _array = require("../../utils/array.js"); 13 14 var _IndexError = require("../../error/IndexError.js"); 15 16 var _DimensionError = require("../../error/DimensionError.js"); 17 18 var _factory = require("../../utils/factory.js"); 19 20 var name = 'concat'; 21 var dependencies = ['typed', 'matrix', 'isInteger']; 22 var createConcat = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 23 var typed = _ref.typed, 24 matrix = _ref.matrix, 25 isInteger = _ref.isInteger; 26 27 /** 28 * Concatenate two or more matrices. 29 * 30 * Syntax: 31 * 32 * math.concat(A, B, C, ...) 33 * math.concat(A, B, C, ..., dim) 34 * 35 * Where: 36 * 37 * - `dim: number` is a zero-based dimension over which to concatenate the matrices. 38 * By default the last dimension of the matrices. 39 * 40 * Examples: 41 * 42 * const A = [[1, 2], [5, 6]] 43 * const B = [[3, 4], [7, 8]] 44 * 45 * math.concat(A, B) // returns [[1, 2, 3, 4], [5, 6, 7, 8]] 46 * math.concat(A, B, 0) // returns [[1, 2], [5, 6], [3, 4], [7, 8]] 47 * math.concat('hello', ' ', 'world') // returns 'hello world' 48 * 49 * See also: 50 * 51 * size, squeeze, subset, transpose 52 * 53 * @param {... Array | Matrix} args Two or more matrices 54 * @return {Array | Matrix} Concatenated matrix 55 */ 56 return typed(name, { 57 // TODO: change signature to '...Array | Matrix, dim?' when supported 58 '...Array | Matrix | number | BigNumber': function ArrayMatrixNumberBigNumber(args) { 59 var i; 60 var len = args.length; 61 var dim = -1; // zero-based dimension 62 63 var prevDim; 64 var asMatrix = false; 65 var matrices = []; // contains multi dimensional arrays 66 67 for (i = 0; i < len; i++) { 68 var arg = args[i]; // test whether we need to return a Matrix (if not we return an Array) 69 70 if ((0, _is.isMatrix)(arg)) { 71 asMatrix = true; 72 } 73 74 if ((0, _is.isNumber)(arg) || (0, _is.isBigNumber)(arg)) { 75 if (i !== len - 1) { 76 throw new Error('Dimension must be specified as last argument'); 77 } // last argument contains the dimension on which to concatenate 78 79 80 prevDim = dim; 81 dim = arg.valueOf(); // change BigNumber to number 82 83 if (!isInteger(dim)) { 84 throw new TypeError('Integer number expected for dimension'); 85 } 86 87 if (dim < 0 || i > 0 && dim > prevDim) { 88 // TODO: would be more clear when throwing a DimensionError here 89 throw new _IndexError.IndexError(dim, prevDim + 1); 90 } 91 } else { 92 // this is a matrix or array 93 var m = (0, _object.clone)(arg).valueOf(); 94 var size = (0, _array.arraySize)(m); 95 matrices[i] = m; 96 prevDim = dim; 97 dim = size.length - 1; // verify whether each of the matrices has the same number of dimensions 98 99 if (i > 0 && dim !== prevDim) { 100 throw new _DimensionError.DimensionError(prevDim + 1, dim + 1); 101 } 102 } 103 } 104 105 if (matrices.length === 0) { 106 throw new SyntaxError('At least one matrix expected'); 107 } 108 109 var res = matrices.shift(); 110 111 while (matrices.length) { 112 res = _concat(res, matrices.shift(), dim, 0); 113 } 114 115 return asMatrix ? matrix(res) : res; 116 }, 117 '...string': function string(args) { 118 return args.join(''); 119 } 120 }); 121 }); 122 /** 123 * Recursively concatenate two matrices. 124 * The contents of the matrices is not cloned. 125 * @param {Array} a Multi dimensional array 126 * @param {Array} b Multi dimensional array 127 * @param {number} concatDim The dimension on which to concatenate (zero-based) 128 * @param {number} dim The current dim (zero-based) 129 * @return {Array} c The concatenated matrix 130 * @private 131 */ 132 133 exports.createConcat = createConcat; 134 135 function _concat(a, b, concatDim, dim) { 136 if (dim < concatDim) { 137 // recurse into next dimension 138 if (a.length !== b.length) { 139 throw new _DimensionError.DimensionError(a.length, b.length); 140 } 141 142 var c = []; 143 144 for (var i = 0; i < a.length; i++) { 145 c[i] = _concat(a[i], b[i], concatDim, dim + 1); 146 } 147 148 return c; 149 } else { 150 // concatenate this dimension 151 return a.concat(b); 152 } 153 }