Skip to content

Commit 77e5ed7

Browse files
committed
feat: implement reverseRows and reverseColumns methods
1 parent ea0bcd6 commit 77e5ed7

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

matrix.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,21 @@ declare module 'ml-matrix' {
186186

187187
prod(): number;
188188
norm(type: 'frobenius' | 'max'): number;
189-
cumulativeSum(): Matrix;
189+
cumulativeSum(): this;
190190
dot(vector2: Matrix): number;
191191
mmul(other: Matrix): Matrix;
192192
strassen2x2(other: Matrix): Matrix;
193193
strassen3x3(other: Matrix): Matrix;
194194
mmulStrassen(y: Matrix): Matrix;
195195
scaleRows(min?: number, max?: number): Matrix;
196196
scaleColumns(min?: number, max?: number): Matrix;
197+
reverseRows(): this;
198+
reverseColumns(): this;
197199
kroneckerProduct(other: Matrix): Matrix;
198200
tensorProduct(other: Matrix): Matrix;
199201
transpose(): Matrix;
200-
sortRows(compareFunction: Function): Matrix;
201-
sortColumns(compareFunction: Function): Matrix;
202+
sortRows(compareFunction: Function): this;
203+
sortColumns(compareFunction: Function): this;
202204
subMatrix(
203205
startRow: number,
204206
endRow: number,

src/__tests__/matrix/reverse.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Matrix } from '../..';
2+
3+
describe('revers rows and columns', () => {
4+
it('reverse rows', () => {
5+
const matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
6+
const result = matrix.reverseRows();
7+
expect(result).toBe(matrix);
8+
expect(result.to2DArray()).toStrictEqual([[3, 2, 1], [6, 5, 4]]);
9+
});
10+
11+
it('reverse columns', () => {
12+
const matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
13+
const result = matrix.reverseColumns();
14+
expect(result).toBe(matrix);
15+
expect(result.to2DArray()).toStrictEqual([[4, 5, 6], [1, 2, 3]]);
16+
});
17+
});

src/abstractMatrix.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,32 @@ export default function AbstractMatrix(superCtor) {
13831383
return newMatrix;
13841384
}
13851385

1386+
reverseRows() {
1387+
const middle = Math.ceil(this.columns / 2);
1388+
for (var i = 0; i < this.rows; i++) {
1389+
for (var j = 0; j < middle; j++) {
1390+
var first = this.get(i, j);
1391+
var last = this.get(i, this.columns - 1 - j);
1392+
this.set(i, j, last);
1393+
this.set(i, this.columns - 1 - j, first);
1394+
}
1395+
}
1396+
return this;
1397+
}
1398+
1399+
reverseColumns() {
1400+
const middle = Math.ceil(this.rows / 2);
1401+
for (var j = 0; j < this.columns; j++) {
1402+
for (var i = 0; i < middle; i++) {
1403+
var first = this.get(i, j);
1404+
var last = this.get(this.rows - 1 - i, j);
1405+
this.set(i, j, last);
1406+
this.set(this.rows - 1 - i, j, first);
1407+
}
1408+
}
1409+
return this;
1410+
}
1411+
13861412
/**
13871413
* Returns the Kronecker product (also known as tensor product) between this and other
13881414
* See https://en.wikipedia.org/wiki/Kronecker_product

0 commit comments

Comments
 (0)