Skip to content

Commit

Permalink
feat: add transpose function
Browse files Browse the repository at this point in the history
  • Loading branch information
maasencioh committed Jun 10, 2017
1 parent f6a97b6 commit c264545
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ describe('Sparse Matrix', () => {
m = new SparseMatrix([[0, 1], [0, 1]]);
expect(m.isSymmetric()).toBe(false);
});

it('transpose', () => {
const matrix1 = new SparseMatrix([[1, 2], [3, 4]]);
expect(matrix1.transpose().to2DArray()).toEqual([[1, 3], [2, 4]]);
});
});
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ export class SparseMatrix {
}
return this;
}

/**
* @return {SparseMatrix} - New transposed sparse matrix
*/
transpose() {
let trans = new SparseMatrix(this.columns, this.rows, {initialCapacity: this.cardinality});
this.forEachNonZero((i, j, value) => {
trans.set(j, i, value);
return value;
});
return trans;
}
}

SparseMatrix.prototype.klass = 'Matrix';
Expand Down

0 comments on commit c264545

Please sign in to comment.