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

r""" 

Free quadratic modules 

 

Sage supports computation with free quadratic modules over an arbitrary 

commutative ring. Nontrivial functionality is available over `\ZZ` and 

fields. All free modules over an integral domain are equipped with an 

embedding in an ambient vector space and an inner product, which you 

can specify and change. 

 

Create the free module of rank `n` over an arbitrary commutative ring `R` 

using the command ``FreeModule(R,n)`` with a given inner_product_matrix. 

 

The following example illustrates the creation of both a vector spaces 

and a free module over the integers and a submodule of it. Use the functions 

``FreeModule``, ``span`` and member functions of free modules 

to create free modules. ''Do not use the ``FreeModule_xxx`` constructors 

directly.'' 

 

EXAMPLES:: 

 

sage: M = Matrix(QQ,[[2,1,0],[1,2,1],[0,1,2]]) 

sage: V = VectorSpace(QQ,3,inner_product_matrix=M) 

sage: type(V) 

<class 'sage.modules.free_quadratic_module.FreeQuadraticModule_ambient_field_with_category'> 

sage: V.inner_product_matrix() 

[2 1 0] 

[1 2 1] 

[0 1 2] 

sage: W = V.subspace([[1,2,7], [1,1,0]]) 

sage: type(W) 

<class 'sage.modules.free_quadratic_module.FreeQuadraticModule_submodule_field_with_category'> 

sage: W 

Quadratic space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -7] 

[ 0 1 7] 

Inner product matrix: 

[2 1 0] 

[1 2 1] 

[0 1 2] 

sage: W.gram_matrix() 

[ 100 -104] 

[-104 114] 

 

TESTS:: 

 

sage: M = Matrix(QQ,[[2,1,0],[1,2,1],[0,1,2]]) 

sage: V = VectorSpace(QQ,3,inner_product_matrix = M) 

sage: V == loads(dumps(V)) 

True 

sage: W = QuadraticSpace(QQ,3,M) 

sage: W == V 

True 

 

AUTHORS: 

 

- David Kohel (2008-06): First created (based on free_module.py) 

""" 

 

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

# Copyright (C) 2008 David Kohel <kohel@iml.univ-mrs.fr> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License 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/ 

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

from __future__ import print_function, absolute_import 

 

import weakref 

 

import sage.matrix.matrix_space 

import sage.misc.latex as latex 

import sage.rings.ring as ring 

import sage.rings.integer 

from sage.categories.principal_ideal_domains import PrincipalIdealDomains 

from . import free_module 

from sage.structure.richcmp import richcmp_not_equal, rich_to_bool, richcmp 

 

############################################################################### 

# 

# Constructor functions 

# 

############################################################################### 

_cache = {} 

 

def FreeQuadraticModule( 

base_ring, rank, inner_product_matrix, sparse=False, inner_product_ring=None): 

r""" 

Create the free quadratic module over the given commutative ring of the given rank. 

 

INPUT: 

 

- base_ring -- a commutative ring 

 

- rank -- a nonnegative integer 

 

- inner_product_matrix -- the inner product matrix 

 

- sparse -- bool; (default False) 

 

- inner_product_ring -- the inner product codomain ring; (default None) 

 

OUTPUT: 

 

A free quadratic module (with given inner product matrix). 

 

.. NOTE:: 

 

In Sage it is the case that there is only one dense and one 

sparse free ambient quadratic module of rank `n` over `R` and given 

inner product matrix. 

 

EXAMPLES:: 

 

sage: M2 = FreeQuadraticModule(ZZ,2,inner_product_matrix=[1,2,3,4]) 

sage: M2 is FreeQuadraticModule(ZZ,2,inner_product_matrix=[1,2,3,4]) 

True 

sage: M2.inner_product_matrix() 

[1 2] 

[3 4] 

sage: M3 = FreeModule(ZZ,2,inner_product_matrix=[[1,2],[3,4]]) 

sage: M3 is M2 

True 

""" 

global _cache 

rank = int(rank) 

 

# In order to use coercion into the inner_product_ring we need to pass 

# this ring into the vector classes. 

if inner_product_ring is not None: 

raise NotImplementedError("An inner_product_ring can not currently be defined.") 

 

# We intentionally create a new matrix instead of using the given 

# inner_product_matrix. This ensures that the matrix has the correct 

# parent space. It also gets rid of subdivisions which is good 

# because matrices with and without subdivisions compare equal. 

# Because of uniqueness, we need a canonical matrix, which is the one 

# without subdivisions. 

MS = sage.matrix.matrix_space.MatrixSpace(base_ring, rank) 

inner_product_matrix = MS(list(inner_product_matrix)) 

inner_product_matrix.set_immutable() 

 

key = (base_ring, rank, inner_product_matrix, sparse) 

 

if key in _cache: 

M = _cache[key]() 

if not (M is None): 

return M 

 

if not base_ring.is_commutative(): 

raise TypeError("base_ring must be a commutative ring") 

 

#elif not sparse and isinstance(base_ring,sage.rings.real_double.RealDoubleField_class): 

# M = RealDoubleQuadraticSpace_class(rank, inner_product_matrix=inner_product_matrix, sparse=False) 

 

#elif not sparse and isinstance(base_ring,sage.rings.complex_double.ComplexDoubleField_class): 

# M = ComplexDoubleQuadraticSpace_class(rank, inner_product_matrix=inner_product_matrix, sparse=False) 

 

elif base_ring.is_field(): 

M = FreeQuadraticModule_ambient_field( 

base_ring, rank, sparse=sparse, inner_product_matrix=inner_product_matrix) 

 

elif base_ring in PrincipalIdealDomains(): 

M = FreeQuadraticModule_ambient_pid( 

base_ring, rank, sparse=sparse, inner_product_matrix=inner_product_matrix) 

 

elif isinstance(base_ring, ring.IntegralDomain) or base_ring.is_integral_domain(): 

M = FreeQuadraticModule_ambient_domain( 

base_ring, rank, sparse=sparse, inner_product_matrix=inner_product_matrix) 

else: 

M = FreeQuadraticModule_ambient( 

base_ring, rank, sparse=sparse, inner_product_matrix=inner_product_matrix) 

 

_cache[key] = weakref.ref(M) 

return M 

 

def QuadraticSpace(K, dimension, inner_product_matrix, sparse=False): 

""" 

EXAMPLES: 

 

The base can be complicated, as long as it is a field:: 

 

sage: F.<x> = FractionField(PolynomialRing(ZZ,'x')) 

sage: D = diagonal_matrix([x,x-1,x+1]) 

sage: V = QuadraticSpace(F,3,D) 

sage: V 

Ambient quadratic space of dimension 3 over Fraction Field of Univariate Polynomial Ring in x over Integer Ring 

Inner product matrix: 

[ x 0 0] 

[ 0 x - 1 0] 

[ 0 0 x + 1] 

sage: V.basis() 

[ 

(1, 0, 0), 

(0, 1, 0), 

(0, 0, 1) 

] 

 

The base must be a field or a ``TypeError`` is raised:: 

 

sage: QuadraticSpace(ZZ,5,identity_matrix(ZZ,2)) 

Traceback (most recent call last): 

... 

TypeError: Argument K (= Integer Ring) must be a field. 

""" 

