Skip to content

Commit

Permalink
feat: add isEchelonForm and isReducedEchelonForm (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goneiross authored and targos committed Feb 9, 2019
1 parent 5b48178 commit dee2a94
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/matrix/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,16 @@ describe('utility methods', () => {
expect(result[2][0]).toBeCloseTo(0.24390244, 5);
expect(result[2][1]).toBeCloseTo(0.07317073, 5);
});
it('isEchelonForm', () => {
var matrix = new Matrix([[1, 0], [0, 1]]);
var matrix2 = new Matrix([[2, 1], [1, 1]]);
expect(matrix.isEchelonForm()).toStrictEqual(true);
expect(matrix2.isEchelonForm()).toStrictEqual(false);
});
it('isReducedEchelonForm', () => {
var matrix = new Matrix([[1, 0], [0, 1]]);
var matrix2 = new Matrix([[1, 1], [0, 1]]);
expect(matrix.isReducedEchelonForm()).toStrictEqual(true);
expect(matrix2.isReducedEchelonForm()).toStrictEqual(false);
});
});
61 changes: 61 additions & 0 deletions src/abstractMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,67 @@ export default function AbstractMatrix(superCtor) {
return false;
}

/**
* @return true if the matrix is in echelon form
*/
isEchelonForm() {
let i = 0;
let j = 0;
let previousColumn = -1;
let isEchelonForm = true;
let checked = false;
while ((i < this.rows) && (isEchelonForm)) {
j = 0;
checked = false;
while ((j < this.columns) && (checked === false)) {
if (this.get(i, j) === 0) {
j++;
} else if ((this.get(i, j) === 1) && (j > previousColumn)) {
checked = true;
previousColumn = j;
} else {
isEchelonForm = false;
checked = true;
}
}
i++;
}
return isEchelonForm;
}

/**
* @return true if the matrix is in reduced echelon form
*/
isReducedEchelonForm() {
let i = 0;
let j = 0;
let previousColumn = -1;
let isReducedEchelonForm = true;
let checked = false;
while ((i < this.rows) && (isReducedEchelonForm)) {
j = 0;
checked = false;
while ((j < this.columns) && (checked === false)) {
if (this.get(i, j) === 0) {
j++;
} else if ((this.get(i, j) === 1) && (j > previousColumn)) {
checked = true;
previousColumn = j;
} else {
isReducedEchelonForm = false;
checked = true;
}
}
for (let k = j + 1; k < this.rows; k++) {
if (this.get(i, k) !== 0) {
isReducedEchelonForm = false;
}
}
i++;
}
return isReducedEchelonForm;
}

/**
* Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1
* @abstract
Expand Down

0 comments on commit dee2a94

Please sign in to comment.