From 3405d9339f1d5f23877a9e2ff1cd2ecbb17be0b0 Mon Sep 17 00:00:00 2001 From: Jurgen Van Gael Date: Sun, 16 May 2010 17:41:39 +0800 Subject: [PATCH] Added Cholesky solve. Updated T3 to use .NET 4.0. --- .../Atlas/AtlasLinearAlgebraProvider.cs | 70 +++++---- .../Atlas/AtlasLinearAlgebraProvider.tt | 2 +- .../LinearAlgebra/Atlas/SafeNativeMethods.cs | 5 +- .../LinearAlgebra/Atlas/SafeNativeMethods.tt | 2 +- .../ILinearAlgebraProviderOfT.cs | 14 +- .../ManagedLinearAlgebraProvider.cs | 117 +++++++++++++-- .../Mkl/MklLinearAlgebraProvider.cs | 70 +++++---- .../Mkl/MklLinearAlgebraProvider.tt | 2 +- .../LinearAlgebra/Mkl/SafeNativeMethods.cs | 5 +- .../LinearAlgebra/Mkl/SafeNativeMethods.tt | 2 +- .../NativeAlgebraProvider.include | 71 +++++---- .../LinearAlgebra/SafeNativeMethods.include | 1 + .../Double/Factorization/Cholesky.cs | 28 ++++ .../Double/Factorization/DenseCholesky.cs | 139 ++++++++++++++++++ src/Numerics/Numerics.csproj | 13 +- 15 files changed, 430 insertions(+), 111 deletions(-) diff --git a/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.cs b/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.cs index 316bc6ae..4b1bdb91 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.cs @@ -24,12 +24,12 @@ /* This file is automatically generated - do not modify it. Change NativeLinearAlgebraProvider.include instead. - Last generated on: 4/23/2010 10:04:38 AM + Last generated on: 16/05/2010 10:39:25 */ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas { using System; - using System.Numerics; + using System.Numerics; using Properties; /// @@ -458,11 +458,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, double[] a, double[] b) + public void CholeskySolve(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -470,11 +472,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, double[] a, double[] b) + public void CholeskySolveFactored(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1050,15 +1054,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas SafeNativeMethods.s_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, float[] a, float[] b) + public void CholeskySolve(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1066,11 +1072,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, float[] a, float[] b) + public void CholeskySolveFactored(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1276,7 +1284,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas if (x == null) { throw new ArgumentNullException("x"); - } + } if (alpha.IsOne()) { @@ -1646,15 +1654,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas SafeNativeMethods.z_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, Complex[] a, Complex[] b) + public void CholeskySolve(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1662,11 +1672,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, Complex[] a, Complex[] b) + public void CholeskySolveFactored(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1872,7 +1884,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas if (x == null) { throw new ArgumentNullException("x"); - } + } if (alpha.IsOne()) { @@ -2242,15 +2254,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas SafeNativeMethods.c_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, Complex32[] a, Complex32[] b) + public void CholeskySolve(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -2258,11 +2272,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, Complex32[] a, Complex32[] b) + public void CholeskySolveFactored(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } diff --git a/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.tt b/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.tt index 00754c05..c355ef98 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.tt +++ b/src/Numerics/Algorithms/LinearAlgebra/Atlas/AtlasLinearAlgebraProvider.tt @@ -1,4 +1,4 @@ -<#@ template language="C#v3.5" debug="true" #> +<#@ template language="C#" debug="true" #> <#@ output extenstion="cs" #> <# string library = "Atlas";#> <#@ include file="..\NativeAlgebraProvider.include" #> \ No newline at end of file diff --git a/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.cs b/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.cs index 7a8fd4e4..8834f362 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.cs @@ -28,16 +28,15 @@ /* This file is automatically generated - do not modify it. Change SafeNativeMethods.include instead. - Last generated on: 4/23/2010 10:04:39 AM + Last generated on: 16/05/2010 10:38:42 */ +using System.Numerics; using System.Runtime.InteropServices; using System.Security; namespace MathNet.Numerics.Algorithms.LinearAlgebra.Atlas { - using System.Numerics; - /// /// P/Invoke methods to the native math libraries. /// diff --git a/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.tt b/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.tt index 3aced9d3..7a0c802b 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.tt +++ b/src/Numerics/Algorithms/LinearAlgebra/Atlas/SafeNativeMethods.tt @@ -1,4 +1,4 @@ -<#@ template language="C#v3.5" debug="true" #> +<#@ template language="C#" debug="true" #> <#@ output extenstion="cs" #> <# string namespaceSuffix = "Atlas"; string library = "ATLAS"; diff --git a/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs b/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs index f1e8736c..8bf94015 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs @@ -307,20 +307,24 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - void CholeskySolve(int columnsOfB, T[] a, T[] b); + void CholeskySolve(T[] a, int aOrder, T[] b, int bRows, int bColumns); /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - void CholeskySolveFactored(int columnsOfB, T[] a, T[] b); + void CholeskySolveFactored(T[] a, int aOrder, T[] b, int bRows, int bColumns); /// /// Computes the QR factorization of A. diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs index 3ed52ca9..43692fdf 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs @@ -776,14 +776,59 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } } - public void CholeskySolve(int columnsOfB, double[] a, double[] b) + /// + /// Solves A*X=B for X using Cholesky factorization. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRF add POTRS LAPACK routines. + public void CholeskySolve(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } - public void CholeskySolveFactored(int columnsOfB, double[] a, double[] b) + /// + /// Solves A*X=B for X using a previously factored A matrix. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRS LAPACK routine. + public void CholeskySolveFactored(double[] a, int aOrder, double[] b, int bRows, int bColumns) { - throw new NotImplementedException(); + for (int c = 0; c < bColumns; c++) + { + int cindex = c * aOrder; + + // Solve L*Y = B; + double sum; + for (int i = 0; i < aOrder; i++) + { + sum = b[c * aOrder + i]; + for (int k = i - 1; k >= 0; k--) + { + sum -= a[k * aOrder + i] * b[cindex + k]; + } + b[cindex + i] = sum / a[i * aOrder + i]; + } + + // Solve L'*X = Y; + for (int i = aOrder - 1; i >= 0; i--) + { + sum = b[cindex + i]; + int iindex = i * aOrder; + for (int k = i + 1; k < aOrder; k++) + { + sum -= a[iindex + k] * b[cindex + k]; + } + b[cindex + i] = sum / a[iindex + i]; + } + } } public void QRFactor(double[] r, double[] q) @@ -1575,12 +1620,30 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra Buffer.BlockCopy(factor, 0, a, 0, factor.Length * Constants.SizeOfFloat); } - public void CholeskySolve(int columnsOfB, float[] a, float[] b) + /// + /// Solves A*X=B for X using Cholesky factorization. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRF add POTRS LAPACK routines. + public void CholeskySolve(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } - public void CholeskySolveFactored(int columnsOfB, float[] a, float[] b) + /// + /// Solves A*X=B for X using a previously factored A matrix. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRS LAPACK routine. + public void CholeskySolveFactored(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -2339,12 +2402,30 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new NotImplementedException(); } - public void CholeskySolve(int columnsOfB, Complex[] a, Complex[] b) + /// + /// Solves A*X=B for X using Cholesky factorization. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRF add POTRS LAPACK routines. + public void CholeskySolve(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } - public void CholeskySolveFactored(int columnsOfB, Complex[] a, Complex[] b) + /// + /// Solves A*X=B for X using a previously factored A matrix. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRS LAPACK routine. + public void CholeskySolveFactored(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -3103,12 +3184,30 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new NotImplementedException(); } - public void CholeskySolve(int columnsOfB, Complex32[] a, Complex32[] b) + /// + /// Solves A*X=B for X using Cholesky factorization. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRF add POTRS LAPACK routines. + public void CholeskySolve(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } - public void CholeskySolveFactored(int columnsOfB, Complex32[] a, Complex32[] b) + /// + /// Solves A*X=B for X using a previously factored A matrix. + /// + /// The square, positive definite matrix A. + /// The number of rows and columns in A. + /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. + /// This is equivalent to the POTRS LAPACK routine. + public void CholeskySolveFactored(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } diff --git a/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs b/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs index 1422bde4..d773e1b7 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs @@ -24,12 +24,12 @@ /* This file is automatically generated - do not modify it. Change NativeLinearAlgebraProvider.include instead. - Last generated on: 4/23/2010 10:04:40 AM + Last generated on: 16/05/2010 10:39:49 */ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl { using System; - using System.Numerics; + using System.Numerics; using Properties; /// @@ -457,11 +457,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, double[] a, double[] b) + public void CholeskySolve(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -469,11 +471,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, double[] a, double[] b) + public void CholeskySolveFactored(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1049,15 +1053,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl SafeNativeMethods.s_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, float[] a, float[] b) + public void CholeskySolve(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1065,11 +1071,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, float[] a, float[] b) + public void CholeskySolveFactored(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1275,7 +1283,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl if (x == null) { throw new ArgumentNullException("x"); - } + } if (alpha.IsOne()) { @@ -1645,15 +1653,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl SafeNativeMethods.z_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, Complex[] a, Complex[] b) + public void CholeskySolve(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1661,11 +1671,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, Complex[] a, Complex[] b) + public void CholeskySolveFactored(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1871,7 +1883,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl if (x == null) { throw new ArgumentNullException("x"); - } + } if (alpha.IsOne()) { @@ -2241,15 +2253,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl SafeNativeMethods.c_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, Complex32[] a, Complex32[] b) + public void CholeskySolve(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -2257,11 +2271,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, Complex32[] a, Complex32[] b) + public void CholeskySolveFactored(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } diff --git a/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.tt b/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.tt index 2eb924ad..2f51b3d9 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.tt +++ b/src/Numerics/Algorithms/LinearAlgebra/Mkl/MklLinearAlgebraProvider.tt @@ -1,4 +1,4 @@ -<#@ template language="C#v3.5" debug="true" #> +<#@ template language="C#" debug="true" #> <#@ output extenstion="cs" #> <# string library = "Mkl";#> <#@ include file="..\NativeAlgebraProvider.include" #> \ No newline at end of file diff --git a/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.cs b/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.cs index 09aa5d19..989353ab 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.cs @@ -28,16 +28,15 @@ /* This file is automatically generated - do not modify it. Change SafeNativeMethods.include instead. - Last generated on: 4/23/2010 10:04:40 AM + Last generated on: 16/05/2010 10:38:47 */ +using System.Numerics; using System.Runtime.InteropServices; using System.Security; namespace MathNet.Numerics.Algorithms.LinearAlgebra.Mkl { - using System.Numerics; - /// /// P/Invoke methods to the native math libraries. /// diff --git a/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.tt b/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.tt index 27578188..75b9f980 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.tt +++ b/src/Numerics/Algorithms/LinearAlgebra/Mkl/SafeNativeMethods.tt @@ -1,4 +1,4 @@ -<#@ template language="C#v3.5" debug="true" #> +<#@ template language="C#" debug="true" #> <#@ output extenstion="cs" #> <# string namespaceSuffix = "Mkl"; string library = "MKL"; diff --git a/src/Numerics/Algorithms/LinearAlgebra/NativeAlgebraProvider.include b/src/Numerics/Algorithms/LinearAlgebra/NativeAlgebraProvider.include index d770143d..f1a2c689 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/NativeAlgebraProvider.include +++ b/src/Numerics/Algorithms/LinearAlgebra/NativeAlgebraProvider.include @@ -29,6 +29,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> { using System; + using System.Numerics; using Properties; /// @@ -471,11 +472,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, double[] a, double[] b) + public void CholeskySolve(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -483,11 +486,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, double[] a, double[] b) + public void CholeskySolveFactored(double[] a, int aOrder, double[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1075,15 +1080,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> SafeNativeMethods.s_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, float[] a, float[] b) + public void CholeskySolve(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1091,11 +1098,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, float[] a, float[] b) + public void CholeskySolveFactored(float[] a, int aOrder, float[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1282,7 +1291,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (alpha.IsZero) + if (alpha.IsZero()) { return; } @@ -1303,7 +1312,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> throw new ArgumentNullException("x"); } - if (alpha.IsOne) + if (alpha.IsOne()) { return; } @@ -1683,15 +1692,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> SafeNativeMethods.z_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, Complex[] a, Complex[] b) + public void CholeskySolve(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1699,11 +1710,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, Complex[] a, Complex[] b) + public void CholeskySolveFactored(Complex[] a, int aOrder, Complex[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -1890,7 +1903,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (alpha.IsZero) + if (alpha.IsZero()) { return; } @@ -1911,7 +1924,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> throw new ArgumentNullException("x"); } - if (alpha.IsOne) + if (alpha.IsOne()) { return; } @@ -2291,15 +2304,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> SafeNativeMethods.c_cholesky_factor(order, a); } - + /// /// Solves A*X=B for X using Cholesky factorization. /// - /// The number of columns of B. /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRF add POTRS LAPACK routines. - public void CholeskySolve(int columnsOfB, Complex32[] a, Complex32[] b) + public void CholeskySolve(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } @@ -2307,11 +2322,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra.<#=library#> /// /// Solves A*X=B for X using a previously factored A matrix. /// - /// The number of columns of B. - /// The factored A matrix. + /// The square, positive definite matrix A. + /// The number of rows and columns in A. /// The B matrix. + /// The number of rows in the B matrix. + /// The number of columns in the B matrix. /// This is equivalent to the POTRS LAPACK routine. - public void CholeskySolveFactored(int columnsOfB, Complex32[] a, Complex32[] b) + public void CholeskySolveFactored(Complex32[] a, int aOrder, Complex32[] b, int bRows, int bColumns) { throw new NotImplementedException(); } diff --git a/src/Numerics/Algorithms/LinearAlgebra/SafeNativeMethods.include b/src/Numerics/Algorithms/LinearAlgebra/SafeNativeMethods.include index 115ea85b..93039291 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/SafeNativeMethods.include +++ b/src/Numerics/Algorithms/LinearAlgebra/SafeNativeMethods.include @@ -31,6 +31,7 @@ Last generated on: <#=DateTime.Now.ToString()#> */ +using System.Numerics; using System.Runtime.InteropServices; using System.Security; diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/Cholesky.cs b/src/Numerics/LinearAlgebra/Double/Factorization/Cholesky.cs index 47e0c652..02f8839b 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/Cholesky.cs +++ b/src/Numerics/LinearAlgebra/Double/Factorization/Cholesky.cs @@ -104,5 +104,33 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization return det; } } + + /// + /// Solves a system of linear equations, AX = B, with A Cholesky factorized. + /// + /// The right hand side , B. + /// The left hand side , X. + public abstract Matrix Solve(Matrix input); + + /// + /// Solves a system of linear equations, AX = B, with A Cholesky factorized. + /// + /// The right hand side , B. + /// The left hand side , X. + public abstract void Solve(Matrix input, Matrix result); + + /// + /// Solves a system of linear equations, Ax = b, with A Cholesky factorized. + /// + /// The right hand side vector, b. + /// The left hand side , x. + public abstract Vector Solve(Vector input); + + /// + /// Solves a system of linear equations, Ax = b, with A Cholesky factorized. + /// + /// The right hand side vector, b. + /// The left hand side , x. + public abstract void Solve(Vector input, Vector result); } } diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs b/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs index ad1e1771..eafbbc83 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs +++ b/src/Numerics/LinearAlgebra/Double/Factorization/DenseCholesky.cs @@ -69,5 +69,144 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization Control.LinearAlgebraProvider.CholeskyFactor(factor.Data, factor.RowCount); mFactor = factor; } + + /// + /// Solves a system of linear equations, AX = B, with A Cholesky factorized. + /// + /// The right hand side , B. + /// The left hand side , X. + public override Matrix Solve(Matrix input) + { + // Check for proper arguments. + if (input == null) + { + throw new ArgumentNullException("input"); + } + + var X = new DenseMatrix(input.RowCount, input.ColumnCount); + Solve(input, X); + return X; + } + + /// + /// Solves a system of linear equations, AX = B, with A Cholesky factorized. + /// + /// The right hand side , B. + /// The left hand side , X. + public override void Solve(Matrix input, Matrix result) + { + // Check for proper arguments. + if (input == null) + { + throw new ArgumentNullException("input"); + } + + if (result == null) + { + throw new ArgumentNullException("result"); + } + + // Check for proper dimensions. + if (result.RowCount != input.RowCount) + { + throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); + } + + if (result.ColumnCount != input.ColumnCount) + { + throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); + } + + if (input.RowCount != mFactor.RowCount) + { + throw new ArgumentException(Resources.ArgumentMatrixDimensions); + } + + var dinput = input as DenseMatrix; + if (dinput == null) + { + throw new NotImplementedException("Can only do Cholesky factorization for dense matrices at the moment."); + } + + var dresult = input as DenseMatrix; + if (dresult == null) + { + throw new NotImplementedException("Can only do Cholesky factorization for dense matrices at the moment."); + } + + // Copy the contents of input to result. + Buffer.BlockCopy(dinput.Data, 0, dresult.Data, 0, dinput.Data.Length * Constants.SizeOfDouble); + + // Cholesky solve by overwriting result. + var dfactor = mFactor as DenseMatrix; + Control.LinearAlgebraProvider.CholeskySolveFactored(dfactor.Data, dfactor.RowCount, dresult.Data, dresult.RowCount, dresult.ColumnCount); + } + + /// + /// Solves a system of linear equations, Ax = b, with A Cholesky factorized. + /// + /// The right hand side vector, b. + /// The left hand side , x. + public override Vector Solve(Vector input) + { + // Check for proper arguments. + if (input == null) + { + throw new ArgumentNullException("input"); + } + + var x = new DenseVector(input.Count); + Solve(input, x); + return x; + } + + /// + /// Solves a system of linear equations, Ax = b, with A Cholesky factorized. + /// + /// The right hand side vector, b. + /// The left hand side , x. + public override void Solve(Vector input, Vector result) + { + // Check for proper arguments. + if (input == null) + { + throw new ArgumentNullException("input"); + } + + if (result == null) + { + throw new ArgumentNullException("result"); + } + + // Check for proper dimensions. + if (input.Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + if (input.Count != mFactor.RowCount) + { + throw new ArgumentException(Resources.ArgumentMatrixDimensions); + } + + var dinput = input as DenseVector; + if (dinput == null) + { + throw new NotImplementedException("Can only do Cholesky factorization for dense vectors at the moment."); + } + + var dresult = input as DenseVector; + if (dresult == null) + { + throw new NotImplementedException("Can only do Cholesky factorization for dense vectors at the moment."); + } + + // Copy the contents of input to result. + Buffer.BlockCopy(dinput.Data, 0, dresult.Data, 0, dinput.Data.Length * Constants.SizeOfDouble); + + // Cholesky solve by overwriting result. + var dfactor = mFactor as DenseMatrix; + Control.LinearAlgebraProvider.CholeskySolveFactored(dfactor.Data, dfactor.RowCount, dresult.Data, dresult.Count, 1); + } } } diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 6aab4efb..2c45494a 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -77,14 +77,19 @@ True True + + TextTemplatingFileGenerator + MklLinearAlgebraProvider.cs + - True - True MklLinearAlgebraProvider.tt + Code + True + True True @@ -205,10 +210,6 @@ TextTemplatingFileGenerator SafeNativeMethods.cs - - TextTemplatingFileGenerator - MklLinearAlgebraProvider.cs - TextTemplatingFileGenerator SafeNativeMethods.cs