if not K.is_field(): 

raise TypeError("Argument K (= %s) must be a field." % K) 

if not sparse in (True,False): 

raise TypeError("Argument sparse (= %s) must be a boolean."%sparse) 

return FreeQuadraticModule(K, rank=dimension, inner_product_matrix=inner_product_matrix, sparse=sparse) 

 

InnerProductSpace = QuadraticSpace 

 

############################################################################### 

# 

# Base class for all free modules 

# 

############################################################################### 

 

def is_FreeQuadraticModule(M): 

""" 

Return True if `M` is a free quadratic module. 

 

EXAMPLES:: 

 

sage: from sage.modules.free_quadratic_module import is_FreeQuadraticModule 

sage: U = FreeModule(QQ,3) 

sage: is_FreeQuadraticModule(U) 

False 

sage: V = FreeModule(QQ,3,inner_product_matrix=diagonal_matrix([1,1,1])) 

sage: is_FreeQuadraticModule(V) 

True 

sage: W = FreeModule(QQ,3,inner_product_matrix=diagonal_matrix([2,3,3])) 

sage: is_FreeQuadraticModule(W) 

True 

""" 

return isinstance(M, FreeQuadraticModule_generic) 

 

class FreeQuadraticModule_generic(free_module.FreeModule_generic): 

""" 

Base class for all free quadratic modules. 

 

Modules are ordered by inclusion in the same ambient space. 

 

TESTS: 

 

We compare rank three free modules over the integers, 

rationals, and complex numbers:: 

 

sage: Q3 = FreeQuadraticModule(QQ,3,matrix.identity(3))  

sage: C3 = FreeQuadraticModule(CC,3,matrix.identity(3)) 

sage: Z3 = FreeQuadraticModule(ZZ,3,matrix.identity(3)) 

sage: Q3 < C3 

doctest:warning 

... 

DeprecationWarning: The default order on free modules has changed. The old ordering is in sage.modules.free_module.EchelonMatrixKey 

See http://trac.sagemath.org/23878 for details. 

False 

sage: C3 < Q3 

False 

sage: C3 > Q3 

False 

sage: Q3 > Z3 

True 

sage: Q3 < Z3 

False 

sage: Z3 < Q3 

True 

sage: Z3 > Q3 

False 

sage: Q3 == Z3 

False 

sage: Q3 == Q3 

True 

 

sage: V = Q3.span([[1,2,3], [5,6,7], [8,9,10]]) 

sage: V < Q3 

True 

sage: Q3 < V 

False 

 

The :meth:`inner_product_matrix` is part of the comparison:: 

 

sage: Q3zero = FreeQuadraticModule(QQ,3,matrix.zero(3)) 

sage: Q3zero == Q3 

False 

 

We test that :trac:`23915` is fixed:: 

 

sage: M1 = FreeQuadraticModule(ZZ,1,matrix.identity(1)) 

sage: M2 = FreeQuadraticModule(ZZ,1,matrix.identity(1)*2) 

sage: M1 == M2 

False 

""" 

def __init__(self, base_ring, rank, degree, inner_product_matrix, sparse=False): 

""" 

Create the free module of given rank over the given base_ring. 

 

INPUT: 

 

- base_ring -- a commutative ring 

 

- rank -- a non-negative integer 

 

EXAMPLES:: 

 

sage: R = PolynomialRing(QQ,3,'x') 

sage: FreeModule(R,3,inner_product_matrix=diagonal_matrix(list(R.gens()))) 

Ambient free quadratic module of rank 3 over the integral domain Multivariate Polynomial Ring in x0, x1, x2 over Rational Field 

Inner product matrix: 

[x0 0 0] 

[ 0 x1 0] 

[ 0 0 x2] 

""" 

free_module.FreeModule_generic.__init__( 

self, base_ring=base_ring, rank=rank, degree=degree, sparse=sparse) 

self._inner_product_matrix=inner_product_matrix 

 

def _dense_module(self): 

""" 

Creates a dense module with the same defining data as self. 

 

N.B. This function is for internal use only! See dense_module for use. 

 

EXAMPLES:: 

 

sage: A = diagonal_matrix([1,2,2]) 

sage: M = FreeModule(Integers(8),3,inner_product_matrix=A) 

sage: S = FreeModule(Integers(8),3,inner_product_matrix=A,sparse=True) 

sage: M is S._dense_module() 

True 

""" 

A = self.ambient_module().dense_module() 

return A.span(self.basis()) 

 

def _sparse_module(self): 

""" 

Creates a sparse module with the same defining data as self. 

 

N.B. This function is for internal use only! See sparse_module for use. 

 

EXAMPLES:: 

 

sage: A = diagonal_matrix([1,2,2]) 

sage: M = FreeModule(Integers(8),3,inner_product_matrix=A) 

sage: S = FreeModule(Integers(8),3,inner_product_matrix=A,sparse=True) 

sage: M._sparse_module() is S 

True 

""" 

A = self.ambient_module().sparse_module() 

return A.span(self.basis()) 

 

def ambient_module(self): 

""" 

Return the ambient module associated to this module. 

 

EXAMPLES:: 

 

sage: R.<x,y> = QQ[] 

sage: M = FreeModule(R,2) 

sage: M.ambient_module() 

Ambient free module of rank 2 over the integral domain Multivariate Polynomial Ring in x, y over Rational Field 

 

sage: V = FreeModule(QQ, 4).span([[1,2,3,4], [1,0,0,0]]); V 

Vector space of degree 4 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 0 0] 

[ 0 1 3/2 2] 

sage: V.ambient_module() 

Vector space of dimension 4 over Rational Field 

""" 

return FreeQuadraticModule(self.base_ring(), self.degree(), self.inner_product_matrix()) 

 

def determinant(self): 

""" 

Return the determinant of this free module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3, inner_product_matrix=1) 

sage: M.determinant() 

1 

sage: N = M.span([[1,2,3]]) 

sage: N.determinant() 

14 

sage: P = M.span([[1,2,3], [1,1,1]]) 

sage: P.determinant() 

6 

""" 

return self.gram_matrix().determinant() 

 

def discriminant(self): 

