-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcommon.m2
More file actions
472 lines (414 loc) · 14.5 KB
/
common.m2
File metadata and controls
472 lines (414 loc) · 14.5 KB
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
-- IMPORTS
debug needsPackage "NumericalAlgebraicGeometry"
debug needsPackage "MonodromySolver"
debug SLPexpressions
debug needsPackage "Core"
-- FUNCTIONS
size GateMatrix := M -> (numrows M, numcols M)
size Matrix := M -> (numrows M, numcols M)
-*
-- evaluate a gateMatrix G at a matrix x
-- don't use this a lot...
evaluate (GateMatrix, Matrix) := (G, x) -> (
M := mutableMatrix(FF,numrows G,numcols G);
E' := makeEvaluator(G,matrix{cameraVars|dataParams});
evaluate(E',mutableMatrix(x),M);
matrix M
)
*-
--random diagonal matrix
randDiag = n -> diagonalMatrix for i from 1 to n list random FF
dehomogenize = method(Options=>{})
dehomogenize (Matrix, ZZ) := o -> (v, n) -> (
--assumes column vector
(1/v_(n, 0))*v^(toList splice(0..n-1,n+1..numrows v-1))
)
dehomogenize Matrix := o -> v -> dehomogenize(v, numrows v -1)
summary = L -> (
n := #L;
H := sort L;
Q1 := (1/2) * (H#(floor((n-1)/3))+H#(ceiling((n-1)/3)));
med := (1/2) * (H#(floor((n-1)/2))+H#(ceiling((n-1)/2)));
Q3 := (1/2) * (H#(floor(2*(n-1)/3))+H#(ceiling(2*(n-1)/3)));
mean := (sum L)/n;
var := sum(L/(x-> (x - mean)^2))/(n-1);
<< "Min: " << toString(min L) << endl;
<< "1Q: " << toString(Q1) << endl;
<< "Med: " << toString(med) << endl;
<< "Avg: " << toString(sub(mean,RR)) << endl;
<< "3Q: " << toString(Q3) << endl;
<< "Max: " << toString(max L) << endl;
<< "Std Dev: " << toString(sqrt(var)) << endl;
)
-- random element in the kernel of M
randKernel = method(Options=>{Tolerance=>1e-4})
randKernel (Matrix, InexactFieldFamily) := o -> (M, FF) -> (
K := numericalKernel(M, Tolerance=>o.Tolerance);
K*random(FF^(numcols K), FF^1)
)
randKernel Matrix := o -> M -> randKernel(M, FF, oo)
reshapeCol = p -> if (numrows p == 1) then transpose p else p
reshapeRow = p -> if (numcols p == 1) then transpose p else p
-- RANDOMIZATION FOR PARAMETER POINT p
-- assumes random CC has unit modulus!
gammify = method()
gammify Point := p -> gammify reshapeCol matrix p
gammify Matrix := p -> (
gammas := for i from 1 to m*#indepLines list random FF;
indDiag := flatten(gammas/(g->{g,g,g}));
-- abstract to arbitrary diagram, number of cameras
depWInd := depLines/(l->first select(1,last D,i->member(l,i)));
mfoldIntersections := flatten(depWInd/(l->for i from 0 to m-1 list l/(x->m*x+i)));
-- next line assumes indepenedent lines come first!
depDiag := flatten(
mfoldIntersections/(ind -> {
conjugate gammas#(ind#0),
conjugate gammas#(ind#1)
}
)
);
tChartdiag := toList((3*(m-1)+1):random(FF)); -- t chart gamma
qChartDiags := flatten for i from 0 to m-2 list toList(5:random(FF));
p' := diagonalMatrix(indDiag|depDiag|tChartdiag|qChartDiags)*p;
p'
)
-- fold along rows
rfold = L -> if (#L ==0) then random(FF^0,FF^0) else fold(L, (a,b) -> a||b)
-- fold along cols
cfold = L -> fold(L, (a,b) -> a|b)
-- write starting parameters and solutions to file
writeStartSys = method(Options=>{Filename=>"startSys"})
writeStartSys (Matrix, List) := o -> (M, sols) -> writeStartSys(point M, sols, o)
writeStartSys (Point, List) := o -> (p, sols) -> (
assert(instance(o.Filename,String));
f := openOut o.Filename;
f << "Parameter values: " << endl;
f << toExternalString p << endl;
f << "Solutions : " << endl;
for s in sols do f << toExternalString s << endl;
close f;
)
readStartSys = filename -> (
l := separate("\n", get filename);
p0 := value l#1;
sols := for i from 3 to #l-2 list value l#i;
(transpose matrix p0, sols/matrix/transpose)
)
-- for testing the contents of a start system file
startSysTester = (p,sols) -> (
p0 := (transpose matrix V.BasePoint);
p1 := random(FF^(#dataParams),FF^1);
P01 = p0||p1;
Pspec01 := specialize(PH,P0);
target01 := trackHomotopy(Pspec01, sols);
Pspec10 := (gammify p1)|(gammify p0);
trackHomotopy(Pspec10, target01)
)
adjugate = method()
adjugate Thing := M -> (
n := numcols M;
assert(n == numrows M);
matrix table(n,n,(i,j)->((-1)^(i+j))*det submatrix'(M,{j},{i}))
)
-- not printing to high precision -- deprecated?
sol2String = p -> replace("\\{|\\}","",toString p.Coordinates)
-- produces gates for "small" determinants"
det2 = M -> M_(0,0)*M_(1,1)-M_(1,0)*M_(0,1)
det3 = M -> M_(0,0)*det2(M_{1,2}^{1,2})-M_(0,1)*det2(M_{0,2}^{1,2})+M_(0,2)*det2(M_{0,1}^{1,2})
det4 = M -> M_(0,0)*det3(M_{1,2,3}^{1,2,3})-M_(0,1)*det3(M_{0,2,3}^{1,2,3})+M_(0,2)*det3(M_{0,1,3}^{1,2,3})-M_(0,3)*det3(M_{0,1,2}^{1,2,3})
laplaceDet = M -> (
(m, n) := size M;
if (m=!=n) then error("not square matrix")
else if (m>5) then error("no Laplace for matrices larger than 4x4")
else if (m==2) then det2 M
else if (m==3) then det3 M
else -* m==4 *- det4 M
)
-- jacobian of GateMatrix wrt. a list of inputGates
jacobian (GateMatrix, List) := (F,inGates) -> fold(apply(inGates,g->diff(g,F)),(a,b)->a|b)
-- get rotation matrix from cayley parameters
cay2R = method(Options=>{Normalized=>false})
cay2R (Thing,Thing,Thing) := o -> (X,Y,Z) -> (
if instance(X, RR) then x := X_FF else x = X;
if instance(Y, RR) then y := Y_FF else y = Y;
if instance(Z, RR) then z := Z_FF else z = Z;
M := matrix{
{1+x*x-(y*y+z*z), 2*(x*y-z), 2*(x*z+y)},
{2*(x*y+z), 1+y^2-(x*x+z*z), 2*(y*z-x)},
{2*(x*z-y), 2*(y*z+x), 1 +z*z -(x*x+y*y)}
};
if o.Normalized then (1/(1+x^2+y^2+z^2)) * M else M
)
cay2R List := o -> L -> cay2R(L#0, L#1, L#2, o)
-- get Cayley parameters from rotation matrix
R2Cay = method(Options=>{UnNormalize=>false})
R2Cay Matrix := o -> R -> (
assert(numcols R == 3);
assert(numrows R == 3);
S := (R-id_(FF^3))*(R+id_(FF^3))^-1;
(S_(2,1), S_(0,2), S_(1,0))
)
-*/// TEST
restart
needs "common.m2"
(x, y, z) = (random RR, random RR, random RR)
R = cay2R(x, y, z)
(x',y',z') = R2Cay R
R = cay2R(x', y', z')
R2Cay R
///*-
-- get rotation matrix from quaternion parameters
Q2R = method(Options=>{Normalized=>false, FF=>FF})
Q2R (Thing,Thing,Thing, Thing) := o -> (W, X,Y,Z) -> (
if instance(W, RR) then w := W_FF else w = W;
if instance(X, RR) then x := X_FF else x = X;
if instance(Y, RR) then y := Y_FF else y = Y;
if instance(Z, RR) then z := Z_FF else z = Z;
M := matrix{
{w*w+x*x-(y*y+z*z), 2*(x*y-w*z), 2*(x*z+w*y)},
{2*(x*y+w*z), w^2+y^2-(x*x+z*z), 2*(y*z-w*x)},
{2*(x*z-w*y), 2*(y*z+w*x), w^2 +z*z -(x*x+y*y)}
};
if o.Normalized then (1/(w^2+x^2+y^2+z^2)) * M else M
)
Q2R List := o -> L -> Q2R(L#0, L#1, L#2, L#3, o)
-- get Cayley parameters from rotation matrix
R2Q = method(Options=>{UnNormalize=>false,FF=>FF})
R2Q Matrix := o -> R -> (
assert(numcols R == 3);
assert(numrows R == 3);
c := (R_(2,1) - R_(1,2));
b := (R_(0,2) - R_(2,0));
a := (R_(1,0) - R_(0,1));
w := (1/2)*sqrt(R_(0,0)+R_(1,1)+R_(2,2)+1);
x := 1/(4*w) * c;
y := 1/(4*w) * b;
z := 1/(4*w) * a;
-- << w^2+x^2+y^2+z^2 << endl;
(w, x, y, z)
)
-*/// TEST
R=FF[W]
netList solveSystem {W^4-W^2+1/16}
clean T
T=QQ[a..d]
R=Q2R gens T
S = (R-id_(((QQ)^3)))*adjugate(R+id_((QQ)^3));
S
((first x)/(first L))*L
1/sqrt(sum(x/(y->y^2)))*x
L
///*-
-- cross product of col vectors -- takes Matrice or GateMatrix pair
crossProduct = (y,q) -> matrix{{y_(1,0)*q_(2,0)-y_(2,0)*q_(1,0)},{y_(2,0)*q_(0,0)-y_(0,0)*q_(2,0)},{y_(0,0)*q_(1,0)-y_(1,0)*q_(0,0)}}
cmat = method(Options=>{Normalized=>true})
cmat List := o -> L -> cmat(L#0,L#1,L#2,o)
cmat (Thing,Thing,Thing) := o -> (a,b,c) -> (
tx := matrix{{0,-c,b},{c,0,-a},{-b,a,0}};
if o.Normalized and not areEqual(L#2,0.0) then tx = (-1/L#2) * tx;
tx
)
--
randomLineThroughPoints = (P, FF) -> (
m := numrows P; -- m = dim + 1
n := numcols P; -- n = number of points
assert(m>=3 and m<=4);
K := numericalKernel(transpose P,Tolerance=>1e-6);
--assert(numcols K == m-n); -- true if points are distinct
transpose(K * random(FF^(numcols K),FF^(m-2)))
)
-- constructs problem data given a PL diagram D (complete visibility)
-- returns (camera parameters, lines, camera matrices
-- ASSUMES: "intersections" are sorted
fabricatePair = (D, FF, nvars) -> (
(nLines,nGhosts,intersections) := D;
depPoints := set {};
scan(nLines,l->(
ptsOnl := positions(last D,i->member(l,i));
depPoints = depPoints + set drop(ptsOnl,2);
)
);
pointsOnLineIndices := apply(nLines+nGhosts, l->positions(intersections,i->member(l,i)));
worldPointsFF := random(FF^4,FF^0);
scan(#intersections,i->(
pointi := if member(i,depPoints) then (
li := first select(1,pointsOnLineIndices,l->member(i,l));
<< li << endl;
a := random FF;
a*worldPointsFF_{li#0}+(1-a)*worldPointsFF_{li#1}
) else random(FF^4,FF^1);
worldPointsFF = worldPointsFF | pointi
)
);
worldPoints := sub(worldPointsFF,FF);
helperPoints := apply(pointsOnLineIndices, pp->sub(random(FF^4,FF^(2-min(2,#pp))),FF));
-- future (line below): may be interesting to sample space of variables differently depending on the field we fabricate data over
sampleCameraParameters := for i from 1 to nvars list sub(random FF,FF);
subTable := apply(sampleCameraParameters, cameraVars, (a,b) -> b=>inputGate a);
sampleC := apply(C,cam -> (
M := mutableMatrix(FF, 3, 4);
evaluate(cam, mutableMatrix{sampleCameraParameters}, M);
matrix M
)
);
(
sampleCameraParameters,
apply(sampleC, cam->(
P := cam * worldPoints;
L := matrix apply(nLines+nGhosts, l->(
Hl := helperPoints#l;
Pl := P_(pointsOnLineIndices#l);
Pl = Pl|cam*Hl;
line := randomLineThroughPoints(Pl, FF);
{(1/norm(2,line))*line} -- normalization that seemed to benefit the chicago solver
));
(P,L)
)),
sampleC
)
)
-- functions which fabricate input of the form (parameter, solution)
-- notation for supp materials is (y,c)---previous notation is (p,x)
encodey = (P,L,projs,FF) -> (
c := transpose matrix{P};
allLines := L/last;
yIndLineMatrix := cfold(
allLines/(m->m^(toList indepLines))
);
yInd := matrix(yIndLineMatrix,product toList size yIndLineMatrix,1);
yDep := if (#depLines == 0) then random(FF^0,FF^1) else
rfold(
depLines/(l-> (
lineInds := first select(1,last D,i->member(l,i));
triplet := take(lineInds, 2) | {l};
rfold(allLines/(m -> (
n :=numericalKernel(transpose m^triplet, Tolerance=>kTol);
(1/n_(2,0))*n^{0,1}
))
)
)
)
);
ytChart := sub(randKernel(transpose(c^{4*(m-1)..(4*(m-1)+3*(m-1)-1)}||matrix{{1_FF}}), FF),FF);
yqChart := sub(rfold(
for i from 0 to m-2 list randKernel(transpose(c^{4*i..4*i+3}||matrix{{1_FF}}), FF)
),FF);
yInd||yDep||ytChart||yqChart
)
encodeyc = (P, L, projs,FF) -> (
c := transpose matrix{P};
y := encodey(P, L, projs,FF);
(point y, point c)
)
fabricateyc = FF -> (
(P, L, projs) := fabricatePair(D, FF, nvars); -- these variable names are confusing
encodeyc(P, L, projs,FF)
)
-- convenience functions for minors
minors (GateMatrix, ZZ, Sequence, Boolean) := o -> (M, k, S, laplace) -> (
(Sm, Sn) := (first S, last S);
(m,n) := (numrows M, numcols M);
assert(k<=min(m,n));
assert(all(Sm,s->#s==k));
assert(all(Sn,s->#s==k));
flatten apply(Sm,sm->apply(Sn, sn ->
if (laplace) then laplaceDet submatrix(M,sm,sn)
else det submatrix(M,sm,sn)
))
)
allMinors = method(Options=>{Laplace=>false})
allMinors (GateMatrix, ZZ) := o -> (M, k) -> (
(m, n ) := (numrows M, numcols M);
s := (subsets(0..m-1,k),subsets(0..n-1,k));
minors(M, k, s, o.Laplace)
)
maxMinors = method(Options=>{Laplace=>false})
maxMinors GateMatrix := o -> M -> allMinors(M,min(numrows M, numcols M), Laplace=>o.Laplace)
-- this seems to work
complexQR = M -> (
A := mutableMatrix M;
k := ring A;
Q := mutableMatrix(k,0,0,Dense=>true);
R := mutableMatrix(k,0,0,Dense=>true);
rawQR(raw A, raw Q, raw R, true);
assert(areEqual(Q*R,A)); -- idk if it will work every time!
(matrix Q,matrix R)
)
leverageScores = M -> (
Q = first complexQR M;
rsort apply(numrows Q,i->(norm(2,Q^{i}),i))
)
leverageScoreRowSelector = J0 -> (
sortedRows := (leverageScores J0)/last;
r := rowSelector J0^(sortedRows);
sort(r/(i->sortedRows#i))
)
log10 = x -> log(x)/log(10)
argFF = z -> atan((imaginaryPart z)/(realPart z))
-- complex number whose real and imag parts are standard normal
gaussFF = () -> (
(u1,u2):=(random RR,random RR);
sqrt(-2*log(u1))*cos(2*pi*u2)+ii*sqrt(-2*log(u1))*sin(2*pi*u2)
)
-- random sample drawn from normal distriution N(mu, var^2)
rNorm = (mu,var) -> mu+var*(realPart gaussFF())_FF
-- random sample from (n-1)-sphere with radius r
sphere = (n,r) -> (
l:=apply(n,i->rNorm(0,1));
matrix{r/norm(2,l)*l}
)
-- assumes "u" of unit length
householder=method()
householder (Matrix,ZZ) := (u,n) -> (
if (numrows u > 1) then error("householder takes a row vector");
R:=ring u;
k:=numcols u;
id_(R^(n-k))++(id_(R^k)-2*(transpose u)*u)
)
householder (List,ZZ) := (u,n) -> householder(matrix {u},n)
randomOn = n -> diagonalMatrix(toList((n-1):1_RR)|{(-1)^(random 2)}) * fold(reverse apply(2..n,i->householder(sphere(i,1),n)),(a,b)->a*b)
randomCameraNormalized = () -> (
R := randomOn 3;
t := matrix{{random FF},{random FF},{random FF}};
-- t := transpose matrix{sphere(3,1)};
tnorm := (1 / t_(2,0))* t;
(R|tnorm)
)
randomCameraNormalizedCayley = () -> (
R := cay2R(random FF, random FF, random FF,Normalized=>true);
t := matrix{{random FF},{random FF},{random FF}};
-- t := transpose matrix{sphere(3,1)};
tnorm := (1 / t_(2,0))* t;
(R|tnorm)
)
randomCamera = () -> (
R := randomOn 3;
t := transpose matrix{sphere(3,1)};
(R|t)
)
ranks = method(Options=>{})
ranks (Matrix, Matrix) := o -> (x, p) -> (
if (numcols x > 1) then x = matrix(x, numcols x,1);
if (numcols p > 1) then p = matrix(p, numcols p,1);
xpp := mutableMatrix sub(x||p,CC);
a := PE/( m -> (
evaluate(first m, xpp, last m);
numericalRank matrix last m
)
);
b := LE/( m -> (
evaluate(first m, xpp, last m);
numericalRank matrix last m
)
);
(a, b)
)
ranks (Point,Point) := o -> (x,p) -> ranks(matrix x, matrix p)
rankCheck = method(Options=>{Hard=>true})
rankCheck (Matrix, Matrix) := o -> (x, p) -> (
(a, b) := ranks(x, p);
if (o.Hard) then (all(a,x->x==3) and all(b,x->x==2))
else (all(a,x->x<=3) and all(b,x->x<=2))
)
cpMatrix = t -> matrix{{0,-t_(2,0),t_(1,0)},{t_(2,0),0,-t_(0,0)},{-t_(1,0),t_(0,0),0}}
essential = (R,t) -> R * cpMatrix t