collection.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.containsCollections = containsCollections;
  6. exports.deepForEach = deepForEach;
  7. exports.deepMap = deepMap;
  8. exports.reduce = reduce;
  9. exports.scatter = scatter;
  10. var _is = require("./is.js");
  11. var _IndexError = require("../error/IndexError.js");
  12. var _array = require("./array.js");
  13. var _switch2 = require("./switch.js");
  14. /**
  15. * Test whether an array contains collections
  16. * @param {Array} array
  17. * @returns {boolean} Returns true when the array contains one or multiple
  18. * collections (Arrays or Matrices). Returns false otherwise.
  19. */
  20. function containsCollections(array) {
  21. for (var i = 0; i < array.length; i++) {
  22. if ((0, _is.isCollection)(array[i])) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. /**
  29. * Recursively loop over all elements in a given multi dimensional array
  30. * and invoke the callback on each of the elements.
  31. * @param {Array | Matrix} array
  32. * @param {Function} callback The callback method is invoked with one
  33. * parameter: the current element in the array
  34. */
  35. function deepForEach(array, callback) {
  36. if ((0, _is.isMatrix)(array)) {
  37. array = array.valueOf();
  38. }
  39. for (var i = 0, ii = array.length; i < ii; i++) {
  40. var value = array[i];
  41. if (Array.isArray(value)) {
  42. deepForEach(value, callback);
  43. } else {
  44. callback(value);
  45. }
  46. }
  47. }
  48. /**
  49. * Execute the callback function element wise for each element in array and any
  50. * nested array
  51. * Returns an array with the results
  52. * @param {Array | Matrix} array
  53. * @param {Function} callback The callback is called with two parameters:
  54. * value1 and value2, which contain the current
  55. * element of both arrays.
  56. * @param {boolean} [skipZeros] Invoke callback function for non-zero values only.
  57. *
  58. * @return {Array | Matrix} res
  59. */
  60. function deepMap(array, callback, skipZeros) {
  61. if (array && typeof array.map === 'function') {
  62. // TODO: replace array.map with a for loop to improve performance
  63. return array.map(function (x) {
  64. return deepMap(x, callback, skipZeros);
  65. });
  66. } else {
  67. return callback(array);
  68. }
  69. }
  70. /**
  71. * Reduce a given matrix or array to a new matrix or
  72. * array with one less dimension, applying the given
  73. * callback in the selected dimension.
  74. * @param {Array | Matrix} mat
  75. * @param {number} dim
  76. * @param {Function} callback
  77. * @return {Array | Matrix} res
  78. */
  79. function reduce(mat, dim, callback) {
  80. var size = Array.isArray(mat) ? (0, _array.arraySize)(mat) : mat.size();
  81. if (dim < 0 || dim >= size.length) {
  82. // TODO: would be more clear when throwing a DimensionError here
  83. throw new _IndexError.IndexError(dim, size.length);
  84. }
  85. if ((0, _is.isMatrix)(mat)) {
  86. return mat.create(_reduce(mat.valueOf(), dim, callback));
  87. } else {
  88. return _reduce(mat, dim, callback);
  89. }
  90. }
  91. /**
  92. * Recursively reduce a matrix
  93. * @param {Array} mat
  94. * @param {number} dim
  95. * @param {Function} callback
  96. * @returns {Array} ret
  97. * @private
  98. */
  99. function _reduce(mat, dim, callback) {
  100. var i, ret, val, tran;
  101. if (dim <= 0) {
  102. if (!Array.isArray(mat[0])) {
  103. val = mat[0];
  104. for (i = 1; i < mat.length; i++) {
  105. val = callback(val, mat[i]);
  106. }
  107. return val;
  108. } else {
  109. tran = (0, _switch2._switch)(mat);
  110. ret = [];
  111. for (i = 0; i < tran.length; i++) {
  112. ret[i] = _reduce(tran[i], dim - 1, callback);
  113. }
  114. return ret;
  115. }
  116. } else {
  117. ret = [];
  118. for (i = 0; i < mat.length; i++) {
  119. ret[i] = _reduce(mat[i], dim - 1, callback);
  120. }
  121. return ret;
  122. }
  123. }
  124. // TODO: document function scatter
  125. function scatter(a, j, w, x, u, mark, cindex, f, inverse, update, value) {
  126. // a arrays
  127. var avalues = a._values;
  128. var aindex = a._index;
  129. var aptr = a._ptr;
  130. // vars
  131. var k, k0, k1, i;
  132. // check we need to process values (pattern matrix)
  133. if (x) {
  134. // values in j
  135. for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
  136. // row
  137. i = aindex[k];
  138. // check value exists in current j
  139. if (w[i] !== mark) {
  140. // i is new entry in j
  141. w[i] = mark;
  142. // add i to pattern of C
  143. cindex.push(i);
  144. // x(i) = A, check we need to call function this time
  145. if (update) {
  146. // copy value to workspace calling callback function
  147. x[i] = inverse ? f(avalues[k], value) : f(value, avalues[k]);
  148. // function was called on current row
  149. u[i] = mark;
  150. } else {
  151. // copy value to workspace
  152. x[i] = avalues[k];
  153. }
  154. } else {
  155. // i exists in C already
  156. x[i] = inverse ? f(avalues[k], x[i]) : f(x[i], avalues[k]);
  157. // function was called on current row
  158. u[i] = mark;
  159. }
  160. }
  161. } else {
  162. // values in j
  163. for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
  164. // row
  165. i = aindex[k];
  166. // check value exists in current j
  167. if (w[i] !== mark) {
  168. // i is new entry in j
  169. w[i] = mark;
  170. // add i to pattern of C
  171. cindex.push(i);
  172. } else {
  173. // indicate function was called on current row
  174. u[i] = mark;
  175. }
  176. }
  177. }
  178. }