public ComplexPolynomial CalculateAddition(ComplexPolynomial p1)
        {
            int higherDegree         = (this.degree > p1.degree ? this.degree : p1.degree);
            ComplexPolynomial result = new ComplexPolynomial(higherDegree);
            Complex           zero   = new Complex();

            for (int i = 0; i <= higherDegree; i++)
            {
                if (i <= degree && i <= p1.degree)
                {
                    result.coefficients[i] = coefficients[i].add(p1.coefficients[i]);
                }
                else if (i <= degree)
                {
                    result.coefficients[i] = coefficients[i];
                }
                else
                {
                    result.coefficients[i] = p1.coefficients[i];
                }
                if (result.coefficients[i] != zero)
                {
                    result.degree = i;
                }
            }
            //while (result.coefficients[degree] == 0 && result.degree > 0) result.degree--;
            return(result);
        }
        public ComplexPolynomial CalculateSubtraction(ComplexPolynomial p1)
        {
            ComplexPolynomial p1_n = new ComplexPolynomial();

            p1_n.degree       = p1.degree;
            p1_n.coefficients = new Complex[p1_n.degree + 1];
            for (int i = 0; i <= p1_n.degree; i++)
            {
                p1_n.coefficients[i] = p1.coefficients[i].negate();
            }
            return(CalculateAddition(p1_n));
        }
        public ComplexPolynomial clone()
        {
            ComplexPolynomial cpy = new ComplexPolynomial(this.degree);

            //cpy.degree = this.degree;
            //cpy.coefficients = new Complex[this.degree + 1];
            for (int i = 0; i <= this.degree; i++)
            {
                cpy.coefficients[i] = this.coefficients[i].clone();
            }
            return(cpy);
        }
        //works now - Ryan :)
        public ComplexPolynomial CalculateProduct(ComplexPolynomial p1)
        {
            ComplexPolynomial product = new ComplexPolynomial(degree + p1.degree);

            for (int c2 = p1.degree; c2 >= 0; c2--)
            {
                for (int c1 = degree; c1 >= 0; c1--)
                {
                    product.coefficients[c1 + c2] += coefficients[c1] * p1.coefficients[c2];
                }
            }
            return(product);
        }
        //wip
        public ComplexPolynomial CalculateQuotient(ComplexPolynomial p1)
        {
            ComplexPolynomial quotient  = new ComplexPolynomial(this.degree - p1.degree);
            ComplexPolynomial remainder = this.clone();

            for (int c = this.degree; c >= p1.degree; c--)
            {
                ComplexPolynomial mult = new ComplexPolynomial(c - p1.degree);
                mult.coefficients[mult.degree] = remainder.coefficients[c] / p1.coefficients[p1.degree];
                quotient  += mult;
                remainder -= mult * p1;
            }
            return(quotient);
        }
        //wip
        public ComplexPolynomial CalculateRemainder(ComplexPolynomial p1)
        {
            // Code coppied from Calculate Quotient
            ComplexPolynomial quotient  = new ComplexPolynomial(this.degree - p1.degree);
            ComplexPolynomial remainder = this.clone();

            for (int c = this.degree; c >= p1.degree; c--)
            {
                ComplexPolynomial mult = new ComplexPolynomial(c - p1.degree);
                mult.coefficients[mult.degree] = remainder.coefficients[c] / p1.coefficients[p1.degree];
                quotient  += mult;
                remainder -= mult * p1;
            }
            return(remainder); // Return the REMAINDER part
        }
        public void Run()
        {
            ComplexPolynomial p2 = new ComplexPolynomial(new Complex[] {
                new Complex(0, 1),
                new Complex(1, -1),
                new Complex(1, 1)
            }, 2);

            Complex[] answer = new Complex[2];

            answer = p2.CalculateSecondDegreeComplexRoot();

            Console.Write("Polynomial: ");
            p2.Print();
            Console.WriteLine("");
            for (int i = 0; i < p2.getDegree(); i++)
            {
                Console.WriteLine("Root {0}: {1}", i + 1, answer[i].output(true));
            }
            Console.WriteLine("Verify below: (Should be really small ~0)");
            Console.WriteLine("With Root 1: {0}\nWith Root 2: {1}",
                              p2.RootVer(answer[0]).output(true),
                              p2.RootVer(answer[1]).output(true));
        }
        public void Run()
        {
            //Pair 1
            ComplexPolynomial p1a = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0)
            }, 2);
            ComplexPolynomial p1b = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0)
            }, 1);

            //calculate for pair 1
            ComplexPolynomial quo1 = p1a / p1b;
            ComplexPolynomial rem1 = p1a % p1b;

            //Pair 2
            ComplexPolynomial p2a = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(0, 0),
                new Complex(0, 0),
                new Complex(0, 0),
                new Complex(1, 0)
            }, 5);
            ComplexPolynomial p2b = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0)
            }, 2);

            //calculate for pair 2
            ComplexPolynomial quo2 = p2a / p2b;
            ComplexPolynomial rem2 = p2a % p2b;

            //Pair 3
            ComplexPolynomial p3a = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0)
            }, 10);
            ComplexPolynomial p3b = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0)
            }, 3);

            //calculate for pair 3
            ComplexPolynomial quo3 = p3a / p3b;
            ComplexPolynomial rem3 = p3a % p3b;

            Console.WriteLine("Pair 1 Polynomials: {0} and {1}\nQuotient: {2}\nRemainder: {3}\nVerify: {4}\n",
                              p1a.ToString(),
                              p1b.ToString(),
                              quo1.ToString(),
                              rem1.ToString(),
                              (quo1 * p1b + rem1).ToString()
                              );
            Console.WriteLine("Pair 2 Polynomials: {0} and {1}\nQuotient: {2}\nRemainder: {3}\nVerify: {4}\n",
                              p2a.ToString(),
                              p2b.ToString(),
                              quo2.ToString(),
                              rem2.ToString(),
                              (quo2 * p2b + rem2).ToString()
                              );
            Console.WriteLine("Pair 3 Polynomials: {0} and {1}\nQuotient: {2}\nRemainder: {3}\nVerify: {4}",
                              p3a.ToString(),
                              p3b.ToString(),
                              quo3.ToString(),
                              rem3.ToString(),
                              (quo3 * p3b + rem3).ToString()
                              );
        }
