Skip to content

Commit

Permalink
chore: add duplicate option
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Oct 10, 2024
1 parent 5966343 commit be6fa5b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
28 changes: 27 additions & 1 deletion src/matrix/__tests__/matrixGetSubMatrix.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { matrixGetSubMatrix } from '../matrixGetSubMatrix';

test('should extract submatrix correctly', () => {
test('should extract submatrix correctly without duplication', () => {

Check failure on line 3 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.

Check failure on line 3 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (18)

src/matrix/__tests__/matrixGetSubMatrix.test.ts

ReferenceError: test is not defined ❯ src/matrix/__tests__/matrixGetSubMatrix.test.ts:3:1

Check failure on line 3 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (20)

src/matrix/__tests__/matrixGetSubMatrix.test.ts

ReferenceError: test is not defined ❯ src/matrix/__tests__/matrixGetSubMatrix.test.ts:3:1

Check failure on line 3 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (22)

src/matrix/__tests__/matrixGetSubMatrix.test.ts

ReferenceError: test is not defined ❯ src/matrix/__tests__/matrixGetSubMatrix.test.ts:3:1
const matrix = [
new Float64Array([1, 2, 3]),
new Float64Array([4, 5, 6]),
Expand All @@ -16,6 +16,31 @@ test('should extract submatrix correctly', () => {
const subMatrix = matrixGetSubMatrix(matrix, options);
const expectedResult = [new Float64Array([2, 3]), new Float64Array([5, 6])];
expect(subMatrix).toEqual(expectedResult);

Check failure on line 18 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'expect'.

subMatrix[0][0] = 10;
expect(matrix[0][1]).toBe(10);

Check failure on line 21 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'expect'.
});

test('should extract submatrix correctly with duplication', () => {

Check failure on line 24 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
const matrix = [
new Float64Array([1, 2, 3]),
new Float64Array([4, 5, 6]),
new Float64Array([7, 8, 9]),
];
const options = {
startRow: 0,
startColumn: 1,
endRow: 1,
endColumn: 2,
duplicate: true,
};

const subMatrix = matrixGetSubMatrix(matrix, options);
const expectedResult = [new Float64Array([2, 3]), new Float64Array([5, 6])];
expect(subMatrix).toEqual(expectedResult);

Check failure on line 40 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'expect'.

subMatrix[0][0] = 10;
expect(matrix[0][1]).toBe(2);

Check failure on line 43 in src/matrix/__tests__/matrixGetSubMatrix.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'expect'.
});

test('should throw RangeError for out-of-range indices', () => {
Expand All @@ -29,6 +54,7 @@ test('should throw RangeError for out-of-range indices', () => {
startColumn: 1,
endRow: 3, // Out of range
endColumn: 2,
duplicate: true,
};

expect(() => matrixGetSubMatrix(matrix, options)).toThrow(RangeError);
Expand Down
20 changes: 11 additions & 9 deletions src/matrix/matrixGetSubMatrix.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { DoubleMatrix } from '../types';

import { matrixCheckRanges } from './matrixCheckRanges';
import { matrixCreateEmpty } from './matrixCreateEmpty';

export interface MatrixGetSubMatrixOptions {
/**
Expand All @@ -24,6 +21,11 @@ export interface MatrixGetSubMatrixOptions {
* @default matrix[0].length - 1
*/
endColumn: number;
/**
* duplicate the data
* @default false
*/
duplicate?: boolean;
}
/**
* Get a subMatrix from matrix, the function check if the subMatrix
Expand All @@ -32,24 +34,24 @@ export interface MatrixGetSubMatrixOptions {
* @returns The sub `matrix`.
*/
export function matrixGetSubMatrix(
matrix: DoubleMatrix,
matrix: Float64Array[],
options: MatrixGetSubMatrixOptions,
): Float64Array[] {
const {
startRow = 0,
endRow = matrix.length - 1,
startColumn = 0,
endColumn = matrix[0].length - 1,
duplicate = false,
} = options;
matrixCheckRanges(matrix, { startColumn, startRow, endColumn, endRow });
const nbColumns = endColumn - startColumn + 1;
const nbRows = endRow - startRow + 1;

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

return subMatrix;
}

0 comments on commit be6fa5b

Please sign in to comment.