예제 #1
0
        /// <summary>
        /// AugmentedMatrixCheck Method returning an the solution state of the equation
        /// </summary>
        /// <returns></returns>
        private SolutionState AugmentedMatrixCheck()
        {
            SolutionState   state = SolutionState.Infinite;
            Matrix <double> temp  = AugmentedMatrix.Copy(AugmentedMatrix.RowCount, AugmentedMatrix.ColumnCount);

            for (int i = 0; i < AugmentedMatrix.RowCount; i++)
            {
                for (int j = i + 1; j < AugmentedMatrix.RowCount; j++)
                {
                    for (int k = i; k < AugmentedMatrix.ColumnCount; k++)
                    {
                        AugmentedMatrix[j][k] += ((-temp[j][i] / temp[i][i]) * temp[i][k]);
                    }
                }
                temp = AugmentedMatrix.Copy(temp.RowCount, temp.ColumnCount);
            }

            for (int i = 0; i < temp.ColumnCount; i++)
            {
                if (temp[temp.RowCount - 1][temp.ColumnCount - 1] != 0)
                {
                    state = SolutionState.NoSolution;
                }
            }

            return(state);
        }
예제 #2
0
        public static void TestAlgebra()
        {
            RationalNumberMatrix rat1 = new RationalNumberMatrix(2, 2);

            rat1.Replace(0, 0, new RationalNumber(3, 4));
            rat1.Replace(0, 1, new RationalNumber(4, 3));
            rat1.Replace(1, 0, new RationalNumber(2, 5));
            rat1.Replace(1, 1, new RationalNumber(1, 7));
            RationalNumberColumnVector col1 = new RationalNumberColumnVector(2);

            col1.Replace(0, 0, new RationalNumber(1, 1));
            col1.Replace(1, 0, new RationalNumber(1, 2));
            Console.WriteLine(rat1.ToString());
            AugmentedMatrix aug1 = new AugmentedMatrix(rat1, col1);

            Console.WriteLine(aug1.ToString());
            RationalNumber r1 = new RationalNumber(3, 4);
            RationalNumber r2 = new RationalNumber(5, 6);

            RationalNumber[] crosses = RationalNumber.CrossMultiply(r1, r2);
            foreach (RationalNumber ra in crosses)
            {
                //Console.WriteLine(ra);
            }
            //RationalNumberRowVector toRow = aug1.SolveRow(0, 1, 1);
            //Console.WriteLine(toRow);
            //AugmentedMatrix aug2 = aug1.GaussMatrix();
            //Console.WriteLine(aug2);
            RationalNumber[]           rary1     = { new RationalNumber(9), new RationalNumber(3), new RationalNumber(4) };
            RationalNumber[]           rary2     = { new RationalNumber(4), new RationalNumber(3), new RationalNumber(4) };
            RationalNumber[]           rary3     = { new RationalNumber(1), new RationalNumber(1), new RationalNumber(1) };
            RationalNumber[]           rcol1     = { new RationalNumber(7), new RationalNumber(8), new RationalNumber(3) };
            RationalNumberRowVector    rowv1     = new RationalNumberRowVector(rary1);
            RationalNumberRowVector    rowv2     = new RationalNumberRowVector(rary2);
            RationalNumberRowVector    rowv3     = new RationalNumberRowVector(rary3);
            RationalNumberColumnVector colv1     = new RationalNumberColumnVector(rcol1);
            RationalNumberMatrix       asdf      = new RationalNumberMatrix(new RationalNumberRowVector[] { rowv1, rowv2, rowv3 });
            AugmentedMatrix            aug3      = new AugmentedMatrix(asdf, colv1);
            AugmentedMatrix            aug3Gauss = aug3.GaussMatrix();

            Console.WriteLine(aug3);
            Console.WriteLine(aug3Gauss);
        }
예제 #3
0
        public void AugmentedReducedRowEchelonFormTest()
        {
            var A = new Matrix(new double[, ] {
                { 1, 2, 3 }, { 0, 1, 4 }, { 5, 6, 0 }
            });
            var I     = A.GetIdentity();
            var aug   = new AugmentedMatrix(new Matrix[] { A, I });
            var augGE = aug.ReducedRowEchelonForm();

            var identity = new Matrix(new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
            });
            var inverse = new Matrix(new double[, ] {
                { -24, 18, 5 }, { 20, -15, -4 }, { -5, 4, 1 }
            });

            Assert.That(augGE[0], Is.EqualTo(identity));
            Assert.That(augGE[1], Is.EqualTo(inverse));

            var B = new Matrix(new double[, ] {
                { 1, 2, 3, 6 }, { 0, 1, 4, 8 }, { 5, 6, 0, 9 }, { 9, 4, 7, 3 }
            });

            I = new Matrix(4, 4).GetIdentity();
            var aug2     = new AugmentedMatrix(new Matrix[] { B, I });
            var augRref2 = aug2.ReducedRowEchelonForm();

            var identity2 = new Matrix(4, 4).GetIdentity();
            var inverse2  =
                new Matrix(
                    new double[, ]
            {
                { -69.0 / 67.0, 36.0 / 67.0, 11.0 / 67.0, 9.0 / 67.0 },
                { 544.0 / 335.0, -69.0 / 67, -44.0 / 335.0, -36.0 / 335.0 },
                { 206.0 / 335.0, -18.0 / 67.0, -61.0 / 335.0, 11.0 / 335.0 },
                { -171.0 / 335.0, 26.0 / 67.0, 36.0 / 335.0, -1.0 / 335.0 }
            });

            Assert.That(augRref2[0], Is.EqualTo(identity2));
            Assert.That(augRref2[1], Is.EqualTo(inverse2));
        }
예제 #4
0
        public void Gaussian()
        {
            for (int i = 0; i < M; i++)
            {
                // get the index of the leading variable
                var j = 0;
                var r = AugmentedMatrix.GetRow(i);
                while (r[j].Equals(0))
                {
                    j++;
                }

                var source = AugmentedMatrix[i, j];
                // check if the leading var of the row is one
                if (!source.Equals(1))
                {
                    // if not, factor the row by itself with a factor of 1 / leading var
                    FactorRow(i, i, source);
                }
                // reassign source incase it changed, we should now have a leading factor of 1
                source = AugmentedMatrix[i, j];
                var clean = CleanColumn(AugmentedMatrix, i, j);
                if (!clean)
                {
                    for (int p = i + 1; p < M; p++)
                    {
                        if (!AugmentedMatrix[p, j].Equals(0))
                        {
                            var target = AugmentedMatrix[p, j];
                            var factor = source / target;
                            FactorRow(i, p, factor);
                        }
                    }
                }
            }
        }