public void OperatorSubtractTests(double[] firstCoefficients, double[] secondCoefficients, double eps)
 {
     PolynomialClass firstSummand = new PolynomialClass(firstCoefficients, eps);
     PolynomialClass secondSummand = new PolynomialClass(secondCoefficients, eps);
     double[] sumArray = new double[Math.Max(firstCoefficients.Count(), secondCoefficients.Count())];
     for (int i = 0; i < sumArray.Count(); i++)
     {
         if (i < firstCoefficients.Count() && i < secondCoefficients.Count())
         {
             sumArray[i] = firstCoefficients[i] - secondCoefficients[i];
         }
         if (i > firstCoefficients.Count())
         {
             sumArray[i] = secondCoefficients[i];
         }
         if (i > secondCoefficients.Count())
         {
             sumArray[i] = firstCoefficients[i];
         }
     }
     PolynomialClass sum = new PolynomialClass(sumArray, eps);
     Assert.AreEqual(sum, firstSummand - secondSummand);
 }
 public void OperatorMultiplyTests(double[] firstCoefficients, double[] secondCoefficients, double eps)
 {
     PolynomialClass firstMultiplier = new PolynomialClass(firstCoefficients, eps);
     PolynomialClass secondMultiplier = new PolynomialClass(secondCoefficients, eps);
     double[] mulArray = new double[firstCoefficients.Count() * secondCoefficients.Count()];
     for (int i = 0; i < firstCoefficients.Count(); i++)
     {
         if (Math.Abs(firstCoefficients[i]) > eps)
         for (int j = 0; j<secondCoefficients.Count(); j++)
         {
             if (Math.Abs(secondCoefficients[j]) > eps)
             {
                 mulArray[i + j] += firstCoefficients[i]*secondCoefficients[j];
             }
         }
     }
     PolynomialClass mul = new PolynomialClass(mulArray, eps);
     Assert.AreEqual(mul, firstMultiplier * secondMultiplier);
 }
 public void GetHashCodeTests(double[] firstCoefficients, double[] secondCoefficients, double eps)
 {
     PolynomialClass firstPolynomial = new PolynomialClass(firstCoefficients, eps);
     PolynomialClass secondPolynomial = new PolynomialClass(secondCoefficients, eps);
     Assert.AreEqual(firstPolynomial.GetHashCode(), secondPolynomial.GetHashCode());
     Assert.AreEqual(firstPolynomial, secondPolynomial);
 }
 static void Main(string[] args)
 {
     PolynomialClass p1 = new PolynomialClass(coefficients: new double[ ] { 10, 0, 0, -2.5, 9});
     PolynomialClass p2 = new PolynomialClass(-11.11111111, 3, eps: 1E-4);
     PolynomialClass p3 = p1 + p2;
     PolynomialClass p4 = p1 + p1;
     PolynomialClass p5 = p1 - p2;
     PolynomialClass p6 = p1 - p1;
     PolynomialClass p7 = p1*p2;
     p6 = p6 + p6;
     p6 = p6 - p6;
     for (int i = -2; i<3; i++)
     {
         Console.WriteLine(p1.Calculate(i));//10-2.5x^3+9x^4
     }
     Console.WriteLine();
     for (int i = -2; i < 3; i++)
     {
         Console.WriteLine(p2.Calculate(i));//-11.11111x^3
     }
     Console.WriteLine();
     for (int i = -2; i < 3; i++)
     {
         Console.WriteLine(p3.Calculate(i));//10-13.6111x^3+9x^4
     }
     Console.WriteLine();
     for (int i = -2; i < 3; i++)
     {
         Console.WriteLine(p4.Calculate(i));//20-5x^3+18x^4
     }
     for (int i = -2; i < 3; i++)
     {
         Console.WriteLine(p5.Calculate(i));//10+8.6111x^3+9x^4
     }
     for (int i = -2; i < 3; i++)
     {
         Console.WriteLine(p6.Calculate(i));//0
     }
     //GetHash for a+bx, a,b = [-100;100], step 0.05
     List<int> hashes = new List<int>();
     double[] coefficients;
     //for(double coefficientDegree0 = -100; coefficientDegree0 < 100; coefficientDegree0 += 0.05)
     //{
     //    for (double coefficientDegree1 = -100; coefficientDegree1 < 100; coefficientDegree1 += 0.05)
     //    {
     //        coefficients = new double[] {coefficientDegree0, coefficientDegree1};
     //        hashes.Add(new PolynomialClass(coefficients).GetHashCode());
     //    }
     //}
     //Console.WriteLine(hashes.Count() - hashes.Distinct().Count());
     
     Random random = new Random();
     for (int i = 0; i<10000000; i++)
     {
         coefficients = new double[random.Next(10) + 1];
         for (int j = 0; j < coefficients.Count(); j++)
         {
             coefficients[j] = random.Next(20000) - 10000 + random.NextDouble();
         }
         hashes.Add(new PolynomialClass(coefficients).GetHashCode());
     }
     Console.WriteLine(hashes.Count() - hashes.Distinct().Count());
     //Console.WriteLine(p1.GetHashCode());
     //Console.WriteLine(p2.GetHashCode());
     //Console.WriteLine(p3.GetHashCode());
     //Console.WriteLine(p4.GetHashCode());
     //Console.WriteLine(p5.GetHashCode());
     //Console.WriteLine(p6.GetHashCode());
     //Console.WriteLine(p7.GetHashCode());
     Console.WriteLine();
     Console.ReadLine();
 }
 public bool Equals(PolynomialClass polynomial)
 {
     if (polynomial == null)
     {
         return false;
     }
     foreach (PolynomItem item in _polynomial)
     {
         if (Math.Abs(polynomial.CoefficientNearDegree(item.Degree) - item.Coefficient) > 1E-6)
         {
             return false;
         }
     }
     return true;
 }
 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;
 }