-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewtonOnly.c
303 lines (248 loc) · 11.5 KB
/
newtonOnly.c
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
/*
alphaCertified
Jonathan Hauenstein & Frank Sottile
May 7, 2010
Copyright 2010
newtonOnly.c: Only perform newton iterations on the points
*/
#include "alphaCertified.h"
void create_newton_only_summary(int numPoints, point_struct *Pts, configurations *S, polynomial_system *F)
/***************************************************************\
* USAGE: create summary file *
\***************************************************************/
{
int i, base = 10, digits = 16;
FILE *OUT = fopen("summary", "w");
fprintf(OUT, "Floating point (%d bits) summary:\n\n", S->startingPrecision);
// loop over the points
for (i = 0; i < numPoints; i++)
{ // print the data for the ith point
fprintf(OUT, "-------------------------\nPoint %d\n", i);
print_vector_coordinate(OUT, 0, Pts[i].origX);
// print original values of alpha, beta, and gamma
fprintf(OUT, "Original values:\n");
fprintf(OUT, " alpha < "); mpf_out_str(OUT, base, digits, Pts[i].origAlpha->re); fprintf(OUT, "\n");
fprintf(OUT, " beta ~= "); mpf_out_str(OUT, base, digits, Pts[i].origBeta->re); fprintf(OUT, "\n");
fprintf(OUT, " gamma < "); mpf_out_str(OUT, base, digits, Pts[i].origGamma->re); fprintf(OUT, "\n");
// print final values of alpha, beta, and gamma
fprintf(OUT, "Final values:\n");
fprintf(OUT, " alpha < "); mpf_out_str(OUT, base, digits, Pts[i].alpha->re); fprintf(OUT, "\n");
fprintf(OUT, " beta ~= "); mpf_out_str(OUT, base, digits, Pts[i].beta->re); fprintf(OUT, "\n");
fprintf(OUT, " gamma < "); mpf_out_str(OUT, base, digits, Pts[i].gamma->re); fprintf(OUT, "\n");
}
fprintf(OUT, "\n");
// print configurations and message about alphaCertified
configuration_summary(OUT, S, F);
fclose(OUT);
return;
}
void newton_only_output(int numPoints, point_struct *Pts, configurations *S, polynomial_system *F)
/***************************************************************\
* USAGE: create output files *
\***************************************************************/
{
int i, base = 10, digits = 16;
FILE *F1 = NULL;
// create the best approximations file
F1 = fopen("refinedPoints", "w");
fprintf(F1, "%d\n\n", numPoints);
for (i = 0; i < numPoints; i++)
{ // print to F1
print_vector_coordinate(F1, 0, Pts[i].x);
fprintf(F1, "\n");
}
fclose(F1);
// create certified and unknown files
F1 = fopen("constantValues", "w");
fprintf(F1, "%d\n\n", numPoints);
for (i = 0; i < numPoints; i++)
{ // print alpha, beta & gamma for origSoln
mpf_out_str(F1, base, digits, Pts[i].origAlpha->re);
fprintf(F1, "\n");
mpf_out_str(F1, base, digits, Pts[i].origBeta->re);
fprintf(F1, "\n");
mpf_out_str(F1, base, digits, Pts[i].origGamma->re);
fprintf(F1, "\n\n");
}
fclose(F1);
// create summary file
create_newton_only_summary(numPoints, Pts, S, F);
// print to screen the files that were created
printf("\n------------------------------------------------------------------------------------------------------------\n");
printf("The following files have been created:\n\n");
printf("constantValues: A list of the values of alpha, beta, and gamma for the points.\n");
printf("refinedPoints: A list of points after the requested Newton iterations.\n");
printf("summary: A human-readable summary for each point - main output file.\n");
printf("------------------------------------------------------------------------------------------------------------\n");
return;
}
void newton_only(int numPoints, complex_vector *Points, polynomial_system *F, configurations *S)
/***************************************************************\
* USAGE: perform newton iterations only on the points *
\***************************************************************/
{
int i, j, rV, curr_prec, numVars = F->numVariables;
point_struct *Points_struct = (point_struct *)errMalloc(numPoints * sizeof(point_struct));
// setup Points_struct and perform the newton iterations
for (i = 0; i < numPoints; i++)
{ // initialize
curr_prec = S->startingPrecision;
setPrec(curr_prec);
initialize_point_struct(&Points_struct[i], numVars);
// copy point
copy_vector(Points_struct[i].origX, Points[i]);
copy_vector(Points_struct[i].x, Points[i]);
// compute alpha, beta, & gamma (and save to original values of alpha, beta, & gamma)
rV = compute_alpha_beta_gamma(Points_struct[i].Nx, Points_struct[i].alpha, Points_struct[i].beta, Points_struct[i].gamma, F, Points_struct[i].x, S->startingPrecision);
set_number(Points_struct[i].origAlpha, Points_struct[i].alpha);
set_number(Points_struct[i].origBeta, Points_struct[i].beta);
set_number(Points_struct[i].origGamma, Points_struct[i].gamma);
// perform the requested number of Newton iterations
for (j = 0; j < S->newtonIts && !rV; j++)
{ // set x to Nx
curr_prec *= 2;
setPrec_vector(Points_struct[i].x, curr_prec);
copy_vector(Points_struct[i].x, Points_struct[i].Nx);
// compute alpha, beta & gamma for x
rV = compute_alpha_beta_gamma(Points_struct[i].Nx, Points_struct[i].alpha, Points_struct[i].beta, Points_struct[i].gamma, F, Points_struct[i].x, curr_prec);
}
}
// print the data out
newton_only_output(numPoints, Points_struct, S, F);
// clear Points_struct
for (i = 0; i < numPoints; i++)
clear_point_struct(&Points_struct[i]);
free(Points_struct);
Points_struct = NULL;
return;
}
void create_newton_only_summary_rational(int numPoints, rational_point_struct *Pts, configurations *S, polynomial_system *F)
/***************************************************************\
* USAGE: create summary file *
\***************************************************************/
{
int i, base = 10, digits = 16;
mpf_t tempMPF;
FILE *OUT = fopen("summary", "w");
mpf_init2(tempMPF, 1024);
fprintf(OUT, "Rational summary:\n\n");
// loop over the points
for (i = 0; i < numPoints; i++)
{ // print the data for the ith point
fprintf(OUT, "-------------------------\nPoint %d\n", i);
print_rational_vector_coordinate(OUT, Pts[i].origX);
// print original approximate values of alpha, beta, and gamma
fprintf(OUT, "Original values:\n");
mpf_set_q(tempMPF, Pts[i].origAlpha_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
fprintf(OUT, " alpha < "); mpf_out_str(OUT, base, digits, tempMPF); fprintf(OUT, "\n");
mpf_set_q(tempMPF, Pts[i].origBeta_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
fprintf(OUT, " beta ~= "); mpf_out_str(OUT, base, digits, tempMPF); fprintf(OUT, "\n");
mpf_set_q(tempMPF, Pts[i].origGamma_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
fprintf(OUT, " gamma < "); mpf_out_str(OUT, base, digits, tempMPF); fprintf(OUT, "\n");
// print approximate values of alpha, beta, and gamma
fprintf(OUT, "Final values:\n");
mpf_set_q(tempMPF, Pts[i].alpha_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
fprintf(OUT, " alpha < "); mpf_out_str(OUT, base, digits, tempMPF); fprintf(OUT, "\n");
mpf_set_q(tempMPF, Pts[i].beta_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
fprintf(OUT, " beta ~= "); mpf_out_str(OUT, base, digits, tempMPF); fprintf(OUT, "\n");
mpf_set_q(tempMPF, Pts[i].gamma_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
fprintf(OUT, " gamma < "); mpf_out_str(OUT, base, digits, tempMPF); fprintf(OUT, "\n");
}
fprintf(OUT, "\n");
// print configurations and message about alphaCertified
configuration_summary(OUT, S, F);
fclose(OUT);
mpf_clear(tempMPF);
return;
}
void newton_only_output_rational(int numPoints, rational_point_struct *Pts, configurations *S, polynomial_system *F)
/***************************************************************\
* USAGE: create output files *
\***************************************************************/
{
int i, base = 10, digits = 16;
mpf_t tempMPF;
FILE *F1 = NULL;
mpf_init2(tempMPF, 1024);
// create the best approximations file
F1 = fopen("refinedPoints", "w");
fprintf(F1, "%d\n\n", numPoints);
for (i = 0; i < numPoints; i++)
{ // print to F1
print_rational_vector_coordinate(F1, Pts[i].x);
fprintf(F1, "\n");
}
fclose(F1);
// create certified and unknown files
F1 = fopen("constantValues", "w");
fprintf(F1, "%d\n\n", numPoints);
for (i = 0; i < numPoints; i++)
{ // print alpha, beta & gamma for origSoln
mpf_set_q(tempMPF, Pts[i].origAlpha_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
mpf_out_str(F1, base, digits, tempMPF);
fprintf(F1, "\n");
mpf_set_q(tempMPF, Pts[i].origBeta_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
mpf_out_str(F1, base, digits, tempMPF);
fprintf(F1, "\n");
mpf_set_q(tempMPF, Pts[i].origGamma_sqr->re);
mpf_sqrt(tempMPF, tempMPF);
mpf_out_str(F1, base, digits, tempMPF);
fprintf(F1, "\n\n");
}
fclose(F1);
// create summary file
create_newton_only_summary_rational(numPoints, Pts, S, F);
// print to screen the files that were created
printf("\n------------------------------------------------------------------------------------------------------------\n");
printf("The following files have been created:\n\n");
printf("constantValues: A list of the values of alpha, beta, and gamma for the points.\n");
printf("refinedPoints: A list of points after the requested Newton iterations.\n");
printf("summary: A human-readable summary for each point - main output file.\n");
printf("------------------------------------------------------------------------------------------------------------\n");
mpf_clear(tempMPF);
return;
}
void newton_only_rational(int numPoints, rational_complex_vector *Points, polynomial_system *F, configurations *S)
/***************************************************************\
* USAGE: perform newton iterations only on the points *
\***************************************************************/
{
int i, j, rV, numVars = F->numVariables;
rational_point_struct *Points_struct = (rational_point_struct *)errMalloc(numPoints * sizeof(rational_point_struct));
// setup Points_struct and perform the newton iterations
for (i = 0; i < numPoints; i++)
{ // initialize
initialize_rational_point_struct(&Points_struct[i], numVars);
// copy point
copy_rational_vector(Points_struct[i].origX, Points[i]);
copy_rational_vector(Points_struct[i].x, Points[i]);
// compute alpha, beta, & gamma (and save to original values of alpha, beta, & gamma)
rV = compute_alpha_beta_gamma_sqr_rational(Points_struct[i].Nx, Points_struct[i].alpha_sqr, Points_struct[i].beta_sqr, Points_struct[i].gamma_sqr, F, Points_struct[i].x);
set_rational_number(Points_struct[i].origAlpha_sqr, Points_struct[i].alpha_sqr);
set_rational_number(Points_struct[i].origBeta_sqr, Points_struct[i].beta_sqr);
set_rational_number(Points_struct[i].origGamma_sqr, Points_struct[i].gamma_sqr);
// perform the requested number of Newton iterations
for (j = 0; j < S->newtonIts && !rV; j++)
{ // set x to Nx
copy_rational_vector(Points_struct[i].x, Points_struct[i].Nx);
// compute alpha, beta & gamma for x
rV = compute_alpha_beta_gamma_sqr_rational(Points_struct[i].Nx, Points_struct[i].alpha_sqr, Points_struct[i].beta_sqr, Points_struct[i].gamma_sqr, F, Points_struct[i].x);
}
}
// print the data out
newton_only_output_rational(numPoints, Points_struct, S, F);
// clear Points_struct
for (i = 0; i < numPoints; i++)
clear_rational_point_struct(&Points_struct[i]);
free(Points_struct);
Points_struct = NULL;
return;
}