# Roots of a polynomial - Fundamental Theorem of Algebra • ### Question

• Hello everybody:

The program written by GoToLoop (Gauss-Jordan method - Matrix), we are studying now, with the area of ​​mathematics and after we´ll work it with students. Thank you very much again, GoToLoop for your important help.

Now I just discovered a program in the web,( I think it's C + +), that calculates the roots of a polynomial (Fundamental Theorem of Algebra) After understanding it, I´ll try (into my own time) to convert it to MS Small Basic that is the language used at this school

I´m uploading it, just in case anyone will be interested in this topic. If not, let it go, please

Best regards (and really sorry, code is in spanish)

---------------------------------------------------------------------------------------------------------

// raíces de un polinomio

public void hallarRaices(){

tabla();

//el primer coeficiente a[m] es siempre 1

for(int i=1; i<n+1; i++){

if(cambiaSigno(i)){

//raíz compleja y su correspondiente conjugada

i++;

continue;

}

---------------------------------------------------------------------------------------------------------

//raíces simple y dobles

double logaritmo=Math.log(a[m][i])-2*Math.log(a[m-1][i]);

if(Math.abs(logaritmo)<CERO){

raizRealSimple(i);

}else{

raizRealDoble(i);

i++;

continue;

}

}

if(numComplejas==1){

unaRaizCompleja();

}

if(numComplejas==2){

dosRaicesComplejas();

}

}

---------------------------------------------------------------------------------------------------------

//Mostrar raíces

public void mostrarRaices(){

hallarRaices();

//raíces reales

System.out.println("Raíces reales");

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

System.out.println((double)Math.round(raicesReales[i]*100)/100+" ---> "+

valorPolinomio(raicesReales[i]));

}

System.out.println("");

//raíces complejas

System.out.println("Raíces complejas");

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

System.out.println(raicesComplejas[2*i]+" ---> "+

valorPolinomio(raicesComplejas[2*i]));

System.out.println(raicesComplejas[2*i+1]+" ---> "+

valorPolinomio(raicesComplejas[2*i]));

}

System.out.println("");

}

---------------------------------------------------------------------------------------------------------

// valor polinomio

public double valorPolinomio(double x){

double y=0.0;

//sucesivas potencias de x, se puede utilizar tambien la funcion Math.pow

double[] pot_x=new double[n+1];

pot_x=1.0;

for(int i=1; i<n+1; i++){

pot_x[i]=pot_x[i-1]*x;

}

//valores de los sucesivos términos

for(int i=0; i<n+1; i++){

y+=a[i]*pot_x[n-i];

}

return y;

}

carlosfmur - Buenos Aires

Sunday, June 3, 2012 6:35 AM

• Holá Carlos!

When I saw that, I wanted to convert it as well. But as soon as I just started to analyze it, I've spotted, at the very beginning of the code, a call to function tabla()!!!
I've tried to browse within the source site to find it; but no luck. Only more mentions of it from other codes also needing it!
Only another day, when I decided to deep search for it, using an advanced domain search, I was able to see it in its raw glory!
So today I decided to convert this particular function to SB.
Besides converting it, I had to study its math background to know which data it needed fed for tabla() to work!
It was a little hard for I don't know much math!  @_@
Well, here's tabla() function, now a subroutine :P, working for SB. I'll see the rest of the puzzle later.

http://www.sc.ehu.es/sbweb/fisica/cursoJava/numerico/raices/graeffe/graeffe.htm

x³ - 4x² + x + 6 = 0:  ```'  Dandelin-Gräffe Method
'  Adapted from Java to SB by GoToLoop
'  Site: http://www.sc.ehu.es/sbweb/fisica/cursoJava/numerico/raices/graeffe/graeffe.htm
'  Forum: http://social.msdn.microsoft.com/Forums/en-US/smallbasic

'  x³ - 4x² + x + 6 = 0
'  a0 = 1; a1 = -4; a2 = 1; a3 = 6
'  x1 = 3; x2 = 2; x3 = -1
'  n = 3 (grado del polinomio)
'  MAX_ITER = (número de veces que se repite el proceso de elevación al cuadrado)

'  a[Max_Iter][n]
'  a = coef[n]
'_______________________________________________________________'
LF  = Text.GetCharacter(10)
TAB = Text.GetCharacter(9)

'==============================================================='Max_Iter   = 6'                                                                                 x³ - 4x² + x + 6 = 0coef = "0=1;1=-4;2=1;3=6;"     '  <--- a0 = 1; a1 = -4; a2 = 1; a3 = 6'===============================================================''Max_Iter   = 5'                                                                                 x³ - 7x² + 16x - 12 = 0 -> x1 = 2; x2 = 3'coef = "0=1;1=-7;2=16;3=-12;"  '  <--- a0 = 1; a1 = -7; a2 = 16; a3 = -12'==============================================================='a = coef  ' <--- a[] contains all the polynomial's coefficients!Max_Iter_M = Max_Iter - 1

n  = Array.GetItemCount( a ) - 1
nP = n + 1
nM = n - 1
nH = Math.Floor( n/2 )

tabla()

TextWindow.ForegroundColor = "Red"TextWindow.WriteLine("Grado      : " + n)TextWindow.WriteLine("Iteracions : " + Max_Iter_M)TextWindow.WriteLine("Potencia   : " + powM2 + LF)TextWindow.ForegroundColor = "Green"For m = 0 To Max_Iter_M  TextWindow.WriteLine(m + "(" + Math.Power(2,m) + ")" + TAB + a[m] + LF)EndForTextWindow.ForegroundColor = "Magenta"
'_______________________________________________________________'
Sub tabla
' ---  int k, l, sign

'//divide el polinomio por el primer coeficiente, las raíces no cambian:

For i=1 To n                   ' <---  for (int i=1; i<n+1; i++) {
a[i] = a[i] / a
EndFor

a = 1

For m=1 To Max_Iter_M
For i=0 To n                 ' <--- for (int i=0; i<n+1; i++) {
a[m][i] = a[m-1][i] * a[m-1][i]
EndFor
'//dobles productos:
For i=1 To nM                ' <--- for (int i=1; i<n; i++) {
For s=1 To nH              ' <--- for (int s=1; s<n/2+1; s++) {

k = i-s
l = i+s

If k < 0 Or l > n Then  '//términos simétricos
s = nH   ' <---  break;

Else
If Math.Remainder(s,2) = 0 Then  ' <--- sign= (s%2 == 0)? +1: -1;
sign =  1
Else
sign = -1
EndIf

a[m][i] = a[m][i] + sign*2 * a[m-1][k] * a[m-1][l]

EndIf

EndFor
EndFor

EndFor

'//potencia de m de 2:
m = m-1
powM2 = 1

For i=1 To m                   ' <--- for (int i=1; i<=m; i++) {
powM2 = powM2 * 2
EndFor

EndSub
'________________________________________________________________'```

Tuesday, June 5, 2012 9:25 PM
• oh GoToLoop, this is great

I appreciate your effort and concern

It is very important, for the great application that has the calculation of polynomials in fields such as engineering, economics, biology, etc.

Thank you very much again and we´ll keep in contact, I hope

Best regards

carlosfmur - Buenos Aires

Wednesday, June 6, 2012 12:34 AM

### All replies

• Oh, sorry for my carelessness
I omitted the link to where I got the original program code, to solve thid issue

http://www.sc.ehu.es/sbweb/fisica/cursoJava/numerico/raices/graeffe/graeffe2.htm

I can not test it because I don´t know this programming language (is this C + +...or another?)

Since this original code, I will depart to arrive at Small Basic code, as far that my own potential as a beginner , will allow it

Many thanks in advance and best regards

carlosfmur - Buenos Aires

Sunday, June 3, 2012 9:26 AM
• As the URL of the original program contains  ../cursoJava/.. the language is probably Java

If you go to the website www.sc.ehu.es/sbweb/fisica you get an explanation (in spanish)
Monday, June 4, 2012 8:56 AM
• Hello Wh Turner33
www.sc.ehu.es/sbweb/fisica

From what I understand it is Java, which is far from my platform

For me, it is easy to convert C # code to Small Basic
So I´ll keep working on solve the algorithm directly in Small Basic
As it is a work of integration ( mathematics/ ICT) I will work with the math teacher whom I have been excited with our Small Basic.
Thank you

Best regards

carlosfmur - Buenos Aires

Monday, June 4, 2012 11:00 AM
• Yes, this is java code. It would be really easy to port to C#--- all i see is the need to change all the "System.Out.Println" statements to "Console.WriteLine".
Monday, June 4, 2012 1:22 PM
• Holá Carlos!

When I saw that, I wanted to convert it as well. But as soon as I just started to analyze it, I've spotted, at the very beginning of the code, a call to function tabla()!!!
I've tried to browse within the source site to find it; but no luck. Only more mentions of it from other codes also needing it!
Only another day, when I decided to deep search for it, using an advanced domain search, I was able to see it in its raw glory!
So today I decided to convert this particular function to SB.
Besides converting it, I had to study its math background to know which data it needed fed for tabla() to work!
It was a little hard for I don't know much math!  @_@
Well, here's tabla() function, now a subroutine :P, working for SB. I'll see the rest of the puzzle later.

http://www.sc.ehu.es/sbweb/fisica/cursoJava/numerico/raices/graeffe/graeffe.htm

x³ - 4x² + x + 6 = 0:  ```'  Dandelin-Gräffe Method
'  Adapted from Java to SB by GoToLoop
'  Site: http://www.sc.ehu.es/sbweb/fisica/cursoJava/numerico/raices/graeffe/graeffe.htm
'  Forum: http://social.msdn.microsoft.com/Forums/en-US/smallbasic

'  x³ - 4x² + x + 6 = 0
'  a0 = 1; a1 = -4; a2 = 1; a3 = 6
'  x1 = 3; x2 = 2; x3 = -1
'  n = 3 (grado del polinomio)
'  MAX_ITER = (número de veces que se repite el proceso de elevación al cuadrado)

'  a[Max_Iter][n]
'  a = coef[n]
'_______________________________________________________________'
LF  = Text.GetCharacter(10)
TAB = Text.GetCharacter(9)

'==============================================================='Max_Iter   = 6'                                                                                 x³ - 4x² + x + 6 = 0coef = "0=1;1=-4;2=1;3=6;"     '  <--- a0 = 1; a1 = -4; a2 = 1; a3 = 6'===============================================================''Max_Iter   = 5'                                                                                 x³ - 7x² + 16x - 12 = 0 -> x1 = 2; x2 = 3'coef = "0=1;1=-7;2=16;3=-12;"  '  <--- a0 = 1; a1 = -7; a2 = 16; a3 = -12'==============================================================='a = coef  ' <--- a[] contains all the polynomial's coefficients!Max_Iter_M = Max_Iter - 1

n  = Array.GetItemCount( a ) - 1
nP = n + 1
nM = n - 1
nH = Math.Floor( n/2 )

tabla()

TextWindow.ForegroundColor = "Red"TextWindow.WriteLine("Grado      : " + n)TextWindow.WriteLine("Iteracions : " + Max_Iter_M)TextWindow.WriteLine("Potencia   : " + powM2 + LF)TextWindow.ForegroundColor = "Green"For m = 0 To Max_Iter_M  TextWindow.WriteLine(m + "(" + Math.Power(2,m) + ")" + TAB + a[m] + LF)EndForTextWindow.ForegroundColor = "Magenta"
'_______________________________________________________________'
Sub tabla
' ---  int k, l, sign

'//divide el polinomio por el primer coeficiente, las raíces no cambian:

For i=1 To n                   ' <---  for (int i=1; i<n+1; i++) {
a[i] = a[i] / a
EndFor

a = 1

For m=1 To Max_Iter_M
For i=0 To n                 ' <--- for (int i=0; i<n+1; i++) {
a[m][i] = a[m-1][i] * a[m-1][i]
EndFor
'//dobles productos:
For i=1 To nM                ' <--- for (int i=1; i<n; i++) {
For s=1 To nH              ' <--- for (int s=1; s<n/2+1; s++) {

k = i-s
l = i+s

If k < 0 Or l > n Then  '//términos simétricos
s = nH   ' <---  break;

Else
If Math.Remainder(s,2) = 0 Then  ' <--- sign= (s%2 == 0)? +1: -1;
sign =  1
Else
sign = -1
EndIf

a[m][i] = a[m][i] + sign*2 * a[m-1][k] * a[m-1][l]

EndIf

EndFor
EndFor

EndFor

'//potencia de m de 2:
m = m-1
powM2 = 1

For i=1 To m                   ' <--- for (int i=1; i<=m; i++) {
powM2 = powM2 * 2
EndFor

EndSub
'________________________________________________________________'```

Tuesday, June 5, 2012 9:25 PM
• oh GoToLoop, this is great

I appreciate your effort and concern

It is very important, for the great application that has the calculation of polynomials in fields such as engineering, economics, biology, etc.

Thank you very much again and we´ll keep in contact, I hope

Best regards

carlosfmur - Buenos Aires

Wednesday, June 6, 2012 12:34 AM