forked from windisch/FiberWalks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiberWalks.m2
734 lines (657 loc) · 19.2 KB
/
FiberWalks.m2
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
newPackage("FiberWalks",
Version => "0.2",
Date => "Februar 2016",
Authors => {
{Name => "Tobias Windisch",
Email => "windisch@ovgu.de",
HomePage => "http://www.uni-magdeburg.de/windisch/"}},
Headline => "Package for analysing fiber graphs",
Configuration => {},
PackageImports => {"Graphs","Polyhedra"},
Reload=>true
)
export {
--Datatypes
"Fiber",
"FiberGraph",
--fiber graphs
"fiber",
"fiberGraph",
"compressedFiberGraph",
"adaptedMoves",
"convertMoves",
"areIsomorphic",
"findConnectingPath",
"countEdgeDisjointPaths",
"characteristicPolynomial",
"hammingDistance",
"getHemmeckeMatrix",
"listDegSequences",
--properties
"conductance",
"fiberDimensionOne",
"isSetOfDirections",
--transistion matrices
"simpleFiberWalk",
"simpleWalk",
"metropolisHastingsWalk",
"heatBath",
"slem",
"mixingTime",
"approxFiberMixing",
--miscellaneous
"linearSpan",
"isMultiple",
"enumDimensionFiberDimOne",
"enumFiberDimOne",
--options
"Directed",
"ReturnSet",
"Distribution",
"Stationary",
"Size",
"TermOrder",
"Verbose"
}
--variable for polynomial ring
xx:=vars(23);
--TODOs:
--make final distribution an argument in metropolisHastingsWalk
--datatypes are not working
Fiber = new Type of List
FiberGraph = new Type of Graph
--TODO: Move to graphs package?
conductance = method()
conductance (Matrix) := QQ => (P) ->(
--computes conductance if stationary distribution is uniform
n:=numRows(P);
c:=n;
N:=toList(0..n-1);
M:={};
for i in 1..floor(n/2) do (
<< i << endl;
for S in subsets(n,i) do (
T:=1/i*sum flatten for s in S list for t in toList(set(N)-set(S)) list P_(s,t);
c=min(T,c);
if T==c then M=S;
);
);
return (c,M);
);
isSetOfDirections = method()
isSetOfDirections (List) := Boolean => (M) -> (
--checks whether a set of moves is a set of directions
for a in subsets(M,2) do(
if isMultiple(a_0,a_1) then return false
);
return true;
);
isMultiple = method()
isMultiple (ZZ,ZZ) := Boolean => (u,v) -> (isMultiple(matrix({{u}}),matrix({{v}})))
isMultiple (Matrix,Matrix) := Boolean => (u,v) -> (
--checks whether there is an integer k such that u=kv or v=ku
A:=u|v;
if dim(kernel A)==1 then (
s:=(syz(u|v))_0;
i:=abs(s_0);
j:=abs(s_1);
if gcd(i,j)==min(i,j) then return true;
);
return false;
);
fiberDimensionOne = method()
fiberDimensionOne (Graph) := Boolean => (G) -> (
n:=#(vertexSet(G));
F:=toList(1..n);
S:=toList(1..n-1);
for M in subsets S do(
div:=0;
for a in subsets(M,2) do(
i:=max(a);
j:=min(a);
if gcd(i,j)==j then div=1;
);
if div==0 then(
if areIsomorphic(G,fiberGraph(F,M)) then return true;
);
);
return false;
);
fiberDimensionOne (List) := Boolean => (degs) -> (
--n:=#(vertexSet(G));
n:=#degs;
F:=toList(1..n);
S:=toList(1..n-1);
for M in subsets S do (
if isSetOfDirections(M) then (
H:=fiberGraph(F,M);
if isConnected(H) then (
D:=sort for v in vertexSet(H) list degree(H,v);
if D==degs then (
print D;
);
);
);
);
return false;
);
listDegSequences = method()
listDegSequences (ZZ) := List => (n) -> (
S:=toList(1..n-1);
F:=toList(1..n);
D:={};
for M in subsets S do (
if isSetOfDirections(M) then (
H:=fiberGraph(F,M);
D=D|{sort for v in vertexSet(H) list degree(H,v)};
);
);
return unique D;
);
--TODO: Move this method to a suitable package
characteristicPolynomial = method()
characteristicPolynomial (Matrix) := RingElement => (A) -> (
if numRows(A)!=numColumns(A) then return false;
R:=QQ[vars(0)];
n:=numRows(A);
A=substitute(A,R);
I:=id_(R^n);
return det(R_0*I-A);
);
hammingDistance = method()
hammingDistance (Matrix,Matrix) := ZZ => (A,B) -> (
return sum for a in flatten entries(A-B) list if a!= 0 then 1 else 0;
);
adaptedMoves = method()
adaptedMoves (Matrix,ZZ) := List => (M,r) -> (adaptedMoves(convertMoves(M),r));
adaptedMoves (List,ZZ) := List => (M,r) -> (
if #M===0 then return false;
M=transpose matrix for m in M list flatten entries m;
d:=numColumns(M);
P:=latticePoints crossPolytope(d,r);
return unique for p in P list M*p;
);
fiber = method ()
fiber (Matrix,ZZ) := Fiber => (A,b) -> (fiber(A,toList{b}));
fiber (Matrix,List) := Fiber => (A,b) -> (fiber(A,vector b));
fiber (Matrix,Vector) := Fiber => (A,b) -> (fiber(A,matrix b));
fiber (Matrix,Matrix) := Fiber => (A,b) -> (
d:=numColumns A;
if numRows(A)!=numRows(b) or numColumns(b)>1 then return false;
--check whether fiber finite
--if (matrix(toList{d:{1}})**QQ) % image(transpose(A)**QQ)!=0 then (
-- << "Fiber not finite" << endl;
-- return false;
-- );
--identity matrix
I:=-map(ZZ^d);
o:=matrix toList(d:{0});
P:=intersection(I,o,A,b);
LP:=latticePoints P;
return LP;
);
enumDimensionFiberDimOne = method()
enumDimensionFiberDimOne ZZ := List => (n) -> (
L:= for G in enumFiberDimOne(n) list transpose matrix({for v in vertexSet(G) list degree(G,v)});
return L;
);
enumFiberDimOne = method()
enumFiberDimOne (ZZ) := List => (n) -> (
F:=toList(1..n);
D:=toList(1..(n-1));
GG:=for M in subsets(D) list if isSetOfDirections(M) then fiberGraph(F,M) else continue;
IG:={};
for G in GG do (
c:=sum for H in IG list if areIsomorphic(G,H) then 1 else 0;
if c==0 then IG=IG|{G};
);
return IG;
);
areIsomorphic = method()
areIsomorphic (Graph,Graph) := Boolean => (G,H) -> (
H=indexLabelGraph(reindexBy(H,"mindegree"));
G=indexLabelGraph(reindexBy(G,"mindegree"));
dH:=for v in vertexSet(H) list degree(H,v);
dG:=for v in vertexSet(G) list degree(G,v);
--easy checks
if dH!=dG then return false;
if not isConnected(G)===isConnected(H) then return false;
d:=dH;
PP:={};
for j in min(d)..max(d) do (
P:=for i in 0..(#d-1) list if d_i==j then i else continue;
if #P>0 then PP=PP|{P};
);
PP=apply(PP,permutations);
--create index set
I:=for P in PP list set(0..(#P-1));
if #I>1 then (
IS:=I_0;
for i in 1..(#I-1) do (
IS=(IS**I_i)/splice;
);
) else (
IS={I_0};
);
IS=toList(IS);
AG:=adjacencyMatrix(G);
AH:=adjacencyMatrix(H);
for jj in IS do (
P:=flatten for j in 0..(#PP-1) list (PP_j)_(jj_j);
if (AG_P)^P==AH then return true
);
return false;
);
fiberGraph = method (Options => {Directed => false,TermOrder=>Lex})
fiberGraph (Matrix,Matrix,Matrix) := FiberGraph => opts -> (A,b,M) -> (fiberGraph(A,b,convertMoves(M),opts));
fiberGraph (Matrix,Matrix,List) := FiberGraph => opts -> (A,b,M) -> (fiberGraph(fiber(A,b),M,opts));
fiberGraph (List,Matrix) := FiberGraph => opts -> (F,M) ->(fiberGraph(F,convertMoves(M),opts));
fiberGraph (List,List) := FiberGraph => opts -> (F,M) -> (
n:=#F;
ee:={};
if opts.Directed then (
--directed fiber graphs
if n==0 then return digraph({});
d:=numRows(F_0);
R:=QQ[for i in 0..(d-1) list xx_(i),MonomialOrder=>opts.TermOrder];
for e in subsets(F,2) do (
if member(e_0-e_1,M) or member (e_1-e_0,M) then (
v1:=flatten entries e_0;
v2:=flatten entries e_1;
m1:=product(for i in 0..(d-1) list ((R_i)^(v1_i))_R);
m2:=product(for i in 0..(d-1) list ((R_i)^(v2_i))_R);
if (m1)_R>(m2)_R then ee=ee|{{e_0,e_1}} else ee=ee|{{e_1,e_0}};
);
);
return digraph(ee);
) else (
--undirected fiber graphs
for e in subsets(F,2) do (
if member(e_0-e_1,M) or member (e_1-e_0,M) then (
ee=ee|{e};
);
);
return graph(F,ee);
);
);
compressedFiberGraph = method()
compressedFiberGraph (Matrix,Matrix,List) := FiberGraph => (A,b,M) -> (
G:=fiberGraph(A,b,M);
V:=vertexSet G;
for v in V do (
for m in M do (
l:=deepestDecent(v,-m);
u:=deepestDecent(v,m);
for i in 1..l do G=addEdge(G,set{v,v-i*m});
for i in 1..u do G=addEdge(G,set{v,v+i*m});
);
);
return G;
);
deepestDecent = method()
deepestDecent (Matrix,Matrix) := ZZ => (v,m) -> (
d:=numRows(v);
return floor min for j in 0..(d-1) list if m_(j,0)<0 then -v_(j,0)/m_(j,0) else continue;
);
getHemmeckeMatrix = method ()
getHemmeckeMatrix (ZZ) := Matrix => (k) -> (
if k==0 then return matrix({{0}});
I:=map(ZZ^k);
O:=reshape(ZZ^k,ZZ^k,matrix 0_(ZZ^(k*k)));
i:=matrix toList(k:{-1});
o:=matrix toList(k:{0});
ll:=matrix({toList((4*k):0)|{1,1}});
A:=(I|I|O|O|i|o)||(O|O|I|I|o|i)||ll;
return A;
);
convertMoves = method()
convertMoves (Matrix) := List => (M) -> (
return for m in entries M list matrix vector m;
);
--TODO: Move this method to Graphs (implement Dijkstra maybe)
findConnectingPath = method()
findConnectingPath (Graph,Thing,Thing) := List => (G,v,w) -> (
--return path (=list of vertices) where no vertex is used twice
FW:=floydWarshall(G);
--length of shortest path (no vertex is used twice)
d:=FW#(v,w);
if d == infinity then return {};
P:=findPaths(G,v,d);
for p in P do if last p === w then return p;
return false;
);
--TODO: Move this method to Graphs (implement dijkstra maybe)
countEdgeDisjointPaths = method()
countEdgeDisjointPaths (Graph,Thing,Thing) := ZZ => (G,v,w) -> (
P:=findConnectingPath(G,v,w);
n:=0;
while #P > 0 do (
n=n+1;
--remove edges from G
PE:=for i in 1..(#P-1) list {P_(i-1),P_(i)};
G=deleteEdges(G,PE);
P=findConnectingPath(G,v,w);
);
return n;
);
linearSpan = method ()
linearSpan (List) := Module => (L) -> (
return image transpose matrix apply(L,l->flatten entries l);
);
----------------------------------
---- TRANSITION MATRICES --------
----------------------------------
simpleFiberWalk = method ()
simpleFiberWalk (Matrix,Matrix,Matrix) := Matrix => (A,b,M) -> (simpleFiberWalk(A,b,convertMoves(M)));
simpleFiberWalk (Matrix,Matrix,List) := Matrix => (A,b,M) -> (
P:=mutableMatrix(adjacencyMatrix(fiberGraph(A,b,M))**QQ);
D:=#(set(M)+set(-M));
for i in 0..numRows(P)-1 do (
deg:=sum flatten entries P^{i};
P_(i,i)=(D-deg)/D;
for j in 0..numColumns(P)-1 do if j!=i then P_(i,j)=P_(i,j)*1/D;
);
return matrix P;
);
heatBath = method()
heatBath (Polyhedron,List) := Matrix => (P,M) -> (heatBath(latticePoints P,M))
heatBath (List,List) := Matrix => (F,M) -> (
-- F: sample space
-- M: Markov basis
--TODO: remove multiples from Markov basis
--TODO: allow density functions on M
n:=#F;
k:=#M;
P:=mutableMatrix(QQ,n,n);
for i in 0..(n-1) do (
for m in M do (
J:=for j in 0..(n-1) list if (F_i-F_j) % image(m) == 0 then j else continue;
for j in J do if i!=j then P_(i,j)=(1/k)*1/(#J) else continue;
--find vertices that are adjacent to i along m
);
);
--write rejection probabilities
for i in 0..(n-1) do P_(i,i)=1-sum(for j in 0..(n-1) list P_(i,j));
return matrix(P);
);
simpleWalk = method()
--simpleWalk (Matrix,Matrix,Matrix) := Matrix => (A,b,M) -> (simpleWalk(fiberGraph(A,b,M)));
simpleWalk (Graph) := Matrix => (G) -> (
n:=#(vertexSet G);
P:=mutableMatrix(adjacencyMatrix(G)**QQ);
for i in 0..n-1 do (
deg:=sum flatten entries P^{i};
for j in 0..n-1 do if j!=i then P_(i,j)=P_(i,j)*1/deg;
);
return matrix P;
);
metropolisHastingsWalk = method(Options => {Stationary =>
false,Distribution => false})
--metropolisHastingsWalk (Matrix,Matrix,Matrix) := Matrix => (A,b,M) ->(metropolisHastingsWalk(fiberGraph(A,b,M)));
metropolisHastingsWalk (Graph) := Matrix => opts -> (G) -> (
n:=#(vertexSet G);
A:=adjacencyMatrix(G);
p:=toList();
if opts.Stationary then (
if sum(p)!=1 then (
return false;
);
p=opts.Distribution;
);
P:=mutableMatrix(A**QQ);
for i in 0..(n-1) do (
for j in 0..(n-1) do (
if i==j then (
P_(i,i)=sum for k in 0..n-1 list A_(i,k)*max(0,(1/(sum flatten entries A_i))-(1/(sum flatten entries A_k)));
) else (
if A_(i,j)==1 then (
P_(i,j)=min(1/(sum flatten entries A_i),1/(sum flatten entries A_j));
);
);
);
);
return matrix P;
);
slem = method()
slem (Matrix) := RR => (P) -> (
return (rsort unique for v in eigenvalues P list abs v)_1;
);
approxFiberMixing = method()
approxFiberMixing (Matrix,Matrix,List) := ZZ => (A,u,M) -> (
--count the fiber
n:=#fiber(A,A*u);
--make moves symmetric
M=toList(set(M)+set(-M));
nM:=#M;
--observed tables
T:=new MutableHashTable;
--set counter
i:=1;
while true do (
--keep track of tables
if T#?u then T#u=T#u+1 else T#u=1;
--compute distance to uniform distribution
e:=1/2*(sum for v in values(T) list abs(v/i-1/n))+1/2*(n-#values(T))/n;
if e<1/4 then return i;
--get proposal
m:=M_(random(1,nM)-1);
if all(flatten entries(u+m),k -> k>=0) then (
u=u+m;
);
i=i+1;
);
);
mixingTime = method()
mixingTime (Matrix) := RR => (P) -> (return -1/log(slem(P)););
beginDocumentation()
document {
Key => FiberWalks,
Headline => "a package for random walks on fiber graphs",
EM "FiberWalks", " is a package for random walks on fiber
graphs",
BR{},BR{},
BOLD "Literature \n",
UL {
LI {"[DS1998] ", EM "Algebraic algorithms for sampling from
conditional distributions ", "(P. Diaconis, B. Sturmfels,
1998).\n"}}}
document {
Key => {fiber,
(fiber,Matrix,ZZ),(fiber,Matrix,List),(fiber, Matrix,
Vector),(fiber,Matrix,Matrix)},
Headline => "Fiber of a matrix",
Usage => "fiber(A,b)",
Inputs => {
"A" => { "a Matrix"},
"b" => { "an element in ZZ, a List, a Vector or a Matrix"}},
Outputs => {
{"a List containing all elements of the fiber of A for the right-hand side b"} },
EXAMPLE {
"A=matrix({{1,0,-2},{1,1,1}})",
"b=matrix({{2},{10}})",
"fiber(A,b)"
},
SeeAlso => fiberGraph}
document {
Key => {fiberGraph,
(fiberGraph,Matrix,Matrix,Matrix),(fiberGraph,Matrix,Matrix,List),(fiberGraph,List,Matrix),(fiberGraph,List,List)},
Headline => "Fiber graph of a matrix",
Usage => "fiberGraph(A,b,M)",
Inputs => {
"A" => { "a Matrix"},
"b" => { "a Matrix"},
"M" => { "a Matrix or a List"}},
Outputs => {
{"the (directed) fiber graph of A with right-hand side b and allowed
moves M"} },
EXAMPLE {
"needsPackage(\"FourTiTwo\")",
"A=matrix({{1,0,-2},{1,1,1}})",
"b=matrix({{2},{10}})",
"M=toricMarkov(A)",
"fiberGraph(A,b,M);",
"fiberGraph(A,b,M,Directed=>true,TermOrder=>Lex)",
"F=fiber(A,b)",
"fiberGraph(F,M);"
},
SeeAlso => fiber}
document {
Key => {convertMoves,
(convertMoves,Matrix)},
Headline => "Conversion of matrices containing Markov moves",
Usage => "convertMoves(M)",
Inputs => {
"M" => { "a Matrix"}},
Outputs => {
{"A List consisting of the rows of M written as matrices"}},
EXAMPLE {
"needsPackage(\"FourTiTwo\")",
"A=matrix({{1,1,1}})",
"M=toricMarkov(A)",
"convertMoves(M)"
}}
document {
Key => {simpleWalk,
(simpleWalk,Graph)},
Headline => "The simple walk",
Usage => "simpleWalk(G)",
Inputs => {
"G" => { "a Graph"}},
Outputs => {
{"the transition matrix of the simple walk on G"} },
EXAMPLE {
"needsPackage(\"Graphs\")",
"G=graph({{1,2},{2,3},{3,1}})",
"simpleWalk(G)"
},
SeeAlso => {simpleFiberWalk,metropolisHastingsWalk}}
document {
Key => {metropolisHastingsWalk,
(metropolisHastingsWalk,Graph)},
Headline => "The Metropolis-Hastings walk",
Usage => "metropolisHastingsWalk(G)",
Inputs => {
"G" => { "a Graph"}},
Outputs => {
{"the transition matrix of the Metropolis-Hastings walk on G
with uniform as stationary distribution"} },
EXAMPLE {
"needsPackage(\"Graphs\")",
"G=graph({{1,2},{2,3},{3,1},{3,4}})",
"metropolisHastingsWalk(G)"
},
SeeAlso => {simpleFiberWalk,simpleWalk}}
document {
Key => {simpleFiberWalk,
(simpleFiberWalk,Matrix,Matrix,Matrix),(simpleFiberWalk,Matrix,Matrix,List)},
Headline => "The simple walk",
Usage => "simpleFiberWalk(A,b,M)",
Inputs => {
"A" => { "a Matrix"},
"b" => { "a Matrix"},
"M" => { "a Matrix or a List"}},
Outputs => {
{"the transition matrix of the simple fiber walk on the
fiber graph of A with right-hand side b and allowed moves M"} },
EXAMPLE {
"A=matrix({{1,1,1,1}})",
"b=matrix({{2}})",
"M=toricMarkov(A)",
"simpleFiberWalk(A,b,M)"
},
SeeAlso => {simpleWalk,metropolisHastingsWalk}}
document {
Key => {slem,
(slem,Matrix)},
Headline => "Second largest eigenvalue modulus",
Usage => "slem(T)",
Inputs => {
"T" => { "a Matrix"}},
Outputs => {
{"the second largest eigenvalue modulus of the random walk
corresponding to the transition matrix T"} },
EXAMPLE {
"needsPackage(\"Graphs\")",
"G=graph({{1,2},{2,3},{3,1},{3,4}})",
"T=simpleWalk(G);",
"slem(T)"
}}
document {
Key => {approxFiberMixing,
(approxFiberMixing,Matrix,Matrix,List)},
Headline => "Mixing time of fiber walks",
Usage => "approxFiberMixing(A,u,M)",
Inputs => {
"A" => { "a Matrix"},
"u" => { "a Matrix"},
"M" => { "a List"}},
Outputs => {
{"the number of steps needed to get a sample from the fiber with
respect to a probability density function that has total variance
distance to the uniform distribution at most 1/4"} },
EXAMPLE {
"A=matrix({{1,1,1,1}})",
"b=matrix({{2}})",
"M=toricMarkov(A)",
"simpleFiberWalk(A,b,M)"
},
SeeAlso => {mixingTime}}
document {
Key => {mixingTime,
(mixingTime,Matrix)},
Headline => "Mixing time",
Usage => "mixingTime(T)",
Inputs => {
"T" => { "a Matrix"}},
Outputs => {
{"the mixing time of the random walk
corresponding to the transition matrix T"} },
EXAMPLE {
"needsPackage(\"Graphs\")",
"G=graph({{1,2},{2,3},{3,1},{3,4}})",
"T=simpleWalk(G);",
"mixingTime(T)"
}}
-- Tests --
TEST ///
--check creation of adapted moves
M=matrix({{1,-1,0},{1,0,-1}});
AM={
matrix({{-2},{2},{0}}),matrix({{-2},{1},{1}}),matrix({{-1},{1},{0}}),
matrix({{0},{1},{-1}}),matrix({{-2},{0},{2}}),matrix({{-1},{0},{1}}),
matrix({{0},{0},{0}}),matrix({{1},{0},{-1}}),matrix({{2},{0},{-2}}),
matrix({{0},{-1},{1}}),matrix({{1},{-1},{0}}),matrix({{2},{-1},{-1}}),
matrix({{2},{-2},{0}})}
assert(adaptedMoves(M,2)===AM);
///
TEST ///
--check fiber
A=matrix({{1,1,1}});
F={
matrix {{3},{0},{0}},matrix({{2},{0},{1}}),
matrix({{1},{0},{2}}),matrix({{0},{0},{3}}),
matrix({{2},{1},{0}}),matrix({{1},{1},{1}}),
matrix({{0},{1},{2}}),matrix({{1},{2},{0}}),
matrix({{0},{2},{1}}),matrix({{0},{3},{0}})}
L={};
L=L|{fiber(A,3)};
L=L|{fiber(A,{3})};
L=L|{fiber(A,matrix({{3}}))};
L=L|{fiber(A,vector({3}))};
assert(all(L,S->S==F));
///
TEST ///
--check conversion of moves
M=matrix({{1,-1,0},{1,0,-1}});
MM={matrix({{1},{-1},{0}}),matrix({{1},{0},{-1}})};
assert(convertMoves(M)===MM);
///
TEST ///
G=graph({{1,2},{2,3},{1,3},{3,4},{3,5},{4,6},{5,6}});
assert(countEdgeDisjointPaths(G,1,6)===2);
///
TEST ///
assert(areIsomorphic(completeGraph(3),cycleGraph(3))===true);
assert(areIsomorphic(completeGraph(4),cycleGraph(3))===false);
///
end