Exemple #1
0
        /// <summary>
        /// Solves a system of linear equations, <b>Ax = b</b>.
        /// </summary>
        /// <param name="factor">A coefficient matrix A.</param>
        /// <param name="right">A right-hand vector b.</param>
        /// <returns>
        /// Returns a vector x that is a solution of the system of linear equations, <b>Ax = b</b>.
        /// </returns>
        public Vector Solve(Matrix factor, Vector right)
        {
            if (factor == null)
            {
                throw new ArgumentNullException("Factor matrix cannot be null.");
            }

            if (right == null)
            {
                throw new ArgumentNullException("Right vector cannot be null.");
            }

            if (factor.Rows != right.Size)
            {
                throw new ArgumentException("Factor matrix and right vector have inconsistence size.");
            }

            var decomposition = new CholeskyDecomposition();

            var(l, lt) = decomposition.Decompose(factor);

            var y = SolveLower(l, right);
            var x = SolveUpper(lt, y);

            return(x);
        }
        public void CholeskyDecomposition_CalculateLLtMatrices_ReturnsAsExpected()
        {
            var choleskyDecomposition = new CholeskyDecomposition();
            var matrix = new Matrix(3, 3, new double[, ] {
                { 81, -45, 45 }, { -45, 50, -15 }, { 45, -15, 38 }
            });

            var expectedL = new Matrix(3, 3, new double[, ] {
                { 9, 0, 0 }, { -5, 5, 0 }, { 5, 2, 3 }
            });
            var expectedLt = new Matrix(3, 3, new double[, ] {
                { 9, -5, 5 }, { 0, 5, 2 }, { 0, 0, 3 }
            });

            var(l, lt) = choleskyDecomposition.Decompose(matrix);

            Assert.AreEqual(expectedL, l);
            Assert.AreEqual(expectedLt, lt);
        }