コード例 #1
0
ファイル: LDUSolver.cs プロジェクト: ajaykc7/LDUDecomposition
 /// <summary>
 /// Method to perform row reduction on the given row
 /// </summary>
 /// <param name="row">Row being reduced</param>
 /// <param name="factoringRow">helper row that is used to reduce the row</param>
 /// <param name="factor">factor needed to reduce the row to upper triangular form</param>
 private void RowReduce(int row, int factoringRow, BigFraction factor)
 {
     for (int column = 0; column < Size; column++)
     {
         upperMatrix[row, column] = upperMatrix[row, column].Subtract(factor.Multiply(upperMatrix[factoringRow, column]));
     }
 }
コード例 #2
0
        /// <summary>
        /// Method to divide two BigFraction objects
        /// </summary>
        /// <param name="b">BigFraction object being divided from the current object</param>
        /// <returns>result of division in its simplest form</returns>
        public BigFraction Divide(BigFraction b)
        {
            /*When you divide a fraction by another fraction we multiply the dividend by the reciprocal of the divisor
             * So, we will multiply the numerator of a with denominator of b and denominator of a with numerator of b
             */
            BigInteger numeratorB   = GetNumerator(b.ToString());
            BigInteger denominatorB = GetDenominator(b.ToString());

            //Negate the numerator and denominator if numerator is negative
            if (numeratorB.Sign == -1)
            {
                numeratorB   = -numeratorB;
                denominatorB = -denominatorB;
            }

            BigInteger finalNumerator   = BigInteger.One;
            BigInteger finalDenominator = BigInteger.One;

            if (numeratorB.IsZero)
            {
                finalNumerator   = BigInteger.Zero;
                finalDenominator = BigInteger.One;
            }
            else
            {
                finalNumerator   = this.numerator * denominatorB;
                finalDenominator = this.denominator * numeratorB;
            }
            BigFraction result = new BigFraction(finalNumerator.ToString() + "/" + finalDenominator.ToString());

            return(GetSimplestForm(result));
        }
コード例 #3
0
        /// <summary>
        /// Method to multiply two BigFraction datatypes
        /// </summary>
        /// <param name="b">BigFraction being multiplied from current BigFraction</param>
        /// <returns>result of multiplication in its simplest form</returns>
        public BigFraction Multiply(BigFraction b)
        {
            BigInteger numeratorB   = GetNumerator(b.ToString());
            BigInteger denominatorB = GetDenominator(b.ToString());

            BigInteger finalNumerator   = this.numerator * numeratorB;
            BigInteger finalDenominator = this.denominator * denominatorB;

            BigFraction result = new BigFraction(finalNumerator.ToString() + "/" + finalDenominator.ToString());

            return(GetSimplestForm(result));
        }
コード例 #4
0
        /// <summary>
        /// Method to subtract two BigFraction datatypes
        /// </summary>
        /// <param name="b">BigFraction being subtracted from current BigFraction</param>
        /// <returns>result of subtraction in its simplest form</returns>
        public BigFraction Subtract(BigFraction b)
        {
            BigInteger numeratorB   = GetNumerator(b.ToString());
            BigInteger denominatorB = GetDenominator(b.ToString());

            BigInteger lcm            = GetLCM(this.denominator, denominatorB);
            BigInteger finalNumerator = this.numerator * (lcm / this.denominator) -
                                        numeratorB * (lcm / denominatorB);

            BigFraction result = new BigFraction(finalNumerator.ToString() + "/" + lcm.ToString());

            return(GetSimplestForm(result));
        }
コード例 #5
0
ファイル: LDUSolver.cs プロジェクト: ajaykc7/LDUDecomposition
 /// <summary>
 /// Method to solve the LY=B equation and store the value of Y
 /// </summary>
 private void SolveY()
 {
     for (int i = 0; i < Size; i++)
     {
         BigFraction tempSum = zero;
         for (int j = 0; j < Size; j++)
         {
             tempSum = tempSum.Add(lowerMatrix[i, j].Multiply(yMatrix[j]));
             //Console.WriteLine(tempSum);
         }
         yMatrix[i] = bMatrix[i].Subtract(tempSum);
     }
 }
コード例 #6
0
        /// <summary>
        /// Method to get the fraction in its simplest form
        /// </summary>
        /// <param name="frac"></param>
        /// <returns></returns>
        private BigFraction GetSimplestForm(BigFraction frac)
        {
            BigInteger num  = GetNumerator(frac.ToString());
            BigInteger deno = GetDenominator(frac.ToString());
            BigInteger gcf  = GetGCF(num, deno);

            while (!gcf.IsOne)
            {
                num  = num / gcf;
                deno = deno / gcf;
                gcf  = GetGCF(num, deno);
            }

            return(new BigFraction(num.ToString() + "/" + deno.ToString()));
        }
コード例 #7
0
ファイル: LDUSolver.cs プロジェクト: ajaykc7/LDUDecomposition
 /// <summary>
 /// Method to solve the UX=Y and store the result in X
 /// </summary>
 private void SolveX()
 {
     for (int i = Size - 1; i >= 0; i--)
     {
         BigFraction tempSum = zero;
         for (int j = Size - 1; j >= 0; j--)
         {
             if (i < j)
             {
                 tempSum = tempSum.Add(upperMatrix[i, j].Multiply(xMatrix[j]));
             }
         }
         xMatrix[i] = yMatrix[i].Subtract(tempSum).Divide(upperMatrix[i, i]);
     }
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: ajaykc7/LDUDecomposition
        static void Main(string[] args)
        {
            BigFraction temp = new BigFraction("2/4");

            BigFraction[,] a = { { new BigFraction("1/1"), new BigFraction("2/1"),  new BigFraction("2/1")  },
                                 { new BigFraction("3/1"), new BigFraction("-2/1"), new BigFraction("1/1")  },
                                 { new BigFraction("2/1"), new BigFraction("1/1"),  new BigFraction("-1/1") } };
            BigFraction[] b = { new BigFraction("5/1"), new BigFraction("-6/1"), new BigFraction("-1/1") };

            LDUSolver solver = new LDUSolver(a, b, 3);

            BigFraction[] answer = solver.GetResult();
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();
        }
コード例 #9
0
ファイル: LDUSolver.cs プロジェクト: ajaykc7/LDUDecomposition
        /// <summary>
        /// Method to create final lower and upper matrix
        /// </summary>
        private void FactorizeLU()
        {
            //perform row-reduction on the matrix to get lowerMatrix
            for (int i = 0; i < Size; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    if (i > j)
                    {
                        BigFraction factor = upperMatrix[i, j].Divide(upperMatrix[j, j]);

                        //Perform row-reduction on the given row
                        RowReduce(i, j, factor);
                        //Populate the lower matrix with the factor
                        lowerMatrix[i, j] = factor;
                    }
                }
            }
        }