File tree Expand file tree Collapse file tree 4 files changed +85
-0
lines changed
Expand file tree Collapse file tree 4 files changed +85
-0
lines changed Original file line number Diff line number Diff 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",
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change @@ -24,3 +24,4 @@ export * from './matrixToArray';
2424export * from './matrixZPivotRescale' ;
2525export * from './matrixZRescale' ;
2626export * from './matrixZRescalePerColumn' ;
27+ export * from './matrixTranspose' ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments