concat.js 4.4 KB

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