""" 

Return the discriminant of this free module, defined to be (-1)^r 

of the determinant, where r = n/2 (n even) or (n-1)/2 (n odd) for 

a module of rank n. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: M.discriminant() 

1 

sage: N = M.span([[1,2,3]]) 

sage: N.discriminant() 

14 

sage: P = M.span([[1,2,3], [1,1,1]]) 

sage: P.discriminant() 

6 

 

TESTS:: 

 

sage: M=FreeQuadraticModule(ZZ,2,matrix.identity(2)) 

sage: M.discriminant() 

-1 

sage: M=FreeQuadraticModule(QQ,3,matrix.identity(3)) 

sage: M.discriminant() 

-1 

 

""" 

n = self.rank() 

r = n//2 

return (-1)**r*self.gram_matrix().determinant() 

 

def gram_matrix(self): 

""" 

Return the gram matrix associated to this free module, defined to be 

G = B*A*B.transpose(), where A is the inner product matrix (induced from 

the ambient space), and B the basis matrix. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,4) 

sage: u = V([1/2,1/2,1/2,1/2]) 

sage: v = V([0,1,1,0]) 

sage: w = V([0,0,1,1]) 

sage: M = span([u,v,w], ZZ) 

sage: M.inner_product_matrix() == V.inner_product_matrix() 

True 

sage: L = M.submodule_with_basis([u,v,w]) 

sage: L.inner_product_matrix() == M.inner_product_matrix() 

True 

sage: L.gram_matrix() 

[1 1 1] 

[1 2 1] 

[1 1 2] 

 

""" 

if self.is_ambient(): 

return self.inner_product_matrix() 

else: 

if self._gram_matrix is None: 

A = self.inner_product_matrix() 

B = self.basis_matrix() 

self._gram_matrix = B*A*B.transpose() 

return self._gram_matrix 

 

def inner_product_matrix(self): 

""" 

Return the inner product matrix associated to this module. By definition this 

is the inner product matrix of the ambient space, hence may be of degree greater 

than the rank of the module. 

 

N.B. The inner product does not have to be symmetric (see examples). 

 

TODO: Differentiate the image ring of the inner product from the base ring of 

the module and/or ambient space. E.g. On an integral module over ZZ the inner 

product pairing could naturally take values in ZZ, QQ, RR, or CC. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: M.inner_product_matrix() 

[1 0 0] 

[0 1 0] 

[0 0 1] 

 

The inner product does not have to be symmetric or definite:: 

 

sage: N = FreeModule(ZZ,2,inner_product_matrix=[[1,-1],[2,5]]) 

sage: N.inner_product_matrix() 

[ 1 -1] 

[ 2 5] 

sage: u, v = N.basis() 

sage: u.inner_product(v) 

-1 

sage: v.inner_product(u) 

2 

 

The inner product matrix is defined with respect to the ambient space. 

 

sage: V = QQ^3 

sage: u = V([1/2,1,1]) 

sage: v = V([1,1,1/2]) 

sage: M = span([u,v], ZZ) 

sage: M.inner_product_matrix() 

[1 0 0] 

[0 1 0] 

[0 0 1] 

sage: M.inner_product_matrix() == V.inner_product_matrix() 

True 

sage: M.gram_matrix() 

[ 1/2 -3/4] 

[-3/4 13/4] 

 

""" 

return self._inner_product_matrix 

 

def _inner_product_is_dot_product(self): 

""" 

Return whether or not the inner product on this module is induced by 

the dot product on the ambient vector space. This is used internally 

by the inner_product function for optimization. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 3)._inner_product_is_dot_product() 

True 

sage: FreeModule(ZZ, 3, inner_product_matrix=1)._inner_product_is_dot_product() 

True 

sage: FreeModule(ZZ, 2, inner_product_matrix=[1,0,-1,0])._inner_product_is_dot_product() 

False 

 

sage: M = FreeModule(QQ, 3) 

sage: M2 = M.span([[1,2,3]]) 

sage: M2._inner_product_is_dot_product() 

True 

""" 

return self.inner_product_matrix() == 1 

 

 

def _inner_product_is_diagonal(self): 

""" 

Return whether or not the inner product on this module is induced by 

the dot product on the ambient vector space. This is used internally 

by the inner_product function for optimization. 

 

N.B. The FreeModule classes have the identity inner product matrix, 

while FreeQuadraticModules must have an inner_product_matrix, although 

it can be diagonal. 

 

EXAMPLES:: 

 

sage: M0 = FreeModule(ZZ, 3, inner_product_matrix=1) 

sage: M0._inner_product_is_diagonal() 

True 

sage: D = diagonal_matrix([3,5,7]) 

sage: M1 = FreeModule(ZZ, 3, inner_product_matrix=D) 

sage: M1._inner_product_is_diagonal() 

True 

sage: A = Matrix([[2,1,0],[1,2,1],[0,1,2]]) 

sage: M2 = FreeModule(ZZ, 3, inner_product_matrix=A) 

sage: M2._inner_product_is_diagonal() 

False 

sage: M3 = FreeModule(ZZ, 2, inner_product_matrix=[1,0,-1,0]) 

sage: M3._inner_product_is_diagonal() 

False 

 

TODO: Actually use the diagonal form of the inner product. 

""" 

A = self.inner_product_matrix() 

D = sage.matrix.constructor.diagonal_matrix([ A[i,i] for i in range(A.nrows()) ]) 

return A == D 

 

class FreeQuadraticModule_generic_pid( 

free_module.FreeModule_generic_pid, FreeQuadraticModule_generic): 

""" 

Class of all free modules over a PID. 

""" 

def __init__(self, base_ring, rank, degree, inner_product_matrix, sparse=False): 

""" 

Create a free module over a PID. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2, inner_product_matrix=Matrix([[2,1],[1,2]])) 

Ambient free quadratic module of rank 2 over the principal ideal domain Integer Ring 

Inner product matrix: 

[2 1] 

[1 2] 

""" 

free_module.FreeModule_generic_pid.__init__( 

self, base_ring=base_ring, rank=rank, degree=degree, sparse=sparse) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def span(self, gens, check=True, already_echelonized=False): 

""" 

Return the R-span of the given list of gens, where R 

is the base ring of self. Note that this span need not 

be a submodule of self, nor even of the ambient space. 

It must, however, be contained in the ambient vector space, i.e., 

the ambient space tensored with the fraction field of R. 

 

EXAMPLES:: 

 

sage: V = FreeModule(ZZ,3) 

sage: W = V.submodule([V.gen(0)]) 

sage: W.span([V.gen(1)]) 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[0 1 0] 

sage: W.submodule([V.gen(1)]) 

Traceback (most recent call last): 

... 

ArithmeticError: Argument gens (= [(0, 1, 0)]) does not generate a submodule of self. 

""" 

return FreeQuadraticModule_submodule_pid( 

self.ambient_module(), gens, inner_product_matrix=self.inner_product_matrix(), 

check=check, already_echelonized=already_echelonized) 

 

