1/* mpc_rootofunity -- primitive root of unity.
2
3Copyright (C) 2012, 2016 INRIA
4
5This file is part of GNU MPC.
6
7GNU MPC is free software; you can redistribute it and/or modify it under
8the terms of the GNU Lesser General Public License as published by the
9Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with this program. If not, see http://www.gnu.org/licenses/ .
19*/
20
21#include <stdio.h> /* for MPC_ASSERT */
22#include "mpc-impl.h"
23
24static unsigned long
25gcd (unsigned long a, unsigned long b)
26{
27   if (b == 0)
28      return a;
29   else return gcd (b, a % b);
30}
31
32/* put in rop the value of exp(2*i*pi*k/n) rounded according to rnd */
33int
34mpc_rootofunity (mpc_ptr rop, unsigned long n, unsigned long k, mpc_rnd_t rnd)
35{
36   unsigned long g;
37   mpq_t kn;
38   mpfr_t t, s, c;
39   mpfr_prec_t prec;
40   int inex_re, inex_im;
41   mpfr_rnd_t rnd_re, rnd_im;
42
43   if (n == 0) {
44      /* Compute exp (0 + i*inf). */
45      mpfr_set_nan (mpc_realref (rop));
46      mpfr_set_nan (mpc_imagref (rop));
47      return MPC_INEX (0, 0);
48   }
49
50   /* Eliminate common denominator. */
51   k %= n;
52   g = gcd (k, n);
53   k /= g;
54   n /= g;
55
56   /* Now 0 <= k < n and gcd(k,n)=1. */
57
58   /* We assume that only n=1, 2, 3, 4, 6 and 12 may yield exact results
59      and treat them separately; n=8 is also treated here for efficiency
60      reasons. */
61   if (n == 1)
62     {
63       /* necessarily k=0 thus we want exp(0)=1 */
64       MPC_ASSERT (k == 0);
65       return mpc_set_ui_ui (rop, 1, 0, rnd);
66     }
67   else if (n == 2)
68     {
69       /* since gcd(k,n)=1, necessarily k=1, thus we want exp(i*pi)=-1 */
70       MPC_ASSERT (k == 1);
71       return mpc_set_si_si (rop, -1, 0, rnd);
72     }
73   else if (n == 4)
74     {
75       /* since gcd(k,n)=1, necessarily k=1 or k=3, thus we want
76          exp(2*i*pi/4)=i or exp(2*i*pi*3/4)=-i */
77       MPC_ASSERT (k == 1 || k == 3);
78       if (k == 1)
79         return mpc_set_ui_ui (rop, 0, 1, rnd);
80       else
81         return mpc_set_si_si (rop, 0, -1, rnd);
82     }
83   else if (n == 3 || n == 6)
84     {
85       MPC_ASSERT ((n == 3 && (k == 1 || k == 2)) ||
86                   (n == 6 && (k == 1 || k == 5)));
87       /* for n=3, necessarily k=1 or k=2: -1/2+/-1/2*sqrt(3)*i;
88          for n=6, necessarily k=1 or k=5: 1/2+/-1/2*sqrt(3)*i */
89       inex_re = mpfr_set_si (mpc_realref (rop), (n == 3 ? -1 : 1),
90                              MPC_RND_RE (rnd));
91       /* inverse the rounding mode for -sqrt(3)/2 for zeta_3^2 and zeta_6^5 */
92       rnd_im = MPC_RND_IM (rnd);
93       if (k != 1)
94         rnd_im = INV_RND (rnd_im);
95       inex_im = mpfr_sqrt_ui (mpc_imagref (rop), 3, rnd_im);
96       mpc_div_2ui (rop, rop, 1, MPC_RNDNN);
97       if (k != 1)
98         {
99           mpfr_neg (mpc_imagref (rop), mpc_imagref (rop), MPFR_RNDN);
100           inex_im = -inex_im;
101         }
102       return MPC_INEX (inex_re, inex_im);
103     }
104   else if (n == 12)
105     {
106       /* necessarily k=1, 5, 7, 11:
107          k=1: 1/2*sqrt(3) + 1/2*I
108          k=5: -1/2*sqrt(3) + 1/2*I
109          k=7: -1/2*sqrt(3) - 1/2*I
110          k=11: 1/2*sqrt(3) - 1/2*I */
111       MPC_ASSERT (k == 1 || k == 5 || k == 7 || k == 11);
112       /* inverse the rounding mode for -sqrt(3)/2 for zeta_12^5 and zeta_12^7 */
113       rnd_re = MPC_RND_RE (rnd);
114       if (k == 5 || k == 7)
115         rnd_re = INV_RND (rnd_re);
116       inex_re = mpfr_sqrt_ui (mpc_realref (rop), 3, rnd_re);
117       inex_im = mpfr_set_si (mpc_imagref (rop), k < 6 ? 1 : -1,
118                              MPC_RND_IM (rnd));
119       mpc_div_2ui (rop, rop, 1, MPC_RNDNN);
120       if (k == 5 || k == 7)
121         {
122           mpfr_neg (mpc_realref (rop), mpc_realref (rop), MPFR_RNDN);
123           inex_re = -inex_re;
124         }
125       return MPC_INEX (inex_re, inex_im);
126     }
127   else if (n == 8)
128     {
129       /* k=1, 3, 5 or 7:
130          k=1: (1/2*I + 1/2)*sqrt(2)
131          k=3: (1/2*I - 1/2)*sqrt(2)
132          k=5: -(1/2*I + 1/2)*sqrt(2)
133          k=7: -(1/2*I - 1/2)*sqrt(2) */
134       MPC_ASSERT (k == 1 || k == 3 || k == 5 || k == 7);
135       rnd_re = MPC_RND_RE (rnd);
136       if (k == 3 || k == 5)
137         rnd_re = INV_RND (rnd_re);
138       rnd_im = MPC_RND_IM (rnd);
139       if (k > 4)
140         rnd_im = INV_RND (rnd_im);
141       inex_re = mpfr_sqrt_ui (mpc_realref (rop), 2, rnd_re);
142       inex_im = mpfr_sqrt_ui (mpc_imagref (rop), 2, rnd_im);
143       mpc_div_2ui (rop, rop, 1, MPC_RNDNN);
144       if (k == 3 || k == 5)
145         {
146           mpfr_neg (mpc_realref (rop), mpc_realref (rop), MPFR_RNDN);
147           inex_re = -inex_re;
148         }
149       if (k > 4)
150         {
151           mpfr_neg (mpc_imagref (rop), mpc_imagref (rop), MPFR_RNDN);
152           inex_im = -inex_im;
153         }
154       return MPC_INEX (inex_re, inex_im);
155     }
156
157   prec = MPC_MAX_PREC(rop);
158
159   /* For the error analysis justifying the following algorithm,
160      see algorithms.tex. */
161   mpfr_init2 (t, 67);
162   mpfr_init2 (s, 67);
163   mpfr_init2 (c, 67);
164   mpq_init (kn);
165   mpq_set_ui (kn, k, n);
166   mpq_mul_2exp (kn, kn, 1); /* kn=2*k/n < 2 */
167
168   do {
169      prec += mpc_ceil_log2 (prec) + 5; /* prec >= 6 */
170
171      mpfr_set_prec (t, prec);
172      mpfr_set_prec (s, prec);
173      mpfr_set_prec (c, prec);
174
175      mpfr_const_pi (t, MPFR_RNDN);
176      mpfr_mul_q (t, t, kn, MPFR_RNDN);
177      mpfr_sin_cos (s, c, t, MPFR_RNDN);
178   }
179   while (   !mpfr_can_round (c, prec - (4 - mpfr_get_exp (c)),
180                 MPFR_RNDN, MPFR_RNDZ,
181                 MPC_PREC_RE(rop) + (MPC_RND_RE(rnd) == MPFR_RNDN))
182          || !mpfr_can_round (s, prec - (4 - mpfr_get_exp (s)),
183                 MPFR_RNDN, MPFR_RNDZ,
184                 MPC_PREC_IM(rop) + (MPC_RND_IM(rnd) == MPFR_RNDN)));
185
186   inex_re = mpfr_set (mpc_realref(rop), c, MPC_RND_RE(rnd));
187   inex_im = mpfr_set (mpc_imagref(rop), s, MPC_RND_IM(rnd));
188
189   mpfr_clear (t);
190   mpfr_clear (s);
191   mpfr_clear (c);
192   mpq_clear (kn);
193
194   return MPC_INEX(inex_re, inex_im);
195}
196