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
/*
Copyright (C) 2011, 2012 Sebastian Pancratz
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "fmpq.h"
#include "padic.h"
/* printing *******************************************************************/
int _padic_fprint(FILE * file, const fmpz_t u, slong v, const padic_ctx_t ctx)
{
const fmpz *p = ctx->p;
if (fmpz_is_zero(u))
{
fputc('0', file);
return 1;
}
if (ctx->mode == PADIC_TERSE)
{
if (v == 0)
{
fmpz_fprint(file, u);
}
else if (v > 0)
{
fmpz_t t;
fmpz_init(t);
fmpz_pow_ui(t, p, v);
fmpz_mul(t, t, u);
fmpz_fprint(file, t);
fmpz_clear(t);
}
else /* v < 0 */
{
fmpz_t t;
fmpz_init(t);
fmpz_pow_ui(t, p, -v);
_fmpq_fprint(file, u, t);
fmpz_clear(t);
}
}
else if (ctx->mode == PADIC_SERIES)
{
fmpz_t x;
fmpz_t d;
slong j;
fmpz_init(d);
fmpz_init(x);
fmpz_set(x, u);
/* Unroll first step */
j = 0;
{
fmpz_mod(d, x, p); /* d = u mod p^{j+1} */
fmpz_sub(x, x, d); /* x = x - d */
fmpz_divexact(x, x, p); /* x = x / p */
if (!fmpz_is_zero(d))
{
if (j + v != 0)
{
fmpz_fprint(file, d);
fputc('*', file);
fmpz_fprint(file, p);
flint_fprintf(file, "^%wd", j + v);
}
else
{
fmpz_fprint(file, d);
}
}
j++;
}
for ( ; !fmpz_is_zero(x); j++)
{
fmpz_mod(d, x, p); /* d = u mod p^{j+1} */
fmpz_sub(x, x, d); /* x = x - d */
fmpz_divexact(x, x, p); /* x = x / p */
if (!fmpz_is_zero(d))
{
if (j + v != 0)
{
flint_fprintf(file, " + ");
fmpz_fprint(file, d);
fputc('*', file);
fmpz_fprint(file, p);
flint_fprintf(file, "^%wd", j + v);
}
else
{
flint_fprintf(file, " + ");
fmpz_fprint(file, d);
}
}
}
fmpz_clear(x);
fmpz_clear(d);
}
else if (ctx->mode == PADIC_VAL_UNIT)
{
if (v == 0)
{
fmpz_fprint(file, u);
}
else if (v == 1)
{
fmpz_fprint(file, u);
fputc('*', file);
fmpz_fprint(file, p);
}
else
{
fmpz_fprint(file, u);
fputc('*', file);
fmpz_fprint(file, p);
flint_fprintf(file, "^%wd", v);
}
}
else
{
flint_throw(FLINT_ERROR, "Exception (_padic_fprint). Unknown print mode.\n");
}
return 1;
}
int padic_fprint(FILE * file, const padic_t op, const padic_ctx_t ctx) { return _padic_fprint(file, padic_unit(op), padic_val(op), ctx); }
int _padic_print(const fmpz_t u, slong v, const padic_ctx_t ctx) { return _padic_fprint(stdout, u, v, ctx); }
int padic_print(const padic_t op, const padic_ctx_t ctx) { return padic_fprint(stdout, op, ctx); }
/* debugging ******************************************************************/
void padic_debug(const padic_t op)
{
flint_printf("(");
fmpz_print(padic_unit(op));
flint_printf(" %wd %wd)", padic_val(op), padic_prec(op));
}