def span_of_basis(self, basis, check=True, already_echelonized=False): 

r""" 

Return the free R-module with the given basis, where R 

is the base ring of self. Note that this R-module need not 

be a submodule of self, nor even of the ambient space. It 

must, however, be contained in the ambient vector space, i.e., 

the ambient space tensored with the fraction field of R. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ,3) 

sage: W = M.span_of_basis([M([1,2,3])]) 

 

Next we create two free `\ZZ`-modules, neither of which is a 

submodule of `W`: 

 

sage: W.span_of_basis([M([2,4,0])]) 

Free module of degree 3 and rank 1 over Integer Ring 

User basis matrix: 

[2 4 0] 

 

The following module isn't even in the ambient space:: 

 

sage: Q = QQ 

sage: W.span_of_basis([ Q('1/5')*M([1,2,0]), Q('1/7')*M([1,1,0]) ]) 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1/5 2/5 0] 

[1/7 1/7 0] 

 

Of course the input basis vectors must be linearly 

independent:: 

 

sage: W.span_of_basis([ [1,2,0], [2,4,0] ]) 

Traceback (most recent call last): 

... 

ValueError: The given basis vectors must be linearly independent. 

""" 

return FreeQuadraticModule_submodule_with_basis_pid( 

self.ambient_module(), basis=basis, inner_product_matrix=self.inner_product_matrix(), 

check=check, already_echelonized=already_echelonized) 

 

def zero_submodule(self): 

""" 

Return the zero submodule of this module. 

 

EXAMPLES:: 

 

sage: V = FreeModule(ZZ,2) 

sage: V.zero_submodule() 

Free module of degree 2 and rank 0 over Integer Ring 

Echelon basis matrix: 

[] 

""" 

return FreeQuadraticModule_submodule_pid( 

self.ambient_module(), [], self.inner_product_matrix(), check=False) 

 

class FreeQuadraticModule_generic_field( 

free_module.FreeModule_generic_field, FreeQuadraticModule_generic_pid): 

""" 

Base class for all free modules over fields. 

""" 

def __init__(self, base_field, dimension, degree, inner_product_matrix, sparse=False): 

""" 

Creates a vector space over a field. 

 

EXAMPLES:: 

 

sage: FreeModule(QQ, 2, inner_product_matrix=[[2,1],[1,2]]) 

Ambient quadratic space of dimension 2 over Rational Field 

Inner product matrix: 

[2 1] 

[1 2] 

sage: FreeModule(FiniteField(2), 7, inner_product_matrix=1) 

Ambient quadratic space of dimension 7 over Finite Field of size 2 

Inner product matrix: 

[1 0 0 0 0 0 0] 

[0 1 0 0 0 0 0] 

[0 0 1 0 0 0 0] 

[0 0 0 1 0 0 0] 

[0 0 0 0 1 0 0] 

[0 0 0 0 0 1 0] 

[0 0 0 0 0 0 1] 

""" 

if not isinstance(base_field, ring.Field): 

raise TypeError("The base_field (=%s) must be a field" % base_field) 

free_module.FreeModule_generic_field.__init__( 

self, base_field=base_field, dimension=dimension, degree=degree, sparse=sparse) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def span(self, gens, check=True, already_echelonized=False): 

""" 

Return the K-span of the given list of gens, where K is the 

base field of self. Note that this span is a subspace of the 

ambient vector space, but need not be a subspace of self. 

 

INPUT: 

 

- gens -- list of vectors 

 

- check -- bool (default: True): whether or not to coerce 

entries of gens into base field 

 

- already_echelonized -- bool (default: False): set this if 

you know the gens are already in echelon form 

 

EXAMPLES:: 

 

sage: V = VectorSpace(GF(7), 3) 

sage: W = V.subspace([[2,3,4]]); W 

Vector space of degree 3 and dimension 1 over Finite Field of size 7 

Basis matrix: 

[1 5 2] 

sage: W.span([[1,1,1]]) 

Vector space of degree 3 and dimension 1 over Finite Field of size 7 

Basis matrix: 

[1 1 1] 

""" 

if free_module.is_FreeModule(gens): 

gens = gens.gens() 

if not isinstance(gens, (list, tuple)): 

raise TypeError("gens (=%s) must be a list or tuple"%gens) 

 

return FreeQuadraticModule_submodule_field( 

self.ambient_module(), gens, 

inner_product_matrix=self.inner_product_matrix(), 

check=check, already_echelonized=already_echelonized) 

 

def span_of_basis(self, basis, check=True, already_echelonized=False): 

r""" 

Return the free K-module with the given basis, where K 

is the base field of self. Note that this span is 

a subspace of the ambient vector space, but need 

not be a subspace of self. 

 

INPUT: 

 

- basis -- list of vectors 

 

- check -- bool (default: True): whether or not to coerce 

entries of gens into base field 

 

- already_echelonized -- bool (default: False): set this if 

you know the gens are already in echelon form 

 

EXAMPLES:: 

 

sage: V = VectorSpace(GF(7), 3) 

sage: W = V.subspace([[2,3,4]]); W 

Vector space of degree 3 and dimension 1 over Finite Field of size 7 

Basis matrix: 

[1 5 2] 

sage: W.span_of_basis([[2,2,2], [3,3,0]]) 

Vector space of degree 3 and dimension 2 over Finite Field of size 7 

User basis matrix: 

[2 2 2] 

[3 3 0] 

 

The basis vectors must be linearly independent or a 

``ValueError`` exception is raised:: 

 

sage: W.span_of_basis([[2,2,2], [3,3,3]]) 

Traceback (most recent call last): 

... 

ValueError: The given basis vectors must be linearly independent. 

""" 

return FreeQuadraticModule_submodule_with_basis_field( 

self.ambient_module(), basis=basis, 

inner_product_matrix=self.inner_product_matrix(), 

check=check, already_echelonized=already_echelonized) 

 

############################################################################### 

# 

# Generic ambient free modules, i.e., of the form R^n for some commutative ring R. 

# 

############################################################################### 

 

class FreeQuadraticModule_ambient( 

free_module.FreeModule_ambient, FreeQuadraticModule_generic): 

""" 

Ambient free module over a commutative ring. 

""" 

def __init__(self, base_ring, rank, inner_product_matrix, sparse=False): 

""" 

The free module of given rank over the given base_ring. 

 

INPUT: 

 

- base_ring -- a commutative ring 

 

- rank -- a non-negative integer 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 4) 

Ambient free module of rank 4 over the principal ideal domain Integer Ring 

 

""" 

free_module.FreeModule_ambient.__init__(self, base_ring=base_ring, rank=rank, sparse=sparse) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: R = ZZ.quo(12) 

sage: M = R^12 

sage: M 

Ambient free module of rank 12 over Ring of integers modulo 12 

sage: print(M._repr_()) 

