using System;
using System.Runtime.Serialization;
namespace DotNetMatrix
{
/// Cholesky Decomposition.
/// For a symmetric, positive definite matrix A, the Cholesky decomposition
/// is an lower triangular matrix L so that A = L*L'.
/// If the matrix is not symmetric or positive definite, the constructor
/// returns a partial decomposition and sets an internal flag that may
/// be queried by the isSPD() method.
///
[Serializable]
public class CholeskyDecomposition : System.Runtime.Serialization.ISerializable
{
#region Class variables
/// Array for internal storage of decomposition.
/// @serial internal array storage.
///
private decimal[][] L;
/// Row and column dimension (square matrix).
/// @serial matrix dimension.
///
private int n;
/// Symmetric and positive definite flag.
/// @serial is symmetric and positive definite flag.
///
private bool isspd;
#endregion // Class variables
#region Constructor
/// Cholesky algorithm for symmetric and positive definite matrix.
/// Square, symmetric matrix.
///
/// Structure to access L and isspd flag.
///
public CholeskyDecomposition(GeneralMatrix Arg)
{
// Initialize.
decimal[][] A = Arg.Array;
n = Arg.RowDimension;
L = new decimal[n][];
for (int i = 0; i < n; i++)
{
L[i] = new decimal[n];
}
isspd = (Arg.ColumnDimension == n);
// Main loop.
for (int j = 0; j < n; j++)
{
if (!isspd) break;
decimal[] Lrowj = L[j];
decimal d = 0.0m;
for (int k = 0; k < j; k++)
{
decimal[] Lrowk = L[k];
decimal s = 0.0m;
for (int i = 0; i < k; i++)
{
s += Lrowk[i] * Lrowj[i];
}
if (L[k][k] <= 0.0m)
{
isspd = false;
break;
}
Lrowj[k] = s = (A[j][k] - s) / L[k][k];
d = d + s * s;
isspd = isspd & (A[k][j] == A[j][k]);
}
if (!isspd) break;
d = A[j][j] - d;
isspd = isspd & (d > 0.0m);
if (!isspd || d <= 0.0m)
{
isspd = false;
L[j][j] = 0.0m;
break;
}
L[j][j] = DecimalMath.Sqrt(d);
for (int k = j + 1; k < n; k++)
{
L[j][k] = 0.0m;
}
}
}
#endregion // Constructor
#region Public Properties
/// Is the matrix symmetric and positive definite?
/// true if A is symmetric and positive definite.
///
virtual public bool SPD
{
get
{
return isspd;
}
}
#endregion // Public Properties
#region Public Methods
/// Return triangular factor.
/// L
///
public virtual GeneralMatrix GetL()
{
return new GeneralMatrix(L, n, n);
}
/// Solve A*X = B
/// A Matrix with as many rows as A and any number of columns.
///
/// X so that L*L'*X = B
///
/// Matrix row dimensions must agree.
///
/// Matrix is not symmetric positive definite.
///
public virtual GeneralMatrix Solve(GeneralMatrix B)
{
if (B.RowDimension != n)
{
throw new System.ArgumentException("Matrix row dimensions must agree.");
}
if (!isspd)
{
throw new System.SystemException("Matrix is not symmetric positive definite.");
}
// Copy right hand side.
decimal[][] X = B.ArrayCopy;
int nx = B.ColumnDimension;
// Solve L*Y = B;
for (int k = 0; k < n; k++)
{
for (int i = k + 1; i < n; i++)
{
for (int j = 0; j < nx; j++)
{
X[i][j] -= X[k][j] * L[i][k];
}
}
for (int j = 0; j < nx; j++)
{
X[k][j] /= L[k][k];
}
}
// Solve L'*X = Y;
for (int k = n - 1; k >= 0; k--)
{
for (int j = 0; j < nx; j++)
{
X[k][j] /= L[k][k];
}
for (int i = 0; i < k; i++)
{
for (int j = 0; j < nx; j++)
{
X[i][j] -= X[k][j] * L[k][i];
}
}
}
return new GeneralMatrix(X, n, nx);
}
#endregion // Public Methods
// A method called when serializing this class.
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
}
}
}