-
-
Notifications
You must be signed in to change notification settings - Fork 425
/
zariski_vankampen.py
1933 lines (1693 loc) · 71.4 KB
/
zariski_vankampen.py
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
# sage.doctest: needs sage.geometry.polyhedron sage.graphs sage.groups sage.rings.number_field
r"""
Zariski-Van Kampen method implementation
This file contains functions to compute the fundamental group of
the complement of a curve in the complex affine or projective plane,
using Zariski-Van Kampen approach. It depends on the package ``sirocco``.
The current implementation allows to compute a presentation of the
fundamental group of curves over the rationals or number fields with
a fixed embedding on `\QQbar`.
Instead of computing a representation of the braid monodromy, we
choose several base points and a system of paths joining them that
generate all the necessary loops around the points of the discriminant.
The group is generated by the free groups over these points, and
braids over these paths give relations between these generators.
This big group presentation is simplified at the end.
AUTHORS:
- Miguel Marco (2015-09-30): Initial version
EXAMPLES::
sage: # needs sirocco
sage: from sage.schemes.curves.zariski_vankampen import fundamental_group, braid_monodromy
sage: R.<x, y> = QQ[]
sage: f = y^3 + x^3 - 1
sage: braid_monodromy(f)
([s1*s0, s1*s0, s1*s0], {0: 0, 1: 0, 2: 0}, {}, 3)
sage: fundamental_group(f)
Finitely presented group < x0 | >
"""
# ****************************************************************************
# Copyright (C) 2015 Miguel Marco <mmarco@unizar.es>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
import itertools
from copy import copy
from itertools import combinations
from sage.combinat.permutation import Permutation
from sage.functions.generalized import sign
from sage.geometry.voronoi_diagram import VoronoiDiagram
from sage.graphs.graph import Graph
from sage.groups.braid import BraidGroup
from sage.groups.finitely_presented import wrap_FpGroup
from sage.groups.free_group import FreeGroup
from sage.groups.perm_gps.permgroup_named import SymmetricGroup
from sage.matrix.constructor import matrix
from sage.misc.cachefunc import cached_function
from sage.misc.flatten import flatten
from sage.misc.lazy_import import lazy_import
from sage.misc.misc_c import prod
from sage.parallel.decorate import parallel
from sage.rings.complex_interval_field import ComplexIntervalField
from sage.rings.complex_mpfr import ComplexField
from sage.rings.integer_ring import ZZ
from sage.rings.number_field.number_field import NumberField
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.qqbar import QQbar
from sage.rings.rational_field import QQ
from sage.rings.real_mpfr import RealField
from sage.schemes.curves.constructor import Curve
lazy_import('sage.libs.braiding', ['leftnormalform', 'rightnormalform'])
roots_interval_cache = {}
def braid_from_piecewise(strands):
r"""
Compute the braid corresponding to the piecewise linear curves strands.
INPUT:
- ``strands`` -- a list of lists of tuples ``(t, c1, c2)``, where ``t``
is a number between 0 and 1, and ``c1`` and ``c2`` are rationals
or algebraic reals.
OUTPUT:
The braid formed by the piecewise linear strands.
EXAMPLES::
sage: # needs sirocco
sage: from sage.schemes.curves.zariski_vankampen import braid_from_piecewise
sage: paths = [[(0, 0, 1), (0.2, -1, -0.5), (0.8, -1, 0), (1, 0, -1)],
....: [(0, -1, 0), (0.5, 0, -1), (1, 1, 0)],
....: [(0, 1, 0), (0.5, 1, 1), (1, 0, 1)]]
sage: braid_from_piecewise(paths)
s0*s1
"""
L = strands
i = min(val[1][0] for val in L)
totalpoints = [[[a[0][1], a[0][2]]] for a in L]
indices = [1 for a in range(len(L))]
while i < 1:
for j, val in enumerate(L):
if val[indices[j]][0] > i:
xauxr = val[indices[j] - 1][1]
xauxi = val[indices[j] - 1][2]
yauxr = val[indices[j]][1]
yauxi = val[indices[j]][2]
aaux = val[indices[j] - 1][0]
baux = val[indices[j]][0]
interpolar = xauxr + (yauxr - xauxr)*(i - aaux) / (baux - aaux)
interpolai = xauxi + (yauxi - xauxi)*(i - aaux) / (baux - aaux)
totalpoints[j].append([interpolar, interpolai])
else:
totalpoints[j].append([val[indices[j]][1],
val[indices[j]][2]])
indices[j] = indices[j] + 1
i = min(val[indices[k]][0] for k, val in enumerate(L))
for j, val in enumerate(L):
totalpoints[j].append([val[-1][1], val[-1][2]])
braid = []
G = SymmetricGroup(len(totalpoints))
def sgn(x, y):
if x < y:
return 1
if x > y:
return -1
return 0
for i in range(len(totalpoints[0]) - 1):
l1 = [totalpoints[j][i] for j in range(len(L))]
l2 = [totalpoints[j][i + 1] for j in range(len(L))]
M = [[l1[s], l2[s]] for s in range(len(l1))]
M.sort()
l1 = [a[0] for a in M]
l2 = [a[1] for a in M]
cruces = []
for j, l2j in enumerate(l2):
l1j = l1[j]
for k in range(j):
if l2j < l2[k]:
t = (l1j[0] - l1[k][0]) / ((l2[k][0] - l2j[0]) + (l1j[0] - l1[k][0]))
s = sgn(l1[k][1] * (1 - t) + t * l2[k][1],
l1j[1] * (1 - t) + t * l2j[1])
cruces.append([t, k, j, s])
if cruces:
cruces.sort()
P = G(Permutation([]))
while cruces:
# we select the crosses in the same t
crucesl = [c for c in cruces if c[0] == cruces[0][0]]
crossesl = [(P(c[2] + 1) - P(c[1] + 1), c[1], c[2], c[3])
for c in crucesl]
cruces = cruces[len(crucesl):]
while crossesl:
crossesl.sort()
c = crossesl.pop(0)
braid.append(c[3] * min(map(P, [c[1] + 1, c[2] + 1])))
P = G(Permutation([(c[1] + 1, c[2] + 1)])) * P
crossesl = [(P(cr[2] + 1) - P(cr[1] + 1),
cr[1], cr[2], cr[3]) for cr in crossesl]
B = BraidGroup(len(L))
return B(braid)
def discrim(pols) -> tuple:
r"""
Return the points in the discriminant of the product of the polynomials
of a list or tuple ``pols``.
The result is the set of values of the first variable for which
two roots in the second variable coincide.
INPUT:
- ``pols`` -- a list or tuple of polynomials in two variables with
coefficients in a number field with a fixed embedding in `\QQbar`.
OUTPUT:
A tuple with the roots of the discriminant in `\QQbar`.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import discrim
sage: R.<x, y> = QQ[]
sage: flist = (y^3 + x^3 - 1, 2 * x + y)
sage: sorted((discrim(flist)))
[-0.522757958574711?,
-0.500000000000000? - 0.866025403784439?*I,
-0.500000000000000? + 0.866025403784439?*I,
0.2613789792873551? - 0.4527216721561923?*I,
0.2613789792873551? + 0.4527216721561923?*I,
1]
"""
x, y = pols[0].parent().gens()
field = pols[0].base_ring()
pol_ring = PolynomialRing(field, (x,))
@parallel
def discrim_pairs(f, g):
if g is None:
return pol_ring(f.discriminant(y))
return pol_ring(f.resultant(g, y))
pairs = [(f, None) for f in pols] + [tuple(t) for t
in combinations(pols, 2)]
fdiscrim = discrim_pairs(pairs)
rts = ()
poly = 1
for u in fdiscrim:
h0 = u[1].radical()
h1 = h0 // h0.gcd(poly)
rts += tuple(h1.roots(QQbar, multiplicities=False))
poly = poly * h1
return rts
@cached_function
def corrected_voronoi_diagram(points):
r"""
Compute a Voronoi diagram of a set of points with rational coordinates.
The given points are granted to lie one in each bounded region.
INPUT:
- ``points`` -- a tuple of complex numbers
OUTPUT:
A Voronoi diagram constructed from rational approximations of the points,
with the guarantee that each bounded region contains exactly one of the
input points.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import corrected_voronoi_diagram
sage: points = (2, I, 0.000001, 0, 0.000001*I)
sage: V = corrected_voronoi_diagram(points)
sage: V
The Voronoi diagram of 9 points of dimension 2 in the Rational Field
sage: V.regions()
{P(-7, 0): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices and 2 rays,
P(0, -7): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices and 2 rays,
P(0, 0): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices,
P(0, 1): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 5 vertices,
P(0, 1/1000000): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices,
P(0, 7): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 3 vertices and 2 rays,
P(1/1000000, 0): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 5 vertices,
P(2, 0): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 5 vertices,
P(7, 0): A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 2 vertices and 2 rays}
"""
prec = 53
point_coordinates = [(p.real(), p.imag()) for p in points]
while True:
RF = RealField(prec)
apprpoints = {(QQ(RF(p[0])), QQ(RF(p[1]))): p
for p in point_coordinates}
added_points = 3 * max(map(abs, flatten(apprpoints))) + 1
configuration = list(apprpoints.keys()) + [(added_points, 0),
(-added_points, 0),
(0, added_points),
(0, -added_points)]
V = VoronoiDiagram(configuration)
valid = True
for r in V.regions().items():
if (not r[1].rays() and
not r[1].interior_contains(apprpoints[r[0].affine()])):
prec += 53
valid = False
break
if valid:
break
return V
def orient_circuit(circuit, convex=False, precision=53, verbose=False):
r"""
Reverse a circuit if it goes clockwise; otherwise leave it unchanged.
INPUT:
- ``circuit`` -- a circuit in the graph of a Voronoi Diagram, given
by a list of edges
- ``convex`` -- boolean (default: ``False``); if set to ``True`` a simpler
computation is made
- ``precision`` -- bits of precision (default: 53)
- ``verbose`` -- boolean (default: ``False``); for testing purposes
OUTPUT:
The same circuit if it goes counterclockwise, and its reversed otherwise,
given as the ordered list of vertices with identic extremities.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import orient_circuit
sage: points = [(-4, 0), (4, 0), (0, 4), (0, -4), (0, 0)]
sage: V = VoronoiDiagram(points)
sage: E = Graph()
sage: for reg in V.regions().values():
....: if reg.rays() or reg.lines():
....: E = E.union(reg.vertex_graph())
sage: E.vertices(sort=True)
[A vertex at (-2, -2),
A vertex at (-2, 2),
A vertex at (2, -2),
A vertex at (2, 2)]
sage: cir = E.eulerian_circuit()
sage: cir
[(A vertex at (-2, -2), A vertex at (2, -2), None),
(A vertex at (2, -2), A vertex at (2, 2), None),
(A vertex at (2, 2), A vertex at (-2, 2), None),
(A vertex at (-2, 2), A vertex at (-2, -2), None)]
sage: cir_oriented = orient_circuit(cir); cir_oriented
(A vertex at (-2, -2), A vertex at (2, -2), A vertex at (2, 2),
A vertex at (-2, 2), A vertex at (-2, -2))
sage: cirinv = list(reversed([(c[1],c[0],c[2]) for c in cir]))
sage: cirinv
[(A vertex at (-2, -2), A vertex at (-2, 2), None),
(A vertex at (-2, 2), A vertex at (2, 2), None),
(A vertex at (2, 2), A vertex at (2, -2), None),
(A vertex at (2, -2), A vertex at (-2, -2), None)]
sage: orient_circuit(cirinv) == cir_oriented
True
sage: cir_oriented == orient_circuit(cir, convex=True)
True
sage: P0=[(1,1/2),(0,1),(1,1)]; P1=[(0,3/2),(-1,0)]
sage: Q=Polyhedron(P0).vertices()
sage: Q = [Q[2], Q[0], Q[1]] + [_ for _ in reversed(Polyhedron(P1).vertices())]
sage: Q
[A vertex at (1, 1/2), A vertex at (0, 1), A vertex at (1, 1),
A vertex at (0, 3/2), A vertex at (-1, 0)]
sage: E = Graph()
sage: for v, w in zip(Q, Q[1:] + [Q[0]]):
....: E.add_edge((v, w))
sage: cir = orient_circuit(E.eulerian_circuit(), precision=1, verbose=True)
2
sage: cir
(A vertex at (1, 1/2), A vertex at (0, 1), A vertex at (1, 1),
A vertex at (0, 3/2), A vertex at (-1, 0), A vertex at (1, 1/2))
"""
vectors = [v[1].vector() - v[0].vector() for v in circuit]
circuit_vertex = (circuit[0][0],) + tuple(e[1] for e in circuit)
circuit_vertex = tuple(circuit_vertex)
if convex:
pr = matrix([vectors[0], vectors[1]]).determinant()
if pr > 0:
# return circuit
return circuit_vertex
elif pr < 0:
return tuple(reversed(circuit_vertex))
prec = precision
while True:
CIF = ComplexIntervalField(prec)
totalangle = sum((CIF(*vectors[i]) / CIF(*vectors[i - 1])).argument()
for i in range(len(vectors)))
if totalangle < 0:
return tuple(reversed(circuit_vertex))
if totalangle > 0:
return circuit_vertex
prec *= 2
if verbose:
print(prec)
def voronoi_cells(V, vertical_lines=frozenset()):
r"""
Compute the graph, the boundary graph, a base point, a positive orientation
of the boundary graph, and the dual graph of a corrected Voronoi diagram.
INPUT:
- ``V`` -- a corrected Voronoi diagram
- ``vertical_lines`` -- frozenset (default: ``frozenset()``); indices of the
vertical lines
OUTPUT:
- ``G`` -- the graph of the 1-skeleton of ``V``
- ``E`` -- the subgraph of the boundary
- ``p`` -- a vertex in ``E``
- ``EC`` -- a list of vertices (representing a counterclockwise orientation
of ``E``) with identical first and last elements)
- ``DG`` -- the dual graph of ``V``, where the vertices are labelled
by the compact regions of ``V`` and the edges by their dual edges.
- ``vertical_regions`` -- dictionary for the regions associated
with vertical lines
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import corrected_voronoi_diagram, voronoi_cells
sage: points = (2, I, 0.000001, 0, 0.000001*I)
sage: V = corrected_voronoi_diagram(points)
sage: G, E, p, EC, DG, VR = voronoi_cells(V, vertical_lines=frozenset((1,)))
sage: Gv = G.vertices(sort=True)
sage: Ge = G.edges(sort=True)
sage: len(Gv), len(Ge)
(12, 16)
sage: Ev = E.vertices(sort=True); Ev
[A vertex at (-4, 4),
A vertex at (-49000001/14000000, 1000001/2000000),
A vertex at (-7/2, -7/2),
A vertex at (-7/2, 1/2000000),
A vertex at (1/2000000, -7/2),
A vertex at (2000001/2000000, -24500001/7000000),
A vertex at (11/4, 4),
A vertex at (9/2, -9/2),
A vertex at (9/2, 9/2)]
sage: Ev.index(p)
7
sage: EC
(A vertex at (9/2, -9/2),
A vertex at (9/2, 9/2),
A vertex at (11/4, 4),
A vertex at (-4, 4),
A vertex at (-49000001/14000000, 1000001/2000000),
A vertex at (-7/2, 1/2000000),
A vertex at (-7/2, -7/2),
A vertex at (1/2000000, -7/2),
A vertex at (2000001/2000000, -24500001/7000000),
A vertex at (9/2, -9/2))
sage: len(DG.vertices(sort=True)), len(DG.edges(sort=True))
(5, 7)
sage: edg = DG.edges(sort=True)[0]; edg
((0,
(A vertex at (9/2, -9/2),
A vertex at (9/2, 9/2),
A vertex at (11/4, 4),
A vertex at (2000001/2000000, 500001/1000000),
A vertex at (2000001/2000000, -24500001/7000000),
A vertex at (9/2, -9/2))),
(1,
(A vertex at (-49000001/14000000, 1000001/2000000),
A vertex at (1000001/2000000, 1000001/2000000),
A vertex at (2000001/2000000, 500001/1000000),
A vertex at (11/4, 4),
A vertex at (-4, 4),
A vertex at (-49000001/14000000, 1000001/2000000))),
(A vertex at (2000001/2000000, 500001/1000000), A vertex at (11/4, 4), None))
sage: edg[-1] in Ge
True
sage: VR
{1: (A vertex at (-49000001/14000000, 1000001/2000000),
A vertex at (1000001/2000000, 1000001/2000000),
A vertex at (2000001/2000000, 500001/1000000),
A vertex at (11/4, 4),
A vertex at (-4, 4),
A vertex at (-49000001/14000000, 1000001/2000000))}
"""
regions = V.regions()
points = [p for p in V.regions().keys() if V.regions()[p].is_compact()]
compact_regions = [regions[p] for p in points]
vertical_regions = {}
non_compact_regions = [reg for reg in V.regions().values()
if not reg.is_compact()]
G = Graph([u.vertices() for v in compact_regions for u in v.faces(1)],
format='list_of_edges')
E = Graph([u.vertices() for v in non_compact_regions for u in v.faces(1)
if u.is_compact()], format='list_of_edges')
p = next(E.vertex_iterator())
EC = orient_circuit(E.eulerian_circuit())
DG = Graph()
for i, reg in enumerate(compact_regions):
Greg0 = orient_circuit(reg.graph().eulerian_circuit(), convex=True)
if i in vertical_lines:
vertical_regions[i] = Greg0
DG.add_vertex((i, Greg0))
for e in G.edges(sort=True):
a, b = e[:2]
regs = [v for v in DG.vertices(sort=True) if a in v[1] and b in v[1]]
if len(regs) == 2:
DG.add_edge(regs[0], regs[1], e)
return (G, E, p, EC, DG, vertical_regions)
def followstrand(f, factors, x0, x1, y0a, prec=53) -> list:
r"""
Return a piecewise linear approximation of the homotopy continuation
of the root ``y0a`` from ``x0`` to ``x1``.
INPUT:
- ``f`` -- an irreducible polynomial in two variables
- ``factors`` -- a list of irreducible polynomials in two variables
- ``x0`` -- a complex value, where the homotopy starts
- ``x1`` -- a complex value, where the homotopy ends
- ``y0a`` -- an approximate solution of the polynomial `F(y) = f(x_0, y)`
- ``prec`` -- the precision to use
OUTPUT:
A list of values `(t, y_{tr}, y_{ti})` such that:
- ``t`` is a real number between zero and one
- `f(t \cdot x_1 + (1-t) \cdot x_0, y_{tr} + I \cdot y_{ti})`
is zero (or a good enough approximation)
- the piecewise linear path determined by the points has a tubular
neighborhood where the actual homotopy continuation path lies, and
no other root of ``f``, nor any root of the polynomials in ``factors``,
intersects it.
EXAMPLES::
sage: # needs sirocco
sage: from sage.schemes.curves.zariski_vankampen import followstrand
sage: R.<x, y> = QQ[]
sage: f = x^2 + y^3
sage: x0 = CC(1, 0)
sage: x1 = CC(1, 0.5)
sage: followstrand(f, [], x0, x1, -1.0) # abs tol 1e-15
[(0.0, -1.0, 0.0),
(0.7500000000000001, -1.015090921153253, -0.24752813818386948),
(1.0, -1.026166099551513, -0.32768940253604323)]
sage: fup = f.subs({y: y - 1/10})
sage: fdown = f.subs({y: y + 1/10})
sage: followstrand(f, [fup, fdown], x0, x1, -1.0) # abs tol 1e-15
[(0.0, -1.0, 0.0),
(0.5303300858899107, -1.0076747107983448, -0.17588022709184917),
(0.7651655429449553, -1.015686131039112, -0.25243563967299404),
(1.0, -1.026166099551513, -0.3276894025360433)]
"""
if f.degree() == 1:
CF = ComplexField(prec)
g = f.change_ring(CF)
(x, y) = g.parent().gens()
y0 = CF[y](g.subs({x: x0})).roots()[0][0]
y1 = CF[y](g.subs({x: x1})).roots()[0][0]
res = [(0.0, y0.real(), y0.imag()), (1.0, y1.real(), y1.imag())]
return res
CIF = ComplexIntervalField(prec)
CC = ComplexField(prec)
G = f.change_ring(QQbar).change_ring(CIF)
x, y = G.parent().gens()
g = G.subs({x: (1 - x) * CIF(x0) + x * CIF(x1)})
coefs = []
deg = g.total_degree()
for d in range(deg + 1):
for i in range(d + 1):
c = CIF(g.coefficient({x: d - i, y: i}))
cr = c.real()
ci = c.imag()
coefs += list(cr.endpoints())
coefs += list(ci.endpoints())
yr = CC(y0a).real()
yi = CC(y0a).imag()
coefsfactors = []
degsfactors = []
for fc in factors:
degfc = fc.degree()
degsfactors.append(degfc)
G = fc.change_ring(QQbar).change_ring(CIF)
g = G.subs({x: (1 - x) * CIF(x0) + x * CIF(x1)})
for d in range(degfc + 1):
for i in range(d + 1):
c = CIF(g.coefficient({x: d - i, y: i}))
cr = c.real()
ci = c.imag()
coefsfactors += list(cr.endpoints())
coefsfactors += list(ci.endpoints())
from sage.libs.sirocco import (contpath, contpath_mp, contpath_comps, contpath_mp_comps)
try:
if prec == 53:
if factors:
points = contpath_comps(deg, coefs, yr, yi, degsfactors, coefsfactors)
else:
points = contpath(deg, coefs, yr, yi)
else:
if factors:
points = contpath_mp_comps(deg, coefs, yr, yi, prec, degsfactors, coefsfactors)
else:
points = contpath_mp(deg, coefs, yr, yi, prec)
return points
except Exception:
return followstrand(f, factors, x0, x1, y0a, 2 * prec)
def newton(f, x0, i0):
r"""
Return the interval Newton operator.
INPUT:
- ``f`` -- a univariate polynomial
- ``x0`` -- a number
- ``I0`` -- an interval
OUTPUT:
The interval `x_0-\frac{f(x_0)}{f'(I_0)}`
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import newton
sage: R.<x> = QQbar[]
sage: f = x^3 + x
sage: x0 = 1/10
sage: I0 = RIF((-1/5,1/5))
sage: n = newton(f, x0, I0)
sage: n
0.0?
sage: n.real().endpoints()
(-0.0147727272727274, 0.00982142857142862)
sage: n.imag().endpoints()
(0.000000000000000, -0.000000000000000)
"""
return x0 - f(x0) / f.derivative()(i0)
def fieldI(field):
r"""
Return the (either double or trivial) extension of a number field which contains ``I``.
INPUT:
- ``field`` -- a number field with an embedding in `\QQbar`.
OUTPUT:
The extension ``F`` of ``field`` containing ``I`` with an embedding in `\QQbar`.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import fieldI
sage: p = QQ[x](x^5 + 2 * x + 1)
sage: a0 = p.roots(QQbar, multiplicities=False)[0]
sage: F0.<a> = NumberField(p, embedding=a0)
sage: fieldI(F0)
Number Field in prim with defining polynomial
x^10 + 5*x^8 + 14*x^6 - 2*x^5 - 10*x^4 + 20*x^3 - 11*x^2 - 14*x + 10
with prim = 0.4863890359345430? + 1.000000000000000?*I
sage: F0 = CyclotomicField(5)
sage: fieldI(F0)
Number Field in prim with defining polynomial
x^8 - 2*x^7 + 7*x^6 - 10*x^5 + 16*x^4 - 10*x^3 - 2*x^2 + 4*x + 1
with prim = -0.3090169943749474? + 0.04894348370484643?*I
sage: fieldI(QuadraticField(3))
Number Field in prim with defining polynomial x^4 - 4*x^2 + 16
with prim = -1.732050807568878? + 1.000000000000000?*I
sage: fieldI(QuadraticField(-3))
Number Field in prim with defining polynomial x^4 + 8*x^2 + 4
with prim = 0.?e-18 - 0.732050807568878?*I
If ``I`` is already in the field, the result is the field itself::
sage: from sage.schemes.curves.zariski_vankampen import fieldI
sage: p = QQ[x](x^4 + 1)
sage: a0 = p.roots(QQbar, multiplicities=False)[0]
sage: F0.<a> = NumberField(p, embedding=a0)
sage: F1 = fieldI(F0)
sage: F0 == F1
True
sage: QuadraticField(-1) == fieldI(QuadraticField(-1))
True
"""
I0 = QQbar.gen()
if I0 in field:
return field
field_a = field[I0]
field_b = field_a.absolute_field('b0')
b0 = field_b.gen()
q = b0.minpoly()
qembd = field_b.embeddings(QQbar)
for h1 in qembd:
b1 = h1(b0)
b2 = h1(field_b(field_a.gen(0)))
b3 = QQbar(field.gen(0))
F1 = NumberField(q, 'prim', embedding=b1)
if b3 in F1 and b2.imag() > 0:
return F1
@parallel
def roots_interval(f, x0):
"""
Find disjoint intervals that isolate the roots of a polynomial for a fixed
value of the first variable.
INPUT:
- ``f`` -- a bivariate squarefree polynomial
- ``x0`` -- a Gauss rational number corresponding to the first coordinate
The intervals are taken as big as possible to be able to detect when two
approximate roots of `f(x_0, y)` correspond to the same exact root, where
`f` is the product of the polynomials in `flist`.
The result is given as a dictionary, where the keys are
approximations to the roots with rational real and imaginary
parts, and the values are intervals containing them.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import roots_interval, fieldI
sage: R.<x, y> = QQ[]
sage: K = fieldI(QQ)
sage: f = y^3 - x^2
sage: f = f.change_ring(K)
sage: ri = roots_interval(f, 1)
sage: ri
{-138907099/160396102*I - 1/2: -1.? - 1.?*I,
138907099/160396102*I - 1/2: -1.? + 1.?*I,
1: 1.? + 0.?*I}
sage: [r.endpoints() for r in ri.values()]
[(0.566987298107781 - 0.433012701892219*I,
1.43301270189222 + 0.433012701892219*I,
0.566987298107781 + 0.433012701892219*I,
1.43301270189222 - 0.433012701892219*I),
(-0.933012701892219 - 1.29903810567666*I,
-0.0669872981077806 - 0.433012701892219*I,
-0.933012701892219 - 0.433012701892219*I,
-0.0669872981077806 - 1.29903810567666*I),
(-0.933012701892219 + 0.433012701892219*I,
-0.0669872981077806 + 1.29903810567666*I,
-0.933012701892219 + 1.29903810567666*I,
-0.0669872981077806 + 0.433012701892219*I)]
"""
F1 = f.base_ring()
x, y = f.parent().gens()
fx = F1[y](f.subs({x: F1(x0)}))
roots = fx.roots(QQbar, multiplicities=False)
result = {}
for i, r in enumerate(roots):
prec = 53
IF = ComplexIntervalField(prec)
CF = ComplexField(prec)
divisor = 4
diam = min((CF(r) - CF(r0)).abs()
for r0 in roots[:i] + roots[i + 1:]) / divisor
envelop = IF(diam) * IF((-1, 1), (-1, 1))
while not newton(fx, r, r + envelop) in r + envelop:
prec += 53
IF = ComplexIntervalField(prec)
CF = ComplexField(prec)
divisor *= 2
diam = min((CF(r) - CF(r0)).abs()
for r0 in roots[:i] + roots[i + 1:]) / divisor
envelop = IF(diam) * IF((-1, 1), (-1, 1))
qapr = QQ(CF(r).real()) + QQbar.gen() * QQ(CF(r).imag())
if qapr not in r + envelop:
raise ValueError("could not approximate roots with exact values")
result[qapr] = r + envelop
return result
def roots_interval_cached(f, x0):
r"""
Cached version of :func:`roots_interval`.
TESTS::
sage: from sage.schemes.curves.zariski_vankampen import roots_interval, roots_interval_cached, roots_interval_cache, fieldI
sage: R.<x, y> = QQ[]
sage: K = fieldI(QQ)
sage: f = y^3 - x^2
sage: f = f.change_ring(K)
sage: (f, 1) in roots_interval_cache
False
sage: ri = roots_interval_cached(f, 1)
sage: ri
{-138907099/160396102*I - 1/2: -1.? - 1.?*I,
138907099/160396102*I - 1/2: -1.? + 1.?*I,
1: 1.? + 0.?*I}
sage: (f, 1) in roots_interval_cache
True
"""
global roots_interval_cache
try:
return roots_interval_cache[(f, x0)]
except KeyError:
result = roots_interval(f, x0)
roots_interval_cache[(f, x0)] = result
return result
def populate_roots_interval_cache(inputs):
r"""
Call :func:`roots_interval` to the inputs that have not been
computed previously, and cache them.
INPUT:
- ``inputs`` -- a list of tuples ``(f, x0)``
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import populate_roots_interval_cache, roots_interval_cache, fieldI
sage: R.<x,y> = QQ[]
sage: K=fieldI(QQ)
sage: f = y^5 - x^2
sage: f = f.change_ring(K)
sage: (f, 3) in roots_interval_cache
False
sage: populate_roots_interval_cache([(f, 3)])
sage: (f, 3) in roots_interval_cache
True
sage: roots_interval_cache[(f, 3)]
{-1.255469441943070? - 0.9121519421827974?*I: -2.? - 1.?*I,
-1.255469441943070? + 0.9121519421827974?*I: -2.? + 1.?*I,
0.4795466549853897? - 1.475892845355996?*I: 1.? - 2.?*I,
0.4795466549853897? + 1.475892845355996?*I: 1.? + 2.?*I,
14421467174121563/9293107134194871: 2.? + 0.?*I}
"""
global roots_interval_cache
tocompute = [inp for inp in inputs if inp not in roots_interval_cache]
problem_par = True
while problem_par: # hack to deal with random fails in parallelization
try:
result = roots_interval(tocompute)
for r in result:
roots_interval_cache[r[0][0]] = r[1]
problem_par = False
except TypeError:
pass
@parallel
def braid_in_segment(glist, x0, x1, precision={}):
"""
Return the braid formed by the `y` roots of ``f`` when `x` moves
from ``x0`` to ``x1``.
INPUT:
- ``glist`` -- a tuple of polynomials in two variables
- ``x0`` -- a Gauss rational
- ``x1`` -- a Gauss rational
- ``precision`` -- a dictionary (default: `{}`) which assigns a number
precision bits to each element of ``glist``
OUTPUT:
A braid.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import braid_in_segment, fieldI
sage: R.<x, y> = QQ[]
sage: K = fieldI(QQ)
sage: f = x^2 + y^3
sage: f = f.change_ring(K)
sage: x0 = 1
sage: x1 = 1 + I / 2
sage: braid_in_segment(tuple(_[0] for _ in f.factor()), x0, x1) # needs sirocco
s1
TESTS:
Check that :issue:`26503` is fixed::
sage: # needs sage.rings.real_mpfr sage.symbolic
sage: wp = QQ['t']([1, 1, 1]).roots(QQbar)[0][0]
sage: Kw.<wp> = NumberField(wp.minpoly(), embedding=wp)
sage: R.<x, y> = Kw[]
sage: z = -wp - 1
sage: f = y * (y + z) * x * (x - 1) * (x - y) * (x + z * y - 1) * (x + z * y + wp)
sage: from sage.schemes.curves.zariski_vankampen import fieldI, braid_in_segment
sage: Kw1 = fieldI(Kw)
sage: g = f.subs({x: x + 2 * y})
sage: g = g.change_ring(Kw1)
sage: p1 = QQbar(sqrt(-1/3))
sage: p1a = CC(p1)
sage: p1b = QQ(p1a.real()) + I*QQ(p1a.imag())
sage: p2 = QQbar(1/2 + sqrt(-1/3)/2)
sage: p2a = CC(p2)
sage: p2b = QQ(p2a.real()) + I*QQ(p2a.imag())
sage: glist = tuple([_[0] for _ in g.factor()])
sage: B = braid_in_segment(glist, p1b, p2b); B # needs sirocco
s5*s3^-1
"""
precision1 = precision.copy()
g = prod(glist)
F1 = g.base_ring()
x, y = g.parent().gens()
intervals = {}
if not precision1:
precision1 = {f: 53 for f in glist}
y0s = []
for f in glist:
if f.variables() == (y,):
f0 = F1[y](f)
else:
f0 = F1[y](f.subs({x: F1(x0)}))
y0sf = f0.roots(QQbar, multiplicities=False)
y0s += list(y0sf)
while True:
CIFp = ComplexIntervalField(precision1[f])
intervals[f] = [r.interval(CIFp) for r in y0sf]
if not any(a.overlaps(b) for a, b in
itertools.combinations(intervals[f], 2)):
break
precision1[f] *= 2
strands = []
for f in glist:
for i in intervals[f]:
aux = followstrand(f, [p for p in glist if p != f],
x0, x1, i.center(), precision1[f])
strands.append(aux)
complexstrands = [[(QQ(a[0]), QQ(a[1]), QQ(a[2])) for a in b]
for b in strands]
centralbraid = braid_from_piecewise(complexstrands)
initialstrands = []
finalstrands = []
initialintervals = roots_interval_cached(g, x0)
finalintervals = roots_interval_cached(g, x1)
I1 = QQbar.gen()
for cs in complexstrands:
ip = cs[0][1] + I1 * cs[0][2]
fp = cs[-1][1] + I1 * cs[-1][2]
matched = 0
for center, interval in initialintervals.items():
if ip in interval:
initialstrands.append([(0, center.real(), center.imag()),
(1, cs[0][1], cs[0][2])])
matched += 1
if matched != 1:
precision1 = {f: precision1[f] * 2 for f in glist}
return braid_in_segment(glist, x0, x1, precision=precision1)
matched = 0
for center, interval in finalintervals.items():
if fp in interval:
finalstrands.append([(0, cs[-1][1], cs[-1][2]),
(1, center.real(), center.imag())])
matched += 1
if matched != 1:
precision1 = {f: precision1[f] * 2 for f in glist}
return braid_in_segment(glist, x0, x1, precision=precision1)
initialbraid = braid_from_piecewise(initialstrands)
finalbraid = braid_from_piecewise(finalstrands)
return initialbraid * centralbraid * finalbraid
def geometric_basis(G, E, EC0, p, dual_graph, vertical_regions={}) -> list:
r"""
Return a geometric basis, based on a vertex.
INPUT:
- ``G`` -- a graph with the bounded edges of a Voronoi Diagram
- ``E`` -- a subgraph of ``G`` which is a cycle containing the bounded
edges touching an unbounded region of a Voronoi Diagram
- ``EC0`` -- A counterclockwise orientation of the vertices of ``E``
- ``p`` -- a vertex of ``E``
- ``dual_graph`` -- a dual graph for a plane embedding of ``G`` such that
``E`` is the boundary of the non-bounded component of the complement.
The edges are labelled as the dual edges and the vertices are labelled
by a tuple whose first element is the an integer for the position and the
second one is the cyclic ordered list of vertices in the region
- ``vertical_regions`` -- dictionary (default: `{}`); its keys are
the vertices of ``dual_graph`` to fix regions associated with
vertical lines
OUTPUT: A geometric basis and a dictionary.
The geometric basis is formed by a list of sequences of paths. Each path is a
ist of vertices, that form a closed path in ``G``, based at ``p``, that goes
to a region, surrounds it, and comes back by the same path it came. The
concatenation of all these paths is equivalent to ``E``.
The dictionary associates to each vertical line the index of the generator
of the geometric basis associated to it.
EXAMPLES::
sage: from sage.schemes.curves.zariski_vankampen import geometric_basis, corrected_voronoi_diagram, voronoi_cells
sage: points = (0, -1, I, 1, -I)
sage: V = corrected_voronoi_diagram(points)
sage: G, E, p, EC, DG, VR = voronoi_cells(V, vertical_lines=frozenset((0 .. 4)))
sage: gb, vd = geometric_basis(G, E, EC, p, DG, vertical_regions=VR)
sage: gb
[[A vertex at (5/2, -5/2), A vertex at (5/2, 5/2), A vertex at (-5/2, 5/2),
A vertex at (-1/2, 1/2), A vertex at (-1/2, -1/2), A vertex at (1/2, -1/2),
A vertex at (1/2, 1/2), A vertex at (-1/2, 1/2), A vertex at (-5/2, 5/2),
A vertex at (5/2, 5/2), A vertex at (5/2, -5/2)],
[A vertex at (5/2, -5/2), A vertex at (5/2, 5/2), A vertex at (-5/2, 5/2),
A vertex at (-1/2, 1/2), A vertex at (1/2, 1/2), A vertex at (5/2, 5/2),
A vertex at (5/2, -5/2)],
[A vertex at (5/2, -5/2), A vertex at (5/2, 5/2), A vertex at (1/2, 1/2),
A vertex at (1/2, -1/2), A vertex at (5/2, -5/2)], [A vertex at (5/2, -5/2),
A vertex at (1/2, -1/2), A vertex at (-1/2, -1/2), A vertex at (-1/2, 1/2),
A vertex at (-5/2, 5/2), A vertex at (-5/2, -5/2), A vertex at (-1/2, -1/2),