Ambient free module of rank 12 over Ring of integers modulo 12 

 

The system representation can be overwritten, but leaves 

:meth:`_repr_` unmodified:: 

 

sage: M.rename('M') 

sage: M 

M 

sage: print(M._repr_()) 

Ambient free module of rank 12 over Ring of integers modulo 12 

 

Sparse modules print this fact:: 

 

sage: N = FreeModule(R,12,sparse=True) 

sage: N 

Ambient sparse free module of rank 12 over Ring of integers modulo 12 

""" 

if self.is_sparse(): 

return "Ambient sparse free quadratic module of rank %s over %s\n" % ( self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

else: 

return "Ambient free quadratic module of rank %s over %s\n" % ( self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

 

def _latex_(self): 

r""" 

Return a latex representation of this ambient free quadratic module. 

 

EXAMPLES:: 

 

sage: latex(QQ^3) # indirect doctest 

\Bold{Q}^{3} 

 

sage: A = GF(5)^20; latex(A) 

\Bold{F}_{5}^{20} 

 

sage: A = PolynomialRing(QQ,3,'x')^20; latex(A) 

(\Bold{Q}[x_{0}, x_{1}, x_{2}])^{20} 

 

sage: V = QuadraticSpace(QQ,3,inner_product_matrix=[[2,1,0],[1,4,1],[0,1,8]]) 

sage: latex(V) 

None 

""" 

# How do we want to represent this object? 

NotImplementedError 

 

def _dense_module(self): 

""" 

Creates a dense module with the same defining data as self. 

 

N.B. This function is for internal use only! See dense_module for use. 

 

EXAMPLES:: 

 

sage: A = diagonal_matrix([1,2,2]) 

sage: M = FreeModule(Integers(8),3,inner_product_matrix=A) 

sage: S = FreeModule(Integers(8),3,inner_product_matrix=A,sparse=True) 

sage: M is S._dense_module() 

True 

""" 

return FreeQuadraticModule(base_ring=self.base_ring(), rank = self.rank(), 

inner_product_matrix = self.inner_product_matrix(), sparse=False) 

 

def _sparse_module(self): 

""" 

Creates a sparse module with the same defining data as self. 

 

N.B. This function is for internal use only! See sparse_module for use. 

 

EXAMPLES:: 

 

sage: A = diagonal_matrix([1,2,2]) 

sage: M = FreeModule(Integers(8),3,inner_product_matrix=A) 

sage: S = FreeModule(Integers(8),3,inner_product_matrix=A,sparse=True) 

sage: M._sparse_module() is S 

True 

""" 

return FreeQuadraticModule(base_ring = self.base_ring(), rank = self.rank(), 

inner_product_matrix = self.inner_product_matrix(), sparse=True) 

 

############################################################################### 

# 

# Ambient free modules over an integral domain. 

# 

############################################################################### 

 

class FreeQuadraticModule_ambient_domain( 

free_module.FreeModule_ambient_domain, FreeQuadraticModule_ambient): 

""" 

Ambient free quadratic module over an integral domain. 

""" 

def __init__(self, base_ring, rank, inner_product_matrix, sparse=False): 

""" 

EXAMPLES:: 

 

sage: FreeModule(PolynomialRing(GF(5),'x'), 3) 

Ambient free module of rank 3 over the principal ideal domain 

Univariate Polynomial Ring in x over Finite Field of size 5 

""" 

free_module.FreeModule_ambient.__init__(self, base_ring=base_ring, rank=rank, sparse=sparse) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: R = PolynomialRing(ZZ,'x') 

sage: M = FreeModule(R,7) 

sage: M 

Ambient free module of rank 7 over the integral domain Univariate Polynomial Ring in x over Integer Ring 

sage: print(M._repr_()) 

Ambient free module of rank 7 over the integral domain Univariate Polynomial Ring in x over Integer Ring 

 

The system representation can be overwritten, but leaves 

:meth:`_repr_` unmodified:: 

 

sage: M.rename('M') 

sage: M 

M 

sage: print(M._repr_()) 

Ambient free module of rank 7 over the integral domain Univariate Polynomial Ring in x over Integer Ring 

 

Sparse modules print this fact:: 

 

sage: N = FreeModule(R,7,sparse=True) 

sage: N 

Ambient sparse free module of rank 7 over the integral domain Univariate Polynomial Ring in x over Integer Ring 

 

Here is a construction of a free quadratic module with generic 

symmetric inner product matrix:: 

 

sage: R.<a,b,c> = PolynomialRing(QQ,3) 

sage: M = FreeModule(R, 2, inner_product_matrix=[[2*a,b],[b,2*c]]) 

sage: M 

Ambient free quadratic module of rank 2 over the integral domain Multivariate Polynomial Ring in a, b, c over Rational Field 

Inner product matrix: 

[2*a b] 

[ b 2*c] 

sage: M.determinant() 

-b^2 + 4*a*c 

""" 

if self.is_sparse(): 

return "Ambient sparse free quadratic module of rank %s over the integral domain %s\n"%( 

self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

else: 

return "Ambient free quadratic module of rank %s over the integral domain %s\n"%( 

self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

 

def ambient_vector_space(self): 

""" 

Returns the ambient vector space, which is this free module tensored 

with its fraction field. 

 

EXAMPLES:: 

 

sage: M = ZZ^3; M.ambient_vector_space() 

Vector space of dimension 3 over Rational Field 

""" 

try: 

return self.__ambient_vector_space 

except AttributeError: 

self.__ambient_vector_space = FreeQuadraticModule( 

self.base_field(), self.rank(), 

inner_product_matrix=self.inner_product_matrix(), sparse=self.is_sparse()) 

return self.__ambient_vector_space 

 

############################################################################### 

# 

# Ambient free modules over a principal ideal domain. 

# 

############################################################################### 

 

class FreeQuadraticModule_ambient_pid( 

free_module.FreeModule_ambient_pid, FreeQuadraticModule_generic_pid, FreeQuadraticModule_ambient_domain): 

""" 

Ambient free quadratic module over a principal ideal domain. 

""" 

def __init__(self, base_ring, rank, inner_product_matrix, sparse=False): 

""" 

Create the ambient free module of given rank over the given 

principal ideal domain 

 

INPUT: 

 

- base_ring -- a principal ideal domain 

 

- rank -- a non-negative integer 

 

- sparse -- bool (default: False) 

 

- inner_product_matrix -- bool (default: None) 

 

EXAMPLES:: 

 

sage: ZZ^3 

Ambient free module of rank 3 over the principal ideal domain Integer Ring 

sage: FreeModule(ZZ,3,inner_product_matrix=Matrix([[2,-1,0],[-1,2,-1],[0,-1,2]])) 

Ambient free quadratic module of rank 3 over the principal ideal domain Integer Ring 

Inner product matrix: 

[ 2 -1 0] 

[-1 2 -1] 

[ 0 -1 2] 

""" 

free_module.FreeModule_ambient_pid.__init__(self, base_ring=base_ring, rank=rank, sparse=sparse) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2, inner_product_matrix=[[2,-1],[-1,2]]) 

sage: M 

Ambient free quadratic module of rank 2 over the principal ideal domain Integer Ring 

Inner product matrix: 

[ 2 -1] 

[-1 2] 

 

Without a user specified inner product the class and printing 

is simpler:: 

 

sage: M = FreeModule(ZZ,7) 

sage: M 

Ambient free module of rank 7 over the principal ideal domain Integer Ring 

sage: print(M._repr_()) 

Ambient free module of rank 7 over the principal ideal domain Integer Ring 

 

The system representation can be overwritten, but leaves 

:meth:`_repr_` unmodified:: 

 

sage: M.rename('M') 

sage: M 

M 

sage: print(M._repr_()) 

Ambient free module of rank 7 over the principal ideal domain Integer Ring 

 

Sparse modules print this fact:: 

 

sage: N = FreeModule(ZZ,7,sparse=True) 

sage: N 

Ambient sparse free module of rank 7 over the principal ideal domain Integer Ring 

 

""" 

if self.is_sparse(): 

return "Ambient sparse free quadratic module of rank %s over the principal ideal domain %s\n"%( 

self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

else: 

return "Ambient free quadratic module of rank %s over the principal ideal domain %s\n"%( 

self.rank(), self.base_ring()) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

 

############################################################################### 

# 

# Ambient free modules over a field (i.e., a vector space). 

# 

############################################################################### 

 

class FreeQuadraticModule_ambient_field( 

free_module.FreeModule_ambient_field, 

FreeQuadraticModule_generic_field, FreeQuadraticModule_ambient_pid): 

 

def __init__(self, base_field, dimension, inner_product_matrix, sparse=False): 

""" 

Create the ambient vector space of given dimension over the given field. 

 

INPUT: 

 

- base_field -- a field 

 

- dimension -- a non-negative integer 

 

- sparse -- bool (default: False) 

 

EXAMPLES:: 

 

sage: VectorSpace(QQ,3,inner_product_matrix=[[2,1,0],[1,2,0],[0,1,2]]) 

Ambient quadratic space of dimension 3 over Rational Field 

Inner product matrix: 

[2 1 0] 

[1 2 0] 

[0 1 2] 

""" 

free_module.FreeModule_ambient_field.__init__( 

self, base_field=base_field, dimension=dimension, sparse=sparse) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: V = FreeModule(QQ,7) 

sage: V 

Vector space of dimension 7 over Rational Field 

sage: print(V._repr_()) 

Vector space of dimension 7 over Rational Field 

 

The system representation can be overwritten, but leaves 

:meth:`_repr_` unmodified:: 

 

sage: V.rename('V') 

sage: V 

V 

sage: print(V._repr_()) 

Vector space of dimension 7 over Rational Field 

 

Sparse modules print this fact:: 

 

sage: U = FreeModule(QQ,7,sparse=True) 

sage: U 

Sparse vector space of dimension 7 over Rational Field 

""" 

if self.is_sparse(): 

return "Ambient sparse free quadratic space of dimension %s over %s\n" % ( self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

else: 

return "Ambient quadratic space of dimension %s over %s\n" % ( self.rank(), self.base_ring() ) + \ 

"Inner product matrix:\n%s" % self.inner_product_matrix() 

 

############################################################################### 

# 

# R-Submodule of K^n where K is the fraction field of a principal ideal domain R. 

# 

############################################################################### 

 

class FreeQuadraticModule_submodule_with_basis_pid( 

free_module.FreeModule_submodule_with_basis_pid, FreeQuadraticModule_generic_pid): 

""" 

An `R`-submodule of `K^n` with distinguished basis, where `K` is 

the fraction field of a principal ideal domain `R`. 

 

Modules are ordered by inclusion. 

 

EXAMPLES: 

 

First we compare two equal vector spaces:: 

 

sage: A = FreeQuadraticModule(QQ,3,2*matrix.identity(3)) 

sage: V = A.span([[1,2,3], [5,6,7], [8,9,10]]) 

sage: W = A.span([[5,6,7], [8,9,10]]) 

sage: V == W 

True 

 

Next we compare a one dimensional space to the two dimensional 

space defined above:: 

 

sage: M = A.span([[5,6,7]]) 

sage: V == M 

False 

sage: M < V 

doctest:warning 

... 

DeprecationWarning: The default order on free modules has changed. The old ordering is in sage.modules.free_module.EchelonMatrixKey 

See http://trac.sagemath.org/23878 for details. 

True 

sage: V < M 

False 

 

We compare a `\ZZ`-module to the one-dimensional space above:: 

 

sage: V = A.span([[5,6,7]]) 

sage: V = V.change_ring(ZZ).scale(1/11); 

sage: V < M 

True 

sage: M < V 

False 

""" 

def __init__(self, ambient, basis, inner_product_matrix, 

check=True, echelonize=False, echelonized_basis=None, already_echelonized=False): 

""" 

Create a free module with basis over a PID. 

 

EXAMPLES:: 

 

sage: A = diagonal_matrix([1,2,2]) 

sage: M = FreeQuadraticModule(ZZ,3,inner_product_matrix=A) 

sage: W = M.span_of_basis([[1,2,3],[4,5,6]]); W 

Free quadratic module of degree 3 and rank 2 over Integer Ring 

Basis matrix: 

[1 2 3] 

[4 5 6] 

Inner product matrix: 

[1 0 0] 

[0 2 0] 

[0 0 2] 

 

sage: W = M.span_of_basis([[1,2,3/2],[4,5,6]]); W 

Free quadratic module of degree 3 and rank 2 over Integer Ring 

Basis matrix: 

[ 1 2 3/2] 

[ 4 5 6] 

Inner product matrix: 

[1 0 0] 

[0 2 0] 

[0 0 2] 

 

TESTS: 

 

We test that :trac:`23703` is fixed:: 

 

sage: A=FreeQuadraticModule(ZZ,1,matrix.identity(1)) 

sage: B=A.span([[1/2]]) 

sage: C=B.span([[1]]) 

sage: B.intersection(C)==C.intersection(B) 

True 

""" 

free_module.FreeModule_submodule_with_basis_pid.__init__( 

self, ambient=ambient, basis=basis, check=check, 

echelonize=echelonize, echelonized_basis=echelonized_basis, already_echelonized=already_echelonized) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: L = ZZ^8 

sage: E = L.submodule_with_basis([ L.gen(i) - L.gen(0) for i in range(1,8) ]) 

sage: E # indirect doctest 

Free module of degree 8 and rank 7 over Integer Ring 

User basis matrix: 

[-1 1 0 0 0 0 0 0] 

[-1 0 1 0 0 0 0 0] 

[-1 0 0 1 0 0 0 0] 

[-1 0 0 0 1 0 0 0] 

[-1 0 0 0 0 1 0 0] 

[-1 0 0 0 0 0 1 0] 

[-1 0 0 0 0 0 0 1] 

 

sage: M = FreeModule(ZZ,8,sparse=True) 

sage: N = M.submodule_with_basis([ M.gen(i) - M.gen(0) for i in range(1,8) ]) 

sage: N # indirect doctest 

Sparse free module of degree 8 and rank 7 over Integer Ring 

User basis matrix: 

[-1 1 0 0 0 0 0 0] 

[-1 0 1 0 0 0 0 0] 

[-1 0 0 1 0 0 0 0] 

[-1 0 0 0 1 0 0 0] 

[-1 0 0 0 0 1 0 0] 

[-1 0 0 0 0 0 1 0] 

[-1 0 0 0 0 0 0 1] 

""" 

if self.is_sparse(): 

s = "Sparse free quadratic module of degree %s and rank %s over %s\n"%( 

self.degree(), self.rank(), self.base_ring()) + \ 

"Basis matrix:\n%r\n" % self.basis_matrix() + \ 

"Inner product matrix:\n%r" % self.inner_product_matrix() 

else: 

s = "Free quadratic module of degree %s and rank %s over %s\n"%( 

self.degree(), self.rank(), self.base_ring()) + \ 

"Basis matrix:\n%r\n" % self.basis_matrix() + \ 

"Inner product matrix:\n%r" % self.inner_product_matrix() 

return s 

 

def _latex_(self): 

r""" 

Return latex representation of this free module. 

 

EXAMPLES:: 

 

sage: A = ZZ^3 

sage: M = A.span_of_basis([[1,2,3],[4,5,6]]) 

sage: M._latex_() 

'\\mathrm{RowSpan}_{\\Bold{Z}}\\left(\\begin{array}{rrr}\n1 & 2 & 3 \\\\\n4 & 5 & 6\n\\end{array}\\right)' 

""" 

return "\\mathrm{RowSpan}_{%s}%s"%(latex.latex(self.base_ring()), latex.latex(self.basis_matrix())) 

 

def change_ring(self, R): 

""" 

Return the free module over R obtained by coercing each 

element of self into a vector over the fraction field of R, 

then taking the resulting R-module. Raises a TypeError 

if coercion is not possible. 

 

INPUT: 

 

- ``R`` -- a principal ideal domain 

 

EXAMPLES: 

 

Changing rings preserves the inner product and the user basis:: 

 

sage: V = QQ^3 

sage: W = V.subspace([[2, '1/2', 1]]); W 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[ 1 1/4 1/2] 

sage: W.change_ring(GF(7)) 

Vector space of degree 3 and dimension 1 over Finite Field of size 7 

Basis matrix: 

[1 2 4] 

 

sage: N = FreeModule(ZZ, 2, inner_product_matrix=[[1,-1],[2,5]]) 

sage: N.inner_product_matrix() 

[ 1 -1] 

[ 2 5] 

sage: Np = N.change_ring(RDF) 

sage: Np.inner_product_matrix() 

[ 1.0 -1.0] 

[ 2.0 5.0] 

""" 

if self.base_ring() is R: 

return self 

K = R.fraction_field() 

A = self.inner_product_matrix() 

V = QuadraticSpace(K, self.degree(), inner_product_matrix=A) 

B = [ V(b) for b in self.basis() ] 

M = FreeQuadraticModule(R, self.degree(), inner_product_matrix=A) 

if self.has_user_basis(): 

return M.span_of_basis(B) 

else: 

return M.span(B) 

 

class FreeQuadraticModule_submodule_pid( 

free_module.FreeModule_submodule_pid, FreeQuadraticModule_submodule_with_basis_pid): 

""" 

An `R`-submodule of `K^n` where `K` is the fraction field of a 

principal ideal domain `R`. 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: W = M.span_of_basis([[1,2,3],[4,5,19]]); W 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[ 1 2 3] 

[ 4 5 19] 

 

We can save and load submodules and elements:: 

 

sage: loads(W.dumps()) == W 

True 

sage: v = W.0 + W.1 

sage: loads(v.dumps()) == v 

True 

""" 

def __init__(self, ambient, gens, inner_product_matrix, check=True, already_echelonized=False): 

""" 

Create an embedded free module over a PID. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

sage: W = V.span([[1,2,3],[4,5,6]]) 

sage: W 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 2 3] 

[0 3 6] 

""" 

free_module.FreeModule_submodule_pid.__init__( 

self, ambient=ambient, gens=gens, check=check, already_echelonized=already_echelonized) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ,8,inner_product_matrix=1) 

sage: L = M.submodule([ M.gen(i) - M.gen(0) for i in range(1,8) ]) 

sage: L # indirect doctest 

Free module of degree 8 and rank 7 over Integer Ring 

Echelon basis matrix: 

[ 1 0 0 0 0 0 0 -1] 

[ 0 1 0 0 0 0 0 -1] 

[ 0 0 1 0 0 0 0 -1] 

[ 0 0 0 1 0 0 0 -1] 

[ 0 0 0 0 1 0 0 -1] 

[ 0 0 0 0 0 1 0 -1] 

[ 0 0 0 0 0 0 1 -1] 

""" 

if self.is_sparse(): 

s = "Sparse free module of degree %s and rank %s over %s\n"%( 

self.degree(), self.rank(), self.base_ring()) + \ 

"Echelon basis matrix:\n%s"%self.basis_matrix() 

else: 

s = "Free module of degree %s and rank %s over %s\n"%( 

self.degree(), self.rank(), self.base_ring()) + \ 

"Echelon basis matrix:\n%s"%self.basis_matrix() 

return s 

 

class FreeQuadraticModule_submodule_with_basis_field( 

free_module.FreeModule_submodule_with_basis_field, 

FreeQuadraticModule_generic_field, FreeQuadraticModule_submodule_with_basis_pid): 

""" 

An embedded vector subspace with a distinguished user basis. 

 

EXAMPLES:: 

 

sage: M = QQ^3; W = M.submodule_with_basis([[1,2,3], [4,5,19]]); W 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[ 1 2 3] 

[ 4 5 19] 

 

Since this is an embedded vector subspace with a distinguished user 

basis possibly different than the echelonized basis, the 

``echelon_coordinates()`` and user ``coordinates()`` do not agree:: 

 

sage: V = QQ^3 

sage: W = V.submodule_with_basis([[1,2,3], [4,5,6]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[1 2 3] 

[4 5 6] 

 

sage: v = V([1,5,9]) 

sage: W.echelon_coordinates(v) 

[1, 5] 

sage: vector(QQ, W.echelon_coordinates(v)) * W.echelonized_basis_matrix() 

(1, 5, 9) 

 

sage: v = V([1,5,9]) 

sage: W.coordinates(v) 

[5, -1] 

sage: vector(QQ, W.coordinates(v)) * W.basis_matrix() 

(1, 5, 9) 

 

We can load and save submodules:: 

 

sage: loads(W.dumps()) == W 

True 

 

sage: K.<x> = FractionField(PolynomialRing(QQ,'x')) 

sage: M = K^3; W = M.span_of_basis([[1,1,x]]) 

sage: loads(W.dumps()) == W 

True 

""" 

def __init__(self, ambient, basis, inner_product_matrix, 

check=True, echelonize=False, echelonized_basis=None, already_echelonized=False): 

""" 

Create a vector space with given basis. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: W = V.span_of_basis([[1,2,3],[4,5,6]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[1 2 3] 

[4 5 6] 

sage: V = VectorSpace(QQ, 3, inner_product_matrix=1) 

sage: V.span_of_basis([[1,2,3],[4,5,6]]) 

Quadratic space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[1 2 3] 

[4 5 6] 

Inner product matrix: 

[1 0 0] 

[0 1 0] 

[0 0 1] 

""" 

free_module.FreeModule_submodule_with_basis_field.__init__( 

self, ambient=ambient, basis=basis, check=check, 

echelonize=echelonize, echelonized_basis=echelonized_basis, already_echelonized=already_echelonized) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,5) 

