This project has moved and is read-only. For the latest updates, please go here.

CholeskyDecomposition F#

Sep 3, 2012 at 12:39 PM

Hi,

I am trying to do some work around an object of type CholeskyDecomposition in F#. However, I cannot figure out how to access the elements of a CholeskyDecomposition class. I've tried referring to it as <object of type CholeskyDecomposition>.[row, col] but that does not work.

 

Can anybody help?

 

Thanks

Kishor

Sep 3, 2012 at 7:31 PM

Every matrix decomposition is represented by a decomposition object (CholeskyDecomposition, QRDecomposition, SingularValueDecomposition) that is a container for the various factor matrices. To get to an individual factor matrix, call the method specific to it on the decomposition object. Then you can access the individual elements of elements of that matrix, or use the matrix in other operations. In the case of the CholeskyDecomposition class, the factor matrix is obtained from SquareRootMatrix(). Here is some example code:

open Meta.Numerics
open Meta.Numerics.Matrices

// Simple routine to print a matrix
let PrintMatrix (M : AnyRectangularMatrix) =
  for r = 0 to M.RowCount-1 do
    for c = 0 to M.ColumnCount-1 do
      printf "%A " M.[r, c]
    printfn ""

// Define a positve definite, symmetric matrix
let A = new SymmetricMatrix(2)
A.[0,0] <- 2.0
A.[1,1] <- 3.0
A.[0,1] <- 1.0
PrintMatrix(A)

// Cholesky decompose it as A = B B^T
let CD = A.CholeskyDecomposition()

// Get the factor matrix B
let B = CD.SquareRootMatrix()
PrintMatrix(B)

// Verify that the factorization works
PrintMatrix(B * B.Transpose())

Once you have B here, you can access B.[row, col].

Sep 4, 2012 at 3:17 PM

Cool, that worked.  :)