/// <summary> /// Solves the system of equations for right hand side vector B. /// </summary> /// <param name="B"></param> /// <param name="X"></param> public void SolveLU(Vector B, Vector X) { if (_LU != null) { // Make a copy of B on X: for (int i = 0; i < B.Length; i++) { X[i] = B[i]; } // Solve the system A*X = B, overwriting B with X: if (UseIntelMathKernel) { double[] _X = X.Data; char[] trans = "No transponse".ToCharArray(); int one = 1; IntelMathKernel.dgetrs(trans, ref _N, ref one, _LU, ref _N, _IPIV, _X, ref _N, ref _Info); } else { DGETRS _dgetrs = new DGETRS(); double[] _X = X.Data; _dgetrs.Run("No transpose", _N, 1, _LU, 0, _N, _IPIV, 0, ref _X, 0, _N, ref _Info); } } }
/// <summary> /// Computes the LU and caches the results locally. /// Either the native .NET library or Intel MKL is used. /// </summary> /// <param name="A"></param> public void ComputeLU(Matrix A) { _N = A.RowCount; _LU = (double[])A.Data.Clone(); _IPIV = new int[_N]; _Info = 0; if (UseIntelMathKernel) { IntelMathKernel.dgetrf(ref _N, ref _N, _LU, ref _N, _IPIV, ref _Info); } else { DGETRF _dgetrf = new DGETRF(); _dgetrf.Run(_N, _N, ref _LU, 0, _N, ref _IPIV, 0, ref _Info); } }
public static bool Start() { Console.WriteLine("Testing LU decomposition"); int n = 3; int info = 0; int[] ipiv = new int[3]; Matrix Mtx = new Matrix(3); Mtx[0, 0] = 1; Mtx[0, 1] = 9; Mtx[0, 2] = 5; Mtx[1, 0] = 1; Mtx[1, 1] = 3; Mtx[1, 2] = 4; Mtx[2, 0] = 5; Mtx[2, 1] = 6; Mtx[2, 2] = 7; try { // Case 1 Console.WriteLine("Computing LU with MKL dgetrf..."); double[] A = (double[])Mtx.Data.Clone(); IntelMathKernel.dgetrf(ref n, ref n, A, ref n, ipiv, ref info); // Case 2 Console.WriteLine("Computing LU with with .NET dgetrf..."); double[] B = (double[])Mtx.Data.Clone(); DGETRF _dgetrf = new DGETRF(); _dgetrf.Run(n, n, ref B, 0, n, ref ipiv, 0, ref info); Console.WriteLine("Comparing results..."); for (int i = 0; i < 5; i++) { if (A[i] != B[i] || (B[i] > 0.0 & (A[i] - B[i]) / B[i] > 1E-5)) { Console.WriteLine("TEST NOT PASSED"); return(false); } } // Case 3 Console.WriteLine("Solving linear equations with MKL dgetrs..."); info = 0; int nrhs = 1; double[] R = new double[3]; R[0] = 1; R[1] = 2; R[2] = 5; char[] trans = "No transponse".ToCharArray(); IntelMathKernel.dgetrs(trans, ref n, ref nrhs, A, ref n, ipiv, R, ref n, ref info); Console.WriteLine("TEST PASSED"); return(true); } catch { Console.WriteLine("TEST NOT PASSED"); return(false); } }