Пример #9
0
        public void Run()
        {
            //Pair 1
            ComplexPolynomial p1 = new ComplexPolynomial(new Complex[] {
                new Complex(1, 1),
                new Complex(1, -1),
                new Complex(0, 1)
            }, 2);
            ComplexPolynomial add1 = new ComplexPolynomial();
            ComplexPolynomial mul1 = new ComplexPolynomial();

            //calculate for pair 1
            add1 = p1.CalculateAddition(p1);
            mul1 = p1.CalculateProduct(p1);

            //Pair 2
            ComplexPolynomial p2a = new ComplexPolynomial(new Complex[] {
                new Complex(1, 1),
                new Complex(1, -1),
                new Complex(0, 1)
            }, 2);
            ComplexPolynomial p2b = new ComplexPolynomial(new Complex[] {
                new Complex(1, 0),
                new Complex(1, 0),
                new Complex(1, 0)
            }, 2);
            ComplexPolynomial add2 = new ComplexPolynomial();
            ComplexPolynomial mul2 = new ComplexPolynomial();

            //calculate for pair 2
            add2 = p2a.CalculateAddition(p2b);
            mul2 = p2a.CalculateProduct(p2b);

            //Pair 3
            ComplexPolynomial p3a = new ComplexPolynomial(new Complex[] {
                new Complex(1, 1),
                new Complex(1, -1),
                new Complex(0, 1),
                new Complex(0, 1)
            }, 3);
            ComplexPolynomial p3b = new ComplexPolynomial(new Complex[] {
                new Complex(0, 1),
                new Complex(0, 1),
                new Complex(0, 1),
                new Complex(0, 1)
            }, 3);
            ComplexPolynomial add3 = new ComplexPolynomial();
            ComplexPolynomial mul3 = new ComplexPolynomial();

            //calculate for pair 3
            add3 = p3a.CalculateAddition(p3b);
            mul3 = p3a.CalculateProduct(p3b);

            //Write to Console

            Console.WriteLine("Pair 1 Polynomials: {0} and {1}\nAddition: {2}\nMultiplication: {3}\n",
                              p1.ToString(),
                              p1.ToString(),
                              mul1.ToString(),
                              add1.ToString()
                              );
            Console.WriteLine("Pair 2 Polynomials: {0} and {1}\nAddition: {2}\nMultiplication: {3}\n",
                              p2a.ToString(),
                              p2b.ToString(),
                              mul2.ToString(),
                              add2.ToString()
                              );
            Console.WriteLine("Pair 3 Polynomials: {0} and {1}\nAddition: {2}\nMultiplication: {3}\n",
                              p3a.ToString(),
                              p3b.ToString(),
                              mul3.ToString(),
                              add3.ToString()
                              );
        }