Exemple #1
0
        static void AddFactorFromTo(Fraction factor, int from, int to, int aktuelleSpalte, bool ROUND, bool INV, Fraction[,] A, Fraction[] b, Fraction[,] e)
        {
            int n = Matrix.GetSpaltenAnzahl(A);
            for (int no_n = 1; no_n <= n; no_n++)
            {
                //A[i,no_n] = A[i,no_n]+factor*A[k,no_n];
                //from = k
                //to = i
                Fraction temp = factor * A[from, no_n];
                Fraction tempe = factor * e[from, no_n];
                if (ROUND) temp = Round.Runden(temp);
                if (ROUND) tempe = Round.Runden(tempe);

                A[to, no_n] = A[to, no_n] + temp;
                if (INV) e[to, no_n] = e[to, no_n] + tempe;
                if (ROUND)
                {
                    if (INV) e[to, no_n] = Round.Runden(e[to, no_n]);
                    A[to, no_n] = Round.Runden(A[to, no_n]);
                }

                if (ROUND && (no_n == aktuelleSpalte)) A[to, no_n] = 0;
            }

            Fraction temp2 = factor * b[from];
            if (ROUND) temp2 = Round.Runden(temp2);

            b[to] = b[to] + temp2;
            if (ROUND) b[to] = Round.Runden(b[to]);
        }
Exemple #2
0
        //Wrappers by MarcK
        //return ist eigentlich nicht notwendig, da Arrays sowieso verändert werden
        public static Fraction[] Runden(Fraction[] b)
        {
            int m = Matrix.GetZeilenAnzahl(b);

            for (int no_m = 1; no_m <= m; no_m++)
            {
                b[no_m] = Runden(b[no_m]);
            }

            return b;
        }
Exemple #3
0
        public static void Einheitsmatrix(Fraction[,] e)
        {
            int m = GetZeilenAnzahl(e); //Anzahl der Zeilen der Matrix
            int n = GetSpaltenAnzahl(e); //Anzahl der Spalten der Matrix

            for (int no_m = 1; no_m <= m; no_m++)
            {
                for (int no_n = 1; no_n <= n; no_n++)
                {
                    e[no_m, no_n] = 0;
                    if (no_m == no_n) e[no_m, no_n] = 1;
                }
            }
        }
		/// <summary>
		/// internal function for negation
		/// </summary>
		private static Fraction Negate(Fraction frac1)
		{
			long iNumerator=-frac1.Numerator;
			long iDenominator=frac1.Denominator;
			return ( new Fraction(iNumerator, iDenominator) );

		}	
		/// <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;
				}
				
				long 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);
			}
		}
Exemple #6
0
        public static Fraction[,] Runden(Fraction[,] A)
        {
            int m = Matrix.GetZeilenAnzahl(A); //Anzahl der Zeilen der Matrix
            int n = Matrix.GetSpaltenAnzahl(A); //Anzahl der Spalten der Matrix

            for (int no_m = 1; no_m <= m; no_m++)
            {
                for (int no_n = 1; no_n <= n; no_n++)
                {
                    A[no_m, no_n] = Runden(A[no_m, no_n]);
                }
            }

            return A;
        }
		/// <summary>
		/// The function replicates current Fraction object
		/// </summary>
		public Fraction Duplicate()
		{
			Fraction frac=new Fraction();
			frac.Numerator=Numerator;
			frac.Denominator=Denominator;
			return frac;
		}
Exemple #8
0
 public static Fraction operator *(double dbl, Fraction frac1)
 {
     return(Multiply(frac1, Fraction.ToFraction(dbl)));
 }
Exemple #9
0
        //Test, um neue Rundungsfunktion gegen proprietäre zu testen.
        public static void RundungTest()
        {
            Random randObj = new Random();
            while (true)
            {
                int zaehler = randObj.Next(-1000, 1000);
                int nenner = randObj.Next(-1000, 1000);

                if (nenner == 0)
                {
                    nenner = 1;
                }

                Fraction bruch = new Fraction(zaehler, nenner);

                double istRundung = Runde(bruch.ToDouble(), 2);
                double sollRundung = TRunde(bruch.ToDouble(), 2);

                Console.Write(bruch.ToString() + " (" + bruch.ToDouble() + "): " + istRundung + " vs " + sollRundung + " –– ");

                //if (istRundung == sollRundung)
                if (Math.Round(istRundung, 10) == Math.Round(sollRundung, 10))
                {
                    Console.WriteLine("OK");
                }
                else
                {
                    Console.WriteLine("FAIL:");
                    Console.WriteLine("Ist:  " + new Fraction(istRundung).ToString());
                    Console.WriteLine("Soll: " + new Fraction(sollRundung).ToString());
                    Console.WriteLine("");
                }
            }
        }
