Beispiel #1
0
 private static Fraction Multiply(Fraction frac1, Fraction frac2)
 {
     int iNumerator=frac1.Numerator*frac2.Numerator;
     int iDenominator=frac1.Denominator*frac2.Denominator;
     return ( new Fraction(iNumerator, iDenominator) );
 }
Beispiel #2
0
 /// <summary>
 /// internal function for negation
 /// </summary>
 private static Fraction Negate(Fraction frac1)
 {
     int iNumerator=-frac1.Numerator;
     int iDenominator=frac1.Denominator;
     return ( new Fraction(iNumerator, iDenominator) );
 }
Beispiel #3
0
        /// <summary>
        /// The function reduces(simplifies) a Fraction object by dividing both its numerator 
        /// and denominator by their GCD
        /// </summary>
        public static void ReduceFraction(Fraction frac)
        {
            try
            {
                if (frac.Numerator==0)
                {
                    frac.Denominator=1;
                    return;
                }

                int iGCD=GCD(frac.Numerator, frac.Denominator);
                frac.Numerator/=iGCD;
                frac.Denominator/=iGCD;

                if ( frac.Denominator<0 )	// if -ve sign in denominator
                {
                    //pass -ve sign to numerator
                    frac.Numerator*=-1;
                    frac.Denominator*=-1;
                }
            } // end try
            catch(Exception exp)
            {
                throw new FractionException("Cannot reduce Fraction: " + exp.Message);
            }
        }
Beispiel #4
0
 /// <summary>
 /// The function replicates current Fraction object
 /// </summary>
 public Fraction Duplicate()
 {
     Fraction frac=new Fraction();
     frac.Numerator=Numerator;
     frac.Denominator=Denominator;
     return frac;
 }
Beispiel #5
0
 /// <summary>
 /// The function adds two rows for current matrix object
 /// It performs the following calculation:
 /// iTargetRow = iTargetRow + iMultiple*iSecondRow
 /// </summary>
 public void AddRow(int iTargetRow, int iSecondRow, Fraction iMultiple)
 {
     for (int j=0;j<this.Cols;j++)
         this[iTargetRow,j]+=(this[iSecondRow,j]*iMultiple);
 }
Beispiel #6
0
        /// <summary>
        /// The function returns the inverse of a Fraction object
        /// </summary>
        public static Fraction Inverse(Fraction frac1)
        {
            if (frac1.Numerator==0)
                throw new FractionException("Operation not possible (Denominator cannot be assigned a ZERO Value)");

            int iNumerator=frac1.Denominator;
            int iDenominator=frac1.Numerator;
            return ( new Fraction(iNumerator, iDenominator));
        }
Beispiel #7
0
 /// <summary>
 /// The function takes a Fraction object and returns its value as double
 /// </summary>
 public static double ConvertToDouble(Fraction frac)
 {
     return ( (double)frac.Numerator/frac.Denominator );
 }
Beispiel #8
0
 /// <summary>
 /// The function returns the determinent of a Matrix object as Fraction
 /// </summary>
 public static Fraction Determinent(Matrix matrix)
 {
     Fraction det=new Fraction(0);
     if (matrix.Rows!=matrix.Cols)
         throw new MatrixException("Determinent of a non-square matrix doesn't exist");
     if (matrix.Rows==1)
         return matrix[0,0];
     for (int j=0;j<matrix.Cols;j++)
         det+=(matrix[0,j]*Determinent(Matrix.Minor(matrix, 0,j))*(int)System.Math.Pow(-1,0+j));
     return det;
 }
Beispiel #9
0
 public static void EquationSolver()
 {
     Console.Write("Enter no. of variables: ");
     int iVarNo=Convert.ToInt32( Console.ReadLine() );
     string str;
     Matrix mat=new Matrix(iVarNo,iVarNo+1);
     Console.Write("\nEnter value of coefficents and constants (can be an integer(e.g 3), a floating point no(e.g. 12.25), or a fraction(e.g. 15/19)");
     int i,cnt;
     for (i=0;i<iVarNo+1;i++)
     {
         cnt=1;
         for (int k=0;k<i;k++)
         {
             Console.Write("\n"+"Equation "+(k+1)+": ");
             for (int nn=0;nn<iVarNo+1;nn++)
             {
                 if (nn>0 && nn<iVarNo)
                     Console.Write(" + ");
                 if (nn<iVarNo)
                     Console.Write("X"+(nn+1)+"*("+mat[k,nn].ConvertToString()+")");
                 else
                     Console.Write(" = "+mat[k,nn].ConvertToString()  );
             }
         }
         Console.Write("\n\n");
         if (i==iVarNo) continue;
         for(int j=0;j<iVarNo+1;j++,cnt++)
         {
             if (j>0 && j<iVarNo)
                 Console.Write("+");
             if (j<iVarNo)
                 Console.Write("X"+cnt+"*");
             else
                 Console.Write("=");
             str=Console.ReadLine();
             if ( str.Length==0 )
                 str="0";
             mat[i,j]=new Fraction(str);
         }
     }
     Console.WriteLine( "\naugmented matrix:\n"+ mat.ConvertToString() );
     Matrix ech=mat.EchelonForm();
     Console.WriteLine("\nechelon form:\n"+ech.ConvertToString());
     ech=mat.ReducedEchelonForm();
     for (i=0;i<iVarNo;i++)
         if ( ech[i,i]==0 )		// if not unique solution
         {
             Console.WriteLine("The given system of Equation does not produce any unique solution");
             return;
         }
     Console.Write("\nSolution for the given system:\n");
     for (i=0;i<iVarNo;i++)
             Console.Write("X"+(i+1)+"="+ech[i,iVarNo].ConvertToString()+"\t");
 }
Beispiel #10
0
        public static void MatrixDemo()
        {
            Console.WriteLine("Enter no. of Rows: ");
            int iRows=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter no. of Cols: ");
            int iCols=Convert.ToInt32(Console.ReadLine());

            Matrix m1=new Matrix(iRows,iCols);
            Matrix m2=new Matrix(iRows,iCols);
            System.Random rnd=new System.Random();
            for ( int i=0;i<iRows;i++)
            {
                for ( int j=0;j<iCols;j++)
                {
                    m1[i,j]=new Fraction( Convert.ToString( (rnd.Next()%10)-5 ) );
            //					m1[i,j]=new Fraction(Console.ReadLine());
                    m2[i,j]=new Fraction( Convert.ToString( (rnd.Next()%5.5)-2 ) );
                }
            }
            Console.WriteLine("\n\nLet the two matrices be:");
            Console.WriteLine( "\nm1\n"+ m1.ConvertToString() );
            Console.WriteLine( "\nm2\n"+ m2.ConvertToString() );
            Console.ReadLine();
            Console.WriteLine("\nm1+m2=\n"+ (m1+m2).ConvertToString());
            Console.WriteLine("\nm1*m2=\n");
            Console.WriteLine( (m1*m2).ConvertToString());
            Console.ReadLine();
            Console.WriteLine( "\nEchelon Form for m1:\n"+ m1.EchelonForm().ConvertToString() );
            Console.WriteLine( "\nReduced Echelon Form for m1:\n"+ m1.ReducedEchelonForm().ConvertToString() );
            Console.ReadLine();
            Console.WriteLine("\nDeterminent of m1="+ m1.Determinent().ConvertToString());
            Console.WriteLine("\nTranspose m1:\n"+ m1.Transpose().ConvertToString());
            Console.WriteLine("\nInverse of m1:\n "+ m1.Inverse().ConvertToString() );
        }
Beispiel #11
0
 private void SetElement(int iRow, int iCol, Fraction value)
 {
     if ( iRow<0 || iRow>Rows-1 || iCol<0 || iCol>Cols-1 )
             throw new MatrixException("Invalid index specified");
         m_iElement[iRow,iCol]=value.Duplicate();
 }
Beispiel #12
0
 private static Matrix Multiply(Matrix matrix, Fraction frac)
 {
     Matrix result=new Matrix(matrix.Rows,matrix.Cols);
     for (int i=0;i<matrix.Rows;i++)
         for (int j=0;j<matrix.Cols;j++)
             result[i,j]=matrix[i,j]*frac;
     return result;
 }
Beispiel #13
0
 /// <summary>
 /// The function multiplies the given row of the current matrix object by a Fraction 
 /// </summary>
 public void MultiplyRow(int iRow, Fraction frac)
 {
     for (int j=0;j<this.Cols;j++)
     {
         this[iRow,j]*=frac;
         Fraction.ReduceFraction(this[iRow,j]);
     }
 }
Beispiel #14
0
 /// <summary>
 /// Constructors
 /// </summary>
 public Matrix(Fraction[,] elements)
 {
     m_iElement=elements;
     m_iRows=elements.GetLength(0);
     m_iCols=elements.GetLength(1);
 }
Beispiel #15
0
 /// <summary>
 /// The function takes a floating point number as an argument 
 /// and returns its corresponding reduced fraction
 /// </summary>
 public static Fraction ConvertToFraction(double dValue)
 {
     try
     {
         Fraction frac;
         if (dValue>2147483647 && dValue<1.0/2147483647)
             throw new FractionException("Conversion not possible");
         if (dValue%1==0)	// if whole number
         {
             frac=new Fraction( (int) dValue );
         }
         else
         {
             double dTemp=dValue;
             int iMultiple=1;
             string strTemp=dValue.ToString();
             int i=0;
             while ( strTemp[i]!='.' )
                 i++;
             int iDigitsAfterDecimal=strTemp.Length-i-1;
             while ( dTemp*10<2147483647 && iMultiple*10<2147483647 && iDigitsAfterDecimal>0  )
             {
                 dTemp*=10;
                 iMultiple*=10;
                 iDigitsAfterDecimal--;
             }
             frac=new Fraction( (int)Math.Round(dTemp) , iMultiple );
         }
         return frac;
     }
     catch(Exception)
     {
         throw new FractionException("Conversion not possible");
     }
 }
Beispiel #16
0
 public Matrix(int[,] elements)
 {
     m_iRows=elements.GetLength(0);
     m_iCols=elements.GetLength(1);;
     m_iElement=new Fraction[m_iRows,m_iCols];
     for(int i=0;i<elements.GetLength(0);i++)
     {
         for(int j=0;j<elements.GetLength(1);j++)
         {
             this[i,j]=new Fraction( elements[i,j] );
         }
     }
 }
Beispiel #17
0
 /// <summary>
 /// The function takes a Fraction object and returns it as a string
 /// </summary>
 public static string ConvertToString(Fraction frac1)
 {
     string str;
     if ( frac1.Denominator==1 )
         str=frac1.Numerator.ToString();
     else
         str=frac1.Numerator + "/" + frac1.Denominator;
     return str;
 }
Beispiel #18
0
 /// <summary>
 /// The function returns a Null Matrix of dimension ( Row x Col )
 /// </summary>
 public static Matrix NullMatrix(int iRows, int iCols)
 {
     Fraction temp=new Fraction(0);
     Matrix matrix=new Matrix(iRows,iCols);
     for (int i=0;i<iRows;i++)
         for (int j=0;j<iCols;j++)
             matrix[i,j]=temp;
     return matrix;
 }
Beispiel #19
0
 /// <summary>
 /// The function returns a Scalar Matrix of dimension ( Row x Col ) and scalar K
 /// </summary>
 public static Matrix ScalarMatrix(int iRows, int iCols, int K)
 {
     Fraction zero=new Fraction(0);
     Fraction scalar=new Fraction(K);
     Matrix matrix=new Matrix(iRows,iCols);
     for (int i=0;i<iRows;i++)
         for (int j=0;j<iCols;j++)
         {
             if (i==j)
                 matrix[i,j]=scalar;
             else
                 matrix[i,j]=zero;
         }
     return matrix;
 }