Exemple #1
0
        public string MakeNormalForm(Polynomial polynom)
        {
            string result = "";
            for (int i = 0; i < polynom.Order; ++i )
            {
                if(polynom[i] != 0)
                {
                    int deg = (polynom.Order - i - 1);

                    //result += string.Format("{0}{1}", ((polynom[i] > 0 && result != string.Empty) ? "+" : "") + polynom[i],
                    //                        (deg > 0
                    //                             ? ("*x" +
                    //                                (deg > 1
                    //                                     ? ("^" + deg)
                    //                                     : ""
                    //                                ))
                    //                             : ""));

                    /* write coefficient if necessary                    */

                    // force plus sign if necessary
                    // plus sign isn't needed if coefficient is on first place of polynom
                    if (polynom[i] > 0 && result != string.Empty)
                    {
                        result += "+";
                    }

                    // no necessary to write coefficient equal to '1' or '-1' behind our x's
                    // force minus sign if there's coefficient equal to -1
                    if (Math.Round(polynom[i], 5) == -1 && deg !=0)
                    {
                        result += "-";
                    }
                    if(Math.Abs(Math.Round(polynom[i],5)) != 1  || deg == 0)
                    {
                        result += Math.Round(polynom[i],3);
                    }

                    // force '*x' if degree of x is bigger than 0
                    // force '^degree' if degree is bigger than 1
                    if(deg > 0)
                    {
                        // force * if previous coefficient wasn't equal to '1'
                        if (Math.Abs(Math.Round(polynom[i], 5)) != 1)
                        {
                            result += "*";
                        }
                        result += "x";
                        if(deg > 1)
                        {
                            result += "^" + deg;
                        }
                    }
                }
            }
            return result;
        }
Exemple #2
0
        public Polynomial LagrangePolynomial()
        {
            Polynomial result = new Polynomial(0);
            for(int i = 0; i < values.Length; ++i)
            {
                result = result + values[i]*BasicPolynomial(i);
            }

            return result;
        }
Exemple #3
0
 public Polynomial BasicPolynomial(int k)
 {
     Polynomial result = new Polynomial(1);
     for (int i = 0; i < nodes.Length; i++)
     {
         if (i != k)
         {
             result *= new Polynomial(1, -nodes[i]) / (nodes[k] - nodes[i]);
         }
     }
     return result;
 }
 public static Polynomial operator *(double number, Polynomial pFirst)
 {
     Polynomial result = new Polynomial(pFirst._coefficients);
     for (int i = 0; i < result.Order; ++i)
     {
         result[i] *= number;
     }
     return result;
 }
 public static Polynomial operator /(Polynomial pFirst, double number)
 {
     if(number == 0)
     {
         throw new DivideByZeroException();
     }
     Polynomial result = new Polynomial(pFirst._coefficients);
     for (int i = 0; i < result.Order; ++i)
     {
         result[i] /= number;
     }
     return result;
 }