-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassoc_scheme_modified.py
More file actions
2601 lines (2437 loc) · 100 KB
/
assoc_scheme_modified.py
File metadata and controls
2601 lines (2437 loc) · 100 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
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
# -*- coding: utf-8 -*-
from copy import copy
from warnings import warn
from sage.all import pi
from sage.calculus.functional import expand as _expand
from sage.calculus.functional import simplify as _simplify
from sage.combinat.set_partition import SetPartitions
from sage.functions.orthogonal_polys import gegenbauer
from sage.functions.other import floor
from sage.functions.trig import cos
from sage.matrix.constructor import Matrix
from sage.matrix.constructor import diagonal_matrix
from sage.matrix.constructor import identity_matrix
from sage.misc.latex import latex
from sage.misc.latex import LatexExpr
from sage.misc.misc import subsets
from sage.rings.integer import Integer
from sage.structure.sage_object import SageObject
from sage.symbolic.relation import solve as _solve
from sage.symbolic.ring import SR
from sage.typeset.ascii_art import ascii_art
from sage.typeset.symbols import ascii_left_curly_brace
from sage.typeset.symbols import ascii_right_curly_brace
from sage.typeset.symbols import unicode_left_curly_brace
from sage.typeset.symbols import unicode_right_curly_brace
from sage.typeset.unicode_art import unicode_art
from .array3d import Array3D
from .array3d import Array4D
from .aux import InfeasibleError
from .aux import Parameters
from .coefflist import CoefficientList
from .find import find
from .nonex import checkConditions
from .nonex import families
from .nonex import sporadic
from .view import Param
from .util import checklist
from .util import checkNonneg
from .util import checkPos
from .util import _factor
from .util import full_simplify
from .util import integralize
from .util import is_divisible
from .util import is_integral
from .util import make_expressions
from .util import nrows
from .util import refresh
from .util import rewriteExp
from .util import rewriteMatrix
from .util import rewriteTuple
from .util import sort_solution
from .util import subs
from .util import symbol
from .util import variables
TPERMS = [[0, 1, 2], [0, 2, 1], [1, 0, 2],
[1, 2, 0], [2, 0, 1], [2, 1, 0]]
DPERMS = [[0, 1, 2], [1, 0, 2], [0, 2, 1],
[2, 0, 1], [1, 2, 0], [2, 1, 0]]
QHPERMS = [[0, 1, 2, 3, 4, 5], [0, 2, 1, 4, 3, 5], [1, 0, 2, 3, 5, 4],
[1, 2, 0, 5, 3, 4], [2, 0, 1, 4, 5, 3], [2, 1, 0, 5, 4, 3],
[0, 3, 4, 1, 2, 5], [0, 4, 3, 2, 1, 5], [3, 0, 4, 1, 5, 2],
[3, 4, 0, 5, 1, 2], [4, 0, 3, 2, 5, 1], [4, 3, 0, 5, 2, 1],
[1, 3, 5, 0, 2, 4], [1, 5, 3, 2, 0, 4], [3, 1, 5, 0, 4, 2],
[3, 5, 1, 4, 0, 2], [5, 1, 3, 2, 4, 0], [5, 3, 1, 4, 2, 0],
[2, 4, 5, 0, 1, 3], [2, 5, 4, 1, 0, 3], [4, 2, 5, 0, 3, 1],
[4, 5, 2, 3, 0, 1], [5, 2, 4, 1, 3, 0], [5, 4, 2, 3, 1, 0]]
QDPERMS = [[0, 1, 2, 3], [0, 1, 3, 2], [0, 2, 1, 3],
[0, 2, 3, 1], [0, 3, 1, 2], [0, 3, 2, 1],
[1, 0, 2, 3], [1, 0, 3, 2], [1, 2, 0, 3],
[1, 2, 3, 0], [1, 3, 0, 2], [1, 3, 2, 0],
[2, 0, 1, 3], [2, 0, 3, 1], [2, 1, 0, 3],
[2, 1, 3, 0], [2, 3, 0, 1], [2, 3, 1, 0],
[3, 0, 1, 2], [3, 0, 2, 1], [3, 1, 0, 2],
[3, 1, 2, 0], [3, 2, 0, 1], [3, 2, 1, 0]]
DUAL_PARAMETER = "Krein parameter"
DUAL_PARTS = "eigenspaces"
DUAL_SYMBOL = "q"
OBJECT = "association scheme"
PARAMETER = "intersection number"
PARTS = "relations"
SYMBOL = "p"
check_ASParameters = []
check = checklist(check_ASParameters)
class ASParameters(SageObject):
"""
A class for parameters of a general association scheme
and checking their feasibility.
"""
_ = None
_checklist = check_ASParameters
METRIC = False
def __init__(self, p=None, q=None, P=None, Q=None, complement=None):
"""
Object constructor.
"""
self._init_storage()
if self._get_class() is ASParameters:
self._init_prefix()
self._.bipartite = False
assert (p, q, P, Q).count(None) >= 3, \
"precisely one of p, q, P, Q must be given"
if isinstance(p, ASParameters):
p._copy(self)
elif p is not None:
self._.p = self._init_parameters(p, integral=True,
name=PARAMETER, sym=SYMBOL)
self._compute_kTable()
self._check_consistency(self._.p, self._.k,
name=PARAMETER, sym=SYMBOL)
#self.check_handshake()
elif q is not None:
self._.q = self._init_parameters(q, integral=False,
name=DUAL_PARAMETER,
sym=DUAL_SYMBOL)
self._compute_multiplicities()
self._check_consistency(self._.q, self._.m,
name=DUAL_PARAMETER, sym=DUAL_SYMBOL)
elif P is not None:
self._.P = self._init_eigenmatrix(P)
elif Q is not None:
self._.Q = self._init_eigenmatrix(Q)
else:
assert self._.d is not None, "insufficient data"
self._.subconstituents = [None] * (self._.d + 1)
self._compute_complement(complement)
self._init_vars()
def __hash__(self):
"""
Return the hash value.
"""
return hash(id(self))
def __len__(self, expand=False, factor=False, simplify=False):
"""
Return the number of vertices.
"""
self._.n = rewriteExp(self._.n, expand=expand, factor=factor,
simplify=simplify)
return self._.n
def __repr__(self):
"""
String representation.
"""
return "Parameters of an association scheme on %s vertices " \
"with %d classes" % (self._.n, self._.d)
def _check_consistency(self, p, k, name=None, sym=None):
"""
Check for the consistency of the intersection numbers
or Krein parameters.
"""
"""
r = range(self._.d + 1)
for h in r:
assert p[0, h, h] == k[h], \
"mismatch of %s %s[0, %d, %d]" % (name, sym, h, h)
for i in r:
s = 0
for j in r:
assert p[h, i, j] == p[h, j, i], \
"non-symmetric %s %s[%d, %d, %d]" % \
(name, sym, h, i, j)
assert p[h, i, j] * k[h] == p[j, h, i] * k[j], \
"double counting mismatch for %s[%d, %d, %d]" % \
(sym, h, i, j)
s += p[h, i, j]
for u in r:
assert sum(p[v, i, j] * p[u, v, h] for v in r) == \
sum(p[u, i, v] * p[v, j, h] for v in r), \
"double counting mismatch for " \
"sum_l %s[l, %d, %d] %s[%d, l, %d]" % \
(sym, i, j, sym, u, h)
assert s == k[i], \
"mismatch of sum_j %s[%d, %d, j]" % (sym, h, i)
"""
if not self._has("n"):
self._.n = 6#sum(k)
def _check_eigenmatrices(self):
"""
Check whether the eigenmatrices
multiply into a multiple of the identity matrix.
"""
if self._has("P") and self._has("Q") and \
_simplify(_expand(self._.P * self._.Q)) \
!= self.order(expand=True, simplify=True) \
* identity_matrix(SR, self._.d + 1):
warn(Warning("the eigenmatrices do not multiply "
"into a multiple of the identity matrix"))
def _check_family(self):
"""
Check whether the association scheme has parameters for which
nonexistence has been shown as a part of an infinite family.
Currently does nothing for general association schemes.
"""
return
@staticmethod
def _check_parameter(h, i, j, v, integral=False, name=None, sym=None):
"""
Check for the feasibility
of an intersection number or Krein parameter.
The parameter is checked for nonnegativity,
and, if requested, also for integrality.
"""
if integral:
try:
v = integralize(v)
except TypeError:
raise InfeasibleError("%s %s[%d, %d, %d] is nonintegral"
% (name, sym, h, i, j))
assert checkNonneg(v), \
"%s %s[%d, %d, %d] is negative" % (name, sym, h, i, j)
return v
def _check_parameters(self, p, integral=False, name=None, sym=None):
"""
Check for the feasibility
of all intersection numbers or Krein parameters.
The parameters are checked for nonnegativity,
and, if requested, also for integrality.
"""
for h in range(self._.d + 1):
for i in range(self._.d + 1):
for j in range(self._.d + 1):
p[h, i, j] = ASParameters. \
_check_parameter(h, i, j, p[h, i, j],
integral=integral,
name=name, sym=sym)
def _check_zero(self, h, i, j, u, v, w):
"""
Check whether a triple intersection number is not forced to be zero
by the intersection numbers.
"""
return self._.p[u, h, i] != 0 and self._.p[v, h, j] != 0 and \
self._.p[w, i, j] != 0
def _complement(self):
"""
Return the parameters of the complement of a strongly regular graph.
"""
assert self._.d == 2, "the complement is only defined for two classes"
kargs = {"complement": self}
if self._has("p"):
kargs["p"] = self._.p.reorder([0, 2, 1], inplace=False)
elif self._has("q"):
kargs["q"] = self._.q.reorder([0, 2, 1], inplace=False)
elif self._has("P"):
kargs["P"] = self._.P[[0, 2, 1], [0, 2, 1]]
elif self._has("Q"):
kargs["Q"] = self._.Q[[0, 2, 1], [0, 2, 1]]
return ASParameters(**kargs)
def _compute_complement(self, complement):
"""
For a scheme with two classes,
determine its complement if not given.
"""
if self._.d == 2 and complement is not False:
if complement is None:
complement = self._complement()
self._.complement = self.add_subscheme(complement, "complement")
def _compute_dualEigenmatrix(self, expand=False, factor=False,
simplify=False):
"""
Compute the dual eigenmatrix of the association scheme.
"""
if self._has("Q"):
return
if self._has("q"):
self._.Q = self._compute_eigenmatrix(self._.q, expand=expand,
factor=factor,
simplify=simplify)
else:
if not self._has("P"):
self.eigenmatrix(expand=expand, factor=factor,
simplify=simplify)
self._.Q = self._.n * self._.P.inverse()
self._check_eigenmatrices()
def _compute_eigenmatrix(self, p, expand=False, factor=False,
simplify=False):
"""
Compute and return an eigenmatrix of the association scheme.
"""
B = [Matrix(SR, [M[i] for M in p]) for i in range(self._.d + 1)]
V = SR**(self._.d + 1)
R = [[self._.d + 1, V, [Integer(1)]]]
for i in range(1, self._.d + 1):
S = sorted(([k, m, V.subspace_with_basis(b)]
for k, b, m in B[i].eigenvectors_right()),
key=lambda kvb: CoefficientList(kvb[0], self._.vars),
reverse=True)
j = 0
while j < len(R):
m, s, r = R[j]
h = 0
while h < len(S):
k, v, b = S[h]
sb = s.intersection(b)
d = sb.dimension()
if d == v:
del S[h]
else:
S[h][1] -= d
h += 1
if d == m:
R[j][1] = sb
r.append(k)
break
elif d > 0:
R.insert(j, [d, sb, r + [k]])
j += 1
m -= d
R[j][0] = m
j += 1
assert len(R) == self._.d + 1 and all(len(r) == self._.d + 1
for _, _, r in R), \
"failed to compute the eigenmatrix"
return Matrix(SR, [r for _, _, r in R])
def _compute_kreinParameters(self, expand=False, factor=False,
simplify=False):
"""
Compute the Krein parameters.
"""
if self._has("q"):
return
if not self._has("m"):
self.multiplicities(expand=expand, factor=factor,
simplify=simplify)
if not self._has("k"):
self.kTable(expand=expand, factor=factor,
simplify=simplify)
q = Array3D(self._.d + 1)
self._compute_parameters(q, self._.Q, self._.k, integral=False,
name=DUAL_PARAMETER, sym=DUAL_SYMBOL)
self._.q = q
def _compute_kTable(self, expand=False, factor=False, simplify=False):
"""
Compute the valencies of the relations.
"""
if self._has("k"):
return
if self._has("p"):
k = tuple(self._.p[0, i, i] for i in range(self._.d + 1))
else:
if not self._has("P"):
self.eigenmatrix(expand=expand, factor=factor,
simplify=simplify)
k = tuple(integralize(x) for x in self._.P[0])
assert k[0] == 1, \
"the valency of the first relation is not 1"
self._.k = k
def _compute_multiplicities(self, expand=False, factor=False,
simplify=False):
"""
Compute the multiplicities of the eigenspaces.
"""
if self._has("m"):
return
if self._has("q"):
m = tuple(integralize(self._.q[0, i, i])
for i in range(self._.d + 1))
else:
if not self._has("Q"):
self.dualEigenmatrix(expand=expand, factor=factor,
simplify=simplify)
m = tuple(integralize(x) for x in self._.Q[0])
assert m[0] == 1, "the multiplicity of the first eigenspace is not 1"
self._.m = m
def _compute_parameters(self, p, P, m, integral=False, name=None,
sym=None):
"""
Compute the intersection numbers or Krein parameters
from the eigenmatrices.
"""
for h in range(self._.d + 1):
for i in range(self._.d + 1):
for j in range(self._.d + 1):
p[h, i, j] = full_simplify(
sum(m[t] * P[t, h] * P[t, i] * P[t, j]
for t in range(self._.d + 1))
/ (self._.n * P[0, h]))
self._check_parameter(h, i, j, p[h, i, j],
integral=integral,
name=name, sym=sym)
self._check_consistency(p, P[0], name=name, sym=sym)
def _compute_primalEigenmatrix(self, expand=False, factor=False,
simplify=False):
"""
Compute the primal eigenmatrix of the association scheme.
"""
if self._has("P"):
return
if self._has("p"):
self._.P = self._compute_eigenmatrix(self._.p, expand=expand,
factor=factor,
simplify=simplify)
else:
if not self._has("Q"):
self.dualEigenmatrix(expand=expand, factor=factor,
simplify=simplify)
self._.P = self._.n * self._.Q.inverse()
self._check_eigenmatrices()
def _compute_pTable(self, expand=False, factor=False,
simplify=False):
"""
Compute the intersection numbers.
"""
if self._has("p"):
return
if not self._has("k"):
self.kTable(expand=expand, factor=factor, simplify=simplify)
if not self._has("m"):
self.multiplicities(expand=expand, factor=factor,
simplify=simplify)
p = Array3D(self._.d + 1)
self._compute_parameters(p, self._.P, self._.m, integral=True,
name=PARAMETER, sym=SYMBOL)
self._.p = p
self.check_handshake()
def _copy(self, p):
"""
Copy fields to the given obejct.
"""
p._.d = self._.d
p._.n = self._.n
if self._has("p"):
p._.p = copy(self._.p)
if self._has("q"):
p._.q = copy(self._.q)
if self._has("P"):
p._.P = copy(self._.P)
if self._has("Q"):
p._.Q = copy(self._.Q)
if self._has("k"):
p._.k = self._.k
if self._has("m"):
p._.m = self._.m
if self._has("fsd"):
p._.fsd = self._.fsd
if self._has("pPolynomial_ordering"):
p._.pPolynomial_ordering = self._.pPolynomial_ordering
if self._has("qPolynomial_ordering"):
p._.qPolynomial_ordering = self._.qPolynomial_ordering
if self._has("complement"):
p._.complement = self._.complement
p._.fusion_schemes.update(self._.fusion_schemes)
p._.subschemes.update(self._.subschemes)
p._.subconstituents = list(self._.subconstituents)
p._.triple.update(self._.triple)
p._.triple_solution.update(self._.triple_solution)
p._.triple_solution_generator.update(self._.triple_solution_generator)
p._.quadruple.update(self._.quadruple)
def _derived(self, derived=True):
"""
Generate parameters sets of derived association schemes.
"""
self.polynomialOrders()
self.all_subconstituents(compute=derived > 1)
if derived > 1:
self.all_fusions()
for pa, part in self._.fusion_schemes.items():
yield (pa, part, True)
for pa, part in self._.subschemes.items():
yield (pa, part, False)
@staticmethod
def _get_class():
"""
Return the principal class of the object.
"""
return ASParameters
def _get_parameters(self):
"""
Return the defining parameter set, if any.
Currently, this is not defined for general association schemes.
"""
return None
def _has(self, name):
"""
Check whether the given parameter is available.
"""
return hasattr(self._, name)
def _init_eigenmatrix(self, P):
"""
Initialize an eigenmatrix from the specified matrix.
"""
self._.d = nrows(P) - 1
assert all(len(r) == self._.d + 1 for r in P), \
"parameter length mismatch"
P = Matrix(SR, P)
for i, x in enumerate(P[0]):
P[0, i] = integralize(x)
self._.n = sum(P[0])
return P
def _init_parameters(self, p, integral=False, name=None, sym=None):
"""
Initialize the intersection numbers or Krein parameters
from the specified array.
"""
self._.d = nrows(p) - 1
if isinstance(p, Array3D):
a = p
else:
assert all(len(M) == self._.d + 1 and all(len(r) == self._.d+1
for r in M)
for M in p), "parameter length mismatch"
a = Array3D(self._.d + 1)
for h in range(self._.d + 1):
for i in range(self._.d + 1):
for j in range(self._.d + 1):
a[h, i, j] = p[h][i][j]
self._check_parameters(a, integral=integral, name=name, sym=sym)
return a
def _init_prefix(self):
"""
Initialize prefix to be used for internal variables.
"""
self._.prefix = "v%x" % (hash(self) % Integer(2)**32)
def _init_schoenberg(self):
u"""
Initialize parameters for the computation of the limit
up to which Schönberg's theorem is tested.
"""
return (self._.d, 1 / self._.n)
def _init_storage(self):
"""
Initialize parameter storage object.
"""
if self._ is None:
self._ = Parameters(self)
def _init_vars(self):
"""
Initialize the list of variables.
"""
if not self._has("vars"):
if self._has("p"):
self._.vars = self._.p.variables()
elif self._has("q"):
self._.vars = self._.q.variables()
elif self._has("P"):
self._.vars = variables(self._.P)
elif self._has("Q"):
self._.vars = variables(self._.Q)
self._.vars_ordered = len(self._.vars) <= 1
def _is_polynomial(self, p, i):
"""
Check whether the association scheme is polynomial
for the given parameters and principal relation or eigenspace.
"""
order = [0, i]
while len(order) <= self._.d:
j = {h for h in range(self._.d+1)
if h not in order[-2:] and p[order[-1], i, h] != 0}
if len(j) != 1:
return False
j, = j
order.append(j)
return tuple(order)
def _is_trivial(self):
"""
Check whether the association scheme is trivial
for the purposes of feasibility checking.
Returns ``True`` if the scheme has at most one class.
"""
return self._.d <= 1
@staticmethod
def _merge_parts(parts, p, sym=None):
"""
Return a parameter set for a scheme
with merged relations or eigenspaces.
"""
d = len(parts)
concat = sum(parts, [])
assert parts[0] == [0], "identity not preserved"
assert all(len(pt) > 0 for pt in parts), "empty group specified"
assert len(concat) == len(set(concat)), "repeated part specified"
assert set(concat) == set(range(len(p))), "invalid part specified"
a = Array3D(d)
for h in range(d):
for i in range(d):
for j in range(d):
a[h, i, j] = sum(p[parts[h][0], ii, jj]
for ii in parts[i] for jj in parts[j])
if not all(a[h, i, j] == sum(p[hh, ii, jj]
for ii in parts[i]
for jj in parts[j])
for hh in parts[h][1:]):
raise IndexError(
"inconsistent parameters for %s[%d, %d, %d]" %
(sym, h, i, j))
return a
def _reorder(self, order):
"""
Check and normalize a given order of relations or eigenspaces.
"""
if len(order) == 1 and isinstance(order[0], (tuple, list)):
order = order[0]
if 0 in order:
assert order[0] == 0, "zero cannot be reordered"
else:
order = [0] + list(order)
assert len(order) == self._.d + 1, "wrong number of indices"
assert set(order) == set(range(self._.d + 1)), \
"repeating or nonexisting indices"
return tuple(order)
@staticmethod
def _subconstituent_name(h):
"""
Return a properly formatted ordinal for the given subconstituent.
"""
if h == 1:
o = "1st"
elif h == 2:
o = "2nd"
elif h == 3:
o = "3rd"
else:
o = "%dth" % h
return "%s subconstituent" % o
def _subs(self, exp, p, seen):
"""
Substitute the given subexpressions in the paramaters.
"""
if id(self) in seen:
return (seen[id(self)], False)
seen[id(self)] = p
if self._has("p") and not p._has("p"):
p._.p = self._.p.subs(*exp)
if self._has("q") and not p._has("q"):
p._.q = self._.q.subs(*exp)
if self._has("P") and not p._has("P"):
p._.P = self._.P.subs(*exp)
if self._has("Q") and not p._has("Q"):
p._.Q = self._.Q.subs(*exp)
for k, v in self._.triple.items():
p._.triple[k] = v.subs(*exp)
for k, v in self._.quadruple.items():
p._.quadruple[k] = v.subs(*exp)
for par, part in self._.subschemes.items():
try:
p.add_subscheme(par.subs(*exp, seen=seen), part)
except (InfeasibleError, AssertionError) as ex:
raise InfeasibleError(ex, part=part)
for par, part in self._.fusion_schemes.items():
try:
p.add_subscheme(par.subs(*exp, seen=seen), part)
except (InfeasibleError, AssertionError) as ex:
raise InfeasibleError(ex, part=part)
for h, s in enumerate(self._.subconstituents):
if s is None:
continue
name = self._subconstituent_name(h)
try:
p._.subconstituents[h] = p.add_subscheme(
self._.subconstituents[h].subs(*exp, seen=seen), name)
except (InfeasibleError, AssertionError) as ex:
raise InfeasibleError(ex, part=name)
if self._has("complement") and not p._has("complement"):
try:
p._.complement = self._.complement.subs(*exp, seen=seen)
except (InfeasibleError, AssertionError) as ex:
raise InfeasibleError(ex, part="complement")
return (p, True)
def add_subscheme(self, par, part):
"""
Add a derived scheme into the list.
"""
if par in self._.fusion_schemes:
return next(s for s in self._.fusion_schemes if s == par)
elif par in self._.subschemes:
return next(s for s in self._.subschemes if s == par)
elif not isinstance(par, ASParameters):
try:
par = ASParameters(*par)
except (InfeasibleError, AssertionError) as ex:
raise InfeasibleError(ex, part=part)
if par._.n == self._.n:
self._.fusion_schemes[par] = part
else:
self._.subschemes[par] = part
return par
def all_fusions(self):
"""
Return a dictionary of parameters for all fusion schemes.
"""
out = {}
if self._has("p"):
fun = self.merge_subconstituents
elif self._has("q"):
fun = self.merge_eigenspaces
elif self._has("P"):
fun = self.merge_subconstituents
elif self._has("Q"):
fun = self.merge_eigenspaces
for parts in SetPartitions(range(1, self._.d+1)):
if len(parts) == self._.d:
continue
parts = tuple(tuple(sorted(p)) for p in parts)
try:
out[parts] = fun(*parts)
except IndexError:
pass
return out
def all_subconstituents(self, compute=False):
"""
Return a dictionary of parameters for subconstituents
which are known to be association schemes.
"""
out = {}
for i in range(self._.d+1):
try:
out[i] = self.subconstituent(i, compute=compute)
except IndexError:
pass
return out
def check_feasible(self, checked=None, skip=None, derived=True, levels=3,
queue=None, part=()):
"""
Check whether the parameter set is feasible.
"""
if self._is_trivial():
return
par = self._get_parameters()
if checked is None:
checked = set()
if par in checked:
return
if skip is None:
skip = set()
elif isinstance(skip, str):
skip = {skip}
else:
skip = set(skip)
for i, lvl in enumerate(self._checklist[:levels]):
for name, check in lvl:
if name not in skip:
check(self)
if i > 1:
skip.add(name)
if not derived:
return
if par is not None:
checked.add(par)
do_bfs = False
if queue is None:
queue = []
do_bfs = True
for par, pt, reorder in self._derived(derived):
if par in checked:
continue
queue.append((par, (pt, ) + part, skip if reorder else None))
if do_bfs:
i = 0
while i < len(queue):
par, pt, skip = queue[i]
try:
par.check_feasible(checked=checked, skip=skip,
levels=levels, queue=queue, part=pt)
except (InfeasibleError, AssertionError) as ex:
raise InfeasibleError(ex, part=pt)
i += 1
def check_handshake(self):
"""
Verify the handshake lemma for all relations in all subconstituents.
"""
if not self._has("k"):
self.kTable()
if not self._has("p"):
self.pTable()
d = [self._.d, 0 if self.METRIC else self._.d]
b = 2 if self._.bipartite else 1
odd = not is_divisible(self._.n, 2)
ndiv3 = not is_divisible(self._.n, 3)
for i in range(1, self._.d + 1):
d[1] += 2
if not is_divisible(self._.k[i], 2):
if odd:
raise InfeasibleError("handshake lemma not satisfied "
"for relation %d" % i)
for j in range(b, min(d) + 1, b):
if not is_divisible(self._.p[i, i, j], 2):
raise InfeasibleError("handshake lemma not satisfied"
" for relation %d in"
" subconstituent %d" % (j, i))
if ndiv3 and not is_divisible(self._.k[i], 3) \
and not is_divisible(self._.p[i, i, i], 3):
raise InfeasibleError("handshake lemma not satisfied "
"for triangles in relation %d" % i)
def classes(self):
"""
Return the number of classes of the association scheme.
"""
return self._.d
def complement(self):
"""
Return the parameters of the complement of a strongly regular graph.
"""
assert self._.d == 2, "the complement is only defined for two classes"
return self._.complement
def dualEigenmatrix(self, expand=False, factor=False, simplify=False):
"""
Compute and return the dual eigenmatrix of the association scheme.
"""
self._compute_dualEigenmatrix(expand=expand, factor=factor,
simplify=simplify)
self._check_eigenmatrices()
rewriteMatrix(self._.Q, expand=expand, factor=factor,
simplify=simplify)
return Matrix(SR, self._.Q)
def eigenmatrix(self, expand=False, factor=False, simplify=False):
"""
Compute and return the eigenmatrix of the association scheme.
"""
self._compute_primalEigenmatrix(expand=expand, factor=factor,
simplify=simplify)
self._check_eigenmatrices()
rewriteMatrix(self._.P, expand=expand, factor=factor,
simplify=simplify)
return Matrix(SR, self._.P)
def is_formallySelfDual(self):
"""
Check whether the association scheme is formally self-dual.
"""
if not self._has("fsd"):
self._.fsd = (self.eigenmatrix(simplify=2)
- self.dualEigenmatrix(simplify=2)).is_zero()
return self._.fsd
def is_pPolynomial(self):
"""
Check whether the association scheme is P-polynomial,
and return all P-polynomial orderings if it is.
"""
if not self._has("p"):
self.pTable()
if not self._has("pPolynomial_ordering"):
pPoly = tuple(filter(None, (self._is_polynomial(self._.p, i)
for i in range(1, self._.d+1))))
self._.pPolynomial_ordering = False if len(pPoly) == 0 else pPoly
return self._.pPolynomial_ordering
def is_qPolynomial(self):
"""
Check whether the association scheme is Q-polynomial,
and return all Q-polynomial orderings if it is.
"""
if not self._has("q"):
self.kreinParameters()
if not self._has("qPolynomial_ordering"):
qPoly = tuple(filter(None, (self._is_polynomial(self._.q, i)
for i in range(1, self._.d+1))))
self._.qPolynomial_ordering = False if len(qPoly) == 0 else qPoly
return self._.qPolynomial_ordering
def kTable(self, expand=False, factor=False, simplify=False):
"""
Compute and return the valencies of the relations.
"""
self._compute_kTable(expand=expand, factor=factor,
simplify=simplify)
self._.k = rewriteTuple(self._.k, expand=expand, factor=factor,
simplify=simplify)
return self._.k
def merge_eigenspaces(self, *parts):
"""
Return a parameter set for a scheme with merged eigenspaces.
"""
parts = [list(pt) for pt in parts]
if parts[0] != [0]:
parts.insert(0, [0])
if not self._has("q"):
self.kreinParameters()
par = ASParameters(q=self._merge_parts(parts, self._.q, "q"))
self.add_subscheme(par,
"Fusion scheme for eigenspaces %s" % parts)
return par
def merge_relations(self, *parts):
"""
Return a parameter set for a scheme with merged relations.
"""
parts = [list(pt) for pt in parts]
if parts[0] != [0]:
parts.insert(0, [0])
if not self._has("p"):
self.pTable()
par = ASParameters(p=self._merge_parts(parts, self._.p, "p"))
self.add_subscheme(par,
"Fusion scheme for relations %s" % parts)
return par
def mTable(self, expand=False, factor=False, simplify=False):
"""
Compute and return the multiplicities of the eigenspaces.
"""
self._compute_multiplicities(expand=expand, factor=factor,
simplify=simplify)
self._.m = rewriteTuple(self._.m, expand=expand, factor=factor,
simplify=simplify)
return self._.m
def polynomialOrders(self):
"""
Return a dictionary of all P- or Q-polynomial orderings.
"""
from .drg import DRGParameters
from .qpoly import QPolyParameters
out = {}
if self.is_pPolynomial():
for order in self._.pPolynomial_ordering:
pa = self.add_subscheme(DRGParameters(self, order=order),
"P-polynomial ordering %s" % (order, ))
out["P", order] = pa
if self.is_qPolynomial():
for order in self._.qPolynomial_ordering:
pa = self.add_subscheme(QPolyParameters(self, order=order),
"Q-polynomial ordering %s" % (order, ))
out["Q", order] = pa
return out
def pTable(self, expand=False, factor=False, simplify=False):
"""
Compute and return the intersection numbers.
"""
self._compute_pTable(expand=expand, factor=factor, simplify=simplify)
self._.p.rewrite(expand=expand, factor=factor, simplify=simplify)
return self._.p
def qTable(self, expand=False, factor=False, simplify=False):
"""
Compute and return the Krein parameters.
"""
self._compute_kreinParameters(expand=expand, factor=factor,
simplify=simplify)
self._.q.rewrite(expand=expand, factor=factor, simplify=simplify)
return self._.q
def quadrupleEquations(self, h, i, j, r, s, t, krein=None, params=None,
solve=True, fresh=False, save=None):
"""
Solve equations for quadruples of vertices ``w, x, y, z``
such that ``d(w, x) = h``, ``d(w, y) = i``, ``d(w, z) = j``,
``d(x, y) = r``, ``d(x, z) = s``, ``d(y, z) = t``.