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