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
/*
Copyright (C) 2023 Fredrik Johansson
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 <string.h>
#include <stdio.h>
#include <ctype.h>
#include "gr_poly.h"
static int
want_parens(const char * s)
{
/* Try to determine if the expression is already enclosed by
brackets or parentheses. */
if (s[0] == '[' || s[0] == '(' || s[0] == '}')
{
slong depth = 1;
char open = s[0];
char close = (open == '[') ? ']' : (open == '(' ? ')' : '}');
s++;
while (s[0] != '\0')
{
if (s[0] == open)
depth++;
else if (s[0] == close)
depth--;
s++;
if (depth == 0)
return s[0] != '\0';
}
}
if (s[0] == '-')
s++;
while (s[0] != '\0')
{
if (!isalnum(s[0]) && s[0] != '.')
return 1;
s++;
}
return 0;
}
int
_gr_poly_write(gr_stream_t out, gr_srcptr poly, slong n, const char * x, gr_ctx_t ctx)
{
int status;
slong i;
slong sz;
char * s;
int printed_previously = 0;
sz = ctx->sizeof_elem;
status = GR_SUCCESS;
if (n == 0)
{
gr_stream_write(out, "0");
return status;
}
for (i = 0; i < n; i++)
{
if (gr_is_zero(GR_ENTRY(poly, i, sz), ctx) == T_TRUE)
continue;
gr_get_str(&s, GR_ENTRY(poly, i, sz), ctx);
if (i >= 1 && !strcmp(s, "1"))
{
flint_free(s);
if (printed_previously)
gr_stream_write(out, " + ");
gr_stream_write(out, x);
if (i >= 2)
{
gr_stream_write(out, "^");
gr_stream_write_si(out, i);
}
}
else if (i >= 1 && !strcmp(s, "-1"))
{
flint_free(s);
if (printed_previously)
gr_stream_write(out, " - ");
else
gr_stream_write(out, "-");
gr_stream_write(out, x);
if (i >= 2)
{
gr_stream_write(out, "^");
gr_stream_write_si(out, i);
}
}
else
{
if (want_parens(s))
{
if (printed_previously)
gr_stream_write(out, " + ");
gr_stream_write(out, "(");
gr_stream_write_free(out, s);
gr_stream_write(out, ")");
}
else
{
if (printed_previously && s[0] == '-')
{
gr_stream_write(out, " - ");
gr_stream_write(out, s + 1);
flint_free(s);
}
else
{
if (printed_previously)
gr_stream_write(out, " + ");
gr_stream_write_free(out, s);
}
}
if (i == 1)
{
gr_stream_write(out, "*");
gr_stream_write(out, x);
}
else if (i >= 2)
{
gr_stream_write(out, "*");
gr_stream_write(out, x);
gr_stream_write(out, "^");
gr_stream_write_si(out, i);
}
}
printed_previously = 1;
/*
gr_stream_write(out, "(");
status |= gr_write(out, GR_ENTRY(poly, i, sz), ctx);
gr_stream_write(out, ")");
if (i == 1)
{
gr_stream_write(out, "*x");
}
else if (i >= 2)
{
gr_stream_write(out, "*x^");
gr_stream_write_si(out, i);
}
if (i < n - 1)
gr_stream_write(out, " + ");
*/
}
return status;
}
int
gr_poly_write(gr_stream_t out, const gr_poly_t poly, const char * x, gr_ctx_t ctx)
{
return _gr_poly_write(out, poly->coeffs, poly->length, x, ctx);
}
int
gr_poly_print(const gr_poly_t poly, gr_ctx_t ctx)
{
gr_stream_t out;
gr_stream_init_file(out, stdout);
return gr_poly_write(out, poly, "x", ctx);
}
int
_gr_poly_get_str(char ** res, gr_srcptr f, slong len, const char * x, gr_ctx_t ctx)
{
gr_stream_t out;
int status;
gr_stream_init_str(out);
status = _gr_poly_write(out, f, len, x, ctx);
*res = out->s;
return status;
}
int
gr_poly_get_str(char ** res, const gr_poly_t f, const char * x, gr_ctx_t ctx)
{
return _gr_poly_get_str(res, f->coeffs, f->length, x, ctx);
}