Skip to content

Commit 0b00126

Browse files
authored
feat: add matrixTranspose function (#309)
* feat: add matrixTranspose function * chore: add matrixCheck
1 parent 07bf1b1 commit 0b00126

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ exports[`test existence of exported functions 1`] = `
165165
"matrixZPivotRescale",
166166
"matrixZRescale",
167167
"matrixZRescalePerColumn",
168+
"matrixTranspose",
168169
"createNumberArray",
169170
"createDoubleArray",
170171
"createFromToArray",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { test, expect } from 'vitest';
2+
3+
import { matrixTranspose } from '../matrixTranspose';
4+
5+
test('should transpose a square matrix', () => {
6+
const matrix = [
7+
[1, 2],
8+
[3, 4],
9+
];
10+
expect(matrixTranspose(matrix, { ArrayConstructor: Array })).toEqual([
11+
[1, 3],
12+
[2, 4],
13+
]);
14+
});
15+
16+
test('should transpose a rectangular matrix', () => {
17+
const matrix = [
18+
[1, 2, 3],
19+
[4, 5, 6],
20+
];
21+
expect(matrixTranspose(matrix, { ArrayConstructor: Array })).toEqual([
22+
[1, 4],
23+
[2, 5],
24+
[3, 6],
25+
]);
26+
});
27+
28+
test('should transpose a single row matrix', () => {
29+
const matrix = [[1, 2, 3]];
30+
expect(matrixTranspose(matrix, { ArrayConstructor: Array })).toEqual([
31+
[1],
32+
[2],
33+
[3],
34+
]);
35+
});
36+
37+
test('should transpose a single column matrix', () => {
38+
const matrix = [[1], [2], [3]];
39+
expect(matrixTranspose(matrix, { ArrayConstructor: Array })).toEqual([
40+
[1, 2, 3],
41+
]);
42+
});

src/matrix/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export * from './matrixToArray';
2424
export * from './matrixZPivotRescale';
2525
export * from './matrixZRescale';
2626
export * from './matrixZRescalePerColumn';
27+
export * from './matrixTranspose';

src/matrix/matrixTranspose.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { NumberArrayConstructor } from '../utils';
2+
import { matrixCheck } from './matrixCheck';
3+
4+
import { matrixCreateEmpty } from './matrixCreateEmpty';
5+
6+
export interface MatrixTransposeOptions<
7+
ArrayConstructorType extends NumberArrayConstructor = Float64ArrayConstructor,
8+
> {
9+
/**
10+
* Allows to specify the type of array to use
11+
* @default Float64Array
12+
*/
13+
ArrayConstructor?: ArrayConstructorType;
14+
}
15+
16+
export function matrixTranspose<
17+
ArrayConstructorType extends NumberArrayConstructor = Float64ArrayConstructor,
18+
>(
19+
matrix: number[][],
20+
options: MatrixTransposeOptions<ArrayConstructorType> = {},
21+
) {
22+
matrixCheck(matrix);
23+
const { ArrayConstructor } = options;
24+
const nbRows = matrix.length;
25+
const nbColumns = matrix[0].length;
26+
27+
// Create new matrix with swapped dimensions
28+
const result = matrixCreateEmpty({
29+
nbColumns: nbRows,
30+
nbRows: nbColumns,
31+
ArrayConstructor,
32+
});
33+
34+
for (let i = 0; i < nbRows; i++) {
35+
for (let j = 0; j < nbColumns; j++) {
36+
result[j][i] = matrix[i][j];
37+
}
38+
}
39+
40+
return result;
41+
}

0 commit comments

Comments
 (0)