-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
1812 lines (1546 loc) · 49.7 KB
/
misc.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
alphaCertified
Jonathan Hauenstein & Frank Sottile
May 7, 2010
Copyright 2010
misc.c: Contains miscellaneous functions used throughout the program
*/
#include "alphaCertified.h"
#include <ctype.h>
void setPrec(int prec)
/***************************************************************\
* USAGE: set default precision *
\***************************************************************/
{
mpf_set_default_prec(prec);
return;
}
void errExit(int errorCode)
/***************************************************************\
* USAGE: exit alphaCertified *
\***************************************************************/
{
if (errorCode == 0)
errorCode = ERROR_OTHER;
printf("%s\n", ERROR_MESSAGE);
exit(errorCode);
return;
}
void *errMalloc(size_t size)
/***************************************************************\
* USAGE: allocate memory with error checking *
\***************************************************************/
{
if (size <= 0)
{ // nothing to allocate
return NULL;
}
else
{ // try to allocate memory
void *x = malloc(size);
if (x == NULL)
{
printf("ERROR: errMalloc was unable to allocate memory (%d)!\n", (int) size);
errExit(ERROR_MEMORY_ALLOCATION);
}
return x;
}
}
void *errRealloc(void *ptr, size_t size)
/***************************************************************\
* USAGE: reallocate memory with error checking *
\***************************************************************/
{
if (size <= 0)
{ // nothing to reallocate - free memory and return NULL
free(ptr);
ptr = NULL;
}
else
{ // try to reallocate memory
ptr = realloc(ptr, size);
if (ptr == NULL)
{
printf("ERROR: errRealloc was unable to allocate memory (%d)!\n", (int) size);
errExit(ERROR_MEMORY_ALLOCATION);
}
}
return ptr;
}
void setup_polynomial(polynomial *F, int numVars, FILE *IN, int polyNumber)
/***************************************************************\
* USAGE: setup the next polynomial described in IN *
\***************************************************************/
{
int i, j, rV, max, base = 10;
// initialize the number of variables, degree & isReal
F->numVariables = numVars;
F->degree = 0;
F->isReal = 1;
// read in the number of terms
fscanf(IN, "%d\n", &F->numTerms);
// error checking - want number of terms >= 0
if (F->numTerms <= 0)
{ // error
printf("ERROR: The number of terms (%d) must be positive.\n", F->numTerms);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// allocate memory
mpq_init(F->norm_sqr);
F->coeff = (rational_complex_number *)errMalloc(F->numTerms * sizeof(rational_complex_number));
F->exponents = (int **)errMalloc(F->numTerms * sizeof(int *));
// setup the terms
for (i = 0; i < F->numTerms; i++)
{ // allocate & initialize memory
F->exponents[i] = (int *)errMalloc(numVars * sizeof(int));
initialize_rational_number(F->coeff[i]);
// read in exponents, compute degree, and perform error checking - want exponents >= 0
max = 0;
for (j = 0; j < numVars; j++)
{
fscanf(IN, "%d", &F->exponents[i][j]);
if (F->exponents[i][j] < 0)
{ // error
printf("ERROR: The exponent for variable %d in monomial %d of polynomial %d must be nonnegative (%d).\n", j+1, i+1, polyNumber+1, F->exponents[i][j]);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
max += F->exponents[i][j];
}
// update degree, if needed
if (max > F->degree)
F->degree = max;
// read in real & imaginary part of coefficient
rV = mpq_inp_str(F->coeff[i]->re, IN, base);
if (rV == 0)
{ // error in reading the coefficient
printf("ERROR: There appears to be an error when reading in the real part of the\n coefficient for monomial %d of polynomial %d.\n", i+1, polyNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
rV = mpq_inp_str(F->coeff[i]->im, IN, base);
if (rV == 0)
{ // error in reading the coefficient
printf("ERROR: There appears to be an error when reading in the imaginary part of the\n coefficient for monomial %d of polynomial %d.\n", i+1, polyNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// setup in canonical form
mpq_canonicalize(F->coeff[i]->re);
mpq_canonicalize(F->coeff[i]->im);
// update isReal, if needed
if (F->isReal && mpq_cmp_ui(F->coeff[i]->im, 0, 1) != 0)
F->isReal = 0;
}
// compute norm_sqr
norm_sqr_polynomial(F->norm_sqr, F);
return;
}
int readInExpFunction(FILE *IN, char *expFunction, int *isHyperbolic)
/***************************************************************\
* USAGE: read in exponential function - skip white space *
\***************************************************************/
{
int rV = 0; // 0 - error, 1 - read in a char
char c;
do
{ // read in char
c = fgetc(IN);
} while (c != EOF && isspace((int) c));
if (c == EOF)
{ // return error
rV = 0;
}
else
{ // save char
rV = 1;
*expFunction = c;
// initialize isHyperbolic
*isHyperbolic = 0;
// read in next char and determine what to do
c = fgetc(IN);
if (c == 'H' || c == 'h')
{ // using hyperbolic
*isHyperbolic = 1;
}
else
{ // put char back
ungetc(c, IN);
}
}
return rV;
}
void setup_exponential(exponential *F, int numVars, FILE *IN, int expNumber, int yIndex)
/***************************************************************\
* USAGE: setup the next exponential described in IN *
\***************************************************************/
{
int rV, base = 10;
// read in the x variable index
rV = fscanf(IN, "%d", &F->xIndex);
if (rV == 0)
{ // error in reading the x variable index
printf("ERROR: There appears to be an error when reading in the variable index\n for exponential %d.\n", expNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// error checking - want variable index to be in 1,2,..,numVars
if (F->xIndex < 1 || F->xIndex > numVars)
{ // error
printf("ERROR: The variable index for exponential %d must be between 1 and %d.\n", expNumber+1, numVars);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// setup x & y variable index
F->xIndex--;
F->yIndex = yIndex;
// read in the type of exponential function: exp, sin, or cos
rV = readInExpFunction(IN, &F->expFunction, &F->isHyperbolic);
if (rV == 0)
{ // error in reading the exponential function
printf("ERROR: There appears to be an error when reading in the exponential function\n for exponential %d.\n", expNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// error checking
if (F->expFunction != 'X' && F->expFunction != 'S' && F->expFunction != 'C')
{ // error
printf("ERROR: The exponential function for exponential %d appears to be incorrect.\n", expNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// read in real & imaginary part of beta
initialize_rational_number(F->beta);
rV = mpq_inp_str(F->beta->re, IN, base);
if (rV == 0)
{ // error in reading the coefficient
printf("ERROR: There appears to be an error when reading in the real part of the\n exponential constant for exponential %d.\n", expNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
rV = mpq_inp_str(F->beta->im, IN, base);
if (rV == 0)
{ // error in reading the coefficient
printf("ERROR: There appears to be an error when reading in the imaginary part of the\n exponential constant exponential %d.\n", expNumber+1);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// setup in canonical form
mpq_canonicalize(F->beta->re);
mpq_canonicalize(F->beta->im);
// update isReal
F->isReal = !mpq_cmp_ui(F->beta->im, 0, 1);
return;
}
void polynomial_system_check(FILE *IN)
/***************************************************************\
* USAGE: check the polynomial system for decimal points, e, & E *
\***************************************************************/
{
char c;
while ((c = fgetc(IN)) != EOF)
{ // check c
if (c == '.')
{
printf("ERROR: To prevent floating point coefficients, no decimal points are allowed in the polynomial system file.\n");
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
else if (c == 'e')
{
printf("ERROR: To prevent floating point coefficients, 'e' is allowed in the polynomial system file.\n");
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
else if (c == 'E')
{
printf("ERROR: To prevent floating point coefficients, 'E' is allowed in the polynomial system file.\n");
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
}
// rewind the file
rewind(IN);
return;
}
void setup_polynomial_system(polynomial_system *F, char *fileName, int realityCheck, int arithmeticType)
/***************************************************************\
* USAGE: setup the polynomial system described in fileName *
\***************************************************************/
{
int i, hasRealCoeff = 1;
FILE *IN = fopen(fileName, "r");
// verify file exists
if (IN == NULL)
{
printf("\nERROR: '%s' does not exist!\n", fileName);
errExit(ERROR_FILE_NOT_EXIST);
}
// print message about rational coefficients
printf("Please note that all coefficients must be complex rational numbers.\n\n");
// check for decimal points, e, and E in the polynomial system - rewinds file if okay, closes file if error
polynomial_system_check(IN);
// initialize polynomial system
initialize_polynomial_system(F);
// read in the number of variables & number of polynomials
fscanf(IN, "%d%d\n", &F->numVariables, &F->numPolynomials);
// want 0 < variables && 0 < polynomials
if (F->numVariables <= 0)
{ // error
printf("ERROR: The number of variables (%d) must be positive.\n", F->numVariables);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
else if (F->numPolynomials <= 0)
{ // error
printf("ERROR: The number of polynomials (%d) must be positive.\n", F->numPolynomials);
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
// determine if using exponentials - if so, verify arithmetic type and allocate memory
if (F->numVariables <= F->numPolynomials)
{ // polynomial only system
printf("alphaCertified is using the polynomial certification algorithms.\n\n");
F->numExponentials = 0;
F->exponentials = NULL;
}
else // numVariables > numPolynomials
{ // polynomial exponential system
if (arithmeticType)
{ // floating point - allocate memory
printf("alphaCertified is using the polynomial-exponential certification algorithms.\n\n");
F->numExponentials = F->numVariables - F->numPolynomials;
F->exponentials = (exponential *)errMalloc(F->numExponentials * sizeof(exponential));
}
else
{ // error
printf("ERROR: Exponentials may only be used with floating point arithmetic.\n\n");
// close file and exit
fclose(IN);
errExit(ERROR_INPUT_SYSTEM);
}
}
// allocate memory in F for polynomials
F->polynomials = (polynomial *)errMalloc(F->numPolynomials * sizeof(polynomial));
// read in the polynomials, compute maximum degree, hasRealCoeff, and norm_sqr
mpq_set_ui(F->norm_sqr, 0, 1);
for (i = 0; i < F->numPolynomials; i++)
{
setup_polynomial(&F->polynomials[i], F->numVariables, IN, i);
if (F->polynomials[i].degree > F->maximumDegree)
F->maximumDegree = F->polynomials[i].degree;
hasRealCoeff = hasRealCoeff && F->polynomials[i].isReal;
mpq_add(F->norm_sqr, F->norm_sqr, F->polynomials[i].norm_sqr);
}
// read in the exponentials and update hasRealCoeff
for (i = 0; i < F->numExponentials; i++)
{
setup_exponential(&F->exponentials[i], F->numPolynomials, IN, i, F->numPolynomials + i);
hasRealCoeff = hasRealCoeff && F->exponentials[i].isReal;
}
// setup F->isReal
F->isReal = 0; // initialize to 0
if (F->numPolynomials != F->numVariables)
{ // not square system - reality only depends upon coefficients
F->isReal = hasRealCoeff;
}
else if (realityCheck < 0)
{ // square system that user says is real
F->isReal = 1;
}
else // realityCheck >= 0
{ // square system - perform checks based on the settings
if (hasRealCoeff)
{ // system has real coefficients
F->isReal = 1;
}
else if (realityCheck > 0)
{ // test for conjugate map
if (square_conj_map_test(F))
{ // system is invariant under conjugation
F->isReal = 1;
}
}
}
// close IN
fclose(IN);
return;
}
void initialize_polynomial_system(polynomial_system *F)
/***************************************************************\
* USAGE: initialize the memory in F *
\***************************************************************/
{
F->numVariables = F->numPolynomials = F->maximumDegree = F->isReal = F->numExponentials = 0;
mpq_init(F->norm_sqr);
F->polynomials = NULL;
F->exponentials = NULL;
return;
}
void clear_polynomial(polynomial *F)
/***************************************************************\
* USAGE: release the memory in F *
\***************************************************************/
{
int i;
mpq_clear(F->norm_sqr);
for (i = 0; i < F->numTerms; i++)
{
clear_rational_number(F->coeff[i]);
free(F->exponents[i]);
}
free(F->coeff);
free(F->exponents);
F->coeff = NULL;
F->exponents = NULL;
F->numVariables = F->numTerms = F->degree = F->isReal = 0;
return;
}
void clear_exponential(exponential *F)
/***************************************************************\
* USAGE: release the memory in F *
\***************************************************************/
{
clear_rational_number(F->beta);
F->expFunction = '\n';
F->xIndex = F->yIndex = F->isHyperbolic = F->isReal = 0;
return;
}
void clear_polynomial_system(polynomial_system *F)
/***************************************************************\
* USAGE: release the memory in F *
\***************************************************************/
{
int i;
for (i = 0; i < F->numPolynomials; i++)
clear_polynomial(&F->polynomials[i]);
free(F->polynomials);
F->polynomials = NULL;
mpq_clear(F->norm_sqr);
for (i = 0; i < F->numExponentials; i++)
clear_exponential(&F->exponentials[i]);
free(F->exponentials);
F->exponentials = NULL;
F->numVariables = F->numPolynomials = F->maximumDegree = F->isReal = F->numExponentials = 0;
return;
}
void print_number(FILE *OUT, int digits, complex_number z)
/***************************************************************\
* USAGE: prints z to OUT *
\***************************************************************/
{
int base = 10;
// verify that z is a valid number
if (mpfr_number_p(z->re) && mpfr_number_p(z->im))
{ // print to OUT
mpf_out_str(OUT, base, digits, z->re);
if (mpfr_sgn(z->im) >= 0)
fprintf(OUT, "+");
mpf_out_str(OUT, base, digits, z->im);
fprintf(OUT, "*I");
}
else
fprintf(OUT, "NaN+NaN*I");
return;
}
void print_rational_number(FILE *OUT, rational_complex_number z)
/***************************************************************\
* USAGE: prints z to OUT *
\***************************************************************/
{
int base = 10;
mpq_out_str(OUT, base, z->re);
if (mpq_sgn(z->im) >= 0)
fprintf(OUT, "+");
mpq_out_str(OUT, base, z->im);
fprintf(OUT, "*I");
return;
}
void print_vector_coordinate(FILE *OUT, int digits, complex_vector z)
/***************************************************************\
* USAGE: prints z *
\***************************************************************/
{
int i, size = z->size, base = 10;
if (size > 0)
{ // print each coordinate
for (i = 0; i < size; i++)
{
mpf_out_str(OUT, base, digits, z->coord[i]->re);
fprintf(OUT, " ");
mpf_out_str(OUT, base, digits, z->coord[i]->im);
fprintf(OUT, "\n");
}
}
return;
}
void print_vector(FILE *OUT, int digits, complex_vector z)
/***************************************************************\
* USAGE: prints z *
\***************************************************************/
{
int i, size = z->size;
if (size <= 0)
{ // nothing to print
fprintf(OUT, "[];\n");
}
else
{ // size > 0
for (i = 0; i < size; i++)
{
if (i == 0)
fprintf(OUT, "[");
fprintf(OUT, "[");
print_number(OUT, digits, z->coord[i]);
fprintf(OUT, "]");
if (i == size - 1)
fprintf(OUT, "];\n\n");
else
fprintf(OUT, ";\n");
}
}
return;
}
void print_rational_vector_coordinate(FILE *OUT, rational_complex_vector z)
/***************************************************************\
* USAGE: prints z *
\***************************************************************/
{
int i, size = z->size, base = 10;
if (size > 0)
{ // print each coordinate
for (i = 0; i < size; i++)
{
mpq_out_str(OUT, base, z->coord[i]->re);
fprintf(OUT, " ");
mpq_out_str(OUT, base, z->coord[i]->im);
fprintf(OUT, "\n");
}
}
return;
}
void print_rational_vector(FILE *OUT, rational_complex_vector z)
/***************************************************************\
* USAGE: prints z *
\***************************************************************/
{
int i, size = z->size;
if (size <= 0)
{ // nothing to print
fprintf(OUT, "[];\n");
}
else
{ // size > 0
for (i = 0; i < size; i++)
{
if (i == 0)
fprintf(OUT, "[");
fprintf(OUT, "[");
print_rational_number(OUT, z->coord[i]);
fprintf(OUT, "]");
if (i == size - 1)
fprintf(OUT, "];\n\n");
else
fprintf(OUT, ";\n");
}
}
return;
}
void print_matrix(FILE *OUT, int digits, complex_matrix z)
/***************************************************************\
* USAGE: prints z *
\***************************************************************/
{
int i, j, rows = z->rows, cols = z->cols;
if (rows <= 0 || cols <= 0)
{ // nothing to print
fprintf(OUT, "[];\n");
}
else
{ // rows > 0 && cols > 0
for (i = 0; i < rows; i++)
{
if (i == 0)
fprintf(OUT, "[");
fprintf(OUT, "[");
for (j = 0; j < cols; j++)
{
print_number(OUT, digits, z->entry[i][j]);
if (j + 1 < cols)
fprintf(OUT, ", ");
else
fprintf(OUT, "]");
}
if (i == rows - 1)
fprintf(OUT, "];\n\n");
else
fprintf(OUT, ";\n");
}
}
return;
}
void print_rational_matrix(FILE *OUT, rational_complex_matrix z)
/***************************************************************\
* USAGE: prints z *
\***************************************************************/
{
int i, j, rows = z->rows, cols = z->cols;
if (rows <= 0 || cols <= 0)
{ // nothing to print
fprintf(OUT, "[];\n");
}
else
{ // rows > 0 && cols > 0
for (i = 0; i < rows; i++)
{
if (i == 0)
fprintf(OUT, "[");
fprintf(OUT, "[");
for (j = 0; j < cols; j++)
{
print_rational_number(OUT, z->entry[i][j]);
if (j + 1 < cols)
fprintf(OUT, ", ");
else
fprintf(OUT, "]");
}
if (i == rows - 1)
fprintf(OUT, "];\n\n");
else
fprintf(OUT, ";\n");
}
}
return;
}
void determine_pivot_tolerances(mpf_t pivot_tol, mpf_t pivot_drop_tol, int prec)
/***************************************************************\
* USAGE: compute tolerances based on prec *
\***************************************************************/
{
int num_digits = (int) floor(prec * log10(2.0) - 2.5);
// setup pivot_tol
mpf_set_prec(pivot_tol, prec);
mpf_set_ui(pivot_tol, 10);
mpf_pow_ui(pivot_tol, pivot_tol, num_digits);
mpf_ui_div(pivot_tol, 1, pivot_tol);
// setup pivot_drop_tol
mpf_set_prec(pivot_drop_tol, prec);
mpf_set_ui(pivot_drop_tol, 10);
mpf_pow_ui(pivot_drop_tol, pivot_drop_tol, num_digits - 3);
return;
}
void load_floating_points(int *numPoints, complex_vector **points, int numVars, char *PtsFile)
/***************************************************************\
* USAGE: load points from PtsFile *
\***************************************************************/
{
int i, j, rV, base = 10;
FILE *IN = fopen(PtsFile, "r");
// error checking - file must exist
if (IN == NULL)
{
printf("\nERROR: '%s' does not exist!\n", PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
// read in the number of points
rV = fscanf(IN, "%d", numPoints);
// error checking
if (rV != 1)
{
printf("\nERROR: Unable to read the number of points stored in '%s'.\n", PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
else if (*numPoints <= 0)
{
printf("ERROR: The number of points in '%s' must be positive!\n", PtsFile);
errExit(ERROR_CONFIGURATION);
}
// allocate points
*points = (complex_vector *)errMalloc((*numPoints) * sizeof(complex_vector));
// read in the points
for (i = 0; i < *numPoints; i++)
{ // setup points[i]
initialize_vector((*points)[i], numVars);
for (j = 0; j < numVars; j++)
{ // setup real part
rV = mpf_inp_str((*points)[i]->coord[j]->re, IN, base);
// error checking
if (rV == 0)
{
printf("\nERROR: Unable to read in %d floating point vectors from '%s'.\n", *numPoints, PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
// setup imag part
rV = mpf_inp_str((*points)[i]->coord[j]->im, IN, base);
// error checking
if (rV == 0)
{
printf("\nERROR: Unable to read in %d floating point vectors from '%s'.\n", *numPoints, PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
}
}
// close file
fclose(IN);
return;
}
void load_rational_points(int *numPoints, rational_complex_vector **points, int numVars, char *PtsFile)
/***************************************************************\
* USAGE: load rational points from PtsFile *
\***************************************************************/
{
int i, j, rV, base = 10;
FILE *IN = fopen(PtsFile, "r");
// error checking - file must exist
if (IN == NULL)
{
printf("\nERROR: '%s' does not exist!\n", PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
// read in the number of points
rV = fscanf(IN, "%d", numPoints);
// error checking
if (rV != 1)
{
printf("\nERROR: Unable to read the number of points stored in '%s'.\n", PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
else if (*numPoints <= 0)
{
printf("ERROR: The number of points in '%s' must be positive!\n", PtsFile);
errExit(ERROR_CONFIGURATION);
}
// allocate points
*points = (rational_complex_vector *)errMalloc((*numPoints) * sizeof(rational_complex_vector));
// read in the points
for (i = 0; i < *numPoints; i++)
{ // setup points[i]
initialize_rational_vector((*points)[i], numVars);
for (j = 0; j < numVars; j++)
{ // setup real part
rV = mpq_inp_str((*points)[i]->coord[j]->re, IN, base);
// error checking
if (rV == 0)
{
printf("\nERROR: Unable to read in %d rational vectors from '%s'.\n", *numPoints, PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
// setup imag part
rV = mpq_inp_str((*points)[i]->coord[j]->im, IN, base);
// error checking
if (rV == 0)
{
printf("\nERROR: Unable to read in %d rational vectors from '%s'.\n", *numPoints, PtsFile);
errExit(ERROR_FILE_NOT_EXIST);
}
// canonicalize rational numbers
mpq_canonicalize((*points)[i]->coord[j]->re);
mpq_canonicalize((*points)[i]->coord[j]->im);
}
}
// close file
fclose(IN);
return;
}
void initialize_point_struct(point_struct *Pt, int numVariables)
/***************************************************************\
* USAGE: initialize the memory *
\***************************************************************/
{ // initialize the memory
initialize_vector(Pt->origX, numVariables);
initialize_vector(Pt->x, numVariables);
initialize_vector(Pt->Nx, numVariables);
mpf_init(Pt->norm_x);
initialize_number(Pt->origAlpha);
initialize_number(Pt->origBeta);
initialize_number(Pt->origGamma);
initialize_number(Pt->alpha);
initialize_number(Pt->beta);
initialize_number(Pt->gamma);
Pt->isApproxSoln = Pt->isActive = Pt->isReal = 0;
return;
}
void initialize_rational_point_struct(rational_point_struct *Pt, int numVariables)
/***************************************************************\
* USAGE: initialize the memory *
\***************************************************************/
{ // initialize the memory
initialize_rational_vector(Pt->origX, numVariables);
initialize_rational_vector(Pt->x, numVariables);
initialize_rational_vector(Pt->Nx, numVariables);
mpq_init(Pt->norm_sqr_x);
initialize_rational_number(Pt->origAlpha_sqr);
initialize_rational_number(Pt->origBeta_sqr);
initialize_rational_number(Pt->origGamma_sqr);
initialize_rational_number(Pt->alpha_sqr);
initialize_rational_number(Pt->beta_sqr);
initialize_rational_number(Pt->gamma_sqr);
Pt->isApproxSoln = Pt->isActive = Pt->isReal = 0;
return;
}
void clear_point_struct(point_struct *Pt)
/***************************************************************\
* USAGE: clear the memory *
\***************************************************************/
{ // clear the memory
clear_vector(Pt->origX);
clear_vector(Pt->x);
clear_vector(Pt->Nx);
mpf_clear(Pt->norm_x);
clear_number(Pt->origAlpha);
clear_number(Pt->origBeta);
clear_number(Pt->origGamma);
clear_number(Pt->alpha);
clear_number(Pt->beta);
clear_number(Pt->gamma);
Pt->isApproxSoln = Pt->isActive = Pt->isReal = 0;
return;
}
void clear_rational_point_struct(rational_point_struct *Pt)
/***************************************************************\
* USAGE: clear the memory *
\***************************************************************/
{ // initialize the memory
clear_rational_vector(Pt->origX);
clear_rational_vector(Pt->x);
clear_rational_vector(Pt->Nx);
mpq_clear(Pt->norm_sqr_x);
clear_rational_number(Pt->origAlpha_sqr);
clear_rational_number(Pt->origBeta_sqr);
clear_rational_number(Pt->origGamma_sqr);
clear_rational_number(Pt->alpha_sqr);
clear_rational_number(Pt->beta_sqr);
clear_rational_number(Pt->gamma_sqr);
Pt->isApproxSoln = Pt->isActive = Pt->isReal = 0;
return;
}
void create_random_number_str(char **str)
/***************************************************************\
* USAGE: creates a random number in [-1,1] *
\***************************************************************/
{
int i, counter = 0, numDigits = RATIONALDIGITLENGTH;
// allocate str to size
*str = (char *)errRealloc(*str, (2*numDigits + 4) * sizeof(char));
// random sign
if (rand() % 2)
{
(*str)[counter] = '-';
counter++;
}
for (i = 0; i < numDigits; i++)
{
(*str)[counter] = 48 + (rand() % 10); // ASCII for the digits
counter++;
}
(*str)[counter] = '/';
counter++;
(*str)[counter] = '1';
counter++;
for (i = 0; i < numDigits; i++)
{
(*str)[counter] = 48 + (rand() % 10); // ASCII for the digits