Skip to content

Commit b255255

Browse files
author
Release Manager
committed
Trac #24345: Disallow boolean operations with Unknown
The `Unknown` from `misc/unknown.py` incorrectly behaves like `False` in the context of boolean operations. It is mostly useless as long as Python has no tristate conditionals and logic operators. This ticket raises an error instead. As a consequence of this modification, Sage code is adapted as follows for a given tristate `troolean` {{{ if troolean is True: ... elif troolean is False: ... else: ... }}} URL: https://trac.sagemath.org/24345 Reported by: rws Ticket author(s): Ralf Stephan Reviewer(s): Vincent Delecroix
2 parents 0d09e98 + 96fd7a2 commit b255255

10 files changed

+191
-147
lines changed

src/sage/combinat/designs/bibd.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def balanced_incomplete_block_design(v, k, existence=False, use_LJCR=False):
232232
return True
233233
B = BIBD_from_arc_in_desarguesian_projective_plane(v,k)
234234
return BalancedIncompleteBlockDesign(v, B, copy=False)
235-
if BIBD_from_TD(v,k,existence=True):
235+
if BIBD_from_TD(v,k,existence=True) is True:
236236
if existence:
237237
return True
238238
return BalancedIncompleteBlockDesign(v, BIBD_from_TD(v,k), copy=False)
@@ -241,7 +241,7 @@ def balanced_incomplete_block_design(v, k, existence=False, use_LJCR=False):
241241
return True
242242
from .block_design import projective_plane
243243
return BalancedIncompleteBlockDesign(v, projective_plane(k-1),copy=False)
244-
if difference_family(v,k,existence=True):
244+
if difference_family(v,k,existence=True) is True:
245245
if existence:
246246
return True
247247
G,D = difference_family(v,k)
@@ -434,8 +434,8 @@ def BIBD_from_TD(v,k,existence=False):
434434
"""
435435
# First construction
436436
if (v%k == 0 and
437-
balanced_incomplete_block_design(v//k,k,existence=True) and
438-
transversal_design(k,v//k,existence=True)):
437+
balanced_incomplete_block_design(v//k,k,existence=True) is True and
438+
transversal_design(k,v//k,existence=True) is True):
439439

440440
if existence:
441441
return True
@@ -467,9 +467,8 @@ def BIBD_from_TD(v,k,existence=False):
467467

468468
# Third construction
469469
elif ((v-k)%k == 0 and
470-
balanced_incomplete_block_design((v-k)//k+k,k,existence=True) and
471-
transversal_design(k,(v-k)//k,existence=True)):
472-
470+
balanced_incomplete_block_design((v-k)//k+k,k,existence=True) is True
471+
and transversal_design(k,(v-k)//k,existence=True) is True):
473472
if existence:
474473
return True
475474

src/sage/combinat/designs/difference_family.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ def is_difference_family(G, D, v=None, k=None, l=None, verbose=False):
219219
sage: _
220220
False
221221
"""
222-
import operator
223-
224222
identity, mul, inv = group_law(G)
225223

