CholeskyDecomposition.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace VisualMath.Accord.Math.Decompositions
  7. {
  8. using System;
  9. /// <summary>
  10. /// Cholesky Decomposition of a symmetric, positive definite matrix.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// For a symmetric, positive definite matrix <c>A</c>, the Cholesky decomposition is a
  15. /// lower triangular matrix <c>L</c> so that <c>A = L * L'</c>.
  16. /// If the matrix is not symmetric or positive definite, the constructor returns a partial
  17. /// decomposition and sets two internal variables that can be queried using the
  18. /// <see cref="Symmetric"/> and <see cref="PositiveDefinite"/> properties.</para>
  19. /// <para>
  20. /// Any square matrix A with non-zero pivots can be written as the product of a
  21. /// lower triangular matrix L and an upper triangular matrix U; this is called
  22. /// the LU decomposition. However, if A is symmetric and positive definite, we
  23. /// can choose the factors such that U is the transpose of L, and this is called
  24. /// the Cholesky decomposition. Both the LU and the Cholesky decomposition are
  25. /// used to solve systems of linear equations.</para>
  26. /// <para>
  27. /// When it is applicable, the Cholesky decomposition is twice as efficient
  28. /// as the LU decomposition.</para>
  29. /// </remarks>
  30. ///
  31. public sealed class CholeskyDecomposition : ICloneable
  32. {
  33. private double[,] L;
  34. private bool symmetric;
  35. private bool positiveDefinite;
  36. /// <summary>Constructs a Cholesky Decomposition.</summary>
  37. public CholeskyDecomposition(double[,] value)
  38. {
  39. if (value == null)
  40. {
  41. throw new ArgumentNullException("value", "Matrix cannot be null.");
  42. }
  43. if (value.GetLength(0) != value.GetLength(1))
  44. {
  45. throw new ArgumentException("Matrix is not square.", "value");
  46. }
  47. int dimension = value.GetLength(0);
  48. L = new double[dimension, dimension];
  49. double[,] a = value;
  50. this.positiveDefinite = true;
  51. this.symmetric = true;
  52. unsafe
  53. {
  54. fixed (double* l = L)
  55. {
  56. for (int j = 0; j < dimension; j++)
  57. {
  58. double* Lrowj = l + j * dimension;
  59. double d = 0.0;
  60. for (int k = 0; k < j; k++)
  61. {
  62. double* Lrowk = l + k * dimension;
  63. double s = 0.0;
  64. for (int i = 0; i < k; i++)
  65. {
  66. s += Lrowk[i] * Lrowj[i];
  67. }
  68. Lrowj[k] = s = (a[j, k] - s) / Lrowk[k];
  69. d = d + s * s;
  70. this.symmetric = this.symmetric & (a[k, j] == a[j, k]);
  71. }
  72. d = a[j, j] - d;
  73. this.positiveDefinite = this.positiveDefinite & (d > 0.0);
  74. Lrowj[j] = System.Math.Sqrt(System.Math.Max(d, 0.0));
  75. for (int k = j + 1; k < dimension; k++)
  76. {
  77. Lrowj[k] = 0.0;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. /// <summary>Returns <see langword="true"/> if the matrix is symmetric.</summary>
  84. public bool Symmetric
  85. {
  86. get { return this.symmetric; }
  87. }
  88. /// <summary>Returns <see langword="true"/> if the matrix is positive definite.</summary>
  89. public bool PositiveDefinite
  90. {
  91. get { return this.positiveDefinite; }
  92. }
  93. /// <summary>Returns the left triangular factor <c>L</c> so that <c>A = L * L'</c>.</summary>
  94. public double[,] LeftTriangularFactor
  95. {
  96. get { return this.L; }
  97. }
  98. /// <summary>Solves a set of equation systems of type <c>A * X = B</c>.</summary>
  99. /// <param name="value">Right hand side matrix with as many rows as <c>A</c> and any number of columns.</param>
  100. /// <returns>Matrix <c>X</c> so that <c>L * L' * X = B</c>.</returns>
  101. /// <exception cref="T:System.ArgumentException">Matrix dimensions do not match.</exception>
  102. /// <exception cref="T:System.InvalidOperationException">Matrix is not symmetric and positive definite.</exception>
  103. public double[,] Solve(double[,] value)
  104. {
  105. if (value == null)
  106. {
  107. throw new ArgumentNullException("value");
  108. }
  109. if (value.GetLength(0) != L.GetLength(0))
  110. {
  111. throw new ArgumentException("Matrix dimensions do not match.");
  112. }
  113. if (!this.symmetric)
  114. {
  115. throw new InvalidOperationException("Matrix is not symmetric.");
  116. }
  117. if (!this.positiveDefinite)
  118. {
  119. throw new InvalidOperationException("Matrix is not positive definite.");
  120. }
  121. int dimension = L.GetLength(0);
  122. int count = value.GetLength(1);
  123. double[,] B = (double[,])value.Clone();
  124. // Solve L*Y = B;
  125. for (int k = 0; k < dimension; k++)
  126. {
  127. for (int j = 0; j < count; j++)
  128. {
  129. for (int i = 0; i < k; i++)
  130. {
  131. B[k, j] -= B[i, j] * L[k, i];
  132. }
  133. B[k, j] /= L[k, k];
  134. }
  135. }
  136. // Solve L'*X = Y;
  137. for (int k = dimension - 1; k >= 0; k--)
  138. {
  139. for (int j = 0; j < count; j++)
  140. {
  141. for (int i = k + 1; i < dimension; i++)
  142. {
  143. B[k, j] -= B[i, j] * L[i, k];
  144. }
  145. B[k, j] /= L[k, k];
  146. }
  147. }
  148. return B;
  149. }
  150. /// <summary>Solves a set of equation systems of type <c>A * x = b</c>.</summary>
  151. /// <param name="value">Right hand side column vector with as many rows as <c>A</c>.</param>
  152. /// <returns>Vector <c>x</c> so that <c>L * L' * x = b</c>.</returns>
  153. /// <exception cref="T:System.ArgumentException">Matrix dimensions do not match.</exception>
  154. /// <exception cref="T:System.InvalidOperationException">Matrix is not symmetric and positive definite.</exception>
  155. public double[] Solve(double[] value)
  156. {
  157. if (value == null)
  158. {
  159. throw new ArgumentNullException("value");
  160. }
  161. if (value.Length != L.GetLength(0))
  162. {
  163. throw new ArgumentException("Matrix dimensions do not match.");
  164. }
  165. if (!this.symmetric)
  166. {
  167. throw new InvalidOperationException("Matrix is not symmetric.");
  168. }
  169. if (!this.positiveDefinite)
  170. {
  171. throw new InvalidOperationException("Matrix is not positive definite.");
  172. }
  173. int dimension = L.GetLength(0);
  174. double[] B = (double[])value.Clone();
  175. // Solve L*Y = B;
  176. for (int k = 0; k < dimension; k++)
  177. {
  178. for (int i = 0; i < k; i++)
  179. B[k] -= B[i] * L[k, i];
  180. B[k] /= L[k, k];
  181. }
  182. // Solve L'*X = Y;
  183. for (int k = dimension - 1; k >= 0; k--)
  184. {
  185. for (int i = k + 1; i < dimension; i++)
  186. B[k] -= B[i] * L[i, k];
  187. B[k] /= L[k, k];
  188. }
  189. return B;
  190. }
  191. #region ICloneable Members
  192. private CholeskyDecomposition()
  193. {
  194. }
  195. /// <summary>
  196. /// Creates a new object that is a copy of the current instance.
  197. /// </summary>
  198. /// <returns>
  199. /// A new object that is a copy of this instance.
  200. /// </returns>
  201. public object Clone()
  202. {
  203. var clone = new CholeskyDecomposition();
  204. clone.L = (double[,])L.Clone();
  205. clone.positiveDefinite = positiveDefinite;
  206. clone.symmetric = symmetric;
  207. return clone;
  208. }
  209. #endregion
  210. }
  211. }