diff --git a/src/Numerics/LinearAlgebra/Complex/Factorization/DenseCholesky.cs b/src/Numerics/LinearAlgebra/Complex/Factorization/DenseCholesky.cs index 04074f96..da788e9e 100644 --- a/src/Numerics/LinearAlgebra/Complex/Factorization/DenseCholesky.cs +++ b/src/Numerics/LinearAlgebra/Complex/Factorization/DenseCholesky.cs @@ -154,5 +154,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization var dfactor = (DenseMatrix) Factor; Control.LinearAlgebraProvider.CholeskySolveFactored(dfactor.Values, dfactor.RowCount, dresult.Values, 1); } + + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != matrix.ColumnCount) + { + throw new ArgumentException(Resources.ArgumentMatrixSquare); + } + + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + var dmatrix = matrix as DenseMatrix; + if (dmatrix == null) + { + throw new NotSupportedException("Can only do Cholesky factorization for dense matrices at the moment."); + } + + var dfactor = (DenseMatrix)Factor; + + // Overwrite the existing Factor matrix with the input. + Array.Copy(dmatrix.Values, 0, dfactor.Values, 0, dmatrix.Values.Length); + + // Perform factorization (while overwriting). + Control.LinearAlgebraProvider.CholeskyFactor(dfactor.Values, dfactor.RowCount); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Factorization/UserCholesky.cs b/src/Numerics/LinearAlgebra/Complex/Factorization/UserCholesky.cs index 51284380..487ff885 100644 --- a/src/Numerics/LinearAlgebra/Complex/Factorization/UserCholesky.cs +++ b/src/Numerics/LinearAlgebra/Complex/Factorization/UserCholesky.cs @@ -52,22 +52,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization internal sealed class UserCholesky : Cholesky { /// - /// Initializes a new instance of the class. This object will compute the - /// Cholesky factorization when the constructor is called and cache it's factorization. + /// Computes the Cholesky factorization in-place. /// - /// The matrix to factor. - /// If is null. - /// If is not a square matrix. - /// If is not positive definite. - public static UserCholesky Create(Matrix matrix) + /// On entry, the matrix to factor. On exit, the Cholesky factor matrix + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + static void DoCholesky(Matrix factor) { - if (matrix.RowCount != matrix.ColumnCount) + if (factor.RowCount != factor.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). - var factor = matrix.Clone(); var tmpColumn = new Complex[factor.RowCount]; // Main loop - along the diagonal @@ -103,10 +101,43 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization factor.At(ij, i, Complex.Zero); } } + } + /// + /// Initializes a new instance of the class. This object will compute the + /// Cholesky factorization when the constructor is called and cache it's factorization. + /// + /// The matrix to factor. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + public static UserCholesky Create(Matrix matrix) + { + // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). + var factor = matrix.Clone(); + DoCholesky(factor); return new UserCholesky(factor); } + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + matrix.CopyTo(Factor); + DoCholesky(Factor); + } + UserCholesky(Matrix factor) : base(factor) { diff --git a/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseCholesky.cs b/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseCholesky.cs index 787edf5a..439cf121 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseCholesky.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseCholesky.cs @@ -149,5 +149,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization var dfactor = (DenseMatrix) Factor; Control.LinearAlgebraProvider.CholeskySolveFactored(dfactor.Values, dfactor.RowCount, dresult.Values, 1); } + + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != matrix.ColumnCount) + { + throw new ArgumentException(Resources.ArgumentMatrixSquare); + } + + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + var dmatrix = matrix as DenseMatrix; + if (dmatrix == null) + { + throw new NotSupportedException("Can only do Cholesky factorization for dense matrices at the moment."); + } + + var dfactor = (DenseMatrix)Factor; + + // Overwrite the existing Factor matrix with the input. + Array.Copy(dmatrix.Values, 0, dfactor.Values, 0, dmatrix.Values.Length); + + // Perform factorization (while overwriting). + Control.LinearAlgebraProvider.CholeskyFactor(dfactor.Values, dfactor.RowCount); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Factorization/UserCholesky.cs b/src/Numerics/LinearAlgebra/Complex32/Factorization/UserCholesky.cs index 24e14c66..62aa849d 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Factorization/UserCholesky.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Factorization/UserCholesky.cs @@ -47,22 +47,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization internal sealed class UserCholesky : Cholesky { /// - /// Initializes a new instance of the class. This object will compute the - /// Cholesky factorization when the constructor is called and cache it's factorization. + /// Computes the Cholesky factorization in-place. /// - /// The matrix to factor. - /// If is null. - /// If is not a square matrix. - /// If is not positive definite. - public static UserCholesky Create(Matrix matrix) + /// On entry, the matrix to factor. On exit, the Cholesky factor matrix + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + static void DoCholesky(Matrix factor) { - if (matrix.RowCount != matrix.ColumnCount) + if (factor.RowCount != factor.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). - var factor = matrix.Clone(); var tmpColumn = new Complex32[factor.RowCount]; // Main loop - along the diagonal @@ -98,10 +96,43 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization factor.At(ij, i, Complex32.Zero); } } + } + /// + /// Initializes a new instance of the class. This object will compute the + /// Cholesky factorization when the constructor is called and cache it's factorization. + /// + /// The matrix to factor. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + public static UserCholesky Create(Matrix matrix) + { + // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). + var factor = matrix.Clone(); + DoCholesky(factor); return new UserCholesky(factor); } + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + matrix.CopyTo(Factor); + DoCholesky(Factor); + } + UserCholesky(Matrix factor) : base(factor) { diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs b/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs index a89aba69..6fc78b10 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs +++ b/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs @@ -147,5 +147,40 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization var dfactor = (DenseMatrix) Factor; Control.LinearAlgebraProvider.CholeskySolveFactored(dfactor.Values, dfactor.RowCount, dresult.Values, 1); } + + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != matrix.ColumnCount) + { + throw new ArgumentException(Resources.ArgumentMatrixSquare); + } + + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + var dmatrix = matrix as DenseMatrix; + if (dmatrix == null) + { + throw new NotSupportedException("Can only do Cholesky factorization for dense matrices at the moment."); + } + + var dfactor = (DenseMatrix)Factor; + + // Overwrite the existing Factor matrix with the input. + Buffer.BlockCopy(dmatrix.Values, 0, dfactor.Values, 0, dmatrix.Values.Length * Constants.SizeOfDouble); + + // Perform factorization (while overwriting). + Control.LinearAlgebraProvider.CholeskyFactor(dfactor.Values, dfactor.RowCount); + } } } diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/UserCholesky.cs b/src/Numerics/LinearAlgebra/Double/Factorization/UserCholesky.cs index 43f4fdeb..eed115b3 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/UserCholesky.cs +++ b/src/Numerics/LinearAlgebra/Double/Factorization/UserCholesky.cs @@ -45,22 +45,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization internal sealed class UserCholesky : Cholesky { /// - /// Initializes a new instance of the class. This object will compute the - /// Cholesky factorization when the constructor is called and cache it's factorization. + /// Computes the Cholesky factorization in-place. /// - /// The matrix to factor. - /// If is null. - /// If is not a square matrix. - /// If is not positive definite. - public static UserCholesky Create(Matrix matrix) + /// On entry, the matrix to factor. On exit, the Cholesky factor matrix + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + static void DoCholesky(Matrix factor) { - if (matrix.RowCount != matrix.ColumnCount) + if (factor.RowCount != factor.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } - // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). - var factor = matrix.Clone(); var tmpColumn = new double[factor.RowCount]; // Main loop - along the diagonal @@ -96,10 +93,43 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization factor.At(ij, i, 0.0); } } + } + /// + /// Initializes a new instance of the class. This object will compute the + /// Cholesky factorization when the constructor is called and cache it's factorization. + /// + /// The matrix to factor. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + public static UserCholesky Create(Matrix matrix) + { + // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). + var factor = matrix.Clone(); + DoCholesky(factor); return new UserCholesky(factor); } + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + matrix.CopyTo(Factor); + DoCholesky(Factor); + } + UserCholesky(Matrix factor) : base(factor) { diff --git a/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs b/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs index 0710efbd..fb55b537 100644 --- a/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs +++ b/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs @@ -64,6 +64,12 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// public abstract T DeterminantLn { get; } + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + public abstract void Factorize(Matrix matrix); + /// /// Solves a system of linear equations, AX = B, with A Cholesky factorized. /// diff --git a/src/Numerics/LinearAlgebra/Single/Factorization/DenseCholesky.cs b/src/Numerics/LinearAlgebra/Single/Factorization/DenseCholesky.cs index 2aee28d8..f7ca41dd 100644 --- a/src/Numerics/LinearAlgebra/Single/Factorization/DenseCholesky.cs +++ b/src/Numerics/LinearAlgebra/Single/Factorization/DenseCholesky.cs @@ -147,5 +147,40 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization var dfactor = (DenseMatrix) Factor; Control.LinearAlgebraProvider.CholeskySolveFactored(dfactor.Values, dfactor.RowCount, dresult.Values, 1); } + + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != matrix.ColumnCount) + { + throw new ArgumentException(Resources.ArgumentMatrixSquare); + } + + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + var dmatrix = matrix as DenseMatrix; + if (dmatrix == null) + { + throw new NotSupportedException("Can only do Cholesky factorization for dense matrices at the moment."); + } + + var dfactor = (DenseMatrix)Factor; + + // Overwrite the existing Factor matrix with the input. + Buffer.BlockCopy(dmatrix.Values, 0, dfactor.Values, 0, dmatrix.Values.Length * Constants.SizeOfFloat); + + // Perform factorization (while overwriting). + Control.LinearAlgebraProvider.CholeskyFactor(dfactor.Values, dfactor.RowCount); + } } } diff --git a/src/Numerics/LinearAlgebra/Single/Factorization/UserCholesky.cs b/src/Numerics/LinearAlgebra/Single/Factorization/UserCholesky.cs index aac3dab7..c83219b9 100644 --- a/src/Numerics/LinearAlgebra/Single/Factorization/UserCholesky.cs +++ b/src/Numerics/LinearAlgebra/Single/Factorization/UserCholesky.cs @@ -45,22 +45,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization internal sealed class UserCholesky : Cholesky { /// - /// Initializes a new instance of the class. This object will compute the - /// Cholesky factorization when the constructor is called and cache it's factorization. + /// Computes the Cholesky factorization in-place. /// - /// The matrix to factor. - /// If is null. - /// If is not a square matrix. - /// If is not positive definite. - public static UserCholesky Create(Matrix matrix) + /// On entry, the matrix to factor. On exit, the Cholesky factor matrix + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + static void DoCholesky(Matrix factor) { - if (matrix.RowCount != matrix.ColumnCount) + if (factor.RowCount != factor.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } - // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). - var factor = matrix.Clone(); var tmpColumn = new float[factor.RowCount]; // Main loop - along the diagonal @@ -96,10 +93,43 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization factor.At(ij, i, 0.0f); } } + } + /// + /// Initializes a new instance of the class. This object will compute the + /// Cholesky factorization when the constructor is called and cache it's factorization. + /// + /// The matrix to factor. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + public static UserCholesky Create(Matrix matrix) + { + // Create a new matrix for the Cholesky factor, then perform factorization (while overwriting). + var factor = matrix.Clone(); + DoCholesky(factor); return new UserCholesky(factor); } + /// + /// Calculates the Cholesky factorization of the input matrix. + /// + /// The matrix to be factorized. + /// If is null. + /// If is not a square matrix. + /// If is not positive definite. + /// If does not have the same dimensions as the existing factor. + public override void Factorize(Matrix matrix) + { + if (matrix.RowCount != Factor.RowCount || matrix.ColumnCount != Factor.ColumnCount) + { + throw Matrix.DimensionsDontMatch(matrix, Factor); + } + + matrix.CopyTo(Factor); + DoCholesky(Factor); + } + UserCholesky(Matrix factor) : base(factor) { diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Factorization/CholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/Factorization/CholeskyTests.cs index a61cf4f8..b878492d 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Factorization/CholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Factorization/CholeskyTests.cs @@ -143,6 +143,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization AssertHelpers.AlmostEqualRelative(matrixX[i, j], matrixXfromC[i, j], 8); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Factorization/UserCholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/Factorization/UserCholeskyTests.cs index 4dd7a73d..faba4eb3 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Factorization/UserCholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Factorization/UserCholeskyTests.cs @@ -142,6 +142,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization AssertHelpers.AlmostEqualRelative(matrixX[i, j], matrixXfromC[i, j], 8); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/CholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/CholeskyTests.cs index 9b8a2bff..a76c39ec 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/CholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/CholeskyTests.cs @@ -140,6 +140,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization Assert.AreEqual(matrixX[i, j].Imaginary, matrixXfromC[i, j].Imaginary, 1e-3f); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/UserCholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/UserCholeskyTests.cs index 59750cac..c76c804b 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/UserCholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Factorization/UserCholeskyTests.cs @@ -139,6 +139,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization Assert.AreEqual(matrixX[i, j].Imaginary, matrixXfromC[i, j].Imaginary, 1e-3f); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/Factorization/CholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Double/Factorization/CholeskyTests.cs index d12bb2b1..b2693805 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Factorization/CholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Factorization/CholeskyTests.cs @@ -218,6 +218,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(row, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(row+1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/Factorization/UserCholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Double/Factorization/UserCholeskyTests.cs index 6d292099..b3b4bae3 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Factorization/UserCholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Factorization/UserCholeskyTests.cs @@ -136,6 +136,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization Assert.AreEqual(matrixX[i, j], matrixXfromC[i, j], 1.0e-11); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/Factorization/CholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Single/Factorization/CholeskyTests.cs index 5555a76b..12dffa5e 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Factorization/CholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Factorization/CholeskyTests.cs @@ -137,6 +137,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization Assert.AreEqual(matrixX[i, j], matrixXfromC[i, j], 1e-3); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/Factorization/UserCholeskyTests.cs b/src/UnitTests/LinearAlgebraTests/Single/Factorization/UserCholeskyTests.cs index ed6f0d94..d225442e 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Factorization/UserCholeskyTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Factorization/UserCholeskyTests.cs @@ -136,6 +136,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization Assert.AreEqual(matrixX[i, j], matrixXfromC[i, j], 1e-3); } } + + // Check update + var matrixC = Matrix.Build.RandomPositiveDefinite(order, 1); + var cholC = matrixC.Cholesky(); + chol.Factorize(matrixC); + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + Assert.AreEqual(cholC.Factor[i, j], chol.Factor[i, j]); + } + } + + // Check size mismatch + var matrixD = Matrix.Build.DenseIdentity(order + 1); + Assert.That(() => chol.Factorize(matrixD), Throws.ArgumentException); } ///