Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

# -*- coding: utf-8 -*- 

r""" 

Difference families 

 

This module gathers everything related to difference families. One can build a 

difference family (or check that it can be built) with :func:`difference_family`:: 

 

sage: G,F = designs.difference_family(13,4,1) 

 

It defines the following functions: 

 

{INDEX_OF_FUNCTIONS} 

 

REFERENCES: 

 

.. [BJL99-1] \T. Beth, D. Jungnickel, H. Lenz "Design theory Vol. I." 

Second edition. Encyclopedia of Mathematics and its Applications, 69. Cambridge 

University Press, (1999). 

 

.. [BLJ99-2] \T. Beth, D. Jungnickel, H. Lenz "Design theory Vol. II." 

Second edition. Encyclopedia of Mathematics and its Applications, 78. Cambridge 

University Press, (1999). 

 

.. [Bo39] \R. C. Bose, "On the construction of balanced incomplete block 

designs", Ann. Eugenics, 9 (1939), 353--399. 

 

.. [Bu95] \M. Buratti "On simple radical difference families", J. 

Combinatorial Designs, 3 (1995) 161--168. 

 

.. [Tu1965] \R. J. Turyn "Character sum and difference sets" 

Pacific J. Math. 15 (1965) 319--346. 

 

.. [Tu1984] \R. J. Turyn "A special class of Williamson matrices and 

difference sets" J. Combinatorial Theory (A) 36 (1984) 111--115. 

 

.. [Wi72] \R. M. Wilson "Cyclotomy and difference families in elementary Abelian 

groups", J. Number Theory, 4 (1972) 17--47. 

 

Functions 

--------- 

""" 

#***************************************************************************** 

# Copyright (C) 2014 Vincent Delecroix <20100.delecroix@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

# python3 

from __future__ import division, print_function, absolute_import 

 

from builtins import zip 

import six 

from six import itervalues 

from six.moves import range 

 

from sage.misc.cachefunc import cached_method 

 

from sage.categories.sets_cat import EmptySetError 

import sage.arith.all as arith 

from sage.misc.unknown import Unknown 

from sage.rings.integer import Integer 

from sage.rings.integer_ring import ZZ 

 

def group_law(G): 

r""" 

Return a triple ``(identity, operation, inverse)`` that define the 

operations on the group ``G``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import group_law 

sage: group_law(Zmod(3)) 

(0, <built-in function add>, <built-in function neg>) 

sage: group_law(SymmetricGroup(5)) 

((), <built-in function mul>, <built-in function inv>) 

sage: group_law(VectorSpace(QQ,3)) 

((0, 0, 0), <built-in function add>, <built-in function neg>) 

""" 

import operator 

from sage.categories.groups import Groups 

from sage.categories.additive_groups import AdditiveGroups 

 

if G in Groups(): # multiplicative groups 

return (G.one(), operator.mul, operator.inv) 

elif G in AdditiveGroups(): # additive groups 

return (G.zero(), operator.add, operator.neg) 

else: 

raise ValueError("%s does not seem to be a group"%G) 

 

def block_stabilizer(G, B): 

r""" 

Compute the left stabilizer of the block ``B`` under the action of ``G``. 

 

This function return the list of all `x\in G` such that `x\cdot B=B` (as a 

set). 

 

INPUT: 

 

- ``G`` -- a group (additive or multiplicative). 

 

- ``B`` -- a subset of ``G``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import block_stabilizer 

 

sage: Z8 = Zmod(8) 

sage: block_stabilizer(Z8, [Z8(0),Z8(2),Z8(4),Z8(6)]) 

[0, 2, 4, 6] 

sage: block_stabilizer(Z8, [Z8(0),Z8(2)]) 

[0] 

 

sage: C = cartesian_product([Zmod(4),Zmod(3)]) 

sage: block_stabilizer(C, [C((0,0)),C((2,0)),C((0,1)),C((2,1))]) 

[(0, 0), (2, 0)] 

 

sage: b = list(map(Zmod(45),[1, 3, 7, 10, 22, 25, 30, 35, 37, 38, 44])) 

sage: block_stabilizer(Zmod(45),b) 

[0] 

""" 

if not B: 

return list(G) 

identity, op, inv = group_law(G) 

b0 = inv(B[0]) 

S = [] 

for b in B: 

# fun: if we replace +(-b) with -b it completely fails!! 

bb0 = op(b,b0) # bb0 = b-B[0] 

if all(op(bb0,c) in B for c in B): 

S.append(bb0) 

return S 

 

def is_difference_family(G, D, v=None, k=None, l=None, verbose=False): 

r""" 

Check wether ``D`` forms a difference family in the group ``G``. 

 

INPUT: 

 

- ``G`` - group of cardinality ``v`` 

 

- ``D`` - a set of ``k``-subsets of ``G`` 

 

- ``v``, ``k`` and ``l`` - optional parameters of the difference family 

 

- ``verbose`` - whether to print additional information 

 

.. SEEALSO:: 

 

:func:`difference_family` 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import is_difference_family 

sage: G = Zmod(21) 

sage: D = [[0,1,4,14,16]] 

sage: is_difference_family(G, D, 21, 5) 

True 

 

sage: G = Zmod(41) 

sage: D = [[0,1,4,11,29],[0,2,8,17,21]] 

sage: is_difference_family(G, D, verbose=True) 

Too few: 

5 is obtained 0 times in blocks [] 

14 is obtained 0 times in blocks [] 

27 is obtained 0 times in blocks [] 

36 is obtained 0 times in blocks [] 

Too much: 

4 is obtained 2 times in blocks [0, 1] 

13 is obtained 2 times in blocks [0, 1] 

28 is obtained 2 times in blocks [0, 1] 

37 is obtained 2 times in blocks [0, 1] 

False 

sage: D = [[0,1,4,11,29],[0,2,8,17,22]] 

sage: is_difference_family(G, D) 

True 

 

sage: G = Zmod(61) 

sage: D = [[0,1,3,13,34],[0,4,9,23,45],[0,6,17,24,32]] 

sage: is_difference_family(G, D) 

True 

 

sage: G = AdditiveAbelianGroup([3]*4) 

sage: a,b,c,d = G.gens() 

sage: D = [[d, -a+d, -c+d, a-b-d, b+c+d], 

....: [c, a+b-d, -b+c, a-b+d, a+b+c], 

....: [-a-b+c+d, a-b-c-d, -a+c-d, b-c+d, a+b], 

....: [-b-d, a+b+d, a-b+c-d, a-b+c, -b+c+d]] 

sage: is_difference_family(G, D) 

True 

 

The following example has a third block with a non-trivial stabilizer:: 

 

sage: G = Zmod(15) 

sage: D = [[0,1,4],[0,2,9],[0,5,10]] 

sage: is_difference_family(G,D,verbose=True) 

It is a (15,3,1)-difference family 

True 

 

The function also supports multiplicative groups (non necessarily Abelian):: 

 

sage: G = DihedralGroup(8) 

sage: x,y = G.gens() 

sage: i = G.one() 

sage: D1 = [[i,x,x^4], [i,x^2, y*x], [i,x^5,y], [i,x^6,y*x^2], [i,x^7,y*x^5]] 

sage: is_difference_family(G, D1, 16, 3, 2) 

True 

sage: from sage.combinat.designs.bibd import BIBD_from_difference_family 

sage: bibd = BIBD_from_difference_family(G,D1,lambd=2) 

 

TESTS:: 

 

sage: K = GF(3^2,'z') 

sage: z = K.gen() 

sage: D = [[1,z+1,2]] 

sage: _ = is_difference_family(K, D, verbose=True) 

the number of differences (=6) must be a multiple of v-1=8 

sage: _ 

False 

""" 

import operator 

 

