Skip to content

Commit

Permalink
fix: correctly ready elements in QR#orthogonalMatrix
Browse files Browse the repository at this point in the history
Fixes: #96
  • Loading branch information
targos committed Sep 30, 2019
1 parent 7c20388 commit 2f527a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const {
linearDependencies,
QrDecomposition,
LuDecomposition,
CholeskyDecomposition
CholeskyDecomposition,
} = require('ml-matrix');

//===========================
Expand Down Expand Up @@ -157,15 +157,15 @@ var error = Matrix.sub(b, A.mmul(x)); // The error enables to evaluate the solut
// QR Decomposition

var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var QR = QrDecomposition(A);
var QR = new QrDecomposition(A);
var Q = QR.orthogonalMatrix;
var R = QR.upperTriangularMatrix;
// So you have the QR decomposition. If you multiply Q by R, you'll see that A = Q.R, with Q orthogonal and R upper triangular

// LU Decomposition

var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var LU = LuDecomposition(A);
var LU = new LuDecomposition(A);
var L = LU.lowerTriangularMatrix;
var U = LU.upperTriangularMatrix;
var P = LU.pivotPermutationVector;
Expand All @@ -174,13 +174,13 @@ var P = LU.pivotPermutationVector;
// Cholesky Decomposition

var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var cholesky = CholeskyDecomposition(A);
var cholesky = new CholeskyDecomposition(A);
var L = cholesky.lowerTriangularMatrix;

// Eigenvalues & eigenvectors

var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var e = EigenvalueDecomposition(A);
var e = new EigenvalueDecomposition(A);
var real = e.realEigenvalues;
var imaginary = e.imaginaryEigenvalues;
var vectors = e.eigenvectorMatrix;
Expand All @@ -195,7 +195,7 @@ var A = new Matrix([
[0, 1, 6, 0],
[0, 3, 0, 1],
[0, 0, 1, 0],
[0, 1, 2, 0]
[0, 1, 2, 0],
]);
var dependencies = linearDependencies(A); // dependencies is a matrix with the dependencies of the rows. When we look row by row, we see that the first row is [0, 0, 0, 0, 0], so it means that the first row is independent, and the second row is [ 0, 0, 0, 4, 1 ], i.e the second row = 4 times the 4th row + the 5th row.
```
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/decompositions/qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ describe('Qr decomposition', () => {
const qr = new QR(A);
expect(qr.solve(b).to2DArray()).toBeDeepCloseTo([[1], [0.9999]], 4);
});

it('should work with the example in the documentation', () => {
const A = [[2, 3, 5], [4, 1, 6], [1, 3, 0]];
const dc = new QR(A);
const Q = dc.orthogonalMatrix;
const R = dc.upperTriangularMatrix;
const qR = Q.mmul(R);
expect(qR.to2DArray()).toBeDeepCloseTo(A, 4);
});
});
2 changes: 1 addition & 1 deletion src/dc/qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class QrDecomposition {
s += qr.get(i, k) * X.get(i, j);
}

s = -s / qr[k][k];
s = -s / qr.get(k, k);

for (i = k; i < rows; i++) {
X.set(i, j, X.get(i, j) + s * qr.get(i, k));
Expand Down

0 comments on commit 2f527a3

Please sign in to comment.