/// <summary> /// Create polynomial with coefficients in parameters. /// Number of parameter means degree which will have coefficient equals this parameter. /// </summary> /// <param name="eps">Coefficients which absolute value less than eps will be ignored.</param> /// <param name="coefficients">Coefficients near item with degree, equals number of parameter, starting from 0</param> public PolynomialClass(double[] coefficients, double eps = 1E-6) { if (coefficients == null) { throw new ArgumentNullException(); } if (coefficients.Count() == 0) { throw new ArgumentNullException(); } _polynomial = new PolynomItem[coefficients.Count()]; int arrayIndex = 0; for (int i = 0; i<coefficients.Count(); i++) { if (Math.Abs(coefficients[i]) > eps) { _polynomial[arrayIndex] = new PolynomItem(i, coefficients[i]); arrayIndex++; } } if (arrayIndex > 0) { Array.Resize(ref _polynomial, arrayIndex); } else { Array.Resize(ref _polynomial, 1); } }
/// <summary> /// Create polynomial with single item. /// </summary> /// <param name="degree">Degree of single item</param> /// <param name="coefficient">Coefficient near single item</param> /// <param name="eps">Coefficients which absolute value less than eps will be ignored.</param> public PolynomialClass(double coefficient, int degree, double eps = 1E-6) { if (degree < 0) { throw new ArgumentException(); } _polynomial = new PolynomItem[1]; if (Math.Abs(coefficient)<eps) { _polynomial[0] = new PolynomItem(); } else { _polynomial[0] = new PolynomItem(degree, coefficient); } }
/// <summary> /// Create polynomial with single item. /// </summary> /// <param name="item">PolynomItem variable with degree and coefficient</param> /// <param name="eps">Coefficients which absolute value less than eps will be ignored.</param> public PolynomialClass(PolynomItem item, double eps = 1E-6) { if (item.Degree < 0) { throw new ArgumentException(); } _polynomial = new PolynomItem[1]; if (Math.Abs(item.Coefficient) < eps) { _polynomial[0] = new PolynomItem(); } else { _polynomial[0] = item; } }
public bool Equals(PolynomItem obj) { if (obj.Degree == Degree && Math.Abs(obj.Coefficient - Coefficient) < 1E-6) { return true; } return false; }
public static PolynomialClass operator *(PolynomialClass first, PolynomialClass second) { if (first == null || second == null) { throw new ArgumentNullException(); } PolynomialClass result = new PolynomialClass(0,0); foreach (PolynomItem itemFirst in first._polynomial) { PolynomItem[] resultArray = new PolynomItem[second._polynomial.Count()]; int resultIndex = 0; foreach(PolynomItem itemSecond in second._polynomial) { resultArray[resultIndex] = new PolynomItem( itemFirst.Degree + itemSecond.Degree, itemFirst.Coefficient * itemSecond.Coefficient); resultIndex++; } result = result + new PolynomialClass(resultArray, 1E-6); } return result; }
public static PolynomialClass operator -(PolynomialClass first, PolynomialClass second) { if (first == null || second == null) { throw new ArgumentNullException(); } PolynomItem[] resultArray = new PolynomItem[first._polynomial.Count() + second._polynomial.Count()]; int firstIndex, secondIndex, maxDegree; firstIndex = 0; secondIndex = 0; maxDegree = Math.Max(first._polynomial.Last().Degree, second._polynomial.Last().Degree); int resultIndex = 0; int currentFirstDegree = 0, currentSecondDegree = 0; for (int i = 0; i <= maxDegree; i++) { if (firstIndex < first._polynomial.Count()) currentFirstDegree = first._polynomial[firstIndex].Degree; else //first polynomial is calculated currentFirstDegree = -1; if (secondIndex < second._polynomial.Count()) currentSecondDegree = second._polynomial[secondIndex].Degree; else //second polynomial is calculated currentSecondDegree = -2; if (currentFirstDegree == currentSecondDegree) { resultArray[resultIndex] = new PolynomItem(currentFirstDegree, first._polynomial[firstIndex].Coefficient - second._polynomial[secondIndex].Coefficient); resultIndex++; firstIndex++; secondIndex++; continue; } if (currentFirstDegree == i) { resultArray[resultIndex] = first._polynomial[firstIndex]; resultIndex++; firstIndex++; continue; } if (currentSecondDegree == i) { resultArray[resultIndex] = new PolynomItem(i, - second._polynomial[secondIndex].Coefficient); resultIndex++; secondIndex++; } } return new PolynomialClass(resultArray, 1E-6); }
private PolynomialClass(PolynomItem[] items, double eps = 1E-6) { if (items == null) { throw new ArgumentNullException(); } if (items.Count() == 0) { throw new ArgumentNullException(); } _polynomial = new PolynomItem[items.Count()]; int arrayIndex = 0; for (int i = 0; i<items.Count(); i++) { if (Math.Abs(items[i].Coefficient) > eps) { _polynomial[arrayIndex] = items[i]; arrayIndex++; } } if (arrayIndex > 0) { Array.Resize(ref _polynomial, arrayIndex); } else { Array.Resize(ref _polynomial, 1); } }