sage: U = V.submodule([ V.gen(i) - V.gen(0) for i in range(1,5) ]) 

sage: U # indirect doctest 

Vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

sage: print(U._repr_()) 

Vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

 

The system representation can be overwritten, but leaves 

:meth:`_repr_` unmodified:: 

 

sage: U.rename('U') 

sage: U 

U 

sage: print(U._repr_()) 

Vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

 

Sparse vector spaces print this fact:: 

 

sage: V = VectorSpace(QQ,5,sparse=True) 

sage: U = V.submodule([ V.gen(i) - V.gen(0) for i in range(1,5) ]) 

sage: U # indirect doctest 

Sparse vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

""" 

if self.is_sparse(): 

return "Sparse quadratic space of degree %s and dimension %s over %s\n"%( 

self.degree(), self.dimension(), self.base_field()) + \ 

"Basis matrix:\n%r" % self.basis_matrix() + \ 

"Inner product matrix:\n%r" % self.inner_product_matrix() 

else: 

return "Quadratic space of degree %s and dimension %s over %s\n"%( 

self.degree(), self.dimension(), self.base_field()) + \ 

"Basis matrix:\n%r\n" % self.basis_matrix() + \ 

"Inner product matrix:\n%r" % self.inner_product_matrix() 

 

class FreeQuadraticModule_submodule_field( 

free_module.FreeModule_submodule_field, FreeQuadraticModule_submodule_with_basis_field): 

""" 

