-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphc.module
1097 lines (1051 loc) · 39.5 KB
/
phc.module
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
phc := module()
description "PHCpack interface: polynomial homotopy continuation";
export
####################### data structures #############################
makeSolution, # point on the variety
makeSystem, # polynomial system
makeWitnessSet, # witness set
setPHCloc, # set path for PHCpack, debug option, etc.
####################### functions ###################################
systemToFile, # writes the system to a file in PHC format
solutionsAppendToFile,# appends solutions to a file in PHC format
solve, # runs the black-box solver
embed, # constructs an embedded system
factor, # performs numerical decomposion
track, # solves and tracks the solution paths
drawPaths, # draws continuation paths
refine, # refines solutions
cascade, # computes the list of equidimensional
# solution components
decompose, # finds the irreducible decomposition
filter, # filters a witness set
deflationStep, # make one deflation step
eqnbyeqn, # equation by equation
intersectWitnessSets,
makeStartSystem,
printSolutions,
subsVariables,
printSystem,
computeResiduals, # recalculates residuals for a list of solutions
# and (their) system
############### Bertini subroutines -------------------------
readSolutionsBertini,
removeBertiniFiles,
makeBertiniInput,
makeBertiniStart,
solveBertini, # same as solve
trackBertini; # uses Bertini as a simple tracker
####################### locals ######################################
local
D_::integer, # to debug(>0) or not to debug(0),
# >=10 = do not clean up temporary files
phcloc::string, # location of PHC executable and .temp directory for temporary files
slash::string, # directory separator (system dependent)
phcexe::string,
nColors::integer,
colorList::array,
setup,
cleanup,
solutionsToFile,
getVars,
etoE,
mysystem, mysystem2,
tempFileName,
fileToString,
parseSolutions,
parsePHCsystem,
readSolutions,
tempFileNameCounter;
####################### options #####################################
option
package,
load = setup,
unload = cleanup;
################ constructors #########################################
setup := proc()
local t;
D_ := 0; ################## DEBUG level is set here !!! #####
phcexe := "phc";
phcloc := currentdir();
t := kernelopts(platform);
if t="dos" or t="windows"
then slash := "\\";
else slash := "/";
fi;
userinfo( 1, 'phc[setup]', "PHCmaple loaded!" );
NULL
end proc; #setup
cleanup := proc()
end proc; #cleanup
setPHCloc := proc(s::string, {debug::integer := 0},
{phc::string := "PHCpack"})
local t, Defaults, Options, num_reqd;
setup(); #!!! have to call this as the package is not loaded automatically
# via read("phc.module")
D_ := debug;
if phc = "PHCpack" then
if D_>0 then phcexe := "phc -0"; fi;
elif phc = "Bertini" then
phcexe := "bertini";
else error "unknown software";
fi;
t := phcloc;
phcloc := s;
if not FileTools[Exists](""||phcloc||slash||".tempPHCmaple")
then # mkdir "the name of directory in quotes"
ssystem("mkdir "||phcloc||slash||".tempPHCmaple");
fi;
tempFileNameCounter := 0;
t
end proc;
makeSolution := proc(a::list,b::integer,c,d,e,f)
if nargs = 1 then
Record(`coords`=a, `mult`=1, `time`=0.0, `err`=0.0, `rco`=1.0, `res`=0.0)
else
Record(`coords`=a, `mult`=b, `time`=c, `err`=d, `rco`=e, `res`=f)
end if;
end proc;
makeSystem := proc(a::list,b::list,c::list)
Record(`vars`=a, `slacks`=b, `polys`=expand(c))
end proc;
makeWitnessSet := proc(a,b::list)
Record(`system`=a, `points`=b)
end proc;
############### mysystem ################################################
mysystem := proc(s::string)
local bat, t, str;
t := kernelopts(platform);
if t="dos" or t="windows" then
bat := tempFileName("bat");
fopen(bat,WRITE):
fprintf(bat,""||phcloc||slash||s):
fclose(bat):
ssystem(bat);
if D_<10 then
fremove(bat);
fi;
else
str := ""||phcloc||slash||s;
if D_>=4 then print("LINUX: ssystem("||str||")"); fi;
ssystem(str);
fi;
end proc; #mysystem
############### mysystem2 ###############################################
#### written to bypass a Unix Maple bug
mysystem2 := proc(s::string, opts::string, input::string, output::string)
local bat, t;
t := kernelopts(platform);
if t="dos" or t="windows" then
mysystem(""||s||" "||opts||" < "||input||" > "||output);
else
bat := ""||phcloc||slash;
if D_>0
then bat := ""||bat||"PHCmapleUNIXhelper_debug";
else bat := ""||bat||"PHCmapleUNIXhelper";
fi;
bat := ""||bat||" "||phcloc||slash||s||" "||opts||" "||input||" "||output;
if D_>=4 then print("UNIXhelper call: "||bat); fi;
ssystem(bat);
fi;
end proc; #mysystem2
############### tempFileName ########################################
tempFileName := proc(s::string)
local rs, f;
# rs := convert(rand(),string);
while true do
rs := tempFileNameCounter;
f := ""||phcloc||slash||".tempPHCmaple"||slash||rs||"."||s;
if not FileTools[Exists](f) then return f; fi;
tempFileNameCounter := tempFileNameCounter + 1;
od;
end proc; #tempFileName
############### getVars #################################################
getVars := proc(sys) [op(sys:-vars), op(sys:-slacks)];
end proc; # returns the list of all variables in _sys_
############### etoE ####################################################
etoE := proc(s) StringTools[SubstituteAll]( s, "e", "E" );
end proc; # replaces "e" with "E" in a string
############### parseSolutions ##########################################
parseSolutions := proc(s::list,sys)
# option trace;
local vars;
vars := getVars(sys);
map(proc(a)
# _a_ is a list of form [time=..., multiplicity=...,
# x1=..., ..., xn=..., err=..., rco=..., res=...]
local p, k, i, b;
p := vector(nops(vars));
for i from 3 to nops(a)-3 do
b := a[i];
if not member(op(1,b), vars, 'k')
then error "incorrect variable "||b;
end if;
p[k] := op(2,b);
end do;
makeSolution(convert(p,list), op(2,a[2]), op(2,a[1]), op(2,a[nops(a)-2]), op(2,a[nops(a)-1]), op(2,a[nops(a)]))
end proc, s)
end proc; #parseSolutions
############### readSolutions ##########################################
readSolutions := proc(f::string, system)
local sols,mf;
mf := tempFileName("readSols"):
mysystem(""||phcexe||" -z "||f||" "||mf):
read(mf); sols := %;
if D_<10 then
fremove(mf);
fi;
parseSolutions(sols,system);
end proc; #readSolutions
############### systemToFile ##########################################
systemToFile := proc(sys, filename::string)
local i;
fopen(filename,WRITE):
if nops(sys:-vars)+nops(sys:-slacks)=nops(sys:-polys) then
fprintf(filename,`%d\n`, nops(sys:-polys));
else fprintf(filename,`%d %d\n`, nops(sys:-polys),
nops(sys:-vars)+nops(sys:-slacks));
end if;
for i from 1 to nops(sys:-polys) do
fprintf(filename,`%s;\n`,etoE(convert(sys:-polys[i],string))):
end do:
fclose(filename):
end proc; #systemToFile
############### fileToString ########################################
fileToString := proc(filename::string) # option trace;
local res,line;
res := "";
line := readline(filename);
while line <> 0 do
res := ""||res||"\n"||line;
line := readline(filename);
end do;
res
end proc; #fileToString
############### parsePHCsystem ########################################
parsePHCsystem := proc(vars::list, slacks::list, s::string) # option trace;
local r;
r := substring(s,1..(StringTools[FirstFromRight](";",s)-1));
r := StringTools[SubstituteAll]( r, ";", "," );
r := StringTools[SubstituteAll]( r, "**", "^" );
r := StringTools[SubstituteAll]( r, "i", "I" );
makeSystem(vars,slacks,[parse(r)])
end proc; #parsePHCsystem
############### printSystem ##########################################
printSystem := proc(sys)
local i;
for i from 1 to nops(sys:-polys) do
printf(`(%d) %s\n`, i, convert(sys:-polys[i],string)):
end do:
end proc; #printSystem
############# computeResiduals ######################################
computeResiduals := proc(sys, s::list)
local vars, i;
vars := getVars(sys);
for i from 1 to nops(s) do
s[i]:-res := max(
op(map(
p->abs(evalf(subs(op(zip((x,y)->x=y, vars,s[i]:-coords)), p))),
sys:-polys))
);
end do;
end proc:
############### subsVariables ##########################################
subsVariables := proc(sys, sub)
makeSystem(subs(sub,sys:-vars), subs(sub,sys:-slacks), subs(sub,sys:-polys))
end proc; #subsVariables
############### solutionsToFile ##########################################
solutionsToFile := proc(sys, s::list, f::string)
local vars, i, j, str;
vars := getVars(sys);
# writing the file with solutions in Maple format
fopen(f,WRITE):
fprintf(f, "[");
for i from 1 to nops(s) do
fprintf(f, "[time = %s,\n multiplicity = %d,",
etoE(convert(s[i]:-time,string)),s[i]:-mult);
for j from 1 to nops(vars) do
# fprintf(f,"%s = %s + %s*I ",
# convert(vars[j],string),
# etoE( convert(Re(s[i]:-coords[j]),string)),
# etoE( convert(Im(s[i]:-coords[j]),string))
# );
fprintf(f,"%s = %s",
convert(vars[j],string),
etoE( convert(s[i]:-coords[j],string))
);
if j < nops(vars)
then fprintf(f, ", ");
else fprintf(f, ",\n err = %s, rco = %s, res = %s]",
etoE(convert(s[i]:-err,string)),
etoE(convert(s[i]:-rco,string)),
etoE(convert(s[i]:-res,string)));
end if;
end do;
if i = nops(s) then fprintf(f,"]");
else fprintf(f, " ,\n");
end if;
end do;
fclose(f);
end proc; #solutionsToFile
############### solutionsAppendToFile ##########################################
solutionsAppendToFile := proc(sys, s::list, filename::string)
local solfile,cmd;
solfile := tempFileName("sols"):
# writing data to the corresponding files
solutionsToFile(sys, s, solfile):
# converting solutions in Maple format to PHCpack format
# and appending it to _filename_
cmd := ""||phcexe||" -z "||solfile||" "||filename:
mysystem(cmd):
if D_<10 then
fremove(solfile);
fi;
end proc; #solutionsAppendToFile
############### printSolutions ##########################################
printSolutions := proc(sys, s::list)
local vars, i;
vars := getVars(sys);
for i from 1 to nops(s) do
printf(`(%d) %s\n`, i, convert([seq(vars[j]=evalf[5](s[i]:-coords[j]),
j=1..nops(vars))],string)):
end do;
end proc; #printSolutions
############### solve #################################################
solve := proc(target)
# option trace;
description `Calls the black-box solver in PHCpack from Maple`:
# IN: (system) is a target system;
# OUT: (list<solution>) is a list of solutions to _target_
local i,sols,targetfile,tarsolfile,outfile,startfile,stsolfile:
# call Bertini
if phcexe = "bertini" then return solveBertini(target); fi;
# declaring all the file names:
tarsolfile := tempFileName("tasols"):
targetfile := tempFileName("target"):
outfile := tempFileName("output"):
# writing data to the corresponding files
systemToFile(target,targetfile):
# launching blackbox solver; converting the solutions to the Maple format
mysystem(""||phcexe||" -b "||targetfile||" "||outfile):
mysystem(""||phcexe||" -z "||targetfile||" "||tarsolfile):
read(tarsolfile); sols := %;
# clean up
if D_<10 then
fremove(targetfile): fremove(tarsolfile): fremove(outfile):
fi;
# parse and output the solutions
parseSolutions(sols, target)
end proc; # solve
############### track #################################################
track := proc( target, start, s::list, n::integer,
{max_steps::integer := 0},
{condition_hom::integer := 0},
{hom_parameter_k::integer := 2},
{block_size::integer := 0},
{max_step_size::float := -1.},
{a_const::complex := 1.+0.*I}
)
description `Calls the path trackers in PHCpack from Maple`:
# IN:
# (1::system) target system
# (2::system) start system
# (s::list<solution>) solutions of the start system
# (n::integer) number of subdivisions
# OUT: list<list<solution>> is a list of n+1 "solutions lists"
local step,sList,last_sList,h,ii,i,sols,l,r,batchfile,targetfile,tarsolfile,outfile,startfile,stsolfile:
if D_ >=4 then
printf("Start system:\n");
printSystem(start);
printf("Start solutions:\n");
printSolutions(start, s);
printf("Target system:\n");
printSystem(target);
fi;
# call Bertini
if phcexe = "bertini" then return trackBertini(target,start,s); fi;
# declare homotopy
sList := [s];
h := (t) -> makeSystem(
target:-vars,
target:-slacks,
expand(start:-polys*(1-t) + target:-polys*t)
);
if nargs<4 then n := 1; step := 1;
else step := 1.0/n;
end if;
for ii from 0 to n-1 do ##################################
i := ii*step;
# declaring all the file names:
tarsolfile := tempFileName("tasols"); stsolfile := tempFileName("ssols"):
targetfile := tempFileName("target"): startfile := tempFileName("start"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(h(i+step),targetfile):
systemToFile(h(i),startfile):
last_sList := sList[nops(sList)];
map( proc(s) s:-time := 0; end proc, last_sList): # make sure solutions' "start time" is 0 (phc wants that)
solutionsToFile(start, last_sList, stsolfile):
map( proc(s) s:-time := i; end proc, last_sList): # make sure solutions' "start time" is corrected
# converting solutions in Maple format to PHCpack format
# and appending it to the startfile
mysystem(""||phcexe||" -z "||stsolfile||" "||startfile):
# make a batch
fopen(batchfile,WRITE):
fprintf(batchfile, ""||targetfile||"\n"||outfile||"\n"||"n\n"||startfile||"\n");
# first menu
if hom_parameter_k <> 2
then fprintf(batchfile, "k\n%d\n", hom_parameter_k);
fi;
fprintf(batchfile, "a\n%f\n%f\n", Re(a_const), Im(a_const));
fprintf(batchfile, "0\n"); # exit the menu
# second menu
if max_steps > 0 then fprintf(batchfile,"3\n%d\n", max_steps); fi;
if condition_hom > 0 then fprintf(batchfile,"1\n%d\n", condition_hom); fi;
if block_size < 0 or block_size >= nops(s) # n of paths to track simultaneously
then fprintf(batchfile,"2\n%d\n", nops(s)); # ... by default is set to the number of solutions
elif block_size > 1 then fprintf(batchfile,"2\n%d\n", block_size);
fi;
if max_step_size > 0 then
if max_step_size < 0.000001 then
fprintf(batchfile,"9\n%f\n", max_step_size/2); # minimum change in <t> ("along path" predictor option)
fi;
#fprintf(batchfile,"10\n%f\n", max_step_size/2); # minimum change in <t> ("end game" predictor option)
fprintf(batchfile,"11\n%f\n", max_step_size); # maximum change in <t> ("along path" predictor option)
if max_step_size < 0.01
then fprintf(batchfile,"12\n%f\n", max_step_size); # maximum change in <t> ("end game" predictor option)
fi;
fi;
fprintf(batchfile,"0\n0\n"): # exit the menu
fclose(batchfile):
# launching "phc -p"
mysystem2(phcexe, "-p", batchfile, "phc.log");
mysystem(""||phcexe||" -z "||outfile||" "||tarsolfile):
read(tarsolfile): sols := %:
# clean up
if D_<10 then
fremove(stsolfile): fremove(tarsolfile):
fremove(targetfile): fremove(startfile):
fremove(outfile): fremove(batchfile):
fi;
# parse and store the solutions
sList := [op(sList), parseSolutions(sols, target)];
end do; #############################################################
if D_ >=4 then
printf("Target solutions:\n");
printSolutions(target, sList[n+1]);
fi;
sList
end proc; # track
############### drawPaths #################################################
drawPaths := proc(s::list, sys) # option trace;
description `Calls _track_ and draws the continuation paths coordinate by coordinate`:
# IN:
# (list<solution>)
# (system) is a target system;
# OUT: (list<PLOT>) plots of the paths coord-by-coord
local c, nP, i, nPaths, nCoords, curveList, rootList, ret, varname, diskRadius, seqNorms;
colorList := [aquamarine, blue, green, gold, khaki,
maroon, cyan, brown, navy, turquoise];
nColors := nops(colorList);
nPaths := nops(s[1]):
nCoords := nops(s[1][1]:-coords):
c := array(1..nPaths,1..nCoords):
for nP from 1 to nPaths do
for i from 1 to nCoords do
c[nP,i] := map(u->u[nP]:-coords[i], s):
# c[nP,i] := remove(u -> abs(u) > 5, c[nP,i]):
c[nP,i] := map(u->[Re(u),Im(u)], c[nP,i]):
end do; end do;
ret := [];
for i from 1 to nCoords do
curveList := [];
rootList := [];
for nP from 1 to nPaths do
curveList := [op(curveList), plottools[curve](c[nP,i], color=colorList[nP mod nColors + 1])];
rootList := [op(rootList),
plottools[disk](c[nP,i][nops(c[1,1])], max(c[1,1][nops(c[1,1])][1],1)/50, color=red),
plottools[disk](c[nP,i][1], max(c[1,1][nops(c[1,1])][1],1)/50, color=yellow)];
end do;
varname := op(i,getVars(sys));
ret := [op(ret), plots[display](curveList, rootList, title="variable: "||varname)];
end do;
ret
end proc; # drawPaths
############### embed #################################################
embed := proc(sys, d::integer) # option trace;
description `Constructs an embedded system for the given system`:
# IN:
# (system) is a system;
# (integer) is the expected dimension of the solution set
# OUT: (system) is the corresponding embedded system
local i,sols,sysfile,outfile,startfile,batchfile,res,line:
# declaring all the file names:
sysfile := tempFileName("system"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(sys,sysfile):
fopen(batchfile,WRITE):
fprintf(batchfile,"1\ny\n"||sysfile||"\n"||outfile||"\n%d\nn\n" ,d):
fclose(batchfile):
# launching "phc -c"
mysystem2(phcexe, "-c", batchfile, "phc.log"):
# read the results
readline(outfile); # skip the first line containing the number of polys
res := fileToString(outfile);
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile):
fremove(outfile):
fi;
# return the system in _res_
parsePHCsystem(getVars(sys),map(i->zz||i, [seq(i,i=1..d)]),res)
end proc; # embed
############### factor #################################################
factor := proc(p) # option trace;
description `Factors a multivariate polynomial`:
# IN: a polynomial to factor;
# OUT: factored polynomial
local i,targetfile,outfile,vars,factorfile,L;
# declaring all the file names:
targetfile := tempFileName("target"):
outfile := tempFileName("output"):
# writing data to the corresponding files
vars := convert(indets(p),list);
systemToFile(makeSystem(vars,[],[p]), targetfile):
# launching factorization;
mysystem(""||phcexe||" -b "||targetfile||" "||outfile):
#collecting factors
L := [];
for i from 1 while FileTools[Exists]( ""||targetfile||"_f"||i ) do
factorfile := ""||targetfile||"_f"||i;
readline(factorfile); # skip the first line containing the number of polys
L := [op(L), (parsePHCsystem(vars, [], fileToString(factorfile))):-polys[1]];
if D_<10 then
fremove(factorfile):
fi
end do;
# clean up
if D_<10 then
fremove(targetfile):
fremove(outfile):
fi;
L
end proc; # factor
############### refine #################################################
refine := proc(sols,sys) # option trace;
description `Refines specified solutions of a system`:
# IN:
# (list<solution>) the list of solutions to be refined;
# (system) is a system;
# OUT: (list<solution>) the list of refined solutions.
local i,sysfile,solfile,outfile,startfile,batchfile,reffile,l,r,refsols;
# call Bertini (returns unrefined solutions!!!)
if phcexe = "bertini" then return sols; fi;
# declaring all the file names:
sysfile := tempFileName("system"):
outfile := tempFileName("output"):
solfile := tempFileName("sol"):
batchfile := tempFileName("batch"):
reffile := tempFileName("ref");
# writing data to the corresponding files
systemToFile(sys,sysfile):
solutionsToFile(sys, sols, solfile):
# convert solutions and append them to sysfile
mysystem(""||phcexe||" -z "||solfile||" "||sysfile):
# make a batch
fopen(batchfile,WRITE):
fprintf(batchfile,"3\ny\n"||sysfile||"\n"||outfile||"\n"):
for i from 3 to nargs do
if type(args[i], `=`) then
l := op(1,args[i]);
r := op(2,args[i]);
if l = digits then
fprintf(batchfile,"7\n%d\n" ,r):
elif l = residual_tol then
fprintf(batchfile,"4\n%E\n" ,r):
elif l = error_tol then
fprintf(batchfile,"3\n%E\n" ,r):
elif l = singular_tol then
fprintf(batchfile,"5\n%E\n" ,r):
elif l = max_iterations then
fprintf(batchfile,"6\n%d\n" ,r):
else error "refine: wrong option";
end if;
end if;
end do;
fprintf(batchfile,"0\n"): # exit the menu
fclose(batchfile):
# launching "phc -v"
mysystem2(phcexe, "-v", batchfile, "phc.log"):
mysystem(""||phcexe||" -z "||outfile||" "||reffile):
read(reffile); refsols := %;
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile): fremove(solfile):
fremove(outfile): fremove(reffile):
fi;
# return refined solutions
parseSolutions(refsols, sys)
end proc; # refine
############### cascade #################################################
cascade := proc(embsys, embsols::list) # option trace;
description `Runs a cascade of homotopies for an embedded positive dimensional system`:
# IN:
# (system) is a EMBEDDED system;
# (list<solutions>) the list of solutions to the system;
# OUT: (list<witness sets>) is a list of witness sets for pure dimensional components
local i,sols,sysfile,embsolfile,outfile,startfile,batchfile,
compfile,compsys,compList,res,newSlacks:
# declaring all the file names:
sysfile := tempFileName("system"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(embsys,sysfile):
# append solutions
solutionsAppendToFile(embsys, embsols, sysfile);
fopen(batchfile,WRITE):
fprintf(batchfile,"2\n"||sysfile||"\n"||outfile||"\n1\n2\n0\n"):
fclose(batchfile):
# launching "phc -c"
mysystem2(phcexe,"-c", batchfile, "phc.log"):
# read the results
compList := [];
for i from 0 to nops(embsys:-slacks) do
compfile := cat(sysfile,"_sw",convert(i, string));
if FileTools[Exists](compfile) then
readline(compfile); # skip the first line containing the number of polys
res := fileToString(compfile);
newSlacks := [op(1..i,embsys:-slacks)];
compsys := parsePHCsystem(embsys:-vars,newSlacks,res);
compList := [op(compList), makeWitnessSet(compsys,
readSolutions(compfile,compsys))];
if D_<10 then
fremove(compfile);
fi;
end if;
end do;
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile): fremove(outfile):
fi;
# return the list of components witness sets
compList
end proc; # cascade
############### decompose #################################################
decompose := proc(ws) # option trace;
description `Runs a cascade of homotopies for a positive dimensional system`:
# IN: (witness set) is a witness set for a component of pure dimension;
# OUT: (list<witness sets>) is the list of witness sets for the irreducible components
local i,sols,sysfile,embsolfile,outfile,startfile,batchfile,
embsys,compfile,compsys,compList,res:
# declaring all the file names:
sysfile := tempFileName("system"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(ws:-system,sysfile):
fopen(batchfile,WRITE):
fprintf(batchfile,"2\n"||sysfile||"\n"||outfile||"\n1\n0\n"):
fclose(batchfile):
solutionsAppendToFile(ws:-system,ws:-points,sysfile);
# launching "phc -f"
mysystem2(phcexe, "-f", batchfile, "phc.log"):
# read the results
compList := [];
i := 1;
while FileTools[Exists]( cat(sysfile,"_f",convert(i, string)) ) do
compfile := cat(sysfile,"_f",convert(i, string));
readline(compfile); # skip the first line containing the number of polys
res := fileToString(compfile);
compsys := parsePHCsystem(ws:-system:-vars,ws:-system:-slacks,res);
compList := [op(compList), makeWitnessSet(compsys,
readSolutions(compfile,compsys))];
i := i + 1;
if D_<10 then
fremove(compfile);
fi;
end do;
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile): fremove(outfile):
fi;
# return the list of components witness sets
compList
end proc; # decompose
############### filter #################################################
filter := proc(top, ws,
{residual_tol::float := -1.},
{wit_set_tol::float := -1.}
) # option trace;
description `Runs a cascade of homotopies for an embedded positive dimensional system`:
# IN:
# (witness set) top dimensional witness set
# (witness set) a witness set
# OUT: (witness set) the filtered witness set
local i,sols,sysfile,embsolfile,outfile,solfile,batchfile,newWS:
# declaring all the file names:
sysfile := tempFileName("system"):
solfile := tempFileName("sol"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(top:-system,sysfile):
solutionsAppendToFile(top:-system, top:-points, sysfile);
solutionsAppendToFile(ws:-system, ws:-points, solfile);
# create batch
fopen(batchfile,WRITE):
fprintf(batchfile,"1\n"||sysfile||"\n"||solfile||"\n"||outfile||"\n"):
if residual_tol > 0 then
fprintf(batchfile,"1\n%f\n", residual_tol); # tolerance for a point to sat. a polynomial
fi;
if wit_set_tol > 0 then
fprintf(batchfile,"2\n%f\n", wit_set_tol); # tolerance for a point to belong to a witness set
fi;
fprintf(batchfile,"0\n");
fclose(batchfile):
# launch "phc -f"
mysystem2(phcexe, "-f", batchfile, "phc.log"):
# read the results
newWS := makeWitnessSet( ws:-system,
readSolutions(outfile, ws:-system) );
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile): fremove(solfile): fremove(outfile):
fi;
# return the list of components witness sets
newWS
end proc; # filter
############### eqnbyeqn #################################################
eqnbyeqn := proc(sys) # option trace;
description `Solves the given system equation-by-equation`:
# IN:
# (sys) a system;
# OUT: (list<witness sets>) is a list of witness sets for pure dimensional components
local i,sols,sysfile,embsolfile,outfile,startfile,batchfile,
compfile,compsys,compList,res,newSlacks:
# declaring all the file names:
sysfile := tempFileName("system"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(sys,sysfile):
fopen(batchfile,WRITE):
fprintf(batchfile,"y\n"||sysfile||"\n"||outfile||"\nno\n0\nno\n0\n0\n"):
fclose(batchfile):
# launching "phc -a"
mysystem2(phcexe, "-a", batchfile, "phc.log"):
# read the results
compList := [];
for i from 0 to nops(sys:-vars-1) do
compfile := cat(outfile,"_w",convert(i, string));
if FileTools[Exists](compfile) then
if D_>0 then printf("eqnbyeqn: processing witness set of dim %d\n", i); fi;
readline(compfile); # skip the first line containing the number of polys
res := fileToString(compfile);
newSlacks := [seq(zz||j, j=1..i)];
compsys := parsePHCsystem(sys:-vars,newSlacks,res);
compList := [op(compList), makeWitnessSet(compsys,
readSolutions(compfile,compsys))];
if D_<10 then
fremove(compfile);
fi;
end if;
end do;
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile): fremove(outfile):
fi;
# return the list of components witness sets
compList
end proc; # eqnbyeqn
############### makeStartSystem #################################################
makeStartSystem := proc(esys) # option trace;
local va,n,s_sys,s_sols,i,flag,j,degs;
degs := map(p->degree(p),esys:-polys);
va := op(esys:-vars),op(esys:-slacks):
n := nops(esys:-polys):
s_sys := makeSystem([va],[],[seq(expand((1+I)*(va[i]^degs[i]-1)), i=1..n)]):
i := vector(n,[seq(0,i=1..n)]):
s_sols := []:
flag := true:
while flag do
j := n:
s_sols := [op(s_sols), makeSolution([seq(eval(exp(2*Pi*I*i[k]/degs[k])), k=1..n)])]:
i[j] := (i[j] + 1) mod degs[j];
while flag and i[j]=0 do
j := j - 1;
if j=1 then flag := false:
else
i[j] := (i[j] + 1) mod degs[j]:
end if:
end do:
end do:
s_sys,s_sols
end proc; #makeStartSystem
############### deflationStep #########################################
deflationStep := proc(sols,sys) # option trace;
description `Refines specified multiple solutions by making one deflation step`:
# IN:
# (list<solution>) the list of solutions to be refined;
# (system) is a system;
# OUT: (list<"rank" = ...,
# "deflated system" = ...,
# "points" = list<solution>>) the list of refined solutions
# grouped by the rank of their jacobian.
local i,sr,sysfile,solfile,outfile,startfile,batchfile,reffile,gfile,searchstring,
l,r,pos,refsols,res,nvars,defsys,corank;
# declaring all the file names:
sysfile := tempFileName("system"):
outfile := tempFileName("output"):
solfile := tempFileName("sol"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(sys,sysfile):
solutionsToFile(sys, sols, solfile):
# convert solutions and append them to sysfile
mysystem(""||phcexe||" -z "||solfile||" "||sysfile):
# make a batch
fopen(batchfile,WRITE):
fprintf(batchfile,"6\ny\n"||sysfile||"\n"||outfile||"\n1\ny\n3\n1\n"):
fprintf(batchfile,"0\n"): # exit the menu
fclose(batchfile):
# launching "phc -v"
mysystem2(phcexe, "-v", batchfile, "phc.log"):
# clean up
if D_<10 then
fremove(sysfile): fremove(batchfile): fremove(solfile):
fi;
# read the results
r := fileToString(outfile);
l := [];
searchstring := "See the file ";
while true do
pos := searchtext(searchstring,r);
if pos = 0 then break fi;
r := substring(r, pos+length(searchstring)..length(r));
# get the group filename
gfile := substring(r,1..searchtext("\n",r)-1);
# 1st line contains the number of polys and the number of vars
res := sscanf(readline(gfile),"%d%d");
if nops(res)=1 then
corank := 0;
else corank := res[2] - nops(sys:-vars);
fi;
res := fileToString(gfile);
# lm=lambda
res := StringTools[SubstituteAll](res, "lm[1,", "lambda[");
# get the deflated system
defsys := parsePHCsystem([op(getVars(sys)),seq(lambda[i],i=1..corank)], [], res);
reffile := tempFileName("ref");
if D_>=3 then
print("Parsing: "||phcexe||` -z `||gfile||` `||reffile);
fi;
mysystem(""||phcexe||` -z `||gfile||` `||reffile);
read(reffile); refsols := subs(seq(lm[1,i]=lambda[i],i=1..corank), %);
l := [op(l), ["corank" = corank, "deflated system" = defsys, "multipliers" = [seq(lambda[i],i=1..corank)],
"points" = parseSolutions(refsols, defsys)]];
if D_<10 then
fremove(gfile); fremove(reffile);
fi;
end do;
if D_<10 then
fremove(outfile);
fi;
# return refined solutions
return map(i->table(i),l);
end proc; # deflationStep
############### intersectWitnessSets #################################################
intersectWitnessSets := proc(A, B) # option trace;
description `Intersects components represented by two witness sets`:
# IN: (A) and (B) are witness sets;
# OUT: (list<witness sets>) is a list of witness sets for components
# of the intersection
local i,sols,fileA,fileB,embsolfile,outfile,startfile,batchfile,
compfile,compsys,compList,res,newSlacks:
# declaring all the file names:
fileA := tempFileName("compA"):
fileB := tempFileName("compB"):
outfile := tempFileName("output"):
batchfile := tempFileName("batch"):
# writing data to the corresponding files
systemToFile(A:-system,fileA):
systemToFile(B:-system,fileB):
# append solutions
solutionsAppendToFile(A:-system, A:-points, fileA);
solutionsAppendToFile(B:-system, B:-points, fileB);
fopen(batchfile,WRITE):
fprintf(batchfile,""||fileA||"\n"||fileB||"\n"||outfile||"\n"):
fclose(batchfile):
# launching "phc -w"
mysystem2(phcexe, "-w", batchfile, "phc.log"):
# read the results
compList := [];
for i from 0 to nops(A:-system:-slacks) do
compfile := cat(outfile,"_w",convert(i, string));
if FileTools[Exists](compfile) then
readline(compfile); # skip the first line containing the number of polys
res := fileToString(compfile);
newSlacks := [op(1..i,A:-system:-slacks)];
compsys := parsePHCsystem(A:-system:-vars,newSlacks,res);
compList := [op(compList), makeWitnessSet(compsys,
readSolutions(compfile,compsys))];
if D_<10 then
fremove(compfile);
fi;
end if;
end do;
# clean up
if D_<10 then
fremove(fileA): fremove(fileB): fremove(batchfile): fremove(outfile):
fi;
# return the list of components witness sets
compList
end proc; # intersectWitnessSets
############### makeBertiniInput #####################################
makeBertiniInput := proc(v::list, T::list, S::list := [])
# IN:
# v = variables
# T = polynomials of target system
# OUT:
# f = Bertini input filename
local f, i, TI, SI;
f := "input";
fopen(f,WRITE);
fprintf(f, "CONFIG\n");
fprintf(f, "MPTYPE: 2;\n"); #multiprecision
if nops(S) > 0 then
fprintf(f, "USERHOMOTOPY: 1;\n");
fi;
fprintf(f, "\nEND;\n\n");
fprintf(f, "INPUT\n\n");
if nops(S) > 0 then
fprintf(f, "variable ");
else fprintf(f, "variable_group ");
fi;
for i from 1 to nops(v) do
if i<nops(v)
then fprintf(f, "%s, ", v[i]);
else fprintf(f, "%s;\n", v[i]);
fi;
od;
fprintf(f, "function ");
for i from 1 to nops(T) do
if i<nops(T)
then fprintf(f, "f%d, ", i);
else fprintf(f, "f%d;\n\n", i);
fi;
od;
if nops(S) = 0 then
for i from 1 to nops(T) do
fprintf(f, "f%d = %s;\n", i, convert(T[i],string));
od;
else
fprintf(f, "pathvariable t;\n");
fprintf(f, "parameter s;\n");
fprintf(f, "s = t;\n");
for i from 1 to nops(T) do
TI := T[i];
SI := S[i];
fprintf(f, "f%d = (%s)*(1-s)+s*(%s);\n",
i, convert(TI,string), convert(SI,string));
od;
fi;
fprintf(f, "\nEND\n\n");
fclose(f);
end proc:
############### makeBertiniStart #####################################
makeBertiniStart := proc(s::list)