1 /*=============================================================================
2 
3     This file is part of Antic.
4 
5     Antic is free software: you can redistribute it and/or modify it under
6     the terms of the GNU Lesser General Public License (LGPL) as published
7     by the Free Software Foundation; either version 2.1 of the License, or
8     (at your option) any later version. See <http://www.gnu.org/licenses/>.
9 
10 =============================================================================*/
11 /******************************************************************************
12 
13     Copyright (C) 2012 William Hart
14 
15 ******************************************************************************/
16 
17 #include <stdlib.h>
18 #include <gmp.h>
19 #include "flint/fmpz.h"
20 #include "qfb.h"
21 
qfb_pow_ui(qfb_t r,qfb_t f,fmpz_t D,ulong exp)22 void qfb_pow_ui(qfb_t r, qfb_t f, fmpz_t D, ulong exp)
23 {
24    fmpz_t L;
25    qfb_t pow;
26 
27    if (exp == 0)
28    {
29       qfb_principal_form(r, D);
30       return;
31    }
32 
33    if (exp == 1)
34    {
35       qfb_set(r, f);
36       return;
37    }
38 
39    fmpz_init(L);
40    fmpz_abs(L, D);
41    fmpz_root(L, L, 4);
42 
43    qfb_init(pow);
44 
45    qfb_set(pow, f);
46    while ((exp & 1) == 0)
47    {
48       qfb_nudupl(pow, pow, D, L);
49       qfb_reduce(pow, pow, D);
50       exp >>= 1;
51    }
52 
53    qfb_set(r, pow);
54    exp >>= 1;
55 
56    while (exp)
57    {
58       qfb_nudupl(pow, pow, D, L);
59       qfb_reduce(pow, pow, D);
60       if (exp & 1)
61       {
62          qfb_nucomp(r, r, pow, D, L);
63          qfb_reduce(r, r, D);
64       }
65       exp >>= 1;
66    }
67 
68    qfb_clear(pow);
69    fmpz_clear(L);
70 }
71