Exemplo n.º 1
0
        /// <summary>
        /// Adds another polynomial to this
        /// </summary>
        /// <returns>
        /// this polynomial
        /// </returns>
        /// <param name="other">The polynomial to add</param>
        public MyPolynomial Add(MyPolynomial other)
        {
            double[] result;
            bool     longer = this.GetDegree() >= other.GetDegree();

            if (longer)
            {
                result = new double[_coeffs.Length];
            }
            else
            {
                result = new double[other.GetDegree() + 1];
            }
            for (int i = 0; i < result.Length; i++)
            {
                try
                {
                    result[i] = _coeffs[i] += other.CoeffAt(i);
                }
                catch (IndexOutOfRangeException)
                {
                    if (longer)
                    {
                        result[i] = _coeffs[i];
                    }
                    else
                    {
                        result[i] = other.CoeffAt(i);
                    }
                }
            }
            _coeffs = result;
            return(this);
        }
Exemplo n.º 2
0
        public static String CoeffsToString(MyPolynomial poly)
        {
            String result = "[";

            for (int i = 0; i < poly.GetDegree(); i++)
            {
                result += poly.CoeffAt(i) + ", ";
            }
            result += poly.CoeffAt(poly.GetDegree()) + "]:  ";
            return(result);
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            int numberOfTests = 50;

            // Test ToString by creating 100 random sets of arrays and
            // creating polynomials for each one and printing via
            // ToString
            Console.WriteLine("\n\nTesting ToString:\n");
            Random rnd = new Random();

            for (int i = 0; i < numberOfTests; i++)
            {
                MyPolynomial poly = RandomPolynomial(rnd, -20, 20);
                Console.WriteLine("\n" + CoeffsToString(poly));
                Console.WriteLine(poly.ToString());
            }

            // Test evaluate
            Console.WriteLine("\n\nTesting evaluating a polynomial");

            for (int i = 0; i < numberOfTests; i++)
            {
                double       x      = rnd.Next(-10, 10);
                MyPolynomial poly2  = RandomPolynomial(rnd, -10, 10);
                double       result = poly2.Evaluate(x);
                Console.WriteLine("\n({0}), {1} = {2}", poly2.ToString(), x, result);
            }

            // Test adding polynomials
            Console.WriteLine("\n\nTesting adding polynomials:\n");

            for (int i = 0; i < numberOfTests; i++)
            {
                MyPolynomial poly5 = RandomPolynomial(rnd, -5, 5);
                MyPolynomial poly6 = RandomPolynomial(rnd, -5, 5);
                Console.Write("\n({0}) + ({1}) = ", poly5.ToString(), poly6.ToString());
                poly5.Add(poly6);
                Console.WriteLine(poly5.ToString());
            }

            // Test multiplying polynomials
            Console.WriteLine("\n\nTesting multiplying polynomials:\n");

            for (int i = 0; i < numberOfTests; i++)
            {
                MyPolynomial poly5 = RandomPolynomial(rnd, -5, 5);
                MyPolynomial poly6 = RandomPolynomial(rnd, -5, 5);
                Console.Write("\n({0}) x ({1}) = ", poly5.ToString(), poly6.ToString());
                poly5.Multiply(poly6);
                Console.WriteLine(poly5.ToString());
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Multiplies this by another polynomial
 /// </summary>
 /// <returns>
 /// this polynomial
 /// </returns>
 /// <param name="other">The polynomial to multiply by</param>
 public MyPolynomial Multiply(MyPolynomial other)
 {
     double[] product = new double[_coeffs.Length + other.GetDegree()];
     for (int i = 0; i < _coeffs.Length; i++)
     {
         for (int j = 0; j < other.GetDegree() + 1; j++)
         {
             product[i + j] += _coeffs[i] * other.CoeffAt(j);
         }
     }
     _coeffs = product;
     return(this);
 }