Skip to content

Commit

Permalink
chore: duplicate data by default n improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Oct 13, 2024
1 parent be6fa5b commit b534efc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/matrix/__tests__/matrixGetSubMatrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test('should extract submatrix correctly without duplication', () => {
startColumn: 1,
endRow: 1,
endColumn: 2,
duplicate: false,
};

const subMatrix = matrixGetSubMatrix(matrix, options);
Expand Down
15 changes: 10 additions & 5 deletions src/matrix/matrixGetSubMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface MatrixGetSubMatrixOptions {
endColumn: number;
/**
* duplicate the data
* @default false
* @default true
*/
duplicate?: boolean;
}
Expand All @@ -42,15 +42,20 @@ export function matrixGetSubMatrix(
endRow = matrix.length - 1,
startColumn = 0,
endColumn = matrix[0].length - 1,
duplicate = false,
duplicate = true,
} = options;
matrixCheckRanges(matrix, { startColumn, startRow, endColumn, endRow });
const nbRows = endRow - startRow + 1;

const method = duplicate ? 'slice' : 'subarray';
const subMatrix: Float64Array[] = [];
for (let i = 0; i < nbRows; i++) {
subMatrix.push(matrix[startRow + i][method](startColumn, endColumn + 1));
if (duplicate) {
for (let i = 0; i < nbRows; i++) {
subMatrix.push(matrix[startRow + i].slice(startColumn, endColumn + 1));
}
} else {
for (let i = 0; i < nbRows; i++) {
subMatrix.push(matrix[startRow + i].subarray(startColumn, endColumn + 1));
}
}

return subMatrix;
Expand Down

0 comments on commit b534efc

Please sign in to comment.