/// <summary> /// Splits the string from the input field at every space and adds the elements to a float array /// converts the values from the array to a list of Monoms /// </summary> public void NumbersToPolynom() { PolynomialEquation = new List <Monom>(); numbersString = null; numbersString = inputNumbers.text.Trim().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (numbersString != null) { numbersArray = new float[numbersString.Length]; for (int i = 0; i < numbersString.Length; i++) { numbersArray[i] = float.Parse(numbersString[i].Trim()); } for (int i = numbersArray.Length - 1; i >= 0; i--) { var monomObj = new Monom(numbersArray[numbersArray.Length - 1 - i], i); if (!string.IsNullOrEmpty(monomObj.MonomString())) { PolynomialEquation.Add(monomObj); } } if (PolynomialEquation.Count > 0) { resultText.text = string.Join(" ", MonomUtils.PrintPolynomial(PolynomialEquation)); } else { resultText.text = "Polynom is 0."; } } }
private static void AdjustCoefficientIfSignIsSub(Monom monom, string sign) { switch (sign) { case Sub: monom.Coefficient *= -1; return; } }
private static bool TryJoinToCurrentIfIdentifiersEquals(Monom currentMonom, Monom addendMonom) { if (currentMonom.GetIdentifier() == addendMonom.GetIdentifier()) { currentMonom.Coefficient += addendMonom.Coefficient; return(true); } return(false); }
public bool MoveNext() { if (this.Current == null) { this.Current = new Monom(); return(true); } this.Current = null; return(false); }
public override Polynom VisitNumber(PolynomialParser.NumberContext context) { var monom = new Monom { Coefficient = double.Parse(context.coefficient().GetText()) }; AdjustCoefficientIfSignIsSub(monom, context.SIGN()?.GetText()); return(new Polynom { Monoms = new List <Monom> { monom } }); }
/// <summary> /// checks for every monom in equation, and integrates it /// the result is added to the resulted monoms list /// </summary> private void IntegratePolynomial() { resultedPolynomialEquation = new List <Monom>(); float coeficientResult = 0; float exponentResult = 0; foreach (var monom in initialPolynomialEquation) { coeficientResult = monom.Coefficient * monom.Sign / (monom.Exponent + 1); exponentResult = monom.Exponent + 1; var monomObj = new Monom(coeficientResult, exponentResult); if (!string.IsNullOrEmpty(monomObj.MonomString())) { resultedPolynomialEquation.Add(monomObj); } } }
/// <summary> /// searches in Monom list for every object with the same exponent then adds the coeficients acording to operation value /// it recalls itself until there is no objects with the same exponent /// </summary> /// <param name="firstPolynomial"> the polynomial equation with more than one monom with the same exponent </param> /// <returns>the second polynomial with added monoms of the same exponent value</returns> private List <Monom> AddEquation(List <Monom> firstPolynomial) { List <Monom> secondPolynomial = new List <Monom>(); float result = 0; for (int i = 0; i <= firstPolynomial.Count - 1; i++) { if (((i + 1) <= firstPolynomial.Count - 1) && (firstPolynomial[i].Exponent == firstPolynomial[i + 1].Exponent)) { result = (firstPolynomial[i].Coefficient * firstPolynomial[i].Sign) + (firstPolynomial[i + 1].Coefficient * firstPolynomial[i + 1].Sign); if (result != 0) { var monomObj = new Monom(result, firstPolynomial[i].Exponent); if (!string.IsNullOrEmpty(monomObj.MonomString())) { secondPolynomial.Add(monomObj); } } i++; } else { if (firstPolynomial[i].MonomString() != null) { secondPolynomial.Add(firstPolynomial[i]); } } } if (secondPolynomial.Count == firstPolynomial.Count) { return(secondPolynomial); } else { return(AddEquation(secondPolynomial)); } }
/// <summary> /// function to search on two polynom equations, multiply the coeficients and add the exponents /// it creates a new Monom List with the new values /// </summary> private void MultiplyArrays() { resultedAddedEquation = new List <Monom>(); ResultedPolynomialEquation = new List <Monom>(); float coeficientResult = 0; float exponentResult = 0; for (int i = 0; i <= polynomial1Input.PolynomialEquation.Count - 1; i++) { for (int j = 0; j <= polynomial2Input.PolynomialEquation.Count - 1; j++) { coeficientResult = (polynomial1Input.PolynomialEquation[i].Coefficient * polynomial1Input.PolynomialEquation[i].Sign) * (polynomial2Input.PolynomialEquation[j].Coefficient * polynomial2Input.PolynomialEquation[j].Sign); exponentResult = polynomial1Input.PolynomialEquation[i].Exponent + polynomial2Input.PolynomialEquation[j].Exponent; var monomObj = new Monom(coeficientResult, exponentResult); if (!string.IsNullOrEmpty(monomObj.MonomString())) { resultedAddedEquation.Add(monomObj); } } } resultedAddedEquation.Sort((a, b) => - 1 * a.Exponent.CompareTo(b.Exponent)); }
/// <summary> /// Adds two polynomial equations on a single Monom list /// sorts it in descending order acording to exponent value /// </summary> /// <param name="operation"> this is true if I want to apply the + operation and false for - operation</param> private void AddOrSubPolynomials(bool operation) { ResultedPolynomialEquation = new List <Monom>(); resultedAddedEquation = new List <Monom>(polynomial1Input.PolynomialEquation); foreach (Monom monomFrom2 in polynomial2Input.PolynomialEquation) { if (operation) { resultedAddedEquation.Add(monomFrom2); } else { var monomObj = new Monom(monomFrom2.Coefficient * monomFrom2.Sign * -1, monomFrom2.Exponent); if (!string.IsNullOrEmpty(monomObj.MonomString())) { resultedAddedEquation.Add(monomObj); } } } resultedAddedEquation.Sort((a, b) => - 1 * a.Exponent.CompareTo(b.Exponent)); }
public void ToString_ShouldBeAsExpected(Monom monom, string expected) { Assert.Equal(expected, monom.ToString()); }
/// <summary> /// this function takes two polynoms, checks the first coeficient of every equation and divides them if they can be divided /// the function is recalled until the polynoms can't be divided anymore /// </summary> /// <param name="polynomial1">the first polynomial</param> /// <param name="polynomial2">the second polynomial</param> /// <returns>the remainder from the division of the two polynoms</returns> private List <Monom> DividePolynomials(List <Monom> polynomial1, List <Monom> polynomial2) { List <Monom> DivideEquation = new List <Monom>(); List <Monom> resultedDivideEquation = new List <Monom>(); List <Monom> remainder = new List <Monom>(); float coeficientResult = 0; float exponentResult = 0; if (polynomial1[0].Exponent >= polynomial2[0].Exponent) { coeficientResult = (polynomial1[0].Coefficient * polynomial1[0].Sign) / (polynomial2[0].Coefficient * polynomial2[0].Sign); exponentResult = polynomial1[0].Exponent - polynomial2[0].Exponent; Monom DivideMonom = new Monom(coeficientResult, exponentResult); ResultedPolynomialEquation.Add(DivideMonom); foreach (var monom in polynomial2) { coeficientResult = monom.Coefficient * monom.Sign * (DivideMonom.Coefficient * DivideMonom.Sign); exponentResult = monom.Exponent + DivideMonom.Exponent; var monomObj = new Monom(coeficientResult, exponentResult); if (!string.IsNullOrEmpty(monomObj.MonomString())) { DivideEquation.Add(monomObj); } } resultedAddedEquation = new List <Monom>(polynomial1); foreach (var monom in DivideEquation) { var monomObj = new Monom(monom.Coefficient * monom.Sign * -1, monom.Exponent); if (!string.IsNullOrEmpty(monomObj.MonomString())) { resultedAddedEquation.Add(monomObj); } } resultedAddedEquation.Sort((a, b) => - 1 * a.Exponent.CompareTo(b.Exponent)); resultedDivideEquation = AddEquation(resultedAddedEquation); if (resultedDivideEquation.Count > 0) { if (resultedDivideEquation[0].Exponent >= polynomial2[0].Exponent) { return(DividePolynomials(resultedDivideEquation, polynomial2)); } else { remainder = resultedDivideEquation; return(remainder); } } else { return(null); } } else { ResultedPolynomialEquation = null; return(polynomial1); } }
public void Reset() { this.Current = null; }