Exemple #1
0
        public void TestSolve()
        {
            /* Solve the equations A*X = B */
            // https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesv_ex.c.htm
            const int N = 5;
            const int NRHS = 3;
            const int LDA = N;
            const int LDB = NRHS;
            int       n = N, nrhs = NRHS, lda = LDA, ldb = LDB;

            /* Local arrays */
            int[]    ipiv = new int[N];
            double[] a    = new double[N * N] {
                6.80, -6.05, -0.45, 8.32, -9.67,
                -2.11, -3.30, 2.58, 2.71, -5.14,
                5.66, 5.36, -2.70, 4.35, -7.26,
                5.97, -4.44, 0.27, -7.17, 6.08,
                8.23, 1.08, 9.04, 2.14, -6.87
            };
            double[] b = new double[N * NRHS] {
                4.02, -1.56, 9.81,
                6.19, 4.00, -4.09,
                -8.22, -8.67, -4.57,
                -7.57, 1.75, -8.61,
                -3.03, 2.86, 8.99
            };
            /* Solve the equations A*X = B */
            Lapack.gesv(n, nrhs, a, lda, ipiv, b, ldb);

            // Solution
            var solution = NN.Array(new[] {
                -0.80, -0.39, 0.96,
                -0.70, -0.55, 0.22,
                0.59, 0.84, 1.90,
                1.32, -0.10, 5.36,
                0.57, 0.11, 4.04,
            }).Reshape(n, nrhs);

            AssertArray.AreAlmostEqual(solution, NN.Array(b).Reshape(N, NRHS), 1e-2, 1e-2);

            // Details of LU factorization
            var luFactorization = NN.Array(new[]
            {
                8.23, 1.08, 9.04, 2.14, -6.87,
                0.83, -6.94, -7.92, 6.55, -3.99,
                0.69, -0.67, -14.18, 7.24, -5.19,
                0.73, 0.75, 0.02, -13.82, 14.19,
                -0.26, 0.44, -0.59, -0.34, -3.43,
            }).Reshape(n, n);

            AssertArray.AreAlmostEqual(luFactorization, NN.Array(a).Reshape(n, n), 1e-2, 1e-2);

            // Pivot indices
            AssertArray.AreEqual(new[] { 5, 5, 3, 4, 5 }, ipiv);
        }