identity, mul, inv = group_law(G) 

 

Glist = list(G) 

 

D = [[G(_) for _ in d] for d in D] 

 

# Check v (and define it if needed) 

if v is None: 

v = len(Glist) 

else: 

if len(Glist) != v: 

if verbose: 

print("G must have cardinality v (=%d)" % int(v)) 

return False 

 

# Check k (and define it if needed) 

if k is None: 

k = len(D[0]) 

else: 

k = int(k) 

 

for d in D: 

if len(d) != k: 

if verbose: 

print("the block {} does not have length {}".format(d, k)) 

return False 

 

# Check l (and define it if needed) 

# 

# - nb_diff: the number of pairs (with multiplicity) covered by the BIBD 

# generated by the DF. 

# 

# - stab: the stabilizer of each set. 

nb_diff = 0 

stab = [] 

for d in D: 

s = block_stabilizer(G,d) 

stab.append(s) 

nb_diff += k*(k-1) // len(s) 

if l is None: 

if nb_diff % (v-1) != 0: 

if verbose: 

print("the number of differences (={}) must be a multiple of v-1={}".format(nb_diff, v-1)) 

return False 

l = nb_diff // (v-1) 

else: 

if nb_diff != l*(v-1): 

if verbose: 

print("the number of differences (={}) is not equal to l*(v-1) = {}".format(nb_diff, l*(v-1))) 

return False 

 

# Check that every x \in G-{0},occurs exactly l times as a difference 

counter = {g: 0 for g in Glist} 

where = {g: set() for g in Glist} 

del counter[identity] 

 

for i,d in enumerate(D): 

tmp_counter = {} 

for b in d: 

for c in d: 

if b == c: 

continue 

gg = mul(b,inv(c)) # = b-c or bc^{-1} 

if gg not in tmp_counter: 

tmp_counter[gg] = 0 

where[gg].add(i) 

tmp_counter[gg] += 1 

 

if sum(itervalues(tmp_counter)) != k * (k - 1): 

if verbose: 

print("repeated element in the {}-th block {}".format(i,d)) 

return False 

 

# Normalized number of occurrences added to counter 

stabi = len(stab[i]) 

for gg in tmp_counter: 

counter[gg] += tmp_counter[gg]//stabi 

 

# Check the counter and report any error 

too_few = [] 

too_much = [] 

for g in Glist: 

if g == identity: 

continue 

if counter[g] < l: 

if verbose: 

too_few.append(g) 

else: 

return False 

if counter[g] > l: 

if verbose: 

too_much.append(g) 

else: 

return False 

 

if too_few: 

print("Too few:") 

for g in too_few: 

print(" {} is obtained {} times in blocks {}".format( 

g, counter[g], sorted(where[g]))) 

if too_much: 

print("Too much:") 

for g in too_much: 

print(" {} is obtained {} times in blocks {}".format( 

g, counter[g], sorted(where[g]))) 

if too_few or too_much: 

return False 

 

if verbose: 

print("It is a ({},{},{})-difference family".format(v, k, l)) 

return True 

 

def singer_difference_set(q,d): 

r""" 

Return a difference set associated to the set of hyperplanes in a projective 

space of dimension `d` over `GF(q)`. 

 

A Singer difference set has parameters: 

 

.. MATH:: 

 

v = \frac{q^{d+1}-1}{q-1}, \quad 

k = \frac{q^d-1}{q-1}, \quad 

\lambda = \frac{q^{d-1}-1}{q-1}. 

 

The idea of the construction is as follows. One consider the finite field 

`GF(q^{d+1})` as a vector space of dimension `d+1` over `GF(q)`. The set of 

`GF(q)`-lines in `GF(q^{d+1})` is a projective plane and its set of 

hyperplanes form a balanced incomplete block design. 

 

Now, considering a multiplicative generator `z` of `GF(q^{d+1})`, we get a 

transitive action of a cyclic group on our projective plane from which it is 

possible to build a difference set. 

 

The construction is given in details in [Stinson2004]_, section 3.3. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import singer_difference_set, is_difference_family 

sage: G,D = singer_difference_set(3,2) 

sage: is_difference_family(G,D,verbose=True) 

It is a (13,4,1)-difference family 

True 

 

sage: G,D = singer_difference_set(4,2) 

sage: is_difference_family(G,D,verbose=True) 

It is a (21,5,1)-difference family 

True 

 

sage: G,D = singer_difference_set(3,3) 

sage: is_difference_family(G,D,verbose=True) 

It is a (40,13,4)-difference family 

True 

 

sage: G,D = singer_difference_set(9,3) 

sage: is_difference_family(G,D,verbose=True) 

It is a (820,91,10)-difference family 

True 

""" 

q = Integer(q) 

assert q.is_prime_power() 

assert d >= 2 

 

from sage.rings.finite_rings.finite_field_constructor import GF 

from sage.rings.finite_rings.conway_polynomials import conway_polynomial 

from sage.rings.finite_rings.integer_mod_ring import Zmod 

 

# build a polynomial c over GF(q) such that GF(q)[x] / (c(x)) is a 

# GF(q**(d+1)) and such that x is a multiplicative generator. 

p,e = q.factor()[0] 

c = conway_polynomial(p,e*(d+1)) 

if e != 1: # i.e. q is not a prime, so we factorize c over GF(q) and pick 

# one of its factor 

K = GF(q,'z') 

c = c.change_ring(K).factor()[0][0] 

else: 

K = GF(q) 

z = c.parent().gen() 

 

# Now we consider the GF(q)-subspace V spanned by (1,z,z^2,...,z^(d-1)) inside 

# GF(q^(d+1)). The multiplication by z is an automorphism of the 

# GF(q)-projective space built from GF(q^(d+1)). The difference family is 

# obtained by taking the integers i such that z^i belong to V. 

powers = [0] 

i = 1 

x = z 

k = (q**d-1)//(q-1) 

while len(powers) < k: 

if x.degree() <= (d-1): 

powers.append(i) 

x = (x*z).mod(c) 

i += 1 

 