An embedded vector subspace with echelonized basis. 

 

EXAMPLES: 

 

Since this is an embedded vector subspace with echelonized basis, 

the echelon_coordinates() and user coordinates() agree:: 

 

sage: V = QQ^3 

sage: W = V.span([[1,2,3],[4,5,6]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

 

sage: v = V([1,5,9]) 

sage: W.echelon_coordinates(v) 

[1, 5] 

sage: vector(QQ, W.echelon_coordinates(v)) * W.basis_matrix() 

(1, 5, 9) 

 

sage: v = V([1,5,9]) 

sage: W.coordinates(v) 

[1, 5] 

sage: vector(QQ, W.coordinates(v)) * W.basis_matrix() 

(1, 5, 9) 

""" 

def __init__(self, ambient, gens, inner_product_matrix, check=True, already_echelonized=False): 

""" 

Create an embedded vector subspace with echelonized basis. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: W = V.span([[1,2,3],[4,5,6]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

""" 

free_module.FreeModule_submodule_field.__init__( 

self, ambient=ambient, gens=gens, check=check, already_echelonized=already_echelonized) 

#self._FreeQuadraticModule_generic_inner_product_matrix = inner_product_matrix 

self._inner_product_matrix = inner_product_matrix 

 

def _repr_(self): 

""" 

The default printing representation of self. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,5) 

sage: U = V.submodule([ V.gen(i) - V.gen(0) for i in range(1,5) ]) 

sage: U # indirect doctest 

Vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

sage: print(U._repr_()) 

Vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

 

The system representation can be overwritten, but leaves 

:meth:`_repr_` unmodified:: 

 

sage: U.rename('U') 

sage: U 

U 

sage: print(U._repr_()) 

Vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

 

Sparse vector spaces print this fact:: 

 

sage: V = VectorSpace(QQ,5,sparse=True) 

sage: U = V.submodule([ V.gen(i) - V.gen(0) for i in range(1,5) ]) 

sage: U # indirect doctest 

Sparse vector space of degree 5 and dimension 4 over Rational Field 

Basis matrix: 

[ 1 0 0 0 -1] 

[ 0 1 0 0 -1] 

[ 0 0 1 0 -1] 

[ 0 0 0 1 -1] 

""" 

if self.is_sparse(): 

return "Sparse quadratic space of degree %s and dimension %s over %s\n"%( 

self.degree(), self.dimension(), self.base_field()) + \ 

"Basis matrix:\n%r\n" % self.basis_matrix() + \ 

"Inner product matrix:\n%r" % self.inner_product_matrix() 

else: 

return "Quadratic space of degree %s and dimension %s over %s\n"%( 

self.degree(), self.dimension(), self.base_field()) + \ 

"Basis matrix:\n%r\n" % self.basis_matrix() + \ 

"Inner product matrix:\n%r" % self.inner_product_matrix() 

 

#class RealDoubleQuadraticSpace_class(free_module.RealDoubleVectorSpace_class, FreeQuadraticModule_ambient_field): 

# def __init__(self, dimension, inner_product_matrix, sparse=False): 

# if sparse: 

# raise NotImplementedError, "Sparse matrices over RDF not implemented yet" 

# free_module.RealDoubleVectorSpace_class.__init__(self, dimension=dimension, sparse=False) 

# self._inner_product_matrix = inner_product_matrix 

 

#class ComplexDoubleQuadraticSpace_class( 

# free_module.ComplexDoubleVectorSpace_class, FreeQuadraticModule_generic): #FreeQuadraticModule_ambient_field): 

# def __init__(self, dimension, inner_product_matrix, sparse=False): 

# if sparse: 

# raise NotImplementedError, "Sparse matrices over CDF not implemented yet" 

# free_module.ComplexDoubleVectorSpace_class.__init__(self, dimension=dimension, sparse=False) 

# self._inner_product_matrix = inner_product_matrix 

 

######################################################