forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadic.h
325 lines (225 loc) · 8.98 KB
/
padic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*=============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2011 Sebastian Pancratz
******************************************************************************/
#ifndef PADIC_H
#define PADIC_H
#undef ulong /* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#define ulong unsigned long
#include <mpir.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpq.h"
#include "fmpz_vec.h"
#include "ulong_extras.h"
typedef struct {
fmpz u;
long v;
} padic_struct;
typedef padic_struct padic_t[1];
#define padic_unit(x) (&((x)->u))
#define padic_val(x) ((x)->v)
enum padic_print_mode
{
PADIC_TERSE,
PADIC_SERIES,
PADIC_VAL_UNIT
};
typedef struct {
fmpz_t p;
long N;
double pinv;
fmpz *pow;
long min;
long max;
enum padic_print_mode mode;
} padic_ctx_struct;
typedef padic_ctx_struct padic_ctx_t[1];
typedef struct {
long n;
fmpz *pow;
fmpz *u;
} padic_inv_struct;
typedef padic_inv_struct padic_inv_t[1];
/* Context *******************************************************************/
void padic_ctx_init(padic_ctx_t ctx, const fmpz_t p, long N,
enum padic_print_mode mode);
void padic_ctx_clear(padic_ctx_t ctx);
static __inline__ int
_padic_ctx_pow_ui(fmpz_t rop, ulong e, const padic_ctx_t ctx)
{
if (ctx->min <= e && e < ctx->max)
{
*rop = *(ctx->pow + (e - ctx->min));
return 0;
}
else
{
fmpz_init(rop);
fmpz_pow_ui(rop, ctx->p, e);
return 1;
}
}
/* Memory management *********************************************************/
void _padic_init(padic_t rop);
void padic_init(padic_t rop, const padic_ctx_t ctx);
void _padic_clear(padic_t rop);
void padic_clear(padic_t rop, const padic_ctx_t ctx);
static __inline__ void _padic_canonicalise(padic_t rop, const padic_ctx_t ctx)
{
if (!fmpz_is_zero(padic_unit(rop)))
{
padic_val(rop) += _fmpz_remove(padic_unit(rop), ctx->p, ctx->pinv);
}
else
{
padic_val(rop) = 0;
}
}
void _padic_reduce(padic_t rop, const padic_ctx_t ctx);
void padic_reduce(padic_t rop, const padic_ctx_t ctx);
/* Randomisation *************************************************************/
void padic_randtest(padic_t rop, flint_rand_t state, const padic_ctx_t ctx);
void padic_randtest_not_zero(padic_t rop, flint_rand_t state,
const padic_ctx_t ctx);
/* Assignments and conversions ***********************************************/
void _padic_set(padic_t rop, const padic_t op);
void padic_set(padic_t rop, const padic_t op, const padic_ctx_t ctx);
void _padic_set_si(padic_t rop, long op, const padic_ctx_t ctx);
void padic_set_si(padic_t rop, long op, const padic_ctx_t ctx);
void _padic_set_ui(padic_t rop, ulong op, const padic_ctx_t ctx);
void padic_set_ui(padic_t rop, ulong op, const padic_ctx_t ctx);
void _padic_set_fmpz(padic_t rop, const fmpz_t op, const padic_ctx_t ctx);
void padic_set_fmpz(padic_t rop, const fmpz_t op, const padic_ctx_t ctx);
void padic_set_fmpq(padic_t rop, const fmpq_t op, const padic_ctx_t ctx);
void _padic_set_mpz(padic_t rop, const mpz_t op, const padic_ctx_t ctx);
void padic_set_mpz(padic_t rop, const mpz_t op, const padic_ctx_t ctx);
void padic_set_mpq(padic_t rop, const mpq_t op, const padic_ctx_t ctx);
void _padic_get_fmpz(fmpz_t rop, const padic_t op, const padic_ctx_t ctx);
void padic_get_fmpz(fmpz_t rop, const padic_t op, const padic_ctx_t ctx);
void _padic_get_fmpq(fmpq_t rop, const padic_t op, const padic_ctx_t ctx);
void padic_get_fmpq(fmpq_t rop, const padic_t op, const padic_ctx_t ctx);
void _padic_get_mpz(mpz_t rop, const padic_t op, const padic_ctx_t ctx);
void padic_get_mpz(mpz_t rop, const padic_t op, const padic_ctx_t ctx);
void _padic_get_mpq(mpq_t rop, const padic_t op, const padic_ctx_t ctx);
void padic_get_mpq(mpq_t rop, const padic_t op, const padic_ctx_t ctx);
static __inline__ void padic_swap(padic_t op1, padic_t op2)
{
long t;
fmpz_swap(padic_unit(op1), padic_unit(op2));
t = padic_val(op1);
padic_val(op1) = padic_val(op2);
padic_val(op2) = t;
}
static __inline__ void padic_zero(padic_t rop)
{
fmpz_zero(padic_unit(rop));
padic_val(rop) = 0;
}
static __inline__ void _padic_one(padic_t rop)
{
fmpz_one(padic_unit(rop));
padic_val(rop) = 0;
}
static __inline__ void padic_one(padic_t rop, const padic_ctx_t ctx)
{
if (ctx->N > 0)
_padic_one(rop);
else
padic_zero(rop);
}
/* Arithmetic operations *****************************************************/
void _padic_add(padic_t rop, const padic_t op1, const padic_t op2,
const padic_ctx_t ctx);
void padic_add(padic_t rop, const padic_t op1, const padic_t op2,
const padic_ctx_t ctx);
void _padic_sub(padic_t rop, const padic_t op1, const padic_t op2,
const padic_ctx_t ctx);
void padic_sub(padic_t rop, const padic_t op1, const padic_t op2,
const padic_ctx_t ctx);
void _padic_neg(padic_t rop, const padic_t op);
void padic_neg(padic_t rop, const padic_t op, const padic_ctx_t ctx);
void _padic_mul(padic_t rop, const padic_t op1, const padic_t op2);
void padic_mul(padic_t rop, const padic_t op1, const padic_t op2,
const padic_ctx_t ctx);
void padic_shift(padic_t rop, const padic_t op, long v, const padic_ctx_t ctx);
void padic_div(padic_t rop, const padic_t op1, const padic_t op2,
const padic_ctx_t ctx);
void _padic_inv_precompute(padic_inv_t S, const fmpz_t p, long N);
void _padic_inv_clear(padic_inv_t S);
void _padic_inv_precomp(fmpz_t rop, const fmpz_t op, padic_inv_t S);
void _padic_inv(fmpz_t rop, const fmpz_t op, const fmpz_t p, long N);
void padic_inv(padic_t rop, const padic_t op, const padic_ctx_t ctx);
int padic_sqrt(padic_t rop, const padic_t op, const padic_ctx_t ctx);
void padic_pow_si(padic_t rop, const padic_t op, long e,
const padic_ctx_t ctx);
/* Comparison ****************************************************************/
static __inline__ int _padic_is_zero(const padic_t op)
{
return fmpz_is_zero(padic_unit(op));
}
static __inline__ int padic_is_zero(const padic_t op, const padic_ctx_t ctx)
{
return fmpz_is_zero(padic_unit(op)) || (padic_val(op) >= ctx->N);
}
static __inline__ int _padic_is_one(const padic_t op)
{
return fmpz_is_one(padic_unit(op)) && (padic_val(op) == 0);
}
static __inline__ int padic_is_one(const padic_t op, const padic_ctx_t ctx)
{
if (ctx->N > 0)
return _padic_is_one(op);
else
return (padic_val(op) >= ctx->N);
}
int _padic_equal(const padic_t op1, const padic_t op2);
int padic_equal(const padic_t op1, const padic_t op2, const padic_ctx_t ctx);
/* Special functions *********************************************************/
void padic_teichmuller(padic_t rop, const padic_t op, const padic_ctx_t ctx);
int padic_exp(padic_t rop, const padic_t op, const padic_ctx_t ctx);
int padic_log(padic_t rop, const padic_t op, const padic_ctx_t ctx);
ulong padic_val_fac_ui2(ulong N);
ulong padic_val_fac_ui(ulong N, const fmpz_t p);
void padic_val_fac(fmpz_t rop, const fmpz_t op, const fmpz_t p);
/* Input and output **********************************************************/
char * _padic_get_str(char * str, const padic_t op, const padic_ctx_t ctx);
char * padic_get_str(char * str, const padic_t op, const padic_ctx_t ctx);
int __padic_fprint(FILE * file, const fmpz_t u, long v, const padic_ctx_t ctx);
int _padic_fprint(FILE * file, const padic_t op, const padic_ctx_t ctx);
int padic_fprint(FILE * file, const padic_t op, const padic_ctx_t ctx);
static __inline__ int
__padic_print(const fmpz_t u, long v, const padic_ctx_t ctx)
{
return __padic_fprint(stdout, u, v, ctx);
}
static __inline__ int _padic_print(const padic_t op, const padic_ctx_t ctx)
{
return _padic_fprint(stdout, op, ctx);
}
static __inline__ int padic_print(const padic_t op, const padic_ctx_t ctx)
{
return padic_fprint(stdout, op, ctx);
}
static __inline__ void _padic_debug(const padic_t op)
{
printf("{u = ");
fmpz_print(padic_unit(op));
printf(", v = %ld}", padic_val(op));
}
#endif