## File: Roots-of-Polynomials-Examples.html

package info (click to toggle)
gsl-ref-html 2.3-1
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (128 lines) | stat: -rw-r--r-- 5,151 bytes parent folder | download
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128  GNU Scientific Library – Reference Manual: Roots of Polynomials Examples

6.6 Examples

To demonstrate the use of the general polynomial solver we will take the polynomial P(x) = x^5 - 1 which has the following roots,

1, e^{2\pi i /5}, e^{4\pi i /5}, e^{6\pi i /5}, e^{8\pi i /5}

The following program will find these roots.

#include <stdio.h> #include <gsl/gsl_poly.h>  int main (void) {   int i;   /* coefficients of P(x) =  -1 + x^5  */   double a[6] = { -1, 0, 0, 0, 0, 1 };     double z[10];    gsl_poly_complex_workspace * w        = gsl_poly_complex_workspace_alloc (6);      gsl_poly_complex_solve (a, 6, w, z);    gsl_poly_complex_workspace_free (w);    for (i = 0; i < 5; i++)     {       printf ("z%d = %+.18f %+.18f\n",                i, z[2*i], z[2*i+1]);     }    return 0; }

The output of the program is,

\$ ./a.out
z0 = -0.809016994374947673 +0.587785252292473359 z1 = -0.809016994374947673 -0.587785252292473359 z2 = +0.309016994374947507 +0.951056516295152976 z3 = +0.309016994374947507 -0.951056516295152976 z4 = +0.999999999999999889 +0.000000000000000000

which agrees with the analytic result, z_n = \exp(2 \pi n i/5).