public static Polynomial Newtonsecond(double [] Y ,double h ,int n ,double x0)
 {
     Double[] DalthY = new double[3];
      Double[] Dalth2Y = new double[2];
      double Dalth3Y = 0;
      for (int i = 0; i < 3; i++)
      {
          DalthY[i] = Y[i + 1] - Y[i];
      }
      for (int i = 0; i < 2; i++)
      {
          Dalth2Y[i] = DalthY[i + 1] - DalthY[i];
      }
      Dalth3Y = Dalth2Y[1] - Dalth2Y[0];
      Polynomial p = new Polynomial();
      Polynomial temp = new Polynomial();
      Polynomial temp1 = new Polynomial();
      Polynomial temp2 = new Polynomial();
      temp = Polynomial.insert(Dalth2Y[0], 0, temp);
      p = Polynomial.insert(1 / h, 1, p);
      p = Polynomial.insert(-1 * (x0 / h), 0, p);
      temp1 = Polynomial.insert(Dalth3Y, 0, temp1);
      temp1 = Polynomial.multi(temp1, p);
      temp1 = Polynomial.insert(-1*Dalth3Y, 0, temp1);
      temp1 = Polynomial.add(temp1, temp);
      h = h * h;
      temp2 = Polynomial.insert((1 / h), 0, temp2);
      temp1 = Polynomial.multi(temp1, temp2);
      return temp1;
 }
 public static Polynomial Derive(Polynomial temp)
 {
     Polynomial l=temp ;
     while((l != null)&&(l.power != 0))
     {
             l.amthal = l.power*l.amthal;
             l.power = l.power - 1;
             l=l.next;
     }
     return temp;
 }
        public static Polynomial insert(double x, double y, Polynomial l)
        {
            Polynomial temp = new Polynomial();
            temp.amthal = x;
            temp.power = y;
            temp.next = null;
            if ((l.next == null) && (l.power == 0) && (l.amthal == 0))
                l = temp;
            else
            {
                Polynomial prv = l;
                if (prv.power < temp.power)
                {
                    temp.next = prv;
                    l = temp;
                }
                else
                {
                    while ((prv.next != null) && (prv.next.power > temp.power))
                        prv = prv.next;
                    if (prv.next == null)
                    {
                        if (prv.power == temp.power)
                            prv.amthal = prv.amthal + temp.amthal;
                        else
                            prv.next = temp;
                    }
                    else
                    {
                        if ((prv.next.power == temp.power))
                            prv.next.amthal = prv.next.amthal + temp.amthal;
                        else if (prv.power == temp.power)
                            prv.amthal = prv.amthal + temp.amthal;
                        else
                        {
                            temp.next = prv.next;
                            prv.next = temp;
                        }
                    }
                }

            }
            return l;
        }
 public static Polynomial Newton(double [] Y ,double h ,int n ,double x0)
 {
     Double[] DalthY =new double[3];
     Double[] Dalth2Y = new double[2];
     double Dalth3Y = 0;
     for (int i = 0; i < 3; i++)
     {
         DalthY[i] = Y[i + 1] - Y[i];
     }
     for (int i = 0; i < 2; i++)
     {
         Dalth2Y[i] = DalthY[i + 1] - DalthY[i];
     }
     Dalth3Y = Dalth2Y[1] - Dalth2Y[0];
     Polynomial first = new Polynomial();
     Polynomial second = new Polynomial();
     Polynomial third= new Polynomial();
     Polynomial p= new Polynomial();
     Polynomial p2= new Polynomial();
     Polynomial temp = new Polynomial();
     Polynomial temp1 = new Polynomial();
     Polynomial temp2 = new Polynomial();
     p = Polynomial.insert(1/h, 1, p);
     p = Polynomial.insert(-1*(x0/h), 0, p);
     p2 = Polynomial.multi(p, p);
     second=Polynomial.insert((2*Dalth2Y[0])/2, 0,second );
     second = Polynomial.multi(second, p);
     second = Polynomial.insert(-1*(Dalth2Y[0]/h), 0, second);
     first = Polynomial.insert(DalthY[0], 0, first);
     third = Polynomial.insert((3*Dalth3Y)/6, 0, third);
     third = Polynomial.multi(p2, third);
     temp = Polynomial.insert(-1*(Dalth3Y), 0, temp);
     temp = Polynomial.multi(temp, p);
     third = Polynomial.add(third, temp);
     temp1 = Polynomial.insert((2*Dalth3Y)/6,0,temp1);
     third = Polynomial.add(third, temp1);
     third = Polynomial.add(third,first);
     third = Polynomial.add(third, second);
     temp2 = Polynomial.insert((1/h), 0, temp2);
     third = Polynomial.multi(third, temp2);
     return third;
 }
        public static Polynomial add(Polynomial n, Polynomial m)
        {
            Polynomial temp = n;
            Polynomial z = new Polynomial();
            while (temp != null)
            {

                double frst = temp.amthal;
                double frstpw = temp.power;
                z = insert(frst, frstpw, z);
                temp = temp.next;
            }
            temp = m;
            while (temp != null)
            {
                double frst = temp.amthal;
                double frstpw = temp.power;
                z = insert(frst, frstpw, z);
                temp = temp.next;
            }
            return z;
        }
 private void Calculate_Click(object sender, EventArgs e)
 {
     string Method = comboBox1.Text;
     string[] allMethod = {"newton"};
     string AnyOfMethod = Array.Find(allMethod, s => s.Equals(Method));
     if ((Method != AnyOfMethod))
     {
         MessageBox.Show("This Is Not Method", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         bool XarrayZero = false;
         bool YarrayZero = false;
         xcolum = CreatXarray(ref ThereIsErrorX);
         ycolum = CreatYarray(ref ThereIsErrorY);
         XarrayZero = Share.IfallZero(xcolum);
         YarrayZero = Share.IfallZero(ycolum);
         if ((ThereIsErrorX != true) && (ThereIsErrorY != true) && (XarrayZero != true) && (YarrayZero != true))
         {
             switch (Method)
             {
                 case "newton":
                     {
                         textBox2.Visible = true;
                         textBox3.Visible = true;
                         Polynomial newton = new Polynomial();
                         newton = deriveationclass.Newton(ycolum, xcolum[1] - xcolum[0], NumOfNode, xcolum[0]);
                         textBox2.Text = Polynomial.tostring(newton);
                         newton = deriveationclass.Newtonsecond(ycolum, xcolum[1] - xcolum[0], NumOfNode,
                                                                xcolum[0]);
                         textBox3.Text = Polynomial.tostring(newton);
                     }
                     break;
             }
         }
     }
 }
        public static string lagrang(double[] x, int node, double[] y)
        {
            Polynomial temp = new Polynomial();
            Polynomial onepoly = new Polynomial();

            bool loop = false;

            for (int i = 0; i < node; i++)
            {

                for (int j = 0; j < node; j++)
                {
                    loop = false;
                    if (i != j)
                    {
                        loop = true;
                        temp = new Polynomial();
                        double amthallagrang = x[i] - x[j];
                        temp = Polynomial.insert(y[i] * amthallagrang, 1, temp);
                        temp = Polynomial.insert(y[i] * amthallagrang * -x[j], 0, temp);

                    }
                    if (loop)
                        onepoly = Polynomial.add(temp, onepoly);
                }

            }
            return Polynomial.tostring(onepoly);
        }
        public static Polynomial Splin(double []x,double []y,int node,double point)
        {
            Polynomial temp=new Polynomial();

            int c = 0;
            for (int i = 0; i < node - 1; i++)
            {
                if ((x[i] <= point) && (x[i + 1] >= point))
                {
                    c = i;
                    break;
                }
            }
            double constant = (y[c + 1] - y[c])/(x[c + 1] - x[c]);
            temp = Polynomial.insert(constant, 1, temp);
            temp = Polynomial.insert(-1*constant*x[c], 0, temp);
            temp = Polynomial.insert(y[c], 0, temp);
            return temp;
        }
 public static Polynomial newtonemethod(double[] x, double[] y, double[,] tab, int n)
 {
     Polynomial temp = new Polynomial();
     Polynomial temp1 = new Polynomial();
     Polynomial temp2 = new Polynomial();
     Polynomial temp3 = new Polynomial();
     temp = Polynomial.insert(y[0], .0, temp);
     for (int i = 1; i < n; i++)
     {
         temp1 = new Polynomial();
         temp1 = Polynomial.insert(1.0, .0, temp1);
         temp3 = new Polynomial();
         temp3 = Polynomial.insert(tab[0, i], .0, temp3);
         for (int j = 0; j < i; j++)
         {
             temp2 = new Polynomial();
             temp2 = Polynomial.insert(1.0, 1.0, temp2);
             temp2 = Polynomial.insert(-1.0 * x[j], .0, temp2);
             temp1 = Polynomial.multi(temp2, temp1);
         }
         temp3 = Polynomial.multi(temp1, temp3);
         temp = Polynomial.add(temp, temp3);
     }
     return temp;
 }
        private void Send_Click(object sender, EventArgs e)
        {
            bool XarrayZero = false;
            bool YarrayZero = false;
            string Method = comboBox1.Text;
            string[] allMethod = { "General Methode", "Spline", "Lagrange", "Newton", "Least Square Method" };
            if (Method != ".......")
            {
                xcolum = CreatXarray(ref ThereIsErrorX);
                XarrayZero = Share.IfallZero(xcolum);
                if (XarrayZero != true)
                    ycolum = CreatYarray(ref ThereIsErrorY);
                YarrayZero = Share.IfallZero(ycolum);
                string AnyOfMethod = Array.Find(allMethod, s => s.Equals(Method));
                if ((XarrayZero != true) && (YarrayZero != true) && (ThereIsErrorX != true) && (ThereIsErrorY != true))
                    if ((Method == AnyOfMethod))
                        comboBox1.Enabled = false;
                    else
                        MessageBox.Show("This Is Not Method", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            XarrayZero = Share.IfallZero(xcolum);
            YarrayZero = Share.IfallZero(ycolum);
            switch (Method)
            {
                case "General Methode":
                    {

                        if ((XarrayZero != true) && (YarrayZero != true) && (ThereIsErrorX != true) && (ThereIsErrorY != true))
                        {
                            #region General Methode

                            double[,] matrx;
                            matrx = new double[NumOfNode, NumOfNode];
                            matrx = InterpolationClass.CreatVandrmondmatrix(NumOfNode, xcolum);
                            double determatrx = InterpolationClass.determine(matrx);
                            double[,] another = new double[NumOfNode, NumOfNode];
                            Polynomial temp = new Polynomial();
                            double[] Constant;
                            Constant = new double[NumOfNode];
                            for (int j = 0; j < NumOfNode; j++)
                            {
                                another = InterpolationClass.creatanothermatrix(j, xcolum, ycolum, NumOfNode);
                                double deternother = InterpolationClass.determine(another);
                                Constant[j] = deternother / determatrx;

                            }
                            for (int i = 0; i < Constant.Length; i++)
                            {

                                temp = Polynomial.insert(Constant[i], i, temp);

                            }
                            string result = Polynomial.tostring(temp);

                            if (MessageBox.Show("If You Have An Internet Connection Click Yes To Show The Function On Net Or Click No To See It Here", "Internet Connection",
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                                    DialogResult.Yes)
                            {
                                string url = "http://texify.com/?" + result;
                                Process.Start(url);
                            }
                            else
                            {
                                label3.Visible = true;
                                richTextBox1.Visible = true;
                                richTextBox1.Text = result;
                            }

                            Send.Enabled = false;
                            Refresh.Visible = true;

                            #endregion
                        }
                    }
                    break;
                case "Least Square Method":
                    {

                        if ((XarrayZero != true) && (YarrayZero != true) && (ThereIsErrorX != true) && (ThereIsErrorY != true))
                        {
                            #region Least Square Method
                            int exp = NumOfNode - 1;
                            double sum;
                            double[] tableX = new double[2 * exp];
                            double[] tableY = new double[NumOfNode];
                            for (int j = 0; j < (2 * exp); j++)
                            {
                                sum = 0;
                                for (int i = 0; i < NumOfNode; i++)
                                {
                                    sum = sum + Math.Pow(xcolum[i], j + 1);
                                    tableX[j] = sum;
                                }
                            }
                            for (int j = 0; j < NumOfNode; j++)
                            {
                                sum = 0;
                                for (int i = 0; i < NumOfNode; i++)
                                {
                                    if (j == 0)
                                    {
                                        sum = sum + ycolum[i];
                                    }
                                    else
                                    {
                                        sum = sum + (Math.Pow(xcolum[i], j) * ycolum[i]);
                                    }
                                    tableY[j] = sum;
                                }
                            }
                            double[,] squer = new double[NumOfNode, NumOfNode];
                            squer[0, 0] = NumOfNode;
                            for (int i = 1; i < NumOfNode; i++)
                            {
                                squer[0, i] = tableX[i - 1];
                            }
                            for (int i = 1; i < NumOfNode; i++)
                            {
                                for (int j = 0; j < NumOfNode; j++)
                                    squer[i, j] = tableX[i + j - 1];
                            }
                            double[] A = new double[NumOfNode];
                            double d = InterpolationClass.determine((double[,])squer.Clone());
                            for (int j = 0; j < NumOfNode; j++)
                                A[j] = InterpolationClass.ChangeXY((double[,])squer.Clone(), tableY, j, NumOfNode) / d;
                            Send.Enabled = false;
                            Refresh.Visible = true;

                            #endregion
                        }
                    }
                    break;
                case "Lagrange":
                    {
                        if ((XarrayZero != true) && (YarrayZero != true) && (ThereIsErrorX != true) && (ThereIsErrorY != true))
                        {
                            #region Lagrange

                            {
                                string result = InterpolationClass.lagrang(xcolum, NumOfNode, ycolum);
                                if (MessageBox.Show("If You Have An Internet Connection Click Yes To Show The Function On Net Or Click No To See It Here", "Internet Connection",
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                                    DialogResult.Yes)
                                {
                                    string url = "http://texify.com/?" + result;
                                    Process.Start(url);
                                }
                                else
                                {
                                    label3.Visible = true;
                                    richTextBox1.Visible = true;
                                    richTextBox1.Text = result;
                                }
                            }

                            #endregion
                        }

                    }
                    break;
                case "Spline":
                    {
                        if ((XarrayZero != true) && (YarrayZero != true) && (ThereIsErrorX != true) && (ThereIsErrorY != true))
                        {
                            #region Splin
                            {
                                double point = 0;
                                if ((textBox2.Text != null) && (Share.Isnumber(textBox2.Text)))
                                {
                                    point = double.Parse(textBox2.Text);
                                    string result = Polynomial.tostring(InterpolationClass.Splin(xcolum, ycolum, NumOfNode, point));
                                    if (MessageBox.Show("If You Have An Internet Connection Click Yes To Show The Function On Net Or Click No To See It Here", "Internet Connection",
                                                   MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                                   DialogResult.Yes)
                                    {
                                        string url = "http://texify.com/?" + result;
                                        Process.Start(url);
                                    }
                                    else
                                    {
                                        label3.Visible = true;
                                        richTextBox1.Visible = true;
                                        richTextBox1.Text = result;
                                    }

                                }
                                else
                                    MessageBox.Show("Error Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                            }
                            #endregion
                        }
                    }
                    break;
                case "Newton":
                    {
                        if ((XarrayZero != true) && (YarrayZero != true) && (ThereIsErrorX != true) && (ThereIsErrorY != true))
                        {
                            #region Newton
                            double[,] Temp = InterpolationClass.newtonftab(xcolum, ycolum, NumOfNode);
                            Polynomial Newton = InterpolationClass.newtonemethod(xcolum, ycolum, Temp, NumOfNode);
                            string result = Polynomial.tostring(Newton);
                            if (MessageBox.Show("If You Have An Internet Connection Click Yes To Show The Function On Net Or Click No To See It Here", "Internet Connection",
                                                  MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                                  DialogResult.Yes)
                            {
                                string url = "http://texify.com/?" + result;
                                Process.Start(url);
                            }
                            else
                            {
                                label3.Visible = true;
                                richTextBox1.Visible = true;
                                richTextBox1.Text = result;
                            }

                            #endregion
                        }
                    }
                    break;
                default:
                    {
                        if (Method == ".......")
                            MessageBox.Show("No Method Is Selceted ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
            }
        }
 public static Polynomial multi(Polynomial n, Polynomial m)
 {
     Polynomial temp1 = n;
     Polynomial temp2 = m;
     Polynomial q = new Polynomial();
     while (temp1 != null)
     {
         temp2 = m;
         while (temp2 != null)
         {
             double a = temp1.amthal * temp2.amthal;
             double wn = temp1.power + temp2.power;
             q = insert(a, wn, q);
             temp2 = temp2.next;
         }
         temp1 = temp1.next;
     }
     return q;
 }
        public static string tostring(Polynomial l)
        {
            Polynomial p = l;
            string temp = "";
            while (p != null)
            {
                if (p.power == 0)
                    if (p.amthal > 0)
                        temp += "+" + p.amthal.ToString();
                    else if (p.amthal == 0)
                        temp += "";
                    else
                        temp += p.amthal.ToString();
                else if ((p.amthal > 0) && (p.power > 0))
                    temp += "+" + p.amthal.ToString() + "X^" + p.power.ToString();
                else if ((p.amthal > 0) && (p.power < 0))
                    temp += "+" + p.amthal.ToString() + "X^" + p.power.ToString();
                else if ((p.amthal < 0) && (p.power != 0))
                    temp += p.amthal.ToString() + "X^" + p.power.ToString();

                p = p.next;
            }
            return temp;
        }