226224
Glist = list(G)
@@ -426,7 +424,7 @@ def df_q_6_1(K, existence=False, check=True):
426424
427425
sage: from sage.combinat.designs.difference_family import is_difference_family, df_q_6_1
428426
sage: prime_powers = [v for v in range(31,500,30) if is_prime_power(v)]
429-
sage: parameters = [v for v in prime_powers if df_q_6_1(GF(v,'a'), existence=True)]
427+
sage: parameters = [v for v in prime_powers if df_q_6_1(GF(v,'a'), existence=True) is True]
430428
sage: parameters
431429
[31, 151, 181, 211, 241, 271, 331, 361, 421]
432430
sage: for v in parameters:
@@ -507,7 +505,7 @@ def radical_difference_set(K, k, l=1, existence=False, check=True):
507505
sage: for k in range(2,50):
508506
....: for l in reversed(divisors(k*(k-1))):
509507
....: v = k*(k-1)//l + 1
510-
....: if is_prime_power(v) and radical_difference_set(GF(v,'a'),k,l,existence=True):
508+
....: if is_prime_power(v) and radical_difference_set(GF(v,'a'),k,l,existence=True) is True:
511509
....: _ = radical_difference_set(GF(v,'a'),k,l)
512510
....: print("{:3} {:3} {:3}".format(v,k,l))
513511
3 2 1
@@ -878,7 +876,7 @@ def radical_difference_family(K, k, l=1, existence=False, check=True):
878876
....: for q in range(k*(k-1)+1, 2000, k*(k-1)):
879877
....: if is_prime_power(q):
880878
....: K = GF(q,'a')
881-
....: if radical_difference_family(K, k, existence=True):
879+
....: if radical_difference_family(K, k, existence=True) is True:
882880
....: list_q.append(q)
883881
....: _ = radical_difference_family(K,k)
884882
....: print(" ".join(str(p) for p in list_q))
@@ -896,7 +894,6 @@ def radical_difference_family(K, k, l=1, existence=False, check=True):
896894
"""
897895
v = K.cardinality()
898896
x = K.multiplicative_generator()
899-
one = K.one()
900897
e = k*(k-1)
901898
if (l*(v-1)) % e:
902899
raise ValueError("k (k-1) = {} should be a multiple of l (v-1) ={}".format(
@@ -1209,11 +1206,11 @@ def hadamard_difference_set_product(G1, D1, G2, D2):
12091206
12101207
sage: G11,D11 = hadamard_difference_set_product(G1,D1,G1,D1)
12111208
sage: assert is_difference_family(G11, D11, 256, 120, 56)
1212-
sage: assert designs.difference_family(256, 120, 56, existence=True)
1209+
sage: assert designs.difference_family(256, 120, 56, existence=True) is True
12131210
12141211
sage: G12,D12 = hadamard_difference_set_product(G1,D1,G2,D2)
12151212
sage: assert is_difference_family(G12, D12, 576, 276, 132)
1216-
sage: assert designs.difference_family(576, 276, 132, existence=True)
1213+
sage: assert designs.difference_family(576, 276, 132, existence=True) is True
12171214
"""
12181215
from sage.categories.cartesian_product import cartesian_product
12191216

