Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add matrixGetSubMatrix function #236

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ exports[`test existence of exported functions 1`] = `
"matrixClone",
"matrixColumnsCorrelation",
"matrixCreateEmpty",
"matrixGetSubMatrix",
"matrixHistogram",
"matrixMaxAbsoluteZ",
"matrixMedian",
Expand Down
40 changes: 40 additions & 0 deletions src/matrix/__tests__/matrixCheckRanges.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { matrixCheckRanges } from '../matrixCheckRanges';

test('should not throw error for valid indices', () => {

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

View workflow job for this annotation

GitHub Actions / nodejs / test (18)

src/matrix/__tests__/matrixCheckRanges.test.ts

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

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

View workflow job for this annotation

GitHub Actions / nodejs / test (20)

src/matrix/__tests__/matrixCheckRanges.test.ts

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

Check failure on line 3 in src/matrix/__tests__/matrixCheckRanges.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__/matrixCheckRanges.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (22)

src/matrix/__tests__/matrixCheckRanges.test.ts

ReferenceError: test is not defined ❯ src/matrix/__tests__/matrixCheckRanges.test.ts:3:1
// Define a sample matrix
const matrix = [
new Float64Array([1, 2, 3]),
new Float64Array([4, 5, 6]),
new Float64Array([7, 8, 9]),
];

// Define options with valid indices
const options = {
startRow: 0,
startColumn: 0,
endRow: 2,
endColumn: 2,
};

expect(() => matrixCheckRanges(matrix, options)).not.toThrow();

Check failure on line 19 in src/matrix/__tests__/matrixCheckRanges.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', () => {

Check failure on line 22 in src/matrix/__tests__/matrixCheckRanges.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`.
// Define a sample matrix
const matrix = [
new Float64Array([1, 2, 3]),
new Float64Array([4, 5, 6]),
new Float64Array([7, 8, 9]),
];

// Define options with out-of-range indices
const options = {
startRow: 0,
startColumn: 0,
endRow: 3, // Out of range
endColumn: 2,
};

// Call the function and expect test to throw RangeError
expect(() => matrixCheckRanges(matrix, options)).toThrow(RangeError);

Check failure on line 39 in src/matrix/__tests__/matrixCheckRanges.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Cannot find name 'expect'.
});
62 changes: 62 additions & 0 deletions src/matrix/__tests__/matrixGetSubMatrix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { matrixGetSubMatrix } from '../matrixGetSubMatrix';

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 / 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 / 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 (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]),
new Float64Array([7, 8, 9]),
];
const options = {
startRow: 0,
startColumn: 1,
endRow: 1,
endColumn: 2,
duplicate: false,
};

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

Check failure on line 19 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 22 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 25 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 41 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 44 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', () => {
const matrix = [
new Float64Array([1, 2, 3]),
new Float64Array([4, 5, 6]),
new Float64Array([7, 8, 9]),
];
const options = {
startRow: 0,
startColumn: 1,
endRow: 3, // Out of range
endColumn: 2,
duplicate: true,
};

expect(() => matrixGetSubMatrix(matrix, options)).toThrow(RangeError);
});
1 change: 1 addition & 0 deletions src/matrix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './matrixCheck';
export * from './matrixClone';
export * from './matrixColumnsCorrelation';
export * from './matrixCreateEmpty';
export * from './matrixGetSubMatrix';
export * from './matrixHistogram';
export * from './matrixMaxAbsoluteZ';
export * from './matrixMedian';
Expand Down
27 changes: 27 additions & 0 deletions src/matrix/matrixCheckRanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DoubleMatrix } from '../types';

export function matrixCheckRanges(

Check warning on line 3 in src/matrix/matrixCheckRanges.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc comment
matrix: DoubleMatrix,
boundaries: {
startRow: number;
endRow: number;
startColumn: number;
endColumn: number;
},
) {
const { startRow, endRow, startColumn, endColumn } = boundaries;
if (
startRow > endRow ||
startColumn > endColumn ||
startRow < 0 ||
startRow >= matrix.length ||
endRow < 0 ||
endRow >= matrix.length ||
startColumn < 0 ||
startColumn >= matrix[0].length ||
endColumn < 0 ||
endColumn >= matrix[0].length
) {
throw new RangeError('submatrix indices are out of range');
}
}
62 changes: 62 additions & 0 deletions src/matrix/matrixGetSubMatrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { matrixCheckRanges } from './matrixCheckRanges';

export interface MatrixGetSubMatrixOptions {
/**
* row index in matrix for the first row in subMatrix.
* @default 0
*/
startRow: number;
/**
* column index in matrix for the first column in subMatrix.
* @default 0
*/
startColumn: number;
/**
* row index in matrix for the last row in subMatrix.
* @default matrix.length - 1
*/
endRow: number;
/**
* column index in matrix for the last column in subMatrix.
* @default matrix[0].length - 1
*/
endColumn: number;
/**
* duplicate the data
* @default true
*/
duplicate?: boolean;
}
/**
* Get a subMatrix from matrix, the function check if the subMatrix
* lies into the dimensions of matrix.
* @param matrix - matrix that will receive the new element values.
* @returns The sub `matrix`.
*/
export function matrixGetSubMatrix(
matrix: Float64Array[],
options: MatrixGetSubMatrixOptions,
): Float64Array[] {
const {
startRow = 0,
endRow = matrix.length - 1,
startColumn = 0,
endColumn = matrix[0].length - 1,
duplicate = true,
} = options;
matrixCheckRanges(matrix, { startColumn, startRow, endColumn, endRow });
const nbRows = endRow - startRow + 1;

const subMatrix: Float64Array[] = [];
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;
}
27 changes: 3 additions & 24 deletions src/matrix/matrixSetSubMatrix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DoubleMatrix } from '../types';

import { matrixCheckRanges } from './matrixCheckRanges';

/**
* Set in-place a subMatrix to matrix, the function check if the subMatrix
* lies into the dimensions of matrix.
Expand All @@ -17,34 +19,11 @@ export function matrixSetSubMatrix(
): DoubleMatrix {
const endRow = startRow + subMatrix.length - 1;
const endColumn = startColumn + subMatrix[0].length - 1;
checkRange(matrix, startRow, endRow, startColumn, endColumn);
matrixCheckRanges(matrix, { startRow, endRow, startColumn, endColumn });
for (let i = 0; i < subMatrix.length; i++) {
for (let j = 0; j < subMatrix[0].length; j++) {
matrix[startRow + i][startColumn + j] = subMatrix[i][j];
}
}
return matrix;
}

function checkRange(
matrix: DoubleMatrix,
startRow: number,
endRow: number,
startColumn: number,
endColumn: number,
) {
if (
startRow > endRow ||
startColumn > endColumn ||
startRow < 0 ||
startRow >= matrix.length ||
endRow < 0 ||
endRow >= matrix.length ||
startColumn < 0 ||
startColumn >= matrix[0].length ||
endColumn < 0 ||
endColumn >= matrix[0].length
) {
throw new RangeError('submatrix indices are out of range');
}
}
Loading