File: example_2d.m2

package info (click to toggle)
macaulay2 1.24.11%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 171,648 kB
  • sloc: cpp: 107,850; ansic: 16,307; javascript: 4,188; makefile: 3,947; lisp: 682; yacc: 604; sh: 476; xml: 177; perl: 114; lex: 65; python: 33
file content (195 lines) | stat: -rw-r--r-- 5,663 bytes parent folder | download | duplicates (3)
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
load "quadratic_tp_experiment.m2"

randomPolynomial = (d, R) -> sum(d+1, i -> random(i,R))


findTurningPoints = L -> (
    temp := sortTurningPoints L;
    if #temp === 0 then null
    else {(temp#0).LastT, (temp#1).LastT}
    )

sortTurningPoints = L -> apply(sort apply(L, sol -> {sol.LastT, sol}), last)    

-- will replace with better method (deflation)
getXo = tp ->  (1/2) * (matrix first tp + matrix last tp)

gett0 = tp -> (1/2) * ((first tp).LastT + (last tp).LastT)

getTangent = (tp, x0, H) -> (
   t0 := gett0 tp;
   J := evaluate(transpose jacobian transpose H, x0 | matrix{{t0}});
   n := numRows H;
   transpose submatrix'(((SVD J)#2)^{n}, ,{n} )
    ) 

-- tp is a pair of points approaching a turning point
-- this function is currently producing some sort of fatal error
jump = (tp, H, epsilon) -> (
    if tp === null then error("no turning points inputted");
    x0 := getXo(tp);
    t0 := gett0(tp);
    v := getTangent(tp, x0, H);    
    if any((first tp).Coordinates | (last tp).Coordinates, c -> not areEqual(imaginaryPart c, 0)) then 
    l1 := {transpose x0 + epsilon * v, transpose x0 - epsilon * v }
    else         (
	print "two conjugate nonreal";
	x1 := transpose x0 + epsilon * ii * v;
	l1 = {x1, conjugate x1};
	);
    -- following two lines from prev implementation give toR = zero mapping, generates fatal error in solveRealSystem
    -- R := RR[drop(gens ring H, -1)];
    -- toR := map(R, ring H, gens R | {sub(t0+ epsilon^2, R)});
    -- coefficient corrector: c := solve(x0, xprev, ClosestFit=>true), xprev := trackSegment(H, t0, t0- epsilon, x0)
    -- needs a better stopping criterion (pass number of corrections as option?)

    l1 = apply(l1, l -> l || matrix {{t0 + epsilon^2}});
    for i from 1 to 5 do l1 = apply(l1, l -> newton(polySystem H, l));
    l1
    )

-- first example seems to generate valid output, but gives weird "wrong # of inputs" error message
solveRealSystem = method()
solveRealSystem (PolySystem, PolySystem, List) := (f, g, startSols) -> (
    if ring f =!= ring g then error "expected systems from same ring";
    R := ring f;
    t := local t;
    S := CC (monoid[gens R,t]);
    t = last gens S;
    H := transpose (t*sub(gens ideal g,S) + (1-t)*sub(gens ideal f,S));
    temp := trackSegment(H,0,1,startSols);
    sols := select(temp, s -> status s != Regular);
    finishedSols := select(temp, s -> status s == Regular);
    while sols != {} do (
	sols' := sortTurningPoints(sols);
	if #sols' === 1 or not areEqual((sols'#0).LastT, (sols'#1).LastT)  then error "expected two failed paths";
        epsilon := 0.01;
	tp := take(sols', 2);
        jumped := jump(tp, H, epsilon);
        t0 := (tp#0).LastT;
	tp' := trackSegment(H, t0 +epsilon^2, 1, {point(jumped#0), point(jumped#1)});
	sols = drop(sols,2);
	for s in tp' do 
	if status s == Regular then finishedSols = finishedSols | {s}
	else sols = sols | {s};       		
	);
    finishedSols
    )

end

restart
needs "example_2d.m2"

setRandomSeed 1
R = CC[x,y]
(d1,d2) = (3,3)
f = polySystem {sub(randomPolynomial(d1, RR[gens R]), R),  sub(randomPolynomial(d2, RR[gens R]),R)}
startSols = solveSystem polySystem f
g = polySystem {sub(randomPolynomial(d1, RR[gens R]), R),  sub(randomPolynomial(d2, RR[gens R]),R)}
sols' = solveRealSystem(f,g,startSols)
apply(sols', p -> evaluate(g, matrix p))
findTurningPoints track(f,g,startSols)

(sols#1).Coordinates
epsilon = 0.01
tp = findTurningPoints sols
predictor = getPredictor(tp, epsilon)

sols2 = trackSegment(H, t0 +epsilon^2, 1, {point(predictor#0), point(predictor#1)})

epsilon = 0.01
tp = findTurningPoints sols2
predictor = getPredictor(tp, epsilon)
trackSegment(H, t0 +epsilon^2, 1, {point(predictor#0), point(predictor#1)})



solveSystem polySystem g
solutions = oo
x1
H = matrix {{t*sub(g,S) + (1-t)*sub(f,S)}}
(t0, t1) = (0, 1)


sols = trackSegment(H, 0, 1, {point{{3}},point{{2}},point{{0}},point{{-1}}})
x0 = (1/2) * (matrix sols#2 + matrix sols#3)
t0 = (sols#2).LastT


J
J = evaluate(transpose jacobian H, x0 | matrix{{t0}})
n = numRows H
v = submatrix'(((SVD J)#2)_{n}, {n}, )
epsilon = 0.1
x1 = x0 + epsilon * ii * v

toR = map(R, S, gens R | {t0+epsilon^2})


x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)

trackSegment(H, t0 +epsilon^2, 1, {point(x1)})
solutions = oo


H = matrix {{t*sub(g,S) + (1-t)*sub(f,S)}}
(t0, t1) = (0, 1)


sols = trackSegment(H, 0, 1, {point{{3}},point{{2}},point{{0}},point{{-1}}})
x0 = (1/2) * (matrix sols#2 + matrix sols#3)
t0 = (sols#2).LastT


J
J = evaluate(transpose jacobian H, x0 | matrix{{t0}})
n = numRows H
v = submatrix'(((SVD J)#2)_{n}, {n}, )
epsilon = 0.1
x1 = x0 + epsilon * ii * v

toR = map(R, S, gens R | {t0+epsilon^2})


x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)

trackSegment(H, t0 +epsilon^2, 1, {point(x1)})
solutions = oo


H = matrix {{t*sub(g,S) + (1-t)*sub(f,S)}}
(t0, t1) = (0, 1)


sols = trackSegment(H, 0, 1, {point{{3}},point{{2}},point{{0}},point{{-1}}})
x0 = (1/2) * (matrix sols#2 + matrix sols#3)
t0 = (sols#2).LastT


J
J = evaluate(transpose jacobian H, x0 | matrix{{t0}})
n = numRows H
v = submatrix'(((SVD J)#2)_{n}, {n}, )
epsilon = 0.1
x1 = x0 + epsilon * ii * v

toR = map(R, S, gens R | {t0+epsilon^2})


x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)
x1 = newton(polySystem toR H, x1)

trackSegment(H, t0 +epsilon^2, 1, {point(x1)})
solutions = oo