Debian Codegraph @

src/libmps/chebyshev/chebyshev-parser.c

raw
1 /*
2 * This file is part of MPSolve 3.2.2
3 *
4 * Copyright (C) 2001-2020, Dipartimento di Matematica "L. Tonelli", Pisa.
5 * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
6 *
7 * Authors:
8 * Leonardo Robol <[email protected]>
9 */
10
11 #include <mps/mps.h>
12
13 /**
14 * @brief Parse the stream that has been loaded into buffer and that
15 * describe a mps_chebyshev_poly.
16 *
17 * @param ctx The current mps_context
18 * @param buffer The buffer that needs to be parsed
19 * @param structure The structure of the polynomial
20 * @param density The density configuration of the polynomial.
21 * @param The input precision of the coefficients, if specified, 0 otherwise
22 *
23 * @return A newly allocated mps_chebyshev_poly, or NULL if the parsing fails.
24 */
25 mps_chebyshev_poly *
26 mps_chebyshev_poly_read_from_stream (mps_context * ctx, mps_input_buffer * buffer,
27 mps_structure structure, mps_density density,
28 long int precision)
29 {
30 int i, degree = -1;
31 char * token;
32 mps_chebyshev_poly * cpoly = mps_chebyshev_poly_new (ctx, ctx->n, structure);
33
34 /* Raise the precision if needed to parse the input coefficients */
35 if (precision > 0)
36 {
37 for (i = 0; i < ctx->n; i++)
38 mpc_set_prec (cpoly->mfpc[i], precision);
39 }
40
41 switch (density)
42 {
43 case MPS_DENSITY_DENSE:
44 for (i = 0; i <= ctx->n; i++)
45 {
46 if (MPS_STRUCTURE_IS_FP (structure))
47 {
48 token = mps_input_buffer_next_token (buffer);
49
50 if (!token || (mpf_set_str (mpc_Re (cpoly->mfpc[i]), token, 10) != 0))
51 {
52 mps_raise_parsing_error (ctx, buffer, token,
53 "Error while reading real part of coefficient");
54 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
55 return NULL;
56 }
57 free (token);
58
59 if (MPS_STRUCTURE_IS_COMPLEX (structure))
60 {
61 token = mps_input_buffer_next_token (buffer);
62
63 if (!token || (mpf_set_str (mpc_Im (cpoly->mfpc[i]), token, 10) != 0))
64 {
65 mps_raise_parsing_error (ctx, buffer, token, "Error while reading imaginary part of coefficient");
66 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
67 return NULL;
68 }
69 else
70 free (token);
71 }
72 }
73 else if (MPS_STRUCTURE_IS_RATIONAL (structure) || MPS_STRUCTURE_IS_INTEGER (structure))
74 {
75 token = mps_input_buffer_next_token (buffer);
76
77 if (!token || (mpq_set_str (cpoly->rational_real_coeffs[i], token, 10)))
78 {
79 mps_raise_parsing_error (ctx, buffer, token,
80 "Error while reading the real part of coefficient");
81 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
82 return NULL;
83 }
84 free (token);
85
86 if (MPS_STRUCTURE_IS_COMPLEX (structure))
87 {
88 token = mps_input_buffer_next_token (buffer);
89
90 if (!token || (mpq_set_str (cpoly->rational_imag_coeffs[i], token, 10)))
91 {
92 mps_raise_parsing_error (ctx, buffer, token,
93 "Error while reading the imaginary part of coefficient");
94 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
95 return NULL;
96 }
97 free (token);
98 }
99
100 mpf_set_q (mpc_Re (cpoly->mfpc[i]), cpoly->rational_real_coeffs[i]);
101 mpf_set_q (mpc_Im (cpoly->mfpc[i]), cpoly->rational_imag_coeffs[i]);
102 }
103
104 /* Update other floating point coefficients */
105 mpc_get_cdpe (cpoly->dpc[i], cpoly->mfpc[i]);
106 mpc_get_cplx (cpoly->fpc[i], cpoly->mfpc[i]);
107
108 if (ctx->debug_level & MPS_DEBUG_IO)
109 {
110 MPS_DEBUG_CPLX (ctx, cpoly->fpc[i], "Coefficient %d", i);
111 }
112 }
113 break;
114
115 case MPS_DENSITY_SPARSE:
116 /* Set all the coefficients to zero first, so whatever is not given
117 * will be assumed to be null. */
118 for (i = 0; i < ctx->n; i++)
119 {
120 mpc_set_ui (cpoly->mfpc[i], 0U, 0U);
121 mpq_set_ui (cpoly->rational_real_coeffs[i], 0U, 1U);
122 mpq_set_ui (cpoly->rational_imag_coeffs[i], 0U, 1U);
123 }
124
125 while ((token = mps_input_buffer_next_token (buffer)) != NULL)
126 {
127 /* Read the degree of the coefficient */
128 if (!token || !sscanf (token, "%d", &degree))
129 {
130 mps_raise_parsing_error (ctx, buffer, token, "Cannot parse the degree of the coefficient.");
131 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
132 return NULL;
133 }
134
135 free (token);
136
137 if (MPS_STRUCTURE_IS_FP (structure))
138 {
139 token = mps_input_buffer_next_token (buffer);
140
141 if (!token || (mpf_set_str (mpc_Re (cpoly->mfpc[degree]), token, 10) != 0))
142 {
143 mps_raise_parsing_error (ctx, buffer, token, "Error while reading real part of coefficient");
144 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
145 return NULL;
146 }
147
148 free (token);
149
150 if (MPS_STRUCTURE_IS_COMPLEX (structure))
151 {
152 token = mps_input_buffer_next_token (buffer);
153
154 if (!token || (mpf_set_str (mpc_Im (cpoly->mfpc[degree]), token, 10) != 0))
155 {
156 mps_raise_parsing_error (ctx, buffer, token,
157 "Error while reading imaginary part of coefficient %d", degree);
158 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
159 return NULL;
160 }
161
162 free (token);
163 }
164 }
165 else
166 {
167 token = mps_input_buffer_next_token (buffer);
168
169 if (!token || (mpq_set_str (cpoly->rational_real_coeffs[degree], token, 10)))
170 {
171 mps_raise_parsing_error (ctx, buffer, token, "Error while reading the real part of coefficient %d", i);
172 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
173 return NULL;
174 }
175
176 mpf_set_q (mpc_Re (cpoly->mfpc[degree]), cpoly->rational_real_coeffs[degree]);
177
178 free (token);
179
180 if (MPS_STRUCTURE_IS_COMPLEX (structure))
181 {
182 token = mps_input_buffer_next_token (buffer);
183
184 if (!token || (mpq_set_str (cpoly->rational_imag_coeffs[degree], token, 10)))
185 {
186 mps_raise_parsing_error (ctx, buffer, token,
187 "Error while reading the imaginary part of coefficient %d", i);
188 free (token); mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
189 return NULL;
190 }
191
192 mpf_set_q (mpc_Im (cpoly->mfpc[degree]), cpoly->rational_imag_coeffs[degree]);
193
194 free (token);
195 }
196 }
197
198 mpc_get_cdpe (cpoly->dpc[degree], cpoly->mfpc[degree]);
199 mpc_get_cplx (cpoly->fpc[degree], cpoly->mfpc[degree]);
200 }
201 break;
202
203 default:
204 mps_error (ctx, "Only MPS_DENSITY_DENSE and MPS_DENSITY_SPARSE are supported in Chebyshev polynomials.");
205 mps_polynomial_free (ctx, MPS_POLYNOMIAL (cpoly));
206 return NULL;
207 break;
208 }
209
210 return cpoly;
211 }
212