Ejemplo n.º 1
0
        /// <summary>
        /// Gets a matrix determinant.
        /// </summary>
        /// <returns>
        /// Returns the matrix determinant.
        /// </returns>
        public double Determinant()
        {
            if (!IsSquare)
            {
                throw new NonSquareMatrixException("Attempt to find a determinant of a non square matrix");
            }

            if (Rows == 2 && Columns == 2)
            {
                return(Determinant2x2());
            }

            if (Rows == 3 && Columns == 3)
            {
                return(Determinant3x3());
            }

            double determinant = 1;

            var luDecomposition = new LupDecomposition();

            var(c, _) = luDecomposition.Decompose(this);

            for (int i = 0; i < c.Rows; i++)
            {
                determinant *= c[i, i];
            }

            return(determinant);
        }
Ejemplo n.º 2
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 LupDecomposition();

            var(c, p) = decomposition.Decompose(factor);

            var y = SolveLower(c, p * right);
            var x = SolveUpper(c, y);

            return(x);
        }
Ejemplo n.º 3
0
        public void LupDecomposition_CalculateLUMatrices_ReturnsAsExpected()
        {
            var lupDecomposition = new LupDecomposition();
            var matrix           = new Matrix(4, 4,
                                              new double[, ] {
                { 2, 1, 1, 0 }, { 4, 3, 3, 1 }, { 8, 7, 9, 5 }, { 6, 7, 9, 8 }
            });

            var expectedC = new Matrix(4, 4,
                                       new double[, ] {
                { 8, 7, 9, 5 }, { 0.75, 1.75, 2.25, 4.25 }, { 0.5, -0.28571, -0.85714, -0.28571 }, { 0.25, -0.42857, 0.33333, 0.66667 }
            });
            var expectedP = new Matrix(4, 4,
                                       new double[, ] {
                { 0, 0, 1, 0 }, { 0, 0, 0, 1 }, { 0, 1, 0, 0 }, { 1, 0, 0, 0 }
            });

            var(c, p) = lupDecomposition.Decompose(matrix);

            Assert.That(c.Cast <double>(), Is.EqualTo(expectedC.Cast <double>()).Within(0.00001));
            Assert.AreEqual(expectedP, p);
        }