Debian Codegraph @

examples/C/root_of_unity.c

raw
1 /*
2 * Example code for libmps
3 *
4 * This code computes the n-th roots of unity using the
5 * mps_mpsolve() routine and print them to stdout.
6 *
7 * Can be compiled with:
8 * gcc -o root_of_unity -lm -lgmp -lmps root_of_unity.c
9 *
10 * Author: Leonardo Robol <[email protected]>
11 */
12
13
14 #include <mps/mps.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <gmp.h>
18 #include <string.h>
19
20 int
21 main (int argc, char **argv)
22 {
23 /* n is the degree of the polynomial,
24 * i is used as counter */
25 long int n = 0, i;
26 mps_monomial_poly *p;
27 mps_context *s;
28
29 mpq_t one, m_one, zero;
30
31 mpq_init (one);
32 mpq_init (m_one);
33 mpq_init (zero);
34
35 mpq_set_si (one, 1, 1);
36 mpq_set_si (m_one, -1, 1);
37 mpq_set_si (zero, 0, 1);
38
39 /* Get n from command line */
40 if (argc > 1)
41 {
42 n = atoi (argv[1]);
43 }
44
45 /* If parsing failed set n = 5 */
46 if (n == 0)
47 {
48 n = 5;
49 }
50
51 s = mps_context_new ();
52 p = mps_monomial_poly_new (s, n);
53
54 /* mps_monomial_poly_set_coefficient_q (s, p, 0, m_one, zero);
55 mps_monomial_poly_set_coefficient_q (s, p, n, one, zero); */
56
57 mps_context_select_algorithm(s, MPS_ALGORITHM_SECULAR_GA);
58
59 mps_monomial_poly_set_coefficient_d (s, p, 0, 1, 0);
60 mps_monomial_poly_set_coefficient_d (s, p, n, -1, 0);
61
62 /* Set the input polynomial */
63 mps_context_set_input_poly (s, MPS_POLYNOMIAL (p));
64
65 /* Allocate space to hold the results. We check only floating point results
66 * in here */
67 cplx_t *results = cplx_valloc (n);
68
69 /* Actually solve the polynomial */
70 mps_mpsolve (s);
71
72 /* Save roots computed in the vector results */
73 mps_context_get_roots_d (s, &results, NULL);
74
75 /* Print out roots */
76 for (i = 0; i < n; i++)
77 {
78 cplx_out_str (stdout, results[i]);
79 printf ("\n");
80 }
81
82 free (results);
83
84 return EXIT_SUCCESS;
85 }