-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from jbrown1618/cholesky-decomposition
Implemented Cholesky Decomposition
- Loading branch information
Showing
9 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@josh-brown/vector", | ||
"author": "Joshua Brown <[email protected]>", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "A linear algebra library written in TypeScript that focuses on generality, extensibility, and ease of use.", | ||
"license": "ISC", | ||
"repository": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
import { NumberMatrix } from '../types/matrix/NumberMatrix'; | ||
import { calculateCholeskyDecomposition } from './CholeskyDecomposition'; | ||
|
||
describe('CholeskyDecomposition', () => { | ||
const matrixBuilder = NumberMatrix.builder(); | ||
|
||
describe('calculateCholeskyDecomposition', () => { | ||
it('calculates the Cholesky Decomposition of a matrix A', () => { | ||
const A = matrixBuilder.fromArray([[4, 12, -16], [12, 37, -43], [-16, -43, 98]]); | ||
const expectedL = matrixBuilder.fromArray([[2, 0, 0], [6, 1, 0], [-8, 5, 3]]); | ||
|
||
const decomposition = calculateCholeskyDecomposition(A); | ||
expect(decomposition).not.to.be.undefined; | ||
const { L } = decomposition as any; | ||
expect(L).to.deep.equal(expectedL); | ||
|
||
// Check that LL* equals A | ||
const LLstar = L.multiply(L.adjoint()); | ||
expect(LLstar.equals(A)).to.be.true; | ||
}); | ||
|
||
it('returns undefined for a non-square matrix', () => { | ||
const A = matrixBuilder.fromArray([[1, 2]]); | ||
expect(calculateCholeskyDecomposition(A)).to.be.undefined; | ||
}); | ||
|
||
it('returns undefined for a non-symmetric matrix', () => { | ||
const A = matrixBuilder.fromArray([[4, 12, -16], [12, 37, -43], [-16, -46, 98]]); | ||
expect(calculateCholeskyDecomposition(A)).to.be.undefined; | ||
}); | ||
|
||
it('returns undefined for a non-positive-definite matrix', () => { | ||
const A = matrixBuilder.fromArray([[-1]]); | ||
expect(calculateCholeskyDecomposition(A)).to.be.undefined; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Matrix } from '../types/matrix/Matrix'; | ||
import { isHermitian } from '../utilities/MatrixProperties'; | ||
|
||
export interface CholeskyDecomposition<S> { | ||
L: Matrix<S>; | ||
} | ||
|
||
/** | ||
* Uses the serial version of the Cholesky algorith to calculate the Cholesky | ||
* decomposition of a matrix `A`. | ||
* | ||
* @remarks | ||
* A Cholesky decomposition of a matrix `A` consists of a lower-triangular | ||
* matrix `L` such that _LL* = A_. | ||
* | ||
* A Cholesky decomposition only exists if `A` is symmetrix and positive-definite. | ||
* @param A - The matrix to decompose | ||
*/ | ||
export function calculateCholeskyDecomposition<S>( | ||
A: Matrix<S> | ||
): CholeskyDecomposition<S> | undefined { | ||
if (!isHermitian(A)) return undefined; | ||
|
||
const ops = A.ops(); | ||
const builder = A.builder(); | ||
const dim = A.getNumberOfColumns(); | ||
|
||
let L = builder.zeros(dim); | ||
|
||
for (let j = 0; j < dim; j++) { | ||
const Ajj = A.getEntry(j, j); | ||
let entrySquared = Ajj; | ||
for (let p = 0; p < j; p++) { | ||
const Ljp = L.getEntry(j, p); | ||
entrySquared = ops.subtract(entrySquared, ops.multiply(Ljp, ops.conjugate(Ljp))); | ||
} | ||
const Ljj = ops.getPrincipalSquareRoot(entrySquared); | ||
if (Ljj === undefined) return undefined; | ||
L = L.set(j, j, Ljj); | ||
|
||
for (let i = j + 1; i < dim; i++) { | ||
let difference = A.getEntry(i, j); | ||
for (let p = 0; p < j; p++) { | ||
const Lip = L.getEntry(i, p); | ||
const Ljp = L.getEntry(j, p); | ||
difference = ops.subtract(difference, ops.multiply(Lip, ops.conjugate(Ljp))); | ||
} | ||
const Lij = ops.divide(difference, Ljj); | ||
if (Lij === undefined) return undefined; | ||
|
||
L = L.set(i, j, Lij); | ||
} | ||
} | ||
return { L }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters