core/utils/splits.js

  1. import { ValueErr, NotImplemented } from "./errors.js";
  2. //Different types of data splits can be added into this section.
  3. /**
  4. * @namespace splits
  5. * @description Collection of functions to be used for splitting data across HydroCompute
  6. */
  7. export const splits = {
  8. /**
  9. * Splits a 1D array into N different chunks.
  10. * @param {object} params - The parameters for splitting the array.
  11. * @param {Array} params.data - The 1D array of data.
  12. * @param {number} params.n - The number of chunks to create.
  13. * @returns {Array} - An array of chunks.
  14. */
  15. split1DArray: ({ data: data, n: n }) => {
  16. const chunkSize = Math.ceil(data.length / n);
  17. const chunks = [];
  18. for (let i = 0; i < n; i++) {
  19. const chunk = data.slice(i * chunkSize, (i + 1) * chunkSize);
  20. chunks.push(chunk);
  21. }
  22. return chunks;
  23. },
  24. /**
  25. * Splits each array from a 2D matrix into N different chunks.
  26. * @param {object} params - The parameters for splitting the matrix.
  27. * @param {Array} params.data - The 2D matrix of data.
  28. * @param {number} params.n - The number of chunks to create.
  29. * @returns {Array} - An array of chunks.
  30. */
  31. splitmDArray: ({ data: data, n: n }) => {
  32. if (data.length === 0 || !Array.isArray(data[0])) {
  33. return splits.split1DArray({ data: data, n: n });
  34. } else {
  35. const result = [];
  36. for (const subarray of data) {
  37. result.push(splits.splitmDArray({ data: subarray, n: n }));
  38. }
  39. return result;
  40. }
  41. },
  42. /**
  43. * Splits a matrix into two submatrices.
  44. * @param {object} params - The parameters for splitting the matrix.
  45. * @param {Array} params.data - The matrix to split.
  46. * @param {number} params.n - The number of rows for the first submatrix.
  47. * @param {number} params.m - The number of columns for the first submatrix.
  48. * @returns {Array} - An array containing the two submatrices.
  49. */
  50. splitMatrix: ({ data: data, n: n, m: m }) => {
  51. const matrix1 = [];
  52. const matrix2 = [];
  53. for (let i = 0; i < n; i++) {
  54. matrix1[i] = data[i].slice(0, m);
  55. matrix2[i] = data[i].slice(m);
  56. }
  57. return [matrix1, matrix2];
  58. },
  59. /**
  60. * Divides a matrix into submatrices.
  61. * @param {object} params - The parameters for dividing the matrix.
  62. * @param {Array} params.data - The matrix to divide.
  63. * @param {number} params.k - The number of rows for each submatrix.
  64. * @param {number} params.l - The number of columns for each submatrix.
  65. * @returns {Array} - An array of submatrices.
  66. */
  67. divideIntoSubmatrices: ({ data: data, k: k, l: l }) => {
  68. const submatrices = [];
  69. for (var data of data)
  70. for (let i = 0; i < data.length; i += k) {
  71. for (let j = 0; j < data[0].length; j += l) {
  72. submatrices.push(
  73. data.slice(i, i + k).map((row) => row.slice(j, j + l))
  74. );
  75. }
  76. }
  77. return submatrices;
  78. },
  79. /**
  80. * Joins chunks of data together.
  81. * @param {object} params - The parameters for joining the chunks.
  82. * @param {Array} params.chunks - An array of chunks to join.
  83. * @returns {Array} - The joined array of chunks.
  84. */
  85. join: ({ chunks: chunks }) => {
  86. let firstChunk = chunks[0].slice();
  87. for (var i = 1; i < chunks.length; i++) {
  88. firstChunk.push(...chunks[i]);
  89. }
  90. chunks = undefined;
  91. return firstChunk;
  92. },
  93. /**
  94. * Runs a specific split function based on the provided name and data.
  95. * @param {string} name - The name of the split function to run.
  96. * @param {*} data - The data to pass to the split function.
  97. * @returns {*} - The result of the split function.
  98. * @throws {NotImplemented} - If the split function is not found.
  99. */
  100. main: (name, data) => {
  101. if (typeof splits[name] === "undefined") {
  102. throw new NotImplemented(`Function is not found in the given script.`);
  103. } else {
  104. return splits[name](data);
  105. }
  106. },
  107. };