Exemple #10
0
 public static Matrix operator /(Matrix matrix1, int iNo)
 {
     return(Matrix.Multiply(matrix1, Fraction.Inverse(new Fraction(iNo))));
 }
Exemple #11
0
			/// <summary>
			/// Adds two Fractions
			/// </summary>
			/// <param name="left">A Fraction</param>
			/// <param name="right">Another Fraction</param>
			/// <returns>Sum of the Fractions. Returns NaN if either Fraction is a NaN.</returns>
			/// <exception cref="FractionException">Will throw if an overflow occurs when computing the
			/// GCD-normalized values.</exception>
			private static Fraction Add(Fraction left, Fraction right)
			{
				if (left.IsNaN() || right.IsNaN())
					return NaN;

				long gcd = GCD(left.m_Denominator, right.m_Denominator); // cannot return less than 1
				long leftDenominator = left.m_Denominator / gcd;
				long rightDenominator = right.m_Denominator / gcd;

				try
				{
					checked
					{
						long numerator = left.m_Numerator * rightDenominator + right.m_Numerator * leftDenominator;
						long denominator = leftDenominator * rightDenominator * gcd;

						return new Fraction(numerator, denominator);
					}
				}
				catch (Exception e)
				{
					throw new FractionException("Add error", e);
				}
			}
Exemple #12
0
 /// <summary>
 /// The function multiplies the given row of the current matrix object by a double
 /// </summary>
 public void MultiplyRow(int iRow, double dbl)
 {
     this.MultiplyRow(iRow, Fraction.ToFraction(dbl));
 }
Exemple #13
0
 public static Matrix operator *(double dbl, Matrix matrix1)
 {
     return(Matrix.Multiply(matrix1, Fraction.ToFraction(dbl)));
 }
Exemple #14
0
        /// <summary>
        /// checks whether two fractions are equal
        /// </summary>
        public override bool Equals(object obj)
        {
            Fraction frac = (Fraction)obj;

            return(Numerator == frac.Numerator && Denominator == frac.Denominator);
        }
Exemple #15
0
 public static Fraction operator /(Fraction frac1, double dbl)
 {
     return(Multiply(frac1, Fraction.Inverse(Fraction.ToFraction(dbl))));
 }
Exemple #16
0
 public static Fraction operator /(double dbl, Fraction frac1)
 {
     return(Multiply(Inverse(frac1), Fraction.ToFraction(dbl)));
 }
Exemple #17
0
			/// <summary>
			/// Cross-reduces a pair of Fractions so that we have the best GCD-reduced values for multiplication
			/// </summary>
			/// <param name="frac1">The first Fraction [WILL BE MODIFIED IN PLACE]</param>
			/// <param name="frac2">The second Fraction [WILL BE MODIFIED IN PLACE]</param>
			/// <remarks>Modifies the input arguments in-place!</remarks>
			/// <example>(3/4, 5/9) = (1/4, 5/3)</example>
			public static void CrossReducePair(ref Fraction frac1, ref Fraction frac2)
			{
				// leave the indeterminates alone!
				if (frac1.m_Denominator == 0 || frac2.m_Denominator == 0)
					return;

				long gcdTop = GCD(frac1.m_Numerator, frac2.m_Denominator);
				frac1.m_Numerator = frac1.m_Numerator / gcdTop;
				frac2.m_Denominator = frac2.m_Denominator / gcdTop;

				long gcdBottom = GCD(frac1.m_Denominator, frac2.m_Numerator);
				frac2.m_Numerator = frac2.m_Numerator / gcdBottom;
				frac1.m_Denominator = frac1.m_Denominator / gcdBottom;
			}
Exemple #18
0
 public static Matrix operator /(Matrix matrix1, double dbl)
 {
     return(Matrix.Multiply(matrix1, Fraction.Inverse(Fraction.ToFraction(dbl))));
 }
Exemple #19
0
			/// <summary>
			/// Determines how this Fraction, of an indeterminate type, compares to another Fraction
			/// </summary>
			/// <param name="leftType">What kind of indeterminate</param>
			/// <param name="right">The other Fraction to compare against</param>
			/// <returns>-1 if this is less than <paramref name="right"></paramref>,
			///  0 if they are equal,
			///  1 if this is greater than <paramref name="right"></paramref></returns>
			/// <remarks>NaN is less than anything except NaN and Negative Infinity. Negative Infinity is less
			/// than anything except Negative Infinity. Positive Infinity is greater than anything except
			/// Positive Infinity.</remarks>
			private static int IndeterminantCompare(Indeterminates leftType, Fraction right)
			{
				switch (leftType)
				{
					case Indeterminates.NaN:
						// A NaN is...
						if (right.IsNaN())
							return 0;	// equal to a NaN
						else if (right.IsNegativeInfinity())
							return 1;	// great than Negative Infinity
						else
							return -1;	// less than anything else

					case Indeterminates.NegativeInfinity:
						// Negative Infinity is...
						if (right.IsNegativeInfinity())
							return 0;	// equal to Negative Infinity
						else
							return -1;	// less than anything else

					case Indeterminates.PositiveInfinity:
						if (right.IsPositiveInfinity())
							return 0;	// equal to Positive Infinity
						else
							return 1;	// greater than anything else

					default:
						// this CAN'T happen, something VERY wrong is going on...
						return 0;
				}
			}
Exemple #20
0
 public static Matrix operator /(Matrix matrix1, Fraction frac)
 {
     return(Matrix.Multiply(matrix1, Fraction.Inverse(frac)));
 }
Exemple #21
0
			/// <summary>
			/// Returns the modulus (remainder after dividing) two Fractions
			/// </summary>
			/// <param name="left">A Fraction</param>
			/// <param name="right">Another Fraction</param>
			/// <returns>Modulus of the Fractions. Returns NaN if either Fraction is a NaN.</returns>
			/// <exception cref="FractionException">Will throw if an overflow occurs. Does a cross-reduce to 
			/// insure only the unavoidable overflows occur.</exception>
			private static Fraction Modulus(Fraction left, Fraction right)
			{
				if (left.IsNaN() || right.IsNaN())
					return NaN;

				try
				{
					checked
					{
						// this will discard any fractional places...
						Int64 quotient = (Int64)(left / right);
						Fraction whole = new Fraction(quotient * right.m_Numerator, right.m_Denominator);
						return left - whole;
					}
				}
				catch (Exception e)
				{
					throw new FractionException("Modulus error", e);
				}
			}
Exemple #22
0
        public Fraction(double dDecimalValue)
        {
            Fraction temp = ToFraction(dDecimalValue);

            Initialize(temp.Numerator, temp.Denominator);
        }
Exemple #23
0
 public static Fraction Runden(Fraction zahl)
 {
     return Runde(zahl.ToDouble());
 }
Exemple #24
0
        public Fraction(string strValue)
        {
            Fraction temp = ToFraction(strValue);

            Initialize(temp.Numerator, temp.Denominator);
        }
Exemple #25
0
 public static Fraction operator -(double dbl, Fraction frac1)
 {
     return(Add(-frac1, Fraction.ToFraction(dbl)));
 }
Exemple #26
0
 /// <summary>
 /// Constructors
 /// </summary>
 public Matrix(Fraction[,] elements)
 {
     m_iElement=elements;
     m_iRows=elements.GetLength(0);
     m_iCols=elements.GetLength(1);
 }
		/// <summary>
		/// The function takes a floating point number as an argument 
		/// and returns its corresponding reduced fraction
		/// </summary>
		public static Fraction ToFraction(double dValue)
		{
			try
			{
				checked
				{
					Fraction frac;
					if (dValue%1==0)	// if whole number
					{
						frac=new Fraction( (long) dValue );
					}
					else
					{
						double dTemp=dValue;
						long iMultiple=1;
						string strTemp=dValue.ToString();
						while ( strTemp.IndexOf("E")>0 )	// if in the form like 12E-9
						{
							dTemp*=10;
							iMultiple*=10;
							strTemp=dTemp.ToString();
						}
						int i=0;
						while ( strTemp[i]!='.' )
							i++;
						int iDigitsAfterDecimal=strTemp.Length-i-1;
						while ( iDigitsAfterDecimal>0  )
						{
							dTemp*=10;
							iMultiple*=10;
							iDigitsAfterDecimal--;
						}
						frac=new Fraction( (int)Math.Round(dTemp) , iMultiple );
					}
					return frac;
				}
			}
			catch(OverflowException)
			{
				throw new FractionException("Conversion not possible due to overflow");
			}
			catch(Exception)
			{
				throw new FractionException("Conversion not possible");
			}
		}
Exemple #28
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] );
         }
     }
 }
		/// <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)");
	
			long iNumerator=frac1.Denominator;
			long iDenominator=frac1.Numerator;
			return ( new Fraction(iNumerator, iDenominator));
		}	
Exemple #30
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;
 }
		private static Fraction Multiply(Fraction frac1, Fraction frac2)
		{
			try
			{
				checked
				{
					long iNumerator=frac1.Numerator*frac2.Numerator;
					long iDenominator=frac1.Denominator*frac2.Denominator;
					return ( new Fraction(iNumerator, iDenominator) );
				}
			}
			catch(OverflowException)
			{
				throw new FractionException("Overflow occurred while performing arithemetic operation");
			}
			catch(Exception)
			{
				throw new FractionException("An error occurred while performing arithemetic operation");
			}
		}
Exemple #32
0
        public static void Main(string[] args)
        {
            Arguments clArgs = new Arguments(args);

            bool fromFile = false;
            bool doRound = false;
            bool doFrac = false;
            bool gaussjordan = false;
            bool inverse = false;

            //Rundung-Tests
            clArgs.AddAlias("roundtest", "rt");
            if (clArgs["roundtest"] != null)
            {
                Round.RundungTest();
                return;
            }

            //change forecolor to black
            clArgs.AddAlias("schwarz", "s");
            if (clArgs["schwarz"] != null)
            {
                Console.ForegroundColor = ConsoleColor.Black;
            }

            clArgs.AddAlias("bruch", "b");
            if (clArgs["bruch"] != null)
            {
                Console.WriteLine("· Bruchmodus aktiviert");
                doFrac = true;
            }

            clArgs.AddAlias("rundung", "r");
            int runden_stellen = 0;
            if (clArgs["rundung"] != null && Int32.TryParse(clArgs["rundung"], out runden_stellen))
            {
                Console.WriteLine("· Rundungsmodus ({0} gültige Stellen) aktiviert", runden_stellen);
                doRound = true;
            }
            else if (clArgs["rundung"] != null)
            {
                WriteLineColored("Bitte Anzahl gültiger Stellen bei -r angeben.", ConsoleColor.Red);
                return;
            }

            clArgs.AddAlias("gaussjordan", "gj");
            clArgs.AddAlias("gauss", "g");
            if (clArgs["gaussjordan"] != null && clArgs["gauss"] == null)
            {
                Console.WriteLine("· Gauß-Jordan aktiviert");
                gaussjordan = true;
            }
            else if (clArgs["gaussjordan"] != null && clArgs["gauss"] != null)
            {
                Console.WriteLine("Entweder -g oder -gj auswählen");
                return;
            }
            else
            {
                Console.WriteLine("· Gauß aktiviert");
            }

            clArgs.AddAlias("inverse", "i");
            if (clArgs["inverse"] != null)
            {
                Console.WriteLine("· Inverse aktiviert (Gauß-Jordan aktiviert)");
                gaussjordan = true;
                inverse = true;
            }

            clArgs.AddAlias("file", "f");
            if (clArgs["file"] != null && clArgs["file"] != "")
            {
                if (File.Exists(clArgs["file"]) == false)
                {
                    Console.WriteLine("Datei " + clArgs["file"] + " existiert nicht.");
                    return;
                }

                Console.WriteLine("· File-Input aktiviert: " + clArgs["file"]);

                fromFile = true;
            }

            clArgs.AddAlias("credits", "c");
            if (clArgs["credits"] != null)
            {
                credits();
                return;
            }

            clArgs.AddAlias("help", "h");
            if ((clArgs["help"] != null) || (doRound == false && doFrac == false))
            {
                if (doRound == false && doFrac == false)
                {
                    help(true);
                }
                else
                {
                    help(false);
                }
                return;
            }

            clArgs.AddAlias("debug", "d");
            if (clArgs["debug"] != null)
            {
                debug = true;
            }

            Console.Write("Anzahl der Unbekannten: ");
            n = Convert.ToInt32(Console.ReadLine());

            Console.Write("Anzahl der Gleichungen: ");
            m = Convert.ToInt32(Console.ReadLine());

            A = new Fraction[m + 1, n + 1];
            b = new Fraction[m + 1];

            if (fromFile == true)
            {
                ReadFromFile(clArgs["file"]);
            }
            else
            {
                ReadFromConsole();
            }

            //Einheitsmatrix deklarieren und füllen
            e = new Fraction[m + 1, n + 1];
            Matrix.Einheitsmatrix(e);

            Fraction[,] copyA = new Fraction[m + 1, n + 1];
            Fraction[] copyb = new Fraction[m + 1];
            copyA = (Fraction[,])A.Clone();
            copyb = (Fraction[])b.Clone();

            Console.WriteLine("\nDas LGS wurde wie folgt eingelesen:");
            PrintLGS(inverse);

            if (doFrac)
            {
                WriteLineColored("\nGauss mit Brüchen", ConsoleColor.Yellow);
                WriteLineColored("=================\n", ConsoleColor.Yellow);
                Gauss.DoGauss(false, gaussjordan, inverse, A, b, e);
            }

            if (doRound)
            {
                A = (Fraction[,])copyA.Clone();
                b = (Fraction[])copyb.Clone();
                Round.Runden(A);
                Round.Runden(b);

                WriteLineColored("\nGauss mit Rundungen", ConsoleColor.Yellow);
                WriteLineColored("===================\n", ConsoleColor.Yellow);

                Console.WriteLine("Das LGS wurde wie folgt gerundet:");
                PrintLGS(inverse);
                Console.WriteLine();

                Gauss.DoGauss(true, gaussjordan, inverse, A, b, e);
            }
        }
Exemple #33
0
			/// <summary>
			/// Reduces (simplifies) a Fraction by dividing down to lowest possible denominator (via GCD)
			/// </summary>
			/// <param name="frac">The Fraction to be reduced [WILL BE MODIFIED IN PLACE]</param>
			/// <remarks>Modifies the input arguments in-place! Will normalize the NaN and infinites
			/// representation. Will set Denominator to 1 for any zero numerator. Moves sign to the
			/// Numerator.</remarks>
			/// <example>2/4 will be reduced to 1/2</example>
			public static void ReduceFraction(ref Fraction frac)
			{
				// clean up the NaNs and infinites
				if (frac.m_Denominator == 0)
				{
					frac.m_Numerator = (long)NormalizeIndeterminate(frac.m_Numerator);
					return;
				}

				// all forms of zero are alike.
				if (frac.m_Numerator == 0)
				{
					frac.m_Denominator = 1;
					return;
				}
	            
				long iGCD = GCD(frac.m_Numerator, frac.m_Denominator);
				frac.m_Numerator /= iGCD;
				frac.m_Denominator /= iGCD;
	        
				// if negative sign in denominator
				if ( frac.m_Denominator < 0 )
				{
					//move negative sign to numerator
					frac.m_Numerator = - frac.m_Numerator;
					frac.m_Denominator = - frac.m_Denominator;  
				}
			}
			/// <summary>
			/// Compares this Fraction to another Fraction
			/// </summary>
			/// <param name="right">The Fraction to compare against</param>
			/// <returns>-1 if this is less than <paramref name="right"></paramref>,
			///  0 if they are equal,
			///  1 if this is greater than <paramref name="right"></paramref></returns>
			public int CompareTo(Fraction right)
			{
				// if left is an indeterminate, punt to the helper...
				if (this.m_Denominator == 0)
				{
					return IndeterminantCompare(NormalizeIndeterminate(this.m_Numerator), right);
				}

				// if right is an indeterminate, punt to the helper...
				if (right.m_Denominator == 0)
				{
					// note sign-flip...
					return - IndeterminantCompare(NormalizeIndeterminate(right.m_Numerator), this);
				}

				// they're both normal Fractions
				CrossReducePair(ref this, ref right);

				try
				{
					checked
					{
						long leftScale = this.m_Numerator * right.m_Denominator;
						long rightScale = this.m_Denominator * right.m_Numerator;

						if (leftScale < rightScale)
							return -1;
						else if (leftScale > rightScale)
							return 1;
						else
							return 0;
					}
				}
				catch (Exception e)
				{
					throw new FractionException(string.Format("CompareTo({0}, {1}) error", this, right), e);
				}
			}
Exemple #35
0
			/// <summary>
			/// Compares for equality the current Fraction to the value passed.
			/// </summary>
			/// <param name="right">A Fraction to compare against</param>
			/// <param name="notEqualCheck">If true, we're looking for not-equal</param>
			/// <returns>True if the <paramref name="right"></paramref> equals the current 
			/// fraction, false otherwise. If comparing two NaNs, they are always equal AND
			/// not-equal.</returns>
			private bool CompareEquality(Fraction right, bool notEqualCheck)
			{
				// insure we're normalized first
				ReduceFraction(ref this);

				// now normalize the comperand
				ReduceFraction(ref right);

				if (this.m_Numerator == right.m_Numerator && this.m_Denominator == right.m_Denominator)
				{
					// special-case rule, two NaNs are always both equal
					if (notEqualCheck && this.IsNaN())
						return true;
					else
						return ! notEqualCheck;
				}
				else
				{
					return notEqualCheck;
				}
			}
        public FrmBalanceOptions2(SimpleReaction rxn, List<Compound> extraComps)
        {
            m_OriginalMatrix = rxn.GetBalanceMatrix(extraComps);
            m_OriginalMatrix.RowReduce();

            m_MinValues = new Fraction[m_OriginalMatrix.Columns - 1];
            m_MaxValues = new Fraction[m_OriginalMatrix.Columns - 1];
            for (int i = 0; i < m_MinValues.Length; i++)
            {
                m_MinValues[i] = Fraction.MaxValue;
                if (m_OriginalMatrix.CanGoNegative(i))
                    m_MaxValues[i] = Fraction.MinValue;
                else
                    m_MaxValues[i] = 0;
            }
            CalculateAbsoluteMinMaxValues(m_OriginalMatrix, new List<int>());

            InitializeComponent();

            int c = m_OriginalMatrix.Columns - 1 - m_OriginalMatrix.Rank;
            bool[] changeables = m_OriginalMatrix.ColumnsRemoveable();

            if (c > 1)
                lblInfo.Text = "The selected reaction is under-constrained, and as such requires that some compounds have specified coefficients to allow autbalancing. Please specify " + c + " values to balance.";
            else
                lblInfo.Text = "The selected reaction is under-constrained, and as such requires that a compound have a specified coefficient to allow autbalancing. Please specify a value.";

            List<Compound> products = rxn.OrderedProducts;
            List<Compound> reactants = rxn.OrderedReactants;
            int longestLength = products.Count > reactants.Count - 1 ? products.Count : reactants.Count - 1;
            if (extraComps.Count > longestLength)
                longestLength = extraComps.Count;

            this.Height = extraFormSpace + longestLength * chkSpacing;

            PopulateGroupBox(grpReactants, reactants, 0, changeables, 1);
            PopulateGroupBox(grpProducts, products, reactants.Count - 1, changeables, 0);

            if (extraComps.Count != 0)
            {
                lblNote.Text = "Note that a negative value in the third group indicates the compound will be a reactant, while a positive value indicates it will be a product.";
                int extraWidth = grpProducts.Right - grpReactants.Right;
                this.Width += extraWidth;
                GroupBox grpExtras = new GroupBox();
                grpExtras.Text = "Autobalance Added Compounds";
                this.Controls.Add(grpExtras);
                grpExtras.Top = grpProducts.Top;
                grpExtras.Left = grpProducts.Left + extraWidth;
                grpExtras.Width = grpProducts.Width;
                grpExtras.Height = grpProducts.Height;
                PopulateGroupBox(grpExtras, extraComps, reactants.Count - 1 + products.Count, changeables, 0);
            }

            SetInitialValues();

            m_bDoOnValueChanged = true;
        }
Exemple #37
0
			/// <summary>
			/// Negates the Fraction
			/// </summary>
			/// <param name="frac">Value to negate</param>
			/// <returns>A new Fraction that is sign-flipped from the input</returns>
			private static Fraction Negate(Fraction frac)
			{
				// for a NaN, it's still a NaN
				return new Fraction( - frac.m_Numerator, frac.m_Denominator);
			}
