Beispiel #1
0
        //row operations: multiply a row by a constant, add a multiple of a row to another, swap rows
        public AugmentedMatrix GaussMatrix()
        {
            if (RowSize == 1 && ColumnSize == 2)
            {
                return(this.GenerateClone());                                 //already solved
            }
            //break down into solving two rows
            AugmentedMatrix copy = this.GenerateClone();

            for (int k = 0; k < RowSize; k++)
            {
                for (int i = k + 1; i < RowSize; i++)
                {
                    RationalNumber[] orig = GetRow(i).ToArray <RationalNumber>();
                    for (int j = 0; j < ColumnSize - 1; j++)
                    {
                        RationalNumberRowVector vec = SetToZero(i, k, j);
                        copy.ReplaceRow(k, vec);
                    }
                }
            }
            for (int k = RowSize - 1; k >= 0; k--)
            {
                for (int i = k - 1; i >= 0; i--)
                {
                    RationalNumber[] orig = GetRow(i).ToArray <RationalNumber>();
                    for (int j = ColumnSize - 2; j >= k; j--)
                    {
                        RationalNumberRowVector vec = SetToZero(k, i, j);
                        copy.ReplaceRow(k, vec);
                    }
                }
            }
            return(copy);
        }
        public RationalNumberRowVector ReplaceRow(int row, RationalNumberRowVector other)
        {
            //TODO: should they have to be the same size?
            RationalNumberRowVector ret = new RationalNumberRowVector(GetRow(row).ToArray <RationalNumber>());

            for (int i = 0; i < ColumnSize; i++)
            {
                Replace(row, i, other.Get(0, i));
            }
            return(ret);
        }
Beispiel #3
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);
        }