apply.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createApply = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _array = require("../../utils/array.js");
  8. var _is = require("../../utils/is.js");
  9. var _IndexError = require("../../error/IndexError.js");
  10. var name = 'apply';
  11. var dependencies = ['typed', 'isInteger'];
  12. var createApply = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  13. var typed = _ref.typed,
  14. isInteger = _ref.isInteger;
  15. /**
  16. * Apply a function that maps an array to a scalar
  17. * along a given axis of a matrix or array.
  18. * Returns a new matrix or array with one less dimension than the input.
  19. *
  20. * Syntax:
  21. *
  22. * math.apply(A, dim, callback)
  23. *
  24. * Where:
  25. *
  26. * - `dim: number` is a zero-based dimension over which to concatenate the matrices.
  27. *
  28. * Examples:
  29. *
  30. * const A = [[1, 2], [3, 4]]
  31. * const sum = math.sum
  32. *
  33. * math.apply(A, 0, sum) // returns [4, 6]
  34. * math.apply(A, 1, sum) // returns [3, 7]
  35. *
  36. * See also:
  37. *
  38. * map, filter, forEach
  39. *
  40. * @param {Array | Matrix} array The input Matrix
  41. * @param {number} dim The dimension along which the callback is applied
  42. * @param {Function} callback The callback function that is applied. This Function
  43. * should take an array or 1-d matrix as an input and
  44. * return a number.
  45. * @return {Array | Matrix} res The residual matrix with the function applied over some dimension.
  46. */
  47. return typed(name, {
  48. 'Array | Matrix, number | BigNumber, function': function ArrayMatrixNumberBigNumberFunction(mat, dim, callback) {
  49. if (!isInteger(dim)) {
  50. throw new TypeError('Integer number expected for dimension');
  51. }
  52. var size = Array.isArray(mat) ? (0, _array.arraySize)(mat) : mat.size();
  53. if (dim < 0 || dim >= size.length) {
  54. throw new _IndexError.IndexError(dim, size.length);
  55. }
  56. if ((0, _is.isMatrix)(mat)) {
  57. return mat.create(_apply(mat.valueOf(), dim, callback));
  58. } else {
  59. return _apply(mat, dim, callback);
  60. }
  61. }
  62. });
  63. });
  64. /**
  65. * Recursively reduce a matrix
  66. * @param {Array} mat
  67. * @param {number} dim
  68. * @param {Function} callback
  69. * @returns {Array} ret
  70. * @private
  71. */
  72. exports.createApply = createApply;
  73. function _apply(mat, dim, callback) {
  74. var i, ret, tran;
  75. if (dim <= 0) {
  76. if (!Array.isArray(mat[0])) {
  77. return callback(mat);
  78. } else {
  79. tran = _switch(mat);
  80. ret = [];
  81. for (i = 0; i < tran.length; i++) {
  82. ret[i] = _apply(tran[i], dim - 1, callback);
  83. }
  84. return ret;
  85. }
  86. } else {
  87. ret = [];
  88. for (i = 0; i < mat.length; i++) {
  89. ret[i] = _apply(mat[i], dim - 1, callback);
  90. }
  91. return ret;
  92. }
  93. }
  94. /**
  95. * Transpose a matrix
  96. * @param {Array} mat
  97. * @returns {Array} ret
  98. * @private
  99. */
  100. function _switch(mat) {
  101. var I = mat.length;
  102. var J = mat[0].length;
  103. var i, j;
  104. var ret = [];
  105. for (j = 0; j < J; j++) {
  106. var tmp = [];
  107. for (i = 0; i < I; i++) {
  108. tmp.push(mat[i][j]);
  109. }
  110. ret.push(tmp);
  111. }
  112. return ret;
  113. }