473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Roots of polynomial and Api

Can someone please tell me that what is the root of polynomials and how to calculate them???
and whats this API?
Nov 17 '08 #1
3 3979
Ganon11
3,652 Expert 2GB
The root of a polynomial of the form:

P(x) = ax**n + bx**(n-1) + cx**(n-2) + ... + yx + z (in which a, b, c, ...y, and z are all real numbers, n is an integer, and x is the variable of interest) (x**n means x to the power of n)

is a value of x such that P(x) = 0.

For simple polynomials, there are easy roots. For instance, a polynomial of degree 0 (i.e. P(x) = a, where a is a real number) has infinite solutions if a = 0, and no solutions if a != 0.

A polynomial of degree 1 (i.e. P(x) = ax + b) has a root x = -(b/a) if and only if a != 0 (if a = 0, P(x) has no root).

A polynomial of degree 2 has roots according to the quadratic formula (Google it if you're interested).

I believe algorithms for computing roots of 3rd degree and 4th degree polynomials have been found, but they are non-trivial.

No algorithm exists to find roots of 5th or higher degree polynomials.

You may be able to use Newton's Method to approximate roots of any polynomial - however, Newton's method requires a 'good' initial guess.

I'm not sure what API has to do with polynomials.
Nov 17 '08 #2
The root of a polynomial of the form:

P(x) = ax**n + bx**(n-1) + cx**(n-2) + ... + yx + z (in which a, b, c, ...y, and z are all real numbers, n is an integer, and x is the variable of interest) (x**n means x to the power of n)

is a value of x such that P(x) = 0.

For simple polynomials, there are easy roots. For instance, a polynomial of degree 0 (i.e. P(x) = a, where a is a real number) has infinite solutions if a = 0, and no solutions if a != 0.

A polynomial of degree 1 (i.e. P(x) = ax + b) has a root x = -(b/a) if and only if a != 0 (if a = 0, P(x) has no root).

A polynomial of degree 2 has roots according to the quadratic formula (Google it if you're interested).

I believe algorithms for computing roots of 3rd degree and 4th degree polynomials have been found, but they are non-trivial.

No algorithm exists to find roots of 5th or higher degree polynomials.

You may be able to use Newton's Method to approximate roots of any polynomial - however, Newton's method requires a 'good' initial guess.

I'm not sure what API has to do with polynomials.

ok......thanks....
thanks a lot.....
Nov 17 '08 #3
I have the following C code for the solution of a Polynomial of degree DIM, for DIM up to degree 4. The Mathematician Galois (1700's) showed there can be no direct computational method of solution of polynomials above degree 4. API probably means 'Advanced Programmer Interface'. An interface between the programmer and the computer, to enable the programmer to easily develop programs. Code follows:-
Expand|Select|Wrap|Line Numbers
  1. if(DIM==0){
  2.                 if(coeffs[4]==coeffs[5])MessageBox(hwnd,"Equation is a Tautology!","Message from Polynomial",MB_OK);
  3.                 else MessageBox(hwnd,"Equation is a Impossible!","Error from Polynomial",MB_OK);
  4.                 EigR[0]=0.0;
  5.            EigR[1]=0.0;
  6.            EigR[2]=0.0;
  7.            EigR[3]=0.0;
  8.            EigI[0]=0.0;
  9.            EigI[1]=0.0;
  10.            EigI[2]=0.0;
  11.            EigI[3]=0.0;
  12.            } else if(DIM==1){EigR[0]=(coeffs[5]-coeffs[4])/coeffs[3];
  13.            EigR[1]=0.0;
  14.            EigR[2]=0.0;
  15.            EigR[3]=0.0;
  16.            EigI[0]=0.0;
  17.            EigI[1]=0.0;
  18.            EigI[2]=0.0;
  19.            EigI[3]=0.0;
  20.             } else if(DIM==2){a=1.0;b=coeffs[3]/coeffs[2];c=(coeffs[4]-coeffs[5])/coeffs[2];discrim=b*b-4*a*c;
  21.          if(discrim>0.0)
  22.          {discrim=sqrt(discrim)/2;
  23.            EigR[0]=-b/2+discrim;
  24.            EigR[1]=-b/2-discrim;
  25.            EigR[2]=0.0;
  26.            EigR[3]=0.0;
  27.            EigI[0]=0.0;
  28.            EigI[1]=0.0;
  29.            EigI[2]=0.0;
  30.            EigI[3]=0.0;
  31.            } else if(discrim==0.0)
  32.            {EigR[0]=-b/2;
  33.            EigR[1]=-b/2;
  34.            EigR[2]=0.0;
  35.            EigR[3]=0.0;
  36.            EigI[0]=0.0;
  37.            EigI[1]=0.0;
  38.            EigI[2]=0.0;
  39.            EigI[3]=0.0;
  40.              } else {discrim=sqrt(-discrim)/2;
  41.            EigR[0]=-b/2;
  42.            EigR[1]=-b/2;
  43.            EigR[2]=0.0;
  44.            EigR[3]=0.0;
  45.            EigI[0]=discrim;
  46.            EigI[1]=-discrim;
  47.            EigI[2]=0.0;
  48.            EigI[3]=0.0;
  49.              }
  50.          } else if(DIM==3)
  51.          {a=coeffs[2]/coeffs[1];
  52.            b=coeffs[3]/coeffs[1];
  53.            c=(coeffs[4]-coeffs[5])/coeffs[1];
  54.            q=(b/3.0)-((a*a)/9.0);r=(((a*b)-(3.0*c))/6.0)-((a*a*a)/27.0);
  55.            discrim=q*q*q+r*r;
  56.            third=1.0/3.0;
  57.            if(fabs(discrim)==0.0)discrim=0.0;
  58.            if(discrim>=0)
  59.            {temp1=r+sqrt(discrim);
  60.              if(temp1>=0)s1=pow(temp1,third);
  61.              else s1=-pow(-temp1,third);
  62.              temp2=r-sqrt(discrim);
  63.              if(temp2>=0)s2=pow(temp2,third);
  64.              else s2=-pow(-temp2,third);
  65.              EigR[0]=(s1+s2)-a/3.0;EigI[0]=0.0;
  66.              EigR[1]=-(s1+s2)/2.0-a/3.0;if(s1!=s2)EigI[1]=sqrt(3.0)*(s1-s2)/2.0; else EigI[1]=0.0;
  67.              EigR[2]=-(s1+s2)/2.0-a/3.0;if(s1!=s2)EigI[2]=-sqrt(3.0)*(s1-s2)/2.0; else EigI[2]=0.0;
  68.              EigR[3]=0.0;EigI[3]=0.0;
  69.              if(fabs(EigR[0])<1e-6)EigR[0]=0.0;
  70.              if(fabs(EigR[1])<1e-6)EigR[1]=0.0;
  71.              if(fabs(EigR[2])<1e-6)EigR[2]=0.0;
  72.              if(fabs(EigR[3])<1e-6)EigR[3]=0.0;
  73.              }
  74.            else {SQRT=sqrt(-discrim);
  75.      if(fabs(r)==0.0)theta=PIBY2; else if(r>0.0)theta=atan(SQRT/r); else theta=2.0*PIBY2+atan(SQRT/r);
  76.              theta/=3.0;R=pow(sqrt(r*r+SQRT*SQRT),third);
  77.              COS=cos(theta)*R;SIN=sin(theta)*R;
  78.              EigR[0]=2.0*COS-a/3.0;EigI[0]=0.0;
  79.              EigR[1]=-COS-a/3.0+sqrt(3.0)*SIN;EigI[1]=0.0;
  80.              EigR[2]=-COS-a/3.0-sqrt(3.0)*SIN;EigI[2]=0.0;
  81.              EigR[3]=0.0;EigI[3]=0.0;
  82.              if(fabs(EigR[0])<1e-6)EigR[0]=0.0;
  83.              if(fabs(EigR[1])<1e-6)EigR[1]=0.0;
  84.              if(fabs(EigR[2])<1e-6)EigR[2]=0.0;
  85.              if(fabs(EigR[3])<1e-6)EigR[3]=0.0;             
  86.              }
  87.            } else if(DIM==4){a3=coeffs[1]/coeffs[0];
  88.            a2=coeffs[2]/coeffs[0];
  89.            a1=coeffs[3]/coeffs[0];
  90.            a0=(coeffs[4]-coeffs[5])/coeffs[0];
  91.            a=-a2;b=(a1*a3-4.0*a0);c=-(a1*a1+a0*a3*a3-4.0*a0*a2);
  92.            q=(b/3.0)-((a*a)/9.0);r=(((a*b)-(3.0*c))/6.0)-((a*a*a)/27.0);
  93.            discrim=q*q*q+r*r;
  94.            third=1.0/3.0;
  95.            if(fabs(discrim)==0.0)discrim=0.0;
  96.            if(discrim>=0.0)
  97.            {temp1=r+sqrt(discrim);
  98.              if(temp1>=0)s1=pow(temp1,third);
  99.              else s1=-pow(-temp1,third);
  100.              temp2=r-sqrt(discrim);
  101.              if(temp2>=0)s2=pow(temp2,third);
  102.              else s2=-pow(-temp2,third);
  103.              u1=(s1+s2)-a/3.0;
  104.              SQRT1=a3*a3/4.0+u1-a2;
  105.              if(fabs(SQRT1)<1e-6)SQRT1=0.0;
  106.           if(SQRT1<0.0 && discrim==0.0){u1=-(s1+s2)/2.0-a/3.0;
  107.               SQRT1=a3*a3/4.0+u1-a2;}; 
  108.           if(SQRT1<0.0){MessageBox(hwnd,"Sorry! Something has gone Wrong with the Calculation of Quartic Roots!","Error from Polynomial",MB_OK);
  109.                                     for(j=0;j<4;j++){EigR[j]=0.0;EigI[j]=0.0;};}
  110.              else {SQRT1=sqrt(SQRT1);
  111.              SQRT2=u1*u1/4.0-a0;I=-1;
  112.              if(fabs(SQRT2)<1e-6)SQRT2=0.0;
  113.           if(SQRT2<0.0){MessageBox(hwnd,"Sorry! Something has gone Wrong with the Calculation of Quartic Roots!","Error from Polynomial",MB_OK);
  114.                                     for(j=0;j<4;j++){EigR[j]=0.0;EigI[j]=0.0;};}
  115.              else {SQRT2=sqrt(SQRT2);
  116.                AA[0]=1.0;BB[0]=a3/2.0-SQRT1;CC[0]=u1/2.0-SQRT2;
  117.                AA[1]=1.0;BB[1]=a3/2.0+SQRT1;CC[1]=u1/2.0+SQRT2;
  118.                AA[2]=1.0;BB[2]=a3/2.0-SQRT1;CC[2]=u1/2.0+SQRT2;
  119.                AA[3]=1.0;BB[3]=a3/2.0+SQRT1;CC[3]=u1/2.0-SQRT2;
  120.                for(m=0;m<4;m++){
  121.                  for(n=m;n<4;n++)
  122.                  {A1=AA[m];B1=BB[m];C1=CC[m];
  123.                   A2=AA[n];B2=BB[n];C2=CC[n];
  124.              if(fabs(C1*C2-a0)<0.01 && fabs(B1*C2+B2*C1-a1)<0.01  && fabs(A1*C2+A2*C1+B1*B2-a2)<0.01 && fabs(A1*B2+A2*B1-a3)<0.01){I=0;break;};
  125.                       }
  126.                       if(I!=-1)break;
  127.                     }
  128.               if(I==-1){MessageBox(hwnd,"Sorry! Something has gone Wrong with the Calculation of Quartic Roots!","Error from Polynomial",MB_OK);for(j=0;j<4;j++){EigR[j]=0.0;EigI[j]=0.0;};       
  129.                                     } else {
  130.              discrim1=B1*B1-4.0*A1*C1;
  131.              discrim2=B2*B2-4.0*A2*C2;
  132.              if(fabs(discrim1)<1e-6)discrim1=0.0;
  133.              if(fabs(discrim2)<1e-6)discrim2=0.0;
  134.              if(discrim1>=0.0)
  135.              {SQRT=sqrt(discrim1);
  136.                EigR[0]=(-B1+SQRT)/2.0;EigI[0]=0.0;
  137.                EigR[1]=(-B1-SQRT)/2.0;EigI[1]=0.0;
  138.                if(fabs(EigR[0])<1e-6)EigR[0]=0.0;
  139.                if(fabs(EigR[1])<1e-6)EigR[1]=0.0;
  140.                } else {
  141.                SQRT=sqrt(-discrim1);
  142.                EigR[0]=-B1/2.0;EigI[0]=SQRT/2.0;
  143.                EigR[1]=-B1/2.0;EigI[1]=-SQRT/2.0;
  144.                if(fabs(EigR[0])<1e-6)EigR[0]=0.0;
  145.                if(fabs(EigR[1])<1e-6)EigR[1]=0.0;
  146.                        }
  147.              if(discrim2>=0.0)
  148.              {SQRT=sqrt(discrim2);
  149.                EigR[2]=(-B2+SQRT)/2.0;EigI[2]=0.0;
  150.                EigR[3]=(-B2-SQRT)/2.0;EigI[3]=0.0;
  151.                if(fabs(EigR[2])<1e-6)EigR[2]=0.0;
  152.                if(fabs(EigR[3])<1e-6)EigR[3]=0.0;
  153.                } else {
  154.                SQRT=sqrt(-discrim2);
  155.                EigR[2]=-B2/2.0;EigI[2]=SQRT/2.0;
  156.                EigR[3]=-B2/2.0;EigI[3]=-SQRT/2.0;
  157.                if(fabs(EigR[2])<1e-6)EigR[2]=0.0;
  158.                if(fabs(EigR[3])<1e-6)EigR[3]=0.0;
  159.                                    }
  160.                                 }
  161.                              }
  162.                            }
  163.              } else {SQRT=sqrt(-discrim);
  164.              if(fabs(r)==0.0)theta=PIBY2; else if(r>0.0)theta=atan(SQRT/r); else theta=2.0*PIBY2+atan(SQRT/r);
  165.              theta/=3.0;R=pow(sqrt(r*r+SQRT*SQRT),third);
  166.              COS=cos(theta)*R;SIN=sin(theta)*R;
  167.              u[0]=2.0*COS-a/3.0;
  168.              u[1]=-COS-a/3.0+sqrt(3.0)*SIN;
  169.              u[2]=-COS-a/3.0-sqrt(3.0)*SIN;
  170.              I=-1;error=0;
  171.              for(i=0;i<3 && !error;i++)
  172.              {
  173.              SQRT1=a3*a3/4.0+u[i]-a2;
  174.              if(fabs(SQRT1)<1e-6)SQRT1=0.0;
  175.              if(SQRT1<0.0);
  176.              else {SQRT1=sqrt(SQRT1);
  177.              SQRT2=u[i]*u[i]/4.0-a0;
  178.              if(fabs(SQRT2)<1e-6)SQRT2=0.0;
  179.              if(SQRT2<0.0);
  180.              else {SQRT2=sqrt(SQRT2);
  181.                AA[0]=1;BB[0]=a3/2.0-SQRT1;CC[0]=u[i]/2.0-SQRT2;
  182.                AA[1]=1;BB[1]=a3/2.0+SQRT1;CC[1]=u[i]/2.0+SQRT2;
  183.                AA[2]=1;BB[2]=a3/2.0-SQRT1;CC[2]=u[i]/2.0+SQRT2;
  184.                AA[3]=1;BB[3]=a3/2.0+SQRT1;CC[3]=u[i]/2.0-SQRT2;
  185.                for(m=0;m<4;m++){
  186.                  for(n=m;n<4;n++)
  187.                  {A1=AA[m];B1=BB[m];C1=CC[m];
  188.                   A2=AA[n];B2=BB[n];C2=CC[n];
  189.              if(fabs(C1*C2-a0)<0.01 && fabs(B1*C2+B2*C1-a1)<0.01  && fabs(A1*C2+A2*C1+B1*B2-a2)<0.01 && fabs(A1*B2+A2*B1-a3)<0.01){I=i;break;};
  190.                       }
  191.                       if(I!=-1)break;
  192.                     }
  193.                     }
  194.                     }
  195.                     if(I!=-1)break;
  196.                   }
  197.         if(I==-1){MessageBox(hwnd,"Sorry! Something has gone Wrong with the Calculation of Quartic Roots!","Error from Polynomial",MB_OK);
  198.                                     for(j=0;j<4;j++){EigR[j]=0.0;EigI[j]=0.0;}
  199.              } else {
  200.              discrim1=B1*B1-4.0*A1*C1;
  201.              discrim2=B2*B2-4.0*A2*C2;
  202.              if(fabs(discrim1)<1e-6)discrim1=0.0;
  203.              if(fabs(discrim2)<1e-6)discrim2=0.0;
  204.              if(discrim1>=0.0)
  205.              {SQRT=sqrt(discrim1);
  206.                EigR[0]=(-B1+SQRT)/2.0;EigI[0]=0.0;
  207.                EigR[1]=(-B1-SQRT)/2.0;EigI[1]=0.0;
  208.                if(fabs(EigR[0])<1e-6)EigR[0]=0.0;
  209.                if(fabs(EigR[1])<1e-6)EigR[1]=0.0;
  210.                } else {
  211.                SQRT=sqrt(-discrim1);
  212.                EigR[0]=-B1/2.0;EigI[0]=SQRT/2.0;
  213.                EigR[1]=-B1/2.0;EigI[1]=-SQRT/2.0;
  214.                if(fabs(EigR[0])<1e-6)EigR[0]=0.0;
  215.                if(fabs(EigR[1])<1e-6)EigR[1]=0.0;
  216.                        }
  217.              if(discrim2>=0.0)
  218.              {SQRT=sqrt(discrim2);
  219.                EigR[2]=(-B2+SQRT)/2.0;EigI[2]=0.0;
  220.                EigR[3]=(-B2-SQRT)/2.0;EigI[3]=0.0;
  221.                if(fabs(EigR[2])<1e-6)EigR[2]=0.0;
  222.                if(fabs(EigR[3])<1e-6)EigR[3]=0.0;
  223.                } else {
  224.                SQRT=sqrt(-discrim2);
  225.                EigR[2]=-B2/2.0;EigI[2]=SQRT/2.0;
  226.                EigR[3]=-B2/2.0;EigI[3]=-SQRT/2.0;
  227.                if(fabs(EigR[2])<1e-6)EigR[2]=0.0;
  228.                if(fabs(EigR[3])<1e-6)EigR[3]=0.0;
  229.                                 }
  230.                        }             
  231.                 }
  232.              }
Jun 19 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Just | last post by:
While googling for a non-linear equation solver, I found Math::Polynomial::Solve in CPAN. It seems a great little module, except it's not Python... I'm especially looking for its poly_root()...
9
by: strotee76 | last post by:
What do I need to do to setTerm() for it to work properly? If you notice any other glaring issues, then please let me know. With this header file: ======================= #ifndef Polynomial_h...
1
by: Rubén Campos | last post by:
I've trying to implement polynomials of arbitrary order as a C++ template, as shown here: template <unsigned long int N> class Polynomial { public: Polynomial (); ~Polynomial ();
2
by: temper3243 | last post by:
Hi, I have been trying to solve the problem Roots of the polynomial 038 from mipt.ru (el judge). I have failed to do so. I tried to search on the net but found about contour integrals for...
0
by: Raymond L. Buvel | last post by:
If you are using the root finder in Numeric, and are having problems, check out the root finder in the ratfun module. My testing indicates that it will give the exact roots of a Wilkinson...
6
by: sekitoleko | last post by:
c program for newton raphson algorithm for finding roots of a polynomial
2
by: =?ISO-8859-1?Q?Sch=FCle_Daniel?= | last post by:
Hello NG, given this call to roots funtion from pylab In : roots() Out: array() as far as I understand it stands for a0+a1*x+a2*x^2 in the above case it yields 2x^2+2x = 2x(1+x) and the...
2
by: Peng Yu | last post by:
Hi, I have the following program which computes roots of a cubic function. The solution is sensitive to the type, which is due to the truncation error. 'long double T' gives three solutions, and...
1
by: madman228 | last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any...
1
by: Cassandra Walke | last post by:
I am having trouble writing my c++ code for a polynomial I need help with istream and operator* Here is my code: #include "polynomial.h" #include <iostream> using namespace std; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.