@@ -1379,7 +1376,8 @@ def difference_family(v, k, l=1, existence=False, explain_construction=False, ch
13791376
....: constructions = []
13801377
....: for k in range(2,10):
13811378
....: for l in range(1,10):
1382-
....: if designs.difference_family(v,k,l,existence=True):
1379+
....: ret = designs.difference_family(v,k,l,existence=True)
1380+
....: if ret is True:
13831381
....: constructions.append((k,l))
13841382
....: _ = designs.difference_family(v,k,l)
13851383
....: if constructions:
@@ -1490,7 +1488,7 @@ def difference_family(v, k, l=1, existence=False, explain_construction=False, ch
14901488
sage: for N in range(2,21):
14911489
....: v = 4*N^2; k = 2*N^2-N; l = N^2-N
14921490
....: status = designs.difference_family(v,k,l,existence=True)
1493-
....: print("{:2} {}".format(N,designs.difference_family(v,k,l,explain_construction=True) if status else status))
1491+
....: print("{:2} {}".format(N,designs.difference_family(v,k,l,explain_construction=True) if status is True else status))
14941492
2 McFarland 1973 construction
14951493
3 Turyn 1965 construction
14961494
4 McFarland 1973 construction
@@ -1602,7 +1600,6 @@ def difference_family(v, k, l=1, existence=False, explain_construction=False, ch
16021600
if existence:
16031601
return Unknown
16041602
raise NotImplementedError("No construction available for ({},{},{})-difference family".format(v,k,l))
1605-
t = l*(v-1) // e # number of blocks
16061603

16071604
# trivial construction
16081605
if k == (v-1) and l == (v-2):
@@ -1666,7 +1663,7 @@ def difference_family(v, k, l=1, existence=False, explain_construction=False, ch
16661663
else:
16671664
raise EmptySetError("by McFarland 1989 such difference family does not exist")
16681665

1669-
elif len(factorization) == 1 and radical_difference_family(K, k, l, existence=True):
1666+
elif len(factorization) == 1 and radical_difference_family(K, k, l, existence=True) is True:
16701667
if existence:
16711668
return True
16721669
elif explain_construction:
@@ -1675,7 +1672,10 @@ def difference_family(v, k, l=1, existence=False, explain_construction=False, ch
16751672
D = radical_difference_family(K,k,l)
16761673
G = K
16771674

1678-
elif len(factorization) == 1 and l == 1 and k == 6 and df_q_6_1(K, existence=True):
1675+
elif (len(factorization) == 1
1676+
and l == 1
1677+
and k == 6
1678+
and df_q_6_1(K, existence=True) is True):
16791679
if existence:
16801680
return True
16811681
elif explain_construction:

src/sage/combinat/designs/difference_matrices.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def find_product_decomposition(g, k, lmbda=1):
6666
g2 = g//g1
6767
if g1 > g2:
6868
break
69-
if (difference_matrix(g1,k,lmbda1,existence=True) and
70-
difference_matrix(g2,k,lmbda2,existence=True)):
69+
if (difference_matrix(g1,k,lmbda1,existence=True) is True and
70+
difference_matrix(g2,k,lmbda2,existence=True) is True):
7171
return (g1,lmbda1),(g2,lmbda2)
7272

7373
return False
@@ -245,7 +245,7 @@ def difference_matrix(g,k,lmbda=1,existence=False,check=True):
245245
# (find the max k such that there exists a DM)
246246
elif k is None:
247247
i = 2
248-
while difference_matrix(g=g,k=i,lmbda=lmbda,existence=True):
248+
while difference_matrix(g=g,k=i,lmbda=lmbda,existence=True) is True:
249249
i += 1
250250
return i-1
251251

src/sage/combinat/designs/orthogonal_arrays.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
4646
REFERENCES:
4747
48-
- [CD1996]_
48+
-- [CD1996]_
4949
5050
Functions
5151
---------
@@ -917,7 +917,7 @@ def orthogonal_array(k,n,t=2,resolvable=False, check=True,existence=False,explai
917917
return [[i,j,(i+j)%n] for i in range(n) for j in range(n)]
918918

919919
# projective spaces are equivalent to OA(n+1,n,2)
920-
elif (projective_plane(n, existence=True) or
920+
elif (projective_plane(n, existence=True) is True or
921921
(k == n+1 and projective_plane(n, existence=True) is False)):
922922
_OA_cache_set(n+1,n,projective_plane(n, existence=True))
923923
if k == n+1:
@@ -983,7 +983,7 @@ def orthogonal_array(k,n,t=2,resolvable=False, check=True,existence=False,explai
983983
break
984984

985985
# From Difference Matrices
986-
elif may_be_available and difference_matrix(n,k-1,existence=True):
986+
elif may_be_available and difference_matrix(n,k-1,existence=True) is True:
987987
_OA_cache_set(k,n,True)
988988
if existence:
989989
return True
@@ -1050,7 +1050,7 @@ def largest_available_k(n,t=2):
10501050
from sage.rings.infinity import Infinity
10511051
return Infinity
10521052
elif t == 2:
1053-
if projective_plane(n,existence=True):
1053+
if projective_plane(n,existence=True) is True:
10541054
return n+1
10551055
else:
10561056
k=1
@@ -1337,15 +1337,15 @@ def incomplete_orthogonal_array(k,n,holes,resolvable=False, existence=False):
13371337
"intersect in a projective plane.").format(number_of_holes))
13381338

13391339
# Holes of size 1 from OA(k+1,n)
1340-
elif max_hole==1 and orthogonal_array(k+1,n,existence=True):
1340+
elif max_hole==1 and orthogonal_array(k+1,n,existence=True) is True:
13411341
if existence:
13421342
return True
13431343
OA = orthogonal_array(k+1,n)
13441344
independent_set = [B[:-1] for B in OA if B[-1] == 0][:number_of_holes]
13451345
OA = [B[:-1] for B in OA]
13461346

1347-
elif max_hole==1 and orthogonal_array(k,n,existence=True):
1348-
OA = orthogonal_array(k, n)
1347+
elif max_hole==1 and orthogonal_array(k,n,existence=True) is True:
1348+
OA = orthogonal_array(k,n)
13491349
try:
13501350
independent_set = OA_find_disjoint_blocks(OA,k,n,number_of_holes)
13511351
except ValueError:
@@ -1356,7 +1356,7 @@ def incomplete_orthogonal_array(k,n,holes,resolvable=False, existence=False):
13561356
return True
13571357
independent_set = OA_find_disjoint_blocks(OA,k,n,number_of_holes)
13581358

1359-
elif max_hole==1 and not orthogonal_array(k,n,existence=True):
1359+
elif max_hole==1 and not orthogonal_array(k,n,existence=True) is True:
13601360
return orthogonal_array(k,n,existence=existence)
13611361

13621362
# From a quasi-difference matrix

src/sage/combinat/designs/resolvable_bibd.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def resolvable_balanced_incomplete_block_design(v,k,existence=False):
9292
9393
sage: for v in range(40):
9494
....: for k in range(v):
95-
....: if designs.resolvable_balanced_incomplete_block_design(v,k,existence=True):
95+
....: if designs.resolvable_balanced_incomplete_block_design(v,k,existence=True) is True:
9696
....: _ = designs.resolvable_balanced_incomplete_block_design(v,k)
9797
"""
9898
# Trivial cases
@@ -448,7 +448,7 @@ def PBD_4_7(v,check=True, existence=False):
448448
449449
sage: for i in range(1,300,3):
450450
....: if i not in [10,19,31]:
451-
....: assert PBD_4_7(i,existence=True)
451+
....: assert PBD_4_7(i,existence=True) is True
452452
....: _ = PBD_4_7(i,check=True)
453453
"""
454454
if v%3 != 1 or v in [10,19,31]:
@@ -623,7 +623,7 @@ def PBD_4_7(v,check=True, existence=False):
623623

624624
return PBD_4_7_from_Y(GDD,check=check)
625625

626-
elif v % 6 == 1 and GDD_4_2((v - 1) // 6, existence=True):
626+
elif v % 6 == 1 and GDD_4_2((v - 1) // 6, existence=True) is True:
627627
# VII.5.17 from [BJL99]
628628
gdd = GDD_4_2((v - 1) // 6)
629629
return PBD_4_7_from_Y(gdd, check=check)
@@ -634,9 +634,9 @@ def PBD_4_7(v,check=True, existence=False):
634634
PBD = PBD_4_7_from_Y(PBD,check=False)
635635
return PBD_4_7_from_Y(PBD,check=check)
636636

637-
elif balanced_incomplete_block_design(v,4,existence=True):
637+
elif balanced_incomplete_block_design(v,4,existence=True) is True:
638638
return balanced_incomplete_block_design(v,4)
639-
elif balanced_incomplete_block_design(v,7,existence=True):
639+
elif balanced_incomplete_block_design(v,7,existence=True) is True:
640640
return balanced_incomplete_block_design(v,7)
641641
else:
642642
from sage.combinat.designs.orthogonal_arrays import orthogonal_array
@@ -651,9 +651,9 @@ def PBD_4_7(v,check=True, existence=False):
651651
vv = (v - 1) // 3
652652
for g in range((vv + 5 - 1) // 5, vv // 4 + 1):
653653
u = vv-4*g
654-
if (orthogonal_array(5,g,existence=True) and
655-
PBD_4_7(3*g+1,existence=True) and
656-
PBD_4_7(3*u+1,existence=True)):
654+
if (orthogonal_array(5,g,existence=True) is True and
655+
PBD_4_7(3*g+1,existence=True) is True and
656+
PBD_4_7(3*u+1,existence=True) is True):
657657
from .orthogonal_arrays import transversal_design
658658
domain = set(range(vv))
659659
GDD = transversal_design(5,g)
@@ -723,7 +723,7 @@ def PBD_4_7_from_Y(gdd,check=True):
723723
"but there are other: {}".format(txt))
724724

725725
for gs in group_sizes:
726-
if not PBD_4_7(3*gs+1,existence=True):
726+
if not PBD_4_7(3*gs+1,existence=True) is True:
727727
raise RuntimeError("A group has size {} but I do not know how to "
728728
"build a ({},[4,7])-PBD".format(gs,3*gs+1))
729729

src/sage/combinat/matrices/hadamard_matrix.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,8 @@ def true():
653653
elif ( e == 1 and
654654
not sqn is None and
655655
sqn%4 == 2 and
656-
True == strongly_regular_graph(sqn-1,(sqn-2)//2,(sqn-6)//4,
657-
existence=True) and
656+
strongly_regular_graph(sqn-1,(sqn-2)//2,(sqn-6)//4,
657+
existence=True) is True and
658658
is_prime_power(ZZ(sqn+1))):
659659
if existence:
660660
return true()
@@ -666,8 +666,8 @@ def true():
666666
for n1,e1 in product(divisors(n)[1:-1],[-1,1]):
667667
e2 = e1*e
668668
n2 = n//n1
669-
if (regular_symmetric_hadamard_matrix_with_constant_diagonal(n1,e1,existence=True) and
670-
regular_symmetric_hadamard_matrix_with_constant_diagonal(n2,e2,existence=True)):
669+
if (regular_symmetric_hadamard_matrix_with_constant_diagonal(n1,e1,existence=True) is True and
670+
regular_symmetric_hadamard_matrix_with_constant_diagonal(n2,e2,existence=True)) is True:
671671
if existence:
672672
return true()
673673
M1 = regular_symmetric_hadamard_matrix_with_constant_diagonal(n1,e1)
@@ -1122,7 +1122,7 @@ def true():
11221122
M = hadamard_matrix_paleyI(n, normalize=False)
11231123

11241124
elif n % 8 == 0:
1125-
if skew_hadamard_matrix(n//2,existence=True): # (Lemma 14.1.6 in [Ha83]_)
1125+
if skew_hadamard_matrix(n//2,existence=True) is True: # (Lemma 14.1.6 in [Ha83]_)
11261126
if existence:
11271127
return true()
11281128
H = skew_hadamard_matrix(n//2,check=False)
@@ -1132,7 +1132,7 @@ def true():
11321132
for d in divisors(n)[2:-2]: # skip 1, 2, n/2, and n
11331133
n1 = n//d
11341134
if is_prime_power(d - 1) and (d % 4 == 0) and (n1 % 4 == 0)\
1135-
and skew_hadamard_matrix(n1,existence=True):
1135+
and skew_hadamard_matrix(n1,existence=True) is True:
11361136
if existence:
11371137
return true()
11381138
H = skew_hadamard_matrix(n1, check=False)-I(n1)
@@ -1145,7 +1145,7 @@ def true():
11451145
M = A.tensor_product(I(n1))+(U*A).tensor_product(H)
11461146
break
11471147
if M is None: # try Williamson-Goethals-Seidel construction
1148-
if GS_skew_hadamard_smallcases(n, existence=True):
1148+
if GS_skew_hadamard_smallcases(n, existence=True) is True:
11491149
if existence:
11501150
return true()
11511151
M = GS_skew_hadamard_smallcases(n)

src/sage/databases/oeis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ def __iter__(self):
14701470
"""
14711471
for x in self.first_terms():
14721472
yield x
1473-
if not self.is_full():
1473+
if not self.is_full() is True:
14741474
raise LookupError("Future values not provided by OEIS.")
14751475

14761476
def references(self):

0 commit comments

Comments
 (0)