return Zmod((q**(d+1)-1)//(q-1)), [powers] 

 

def df_q_6_1(K, existence=False, check=True): 

r""" 

Return a `(q,6,1)`-difference family over the finite field `K`. 

 

The construction uses Theorem 11 of [Wi72]_. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import is_difference_family, df_q_6_1 

sage: prime_powers = [v for v in range(31,500,30) if is_prime_power(v)] 

sage: parameters = [v for v in prime_powers if df_q_6_1(GF(v,'a'), existence=True)] 

sage: parameters 

[31, 151, 181, 211, 241, 271, 331, 361, 421] 

sage: for v in parameters: 

....: K = GF(v, 'a') 

....: df = df_q_6_1(K, check=True) 

....: assert is_difference_family(K, df, v, 6, 1) 

 

.. TODO:: 

 

Do improvements due to Zhen and Wu 1999. 

""" 

v = K.cardinality() 

x = K.multiplicative_generator() 

one = K.one() 

if v % 30 != 1: 

if existence: 

return False 

raise EmptySetError("k(k-1)=30 should divide (v-1)") 

t = (v-1) // 30 # number of blocks 

 

r = x**((v-1)//3) # primitive cube root of unity 

r2 = r*r # the other primitive cube root 

 

# we now compute the cosets of x**i 

xx = x**5 

to_coset = {x**i * xx**j: i for i in range(5) for j in range((v-1)/5)} 

 

for c in to_coset: # the loop runs through all nonzero elements of K 

if c == one or c == r or c == r2: 

continue 

if len(set(to_coset[elt] for elt in (r-one, c*(r-one), c-one, c-r, c-r**2))) == 5: 

if existence: 

return True 

B = [one,r,r2,c,c*r,c*r2] 

D = [[xx**i * b for b in B] for i in range(t)] 

break 

else: 

if existence: 

return Unknown 

raise NotImplementedError("Wilson construction failed for v={}".format(v)) 

 

if check and not is_difference_family(K, D, v, 6, 1): 

raise RuntimeError("Wilson 1972 construction failed! Please e-mail sage-devel@googlegroups.com") 

 

return D 

 

def radical_difference_set(K, k, l=1, existence=False, check=True): 

r""" 

Return a difference set made of a cyclotomic coset in the finite field 

``K`` and with parameters ``k`` and ``l``. 

 

Most of these difference sets appear in chapter VI.18.48 of the Handbook of 

combinatorial designs. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import radical_difference_set 

 

sage: D = radical_difference_set(GF(7), 3, 1); D 

[[1, 2, 4]] 

sage: sorted(x-y for x in D[0] for y in D[0] if x != y) 

[1, 2, 3, 4, 5, 6] 

 

sage: D = radical_difference_set(GF(16,'a'), 6, 2) 

sage: sorted(x-y for x in D[0] for y in D[0] if x != y) 

[1, 

1, 

a, 

a, 

a + 1, 

a + 1, 

a^2, 

a^2, 

... 

a^3 + a^2 + a + 1, 

a^3 + a^2 + a + 1] 

 

sage: for k in range(2,50): 

....: for l in reversed(divisors(k*(k-1))): 

....: v = k*(k-1)//l + 1 

....: if is_prime_power(v) and radical_difference_set(GF(v,'a'),k,l,existence=True): 

....: _ = radical_difference_set(GF(v,'a'),k,l) 

....: print("{:3} {:3} {:3}".format(v,k,l)) 

3 2 1 

4 3 2 

7 3 1 

5 4 3 

7 4 2 

13 4 1 

11 5 2 

7 6 5 

11 6 3 

16 6 2 

8 7 6 

9 8 7 

19 9 4 

37 9 2 

73 9 1 

11 10 9 

19 10 5 

23 11 5 

13 12 11 

23 12 6 

27 13 6 

27 14 7 

16 15 14 

31 15 7 

... 

41 40 39 

79 40 20 

83 41 20 

43 42 41 

83 42 21 

47 46 45 

49 48 47 

197 49 12 

""" 

v = K.cardinality() 

 

if l*(v-1) != k*(k-1): 

if existence: 

return False 

raise EmptySetError("l*(v-1) is not equal to k*(k-1)") 

 

# trivial case 

if (v-1) == k: 

if existence: 

return True 

add_zero = False 

 

# q = 3 mod 4 

elif v%4 == 3 and k == (v-1)//2: 

if existence: 

return True 

add_zero = False 

 

# q = 3 mod 4 

elif v%4 == 3 and k == (v+1)//2: 

if existence: 

return True 

add_zero = True 

 

# q = 4t^2 + 1, t odd 

elif v%8 == 5 and k == (v-1)//4 and arith.is_square((v-1)//4): 

if existence: 

return True 

add_zero = False 

 

# q = 4t^2 + 9, t odd 

elif v%8 == 5 and k == (v+3)//4 and arith.is_square((v-9)//4): 

if existence: 

return True 

add_zero = True 

 

# exceptional case 1 

elif (v,k,l) == (16,6,2): 

if existence: 

return True 

add_zero = True 

 

# exceptional case 2 

elif (v,k,l) == (73,9,1): 

if existence: 

return True 

add_zero = False 

 

# are there more ?? 

else: 

x = K.multiplicative_generator() 

D = K.cyclotomic_cosets(x**((v-1)//k), [K.one()]) 

if is_difference_family(K, D, v, k, l): 

print("** You found a new example of radical difference set **\n"\ 

"** for the parameters (v,k,l)=({},{},{}). **\n"\ 

"** Please contact sage-devel@googlegroups.com **\n".format(v, k, l)) 

if existence: 

return True 

add_zero = False 

 

else: 

D = K.cyclotomic_cosets(x**((v-1)//(k-1)), [K.one()]) 

D[0].insert(0,K.zero()) 

if is_difference_family(K, D, v, k, l): 

print("** You found a new example of radical difference set **\n"\ 

"** for the parameters (v,k,l)=({},{},{}). **\n"\ 

"** Please contact sage-devel@googlegroups.com **\n".format(v, k, l)) 

if existence: 

return True 

add_zero = True 

 

elif existence: 

return False 

else: 

raise EmptySetError("no radical difference set exist " 

"for the parameters (v,k,l) = ({},{},{}".format(v,k,l)) 

 

x = K.multiplicative_generator() 

if add_zero: 

r = x**((v-1)//(k-1)) 

D = K.cyclotomic_cosets(r, [K.one()]) 

D[0].insert(0, K.zero()) 

else: 

r = x**((v-1)//k) 

D = K.cyclotomic_cosets(r, [K.one()]) 

 

if check and not is_difference_family(K, D, v, k, l): 

raise RuntimeError("Sage tried to build a radical difference set with " 

"parameters ({},{},{}) but it seems that it failed! Please " 

"e-mail sage-devel@googlegroups.com".format(v,k,l)) 

 

return D 

 

def one_cyclic_tiling(A,n): 

r""" 

Given a subset ``A`` of the cyclic additive group `G = Z / nZ` return 

another subset `B` so that `A + B = G` and `|A| |B| = n` (i.e. any element 

of `G` is uniquely expressed as a sum `a+b` with `a` in `A` and `b` in `B`). 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import one_cyclic_tiling 

sage: tile = [0,2,4] 

sage: m = one_cyclic_tiling(tile,6); m 

[0, 3] 

sage: sorted((i+j)%6 for i in tile for j in m) 

[0, 1, 2, 3, 4, 5] 

 

sage: def print_tiling(tile, translat, n): 

....: for x in translat: 

....: print(''.join('X' if (i-x)%n in tile else '.' for i in range(n))) 

 

sage: tile = [0, 1, 2, 7] 

sage: m = one_cyclic_tiling(tile, 12) 

sage: print_tiling(tile, m, 12) 

XXX....X.... 

....XXX....X 

...X....XXX. 

 

sage: tile = [0, 1, 5] 

sage: m = one_cyclic_tiling(tile, 12) 

sage: print_tiling(tile, m, 12) 

XX...X...... 

...XX...X... 

......XX...X 

..X......XX. 

 

sage: tile = [0, 2] 

sage: m = one_cyclic_tiling(tile, 8) 

sage: print_tiling(tile, m, 8) 

X.X..... 

....X.X. 

.X.X.... 

.....X.X 

 

ALGORITHM: 

 

Uses dancing links :mod:`sage.combinat.dlx` 

""" 

# we first try a naive approach which correspond to what Wilson used in his 

# 1972 article 

n = int(n) 

d = len(A) 

if len(set(a%d for a in A)) == d: 

return [i*d for i in range(n//d)] 

 

# next, we consider an exhaustive search 

from sage.combinat.dlx import DLXMatrix 

 

rows = [] 

for i in range(n): 

rows.append([i+1, [(i+a)%n+1 for a in A]]) 

M = DLXMatrix(rows) 

for c in M: 

return [i-1 for i in c] 

 

def one_radical_difference_family(K, k): 

r""" 

Search for a radical difference family on ``K`` using dancing links 

algorithm. 

 

For the definition of radical difference family, see 

:func:`radical_difference_family`. Here, we consider only radical difference 

family with `\lambda = 1`. 

 

INPUT: 

 

- ``K`` -- a finite field of cardinality `q`. 

 

- ``k`` -- a positive integer so that `k(k-1)` divides `q-1`. 

 

OUTPUT: 

 

Either a difference family or ``None`` if it does not exist. 

 

ALGORITHM: 

 

The existence of a radical difference family is equivalent to a one 

dimensional tiling (or packing) problem in a cyclic group. This subsequent 

problem is solved by a call to the function :func:`one_cyclic_tiling`. 

 

Let `K^*` be the multiplicative group of the finite field `K`. A radical 

family has the form `\mathcal B = \{x_1 B, \ldots, x_k B\}`, where 

`B=\{x:x^{k}=1\}` (for `k` odd) or `B=\{x:x^{k-1}=1\}\cup \{0\}` (for 

`k` even). Equivalently, `K^*` decomposes as: 

 

.. MATH:: 

 

K^* = \Delta (x_1 B) \cup ... \cup \Delta (x_k B) = x_1 \Delta B \cup ... \cup x_k \Delta B 

 

We observe that `C=B\backslash 0` is a subgroup of the (cyclic) group 

`K^*`, that can thus be generated by some element `r`. Furthermore, we 

observe that `\Delta B` is always a union of cosets of `\pm C` (which is 

twice larger than `C`). 

 

.. MATH:: 

 

\begin{array}{llll} 

(k\text{ odd} ) & \Delta B &= \{r^i-r^j:r^i\neq r^j\} &= \pm C\cdot \{r^i-1: 0 < i \leq m\}\\ 

(k\text{ even}) & \Delta B &= \{r^i-r^j:r^i\neq r^j\}\cup C &= \pm C\cdot \{r^i-1: 0 < i < m\}\cup \pm C 

\end{array} 

 

where 

 

.. MATH:: 

 

(k\text{ odd})\ m = (k-1)/2 \quad \text{and} \quad (k\text{ even})\ m = k/2. 

 

Consequently, `\mathcal B = \{x_1 B, \ldots, x_k B\}` is a radical 

difference family if and only if `\{x_1 (\Delta B/(\pm C)), \ldots, x_k 

(\Delta B/(\pm C))\}` is a partition of the cyclic group `K^*/(\pm C)`. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import ( 

....: one_radical_difference_family, 

....: is_difference_family) 

 

sage: one_radical_difference_family(GF(13),4) 

[[0, 1, 3, 9]] 

 

The parameters that appear in [Bu95]_:: 

 

sage: df = one_radical_difference_family(GF(449), 8); df 

[[0, 1, 18, 25, 176, 324, 359, 444], 

[0, 9, 88, 162, 222, 225, 237, 404], 

[0, 11, 140, 198, 275, 357, 394, 421], 

[0, 40, 102, 249, 271, 305, 388, 441], 

[0, 49, 80, 93, 161, 204, 327, 433], 

[0, 70, 99, 197, 230, 362, 403, 435], 

[0, 121, 141, 193, 293, 331, 335, 382], 

[0, 191, 285, 295, 321, 371, 390, 392]] 

sage: is_difference_family(GF(449), df, 449, 8, 1) 

True 

""" 

q = K.cardinality() 

x = K.multiplicative_generator() 

 

e = k*(k-1) 

if q%e != 1: 

raise ValueError("q%e is not 1") 

 

# We define A by (see the function's documentation): 

# ΔB = C.A 

if k%2 == 1: 

m = (k-1) // 2 

r = x ** ((q-1) // k) # k-th root of unity 

A = [r**i - 1 for i in range(1,m+1)] 

else: 

m = k // 2 

r = x ** ((q-1) // (k-1)) # (k-1)-th root of unity 

A = [r**i - 1 for i in range(1,m)] 

A.append(K.one()) 

 

# instead of the complicated multiplicative group K^*/(±C) we use the 

# discrete logarithm to convert everything into the additive group Z/cZ 

c = m * (q-1) // e # cardinal of ±C 

from sage.groups.generic import discrete_log 

logA = [discrete_log(a,x)%c for a in A] 

 

# if two elments of A are equal modulo c then no tiling is possible 

if len(set(logA)) != m: 

return None 

 

# brute force 

tiling = one_cyclic_tiling(logA, c) 

if tiling is None: 

return None 

 

D = K.cyclotomic_cosets(r, [x**i for i in tiling]) 

if k%2 == 0: 

for d in D: 

d.insert(K.zero(),0) 

return D 

 

def radical_difference_family(K, k, l=1, existence=False, check=True): 

r""" 

Return a ``(v,k,l)``-radical difference family. 

 

Let fix an integer `k` and a prime power `q = t k(k-1) + 1`. Let `K` be a 

field of cardinality `q`. A `(q,k,1)`-difference family is *radical* if 

its base blocks are either: a coset of the `k`-th root of unity for `k` odd 

or a coset of `k-1`-th root of unity and `0` if `k` is even (the number `t` 

is the number of blocks of that difference family). 

 

The terminology comes from M. Buratti article [Bu95]_ but the first 

constructions go back to R. Wilson [Wi72]_. 

 

INPUT: 

 

- ``K`` - a finite field 

 

- ``k`` -- positive integer, the size of the blocks 

 

- ``l`` -- the `\lambda` parameter (default to `1`) 

 

- ``existence`` -- if ``True``, then return either ``True`` if Sage knows 

how to build such design, ``Unknown`` if it does not and ``False`` if it 

knows that the design does not exist. 

 

- ``check`` -- boolean (default: ``True``). If ``True`` then the result of 

the computation is checked before being returned. This should not be 

needed but ensures that the output is correct. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import radical_difference_family 

 

sage: radical_difference_family(GF(73),9) 

[[1, 2, 4, 8, 16, 32, 37, 55, 64]] 

 

sage: radical_difference_family(GF(281),5) 

[[1, 86, 90, 153, 232], 

[4, 50, 63, 79, 85], 

[5, 36, 149, 169, 203], 

[7, 40, 68, 219, 228], 

[9, 121, 212, 248, 253], 

[29, 81, 222, 246, 265], 

[31, 137, 167, 247, 261], 

[32, 70, 118, 119, 223], 

[39, 56, 66, 138, 263], 

[43, 45, 116, 141, 217], 

[98, 101, 109, 256, 279], 

[106, 124, 145, 201, 267], 

[111, 123, 155, 181, 273], 

[156, 209, 224, 264, 271]] 

 

sage: for k in range(5,10): 

....: print("k = {}".format(k)) 

....: list_q = [] 

....: for q in range(k*(k-1)+1, 2000, k*(k-1)): 

....: if is_prime_power(q): 

....: K = GF(q,'a') 

....: if radical_difference_family(K, k, existence=True): 

....: list_q.append(q) 

....: _ = radical_difference_family(K,k) 

....: print(" ".join([str(p) for p in list_q])) 

k = 5 

41 61 81 241 281 401 421 601 641 661 701 761 821 881 1181 1201 1301 1321 

1361 1381 1481 1601 1681 1801 1901 

k = 6 

181 211 241 631 691 1531 1831 1861 

k = 7 

337 421 463 883 1723 

k = 8 

449 1009 

k = 9 

73 1153 1873 

""" 

v = K.cardinality() 

x = K.multiplicative_generator() 

one = K.one() 

e = k*(k-1) 

if (l*(v-1)) % e: 

raise ValueError("k (k-1) = {} should be a multiple of l (v-1) ={}".format( 

k*(k-1), l*(v-1))) 

t = l*(v-1) // e # number of blocks 

 

if t == 1: 

return radical_difference_set(K, k, l, existence=existence, check=check) 

 

elif l == (k-1): 

if existence: 

return True 

else: 

return K.cyclotomic_cosets(x**((v-1)//k))[1:] 

 

# all the other cases below concern the case l == 1 

elif l != 1: 

if existence: 

return Unknown 

raise NotImplementedError("No radical families implemented for l > 2") 

 

else: 

D = one_radical_difference_family(K,k) 

if D is None: 

if existence: 

return False 

raise EmptySetError("No such difference family") 

elif existence: 

return True 

 

 

if check and not is_difference_family(K, D, v, k, l): 

raise RuntimeError("radical_difference_family produced a wrong " 

"difference family with parameters v={}, " 

"k={}, l={}. Please contact " 

"sage-devel@googlegroups.com".format(v,k,l)) 

 

return D 

 

def twin_prime_powers_difference_set(p, check=True): 

r""" 

Return a difference set on `GF(p) \times GF(p+2)`. 

 

The difference set is built from the following element of the Cartesian 

product of finite fields `GF(p) \times GF(p+2)`: 

 

- `(x,0)` with any `x` 

- `(x,y)` with `x` and `y` squares 

- `(x,y)` with `x` and `y` non-squares 

 

For more information see :wikipedia:`Difference_set`. 

 

INPUT: 

 

- ``check`` -- boolean (default: ``True``). If ``True`` then the result of 

the computation is checked before being returned. This should not be 

needed but ensures that the output is correct. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import twin_prime_powers_difference_set 

sage: G,D = twin_prime_powers_difference_set(3) 

sage: G 

The Cartesian product of (Finite Field of size 3, Finite Field of size 5) 

sage: D 

[[(1, 1), (1, 4), (2, 2), (2, 3), (0, 0), (1, 0), (2, 0)]] 

""" 

from sage.rings.finite_rings.finite_field_constructor import FiniteField 

from sage.categories.cartesian_product import cartesian_product 

from itertools import product 

Fp = FiniteField(p,'x') 

Fq = FiniteField(p+2,'x') 

Fpset = set(Fp) 

Fqset = set(Fq) 

Fp_squares = set(x**2 for x in Fpset) 

Fq_squares = set(x**2 for x in Fqset) 

 

# Pairs of squares, pairs of non-squares 

d = [] 

d.extend(product(Fp_squares.difference([0]),Fq_squares.difference([0]))) 

d.extend(product(Fpset.difference(Fp_squares),Fqset.difference(Fq_squares))) 

 

# All (x,0) 

d.extend((x,0) for x in Fpset) 

 

G = cartesian_product([Fp,Fq]) 

 

if check and not is_difference_family(G, [d]): 

raise RuntimeError("twin_prime_powers_difference_set produced a wrong " 

"difference set with p={}. Please contact " 

"sage-devel@googlegroups.com".format(p)) 

 

return G, [d] 

 

def are_mcfarland_1973_parameters(v, k, lmbda, return_parameters=False): 

r""" 

Test whether ``(v,k,lmbda)`` is a triple that can be obtained from the 

construction from [McF1973]_. 

 

See :func:`mcfarland_1973_construction`. 

 

INPUT: 

 

- ``v``, ``k``, ``lmbda`` - (integers) parameters of the difference family 

 

- ``return_parameters`` -- (boolean, default ``False``) if ``True`` return a 

pair ``(True, (q, s))`` so that ``(q,s)`` can be used in the function 

:func:`mcfarland_1973_construction` to actually build a 

``(v,k,lmbda)``-difference family. Or ``(False, None)`` if the 

construction is not possible. 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import are_mcfarland_1973_parameters 

sage: are_mcfarland_1973_parameters(64, 28, 12) 

True 

sage: are_mcfarland_1973_parameters(64, 28, 12, return_parameters=True) 

(True, (2, 2)) 

sage: are_mcfarland_1973_parameters(60, 13, 5) 

False 

sage: are_mcfarland_1973_parameters(98125, 19500, 3875) 

True 

sage: are_mcfarland_1973_parameters(98125, 19500, 3875, True) 

(True, (5, 3)) 

 

sage: from sage.combinat.designs.difference_family import are_mcfarland_1973_parameters 

sage: for v in range(1, 100): 

....: for k in range(1,30): 

....: for l in range(1,15): 

....: if are_mcfarland_1973_parameters(v,k,l): 

....: answer, (q,s) = are_mcfarland_1973_parameters(v,k,l,return_parameters=True) 

....: print("{} {} {} {} {}".format(v,k,l,q,s)) 

....: assert answer is True 

....: assert designs.difference_family(v,k,l,existence=True) is True 

....: G,D = designs.difference_family(v,k,l) 

16 6 2 2 1 

45 12 3 3 1 

64 28 12 2 2 

96 20 4 4 1 

""" 

if v <= k or k <= lmbda: 

return (False,None) if return_parameters else False 

k = ZZ(k) 

lmbda = ZZ(lmbda) 

qs,r = (k - lmbda).sqrtrem() # sqrt(k-l) should be q^s 

if r or (qs*(qs-1))%lmbda: 

return (False,None) if return_parameters else False 

 

q = qs*(qs-1) // lmbda + 1 

if (q <= 1 or 

v * (q-1) != qs*q * (qs*q+q-2) or 

k * (q-1)!= qs * (qs*q-1)): 

return (False,None) if return_parameters else False 

 

# NOTE: below we compute the value of s so that qs = q^s. If the method 

# is_power_of of integers would be able to return the exponent, we could use 

# that... but currently this is not the case 

# see trac ticket #19792 

p1,a1 = qs.is_prime_power(get_data=True) 

p2,a2 = q.is_prime_power(get_data=True) 

 

if a1 == 0 or a2 == 0 or p1 != p2 or a1%a2: 

return (False,None) if return_parameters else False 

 

return (True, (q, a1//a2)) if return_parameters else True 

 

def mcfarland_1973_construction(q, s): 

r""" 

Return a difference set. 

 

The difference set returned has the following parameters 

 

.. MATH:: 

 

v = \frac{q^{s+1}(q^{s+1}+q-2)}{q-1}, 

k = \frac{q^s (q^{s+1}-1)}{q-1}, 

\lambda = \frac{q^s(q^s-1)}{q-1} 

 

This construction is due to [McF1973]_. 

 

INPUT: 

 

- ``q``, ``s`` - (integers) parameters for the difference set (see the above 

formulas for the expression of ``v``, ``k``, ``l`` in terms of ``q`` and 

``s``) 

 

.. SEEALSO:: 

 

The function :func:`are_mcfarland_1973_parameters` makes the translation 

between the parameters `(q,s)` corresponding to a given triple 

`(v,k,\lambda)`. 

 

REFERENCES: 

 

.. [McF1973] Robert L. McFarland 

"A family of difference sets in non-cyclic groups" 

J. Combinatorial Theory (A) 15 (1973) 1--10. 

:doi:`10.1016/0097-3165(73)90031-9` 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import ( 

....: mcfarland_1973_construction, is_difference_family) 

 

sage: G,D = mcfarland_1973_construction(3, 1) 

sage: assert is_difference_family(G, D, 45, 12, 3) 

 

sage: G,D = mcfarland_1973_construction(2, 2) 

sage: assert is_difference_family(G, D, 64, 28, 12) 

""" 

from sage.rings.finite_rings.finite_field_constructor import GF 

from sage.modules.free_module import VectorSpace 

from sage.rings.finite_rings.integer_mod_ring import Zmod 

from sage.categories.cartesian_product import cartesian_product 

 

r = (q**(s+1)-1) // (q-1) 

F = GF(q,'a') 

V = VectorSpace(F, s+1) 

K = Zmod(r+1) 

 

G = cartesian_product([F]*(s+1) + [K]) 

 

D = [] 

for k, H in zip(K, V.subspaces(s)): 

for v in H: 

D.append(G((tuple(v) + (k,)))) 

 

return G,[D] 

 

def are_hadamard_difference_set_parameters(v, k, lmbda): 

r""" 

Check whether ``(v,k,lmbda)`` is of the form ``(4N^2, 2N^2 - N, N^2 - N)``. 

 

INPUT: 

 

- ``(v,k,lmbda)`` -- parameters of a difference set 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import are_hadamard_difference_set_parameters 

sage: are_hadamard_difference_set_parameters(36, 15, 6) 

True 

sage: are_hadamard_difference_set_parameters(60, 13, 5) 

False 

""" 

N = k - 2*lmbda 

N2 = N*N 

return v == 4*N2 and k == 2*N2 - N and lmbda == N2 - N 

 

@cached_method 

def hadamard_difference_set_product_parameters(N): 

r""" 

Check whether a product construction is available for Hadamard difference 

set with parameter ``N``. 

 

This function looks for two integers `N_1` and `N_2`` greater than `1` 

and so that `N = 2 N_1 N_2` and there exists Hadamard difference set with 

parameters `(4 N_i^2, 2N_i^2 - N_i, N_i^2 - N_i)`. If such pair exists, 

the output is the pair ``(N_1, N_2)`` otherwise it is ``None``. 

 

INPUT: 

 

- ``N`` -- positive integer 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import hadamard_difference_set_product_parameters 

sage: hadamard_difference_set_product_parameters(8) 

(2, 2) 

""" 

if N % 2: 

return False 

 

for N1 in (N//2).divisors()[1:]: 

if 4*N1 > N: 

break 

v1 = 4*N1*N1 

k1 = 2*N1*N1 - N1 

l1 = N1*N1 - N1 

if not difference_family(v1, k1, l1, existence=True): 

continue 

N2 = N // (2*N1) 

v2 = 4*N2*N2 

k2 = 2*N2*N2 - N2 

l2 = N2*N2 - N2 

if not difference_family(v2, k2, l2, existence=True): 

continue 

 

return (N1,N2) 

 

return None 

 

def hadamard_difference_set_product(G1, D1, G2, D2): 

r""" 

Make a product of two Hadamard difference sets. 

 

This product construction appears in [Tu1984]_. 

 

INPUT: 

 

- ``G1,D1``, ``G2,D2`` -- two Hadamard difference sets 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import hadamard_difference_set_product 

sage: from sage.combinat.designs.difference_family import is_difference_family 

 

sage: G1,D1 = designs.difference_family(16,6,2) 

sage: G2,D2 = designs.difference_family(36,15,6) 

 

sage: G11,D11 = hadamard_difference_set_product(G1,D1,G1,D1) 

sage: assert is_difference_family(G11, D11, 256, 120, 56) 

sage: assert designs.difference_family(256, 120, 56, existence=True) 

 

sage: G12,D12 = hadamard_difference_set_product(G1,D1,G2,D2) 

sage: assert is_difference_family(G12, D12, 576, 276, 132) 

sage: assert designs.difference_family(576, 276, 132, existence=True) 

""" 

from sage.categories.cartesian_product import cartesian_product 

 

G = cartesian_product([G1,G2]) 

D1 = set(D1[0]) 

D1c = set(s for s in G1 if s not in D1) 

D2 = set(D2[0]) 

D2c = set(s for s in G2 if s not in D2) 

 

D = set().union((G((s1,s2)) for s1 in D1 for s2 in D2), 

(G((s1,s2)) for s1 in D1c for s2 in D2c)) 

 

return G, [[s for s in G if s not in D]] 

 

def turyn_1965_3x3xK(k=4): 

r""" 

Return a difference set in either `C_3 \times C_3 \times C_4` or `C_3 \times 

C_3 \times C_2 \times C_2` with parameters `v=36`, `k=15`, `\lambda=6`. 

 

This example appears in [Tu1965]_. 

 

INPUT: 

 

- ``k`` -- either ``2`` (to get a difference set in `C_3 \times C_3 \times 

C_2 \times C_2`) or ``4`` (to get a difference set in `C_3 \times C_3 

\times C_3 \times C_4`). 

 

EXAMPLES:: 

 

sage: from sage.combinat.designs.difference_family import turyn_1965_3x3xK 

sage: from sage.combinat.designs.difference_family import is_difference_family 

sage: G,D = turyn_1965_3x3xK(4) 

sage: assert is_difference_family(G, D, 36, 15, 6) 

sage: G,D = turyn_1965_3x3xK(2) 

sage: assert is_difference_family(G, D, 36, 15, 6) 

""" 

from sage.categories.cartesian_product import cartesian_product 

from sage.rings.finite_rings.integer_mod_ring import Zmod 

 

if k == 2: 

G = cartesian_product([Zmod(3), Zmod(3), Zmod(2), Zmod(2)]) 

K = [(0,0), (0,1), (1,0), (1,1)] 

elif k == 4: 

G = cartesian_product([Zmod(3), Zmod(3), Zmod(4)]) 

K = [(0,), (1,), (2,), (3,)] 

else: 

raise ValueError("k must be 2 or 4") 

 

L = [[(0,1),(1,1),(2,1),(0,2),(1,2),(2,2)], # complement of y=0 

[(0,0),(1,1),(2,2)], # x-y=0 

[(0,0),(1,2),(2,1)], # x+y=0 

[(0,0),(0,1),(0,2)]] # x=0 

 

return G, [[G(v + k) for l, k in zip(L, K) for v in l]] 

 

 

def difference_family(v, k, l=1, existence=False, explain_construction=False, check=True): 

r""" 

Return a (``k``, ``l``)-difference family on an Abelian group of cardinality ``v``. 

 

Let `G` be a finite Abelian group. For a given subset `D` of `G`, we define 

`\Delta D` to be the multi-set of differences `\Delta D = \{x - y; x \in D, 

y \in D, x \not= y\}`. A `(G,k,\lambda)`-*difference family* is a collection 

of `k`-subsets of `G`, `D = \{D_1, D_2, \ldots, D_b\}` such that the union 

of the difference sets `\Delta D_i` for `i=1,...b`, seen as a multi-set, 

contains each element of `G \backslash \{0\}` exactly `\lambda`-times. 

 

When there is only one block, i.e. `\lambda(v - 1) = k(k-1)`, then a 

`(G,k,\lambda)`-difference family is also called a *difference set*. 

 

See also :wikipedia:`Difference_set`. 

 

If there is no such difference family, an ``EmptySetError`` is raised and if 

there is no construction at the moment ``NotImplementedError`` is raised. 

 

INPUT: 

 

- ``v,k,l`` -- parameters of the difference family. If ``l`` is not provided 

it is assumed to be ``1``. 

 

- ``existence`` -- if ``True``, then return either ``True`` if Sage knows 

how to build such design, ``Unknown`` if it does not and ``False`` if it 

knows that the design does not exist. 

 

- ``explain_construction`` -- instead of returning a difference family, 

returns a string that explains the construction used. 

 

- ``check`` -- boolean (default: ``True``). If ``True`` then the result of 

the computation is checked before being returned. This should not be 

needed but ensures that the output is correct. 

 

OUTPUT: 

 

A pair ``(G,D)`` made of a group `G` and a difference family `D` on that 

group. Or, if ``existence`` is ``True`` a troolean or if 

``explain_construction`` is ``True`` a string. 

 

EXAMPLES:: 

 

sage: G,D = designs.difference_family(73,4) 

sage: G 

Finite Field of size 73 

sage: D 

[[0, 1, 5, 18], 

[0, 3, 15, 54], 

[0, 9, 45, 16], 

[0, 27, 62, 48], 

[0, 8, 40, 71], 

[0, 24, 47, 67]] 

 

sage: print(designs.difference_family(73, 4, explain_construction=True)) 

The database contains a (73,4)-evenly distributed set 

 

sage: G,D = designs.difference_family(15,7,3) 

sage: G 

Ring of integers modulo 15 

sage: D 

[[0, 1, 2, 4, 5, 8, 10]] 

sage: print(designs.difference_family(15,7,3,explain_construction=True)) 

Singer difference set 

 

sage: print(designs.difference_family(91,10,1,explain_construction=True)) 

Singer difference set 

sage: print(designs.difference_family(64,28,12, explain_construction=True)) 

McFarland 1973 construction 

sage: print(designs.difference_family(576, 276, 132, explain_construction=True)) 

Hadamard difference set product from N1=2 and N2=3 

 

For `k=6,7` we look at the set of small prime powers for which a 

construction is available:: 

 

sage: def prime_power_mod(r,m): 

....: k = m+r 

....: while True: 

....: if is_prime_power(k): 

....: yield k 

....: k += m 

 

sage: from itertools import islice 

sage: l6 = {True:[], False: [], Unknown: []} 

sage: for q in islice(prime_power_mod(1,30), 60): 

....: l6[designs.difference_family(q,6,existence=True)].append(q) 

sage: l6[True] 

[31, 121, 151, 181, 211, ..., 3061, 3121, 3181] 

sage: l6[Unknown] 

[61] 

sage: l6[False] 

[] 

 

sage: l7 = {True: [], False: [], Unknown: []} 

sage: for q in islice(prime_power_mod(1,42), 60): 

....: l7[designs.difference_family(q,7,existence=True)].append(q) 

sage: l7[True] 

[169, 337, 379, 421, 463, 547, 631, 673, 757, 841, 883, 967, ..., 4621, 4957, 5167] 

sage: l7[Unknown] 

[43, 127, 211, 2017, 2143, 2269, 2311, 2437, 2521, 2647, ..., 4999, 5041, 5209] 

sage: l7[False] 

[] 

 

List available constructions:: 

 

sage: for v in range(2,100): 

....: constructions = [] 

....: for k in range(2,10): 

....: for l in range(1,10): 

....: if designs.difference_family(v,k,l,existence=True): 

....: constructions.append((k,l)) 

....: _ = designs.difference_family(v,k,l) 

....: if constructions: 

....: print("%2d: %s"%(v, ', '.join('(%d,%d)'%(k,l) for k,l in constructions))) 

3: (2,1) 

4: (3,2) 

5: (2,1), (4,3) 

6: (5,4) 

7: (2,1), (3,1), (3,2), (4,2), (6,5) 

8: (7,6) 

9: (2,1), (4,3), (8,7) 

10: (9,8) 

11: (2,1), (4,6), (5,2), (5,4), (6,3) 

13: (2,1), (3,1), (3,2), (4,1), (4,3), (5,5), (6,5) 

15: (3,1), (4,6), (5,6), (7,3) 

16: (3,2), (5,4), (6,2) 

17: (2,1), (4,3), (5,5), (8,7) 

19: (2,1), (3,1), (3,2), (4,2), (6,5), (9,4), (9,8) 

21: (3,1), (4,3), (5,1), (6,3), (6,5) 

22: (4,2), (6,5), (7,4), (8,8) 

23: (2,1) 

25: (2,1), (3,1), (3,2), (4,1), (4,3), (6,5), (7,7), (8,7) 

27: (2,1), (3,1) 

28: (3,2), (6,5) 

29: (2,1), (4,3), (7,3), (7,6), (8,4), (8,6) 

31: (2,1), (3,1), (3,2), (4,2), (5,2), (5,4), (6,1), (6,5) 

33: (3,1), (5,5), (6,5) 

34: (4,2) 

35: (5,2) 

37: (2,1), (3,1), (3,2), (4,1), (4,3), (6,5), (9,2), (9,8) 

39: (3,1), (6,5) 

40: (3,2), (4,1) 

41: (2,1), (4,3), (5,1), (5,4), (6,3), (8,7) 

43: (2,1), (3,1), (3,2), (4,2), (6,5), (7,2), (7,3), (7,6), (8,4) 

45: (3,1), (5,1) 

46: (4,2), (6,2) 

47: (2,1) 

49: (2,1), (3,1), (3,2), (4,1), (4,3), (6,5), (8,7), (9,3) 

51: (3,1), (5,2), (6,3) 

52: (4,1) 

53: (2,1), (4,3) 

55: (3,1), (9,4) 

57: (3,1), (7,3), (8,1) 

59: (2,1) 

61: (2,1), (3,1), (3,2), (4,1), (4,3), (5,1), (5,4), (6,2), (6,3), (6,5) 

63: (3,1) 

64: (3,2), (4,1), (7,2), (7,6), (9,8) 

65: (5,1) 

67: (2,1), (3,1), (3,2), (6,5) 

69: (3,1) 

71: (2,1), (5,2), (5,4), (7,3), (7,6), (8,4) 

73: (2,1), (3,1), (3,2), (4,1), (4,3), (6,5), (8,7), (9,1), (9,8) 

75: (3,1), (5,2) 

76: (4,1) 

79: (2,1), (3,1), (3,2), (6,5) 

81: (2,1), (3,1), (4,3), (5,1), (5,4), (8,7) 

83: (2,1) 

85: (4,1), (7,2), (7,3), (8,2) 

89: (2,1), (4,3), (8,7) 

91: (6,1), (7,1) 

97: (2,1), (3,1), (3,2), (4,1), (4,3), (6,5), (8,7), (9,3) 

 

TESTS: 

 

Check more of the Wilson constructions from [Wi72]_:: 

 

sage: Q5 = [241, 281,421,601,641, 661, 701, 821,881] 

sage: Q9 = [73, 1153, 1873, 2017] 

sage: Q15 = [76231] 

sage: Q4 = [13, 73, 97, 109, 181, 229, 241, 277, 337, 409, 421, 457] 

sage: Q8 = [1009, 3137, 3697] 

sage: for Q,k in [(Q4,4),(Q5,5),(Q8,8),(Q9,9),(Q15,15)]: 

....: for q in Q: 

....: assert designs.difference_family(q,k,1,existence=True) is True 

....: _ = designs.difference_family(q,k,1) 

 

Check Singer difference sets:: 

 

sage: sgp = lambda q,d: ((q**(d+1)-1)//(q-1), (q**d-1)//(q-1), (q**(d-1)-1)//(q-1)) 

 

sage: for q in range(2,10): 

....: if is_prime_power(q): 

....: for d in [2,3,4]: 

....: v,k,l = sgp(q,d) 

....: assert designs.difference_family(v,k,l,existence=True) is True 

....: _ = designs.difference_family(v,k,l) 

 

Check twin primes difference sets:: 

 

sage: for p in [3,5,7,9,11]: 

....: v = p*(p+2); k = (v-1)/2; lmbda = (k-1)/2 

....: G,D = designs.difference_family(v,k,lmbda) 

 

Check the database:: 

 

sage: from sage.combinat.designs.database import DF,EDS 

sage: for v,k,l in DF: 

....: assert designs.difference_family(v,k,l,existence=True) is True 

....: df = designs.difference_family(v,k,l,check=True) 

 

sage: for k in EDS: 

....: for v in EDS[k]: 

....: assert designs.difference_family(v,k,1,existence=True) is True 

....: df = designs.difference_family(v,k,1,check=True) 

 

Check the known Hadamard parameters:: 

 

sage: for N in range(2,21): 

....: v = 4*N^2; k = 2*N^2-N; l = N^2-N 

....: status = designs.difference_family(v,k,l,existence=True) 

....: print("{:2} {}".format(N,designs.difference_family(v,k,l,explain_construction=True) if status else status)) 

2 McFarland 1973 construction 

3 Turyn 1965 construction 

4 McFarland 1973 construction 

5 False 

6 Unknown 

7 False 

8 McFarland 1973 construction 

9 Unknown 

10 Unknown 

11 False 

12 Hadamard difference set product from N1=2 and N2=3 

13 False 

14 Unknown 

15 Unknown 

16 McFarland 1973 construction 

17 False 

18 Hadamard difference set product from N1=3 and N2=3 

19 False 

20 Unknown 

 

Check a failing construction (:trac:`17528`):: 

 

sage: designs.difference_family(9,3) 

Traceback (most recent call last): 

... 

NotImplementedError: No construction available for (9,3,1)-difference family 

 

Check that when ``existence=True`` we always obtain ``True``, ``False`` or ``Unknown`` 

and when ``explain_construction=True``it is a string (see :trac:`24513`):: 

 

sage: designs.difference_family(3, 2, 1, existence=True) 

True 

sage: designs.difference_family(3, 2, 1, explain_construction=True) 

'Trivial difference family' 

 

sage: for _ in range(100): 

....: v = randint(1, 30) 

....: k = randint(2, 30) 

....: l = randint(1, 30) 

....: res = designs.difference_family(v, k, l, existence=True) 

....: assert res is True or res is False or res is Unknown 

....: if res is True: 

....: assert isinstance(designs.difference_family(3, 2, 1, explain_construction=True), str) 

 

.. TODO:: 

 

Implement recursive constructions from Buratti "Recursive for difference 

matrices and relative difference families" (1998) and Jungnickel 

"Composition theorems for difference families and regular planes" (1978) 

""" 

from .block_design import are_hyperplanes_in_projective_geometry_parameters 

 

from .database import DF, EDS 

 

v = ZZ(v) 

k = ZZ(k) 

l = ZZ(l) 

 

if (v,k,l) in DF: 

if existence: 

return True 

elif explain_construction: 

return "The database contains a ({},{},{})-difference family".format(v,k,l) 

 

vv, blocks = next(six.iteritems(DF[v,k,l])) 

 

# Build the group 

from sage.rings.finite_rings.integer_mod_ring import Zmod 

if len(vv) == 1: 

G = Zmod(vv[0]) 

else: 

from sage.categories.cartesian_product import cartesian_product 

G = cartesian_product([Zmod(i) for i in vv]) 

 

df = [[G(i) for i in b] for b in blocks] 

 

if check and not is_difference_family(G, df, v=v, k=k, l=l): 

raise RuntimeError("There is an invalid ({},{},{})-difference " 

"family in the database... Please contact " 

"sage-devel@googlegroups.com".format(v,k,l)) 

 

return G,df 

 

elif l == 1 and k in EDS and v in EDS[k]: 

if existence: 

return True 

elif explain_construction: 

return "The database contains a ({},{})-evenly distributed set".format(v,k) 

 

from sage.rings.finite_rings.finite_field_constructor import GF 

poly,B = EDS[k][v] 

if poly is None: # q is prime 

K = G = GF(v) 

else: 

K = G = GF(v,'a',modulus=poly) 

 

B = map(K,B) 

e = k*(k-1)//2 

xe = G.multiplicative_generator()**e 

df = [[xe**j*b for b in B] for j in range((v-1)//(2*e))] 

if check and not is_difference_family(G, df, v=v, k=k, l=l): 

raise RuntimeError("There is an invalid ({},{})-evenly distributed " 

"set in the database... Please contact " 

"sage-devel@googlegroups.com".format(v,k,l)) 

return G,df 

 

e = k*(k-1) 

if (l*(v-1)) % e: 

if existence: 

return Unknown 

raise NotImplementedError("No construction available for ({},{},{})-difference family".format(v,k,l)) 

t = l*(v-1) // e # number of blocks 

 

# trivial construction 

if k == (v-1) and l == (v-2): 

if existence: 

return True 

elif explain_construction: 

return "Trivial difference family" 

 

from sage.rings.finite_rings.integer_mod_ring import Zmod 

G = Zmod(v) 

return G, [list(range(1, v))] 

 

factorization = arith.factor(v) 

if len(factorization) == 1: 

from sage.rings.finite_rings.finite_field_constructor import GF 

K = GF(v,'z') 

 

if are_mcfarland_1973_parameters(v,k,l): 

if existence: 

return True 

elif explain_construction: 

return "McFarland 1973 construction" 

else: 

_, (q,s) = are_mcfarland_1973_parameters(v,k,l,True) 

G,D = mcfarland_1973_construction(q,s) 

 

elif are_hyperplanes_in_projective_geometry_parameters(v,k,l): 

if existence: 

return True 

elif explain_construction: 

return "Singer difference set" 

else: 

_, (q,d) = are_hyperplanes_in_projective_geometry_parameters(v,k,l,True) 

G,D = singer_difference_set(q,d) 

 

elif are_hadamard_difference_set_parameters(v,k,l) and k-2*l == 3: 

if existence: 

return True 

elif explain_construction: 

return "Turyn 1965 construction" 

else: 

G,D = turyn_1965_3x3xK(4) 

 

elif are_hadamard_difference_set_parameters(v,k,l) and hadamard_difference_set_product_parameters(k-2*l): 

N1,N2 = hadamard_difference_set_product_parameters(k-2*l) 

if existence: 

return True 

elif explain_construction: 

return "Hadamard difference set product from N1={} and N2={}".format(N1,N2) 

else: 

v1 = 4*N1*N1; v2 = 4*N2*N2 

k1 = 2*N1*N1 - N1; k2 = 2*N2*N2 - N2 

l1 = N1*N1 - N1; l2 = N2*N2 - N2 

G1,D1 = difference_family(v1,k1,l1) 

G2,D2 = difference_family(v2,k2,l2) 

G,D = hadamard_difference_set_product(G1,D1,G2,D2) 

 

elif are_hadamard_difference_set_parameters(v,k,l) and (k-2*l).is_prime(): 

if existence: 

return False 

else: 

raise EmptySetError("by McFarland 1989 such difference family does not exist") 

 

elif len(factorization) == 1 and radical_difference_family(K, k, l, existence=True): 

if existence: 

return True 

elif explain_construction: 

return "Radical difference family on a finite field" 

else: 

D = radical_difference_family(K,k,l) 

G = K 

 

elif len(factorization) == 1 and l == 1 and k == 6 and df_q_6_1(K, existence=True): 

if existence: 

return True 

elif explain_construction: 

return "Wilson 1972 difference family made from the union of two cyclotomic cosets" 

else: 

D = df_q_6_1(K) 

G = K 

 

elif (k == (v-1)//2 and 

l == (k-1)//2 and 

len(factorization) == 2 and 

abs(pow(*factorization[0]) - pow(*factorization[1])) == 2): 

# Twin prime powers construction 

# i.e. v = p(p+2) where p and p+2 are prime powers 

# k = (v-1)/2 

# lambda = (k-1)/2 (ie 2l+1 = k) 

if existence: 

return True 

elif explain_construction: 

return "Twin prime powers difference family" 

else: 

p = pow(*factorization[0]) 

q = pow(*factorization[1]) 

if p > q: 

p,q = q,p 

G,D = twin_prime_powers_difference_set(p,check=False) 

 

else: 

if existence: 

return Unknown 

raise NotImplementedError("No constructions for these parameters") 

 

if check and not is_difference_family(G,D,v=v,k=k,l=l,verbose=False): 

raise RuntimeError("There is a problem. Sage built the following " 

"difference family on G='{}' with parameters ({},{},{}):\n " 

"{}\nwhich seems to not be a difference family... " 

"Please contact sage-devel@googlegroups.com".format(G,v,k,l,D)) 

 

return G, D 

 

from sage.misc.rest_index_of_methods import gen_rest_table_index 

import sys 

__doc__ = __doc__.format(INDEX_OF_FUNCTIONS=gen_rest_table_index(sys.modules[__name__]))