Exemple #38
0
			/// <summary>
			/// Creates an inverted Fraction
			/// </summary>
			/// <returns>The inverted Fraction (with Denominator over Numerator)</returns>
			/// <remarks>Does NOT throw for zero Numerators as later use of the fraction will catch the error.</remarks>
			public static Fraction Inverted(double value)
			{
				Fraction frac = new Fraction(value);
				return frac.Inverse();
			}
Exemple #39
0
			/// <summary>
			/// Multiplies two Fractions
			/// </summary>
			/// <param name="left">A Fraction</param>
			/// <param name="right">Another Fraction</param>
			/// <returns>Product of the Fractions. Returns NaN if either Fraction is a NaN.</returns>
			/// <exception cref="FractionException">Will throw if an overflow occurs. Does a cross-reduce to 
			/// insure only the unavoidable overflows occur.</exception>
			private static Fraction Multiply(Fraction left, Fraction right)
			{
				if (left.IsNaN() || right.IsNaN())
					return NaN;

				// this would be unsafe if we were not a ValueType, because we would be changing the
				// caller's values.  If we change back to a class, must use temporaries
				CrossReducePair(ref left, ref right);

				try
				{
					checked
					{
						long numerator = left.m_Numerator * right.m_Numerator;
						long denominator = left.m_Denominator * right.m_Denominator;

						return new Fraction(numerator, denominator);
					}
				}
				catch (Exception e)
				{
					throw new FractionException("Multiply error", e);
				}
			}
Exemple #40
0
 /// <summary>
 /// The function takes a Fraction object and returns its value as double
 /// </summary>
 public static double ToDouble(Fraction frac)
 {
     return((double)frac.Numerator / frac.Denominator);
 }
Exemple #41
0
			/// <summary>
			/// Inverts a Fraction
			/// </summary>
			/// <returns>The inverted Fraction (with Denominator over Numerator)</returns>
			/// <remarks>Does NOT throw for zero Numerators as later use of the fraction will catch the error.</remarks>
			public Fraction Inverse()
			{
				// don't use the obvious constructor because we do not want it normalized at this time
				Fraction frac = new Fraction();

				frac.m_Numerator = this.m_Denominator;
				frac.m_Denominator = this.m_Numerator;
				return frac;
			}
Exemple #42
0
 public static Fraction operator -(Fraction frac1, double dbl)
 {
     return(Add(frac1, -Fraction.ToFraction(dbl)));
 }
Exemple #43
0
			/// <summary>
			/// Compares this Fraction to another Fraction
			/// </summary>
			/// <param name="right">The Fraction to compare against</param>
			/// <returns>-1 if this is less than <paramref name="right"></paramref>,
			///  0 if they are equal,
			///  1 if this is greater than <paramref name="right"></paramref></returns>
			public int CompareTo(Fraction right)
			{
				// if left is an indeterminate, punt to the helper...
				if (this.m_Denominator == 0)
				{
					return IndeterminantCompare(NormalizeIndeterminate(this.m_Numerator), right);
				}

				// if right is an indeterminate, punt to the helper...
				if (right.m_Denominator == 0)
				{
					// note sign-flip...
					return - IndeterminantCompare(NormalizeIndeterminate(right.m_Numerator), this);
				}

                //To make Fraction.MaxValue and Fraction.MinValue not cause overflow exceptions:
                if (this == MaxValue)
                    if (right == MaxValue)
                        return 0;
                    else
                        return 1;
                else if (right == MaxValue)
                    return -1;

                if (this == MinValue)
                    if (right == MinValue)
                        return 0;
                    else
                        return -1;
                else if (right == MinValue)
                    return 1;

                //No idea what this was in here for.
				// they're both normal Fractions
				//CrossReducePair(ref this, ref right);

				try
				{
					checked
					{
						long leftScale = this.m_Numerator * right.m_Denominator;
						long rightScale = this.m_Denominator * right.m_Numerator;

						if (leftScale < rightScale)
							return -1;
						else if (leftScale > rightScale)
							return 1;
						else
							return 0;
					}
				}
				catch (Exception e)
				{
					throw new FractionException(string.Format("CompareTo({0}, {1}) error", this, right), e);
				}
			}
Exemple #44
0
 public static int GetZeilenAnzahl(Fraction[] x)
 {
     return x.GetLength(0) - 1; //Anzahl der Spalten der Matrix
 }