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

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

5251

5252

5253

5254

5255

5256

5257

5258

5259

5260

5261

5262

5263

5264

5265

5266

5267

5268

5269

5270

5271

5272

5273

5274

5275

5276

5277

5278

5279

5280

5281

5282

5283

5284

5285

5286

5287

5288

5289

5290

5291

5292

5293

5294

5295

5296

5297

5298

5299

5300

5301

5302

5303

5304

5305

5306

5307

5308

5309

5310

5311

5312

5313

5314

5315

5316

5317

5318

5319

5320

5321

5322

5323

5324

5325

5326

5327

5328

5329

5330

5331

5332

5333

5334

5335

5336

5337

5338

5339

5340

5341

5342

5343

5344

5345

5346

5347

5348

5349

5350

5351

5352

5353

5354

5355

5356

5357

5358

5359

5360

5361

5362

5363

5364

5365

5366

5367

5368

5369

5370

5371

5372

5373

5374

5375

5376

5377

5378

5379

5380

5381

5382

5383

5384

5385

5386

5387

5388

5389

5390

5391

5392

5393

5394

5395

5396

5397

5398

5399

5400

5401

5402

5403

5404

5405

5406

5407

5408

5409

5410

5411

5412

5413

5414

5415

5416

5417

5418

5419

5420

5421

5422

5423

5424

5425

5426

5427

5428

5429

5430

5431

5432

5433

5434

5435

5436

5437

5438

5439

5440

5441

5442

5443

5444

5445

5446

5447

5448

5449

5450

5451

5452

5453

5454

5455

5456

5457

5458

5459

5460

5461

5462

5463

5464

5465

5466

5467

5468

5469

5470

5471

5472

5473

5474

5475

5476

5477

5478

5479

5480

5481

5482

5483

5484

5485

5486

5487

5488

5489

5490

5491

5492

5493

5494

5495

5496

5497

5498

5499

5500

5501

5502

5503

5504

5505

5506

5507

5508

5509

5510

5511

5512

5513

5514

5515

5516

5517

5518

5519

5520

5521

5522

5523

5524

5525

5526

5527

5528

5529

5530

5531

5532

5533

5534

5535

5536

5537

5538

5539

5540

5541

5542

5543

5544

5545

5546

5547

5548

5549

5550

5551

5552

5553

5554

5555

5556

5557

5558

5559

5560

5561

5562

5563

5564

5565

5566

5567

5568

5569

5570

5571

5572

5573

5574

5575

5576

5577

5578

5579

5580

5581

5582

5583

5584

5585

5586

5587

5588

5589

5590

5591

5592

5593

5594

5595

5596

5597

5598

5599

5600

5601

5602

5603

5604

5605

5606

5607

5608

5609

5610

5611

5612

5613

5614

5615

5616

5617

5618

5619

5620

5621

5622

5623

5624

5625

5626

5627

5628

5629

5630

5631

5632

5633

5634

5635

5636

5637

5638

5639

5640

5641

5642

5643

5644

5645

5646

5647

5648

5649

5650

5651

5652

5653

5654

5655

5656

5657

5658

5659

5660

5661

5662

5663

5664

5665

5666

5667

5668

5669

5670

5671

5672

5673

5674

5675

5676

5677

5678

5679

5680

5681

5682

5683

5684

5685

5686

5687

5688

5689

5690

5691

5692

5693

5694

5695

5696

5697

5698

5699

5700

5701

5702

5703

5704

5705

5706

5707

5708

5709

5710

5711

5712

5713

5714

5715

5716

5717

5718

5719

5720

5721

5722

5723

5724

5725

5726

5727

5728

5729

5730

5731

5732

5733

5734

5735

5736

5737

5738

5739

5740

5741

5742

5743

5744

5745

5746

5747

5748

5749

5750

5751

5752

5753

5754

5755

5756

5757

5758

5759

5760

5761

5762

5763

5764

5765

5766

5767

5768

5769

5770

5771

5772

5773

5774

5775

5776

5777

5778

5779

5780

5781

5782

5783

5784

5785

5786

5787

5788

5789

5790

5791

5792

5793

5794

5795

5796

5797

5798

5799

5800

5801

5802

5803

5804

5805

5806

5807

5808

5809

5810

5811

5812

5813

5814

5815

5816

5817

5818

5819

5820

5821

5822

5823

5824

5825

5826

5827

5828

5829

5830

5831

5832

5833

5834

5835

5836

5837

5838

5839

5840

5841

5842

5843

5844

5845

5846

5847

5848

5849

5850

5851

5852

5853

5854

5855

5856

5857

5858

5859

5860

5861

5862

5863

5864

5865

5866

5867

5868

5869

5870

5871

5872

5873

5874

5875

5876

5877

5878

5879

5880

5881

5882

5883

5884

5885

5886

5887

5888

5889

5890

5891

5892

5893

5894

5895

5896

5897

5898

5899

5900

5901

5902

5903

5904

5905

5906

5907

5908

5909

5910

5911

5912

5913

5914

5915

5916

5917

5918

5919

5920

5921

5922

5923

5924

5925

5926

5927

5928

5929

5930

5931

5932

5933

5934

5935

5936

5937

5938

5939

5940

5941

5942

5943

5944

5945

5946

5947

5948

5949

5950

5951

5952

5953

5954

5955

5956

5957

5958

5959

5960

5961

5962

5963

5964

5965

5966

5967

5968

5969

5970

5971

5972

5973

5974

5975

5976

5977

5978

5979

5980

5981

5982

5983

5984

5985

5986

5987

5988

5989

5990

5991

5992

5993

5994

5995

5996

5997

5998

5999

6000

6001

6002

6003

6004

6005

6006

6007

6008

6009

6010

6011

6012

6013

6014

6015

6016

6017

6018

6019

6020

6021

6022

6023

6024

6025

6026

6027

6028

6029

6030

6031

6032

6033

6034

6035

6036

6037

6038

6039

6040

6041

6042

6043

6044

6045

6046

6047

6048

6049

6050

6051

6052

6053

6054

6055

6056

6057

6058

6059

6060

6061

6062

6063

6064

6065

6066

6067

6068

6069

6070

6071

6072

6073

6074

6075

6076

6077

6078

6079

6080

6081

6082

6083

6084

6085

6086

6087

6088

6089

6090

6091

6092

6093

6094

6095

6096

6097

6098

6099

6100

6101

6102

6103

6104

6105

6106

6107

6108

6109

6110

6111

6112

6113

6114

6115

6116

6117

6118

6119

6120

6121

6122

6123

6124

6125

6126

6127

6128

6129

6130

6131

6132

6133

6134

6135

6136

6137

6138

6139

6140

6141

6142

6143

6144

6145

6146

6147

6148

6149

6150

6151

6152

6153

6154

6155

6156

6157

6158

6159

6160

6161

6162

6163

6164

6165

6166

6167

6168

6169

6170

6171

6172

6173

6174

6175

6176

6177

6178

6179

6180

6181

6182

6183

6184

6185

6186

6187

6188

6189

6190

6191

6192

6193

6194

6195

6196

6197

6198

6199

6200

6201

6202

6203

6204

6205

6206

6207

6208

6209

6210

6211

6212

6213

6214

6215

6216

6217

6218

6219

6220

6221

6222

6223

6224

6225

6226

6227

6228

6229

6230

6231

6232

6233

6234

6235

6236

6237

6238

6239

6240

6241

6242

6243

6244

6245

6246

6247

6248

6249

6250

6251

6252

6253

6254

6255

6256

6257

6258

6259

6260

6261

6262

6263

6264

6265

6266

6267

6268

6269

6270

6271

6272

6273

6274

6275

6276

6277

6278

6279

6280

6281

6282

6283

6284

6285

6286

6287

6288

6289

6290

6291

6292

6293

6294

6295

6296

6297

6298

6299

6300

6301

6302

6303

6304

6305

6306

6307

6308

6309

6310

6311

6312

6313

6314

6315

6316

6317

6318

6319

6320

6321

6322

6323

6324

6325

6326

6327

6328

6329

6330

6331

6332

6333

6334

6335

6336

6337

6338

6339

6340

6341

6342

6343

6344

6345

6346

6347

6348

6349

6350

6351

6352

6353

6354

6355

6356

6357

6358

6359

6360

6361

6362

6363

6364

6365

6366

6367

6368

6369

6370

6371

6372

6373

6374

6375

6376

6377

6378

6379

6380

6381

6382

6383

6384

6385

6386

6387

6388

6389

6390

6391

6392

6393

6394

6395

6396

6397

6398

6399

6400

6401

6402

6403

6404

6405

6406

6407

6408

6409

6410

6411

6412

6413

6414

6415

6416

6417

6418

6419

6420

6421

6422

6423

6424

6425

6426

6427

6428

6429

6430

6431

6432

6433

6434

6435

6436

6437

6438

6439

6440

6441

6442

6443

6444

6445

6446

6447

6448

6449

6450

6451

6452

6453

6454

6455

6456

6457

6458

6459

6460

6461

6462

6463

6464

6465

6466

6467

6468

6469

6470

6471

6472

6473

6474

6475

6476

6477

6478

6479

6480

6481

6482

6483

6484

6485

6486

6487

6488

6489

6490

6491

6492

6493

6494

6495

6496

6497

6498

6499

6500

6501

6502

6503

6504

6505

6506

6507

6508

6509

6510

6511

6512

6513

6514

6515

6516

6517

6518

6519

6520

6521

6522

6523

6524

6525

6526

6527

6528

6529

6530

6531

6532

6533

6534

6535

6536

6537

6538

6539

6540

6541

6542

6543

6544

6545

6546

6547

6548

6549

6550

6551

6552

6553

6554

6555

6556

6557

6558

6559

6560

6561

6562

6563

6564

6565

6566

6567

6568

6569

6570

6571

6572

6573

6574

6575

6576

6577

6578

6579

6580

6581

6582

6583

6584

6585

6586

6587

6588

6589

6590

6591

6592

6593

6594

6595

6596

6597

6598

6599

6600

6601

6602

6603

6604

6605

6606

6607

6608

6609

6610

6611

6612

6613

6614

6615

6616

6617

6618

6619

6620

6621

6622

6623

6624

6625

6626

6627

6628

6629

6630

6631

6632

6633

6634

6635

6636

6637

6638

6639

6640

6641

6642

6643

6644

6645

6646

6647

6648

6649

6650

6651

6652

6653

6654

6655

6656

6657

6658

6659

6660

6661

6662

6663

6664

6665

6666

6667

6668

6669

6670

6671

6672

6673

6674

6675

6676

6677

6678

6679

6680

6681

6682

6683

6684

6685

6686

6687

6688

6689

6690

6691

6692

6693

6694

6695

6696

6697

6698

6699

6700

6701

6702

6703

6704

6705

6706

6707

6708

6709

6710

6711

6712

6713

6714

6715

6716

6717

6718

6719

6720

6721

6722

6723

6724

6725

6726

6727

6728

6729

6730

6731

6732

6733

6734

6735

6736

6737

6738

6739

6740

6741

6742

6743

6744

6745

6746

6747

6748

6749

6750

6751

6752

6753

6754

6755

6756

6757

6758

6759

6760

6761

6762

6763

6764

6765

6766

6767

6768

6769

6770

6771

6772

6773

6774

6775

6776

6777

6778

6779

6780

6781

6782

6783

6784

6785

6786

6787

6788

6789

6790

6791

6792

6793

6794

6795

6796

6797

6798

6799

6800

6801

6802

6803

6804

6805

6806

6807

6808

6809

6810

6811

6812

6813

6814

6815

6816

6817

6818

6819

6820

6821

6822

6823

6824

6825

6826

6827

6828

6829

6830

6831

6832

6833

6834

6835

6836

6837

6838

6839

6840

6841

6842

6843

6844

6845

6846

6847

6848

6849

6850

6851

6852

6853

6854

6855

6856

6857

6858

6859

6860

6861

6862

6863

6864

6865

6866

6867

6868

6869

6870

6871

6872

6873

6874

6875

6876

6877

6878

6879

6880

6881

6882

6883

6884

6885

6886

6887

6888

6889

6890

6891

6892

6893

6894

6895

6896

6897

6898

6899

6900

6901

6902

6903

6904

6905

6906

6907

6908

6909

6910

6911

6912

6913

6914

6915

6916

6917

6918

6919

6920

6921

6922

6923

6924

6925

6926

6927

6928

6929

6930

6931

6932

6933

6934

6935

6936

6937

6938

6939

6940

6941

6942

6943

6944

6945

6946

6947

6948

6949

6950

6951

6952

6953

6954

6955

6956

6957

6958

6959

6960

6961

6962

6963

6964

6965

6966

6967

6968

6969

6970

6971

6972

6973

6974

6975

6976

6977

6978

6979

6980

6981

6982

6983

6984

6985

6986

6987

6988

6989

6990

6991

6992

6993

6994

6995

6996

6997

6998

6999

7000

7001

7002

7003

7004

7005

7006

7007

7008

7009

7010

7011

7012

7013

7014

7015

7016

7017

7018

7019

7020

7021

7022

7023

7024

7025

7026

7027

7028

7029

7030

7031

7032

7033

7034

7035

7036

7037

7038

7039

7040

7041

7042

7043

7044

7045

7046

7047

7048

7049

7050

7051

7052

7053

7054

7055

7056

7057

7058

7059

7060

7061

7062

7063

7064

7065

7066

7067

7068

7069

7070

7071

7072

7073

7074

7075

7076

7077

7078

7079

7080

7081

7082

7083

7084

7085

7086

7087

7088

7089

7090

7091

7092

7093

7094

7095

7096

7097

7098

7099

7100

7101

7102

7103

7104

7105

7106

7107

7108

7109

7110

7111

7112

7113

7114

7115

7116

7117

7118

7119

7120

7121

7122

7123

7124

7125

7126

7127

7128

7129

7130

7131

7132

7133

7134

7135

7136

7137

7138

7139

7140

7141

7142

7143

7144

7145

7146

7147

7148

7149

7150

7151

7152

7153

7154

7155

7156

7157

7158

7159

7160

7161

7162

7163

7164

7165

7166

7167

7168

7169

7170

7171

7172

7173

7174

7175

7176

7177

7178

7179

7180

7181

7182

7183

7184

7185

7186

7187

7188

7189

7190

7191

7192

7193

7194

7195

7196

7197

7198

7199

7200

7201

7202

7203

7204

7205

7206

7207

7208

7209

7210

7211

7212

7213

7214

7215

7216

7217

7218

7219

7220

7221

7222

7223

7224

7225

7226

7227

7228

7229

7230

7231

7232

7233

7234

7235

7236

7237

7238

7239

7240

7241

7242

7243

7244

7245

7246

7247

7248

7249

7250

7251

7252

7253

7254

7255

7256

7257

7258

7259

7260

7261

7262

7263

7264

7265

7266

7267

7268

7269

7270

7271

7272

7273

7274

7275

7276

7277

7278

7279

7280

7281

7282

7283

7284

7285

7286

7287

7288

7289

7290

7291

7292

7293

7294

7295

7296

7297

7298

7299

7300

7301

7302

7303

7304

7305

7306

7307

7308

7309

7310

7311

7312

7313

7314

7315

7316

7317

7318

7319

7320

7321

7322

7323

7324

7325

7326

7327

7328

7329

7330

7331

7332

7333

7334

7335

7336

r""" 

Free modules 

 

Sage supports computation with free modules over an arbitrary commutative ring. 

Nontrivial functionality is available over `\ZZ`, fields, and some principal 

ideal domains (e.g. `\QQ[x]` and rings of integers of number 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)``. Equivalently, ``R^n`` also creates that free 

module. 

 

The following example illustrates the creation of both a vector space 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: V = VectorSpace(QQ,3) 

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

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -7] 

[ 0 1 7] 

sage: C = VectorSpaces(FiniteField(7)) 

sage: C 

Category of vector spaces over Finite Field of size 7 

sage: C(W) 

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

Basis matrix: 

[1 0 0] 

[0 1 0] 

 

:: 

 

sage: M = ZZ^3 

sage: C = VectorSpaces(FiniteField(7)) 

sage: C(M) 

Vector space of dimension 3 over Finite Field of size 7 

sage: W = M.submodule([[1,2,7], [8,8,0]]) 

sage: C(W) 

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

Basis matrix: 

[1 0 0] 

[0 1 0] 

 

We illustrate the exponent notation for creation of free modules. 

 

:: 

 

sage: ZZ^4 

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

sage: QQ^2 

Vector space of dimension 2 over Rational Field 

sage: RR^3 

Vector space of dimension 3 over Real Field with 53 bits of precision 

 

Base ring:: 

 

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

sage: M = FreeModule(R,2) 

sage: M.base_ring() 

Multivariate Polynomial Ring in x, y over Rational Field 

 

:: 

 

sage: VectorSpace(QQ, 10).base_ring() 

Rational Field 

 

TESTS: 

 

We intersect a zero-dimensional vector space with a 

1-dimension submodule. 

 

:: 

 

sage: V = (QQ^1).span([]) 

sage: W = ZZ^1 

sage: V.intersection(W) 

Free module of degree 1 and rank 0 over Integer Ring 

Echelon basis matrix: 

[] 

 

We construct subspaces of real and complex double vector spaces and 

verify that the element types are correct:: 

 

sage: V = FreeModule(RDF, 3); V 

Vector space of dimension 3 over Real Double Field 

sage: V.0 

(1.0, 0.0, 0.0) 

sage: type(V.0) 

<type 'sage.modules.vector_real_double_dense.Vector_real_double_dense'> 

sage: W = V.span([V.0]); W 

Vector space of degree 3 and dimension 1 over Real Double Field 

Basis matrix: 

[1.0 0.0 0.0] 

sage: type(W.0) 

<type 'sage.modules.vector_real_double_dense.Vector_real_double_dense'> 

sage: V = FreeModule(CDF, 3); V 

Vector space of dimension 3 over Complex Double Field 

sage: type(V.0) 

<type 'sage.modules.vector_complex_double_dense.Vector_complex_double_dense'> 

sage: W = V.span_of_basis([CDF.0 * V.1]); W 

Vector space of degree 3 and dimension 1 over Complex Double Field 

User basis matrix: 

[ 0.0 1.0*I 0.0] 

sage: type(W.0) 

<type 'sage.modules.vector_complex_double_dense.Vector_complex_double_dense'> 

 

Basis vectors are immutable:: 

 

sage: A = span([[1,2,3], [4,5,6]], ZZ) 

sage: A.0 

(1, 2, 3) 

sage: A.0[0] = 5 

Traceback (most recent call last): 

... 

ValueError: vector is immutable; please change a copy instead (use copy()) 

 

Among other things, this tests that we can save and load submodules 

and elements:: 

 

sage: M = ZZ^3 

sage: TestSuite(M).run() 

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

sage: TestSuite(W).run() 

sage: v = W.0 + W.1 

sage: TestSuite(v).run() 

 

AUTHORS: 

 

- William Stein (2005, 2007) 

 

- David Kohel (2007, 2008) 

 

- Niles Johnson (2010-08): (:trac:`3893`) ``random_element()`` should pass on ``*args`` and ``**kwds``. 

 

- Simon King (2010-12): 

:trac:`8800` : Fixing a bug in ``denominator()``. 

 

- Simon King (2010-12), Peter Bruin (June 2014): 

:trac:`10513` : New coercion model and category framework. 

 

""" 

 

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

# Copyright (C) 2005, 2007 William Stein <wstein@gmail.com> 

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

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function, absolute_import 

from six import integer_types 

 

from . import free_module_element 

import sage.matrix.matrix_space 

import sage.misc.latex as latex 

 

from sage.modules.module import Module 

import sage.rings.finite_rings.finite_field_constructor as finite_field 

import sage.rings.ring as ring 

import sage.rings.integer_ring 

import sage.rings.rational_field 

import sage.rings.finite_rings.integer_mod_ring 

import sage.rings.infinity 

import sage.rings.integer 

from sage.categories.principal_ideal_domains import PrincipalIdealDomains 

from sage.misc.randstate import current_randstate 

from sage.structure.sequence import Sequence 

from sage.structure.richcmp import (richcmp_method, rich_to_bool, richcmp, 

richcmp_not_equal, revop, 

op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE) 

from sage.misc.cachefunc import cached_method 

from sage.misc.superseded import deprecation 

 

from warnings import warn 

 

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

# 

# Constructor functions 

# 

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

 

from sage.structure.factory import UniqueFactory 

 

class FreeModuleFactory(UniqueFactory): 

r""" 

Create the free module over the given commutative ring of the given 

rank. 

 

INPUT: 

 

 

- ``base_ring`` - a commutative ring 

 

- ``rank`` - a nonnegative integer 

 

- ``sparse`` - bool; (default False) 

 

- ``inner_product_matrix`` - the inner product 

matrix (default None) 

 

 

OUTPUT: a free module 

 

.. note:: 

 

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

sparse free ambient module of rank `n` over `R`. 

 

EXAMPLES: 

 

First we illustrate creating free modules over various base fields. 

The base field affects the free module that is created. For 

example, free modules over a field are vector spaces, and free 

modules over a principal ideal domain are special in that more 

functionality is available for them than for completely general 

free modules. 

 

:: 

 

sage: FreeModule(Integers(8),10) 

Ambient free module of rank 10 over Ring of integers modulo 8 

sage: FreeModule(QQ,10) 

Vector space of dimension 10 over Rational Field 

sage: FreeModule(ZZ,10) 

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

sage: FreeModule(FiniteField(5),10) 

Vector space of dimension 10 over Finite Field of size 5 

sage: FreeModule(Integers(7),10) 

Vector space of dimension 10 over Ring of integers modulo 7 

sage: FreeModule(PolynomialRing(QQ,'x'),5) 

Ambient free module of rank 5 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field 

sage: FreeModule(PolynomialRing(ZZ,'x'),5) 

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

 

Of course we can make rank 0 free modules:: 

 

sage: FreeModule(RealField(100),0) 

Vector space of dimension 0 over Real Field with 100 bits of precision 

 

Next we create a free module with sparse representation of 

elements. Functionality with sparse modules is *identical* to dense 

modules, but they may use less memory and arithmetic may be faster 

(or slower!). 

 

:: 

 

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

sage: M.is_sparse() 

True 

sage: type(M.0) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

 

The default is dense. 

 

:: 

 

sage: M = ZZ^200 

sage: type(M.0) 

<type 'sage.modules.vector_integer_dense.Vector_integer_dense'> 

 

Note that matrices associated in some way to sparse free modules 

are sparse by default:: 

 

sage: M = FreeModule(Integers(8), 2) 

sage: A = M.basis_matrix() 

sage: A.is_sparse() 

False 

sage: Ms = FreeModule(Integers(8), 2, sparse=True) 

sage: M == Ms # as mathematical objects they are equal 

True 

sage: Ms.basis_matrix().is_sparse() 

True 

 

We can also specify an inner product matrix, which is used when 

computing inner products of elements. 

 

:: 

 

sage: A = MatrixSpace(ZZ,2)([[1,0],[0,-1]]) 

sage: M = FreeModule(ZZ,2,inner_product_matrix=A) 

sage: v, w = M.gens() 

sage: v.inner_product(w) 

0 

sage: v.inner_product(v) 

1 

sage: w.inner_product(w) 

-1 

sage: (v+2*w).inner_product(w) 

-2 

 

You can also specify the inner product matrix by giving anything 

that coerces to an appropriate matrix. This is only useful if the 

inner product matrix takes values in the base ring. 

 

:: 

 

sage: FreeModule(ZZ,2,inner_product_matrix=1).inner_product_matrix() 

[1 0] 

[0 1] 

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

[1 2] 

[3 4] 

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

[1 2] 

[3 4] 

 

.. todo:: 

 

Refactor modules such that it only counts what category the base 

ring belongs to, but not what is its Python class. 

 

""" 

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

""" 

TESTS:: 

 

sage: loads(dumps(ZZ^6)) is ZZ^6 

True 

sage: loads(dumps(RDF^3)) is RDF^3 

True 

 

TODO: replace the above by ``TestSuite(...).run()``, once 

:meth:`_test_pickling` will test unique representation and not 

only equality. 

""" 

rank = int(sage.rings.integer.Integer(rank)) 

 

if not (inner_product_matrix is None): 

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

inner_product_matrix.set_immutable() 

 

return (base_ring, rank, sparse, inner_product_matrix) 

 

def create_object(self, version, key): 

 

base_ring, rank, sparse, inner_product_matrix = key 

 

if inner_product_matrix is not None: 

from .free_quadratic_module import FreeQuadraticModule 

return FreeQuadraticModule(base_ring, rank, inner_product_matrix=inner_product_matrix, sparse=sparse) 

 

if not isinstance(sparse,bool): 

raise TypeError("Argument sparse (= %s) must be True or False" % sparse) 

 

if not (hasattr(base_ring,'is_commutative') and base_ring.is_commutative()): 

warn("""You are constructing a free module 

over a noncommutative ring. Sage does not have a concept 

of left/right and both sided modules, so be careful. 

It's also not guaranteed that all multiplications are 

done from the right side.""") 

 

# raise TypeError, "The base_ring must be a commutative ring." 

 

try: 

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

return RealDoubleVectorSpace_class(rank) 

 

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

return ComplexDoubleVectorSpace_class(rank) 

 

elif base_ring.is_field(): 

return FreeModule_ambient_field(base_ring, rank, sparse=sparse) 

 

elif base_ring in PrincipalIdealDomains(): 

return FreeModule_ambient_pid(base_ring, rank, sparse=sparse) 

 

elif isinstance(base_ring, sage.rings.number_field.order.Order) \ 

and base_ring.is_maximal() and base_ring.class_number() == 1: 

return FreeModule_ambient_pid(base_ring, rank, sparse=sparse) 

 

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

return FreeModule_ambient_domain(base_ring, rank, sparse=sparse) 

 

else: 

return FreeModule_ambient(base_ring, rank, sparse=sparse) 

except NotImplementedError: 

return FreeModule_ambient(base_ring, rank, sparse=sparse) 

 

 

FreeModule = FreeModuleFactory("FreeModule") 

 

 

def VectorSpace(K, dimension, sparse=False, inner_product_matrix=None): 

""" 

EXAMPLES: 

 

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

 

:: 

 

sage: V = VectorSpace(FractionField(PolynomialRing(ZZ,'x')),3) 

sage: V 

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

sage: V.basis() 

[ 

(1, 0, 0), 

(0, 1, 0), 

(0, 0, 1) 

] 

 

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

 

:: 

 

sage: VectorSpace(ZZ,5) 

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 FreeModule(K, rank=dimension, sparse=sparse, inner_product_matrix=inner_product_matrix) 

 

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

# 

# The span of vectors 

# 

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

 

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

r""" 

Return the span of the vectors in ``gens`` using scalars from ``base_ring``. 

 

INPUT: 

 

- ``gens`` - a list of either vectors or lists of ring elements 

used to generate the span 

 

- ``base_ring`` - default: ``None`` - a principal ideal domain 

for the ring of scalars 

 

- ``check`` - default: ``True`` - passed to the ``span()`` method 

of the ambient module 

 

- ``already_echelonized`` - default: ``False`` - set to ``True`` 

if the vectors form the rows of a matrix in echelon form, in 

order to skip the computation of an echelonized basis for the 

span. 

 

OUTPUT: 

 

A module (or vector space) that is all the linear combinations of the 

free module elements (or vectors) with scalars from the 

ring (or field) given by ``base_ring``. See the examples below 

describing behavior when the base ring is not specified and/or 

the module elements are given as lists that do not carry 

explicit base ring information. 

 

EXAMPLES: 

 

The vectors in the list of generators can be given as 

lists, provided a base ring is specified and the elements of the list 

are in the ring (or the fraction field of the ring). If the 

base ring is a field, the span is a vector space. :: 

 

sage: V = span([[1,2,5], [2,2,2]], QQ); V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -3] 

[ 0 1 4] 

 

sage: span([V.gen(0)], QuadraticField(-7,'a')) 

Vector space of degree 3 and dimension 1 over Number Field in a with defining polynomial x^2 + 7 

Basis matrix: 

[ 1 0 -3] 

 

sage: span([[1,2,3], [2,2,2], [1,2,5]], GF(2)) 

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

Basis matrix: 

[1 0 1] 

 

If the base ring is not a field, then a module is created. 

The entries of the vectors can lie outside the ring, if they 

are in the fraction field of the ring. :: 

 

sage: span([[1,2,5], [2,2,2]], ZZ) 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1 0 -3] 

[ 0 2 8] 

 

sage: span([[1,1,1], [1,1/2,1]], ZZ) 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1 0 1] 

[ 0 1/2 0] 

 

sage: R.<x> = QQ[] 

sage: M= span( [[x, x^2+1], [1/x, x^3]], R); M 

Free module of degree 2 and rank 2 over 

Univariate Polynomial Ring in x over Rational Field 

Echelon basis matrix: 

[ 1/x x^3] 

[ 0 x^5 - x^2 - 1] 

sage: M.basis()[0][0].parent() 

Fraction Field of Univariate Polynomial Ring in x over Rational Field 

 

A base ring can be inferred if the generators are given as a 

list of vectors. :: 

 

sage: span([vector(QQ, [1,2,3]), vector(QQ, [4,5,6])]) 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: span([vector(QQ, [1,2,3]), vector(ZZ, [4,5,6])]) 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: span([vector(ZZ, [1,2,3]), vector(ZZ, [4,5,6])]) 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 2 3] 

[0 3 6] 

 

TESTS:: 

 

sage: span([[1,2,3], [2,2,2], [1,2/3,5]], ZZ) 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[ 1 0 13] 

[ 0 2/3 6] 

[ 0 0 14] 

sage: span([[1,2,3], [2,2,2], [1,2,QQ['x'].gen()]], ZZ) 

Traceback (most recent call last): 

... 

ValueError: The elements of gens (= [[1, 2, 3], [2, 2, 2], [1, 2, x]]) must be defined over base_ring (= Integer Ring) or its field of fractions. 

 

For backwards compatibility one can also give the base ring as the 

first argument. :: 

 

sage: span(QQ,[[1,2],[3,4]]) 

Vector space of degree 2 and dimension 2 over Rational Field 

Basis matrix: 

[1 0] 

[0 1] 

 

The base ring must be a principal ideal domain (PID). :: 

 

sage: span([[1,2,3]], Integers(6)) 

Traceback (most recent call last): 

... 

TypeError: The base_ring (= Ring of integers modulo 6) 

must be a principal ideal domain. 

 

Fix :trac:`5575`:: 

 

sage: V = QQ^3 

sage: span([V.0, V.1]) 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[1 0 0] 

[0 1 0] 

 

Improve error message from :trac:`12541`:: 

 

sage: span({0:vector([0,1])}, QQ) 

Traceback (most recent call last): 

... 

TypeError: generators must be lists of ring elements 

or free module elements! 

""" 

if ring.is_Ring(gens): 

# we allow the old input format with first input the base_ring. 

# Do we want to deprecate it?.. 

base_ring, gens = gens, base_ring 

 

try: 

if base_ring is None: 

gens = Sequence(gens) 

R = gens.universe().base_ring() 

else: 

gens = list(gens) 

R = base_ring 

except TypeError: 

raise TypeError("generators must be given as an iterable structure!") 

 

if R not in PrincipalIdealDomains(): 

raise TypeError("The base_ring (= %s) must be a principal ideal " 

"domain." % R) 

if len(gens) == 0: 

return FreeModule(R, 0) 

else: 

x = gens[0] 

if free_module_element.is_FreeModuleElement(x): 

M = x.parent() 

else: 

try: 

x = list(x) 

except TypeError: 

raise TypeError("generators must be lists of ring elements or " 

"free module elements!") 

M = FreeModule(R, len(x)) 

try: 

gens = [M(_) for _ in gens] 

except TypeError: 

R = R.fraction_field() 

M = FreeModule(R, len(x)) 

try: 

gens = [M(_) for _ in gens] 

except TypeError: 

raise ValueError("The elements of gens (= %s) must be " 

"defined over base_ring (= %s) or its " 

"field of fractions." % (gens, base_ring)) 

return M.span(gens=gens, base_ring=base_ring, check=check, 

already_echelonized=already_echelonized) 

 

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

# 

# Base class for all free modules 

# 

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

 

def is_FreeModule(M): 

""" 

Return True if M inherits from FreeModule_generic. 

 

EXAMPLES:: 

 

sage: from sage.modules.free_module import is_FreeModule 

sage: V = ZZ^3 

sage: is_FreeModule(V) 

True 

sage: W = V.span([ V.random_element() for i in range(2) ]) 

sage: is_FreeModule(W) 

True 

""" 

return isinstance(M, FreeModule_generic) 

 

@richcmp_method 

class FreeModule_generic(Module): 

""" 

Base class for all free modules. 

 

TESTS: 

 

Check that :trac:`17576` is fixed:: 

 

sage: V = VectorSpace(RDF, 3) 

sage: v = vector(RDF, [1, 2, 3, 4]) 

sage: v in V 

False 

""" 

def __init__(self, base_ring, rank, degree, sparse=False, 

coordinate_ring=None, category=None): 

""" 

Create the free module of given rank ``rank`` over the given base 

ring ``base_ring``. 

 

INPUT: 

 

- ``base_ring`` -- a commutative ring 

 

- ``rank`` -- a non-negative integer 

 

- ``degree`` -- a non-negative integer 

 

- ``sparse`` -- bool (default: False) 

 

- ``coordinate_ring`` -- a ring containing ``base_ring`` 

(default: equal to ``base_ring``) 

 

- ``category`` -- category (default: None) 

 

If ``base_ring`` is a field, then the default category is the 

category of finite-dimensional vector spaces over that field; 

otherwise it is the category of finite-dimensional free modules 

over that ring. In addition, the category is intersected with the 

category of finite enumerated sets if the ring is finite or the 

rank is 0. 

 

EXAMPLES:: 

 

sage: PolynomialRing(QQ,3,'x')^3 

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

 

sage: FreeModule(GF(7),3).category() 

Category of enumerated finite dimensional vector spaces with basis over 

(finite enumerated fields and subquotients of monoids and quotients of semigroups) 

sage: V = QQ^4; V.category() 

Category of finite dimensional vector spaces with basis over 

(number fields and quotient fields and metric spaces) 

sage: V = GF(5)**20; V.category() 

Category of enumerated finite dimensional vector spaces with basis over (finite enumerated fields and subquotients of monoids and quotients of semigroups) 

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

Category of finite dimensional modules with basis over 

(euclidean domains and infinite enumerated sets 

and metric spaces) 

sage: (QQ^0).category() 

Category of finite enumerated finite dimensional vector spaces with basis 

over (number fields and quotient fields and metric spaces) 

 

TESTS:: 

 

sage: M = FreeModule(ZZ,20,sparse=False) 

sage: x = M.random_element() 

sage: type(x) 

<type 'sage.modules.vector_integer_dense.Vector_integer_dense'> 

sage: M.element_class 

<type 'sage.modules.vector_integer_dense.Vector_integer_dense'> 

 

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

sage: y = N.random_element() 

sage: type(y) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

sage: N.element_class 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

 

""" 

if not base_ring.is_commutative(): 

warn("""You are constructing a free module 

over a noncommutative ring. Sage does not have a concept 

of left/right and both sided modules, so be careful. 

It's also not guaranteed that all multiplications are 

done from the right side.""") 

 

if coordinate_ring is None: 

coordinate_ring = base_ring 

 

if not hasattr(self, 'Element'): 

self.Element = element_class(coordinate_ring, sparse) 

 

rank = sage.rings.integer.Integer(rank) 

if rank < 0: 

raise ValueError("rank (=%s) must be nonnegative"%rank) 

degree = sage.rings.integer.Integer(degree) 

if degree < 0: 

raise ValueError("degree (=%s) must be nonnegative"%degree) 

 

if category is None: 

from sage.categories.all import FreeModules 

category = FreeModules(base_ring.category()).FiniteDimensional() 

try: 

if base_ring.is_finite() or rank == 0: 

category = category.Enumerated().Finite() 

except Exception: 

pass 

 

super(FreeModule_generic, self).__init__(base_ring, category=category) 

self.__coordinate_ring = coordinate_ring 

self.__uses_ambient_inner_product = True 

self.__rank = rank 

self.__degree = degree 

self.__is_sparse = sparse 

self._gram_matrix = None 

 

def construction(self): 

""" 

The construction functor and base ring for self. 

 

EXAMPLES:: 

 

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

sage: V = R^5 

sage: V.construction() 

(VectorFunctor, Multivariate Polynomial Ring in x0, x1, x2 over Rational Field) 

""" 

from sage.categories.pushout import VectorFunctor 

if hasattr(self,'_inner_product_matrix'): 

return VectorFunctor(self.rank(), self.is_sparse(),self.inner_product_matrix()), self.base_ring() 

return VectorFunctor(self.rank(), self.is_sparse()), self.base_ring() 

 

# FIXME: what's the level of generality of FreeModuleHomspace? 

# Should there be a category for free modules accepting it as hom space? 

# See similar method for FreeModule_generic_field class 

def _Hom_(self, Y, category): 

from .free_module_homspace import FreeModuleHomspace 

return FreeModuleHomspace(self, Y, category) 

 

def dense_module(self): 

""" 

Return corresponding dense module. 

 

EXAMPLES: 

 

We first illustrate conversion with ambient spaces:: 

 

sage: M = FreeModule(QQ,3) 

sage: S = FreeModule(QQ,3, sparse=True) 

sage: M.sparse_module() 

Sparse vector space of dimension 3 over Rational Field 

sage: S.dense_module() 

Vector space of dimension 3 over Rational Field 

sage: M.sparse_module() == S 

True 

sage: S.dense_module() == M 

True 

sage: M.dense_module() == M 

True 

sage: S.sparse_module() == S 

True 

 

Next we create a subspace:: 

 

sage: M = FreeModule(QQ,3, sparse=True) 

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

Sparse vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 2 3] 

sage: V.sparse_module() 

Sparse vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 2 3] 

""" 

if self.is_sparse(): 

return self._dense_module() 

return self 

 

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: M = FreeModule(Integers(8),3) 

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

sage: M is S._dense_module() 

True 

""" 

A = self.ambient_module().dense_module() 

return A.span(self.basis()) 

 

def sparse_module(self): 

""" 

Return the corresponding sparse module with the same defining 

data. 

 

EXAMPLES: 

 

We first illustrate conversion with ambient spaces:: 

 

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

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

sage: M.sparse_module() 

Ambient sparse free module of rank 3 over Ring of integers modulo 8 

sage: S.dense_module() 

Ambient free module of rank 3 over Ring of integers modulo 8 

sage: M.sparse_module() is S 

True 

sage: S.dense_module() is M 

True 

sage: M.dense_module() is M 

True 

sage: S.sparse_module() is S 

True 

 

Next we convert a subspace:: 

 

sage: M = FreeModule(QQ,3) 

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

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 2 3] 

sage: V.sparse_module() 

Sparse vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 2 3] 

""" 

if self.is_sparse(): 

return self 

return self._sparse_module() 

 

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: M = FreeModule(Integers(8),3) 

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

sage: M._sparse_module() is S 

True 

""" 

A = self.ambient_module().sparse_module() 

return A.span(self.basis()) 

 

def _an_element_(self): 

""" 

Returns an arbitrary element of a free module. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,2) 

sage: V._an_element_() 

(1, 0) 

sage: U = V.submodule([[1,0]]) 

sage: U._an_element_() 

(1, 0) 

sage: W = V.submodule([]) 

sage: W._an_element_() 

(0, 0) 

""" 

try: 

return self.gen(0) 

except ValueError: 

return self(0) 

 

def some_elements(self): 

r""" 

Return some elements of this free module. 

 

See :class:`TestSuite` for a typical use case. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: F = FreeModule(ZZ, 2) 

sage: tuple(F.some_elements()) 

((1, 0), 

(1, 1), 

(0, 1), 

(-1, 2), 

(-2, 3), 

... 

(-49, 50)) 

 

sage: F = FreeModule(QQ, 3) 

sage: tuple(F.some_elements()) 

((1, 0, 0), 

(1/2, 1/2, 1/2), 

(1/2, -1/2, 2), 

(-2, 0, 1), 

(-1, 42, 2/3), 

(-2/3, 3/2, -3/2), 

(4/5, -4/5, 5/4), 

... 

(46/103823, -46/103823, 103823/46)) 

 

sage: F = FreeModule(SR, 2) 

sage: tuple(F.some_elements()) 

((1, 0), (some_variable, some_variable)) 

""" 

from itertools import islice 

yield self.an_element() 

yield self.base().an_element() * sum(self.gens()) 

some_elements_base = iter(self.base().some_elements()) 

n = self.degree() 

while True: 

L = list(islice(some_elements_base, n)) 

if len(L) != n: 

return 

try: 

yield self(L) 

except (TypeError, ValueError): 

pass 

 

def _element_constructor_(self, x, coerce=True, copy=True, check=True): 

r""" 

Create an element of this free module from ``x``. 

 

The ``coerce`` and ``copy`` arguments are 

passed on to the underlying element constructor. If 

``check`` is ``True``, confirm that the 

element specified by x does in fact lie in self. 

 

.. note:: 

 

In the case of an inexact base ring (i.e. RDF), we don't 

verify that the element is in the subspace, even when 

``check=True``, to account for numerical instability 

issues. 

 

EXAMPLES:: 

 

sage: M = ZZ^4 

sage: M([1,-1,0,1]) #indirect doctest 

(1, -1, 0, 1) 

sage: M(0) 

(0, 0, 0, 0) 

 

:: 

 

sage: N = M.submodule([[1,0,0,0], [0,1,1,0]]) 

sage: N([1,1,1,0]) 

(1, 1, 1, 0) 

sage: N((3,-2,-2,0)) 

(3, -2, -2, 0) 

sage: N((0,0,0,1)) 

Traceback (most recent call last): 

... 

TypeError: element (0, 0, 0, 1) is not in free module 

 

Beware that using check=False can create invalid results:: 

 

sage: N((0,0,0,1), check=False) 

(0, 0, 0, 1) 

sage: N((0,0,0,1), check=False) in N 

True 

""" 

if (isinstance(x, integer_types + (sage.rings.integer.Integer,)) and 

x == 0): 

return self.zero_vector() 

elif isinstance(x, free_module_element.FreeModuleElement): 

if x.parent() is self: 

if copy: 

return x.__copy__() 

else: 

return x 

x = x.list() 

if check and self.coordinate_ring().is_exact(): 

if isinstance(self, FreeModule_ambient): 

return self.element_class(self, x, coerce, copy) 

try: 

c = self.coordinates(x) 

R = self.base_ring() 

for d in c: 

if d not in R: 

raise ArithmeticError 

except ArithmeticError: 

raise TypeError("element {!r} is not in free module".format(x)) 

return self.element_class(self, x, coerce, copy) 

 

def __richcmp__(self, other, op): 

""" 

Rich comparison via containment in the same ambient space. 

 

Two modules compare if their ambient module/space is equal. 

Ambient spaces are equal if they have the same 

base ring, rank and inner product matrix. 

 

EXAMPLES: 

 

We compare rank three free modules over the integers, 

rationals, and complex numbers. Note the free modules 

``QQ^3`` (and hence ``ZZ^3``) and ``CC^3`` are incomparable 

because of the different ambient vector spaces:: 

 

sage: QQ^3 <= CC^3 

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: CC^3 <= QQ^3 

False 

sage: QQ^3 <= QQ^3 

True 

 

sage: QQ^3 <= ZZ^3 

False 

sage: ZZ^3 <= QQ^3 

True 

sage: ZZ^3 <= CC^3 

False 

sage: CC^3 <= ZZ^3 

False 

 

Comparison with a submodule:: 

 

sage: A = QQ^3 

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

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: V <= A 

True 

sage: A <= V 

False 

sage: L1 = span([[1,2,3], [5,6,7], [8,9,10]], ZZ) 

sage: L2 = span([[2,4,6], [10,12,14], [16,18,20]], ZZ) 

sage: 2*L1 <= L2 

True 

sage: L2 <= L1 

True 

sage: L1 <= L2 

False 

 

More exotic comparisons:: 

 

sage: R1 = ZZ[sqrt(2)] 

sage: F1 = R1^3 

sage: V1 = F1.span([[sqrt(2),sqrt(2),0]]) 

sage: F2 = ZZ^3 

sage: V2 = F2.span([[2,2,0]]) 

sage: V2 <= V1 # Different ambient vector spaces 

False 

sage: V1 <= V2 

False 

sage: R2 = GF(5)[x] 

sage: F3 = R2^3 

sage: V3 = F3.span([[x^5-1,1+x+x^2+x^3+x^4,0]]) 

sage: W3 = F3.span([[1,1,0],[0,4,0]]) 

sage: V3 <= W3 

True 

sage: W3 <= V3 

False 

 

We compare a one dimensional space to a two dimensional space:: 

 

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

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

sage: M <= V 

True 

sage: V <= M 

False 

 

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

 

sage: A = (QQ^1).span([[1/3]],ZZ); B = (QQ^1).span([[1]],ZZ); 

sage: A.intersection(B) 

Free module of degree 1 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1] 

 

We create the module `\ZZ^3`, and the submodule generated by 

one vector `(1,1,0)`, and check whether certain elements are 

in the submodule:: 

 

sage: R = FreeModule(ZZ, 3) 

sage: V = R.submodule([R.gen(0) + R.gen(1)]) 

sage: R.gen(0) + R.gen(1) in V 

True 

sage: R.gen(0) + 2*R.gen(1) in V 

False 

 

sage: w = (1/2)*(R.gen(0) + R.gen(1)) 

sage: w 

(1/2, 1/2, 0) 

sage: w.parent() 

Vector space of dimension 3 over Rational Field 

sage: w in V 

False 

sage: V.coordinates(w) 

[1/2] 

 

TESTS:: 

 

sage: QQ^3 < ZZ^3 

False 

sage: ZZ^3 < QQ^3 

True 

sage: ZZ^3 < CC^3 

False 

sage: CC^3 < ZZ^3 

False 

 

Comparison with a submodule:: 

 

sage: A = QQ^3 

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

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: V < A 

True 

sage: A < V 

False 

sage: L1 = span([[1,2,3], [5,6,7], [8,9,10]], ZZ) 

sage: L2 = span([[2,4,6], [10,12,14], [16,18,20]], ZZ) 

sage: 2*L1 < L2 

False 

sage: L2 < L1 

True 

sage: L1 < L2 

False 

 

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

 

sage: V = span([[5,6,7]], ZZ).scale(1/11); V 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[5/11 6/11 7/11] 

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

sage: V < M 

True 

sage: M < V 

False 

 

We compare rank three free modules over the rationals and 

complex numbers:: 

 

sage: QQ^3 >= CC^3 

False 

sage: CC^3 >= QQ^3 

False 

sage: QQ^3 >= QQ^3 

True 

 

Comparison with a submodule:: 

 

sage: A = QQ^3 

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

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: V >= A 

False 

sage: A >= V 

True 

sage: L1 = span([[1,2,3], [5,6,7], [8,9,10]], ZZ) 

sage: L2 = span([[2,4,6], [10,12,14], [16,18,20]], ZZ) 

sage: 2*L1 >= L2 

True 

sage: L2 >= L1 

False 

sage: L1 >= L2 

True 

 

We compare rank three free modules over the rationals and 

complex numbers:: 

 

sage: QQ^3 > CC^3 

False 

sage: CC^3 > QQ^3 

False 

sage: QQ^3 > QQ^3 

False 

 

Comparison with a submodule:: 

 

sage: A = QQ^3 

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

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: V > A 

False 

sage: A > V 

True 

sage: L1 = span([[1,2,3], [5,6,7], [8,9,10]], ZZ) 

sage: L2 = span([[2,4,6], [10,12,14], [16,18,20]], ZZ) 

sage: 2*L1 > L2 

False 

sage: L2 > L1 

False 

sage: L1 > L2 

True 

""" 

if self is other: 

return rich_to_bool(op, 0) 

if not isinstance(other, FreeModule_generic): 

return NotImplemented 

# Check equality first if needed 

if op == op_EQ: 

return self._eq(other) 

if op == op_NE: 

return not self._eq(other) 

deprecation(23878,"The default order on free modules has changed. " 

"The old ordering is in sage.modules.free_module.EchelonMatrixKey") 

if op == op_LE: 

return self.is_submodule(other) 

if op == op_GE: 

return other.is_submodule(self) 

if op == op_LT: 

return (not self._eq(other)) and self.is_submodule(other) 

if op == op_GT: 

return (not self._eq(other)) and other.is_submodule(self) 

 

def _eq(self, other): 

r""" 

Return if ``self`` is equal to ``other``. 

 

Ambient spaces are considered equal if they have the same 

rank, basering and inner product matrix. 

 

Modules in the same ambient space are partially ordered by inclusion. 

 

EXAMPLES:: 

 

sage: L = IntegralLattice("U") 

sage: L is IntegralLattice("U") 

False 

sage: L._eq(IntegralLattice("U")) 

True 

""" 

if self.rank() != other.rank(): 

return False 

if self.base_ring() != other.base_ring(): 

return False 

# We do not want to create an inner product matrix in memory if 

# self and other use the dot product 

if not (self._inner_product_is_dot_product() 

and other._inner_product_is_dot_product()): 

# This only affects free_quadratic_modules 

if self.inner_product_matrix() != other.inner_product_matrix(): 

return False 

from sage.modules.quotient_module import FreeModule_ambient_field_quotient 

lq = isinstance(self, FreeModule_ambient_field_quotient) 

rq = isinstance(other, FreeModule_ambient_field_quotient) 

if lq or rq: 

# if the relations agree we continue with the covers. 

if lq: 

lx = self.relations() 

self = self.cover() 

else: 

lx = self.zero_submodule() 

if rq: 

rx = other.relations() 

other = other.cover() 

else: 

rx = other.zero_submodule() 

if lx != rx: 

return False 

if isinstance(self, FreeModule_ambient) and isinstance(other, FreeModule_ambient): 

return True 

# self and other are not ambient. 

# but they are contained in the same ambient space 

 

# We use self.echelonized_basis_matrix() == other.echelonized_basis_matrix() 

# with the matrix to avoid a circular reference. 

from sage.rings.integer_ring import IntegerRing 

if self.base_ring().is_field() or self.base_ring() is IntegerRing(): 

# We know that the Hermite normal form is unique here. 

return self.echelonized_basis_matrix() == other.echelonized_basis_matrix() 

return self.is_submodule(other) and other.is_submodule(self) 

 

def is_submodule(self, other): 

r""" 

Return ``True`` if ``self`` is a submodule of ``other``. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ,3) 

sage: V = M.ambient_vector_space() 

sage: X = V.span([[1/2,1/2,0],[1/2,0,1/2]], ZZ) 

sage: Y = V.span([[1,1,1]], ZZ) 

sage: N = X + Y 

sage: M.is_submodule(X) 

False 

sage: M.is_submodule(Y) 

False 

sage: Y.is_submodule(M) 

True 

sage: N.is_submodule(M) 

False 

sage: M.is_submodule(N) 

True 

 

sage: M = FreeModule(ZZ,2) 

sage: M.is_submodule(M) 

True 

sage: N = M.scale(2) 

sage: N.is_submodule(M) 

True 

sage: M.is_submodule(N) 

False 

sage: N = M.scale(1/2) 

sage: N.is_submodule(M) 

False 

sage: M.is_submodule(N) 

True 

 

Since :meth:`basis` is not implemented in general, submodule 

testing does not work for all PID's. However, trivial cases are 

already used (and useful) for coercion, e.g.:: 

 

sage: QQ(1/2) * vector(ZZ['x']['y'],[1,2,3,4]) 

(1/2, 1, 3/2, 2) 

sage: vector(ZZ['x']['y'],[1,2,3,4]) * QQ(1/2) 

(1/2, 1, 3/2, 2) 

 

TESTS:: 

 

M = QQ^3 / [[1,2,3]] 

V = QQ^2 

V.is_submodule(M) 

False 

 

sage: M1 = QQ^3 / [[1,2,3]] 

sage: V1 = span(QQ,[(1,0,0)]) + M1.relations() 

sage: M2 = V1 / M1.relations() 

sage: M2.is_submodule(M1) # Different ambient vector spaces 

False 

""" 

if self is other: 

return True 

if not isinstance(other, FreeModule_generic): 

return False 

# Removing this try-except block changes the behavior of 

# is_submodule for (QQ^2).is_submodule(CC^2) 

try: 

if self.ambient_vector_space() != other.ambient_vector_space(): 

return False 

if other is other.ambient_vector_space(): 

return True 

except AttributeError: 

# Not all free modules have an ambient_vector_space. 

pass 

from sage.modules.quotient_module import FreeModule_ambient_field_quotient 

if isinstance(other, FreeModule_ambient_field_quotient): 

#if the relations agree we continue with the covers. 

if isinstance(self, FreeModule_ambient_field_quotient): 

if other.relations() != self.relations(): 

return False 

self = self.cover() 

else: 

if other.relations() != 0: 

return False 

other = other.cover() 

 

if other.rank() < self.rank(): 

return False 

if other.degree() != self.degree(): 

return False 

if self._inner_product_is_dot_product() and other._inner_product_is_dot_product(): 

pass 

else: 

if self.inner_product_matrix() != other.inner_product_matrix(): 

return False 

R = self.base_ring() 

S = other.base_ring() 

if R != S: 

try: 

if not R.is_subring(S): 

return False 

except NotImplementedError: 

if not R.fraction_field().is_subring(S): 

raise NotImplementedError("could not determine if %s is a " 

"subring of %s" % (R, S)) 

# now R is a subring of S 

if other.is_ambient() and S.is_field(): 

return True 

try: 

M = other.basis_matrix().solve_left(self.basis_matrix()) 

except ValueError: 

return False 

except TypeError: 

# only if solve_left does not eat a matrix 

# else this is far to inefficient 

try: 

M = [list(other.basis_matrix().solve_left(self.basis_matrix()[i])) for i in range(self.basis_matrix().nrows())] 

except ValueError: 

return False 

from sage.misc.flatten import flatten 

return all(x in S for x in flatten(M)) 

return all(x in S for x in M.list()) 

 

def __iter__(self): 

""" 

Return iterator over the elements of this free module. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(GF(4,'a'),2) 

sage: [x for x in V] 

[(0, 0), (a, 0), (a + 1, 0), (1, 0), (0, a), (a, a), (a + 1, a), (1, a), (0, a + 1), (a, a + 1), (a + 1, a + 1), (1, a + 1), (0, 1), (a, 1), (a + 1, 1), (1, 1)] 

 

:: 

 

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

sage: [x for x in W] 

[(0, 0), (a, a), (a + 1, a + 1), (1, 1)] 

 

TESTS:: 

 

sage: V = VectorSpace(GF(2,'a'),2) 

sage: V.list() 

[(0, 0), (1, 0), (0, 1), (1, 1)] 

""" 

G = self.gens() 

if len(G) == 0: 

yield self(0) 

return 

R = self.base_ring() 

iters = [iter(R) for _ in range(len(G))] 

for x in iters: next(x) # put at 0 

zero = R(0) 

v = [zero for _ in range(len(G))] 

n = 0 

z = self(0) 

yield z 

while n < len(G): 

try: 

v[n] = next(iters[n]) 

yield self.linear_combination_of_basis(v) 

n = 0 

except StopIteration: 

iters[n] = iter(R) # reset 

next(iters[n]) # put at 0 

v[n] = zero 

n += 1 

 

def cardinality(self): 

r""" 

Return the cardinality of the free module. 

 

OUTPUT: 

 

Either an integer or ``+Infinity``. 

 

EXAMPLES:: 

 

sage: k.<a> = FiniteField(9) 

sage: V = VectorSpace(k,3) 

sage: V.cardinality() 

729 

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

sage: W.cardinality() 

81 

sage: R = IntegerModRing(12) 

sage: M = FreeModule(R,2) 

sage: M.cardinality() 

144 

sage: (QQ^3).cardinality() 

+Infinity 

 

TESTS: 

 

Check that :trac:`22987` is fixed:: 

 

sage: VectorSpace(QQ, 0).cardinality() 

1 

""" 

if not self.rank(): 

return sage.rings.integer.Integer(1) 

return self.base_ring().cardinality() ** self.rank() 

 

__len__ = cardinality # for backward compatibility 

 

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 FreeModule(self.base_ring(), self.degree()) 

 

def basis(self): 

""" 

Return the basis of this module. 

 

EXAMPLES:: 

 

sage: FreeModule(Integers(12),3).basis() 

[ 

(1, 0, 0), 

(0, 1, 0), 

(0, 0, 1) 

] 

""" 

raise NotImplementedError 

 

def gens(self): 

""" 

Return a tuple of basis elements of ``self``. 

 

EXAMPLES:: 

 

sage: FreeModule(Integers(12),3).gens() 

((1, 0, 0), (0, 1, 0), (0, 0, 1)) 

""" 

return tuple(self.basis()) 

 

def basis_matrix(self, ring=None): 

""" 

Return the matrix whose rows are the basis for this free module. 

 

INPUT: 

 

- ``ring`` -- (default: ``self.coordinate_ring()``) a ring over 

which the matrix is defined 

 

EXAMPLES:: 

 

sage: FreeModule(Integers(12),3).basis_matrix() 

[1 0 0] 

[0 1 0] 

[0 0 1] 

 

:: 

 

sage: M = FreeModule(GF(7),3).span([[2,3,4],[1,1,1]]); M 

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

Basis matrix: 

[1 0 6] 

[0 1 2] 

sage: M.basis_matrix() 

[1 0 6] 

[0 1 2] 

 

:: 

 

sage: M = FreeModule(GF(7),3).span_of_basis([[2,3,4],[1,1,1]]); 

sage: M.basis_matrix() 

[2 3 4] 

[1 1 1] 

 

:: 

 

sage: M = FreeModule(QQ,2).span_of_basis([[1,-1],[1,0]]); M 

Vector space of degree 2 and dimension 2 over Rational Field 

User basis matrix: 

[ 1 -1] 

[ 1 0] 

sage: M.basis_matrix() 

[ 1 -1] 

[ 1 0] 

 

TESTS: 

 

See :trac:`3699`:: 

 

sage: K = FreeModule(ZZ, 2000) 

sage: I = K.basis_matrix() 

 

See :trac:`17585`:: 

 

sage: ((ZZ^2)*2).basis_matrix().parent() 

Full MatrixSpace of 2 by 2 dense matrices over Integer Ring 

sage: ((ZZ^2)*2).basis_matrix(RDF).parent() 

Full MatrixSpace of 2 by 2 dense matrices over Real Double Field 

 

sage: M = (ZZ^2)*(1/2) 

sage: M.basis_matrix() 

[1/2 0] 

[ 0 1/2] 

sage: M.basis_matrix().parent() 

Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

sage: M.basis_matrix(QQ).parent() 

Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

sage: M.basis_matrix(ZZ) 

Traceback (most recent call last): 

... 

TypeError: matrix has denominators so can't change to ZZ. 

""" 

try: 

A = self.__basis_matrix 

except AttributeError: 

MAT = sage.matrix.matrix_space.MatrixSpace(self.coordinate_ring(), 

len(self.basis()), self.degree(), 

sparse = self.is_sparse()) 

if self.is_ambient(): 

A = MAT.identity_matrix() 

else: 

A = MAT(self.basis()) 

A.set_immutable() 

self.__basis_matrix = A 

if ring is None or ring is A.base_ring(): 

return A 

else: 

return A.change_ring(ring) 

 

def echelonized_basis_matrix(self): 

""" 

The echelonized basis matrix (not implemented for this module). 

 

This example works because M is an ambient module. Submodule 

creation should exist for generic modules. 

 

EXAMPLES:: 

 

sage: R = IntegerModRing(12) 

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

sage: M = FreeModule(S,3) 

sage: M.echelonized_basis_matrix() 

[1 0 0] 

[0 1 0] 

[0 0 1] 

 

TESTS:: 

 

sage: from sage.modules.free_module import FreeModule_generic 

sage: FreeModule_generic.echelonized_basis_matrix(M) 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def matrix(self): 

""" 

Return the basis matrix of this module, which is the matrix whose 

rows are a basis for this module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2) 

sage: M.matrix() 

[1 0] 

[0 1] 

sage: M.submodule([M.gen(0) + M.gen(1), M.gen(0) - 2*M.gen(1)]).matrix() 

[1 1] 

[0 3] 

""" 

return self.basis_matrix() 

 

def direct_sum(self, other): 

""" 

Return the direct sum of ``self`` and ``other`` as a free module. 

 

EXAMPLES:: 

 

sage: V = (ZZ^3).span([[1/2,3,5], [0,1,-3]]); V 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1/2 0 14] 

[ 0 1 -3] 

sage: W = (ZZ^3).span([[1/2,4,2]]); W 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1/2 4 2] 

sage: V.direct_sum(W) 

Free module of degree 6 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 0 14 0 0 0] 

[ 0 1 -3 0 0 0] 

[ 0 0 0 1/2 4 2] 

""" 

if not is_FreeModule(other): 

raise TypeError("other must be a free module") 

if other.base_ring() != self.base_ring(): 

raise TypeError("base rings of self and other must be the same") 

return self.basis_matrix().block_sum(other.basis_matrix()).row_module(self.base_ring()) 

 

def coordinates(self, v, check=True): 

""" 

Write `v` in terms of the basis for self. 

 

INPUT: 

 

- ``v`` -- vector 

 

- ``check`` -- bool (default: True); if True, also verify that 

`v` is really in self. 

 

OUTPUT: list 

 

Returns a list `c` such that if `B` is the basis 

for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2); M0,M1=M.gens() 

sage: W = M.submodule([M0 + M1, M0 - 2*M1]) 

sage: W.coordinates(2*M0-M1) 

[2, -1] 

""" 

return self.coordinate_vector(v, check=check).list() 

 

def coordinate_vector(self, v, check=True): 

""" 

Return the vector whose coefficients give `v` as a linear 

combination of the basis for self. 

 

INPUT: 

 

- ``v`` -- vector 

 

- ``check`` -- bool (default: True); if True, also verify that 

`v` is really in self. 

 

OUTPUT: list 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2); M0,M1=M.gens() 

sage: W = M.submodule([M0 + M1, M0 - 2*M1]) 

sage: W.coordinate_vector(2*M0 - M1) 

(2, -1) 

""" 

raise NotImplementedError 

 

def coordinate_module(self, V): 

r""" 

Suppose ``V`` is a submodule of ``self`` (or a module commensurable 

with ``self``), and that ``self`` is a free module over `R` of rank 

`n`. Let `\phi` be the map from ``self`` to 

`R^n` that sends the basis vectors of ``self`` in order to the 

standard basis of `R^n`. This function returns the image 

`\phi(V)`. 

 

.. WARNING:: 

 

If there is no integer `d` such that `dV` is a submodule 

of ``self``, then this function will give total nonsense. 

 

EXAMPLES: 

 

We illustrate this function with some 

`\ZZ`-submodules of `\QQ^3`:: 

 

sage: V = (ZZ^3).span([[1/2,3,5], [0,1,-3]]) 

sage: W = (ZZ^3).span([[1/2,4,2]]) 

sage: V.coordinate_module(W) 

Free module of degree 2 and rank 1 over Integer Ring 

User basis matrix: 

[1 4] 

sage: V.0 + 4*V.1 

(1/2, 4, 2) 

 

In this example, the coordinate module isn't even in `\ZZ^3`:: 

 

sage: W = (ZZ^3).span([[1/4,2,1]]) 

sage: V.coordinate_module(W) 

Free module of degree 2 and rank 1 over Integer Ring 

User basis matrix: 

[1/2 2] 

 

The following more elaborate example illustrates using this 

function to write a submodule in terms of integral cuspidal modular 

symbols:: 

 

sage: M = ModularSymbols(54) 

sage: S = M.cuspidal_subspace() 

sage: K = S.integral_structure(); K 

Free module of degree 19 and rank 8 over Integer Ring 

Echelon basis matrix: 

[ 0 1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 

... 

sage: L = M[0].integral_structure(); L 

Free module of degree 19 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 0 1 1 0 -2 1 -1 1 -1 -2 2 0 0 0 0 0 0 0 0] 

[ 0 0 3 0 -3 2 -1 2 -1 -4 2 -1 -2 1 2 0 0 -1 1] 

sage: K.coordinate_module(L) 

Free module of degree 8 and rank 2 over Integer Ring 

User basis matrix: 

[ 1 1 1 -1 1 -1 0 0] 

[ 0 3 2 -1 2 -1 -1 -2] 

sage: K.coordinate_module(L).basis_matrix() * K.basis_matrix() 

[ 0 1 1 0 -2 1 -1 1 -1 -2 2 0 0 0 0 0 0 0 0] 

[ 0 0 3 0 -3 2 -1 2 -1 -4 2 -1 -2 1 2 0 0 -1 1] 

""" 

if not is_FreeModule(V): 

raise ValueError("V must be a free module") 

A = self.basis_matrix() 

A = A.matrix_from_columns(A.pivots()).transpose() 

B = V.basis_matrix() 

B = B.matrix_from_columns(self.basis_matrix().pivots()).transpose() 

S = A.solve_right(B).transpose() 

return (self.base_ring()**S.ncols()).span_of_basis(S.rows()) 

 

def degree(self): 

""" 

Return the degree of this free module. This is the dimension of the 

ambient vector space in which it is embedded. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 10) 

sage: W = M.submodule([M.gen(0), 2*M.gen(3) - M.gen(0), M.gen(0) + M.gen(3)]) 

sage: W.degree() 

10 

sage: W.rank() 

2 

""" 

return self.__degree 

 

def dimension(self): 

""" 

Return the dimension of this free module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(FiniteField(19), 100) 

sage: W = M.submodule([M.gen(50)]) 

sage: W.dimension() 

1 

""" 

return self.rank() 

 

def codimension(self): 

""" 

Return the codimension of this free module, which is the 

dimension of the ambient space minus the dimension of this 

free module. 

 

EXAMPLES:: 

 

sage: M = Matrix(3, 4, range(12)) 

sage: V = M.left_kernel(); V 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[ 1 -2 1] 

sage: V.dimension() 

1 

sage: V.codimension() 

2 

 

The codimension of an ambient space is always zero:: 

 

sage: (QQ^10).codimension() 

0 

""" 

return self.degree() - self.rank() 

 

def discriminant(self): 

""" 

Return the discriminant of this free module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: M.discriminant() 

1 

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

sage: W.discriminant() 

14 

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

sage: W2.discriminant() 

6 

""" 

return self.gram_matrix().determinant() 

 

def base_field(self): 

""" 

Return the base field, which is the fraction field of the base ring 

of this module. 

 

EXAMPLES:: 

 

sage: FreeModule(GF(3), 2).base_field() 

Finite Field of size 3 

sage: FreeModule(ZZ, 2).base_field() 

Rational Field 

sage: FreeModule(PolynomialRing(GF(7),'x'), 2).base_field() 

Fraction Field of Univariate Polynomial Ring in x over Finite Field of size 7 

""" 

return self.base_ring().fraction_field() 

 

def coordinate_ring(self): 

""" 

Return the ring over which the entries of the vectors are 

defined. 

 

This is the same as :meth:`base_ring` unless an explicit basis 

was given over the fraction field. 

 

EXAMPLES:: 

 

sage: M = ZZ^2 

sage: M.coordinate_ring() 

Integer Ring 

 

:: 

 

sage: M = (ZZ^2) * (1/2) 

sage: M.base_ring() 

Integer Ring 

sage: M.coordinate_ring() 

Rational Field 

 

:: 

 

sage: R.<x> = QQ[] 

sage: L = R^2 

sage: L.coordinate_ring() 

Univariate Polynomial Ring in x over Rational Field 

sage: L.span([(x,0), (1,x)]).coordinate_ring() 

Univariate Polynomial Ring in x over Rational Field 

sage: L.span([(x,0), (1,1/x)]).coordinate_ring() 

Fraction Field of Univariate Polynomial Ring in x over Rational Field 

sage: L.span([]).coordinate_ring() 

Univariate Polynomial Ring in x over Rational Field 

""" 

return self.__coordinate_ring 

 

def free_module(self): 

""" 

Return this free module. (This is used by the 

``FreeModule`` functor, and simply returns self.) 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: M.free_module() 

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

""" 

return self 

 

def gen(self, i=0): 

""" 

Return the `i`-th generator for ``self``. 

 

Here `i` is between 0 and rank - 1, inclusive. 

 

INPUT: 

 

- `i` -- an integer (default 0) 

 

OUTPUT: `i`-th basis vector for ``self``. 

 

EXAMPLES:: 

 

sage: n = 5 

sage: V = QQ^n 

sage: B = [V.gen(i) for i in range(n)] 

sage: B 

[(1, 0, 0, 0, 0), 

(0, 1, 0, 0, 0), 

(0, 0, 1, 0, 0), 

(0, 0, 0, 1, 0), 

(0, 0, 0, 0, 1)] 

sage: V.gens() == tuple(B) 

True 

 

TESTS:: 

 

sage: (QQ^3).gen(4/3) 

Traceback (most recent call last): 

... 

TypeError: rational is not an integer 

""" 

if i < 0 or i >= self.rank(): 

raise ValueError("Generator %s not defined." % i) 

return self.basis()[i] 

 

def gram_matrix(self): 

r""" 

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 sage.matrix.matrix_space.MatrixSpace(self.base_ring(), self.degree(), sparse=True)(1) 

else: 

if self._gram_matrix is None: 

B = self.basis_matrix() 

self._gram_matrix = B*B.transpose() 

return self._gram_matrix 

 

def has_user_basis(self): 

""" 

Return ``True`` if the basis of this free module is 

specified by the user, as opposed to being the default echelon 

form. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

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

sage: W.has_user_basis() 

False 

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

sage: W.has_user_basis() 

True 

""" 

return False 

 

def inner_product_matrix(self): 

""" 

Return the default identity 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. 

 

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] 

""" 

return sage.matrix.matrix_space.MatrixSpace(self.base_ring(), self.degree(), sparse=True)(1) 

 

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 True 

 

def is_ambient(self): 

""" 

Returns False since this is not an ambient free module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3).span([[1,2,3]]); M 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1 2 3] 

sage: M.is_ambient() 

False 

sage: M = (ZZ^2).span([[1,0], [0,1]]) 

sage: M 

Free module of degree 2 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 0] 

[0 1] 

sage: M.is_ambient() 

False 

sage: M == M.ambient_module() 

True 

""" 

return False 

 

def is_dense(self): 

""" 

Return ``True`` if the underlying representation of 

this module uses dense vectors, and False otherwise. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2).is_dense() 

True 

sage: FreeModule(ZZ, 2, sparse=True).is_dense() 

False 

""" 

return not self.is_sparse() 

 

def is_full(self): 

""" 

Return ``True`` if the rank of this module equals its 

degree. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2).is_full() 

True 

sage: M = FreeModule(ZZ, 2).span([[1,2]]) 

sage: M.is_full() 

False 

""" 

return self.rank() == self.degree() 

 

def is_finite(self): 

""" 

Returns True if the underlying set of this free module is finite. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2).is_finite() 

False 

sage: FreeModule(Integers(8), 2).is_finite() 

True 

sage: FreeModule(ZZ, 0).is_finite() 

True 

""" 

return self.base_ring().is_finite() or self.rank() == 0 

 

def is_sparse(self): 

""" 

Return ``True`` if the underlying representation of 

this module uses sparse vectors, and False otherwise. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2).is_sparse() 

False 

sage: FreeModule(ZZ, 2, sparse=True).is_sparse() 

True 

""" 

return self.__is_sparse 

 

def ngens(self): 

""" 

Returns the number of basis elements of this free module. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2).ngens() 

2 

sage: FreeModule(ZZ, 0).ngens() 

0 

sage: FreeModule(ZZ, 2).span([[1,1]]).ngens() 

1 

""" 

try: 

return self.__ngens 

except AttributeError: 

self.__ngens = self.rank() 

return self.__ngens 

 

def nonembedded_free_module(self): 

""" 

Returns an ambient free module that is isomorphic to this free 

module. 

 

Thus if this free module is of rank `n` over a ring 

`R`, then this function returns `R^n`, as an 

ambient free module. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2).span([[1,1]]).nonembedded_free_module() 

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

""" 

return FreeModule(self.base_ring(), self.rank()) 

 

def random_element(self, prob=1.0, *args, **kwds): 

""" 

Returns a random element of self. 

 

INPUT: 

 

-- ``prob`` - float. Each coefficient will be set to zero with 

probability `1-prob`. Otherwise coefficients will be chosen 

randomly from base ring (and may be zero). 

 

-- ``*args, **kwds`` - passed on to ``random_element()`` function 

of base ring. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2).span([[1,1]]) 

sage: M.random_element() 

(-1, -1) 

sage: M.random_element() 

(2, 2) 

sage: M.random_element() 

(1, 1) 

 

Passes extra positional or keyword arguments through:: 

 

sage: M.random_element(5,10) 

(9, 9) 

""" 

rand = current_randstate().python_random().random 

R = self.base_ring() 

prob = float(prob) 

c = [0 if rand() > prob else R.random_element(*args, **kwds) for _ in range(self.rank())] 

return self.linear_combination_of_basis(c) 

 

def rank(self): 

""" 

Return the rank of this free module. 

 

EXAMPLES:: 

 

sage: FreeModule(Integers(6), 10000000).rank() 

10000000 

sage: FreeModule(ZZ, 2).span([[1,1], [2,2], [3,4]]).rank() 

2 

""" 

return self.__rank 

 

def __bool__(self): 

""" 

Return ``True`` if and only if the rank of this module is 

non-zero. In other words, this returns ``False`` for the zero 

module and ``True`` otherwise (apart from the exceptional case 

where the base ring is the zero ring). 

 

EXAMPLES:: 

 

sage: bool(QQ^0) 

False 

sage: bool(QQ^1) 

True 

sage: M = Matrix(2, 3, range(6)) 

sage: bool(M.right_kernel()) 

True 

sage: bool(M.left_kernel()) 

False 

 

When the base ring is the zero ring, we still look at the 

"rank" (which may not be mathematically meaningful):: 

 

sage: M = Integers(1)^4; M 

Ambient free module of rank 4 over Ring of integers modulo 1 

sage: M.rank() 

4 

sage: bool(M) 

True 

sage: M.cardinality() 

1 

""" 

return bool(self.rank()) 

 

__nonzero__ = __bool__ 

 

def uses_ambient_inner_product(self): 

r""" 

Return ``True`` if the inner product on this module is 

the one induced by the ambient inner product. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2) 

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

sage: W.uses_ambient_inner_product() 

True 

sage: W.inner_product_matrix() 

[1 0] 

[0 1] 

 

:: 

 

sage: W.gram_matrix() 

[5] 

""" 

return self.__uses_ambient_inner_product 

 

def zero_vector(self): 

""" 

Returns the zero vector in this free module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2) 

sage: M.zero_vector() 

(0, 0) 

sage: M(0) 

(0, 0) 

sage: M.span([[1,1]]).zero_vector() 

(0, 0) 

sage: M.zero_submodule().zero_vector() 

(0, 0) 

""" 

# Do *not* cache this -- it must be computed fresh each time, since 

# it is used by __call__ to make a new copy of the 0 element. 

 

return self.element_class(self, 0) 

 

@cached_method 

def zero(self): 

""" 

Returns the zero vector in this free module. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 2) 

sage: M.zero() 

(0, 0) 

sage: M.span([[1,1]]).zero() 

(0, 0) 

sage: M.zero_submodule().zero() 

(0, 0) 

sage: M.zero_submodule().zero().is_mutable() 

False 

""" 

res = self.element_class(self, 0) 

res.set_immutable() 

return res 

 

def are_linearly_dependent(self, vecs): 

""" 

Return ``True`` if the vectors ``vecs`` are linearly dependent and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: M = QQ^3 

sage: vecs = [M([1,2,3]), M([4,5,6])] 

sage: M.are_linearly_dependent(vecs) 

False 

sage: vecs.append(M([3,3,3])) 

sage: M.are_linearly_dependent(vecs) 

True 

 

sage: R.<x> = QQ[] 

sage: M = FreeModule(R, 2) 

sage: vecs = [M([x^2+1, x+1]), M([x+2, 2*x+1])] 

sage: M.are_linearly_dependent(vecs) 

False 

sage: vecs.append(M([-2*x+1, -2*x^2+1])) 

sage: M.are_linearly_dependent(vecs) 

True 

""" 

from sage.matrix.constructor import matrix 

A = matrix(vecs) 

A.echelonize() 

return any(row.is_zero() for row in A.rows()) 

 

def _magma_init_(self, magma): 

""" 

EXAMPLES:: 

 

sage: magma(QQ^9) # optional - magma 

Full Vector space of degree 9 over Rational Field 

sage: (QQ^9)._magma_init_(magma) # optional - magma 

'RSpace(_sage_[...],9)' 

 

:: 

 

sage: magma(Integers(8)^2) # optional - magma 

Full RSpace of degree 2 over IntegerRing(8) 

sage: magma(FreeModule(QQ['x'], 2)) # optional - magma 

Full RSpace of degree 2 over Univariate Polynomial Ring in x over Rational Field 

 

:: 

 

sage: A = matrix([[1,0],[0,-1]]) 

sage: M = FreeModule(ZZ,2,inner_product_matrix=A); M 

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

Inner product matrix: 

[ 1 0] 

[ 0 -1] 

sage: M._magma_init_(magma) # optional - magma 

'RSpace(_sage_[...],2,_sage_ref...)' 

sage: m = magma(M); m # optional - magma 

Full RSpace of degree 2 over Integer Ring 

Inner Product Matrix: 

[ 1 0] 

[ 0 -1] 

sage: m.Type() # optional - magma 

ModTupRng 

sage: m.sage() # optional - magma 

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

Inner product matrix: 

[ 1 0] 

[ 0 -1] 

sage: m.sage() is M # optional - magma 

True 

 

Now over a field:: 

 

sage: N = FreeModule(QQ,2,inner_product_matrix=A); N 

Ambient quadratic space of dimension 2 over Rational Field 

Inner product matrix: 

[ 1 0] 

[ 0 -1] 

sage: n = magma(N); n # optional - magma 

Full Vector space of degree 2 over Rational Field 

Inner Product Matrix: 

[ 1 0] 

[ 0 -1] 

sage: n.Type() # optional - magma 

ModTupFld 

sage: n.sage() # optional - magma 

Ambient quadratic space of dimension 2 over Rational Field 

Inner product matrix: 

[ 1 0] 

[ 0 -1] 

sage: n.sage() is N # optional - magma 

True 

 

How about some inexact fields:: 

 

sage: v = vector(RR, [1, pi, 5/6]) 

sage: F = v.parent() 

sage: M = magma(F); M # optional - magma 

Full Vector space of degree 3 over Real field of precision 15 

sage: M.Type() # optional - magma 

ModTupFld 

sage: m = M.sage(); m # optional - magma 

Vector space of dimension 3 over Real Field with 53 bits of precision 

sage: m is F # optional - magma 

True 

 

For interval fields, we can convert to Magma but there is no 

interval field in Magma so we cannot convert back:: 

 

sage: v = vector(RealIntervalField(100), [1, pi, 0.125]) 

sage: F = v.parent() 

sage: M = magma(v.parent()); M # optional - magma 

Full Vector space of degree 3 over Real field of precision 30 

sage: M.Type() # optional - magma 

ModTupFld 

sage: m = M.sage(); m # optional - magma 

Vector space of dimension 3 over Real Field with 100 bits of precision 

sage: m is F # optional - magma 

False 

""" 

K = magma(self.base_ring()) 

if not self._inner_product_is_dot_product(): 

M = magma(self.inner_product_matrix()) 

return "RSpace(%s,%s,%s)"%(K.name(), self.rank(), M._ref()) 

else: 

return "RSpace(%s,%s)"%(K.name(), self.rank()) 

 

def _macaulay2_(self, macaulay2=None): 

r""" 

EXAMPLES:: 

 

sage: R = QQ^2 

sage: macaulay2(R) # optional - macaulay2 

2 

QQ 

""" 

if macaulay2 is None: 

from sage.interfaces.macaulay2 import macaulay2 

if self._inner_product_matrix: 

raise NotImplementedError 

else: 

return macaulay2(self.base_ring())**self.rank() 

 

class FreeModule_generic_pid(FreeModule_generic): 

""" 

Base class for all free modules over a PID. 

""" 

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

""" 

Create a free module over a PID. 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 2) 

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

sage: FreeModule(PolynomialRing(GF(7),'x'), 2) 

Ambient free module of rank 2 over the principal ideal domain Univariate Polynomial Ring in x over Finite Field of size 7 

""" 

# The first check should go away once everything is categorized... 

if base_ring not in PrincipalIdealDomains(): 

raise TypeError("The base_ring must be a principal ideal domain.") 

super(FreeModule_generic_pid, self).__init__(base_ring, rank, degree, 

sparse, coordinate_ring) 

 

def scale(self, other): 

""" 

Return the product of this module by the number other, which is the 

module spanned by other times each basis vector. 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: M.scale(2) 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[2 0 0] 

[0 2 0] 

[0 0 2] 

 

:: 

 

sage: a = QQ('1/3') 

sage: M.scale(a) 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/3 0 0] 

[ 0 1/3 0] 

[ 0 0 1/3] 

""" 

if other == 0: 

return self.zero_submodule() 

if other == 1 or other == -1: 

return self 

return self.span([v*other for v in self.basis()]) 

 

def __radd__(self, other): 

""" 

EXAMPLES:: 

 

sage: int(0) + QQ^3 

Vector space of dimension 3 over Rational Field 

sage: sum([QQ^3, QQ^3]) 

Vector space of degree 3 and dimension 3 over Rational Field 

Basis matrix: 

[1 0 0] 

[0 1 0] 

[0 0 1] 

""" 

if other == 0: 

return self 

else: 

raise TypeError 

 

def __add__(self, other): 

r""" 

Return the sum of ``self`` and other, where both ``self`` and ``other`` must be 

submodules of the ambient vector space. 

 

EXAMPLES: 

 

We add two vector spaces:: 

 

sage: V = VectorSpace(QQ, 3) 

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

sage: W2 = V.subspace([V([1,-1,0])]) 

sage: W + W2 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[1 0 0] 

[0 1 0] 

 

We add two free `\ZZ`-modules. 

 

:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: W = M.submodule([M([1,0,2])]) 

sage: W2 = M.submodule([M([2,0,-4])]) 

sage: W + W2 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 0 2] 

[0 0 8] 

 

We can also add free `\ZZ`-modules embedded 

non-integrally into an ambient space. 

 

:: 

 

sage: V = VectorSpace(QQ, 3) 

sage: W = M.span([1/2*V.0 - 1/3*V.1]) 

 

Here the command ``M.span(...)`` creates the span of 

the indicated vectors over the base ring of `M`. 

 

:: 

 

sage: W2 = M.span([1/3*V.0 + V.1]) 

sage: W + W2 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1/6 7/3 0] 

[ 0 11/3 0] 

 

We add two modules over `\ZZ`:: 

 

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

sage: V = (A+2).kernel() 

sage: W = (A-3).kernel() 

sage: V+W 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[5 0 0] 

[0 1 0] 

[0 0 1] 

 

We add a module to 0:: 

 

sage: ZZ^3 + 0 

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

""" 

if not isinstance(other, FreeModule_generic): 

if other == 0: 

return self 

raise TypeError("other (=%s) must be a free module"%other) 

if not (self.ambient_vector_space() == other.ambient_vector_space()): 

raise TypeError("ambient vector spaces must be equal") 

return self.span(self.basis() + other.basis()) 

 

def _mul_(self, other, switch_sides=False): 

r""" 

Multiplication of the basis by ``other``. 

 

EXAMPLES:: 

 

sage: A = ZZ^3 

sage: A * 3 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[3 0 0] 

[0 3 0] 

[0 0 3] 

 

sage: V = A.span([A([1,2,2]), A([-1,0,2])]) 

sage: 2 * V 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 2 0 -4] 

[ 0 4 8] 

 

sage: m = matrix(3, range(9)) 

sage: A * m 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 3 0 -3] 

[ 0 1 2] 

sage: m * A 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 3 0 -3] 

[ 0 1 2] 

 

TESTS: 

 

Check that :trac:`17705` is fixed:: 

 

sage: V = GF(2)^2 

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

sage: x = matrix(GF(2), [[1, 1], [0, 1]]) 

sage: W*x 

Vector space of degree 2 and dimension 1 over Finite Field of size 2 

Basis matrix: 

[1 1] 

 

""" 

B = self.basis_matrix() 

B = other * B if switch_sides else B * other 

return self.span(B.rows()) 

 

def index_in(self, other): 

""" 

Return the lattice index [other:self] of ``self`` in other, as an 

element of the base field. When ``self`` is contained in other, the 

lattice index is the usual index. If the index is infinite, then 

this function returns infinity. 

 

EXAMPLES:: 

 

sage: L1 = span([[1,2]], ZZ) 

sage: L2 = span([[3,6]], ZZ) 

sage: L2.index_in(L1) 

3 

 

Note that the free modules being compared need not be integral. 

 

:: 

 

sage: L1 = span([['1/2','1/3'], [4,5]], ZZ) 

sage: L2 = span([[1,2], [3,4]], ZZ) 

sage: L2.index_in(L1) 

12/7 

sage: L1.index_in(L2) 

7/12 

sage: L1.discriminant() / L2.discriminant() 

49/144 

 

The index of a lattice of infinite index is infinite. 

 

:: 

 

sage: L1 = FreeModule(ZZ, 2) 

sage: L2 = span([[1,2]], ZZ) 

sage: L2.index_in(L1) 

+Infinity 

""" 

if not isinstance(other, FreeModule_generic): 

raise TypeError("other must be a free module") 

 

if self.ambient_vector_space() != other.ambient_vector_space(): 

raise ArithmeticError("self and other must be embedded in the same ambient space.") 

 

if self.base_ring() != other.base_ring(): 

raise NotImplementedError("lattice index only defined for modules over the same base ring.") 

 

if other.base_ring().is_field(): 

if self == other: 

return sage.rings.integer.Integer(1) 

else: 

if self.is_subspace(other): 

return sage.rings.infinity.infinity 

raise ArithmeticError("self must be contained in the vector space spanned by other.") 

 

C = [other.coordinates(b) for b in self.basis()] 

 

if self.rank() < other.rank(): 

return sage.rings.infinity.infinity 

 

a = sage.matrix.matrix_space.MatrixSpace(self.base_field(), self.rank())(C).determinant() 

if sage.rings.integer_ring.is_IntegerRing(self.base_ring()): 

return a.abs() 

elif isinstance(self.base_ring, sage.rings.number_field.order.Order): 

return self.base_ring().ideal(a).norm() 

else: 

raise NotImplementedError 

 

def intersection(self, other): 

r""" 

Return the intersection of ``self`` and ``other``. 

 

EXAMPLES: 

 

We intersect two submodules one of which is clearly 

contained in the other:: 

 

sage: A = ZZ^2 

sage: M1 = A.span([[1,1]]) 

sage: M2 = A.span([[3,3]]) 

sage: M1.intersection(M2) 

Free module of degree 2 and rank 1 over Integer Ring 

Echelon basis matrix: 

[3 3] 

sage: M1.intersection(M2) is M2 

True 

 

We intersection two submodules of `\ZZ^3` of rank 

`2`, whose intersection has rank `1`:: 

 

sage: A = ZZ^3 

sage: M1 = A.span([[1,1,1], [1,2,3]]) 

sage: M2 = A.span([[2,2,2], [1,0,0]]) 

sage: M1.intersection(M2) 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[2 2 2] 

 

We compute an intersection of two `\ZZ`-modules that 

are not submodules of `\ZZ^2`:: 

 

sage: A = ZZ^2 

sage: M1 = A.span([[1,2]]).scale(1/6) 

sage: M2 = A.span([[1,2]]).scale(1/15) 

sage: M1.intersection(M2) 

Free module of degree 2 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1/3 2/3] 

 

We intersect a `\ZZ`-module with a `\QQ`-vector space:: 

 

sage: A = ZZ^3 

sage: L = ZZ^3 

sage: V = QQ^3 

sage: W = L.span([[1/2,0,1/2]]) 

sage: K = V.span([[1,0,1], [0,0,1]]) 

sage: W.intersection(K) 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1/2 0 1/2] 

sage: K.intersection(W) 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1/2 0 1/2] 

 

We intersect two modules over the ring of integers of a number field:: 

 

sage: L.<w> = NumberField(x^2 - x + 2) 

sage: OL = L.ring_of_integers() 

sage: V = L**3 

sage: W1 = V.span([[0,w/5,0], [1,0,-1/17]], OL) 

sage: W2 = V.span([[0,(1-w)/5,0]], OL) 

sage: W1.intersection(W2) 

Free module of degree 3 and rank 1 over Maximal Order in 

Number Field in w with defining polynomial x^2 - x + 2 

Echelon basis matrix: 

[ 0 2/5 0] 

 

TESTS: 

 

Check that :trac:`24702` is fixed:: 

 

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

sage: S1 = L.submodule([(1,0)]) 

sage: S2 = L.submodule([(0,1)]) 

sage: S1.intersection(S2).ambient_module() == S1.ambient_module() 

True 

""" 

if not isinstance(other, FreeModule_generic): 

raise TypeError("other must be a free module") 

 

if self.ambient_vector_space() != other.ambient_vector_space(): 

raise ArithmeticError("self and other must be embedded in the same ambient space.") 

 

if self.base_ring() != other.base_ring(): 

if other.base_ring().is_field(): 

return other.intersection(self) 

raise NotImplementedError("intersection of modules over different base rings (neither a field) is not implemented.") 

 

# dispense with the three easy cases 

if self == self.ambient_vector_space() or other.is_submodule(self): 

return other 

elif other == other.ambient_vector_space() or self.is_submodule(other): 

return self 

elif self.rank() == 0 or other.rank() == 0: 

if self.base_ring().is_field(): 

return other.zero_submodule() 

else: 

return self.zero_submodule() 

 

# standard algorithm for computing intersection of general submodule 

if self.dimension() <= other.dimension(): 

V1 = self; V2 = other 

else: 

V1 = other; V2 = self 

A1 = V1.basis_matrix() 

A2 = V2.basis_matrix() 

S = A1.stack(A2) 

K = S.integer_kernel(self.base_ring()).basis_matrix() 

n = int(V1.dimension()) 

K = K.matrix_from_columns(range(n)) 

B = K*A1 

return self.span(B) 

 

def __and__(self, other): 

r""" 

Return the intersection of ``self`` and ``other``. 

 

See :meth:`intersection`. 

 

EXAMPLES: 

 

We intersect two submodules one of which is clearly 

contained in the other:: 

 

sage: A = ZZ^2 

sage: M1 = A.span([[1,1]]) 

sage: M2 = A.span([[3,3]]) 

sage: M1 & M2 

Free module of degree 2 and rank 1 over Integer Ring 

Echelon basis matrix: 

[3 3] 

sage: M1 & M2 is M2 

True 

 

We intersection two submodules of `\ZZ^3` of rank 

`2`, whose intersection has rank `1`:: 

 

sage: A = ZZ^3 

sage: M1 = A.span([[1,1,1], [1,2,3]]) 

sage: M2 = A.span([[2,2,2], [1,0,0]]) 

sage: M1 & M2 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[2 2 2] 

""" 

return self.intersection(other) 

 

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 self.submodule([], check=False, already_echelonized=True) 

 

def denominator(self): 

""" 

The denominator of the basis matrix of ``self`` (i.e. the LCM of the 

coordinate entries with respect to the basis of the ambient 

space). 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: L = V.span([[1,1/2,1/3], [-1/5,2/3,3]],ZZ) 

sage: L 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1/5 19/6 37/3] 

[ 0 23/6 46/3] 

sage: L.denominator() 

30 

""" 

return self.basis_matrix().denominator() 

 

def index_in_saturation(self): 

r""" 

Return the index of this module in its saturation, i.e., its 

intersection with `R^n`. 

 

EXAMPLES:: 

 

sage: W = span([[2,4,6]], ZZ) 

sage: W.index_in_saturation() 

2 

sage: W = span([[1/2,1/3]], ZZ) 

sage: W.index_in_saturation() 

1/6 

""" 

# TODO: There is probably a much faster algorithm in this case. 

return self.index_in(self.saturation()) 

 

def saturation(self): 

r""" 

Return the saturated submodule of `R^n` that spans the same 

vector space as self. 

 

EXAMPLES: 

 

We create a 1-dimensional lattice that is obviously not 

saturated and saturate it. 

 

:: 

 

sage: L = span([[9,9,6]], ZZ); L 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[9 9 6] 

sage: L.saturation() 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[3 3 2] 

 

We create a lattice spanned by two vectors, and saturate. 

Computation of discriminants shows that the index of lattice in its 

saturation is `3`, which is a prime of congruence between 

the two generating vectors. 

 

:: 

 

sage: L = span([[1,2,3], [4,5,6]], ZZ) 

sage: L.saturation() 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: L.discriminant() 

54 

sage: L.saturation().discriminant() 

6 

 

Notice that the saturation of a non-integral lattice `L` is 

defined, but the result is integral hence does not contain 

`L`:: 

 

sage: L = span([['1/2',1,3]], ZZ) 

sage: L.saturation() 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1 2 6] 

 

TESTS: 

 

We check that :trac:`24702` is fixed:: 

 

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

sage: S = 2*L 

sage: S.saturation().ambient_module() == L 

True 

""" 

R = self.base_ring() 

if R.is_field(): 

return self 

try: 

A, _ = self.basis_matrix()._clear_denom() 

S = self.span(A.saturation()) 

except AttributeError: 

# fallback in case _clear_denom isn't written 

V = self.vector_space() 

A = self.ambient_module() 

S = V.intersection(A) 

# Return exactly self if it is already saturated. 

return self if self == S else S 

 

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

""" 

Return the R-span of the given list of gens, where R = base_ring. 

The default 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. 

sage: V.span([[1,0,0],[1/5,4,0],[6,3/4,0]]) 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1/5 0 0] 

[ 0 1/4 0] 

 

It also works with other things than integers:: 

 

sage: R.<x>=QQ[] 

sage: L=R^1 

sage: a=L.span([(1/x,)]) 

sage: a 

Free module of degree 1 and rank 1 over Univariate Polynomial Ring in x over Rational Field 

Echelon basis matrix: 

[1/x] 

sage: b=L.span([(1/x,)]) 

sage: a(b.gens()[0]) 

(1/x) 

sage: L2 = R^2 

sage: L2.span([[(x^2+x)/(x^2-3*x+2),1/5],[(x^2+2*x)/(x^2-4*x+3),x]]) 

Free module of degree 2 and rank 2 over Univariate Polynomial Ring in x over Rational Field 

Echelon basis matrix: 

[x/(x^3 - 6*x^2 + 11*x - 6) 2/15*x^2 - 17/75*x - 1/75] 

[ 0 x^3 - 11/5*x^2 - 3*x + 4/5] 

 

Note that the ``base_ring`` can make a huge difference. We 

repeat the previous example over the fraction field of R and 

get a simpler vector space. :: 

 

sage: L2.span([[(x^2+x)/(x^2-3*x+2),1/5],[(x^2+2*x)/(x^2-4*x+3),x]],base_ring=R.fraction_field()) 

Vector space of degree 2 and dimension 2 over Fraction Field of Univariate Polynomial Ring in x over Rational Field 

Basis matrix: 

[1 0] 

[0 1] 

""" 

if is_FreeModule(gens): 

gens = gens.gens() 

if base_ring is None or base_ring is self.base_ring(): 

return FreeModule_submodule_pid( 

self.ambient_module(), gens, check=check, already_echelonized=already_echelonized) 

else: 

try: 

M = self.change_ring(base_ring) 

except TypeError: 

raise ValueError("Argument base_ring (= %s) is not compatible "%base_ring + \ 

"with the base field (= %s)." % self.base_field()) 

try: 

return M.span(gens) 

except TypeError: 

raise ValueError("Argument gens (= %s) is not compatible "%gens + \ 

"with base_ring (= %s)."%base_ring) 

 

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

r""" 

Create the R-submodule of the ambient vector space with given 

generators, where R is the base ring of self. 

 

INPUT: 

 

 

- ``gens`` - a list of free module elements or a free 

module 

 

- ``check`` - (default: True) whether or not to verify 

that the gens are in self. 

 

 

OUTPUT: 

 

 

- ``FreeModule`` - the submodule spanned by the 

vectors in the list gens. The basis for the subspace is always put 

in reduced row echelon form. 

 

 

EXAMPLES: 

 

We create a submodule of `\ZZ^3`:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: B = M.basis() 

sage: W = M.submodule([B[0]+B[1], 2*B[1]-B[2]]) 

sage: W 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1 1 0] 

[ 0 2 -1] 

 

We create a submodule of a submodule. 

 

:: 

 

sage: W.submodule([3*B[0] + 3*B[1]]) 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[3 3 0] 

 

We try to create a submodule that isn't really a submodule, 

which results in an ``ArithmeticError`` exception:: 

 

sage: W.submodule([B[0] - B[1]]) 

Traceback (most recent call last): 

... 

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

 

Next we create a submodule of a free module over the principal ideal 

domain `\QQ[x]`, which uses the general Hermite normal form functionality:: 

 

sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 

sage: M = FreeModule(R, 3) 

sage: B = M.basis() 

sage: W = M.submodule([x*B[0], 2*B[1]- x*B[2]]); W 

Free module of degree 3 and rank 2 over Univariate Polynomial Ring in x over Rational Field 

Echelon basis matrix: 

[ x 0 0] 

[ 0 2 -x] 

sage: W.ambient_module() 

Ambient free module of rank 3 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field 

""" 

if is_FreeModule(gens): 

gens = gens.gens() 

V = self.span(gens, check=check, already_echelonized=already_echelonized) 

if check: 

if not V.is_submodule(self): 

raise ArithmeticError("Argument gens (= %s) does not generate a submodule of self."%gens) 

return V 

 

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

r""" 

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

ring of ``self`` or user specified base_ring. 

 

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 in the ambient module `\ZZ^3` 

but is contained in the ambient vector space `\QQ^3`:: 

 

sage: V = M.ambient_vector_space() 

sage: W.span_of_basis([ V([1/5,2/5,0]), V([1/7,1/7,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. 

""" 

if is_FreeModule(basis): 

basis = basis.gens() 

if base_ring is None or base_ring == self.base_ring(): 

try: 

if self.is_dense(): 

from .free_module_integer import FreeModule_submodule_with_basis_integer 

return FreeModule_submodule_with_basis_integer(self.ambient_module(), 

basis=basis, check=check, 

already_echelonized=already_echelonized, 

lll_reduce=False) 

except TypeError: 

pass 

 

return FreeModule_submodule_with_basis_pid( 

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

already_echelonized=already_echelonized) 

else: 

try: 

M = self.change_ring(base_ring) 

except TypeError: 

raise ValueError("Argument base_ring (= %s) is not compatible "%base_ring + \ 

"with the base ring (= %s)."%self.base_ring()) 

try: 

return M.span_of_basis(basis) 

except TypeError: 

raise ValueError("Argument gens (= %s) is not compatible "%basis + \ 

"with base_ring (= %s)."%base_ring) 

 

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

""" 

Create the R-submodule of the ambient vector space with given 

basis, where R is the base ring of self. 

 

INPUT: 

 

- ``basis`` -- a list of linearly independent vectors 

 

- ``check`` -- whether or not to verify that each gen is in 

the ambient vector space 

 

OUTPUT: 

 

- ``FreeModule`` -- the `R`-submodule with given basis 

 

EXAMPLES: 

 

First we create a submodule of `\\ZZ^3`:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: B = M.basis() 

sage: N = M.submodule_with_basis([B[0]+B[1], 2*B[1]-B[2]]) 

sage: N 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[ 1 1 0] 

[ 0 2 -1] 

 

A list of vectors in the ambient vector space may fail to generate 

a submodule. 

 

:: 

 

sage: V = M.ambient_vector_space() 

sage: X = M.submodule_with_basis([ V(B[0]+B[1])/2, V(B[1]-B[2])/2]) 

Traceback (most recent call last): 

... 

ArithmeticError: The given basis does not generate a submodule of self. 

 

However, we can still determine the R-span of vectors in the 

ambient space, or over-ride the submodule check by setting check to 

False. 

 

:: 

 

sage: X = V.span([ V(B[0]+B[1])/2, V(B[1]-B[2])/2 ], ZZ) 

sage: X 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1/2 0 1/2] 

[ 0 1/2 -1/2] 

sage: Y = M.submodule([ V(B[0]+B[1])/2, V(B[1]-B[2])/2 ], check=False) 

sage: X == Y 

True 

 

Next we try to create a submodule of a free module over the 

principal ideal domain `\QQ[x]`, using our general Hermite normal form implementation:: 

 

sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 

sage: M = FreeModule(R, 3) 

sage: B = M.basis() 

sage: W = M.submodule_with_basis([x*B[0], 2*B[0]- x*B[2]]); W 

Free module of degree 3 and rank 2 over Univariate Polynomial Ring in x over Rational Field 

User basis matrix: 

[ x 0 0] 

[ 2 0 -x] 

""" 

V = self.span_of_basis(basis=basis, check=check, already_echelonized=already_echelonized) 

if check: 

if not V.is_submodule(self): 

raise ArithmeticError("The given basis does not generate a submodule of self.") 

return V 

 

def vector_space_span(self, gens, check=True): 

r""" 

Create the vector subspace of the ambient vector space with given 

generators. 

 

INPUT: 

 

 

- ``gens`` - a list of vector in self 

 

- ``check`` - whether or not to verify that each gen 

is in the ambient vector space 

 

 

OUTPUT: a vector subspace 

 

EXAMPLES: 

 

We create a `2`-dimensional subspace of `\QQ^3`. 

 

:: 

 

sage: V = VectorSpace(QQ, 3) 

sage: B = V.basis() 

sage: W = V.vector_space_span([B[0]+B[1], 2*B[1]-B[2]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 1/2] 

[ 0 1 -1/2] 

 

We create a subspace of a vector space over 

`\QQ(i)`. 

 

:: 

 

sage: R.<x> = QQ[] 

sage: K = NumberField(x^2 + 1, 'a'); a = K.gen() 

sage: V = VectorSpace(K, 3) 

sage: V.vector_space_span([2*V.gen(0) + 3*V.gen(2)]) 

Vector space of degree 3 and dimension 1 over Number Field in a with defining polynomial x^2 + 1 

Basis matrix: 

[ 1 0 3/2] 

 

We use the ``vector_space_span`` command to create a 

vector subspace of the ambient vector space of a submodule of 

`\ZZ^3`. 

 

:: 

 

sage: M = FreeModule(ZZ,3) 

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

sage: W.vector_space_span([M([2,3,4])]) 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[ 1 3/2 2] 

""" 

if is_FreeModule(gens): 

gens = gens.gens() 

return FreeModule_submodule_field(self.ambient_vector_space(), gens, check=check) 

 

def vector_space_span_of_basis(self, basis, check=True): 

""" 

Create the vector subspace of the ambient vector space with given 

basis. 

 

INPUT: 

 

- ``basis`` -- a list of linearly independent vectors 

 

- ``check`` -- whether or not to verify that each gen is in 

the ambient vector space 

 

OUTPUT: a vector subspace with user-specified basis 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ, 3) 

sage: B = V.basis() 

sage: W = V.vector_space_span_of_basis([B[0]+B[1], 2*B[1]-B[2]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[ 1 1 0] 

[ 0 2 -1] 

""" 

return FreeModule_submodule_with_basis_field(self.ambient_vector_space(), basis, check=check) 

 

def quotient(self, sub, check=True): 

""" 

Return the quotient of ``self`` by the given submodule sub. 

 

INPUT: 

 

- ``sub`` - a submodule of self, or something that can 

be turned into one via self.submodule(sub). 

 

- ``check`` - (default: True) whether or not to check 

that sub is a submodule. 

 

 

EXAMPLES:: 

 

sage: A = ZZ^3; V = A.span([[1,2,3], [4,5,6]]) 

sage: Q = V.quotient( [V.0 + V.1] ); Q 

Finitely generated module V/W over Integer Ring with invariants (0) 

""" 

# Calling is_subspace may be way too slow and repeat work done below. 

# It will be very desirable to somehow do this step better. 

if check and (not is_FreeModule(sub) or not sub.is_submodule(self)): 

try: 

sub = self.submodule(sub) 

except (TypeError, ArithmeticError): 

raise ArithmeticError("sub must be a subspace of self") 

if self.base_ring() == sage.rings.integer_ring.ZZ: 

from .fg_pid.fgp_module import FGP_Module 

return FGP_Module(self, sub, check=False) 

else: 

raise NotImplementedError("quotients of modules over rings other than fields or ZZ is not fully implemented") 

 

def __truediv__(self, sub): 

""" 

Return the quotient of ``self`` by the given submodule sub. 

 

EXAMPLES:: 

 

sage: V1 = ZZ^2; W1 = V1.span([[1,2],[3,4]]) 

sage: V1/W1 

Finitely generated module V/W over Integer Ring with invariants (2) 

sage: V2 = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W2 = V2.span([2*V2.0+4*V2.1, 9*V2.0+12*V2.1, 4*V2.2]) 

sage: V2/W2 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

""" 

return self.quotient(sub, check=True) 

 

class FreeModule_generic_field(FreeModule_generic_pid): 

""" 

Base class for all free modules over fields. 

""" 

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

""" 

Creates a vector space over a field. 

 

EXAMPLES:: 

 

sage: FreeModule(QQ, 2) 

Vector space of dimension 2 over Rational Field 

sage: FreeModule(FiniteField(2), 7) 

Vector space of dimension 7 over Finite Field of size 2 

 

We test that objects of this type are initialised correctly; 

see :trac:`11166` (the failing ``repr`` is fine because this 

is an abstract base class):: 

 

sage: from sage.modules.free_module import FreeModule_generic_field 

sage: FreeModule_generic_field(QQ, 5, 5) 

<repr(<sage.modules.free_module.FreeModule_generic_field_with_category at 0x...>) failed: NotImplementedError> 

""" 

if not isinstance(base_field, ring.Field): 

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

FreeModule_generic_pid.__init__(self, base_field, dimension, degree, sparse=sparse) 

 

def _Hom_(self, Y, category): 

r""" 

Returns a homspace whose morphisms have this vector space as domain. 

 

This is called by the general methods such as 

:meth:`sage.structure.parent.Parent.Hom`. 

 

INPUT: 

 

- ``Y`` - a free module (or vector space) that will 

be the codomain of the morphisms in returned homspace 

- ``category`` - the category for the homspace 

 

OUTPUT: 

 

If ``Y`` is a free module over a field, in other words, a vector space, 

then this returns a space of homomorphisms between vector spaces, 

in other words a space of linear transformations. 

 

If ``Y`` is a free module that is not a vector space, then 

the returned space contains homomorphisms between free modules. 

 

EXAMPLES:: 

 

sage: V = QQ^2 

sage: W = QQ^3 

sage: H = V._Hom_(W, category=None) 

sage: type(H) 

<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'> 

sage: H 

Set of Morphisms (Linear Transformations) from Vector space of dimension 2 over Rational Field to Vector space of dimension 3 over Rational Field 

 

sage: V = QQ^2 

sage: W = ZZ^3 

sage: H = V._Hom_(W, category=None) 

sage: type(H) 

<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'> 

sage: H 

Set of Morphisms from Vector space of dimension 2 over Rational Field 

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

in Category of finite dimensional vector spaces with basis over 

(number fields and quotient fields and metric spaces) 

""" 

if Y.base_ring().is_field(): 

from . import vector_space_homspace 

return vector_space_homspace.VectorSpaceHomspace(self, Y, category) 

from . import free_module_homspace 

return free_module_homspace.FreeModuleHomspace(self, Y, category) 

 

def scale(self, other): 

""" 

Return the product of ``self`` by the number other, which is the module 

spanned by ``other`` times each basis vector. Since ``self`` is a vector 

space this product equals ``self`` if ``other`` is nonzero, and is the zero 

vector space if ``other`` is 0. 

 

EXAMPLES:: 

 

sage: V = QQ^4 

sage: V.scale(5) 

Vector space of dimension 4 over Rational Field 

sage: V.scale(0) 

Vector space of degree 4 and dimension 0 over Rational Field 

Basis matrix: 

[] 

 

:: 

 

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

sage: W.scale(2) 

Vector space of degree 4 and dimension 1 over Rational Field 

Basis matrix: 

[1 1 1 1] 

sage: W.scale(0) 

Vector space of degree 4 and dimension 0 over Rational Field 

Basis matrix: 

[] 

 

:: 

 

sage: V = QQ^4; V 

Vector space of dimension 4 over Rational Field 

sage: V.scale(3) 

Vector space of dimension 4 over Rational Field 

sage: V.scale(0) 

Vector space of degree 4 and dimension 0 over Rational Field 

Basis matrix: 

[] 

""" 

if other == 0: 

return self.zero_submodule() 

return self 

 

def __add__(self, other): 

""" 

Return the sum of ``self`` and other. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,3) 

sage: V0 = V.span([V.gen(0)]) 

sage: V2 = V.span([V.gen(2)]) 

sage: V0 + V2 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[1 0 0] 

[0 0 1] 

sage: QQ^3 + 0 

Vector space of dimension 3 over Rational Field 

""" 

if not isinstance(other, FreeModule_generic_field): 

if other == 0: 

return self 

raise TypeError("other must be a Vector Space") 

V = self.ambient_vector_space() 

if V != other.ambient_vector_space(): 

raise ArithmeticError("self and other must have the same ambient space") 

return V.span(self.basis() + other.basis()) 

 

def echelonized_basis_matrix(self): 

""" 

Return basis matrix for ``self`` in row echelon form. 

 

EXAMPLES:: 

 

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

sage: V.basis_matrix() 

[1 2 3] 

[4 5 6] 

sage: V.echelonized_basis_matrix() 

[ 1 0 -1] 

[ 0 1 2] 

""" 

return self.basis_matrix().echelon_form() 

 

def intersection(self, other): 

""" 

Return the intersection of ``self`` and other, which must be 

R-submodules of a common ambient vector space. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,3) 

sage: W1 = V.submodule([V.gen(0), V.gen(0) + V.gen(1)]) 

sage: W2 = V.submodule([V.gen(1), V.gen(2)]) 

sage: W1.intersection(W2) 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[0 1 0] 

sage: W2.intersection(W1) 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[0 1 0] 

sage: V.intersection(W1) 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[1 0 0] 

[0 1 0] 

sage: W1.intersection(V) 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[1 0 0] 

[0 1 0] 

sage: Z = V.submodule([]) 

sage: W1.intersection(Z) 

Vector space of degree 3 and dimension 0 over Rational Field 

Basis matrix: 

[] 

""" 

if not isinstance(other, FreeModule_generic): 

raise TypeError("other must be a free module") 

 

if self.ambient_vector_space() != other.ambient_vector_space(): 

raise ArithmeticError("self and other must have the same ambient space.") 

 

if self.rank() == 0 or other.rank() == 0: 

if self.base_ring().is_field(): 

return other.zero_submodule() 

else: 

return self.zero_submodule() 

 

if self.base_ring() != other.base_ring(): 

# Now other is over a ring R whose fraction field K is the base field of V = self. 

# We compute the intersection using the following algorithm: 

# 1. By explicitly computing the nullspace of the matrix whose rows 

# are a basis for self, we obtain the matrix over a linear map 

# phi: K^n ----> W 

# with kernel equal to V = self. 

# 2. Compute the kernel over R of Phi restricted to other. Do this 

# by clearing denominators, computing the kernel of a matrix with 

# entries in R, then restoring denominators to the answer. 

K = self.base_ring() 

R = other.base_ring() 

B = self.basis_matrix().transpose() 

W = B.kernel() 

phi = W.basis_matrix().transpose() 

 

# To restrict phi to other, we multiply the basis matrix for other 

# by phi, thus computing the image of each basis vector. 

X = other.basis_matrix() 

psi = X * phi 

 

# Now psi is a matrix that defines an R-module morphism from other to some 

# R-module, whose kernel defines the long sought for intersection of self and other. 

L = psi.integer_kernel() 

 

# Finally the kernel of the intersection has basis the linear combinations of 

# the basis of other given by a basis for L. 

G = L.basis_matrix() * other.basis_matrix() 

return other.span(G.rows()) 

 

# dispense with the three easy cases 

if self == self.ambient_vector_space(): 

return other 

elif other == other.ambient_vector_space(): 

return self 

elif self.dimension() == 0 or other.dimension() == 0: 

return self.zero_submodule() 

 

# standard algorithm for computing intersection of general subspaces 

if self.dimension() <= other.dimension(): 

V1 = self; V2 = other 

else: 

V1 = other; V2 = self 

A1 = V1.basis_matrix() 

A2 = V2.basis_matrix() 

S = A1.stack(A2) 

K = S.kernel() 

n = int(V1.dimension()) 

B = [A1.linear_combination_of_rows(v.list()[:n]) for v in K.basis()] 

return self.ambient_vector_space().submodule(B, check=False) 

 

def is_subspace(self, other): 

""" 

True if this vector space is a subspace of other. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,3) 

sage: W = V.subspace([V.gen(0), V.gen(0) + V.gen(1)]) 

sage: W2 = V.subspace([V.gen(1)]) 

sage: W.is_subspace(V) 

True 

sage: W2.is_subspace(V) 

True 

sage: W.is_subspace(W2) 

False 

sage: W2.is_subspace(W) 

True 

""" 

return self.is_submodule(other) 

 

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

""" 

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

base field of ``self`` or the user-specified base_ring. 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] 

 

TESTS:: 

 

sage: V = FreeModule(RDF,3) 

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

sage: W.span([V.gen(1)], base_ring=GF(7)) 

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

Basis matrix: 

[0 1 0] 

sage: v = V((1, pi, log(2))); v 

(1.0, 3.141592653589793, 0.6931471805599453) 

sage: W.span([v], base_ring=GF(7)) 

Traceback (most recent call last): 

... 

ValueError: Argument gens (= [(1.0, 3.141592653589793, 0.6931471805599453)]) is not compatible with base_ring (= Finite Field of size 7). 

sage: W = V.submodule([v]) 

sage: W.span([V.gen(2)], base_ring=GF(7)) 

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

Basis matrix: 

[0 0 1] 

""" 

if is_FreeModule(gens): 

gens = gens.gens() 

if base_ring is None or base_ring is self.base_ring(): 

return FreeModule_submodule_field( 

self.ambient_module(), gens=gens, check=check, already_echelonized=already_echelonized) 

else: 

try: 

M = self.ambient_module().change_ring(base_ring) 

except TypeError: 

raise ValueError("Argument base_ring (= %s) is not compatible with the base field (= %s)." % (base_ring, self.base_field() )) 

try: 

return M.span(gens) 

except TypeError: 

raise ValueError("Argument gens (= %s) is not compatible with base_ring (= %s)." % (gens, base_ring)) 

 

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

r""" 

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

field of ``self`` or user specified base_ring. 

 

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. 

""" 

if is_FreeModule(basis): 

basis = basis.gens() 

if base_ring is None: 

return FreeModule_submodule_with_basis_field( 

self.ambient_module(), basis=basis, check=check, already_echelonized=already_echelonized) 

else: 

try: 

M = self.change_ring(base_ring) 

except TypeError: 

raise ValueError("Argument base_ring (= %s) is not compatible with the base field (= %s)." % ( 

base_ring, self.base_field() )) 

try: 

return M.span_of_basis(basis) 

except TypeError: 

raise ValueError("Argument basis (= %s) is not compatible with base_ring (= %s)." % (basis, base_ring)) 

 

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

""" 

Return the subspace of ``self`` spanned by the elements of gens. 

 

INPUT: 

 

- ``gens`` - list of vectors 

 

- ``check`` - bool (default: True) verify that gens 

are all in self. 

 

- ``already_echelonized`` - bool (default: False) set 

to True if you know the gens are in Echelon form. 

 

EXAMPLES: 

 

First we create a 1-dimensional vector subspace of an 

ambient `3`-dimensional space over the finite field of 

order `7`:: 

 

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] 

 

Next we create an invalid subspace, but it's allowed since 

``check=False``. This is just equivalent to computing 

the span of the element:: 

 

sage: W.subspace([[1,1,0]], check=False) 

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

Basis matrix: 

[1 1 0] 

 

With ``check=True`` (the default) the mistake is correctly 

detected and reported with an ``ArithmeticError`` exception:: 

 

sage: W.subspace([[1,1,0]], check=True) 

Traceback (most recent call last): 

... 

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

""" 

return self.submodule(gens, check=check, already_echelonized=already_echelonized) 

 

def subspaces(self, dim): 

""" 

Iterate over all subspaces of dimension dim. 

 

INPUT: 

 

- ``dim`` - int, dimension of subspaces to be generated 

 

EXAMPLES:: 

 

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

sage: len(list(V.subspaces(0))) 

1 

sage: len(list(V.subspaces(1))) 

121 

sage: len(list(V.subspaces(2))) 

1210 

sage: len(list(V.subspaces(3))) 

1210 

sage: len(list(V.subspaces(4))) 

121 

sage: len(list(V.subspaces(5))) 

1 

 

:: 

 

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

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

sage: list(V.subspaces(1)) 

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

Basis matrix: 

[1 1 0 0 0], 

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

Basis matrix: 

[1 1 1 1 0], 

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

Basis matrix: 

[1 1 2 2 0], 

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

Basis matrix: 

[0 0 1 1 0]] 

""" 

if not self.base_ring().is_finite(): 

raise RuntimeError("Base ring must be finite.") 

b = self.basis_matrix() 

from sage.matrix.echelon_matrix import reduced_echelon_matrix_iterator 

for m in reduced_echelon_matrix_iterator(self.base_ring(), dim, self.dimension(), self.is_sparse(), copy=False): 

yield self.subspace((m*b).rows()) 

 

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

""" 

Same as ``self.submodule_with_basis(...)``. 

 

EXAMPLES: 

 

We create a subspace with a user-defined basis. 

 

:: 

 

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

sage: W = V.subspace_with_basis([[2,2,2], [1,2,3]]); W 

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

User basis matrix: 

[2 2 2] 

[1 2 3] 

 

We then create a subspace of the subspace with user-defined basis. 

 

:: 

 

sage: W1 = W.subspace_with_basis([[3,4,5]]); W1 

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

User basis matrix: 

[3 4 5] 

 

Notice how the basis for the same subspace is different if we 

merely use the ``subspace`` command. 

 

:: 

 

sage: W2 = W.subspace([[3,4,5]]); W2 

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

Basis matrix: 

[1 6 4] 

 

Nonetheless the two subspaces are equal (as mathematical objects):: 

 

sage: W1 == W2 

True 

""" 

return self.submodule_with_basis(gens, check=check, already_echelonized=already_echelonized) 

 

def complement(self): 

""" 

Return the complement of ``self`` in the 

:meth:`~sage.modules.free_module.FreeModule_ambient_field.ambient_vector_space`. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: V.complement() 

Vector space of degree 3 and dimension 0 over Rational Field 

Basis matrix: 

[] 

sage: V == V.complement().complement() 

True 

sage: W = V.span([[1, 0, 1]]) 

sage: X = W.complement(); X 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 0] 

sage: X.complement() == W 

True 

sage: X + W == V 

True 

 

Even though we construct a subspace of a subspace, the 

orthogonal complement is still done in the ambient vector 

space `\QQ^3`:: 

 

sage: V = QQ^3 

sage: W = V.subspace_with_basis([[1,0,1],[-1,1,0]]) 

sage: X = W.subspace_with_basis([[1,0,1]]) 

sage: X.complement() 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 0] 

 

All these complements are only done with respect to the inner 

product in the usual basis. Over finite fields, this means 

we can get complements which are only isomorphic to a vector 

space decomposition complement. :: 

 

sage: F2 = GF(2,x) 

sage: V = F2^6 

sage: W = V.span([[1,1,0,0,0,0]]) 

sage: W 

Vector space of degree 6 and dimension 1 over Finite Field of size 2 

Basis matrix: 

[1 1 0 0 0 0] 

sage: W.complement() 

Vector space of degree 6 and dimension 5 over Finite Field of size 2 

Basis matrix: 

[1 1 0 0 0 0] 

[0 0 1 0 0 0] 

[0 0 0 1 0 0] 

[0 0 0 0 1 0] 

[0 0 0 0 0 1] 

sage: W.intersection(W.complement()) 

Vector space of degree 6 and dimension 1 over Finite Field of size 2 

Basis matrix: 

[1 1 0 0 0 0] 

""" 

# Check simple cases 

if self.dimension() == 0: 

return self.ambient_vector_space() 

if self.dimension() == self.ambient_vector_space().dimension(): 

return self.submodule([]) 

return self.basis_matrix().right_kernel() 

 

def vector_space(self, base_field=None): 

""" 

Return the vector space associated to self. Since ``self`` is a vector 

space this function simply returns self, unless the base field is 

different. 

 

EXAMPLES:: 

 

sage: V = span([[1,2,3]],QQ); V 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 2 3] 

sage: V.vector_space() 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 2 3] 

""" 

if base_field is None: 

return self 

return self.change_ring(base_field) 

 

def zero_submodule(self): 

""" 

Return the zero submodule of self. 

 

EXAMPLES:: 

 

sage: (QQ^4).zero_submodule() 

Vector space of degree 4 and dimension 0 over Rational Field 

Basis matrix: 

[] 

""" 

return self.zero_subspace() 

 

def zero_subspace(self): 

""" 

Return the zero subspace of self. 

 

EXAMPLES:: 

 

sage: (QQ^4).zero_subspace() 

Vector space of degree 4 and dimension 0 over Rational Field 

Basis matrix: 

[] 

""" 

return self.submodule([], check=False, already_echelonized=True) 

 

def linear_dependence(self, vectors, zeros='left', check=True): 

r""" 

Returns a list of vectors giving relations of linear dependence for the input list of vectors. 

Can be used to check linear independence of a set of vectors. 

 

INPUT: 

 

- ``vectors`` -- A list of vectors, all from the same vector 

space. 

 

- ``zeros`` -- default: ``'left'`` - ``'left'`` or ``'right'`` 

as a general preference for where zeros are located in the 

returned coefficients 

 

- ``check`` -- default: ``True`` - if ``True`` each item in 

the list ``vectors`` is checked for membership in ``self``. 

Set to ``False`` if you can be certain the vectors come from 

the vector space. 

 

OUTPUT: 

 

Returns a list of vectors. The scalar entries of each vector provide 

the coefficients for a linear combination of the input vectors that 

will equal the zero vector in ``self``. Furthermore, the returned list 

is linearly independent in the vector space over the same base field 

with degree equal to the length of the list ``vectors``. 

 

The linear independence of ``vectors`` is equivalent to the returned 

list being empty, so this provides a test - see the examples below. 

 

The returned vectors are always independent, and with ``zeros`` set to 

``'left'`` they have 1's in their first non-zero entries and a qualitative 

disposition to having zeros in the low-index entries. With ``zeros`` set 

to ``'right'`` the situation is reversed with a qualitative disposition 

for zeros in the high-index entries. 

 

If the vectors in ``vectors`` are made the rows of a matrix `V` and 

the returned vectors are made the rows of a matrix `R`, then the 

matrix product `RV` is a zero matrix of the proper size. And 

`R` is a matrix of full rank. This routine uses kernels of 

matrices to compute these relations of linear dependence, 

but handles all the conversions between sets of vectors 

and matrices. If speed is important, consider working with 

the appropriate matrices and kernels instead. 

 

EXAMPLES: 

 

We begin with two linearly independent vectors, and add three 

non-trivial linear combinations to the set. We illustrate 

both types of output and check a selected relation of linear 

dependence. :: 

 

sage: v1 = vector(QQ, [2, 1, -4, 3]) 

sage: v2 = vector(QQ, [1, 5, 2, -2]) 

sage: V = QQ^4 

sage: V.linear_dependence([v1,v2]) 

[ 

<BLANKLINE> 

] 

 

sage: v3 = v1 + v2 

sage: v4 = 3*v1 - 4*v2 

sage: v5 = -v1 + 2*v2 

sage: L = [v1, v2, v3, v4, v5] 

 

sage: relations = V.linear_dependence(L, zeros='left') 

sage: relations 

[ 

(1, 0, 0, -1, -2), 

(0, 1, 0, -1/2, -3/2), 

(0, 0, 1, -3/2, -7/2) 

] 

sage: v2 + (-1/2)*v4 + (-3/2)*v5 

(0, 0, 0, 0) 

 

sage: relations = V.linear_dependence(L, zeros='right') 

sage: relations 

[ 

(-1, -1, 1, 0, 0), 

(-3, 4, 0, 1, 0), 

(1, -2, 0, 0, 1) 

] 

sage: z = sum([relations[2][i]*L[i] for i in range(len(L))]) 

sage: z == zero_vector(QQ, 4) 

True 

 

A linearly independent set returns an empty list, 

a result that can be tested. :: 

 

sage: v1 = vector(QQ, [0,1,-3]) 

sage: v2 = vector(QQ, [4,1,0]) 

sage: V = QQ^3 

sage: relations = V.linear_dependence([v1, v2]); relations 

[ 

<BLANKLINE> 

] 

sage: relations == [] 

True 

 

Exact results result from exact fields. We start with three 

linearly independent vectors and add in two linear combinations 

to make a linearly dependent set of five vectors. :: 

 

sage: F = FiniteField(17) 

sage: v1 = vector(F, [1, 2, 3, 4, 5]) 

sage: v2 = vector(F, [2, 4, 8, 16, 15]) 

sage: v3 = vector(F, [1, 0, 0, 0, 1]) 

sage: (F^5).linear_dependence([v1, v2, v3]) == [] 

True 

sage: L = [v1, v2, v3, 2*v1+v2, 3*v2+6*v3] 

sage: (F^5).linear_dependence(L) 

[ 

(1, 0, 16, 8, 3), 

(0, 1, 2, 0, 11) 

] 

sage: v1 + 16*v3 + 8*(2*v1+v2) + 3*(3*v2+6*v3) 

(0, 0, 0, 0, 0) 

sage: v2 + 2*v3 + 11*(3*v2+6*v3) 

(0, 0, 0, 0, 0) 

sage: (F^5).linear_dependence(L, zeros='right') 

[ 

(15, 16, 0, 1, 0), 

(0, 14, 11, 0, 1) 

] 

 

TESTS: 

 

With ``check=True`` (the default) a mismatch between vectors 

and the vector space is caught. :: 

 

sage: v1 = vector(RR, [1,2,3]) 

sage: v2 = vector(RR, [1,2,3,4]) 

sage: (RR^3).linear_dependence([v1,v2], check=True) 

Traceback (most recent call last): 

... 

ValueError: vector (1.00000000000000, 2.00000000000000, 3.00000000000000, 4.00000000000000) is not an element of Vector space of dimension 3 over Real Field with 53 bits of precision 

 

The ``zeros`` keyword is checked. :: 

 

sage: (QQ^3).linear_dependence([vector(QQ,[1,2,3])], zeros='bogus') 

Traceback (most recent call last): 

... 

ValueError: 'zeros' keyword must be 'left' or 'right', not 'bogus' 

 

An empty input set is linearly independent, vacuously. :: 

 

sage: (QQ^3).linear_dependence([]) == [] 

True 

""" 

if check: 

for v in vectors: 

if not v in self: 

raise ValueError('vector %s is not an element of %s' % (v, self)) 

if zeros == 'left': 

basis = 'echelon' 

elif zeros == 'right': 

basis = 'pivot' 

else: 

raise ValueError("'zeros' keyword must be 'left' or 'right', not '%s'" % zeros) 

import sage.matrix.constructor 

A = sage.matrix.constructor.matrix(vectors) # as rows, so get left kernel 

return A.left_kernel(basis=basis).basis() 

 

def __truediv__(self, sub): 

""" 

Return the quotient of ``self`` by the given subspace sub. 

 

EXAMPLES:: 

 

sage: V = RDF^3; W = V.span([[1,0,-1], [1,-1,0]]) 

sage: Q = V/W; Q 

Vector space quotient V/W of dimension 1 over Real Double Field where 

V: Vector space of dimension 3 over Real Double Field 

W: Vector space of degree 3 and dimension 2 over Real Double Field 

Basis matrix: 

[ 1.0 0.0 -1.0] 

[ 0.0 1.0 -1.0] 

sage: type(Q) 

<class 'sage.modules.quotient_module.FreeModule_ambient_field_quotient_with_category'> 

sage: V([1,2,3]) 

(1.0, 2.0, 3.0) 

sage: Q == V.quotient(W) 

True 

sage: Q(W.0) 

(0.0) 

""" 

return self.quotient(sub, check=True) 

 

def quotient(self, sub, check=True): 

""" 

Return the quotient of ``self`` by the given subspace sub. 

 

INPUT: 

 

 

- ``sub`` - a submodule of self, or something that can 

be turned into one via self.submodule(sub). 

 

- ``check`` - (default: True) whether or not to check 

that sub is a submodule. 

 

 

EXAMPLES:: 

 

sage: A = QQ^3; V = A.span([[1,2,3], [4,5,6]]) 

sage: Q = V.quotient( [V.0 + V.1] ); Q 

Vector space quotient V/W of dimension 1 over Rational Field where 

V: Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

W: Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[1 1 1] 

sage: Q(V.0 + V.1) 

(0) 

 

We illustrate that the base rings must be the same:: 

 

sage: (QQ^2)/(ZZ^2) 

Traceback (most recent call last): 

... 

ValueError: base rings must be the same 

""" 

# Calling is_submodule may be way too slow and repeat work done below. 

# It will be very desirable to somehow do this step better. 

if is_FreeModule(sub) and self.base_ring() != sub.base_ring(): 

raise ValueError("base rings must be the same") 

if check and (not is_FreeModule(sub) or not sub.is_subspace(self)): 

try: 

sub = self.subspace(sub) 

except (TypeError, ArithmeticError): 

raise ArithmeticError("sub must be a subspace of self") 

A, L = self.__quotient_matrices(sub) 

from . import quotient_module 

return quotient_module.FreeModule_ambient_field_quotient(self, sub, A, L) 

 

def __quotient_matrices(self, sub): 

r""" 

This internal function is used by 

``self.quotient(...)``. 

 

EXAMPLES:: 

 

sage: V = QQ^3; W = V.span([[1,0,-1], [1,-1,0]]) 

sage: A, L = V._FreeModule_generic_field__quotient_matrices(W) 

sage: A 

[1] 

[1] 

[1] 

sage: L 

[1 0 0] 

 

The quotient and lift maps are used to compute in the quotient and 

to lift:: 

 

sage: Q = V/W 

sage: Q(W.0) 

(0) 

sage: Q.lift_map()(Q.0) 

(1, 0, 0) 

sage: Q(Q.lift_map()(Q.0)) 

(1) 

 

An example in characteristic 5:: 

 

sage: A = GF(5)^2; B = A.span([[1,3]]); A / B 

Vector space quotient V/W of dimension 1 over Finite Field of size 5 where 

V: Vector space of dimension 2 over Finite Field of size 5 

W: Vector space of degree 2 and dimension 1 over Finite Field of size 5 

Basis matrix: 

[1 3] 

""" 

# 2. Find a basis C for a another submodule of self, so that 

# B + C is a basis for self. 

# 3. Then the quotient map is: 

# x |---> 'write in terms of basis for C and take the last m = #C-#B components. 

# 4. And a section of this map is: 

# x |---> corresponding linear combination of entries of last m entries 

# of the basis C. 

 

# Step 1: Find bases for spaces 

B = sub.basis_matrix() 

S = self.basis_matrix() 

 

n = self.dimension() 

m = n - sub.dimension() 

 

# Step 2: Extend basis B to a basis for self. 

# We do this by simply finding the pivot rows of the matrix 

# whose rows are a basis for sub concatenated with a basis for 

# self. 

C = B.stack(S).transpose() 

A = C.matrix_from_columns(C.pivots()).transpose() 

 

# Step 3: Compute quotient map 

# The quotient map is given by writing in terms of the above basis, 

# then taking the last #C columns 

 

# Compute the matrix D "change of basis from S to A" 

# that writes each element of the basis 

# for ``self`` in terms of the basis of rows of A, i.e., 

# want to find D such that 

# D * A = S 

# where D is a square n x n matrix. 

# Our algorithm is to note that D is determined if we just 

# replace both A and S by the submatrix got from their pivot 

# columns. 

P = A.pivots() 

AA = A.matrix_from_columns(P) 

SS = S.matrix_from_columns(P) 

D = SS * AA**(-1) 

 

# Compute the image of each basis vector for ``self`` under the 

# map "write an element of ``self`` in terms of the basis A" then 

# take the last n-m components. 

Q = D.matrix_from_columns(range(n - m, n)) 

 

# Step 4. Section map 

# The lifting or section map 

Dinv = D**(-1) 

L = Dinv.matrix_from_rows(range(n - m, n)) 

 

return Q, L 

 

def quotient_abstract(self, sub, check=True): 

r""" 

Return an ambient free module isomorphic to the quotient space 

of ``self`` modulo ``sub``, together with maps from ``self`` to 

the quotient, and a lifting map in the other direction. 

 

Use ``self.quotient(sub)`` to obtain the quotient 

module as an object equipped with natural maps in both directions, 

and a canonical coercion. 

 

INPUT: 

 

- ``sub`` -- a submodule of ``self`` or something that can 

be turned into one via ``self.submodule(sub)`` 

 

- ``check`` -- (default: ``True``) whether or not to check 

that sub is a submodule 

 

OUTPUT: 

 

- ``U`` -- the quotient as an abstract *ambient* free module 

 

- ``pi`` -- projection map to the quotient 

 

- ``lift`` -- lifting map back from quotient 

 

 

EXAMPLES:: 

 

sage: V = GF(19)^3 

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

sage: U,pi,lift = V.quotient_abstract(W) 

sage: pi(V.2) 

(18) 

sage: pi(V.0) 

(1) 

sage: pi(V.0 + V.2) 

(0) 

 

Another example involving a quotient of one subspace by another:: 

 

sage: A = matrix(QQ,4,4,[0,1,0,0, 0,0,1,0, 0,0,0,1, 0,0,0,0]) 

sage: V = (A^3).kernel() 

sage: W = A.kernel() 

sage: U, pi, lift = V.quotient_abstract(W) 

sage: [pi(v) == 0 for v in W.gens()] 

[True] 

sage: [pi(lift(b)) == b for b in U.basis()] 

[True, True] 

""" 

# Calling is_subspace may be way too slow and repeat work done below. 

# It will be very desirable to somehow do this step better. 

if check and (not is_FreeModule(sub) or not sub.is_subspace(self)): 

try: 

sub = self.subspace(sub) 

except (TypeError, ArithmeticError): 

raise ArithmeticError("sub must be a subspace of self") 

 

A, L = self.__quotient_matrices(sub) 

quomap = self.hom(A) 

quo = quomap.codomain() 

liftmap = quo.Hom(self)(L) 

 

return quomap.codomain(), quomap, liftmap 

 

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

# 

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

# 

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

 

class FreeModule_ambient(FreeModule_generic): 

""" 

Ambient free module over a commutative ring. 

""" 

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

""" 

The free module of given rank over the given base_ring. 

 

INPUT: 

 

- ``base_ring`` -- a commutative ring 

 

- ``rank`` -- a non-negative integer 

 

- ``sparse`` -- bool (default: False) 

 

- ``coordinate_ring`` -- a ring containing ``base_ring`` 

(default: equal to ``base_ring``) 

 

EXAMPLES:: 

 

sage: FreeModule(ZZ, 4) 

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

""" 

FreeModule_generic.__init__(self, base_ring, rank=rank, 

degree=rank, sparse=sparse, coordinate_ring=coordinate_ring) 

 

def __hash__(self): 

""" 

The hash is obtained from the rank and the base ring. 

 

.. TODO:: 

 

Make pickling so that the hash is available early enough. 

 

EXAMPLES:: 

 

sage: V = QQ^7 

sage: hash(V) == hash((V.rank(), V.base_ring())) 

True 

""" 

try: 

return hash((self.rank(), self.base_ring())) 

except AttributeError: 

# This is a fallback because sometimes hash is called during object 

# reconstruction (unpickle), and the above fields haven't been 

# filled in yet. 

return 0 

 

def _coerce_map_from_(self, M): 

""" 

Return a coercion map from `M` to ``self``, or None. 

 

TESTS: 

 

Make sure :trac:`10513` is fixed (no coercion from a quotient 

vector space to an isomorphic abstract vector space):: 

 

sage: M = QQ^3 / [[1,2,3]] 

sage: V = QQ^2 

sage: V.coerce_map_from(M) 

 

""" 

if isinstance(M, FreeModule_ambient): 

from sage.modules.quotient_module import FreeModule_ambient_field_quotient 

if isinstance(M, FreeModule_ambient_field_quotient): 

# No forgetful map. 

return None 

elif (self.base_ring().has_coerce_map_from(M.base_ring()) 

and self.rank() == M.rank()): 

# We could return M.hom(self.basis(), self), but the 

# complexity of this is quadratic in space and time, 

# since it constructs a matrix. 

return True 

return super(FreeModule_ambient, self)._coerce_map_from_(M) 

 

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: M = FreeModule(Integers(8),3) 

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

sage: M is S._dense_module() 

True 

""" 

return FreeModule(base_ring=self.base_ring(), rank = self.rank(), 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: M = FreeModule(Integers(8),3) 

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

sage: M._sparse_module() is S 

True 

""" 

return FreeModule(base_ring=self.base_ring(), rank = self.rank(), sparse=True) 

 

def echelonized_basis_matrix(self): 

""" 

The echelonized basis matrix of self. 

 

EXAMPLES:: 

 

sage: V = ZZ^4 

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

sage: W.basis_matrix() 

[ 1 0 0 -1] 

[ 0 1 0 -1] 

[ 0 0 1 -1] 

sage: W.echelonized_basis_matrix() 

[ 1 0 0 -1] 

[ 0 1 0 -1] 

[ 0 0 1 -1] 

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

sage: U.basis_matrix() 

[-1 1 0 0] 

[-1 0 1 0] 

[-1 0 0 1] 

sage: U.echelonized_basis_matrix() 

[ 1 0 0 -1] 

[ 0 1 0 -1] 

[ 0 0 1 -1] 

""" 

return self.basis_matrix() 

 

def _echelon_matrix_richcmp(self, other, op): 

r""" 

Compare the free module ``self`` with ``other`. 

 

This compares modules by their ambient spaces, then by dimension, 

then in order by their echelon matrices. However, if 

``other`` is a sub-module or is a quotient module then its 

total comparison method is used instead of generic comparison. 

 

EXAMPLES: 

 

We compare rank three free modules over the integers and 

rationals:: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

sage: (QQ^3)._echelon_matrix_richcmp(CC^3, op_LT) 

True 

sage: (CC^3)._echelon_matrix_richcmp(QQ^3, op_LT) 

False 

sage: (CC^3)._echelon_matrix_richcmp(QQ^3, op_GT) 

True 

 

:: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

sage: Q = QQ; Z = ZZ 

sage: (Q^3)._echelon_matrix_richcmp(Z^3, op_GT) 

True 

sage: (Q^3)._echelon_matrix_richcmp(Z^3, op_LT) 

False 

sage: (Z^3)._echelon_matrix_richcmp(Q^3, op_LT) 

True 

sage: (Z^3)._echelon_matrix_richcmp(Q^3, op_GT) 

False 

sage: (Q^3)._echelon_matrix_richcmp(Z^3, op_EQ) 

False 

sage: (Q^3)._echelon_matrix_richcmp(Q^3, op_EQ) 

True 

 

Comparison with a sub-module:: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

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

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: A = QQ^3 

sage: V._echelon_matrix_richcmp(A, op_LT) 

True 

sage: A._echelon_matrix_richcmp(V, op_LT) 

False 

 

Comparison with a quotient module (see :trac:`10513`):: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

sage: M = QQ^3 / [[1,2,3]] 

sage: V = QQ^2 

sage: V._echelon_matrix_richcmp(M, op_EQ) 

False 

sage: M._echelon_matrix_richcmp(V, op_EQ) 

False 

""" 

if self is other: 

return rich_to_bool(op, 0) 

if not isinstance(other, FreeModule_generic): 

return NotImplemented 

from sage.modules.quotient_module import FreeModule_ambient_field_quotient 

if isinstance(other, FreeModule_ambient): 

if isinstance(other, FreeModule_ambient_field_quotient) or isinstance(self, FreeModule_ambient_field_quotient): 

return richcmp(self,other,op) 

lx = self.rank() 

rx = other.rank() 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

lx = self.base_ring() 

rx = other.base_ring() 

if lx == rx: 

# We do not want to create an inner product matrix in memory if 

# self and other use the dot product 

if self._inner_product_is_dot_product() and other._inner_product_is_dot_product(): 

return rich_to_bool(op, 0) 

else: 

#this only affects free_quadratic_modules 

lx = self.inner_product_matrix() 

rx = other.inner_product_matrix() 

return richcmp(lx,rx,op) 

try: 

if self.base_ring().is_subring(other.base_ring()): 

return rich_to_bool(op, -1) 

elif other.base_ring().is_subring(self.base_ring()): 

return rich_to_bool(op, 1) 

except NotImplementedError: 

pass 

return richcmp_not_equal(lx, rx, op) 

 

else: 

# now other is not ambient or is a quotient; 

# it knows how to do the comparison. 

return other._echelon_matrix_richcmp( self, revop(op)) 

 

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 _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 

 

(Now clean up again.) 

 

:: 

 

sage: M.reset_name() 

sage: M 

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

""" 

if self.is_sparse(): 

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

else: 

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

 

def _latex_(self): 

r""" 

Return a latex representation of this ambient free module. 

 

EXAMPLES:: 

 

sage: latex(QQ^3) # indirect doctest 

\Bold{Q}^{3} 

 

:: 

 

sage: A = GF(5)^20 

sage: latex(A) # indiret doctest 

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

 

:: 

 

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

sage: latex(A) #indirect doctest 

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

""" 

t = "%s"%latex.latex(self.base_ring()) 

if t.find(" ") != -1: 

t = "(%s)"%t 

return "%s^{%s}"%(t, self.rank()) 

 

def is_ambient(self): 

""" 

Return ``True`` since this module is an ambient 

module. 

 

EXAMPLES:: 

 

sage: A = QQ^5; A.is_ambient() 

True 

sage: A = (QQ^5).span([[1,2,3,4,5]]); A.is_ambient() 

False 

""" 

return True 

 

def ambient_module(self): 

""" 

Return ``self``, since ``self`` is ambient. 

 

EXAMPLES:: 

 

sage: A = QQ^5; A.ambient_module() 

Vector space of dimension 5 over Rational Field 

sage: A = ZZ^5; A.ambient_module() 

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

""" 

return self 

 

def basis(self): 

""" 

Return a basis for this ambient free module. 

 

OUTPUT: 

 

 

- ``Sequence`` - an immutable sequence with universe 

this ambient free module 

 

 

EXAMPLES:: 

 

sage: A = ZZ^3; B = A.basis(); B 

[ 

(1, 0, 0), 

(0, 1, 0), 

(0, 0, 1) 

] 

sage: B.universe() 

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

""" 

try: 

return self.__basis 

except AttributeError: 

ZERO = self(0) 

one = self.coordinate_ring().one() 

w = [] 

for n in range(self.rank()): 

v = ZERO.__copy__() 

v.set(n, one) 

w.append(v) 

self.__basis = basis_seq(self, w) 

return self.__basis 

 

def echelonized_basis(self): 

""" 

Return a basis for this ambient free module in echelon form. 

 

EXAMPLES:: 

 

sage: A = ZZ^3; A.echelonized_basis() 

[ 

(1, 0, 0), 

(0, 1, 0), 

(0, 0, 1) 

] 

""" 

return self.basis() 

 

def change_ring(self, R): 

""" 

Return the ambient free module over R of the same rank as self. 

 

EXAMPLES:: 

 

sage: A = ZZ^3; A.change_ring(QQ) 

Vector space of dimension 3 over Rational Field 

sage: A = ZZ^3; A.change_ring(GF(5)) 

Vector space of dimension 3 over Finite Field of size 5 

 

For ambient modules any change of rings is defined. 

 

:: 

 

sage: A = GF(5)**3; A.change_ring(QQ) 

Vector space of dimension 3 over Rational Field 

""" 

if self.base_ring() is R: 

return self 

from .free_quadratic_module import is_FreeQuadraticModule 

if is_FreeQuadraticModule(self): 

return FreeModule(R, self.rank(), inner_product_matrix=self.inner_product_matrix()) 

else: 

return FreeModule(R, self.rank()) 

 

 

def linear_combination_of_basis(self, v): 

""" 

Return the linear combination of the basis for ``self`` obtained from 

the elements of the list v. 

 

INPUT: 

 

- ``v`` - list 

 

EXAMPLES:: 

 

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

sage: V 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 2 3] 

[0 3 6] 

sage: V.linear_combination_of_basis([1,1]) 

(1, 5, 9) 

 

This should raise an error if the resulting element is not in self:: 

 

sage: W = span([[2,4]], ZZ) 

sage: W.linear_combination_of_basis([1/2]) 

Traceback (most recent call last): 

... 

TypeError: element [1, 2] is not in free module 

""" 

return self(v) 

 

def coordinate_vector(self, v, check=True): 

""" 

Write `v` in terms of the standard basis for ``self`` and 

return the resulting coefficients in a vector over the fraction 

field of the base ring. 

 

Returns a vector `c` such that if `B` is the basis for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: V = Integers(16)^3 

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

(1, 5, 9) 

sage: v.parent() 

Ambient free module of rank 3 over Ring of integers modulo 16 

""" 

return self(v) 

 

def echelon_coordinate_vector(self, v, check=True): 

r""" 

Same as ``self.coordinate_vector(v)``, since ``self`` is 

an ambient free module. 

 

INPUT: 

 

 

- ``v`` - vector 

 

- ``check`` - bool (default: True); if True, also 

verify that v is really in self. 

 

 

OUTPUT: list 

 

EXAMPLES:: 

 

sage: V = QQ^4 

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

sage: v 

(-1/2, 1/2, -1/2, 1/2) 

sage: V.coordinate_vector(v) 

(-1/2, 1/2, -1/2, 1/2) 

sage: V.echelon_coordinate_vector(v) 

(-1/2, 1/2, -1/2, 1/2) 

sage: W = V.submodule_with_basis([[1/2,1/2,1/2,1/2],[1,0,1,0]]) 

sage: W.coordinate_vector(v) 

(1, -1) 

sage: W.echelon_coordinate_vector(v) 

(-1/2, 1/2) 

""" 

return self.coordinate_vector(v, check=check) 

 

def echelon_coordinates(self, v, check=True): 

""" 

Returns the coordinate vector of v in terms of the echelon basis 

for self. 

 

EXAMPLES:: 

 

sage: U = VectorSpace(QQ,3) 

sage: [ U.coordinates(v) for v in U.basis() ] 

[[1, 0, 0], [0, 1, 0], [0, 0, 1]] 

sage: [ U.echelon_coordinates(v) for v in U.basis() ] 

[[1, 0, 0], [0, 1, 0], [0, 0, 1]] 

sage: V = U.submodule([[1,1,0],[0,1,1]]) 

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 1] 

sage: [ V.coordinates(v) for v in V.basis() ] 

[[1, 0], [0, 1]] 

sage: [ V.echelon_coordinates(v) for v in V.basis() ] 

[[1, 0], [0, 1]] 

sage: W = U.submodule_with_basis([[1,1,0],[0,1,1]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[1 1 0] 

[0 1 1] 

sage: [ W.coordinates(v) for v in W.basis() ] 

[[1, 0], [0, 1]] 

sage: [ W.echelon_coordinates(v) for v in W.basis() ] 

[[1, 1], [0, 1]] 

""" 

return self.coordinates(v, check=check) 

 

def random_element(self, prob=1.0, *args, **kwds): 

""" 

Returns a random element of self. 

 

INPUT: 

 

 

- ``prob`` - float. Each coefficient will be set to zero with 

probability `1-prob`. Otherwise coefficients will be chosen 

randomly from base ring (and may be zero). 

 

- ``*args, **kwds`` - passed on to random_element function of base 

ring. 

 

 

EXAMPLES:: 

 

sage: M = FreeModule(ZZ, 3) 

sage: M.random_element() 

(-1, 2, 1) 

sage: M.random_element() 

(-95, -1, -2) 

sage: M.random_element() 

(-12, 0, 0) 

 

Passes extra positional or keyword arguments through:: 

 

sage: M.random_element(5,10) 

(5, 5, 5) 

 

 

:: 

 

sage: M = FreeModule(ZZ, 16) 

sage: M.random_element() 

(-6, 5, 0, 0, -2, 0, 1, -4, -6, 1, -1, 1, 1, -1, 1, -1) 

sage: M.random_element(prob=0.3) 

(0, 0, 0, 0, -3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -3) 

""" 

rand = current_randstate().python_random().random 

R = self.base_ring() 

v = self(0) 

prob = float(prob) 

for i in range(self.rank()): 

if rand() <= prob: 

v[i] = R.random_element(*args, **kwds) 

return v 

 

def gen(self, i=0): 

""" 

Return the `i`-th generator for ``self``. 

 

Here `i` is between 0 and rank - 1, inclusive. 

 

INPUT: 

 

- `i` -- an integer (default 0) 

 

OUTPUT: `i`-th basis vector for ``self``. 

 

EXAMPLES:: 

 

sage: n = 5 

sage: V = QQ^n 

sage: B = [V.gen(i) for i in range(n)] 

sage: B 

[(1, 0, 0, 0, 0), 

(0, 1, 0, 0, 0), 

(0, 0, 1, 0, 0), 

(0, 0, 0, 1, 0), 

(0, 0, 0, 0, 1)] 

sage: V.gens() == tuple(B) 

True 

 

TESTS:: 

 

sage: (QQ^3).gen(4/3) 

Traceback (most recent call last): 

... 

TypeError: rational is not an integer 

 

Check that :trac:`10262` and :trac:`13304` are fixed 

(coercions involving :class:`FreeModule_ambient` used to take 

quadratic time and space in the rank of the module):: 

 

sage: vector([0]*50000)/1 

(0, 0, 0, ..., 0) 

""" 

if i < 0 or i >= self.rank(): 

raise ValueError("Generator %s not defined." % i) 

try: 

return self.__basis[i] 

except AttributeError: 

v = self(0) 

v[i] = self.base_ring().one() 

v.set_immutable() 

return v 

 

 

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

# 

# Ambient free modules over an integral domain. 

# 

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

 

class FreeModule_ambient_domain(FreeModule_ambient): 

""" 

Ambient free module over an integral domain. 

""" 

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

""" 

Create the ambient free module of given rank over the given 

integral domain. 

 

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 

""" 

FreeModule_ambient.__init__(self, base_ring, 

rank, sparse, coordinate_ring) 

 

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 _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 

 

(Now clean up again.) 

 

:: 

 

sage: M.reset_name() 

sage: M 

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

""" 

if self.is_sparse(): 

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

self.rank(), self.base_ring()) 

else: 

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

self.rank(), self.base_ring()) 

 

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; 

sage: V = M.ambient_vector_space(); V 

Vector space of dimension 3 over Rational Field 

 

If an inner product on the module is specified, then this 

is preserved on the ambient vector space. 

 

:: 

 

sage: N = FreeModule(ZZ,4,inner_product_matrix=1) 

sage: U = N.ambient_vector_space() 

sage: U 

Ambient quadratic space of dimension 4 over Rational Field 

Inner product matrix: 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

sage: P = N.submodule_with_basis([[1,-1,0,0],[0,1,-1,0],[0,0,1,-1]]) 

sage: P.gram_matrix() 

[ 2 -1 0] 

[-1 2 -1] 

[ 0 -1 2] 

sage: U == N.ambient_vector_space() 

True 

sage: U == V 

False 

""" 

try: 

return self.__ambient_vector_space 

except AttributeError: 

self.__ambient_vector_space = FreeModule(self.base_field(), self.rank(), sparse=self.is_sparse()) 

return self.__ambient_vector_space 

 

def coordinate_vector(self, v, check=True): 

""" 

Write `v` in terms of the standard basis for ``self`` and 

return the resulting coefficients in a vector over the fraction 

field of the base ring. 

 

INPUT: 

 

- ``v`` -- vector 

 

- ``check`` -- bool (default: True); if True, also verify that 

`v` is really in self. 

 

OUTPUT: list 

 

Returns a vector `c` such that if `B` is the basis for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

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

(1, 5, 9) 

sage: v.parent() 

Vector space of dimension 3 over Rational Field 

""" 

# Calling the element constructor directly, since the 

# usual call method indirectly relies on coordinate_vector, 

# hence, an infinite recursion would result 

try: 

out = self.ambient_vector_space()._element_constructor_(v) 

except TypeError: 

raise ArithmeticError("Error transforming the given vector into the ambient vector space") 

if check and out not in self: 

raise ArithmeticError("The given vector does not belong to this free module") 

return out 

 

def vector_space(self, base_field=None): 

""" 

Returns the vector space obtained from ``self`` by tensoring with the 

fraction field of the base ring and extending to the field. 

 

EXAMPLES:: 

 

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

Vector space of dimension 3 over Rational Field 

""" 

if base_field is None: 

R = self.base_ring() 

return self.change_ring(R.fraction_field()) 

else: 

return self.change_ring(base_field) 

 

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

# 

# Ambient free modules over a principal ideal domain. 

# 

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

 

class FreeModule_ambient_pid(FreeModule_generic_pid, FreeModule_ambient_domain): 

""" 

Ambient free module over a principal ideal domain. 

""" 

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

""" 

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) 

 

- ``coordinate_ring`` -- a ring containing ``base_ring`` 

(default: equal to ``base_ring``) 

 

EXAMPLES:: 

 

sage: ZZ^3 

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

 

We create the same module with coordinates in ``QQ``:: 

 

sage: from sage.modules.free_module import FreeModule_ambient_pid 

sage: M = FreeModule_ambient_pid(ZZ, 3, coordinate_ring=QQ) 

sage: M 

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

sage: v = M.basis()[0]; v 

(1, 0, 0) 

sage: type(v) 

<type 'sage.modules.vector_rational_dense.Vector_rational_dense'> 

""" 

FreeModule_ambient_domain.__init__(self, base_ring=base_ring, 

rank=rank, sparse=sparse, coordinate_ring=coordinate_ring) 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

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 _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 

 

(Now clean up again.) 

 

:: 

 

sage: M.reset_name() 

sage: M 

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

""" 

if self.is_sparse(): 

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

self.rank(), self.base_ring()) 

else: 

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

self.rank(), self.base_ring()) 

 

 

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

# 

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

# 

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

 

class FreeModule_ambient_field(FreeModule_generic_field, FreeModule_ambient_pid): 

""" 

 

""" 

def __init__(self, base_field, dimension, 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: QQ^3 

Vector space of dimension 3 over Rational Field 

""" 

FreeModule_ambient_pid.__init__(self, base_field, dimension, sparse=sparse) 

 

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 _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 

 

(Now clean up again.) 

 

:: 

 

sage: V.reset_name() 

sage: V 

Vector space of dimension 7 over Rational Field 

""" 

if self.is_sparse(): 

return "Sparse vector space of dimension %s over %s"%(self.dimension(), self.base_ring()) 

else: 

return "Vector space of dimension %s over %s"%(self.dimension(), self.base_ring()) 

 

def ambient_vector_space(self): 

""" 

Returns ``self`` as the ambient vector space. 

 

EXAMPLES:: 

 

sage: M = QQ^3 

sage: M.ambient_vector_space() 

Vector space of dimension 3 over Rational Field 

""" 

return self 

 

def base_field(self): 

""" 

Returns the base field of this vector space. 

 

EXAMPLES:: 

 

sage: M = QQ^3 

sage: M.base_field() 

Rational Field 

""" 

return self.base_ring() 

 

def _element_constructor_(self, e, *args, **kwds): 

""" 

Create an element of this vector space. 

 

EXAMPLES:: 

 

sage: k.<a> = GF(3^4) 

sage: VS = k.vector_space() 

sage: VS(a) 

(0, 1, 0, 0) 

""" 

try: 

k = e.parent() 

if finite_field.is_FiniteField(k) and k.base_ring() == self.base_ring() and k.degree() == self.degree(): 

return self(e._vector_()) 

except AttributeError: 

pass 

return FreeModule_generic_field._element_constructor_(self, e, *args, **kwds) 

 

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

# 

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

# 

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

 

class FreeModule_submodule_with_basis_pid(FreeModule_generic_pid): 

r""" 

Construct a submodule of a free module over PID with a distinguished basis. 

 

INPUT: 

 

- ``ambient`` -- ambient free module over a principal ideal domain `R`, 

i.e. `R^n`; 

 

- ``basis`` -- list of elements of `K^n`, where `K` is the fraction field 

of `R`. These elements must be linearly independent and will be used as 

the default basis of the constructed submodule; 

 

- ``check`` -- (default: ``True``) if ``False``, correctness of the input 

will not be checked and type conversion may be omitted, use with care; 

 

- ``echelonize`` -- (default:``False``) if ``True``, ``basis`` will be 

echelonized and the result will be used as the default basis of the 

constructed submodule; 

 

- `` echelonized_basis`` -- (default: ``None``) if not ``None``, must be 

the echelonized basis spanning the same submodule as ``basis``; 

 

- ``already_echelonized`` -- (default: ``False``) if ``True``, ``basis`` 

must be already given in the echelonized form. 

 

OUTPUT: 

 

- `R`-submodule of `K^n` with the user-specified ``basis``. 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

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

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1 2 3] 

[4 5 6] 

 

Now we create a submodule of the ambient vector space, rather than 

``M`` itself:: 

 

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

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[ 1 2 3/2] 

[ 4 5 6] 

""" 

 

def __init__(self, ambient, basis, check=True, 

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

r""" 

See :class:`FreeModule_submodule_with_basis_pid` for documentation. 

 

TESTS:: 

 

sage: M = ZZ^3 

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

sage: TestSuite(W).run() 

 

We test that the issue at :trac:`9502` is solved:: 

 

sage: parent(W.basis()[0]) 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1 2 3] 

[4 5 6] 

sage: parent(W.echelonized_basis()[0]) 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1 2 3] 

[4 5 6] 

 

Now we test that the issue introduced at :trac:`9502` and reported at 

:trac:`10250` is solved as well:: 

 

sage: V = (QQ^2).span_of_basis([[1,1]]) 

sage: w = sqrt(2) * V([1,1]) 

sage: 3 * w 

(3*sqrt(2), 3*sqrt(2)) 

""" 

if not isinstance(ambient, FreeModule_ambient_pid): 

raise TypeError("ambient (=%s) must be ambient." % ambient) 

self.__ambient_module = ambient 

R = ambient.base_ring() 

R_coord = R 

 

# Convert all basis elements to the ambient module 

try: 

basis = [ambient(x) for x in basis] 

except TypeError: 

# That failed, try the ambient vector space instead 

V = ambient.ambient_vector_space() 

R_coord = V.base_ring() 

try: 

basis = [V(x) for x in basis] 

except TypeError: 

raise TypeError("each element of basis must be in " 

"the ambient vector space") 

 

if echelonize and not already_echelonized: 

basis = self._echelonized_basis(ambient, basis) 

 

# The following is WRONG - we should call __init__ of 

# FreeModule_generic_pid. However, it leads to a bunch of errors. 

FreeModule_generic.__init__(self, base_ring=R, coordinate_ring=R_coord, 

rank=len(basis), degree=ambient.degree(), 

sparse=ambient.is_sparse()) 

C = self.element_class 

w = [C(self, x.list(), coerce=False, copy=False) for x in basis] 

self.__basis = basis_seq(self, w) 

 

if echelonize or already_echelonized: 

self.__echelonized_basis = self.__basis 

else: 

if echelonized_basis is None: 

echelonized_basis = self._echelonized_basis(ambient, basis) 

w = [C(self, x.list(), coerce=False, copy=True) 

for x in echelonized_basis] 

self.__echelonized_basis = basis_seq(self, w) 

if check and len(basis) != len(self.__echelonized_basis): 

raise ValueError("The given basis vectors must be linearly " 

"independent.") 

 

def __hash__(self): 

""" 

The hash is given by the basis. 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

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

sage: hash(W) == hash(W.basis()) 

True 

""" 

return hash(self.__basis) 

 

def _echelon_matrix_richcmp(self, other, op): 

r""" 

Compare the free module ``self`` with other. 

 

Modules are ordered by their ambient spaces, then by dimension, 

then in order by their echelon matrices. 

 

.. NOTE:: 

 

Use :meth:`is_submodule` to determine if one 

module is a submodule of another. 

 

EXAMPLES: 

 

First we compare two equal vector spaces. 

 

:: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

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

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

sage: V._echelon_matrix_richcmp(W,op_EQ) 

True 

 

Next we compare a one dimensional space to the two dimensional 

space defined above. 

 

:: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

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

sage: V._echelon_matrix_richcmp(M,op_EQ) 

False 

sage: M._echelon_matrix_richcmp(V, op_LT) 

True 

sage: V._echelon_matrix_richcmp(M, op_LT) 

False 

 

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

space above.:: 

 

sage: from sage.structure.richcmp import op_LT,op_LE,op_EQ,op_NE,op_GT,op_GE 

sage: V = span([[5,6,7]], ZZ).scale(1/11); V 

Free module of degree 3 and rank 1 over Integer Ring 

Echelon basis matrix: 

[5/11 6/11 7/11] 

sage: V._echelon_matrix_richcmp(M, op_LT) 

True 

sage: M._echelon_matrix_richcmp(V, op_LT) 

False 

""" 

if self is other: 

return rich_to_bool(op, 0) 

if not isinstance(other, FreeModule_generic): 

return NotImplemented 

lx = self.ambient_vector_space() 

rx = other.ambient_vector_space() 

if lx != rx: 

return lx._echelon_matrix_richcmp( rx, op) 

 

lx = self.dimension() 

rx = other.dimension() 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

 

lx = self.base_ring() 

rx = other.base_ring() 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

 

# We use self.echelonized_basis_matrix() == other.echelonized_basis_matrix() 

# with the matrix to avoid a circular reference. 

return richcmp(self.echelonized_basis_matrix(), 

other.echelonized_basis_matrix(), op) 

 

def construction(self): 

""" 

Returns the functorial construction of self, namely, the subspace 

of the ambient module spanned by the given basis. 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

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

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1 2 3] 

[4 5 6] 

sage: c, V = W.construction() 

sage: c(V) == W 

True 

""" 

from sage.categories.pushout import SubspaceFunctor 

return SubspaceFunctor(self.basis()), self.ambient_module() 

 

def echelonized_basis_matrix(self): 

""" 

Return basis matrix for ``self`` in row echelon form. 

 

EXAMPLES:: 

 

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

sage: V.basis_matrix() 

[1 2 3] 

[4 5 6] 

sage: V.echelonized_basis_matrix() 

[1 2 3] 

[0 3 6] 

""" 

try: 

return self.__echelonized_basis_matrix 

except AttributeError: 

pass 

self._echelonized_basis(self.ambient_module(), self.__basis) 

return self.__echelonized_basis_matrix 

 

def _echelonized_basis(self, ambient, basis): 

""" 

Given the ambient space and a basis, construct and cache the 

echelonized basis matrix and returns its rows. 

 

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

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: N = M.submodule_with_basis([[1,1,0],[0,2,1]]) 

sage: N._echelonized_basis(M,N.basis()) 

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

sage: V = QQ^3 

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

sage: W._echelonized_basis(V,W.basis()) 

[(1, 0, -1/2), (0, 1, 1/2)] 

sage: V = SR^3 

sage: W = V.submodule_with_basis([[1,0,1]]) 

sage: W._echelonized_basis(V,W.basis()) 

[(1, 0, 1)] 

""" 

# Return the first rank rows (i.e., the nonzero rows). 

d = self._denominator(basis) 

MAT = sage.matrix.matrix_space.MatrixSpace( 

ambient.base_ring(), len(basis), ambient.degree(), sparse = ambient.is_sparse()) 

if d != 1: 

basis = [x*d for x in basis] 

A = MAT(basis) 

E = A.echelon_form() 

if d != 1: 

E = E.matrix_over_field()*(~d) # divide out denominator 

r = E.rank() 

if r < E.nrows(): 

E = E.matrix_from_rows(range(r)) 

self.__echelonized_basis_matrix = E 

return E.rows() 

 

 

 

def _denominator(self, B): 

""" 

The LCM of the denominators of the given list B. 

 

N.B.: This function is for internal use only! 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: L = V.span([[1,1/2,1/3], [-1/5,2/3,3]],ZZ) 

sage: L 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 1/5 19/6 37/3] 

[ 0 23/6 46/3] 

sage: L._denominator(L.echelonized_basis_matrix().list()) 

30 

 

""" 

if len(B) == 0: 

return 1 

d = B[0].denominator() 

from sage.arith.all import lcm 

for x in B[1:]: 

d = lcm(d,x.denominator()) 

return d 

 

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 module of degree %s and rank %s over %s\n"%( 

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

"User basis matrix:\n%r" % self.basis_matrix() 

else: 

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

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

"User basis matrix:\n%r" % self.basis_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 ambient_module(self): 

""" 

Return the ambient module related to the `R`-module self, 

which was used when creating this module, and is of the form 

`R^n`. Note that ``self`` need not be contained in the ambient 

module, though ``self`` will be contained in the ambient vector space. 

 

EXAMPLES:: 

 

sage: A = ZZ^3 

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

sage: M 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[ 1 2 3/7] 

[ 4 5 6] 

sage: M.ambient_module() 

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

sage: M.is_submodule(M.ambient_module()) 

False 

""" 

return self.__ambient_module 

 

def echelon_coordinates(self, v, check=True): 

r""" 

Write `v` in terms of the echelonized basis for self. 

 

INPUT: 

 

 

- ``v`` - vector 

 

- ``check`` - bool (default: True); if True, also 

verify that v is really in self. 

 

 

OUTPUT: list 

 

Returns a list `c` such that if `B` is the basis 

for self, then 

 

.. MATH:: 

 

\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: A = ZZ^3 

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

sage: M.coordinates([8,10,12]) 

[0, 2] 

sage: M.echelon_coordinates([8,10,12]) 

[8, -2] 

sage: B = M.echelonized_basis(); B 

[ 

(1, 2, 3/7), 

(0, 3, -30/7) 

] 

sage: 8*B[0] - 2*B[1] 

(8, 10, 12) 

 

We do an example with a sparse vector space:: 

 

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

sage: W = V.subspace_with_basis([[0,1,2,0,0], [0,-1,0,0,-1/2]]) 

sage: W.echelonized_basis() 

[ 

(0, 1, 0, 0, 1/2), 

(0, 0, 1, 0, -1/4) 

] 

sage: W.echelon_coordinates([0,0,2,0,-1/2]) 

[0, 2] 

""" 

if not isinstance(v, free_module_element.FreeModuleElement): 

v = self.ambient_vector_space()(v) 

elif v.degree() != self.degree(): 

raise ArithmeticError("vector is not in free module") 

# Find coordinates of v with respect to rref basis. 

E = self.echelonized_basis_matrix() 

P = E.pivots() 

w = v.list_from_positions(P) 

# Next use the transformation matrix from the rref basis 

# to the echelon basis. 

T = self._rref_to_echelon_matrix() 

x = T.linear_combination_of_rows(w).list(copy=False) 

if not check: 

return x 

if v.parent() is self: 

return x 

lc = E.linear_combination_of_rows(x) 

if list(lc) != list(v): 

raise ArithmeticError("vector is not in free module") 

return x 

 

def user_to_echelon_matrix(self): 

""" 

Return matrix that transforms a vector written with respect to the 

user basis of ``self`` to one written with respect to the echelon 

basis. The matrix acts from the right, as is usual in Sage. 

 

EXAMPLES:: 

 

sage: A = ZZ^3 

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

sage: M.echelonized_basis() 

[ 

(1, 2, 3), 

(0, 3, 6) 

] 

sage: M.user_to_echelon_matrix() 

[ 1 0] 

[ 4 -1] 

 

The vector `v=(5,7,9)` in `M` is `(1,1)` 

with respect to the user basis. Multiplying the above matrix on the 

right by this vector yields `(5,-1)`, which has components 

the coordinates of `v` with respect to the echelon basis. 

 

:: 

 

sage: v0,v1 = M.basis(); v = v0+v1 

sage: e0,e1 = M.echelonized_basis() 

sage: v 

(5, 7, 9) 

sage: 5*e0 + (-1)*e1 

(5, 7, 9) 

""" 

try: 

return self.__user_to_echelon_matrix 

except AttributeError: 

if self.base_ring().is_field(): 

self.__user_to_echelon_matrix = self._user_to_rref_matrix() 

else: 

rows = sum([self.echelon_coordinates(b,check=False) for b in self.basis()], []) 

M = sage.matrix.matrix_space.MatrixSpace(self.base_ring().fraction_field(), 

self.dimension(), 

sparse = self.is_sparse()) 

self.__user_to_echelon_matrix = M(rows) 

return self.__user_to_echelon_matrix 

 

 

def echelon_to_user_matrix(self): 

""" 

Return matrix that transforms the echelon basis to the user basis 

of self. This is a matrix `A` such that if `v` is a 

vector written with respect to the echelon basis for ``self`` then 

`vA` is that vector written with respect to the user basis 

of self. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

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

sage: W.echelonized_basis() 

[ 

(1, 0, -1), 

(0, 1, 2) 

] 

sage: A = W.echelon_to_user_matrix(); A 

[-5/3 2/3] 

[ 4/3 -1/3] 

 

The vector `(1,1,1)` has coordinates `v=(1,1)` with 

respect to the echelonized basis for self. Multiplying `vA` 

we find the coordinates of this vector with respect to the user 

basis. 

 

:: 

 

sage: v = vector(QQ, [1,1]); v 

(1, 1) 

sage: v * A 

(-1/3, 1/3) 

sage: u0, u1 = W.basis() 

sage: (-u0 + u1)/3 

(1, 1, 1) 

""" 

try: 

return self.__echelon_to_user_matrix 

except AttributeError: 

self.__echelon_to_user_matrix = ~self.user_to_echelon_matrix() 

return self.__echelon_to_user_matrix 

 

def _user_to_rref_matrix(self): 

""" 

Returns a transformation matrix from the user specified basis to row 

reduced echelon form, for this module over a PID. 

 

Note: For internal use only! See user_to_echelon_matrix. 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: N = M.submodule_with_basis([[1,1,0],[0,1,1]]) 

sage: T = N.user_to_echelon_matrix(); T # indirect doctest 

[1 1] 

[0 1] 

sage: N.basis_matrix() 

[1 1 0] 

[0 1 1] 

sage: N.echelonized_basis_matrix() 

[ 1 0 -1] 

[ 0 1 1] 

sage: T * N.echelonized_basis_matrix() == N.basis_matrix() 

True 

""" 

try: 

return self.__user_to_rref_matrix 

except AttributeError: 

A = self.basis_matrix() 

P = self.echelonized_basis_matrix().pivots() 

T = A.matrix_from_columns(P) 

self.__user_to_rref_matrix = T 

return self.__user_to_rref_matrix 

 

def _rref_to_user_matrix(self): 

""" 

Returns a transformation matrix from row reduced echelon form to 

the user specified basis, for this module over a PID. 

 

Note: For internal use only! See user_to_echelon_matrix. 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: N = M.submodule_with_basis([[1,1,0],[0,1,1]]) 

sage: U = N.echelon_to_user_matrix(); U # indirect doctest 

[ 1 -1] 

[ 0 1] 

sage: N.echelonized_basis_matrix() 

[ 1 0 -1] 

[ 0 1 1] 

sage: N.basis_matrix() 

[1 1 0] 

[0 1 1] 

sage: U * N.basis_matrix() == N.echelonized_basis_matrix() 

True 

""" 

try: 

return self.__rref_to_user_matrix 

except AttributeError: 

self.__rref_to_user_matrix = ~self._user_to_rref_matrix() 

return self.__rref_to_user_matrix 

 

def _echelon_to_rref_matrix(self): 

""" 

Returns a transformation matrix from the some matrix to the row 

reduced echelon form for this module over a PID. 

 

Note: For internal use only! and not used! 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: N = M.submodule_with_basis([[1,1,0],[1,1,2]]) 

sage: N 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1 1 0] 

[1 1 2] 

sage: T = N._echelon_to_rref_matrix(); T 

[1 0] 

[0 2] 

sage: type(T) 

<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'> 

sage: U = N._rref_to_echelon_matrix(); U 

[ 1 0] 

[ 0 1/2] 

sage: type(U) 

<type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'> 

""" 

try: 

return self.__echelon_to_rref_matrix 

except AttributeError: 

A = self.echelonized_basis_matrix() 

T = A.matrix_from_columns(A.pivots()) 

self.__echelon_to_rref_matrix = T 

return self.__echelon_to_rref_matrix 

 

def _rref_to_echelon_matrix(self): 

""" 

Returns a transformation matrix from row reduced echelon form to 

some matrix for this module over a PID. 

 

Note: For internal use only! 

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: N = M.submodule_with_basis([[1,1,0],[1,1,2]]) 

sage: N 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1 1 0] 

[1 1 2] 

sage: T = N._echelon_to_rref_matrix(); T 

[1 0] 

[0 2] 

sage: type(T) 

<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'> 

sage: U = N._rref_to_echelon_matrix(); U 

[ 1 0] 

[ 0 1/2] 

sage: type(U) 

<type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'> 

""" 

try: 

return self.__rref_to_echelon_matrix 

except AttributeError: 

self.__rref_to_echelon_matrix = ~self._echelon_to_rref_matrix() 

return self.__rref_to_echelon_matrix 

 

def vector_space(self, base_field=None): 

""" 

Return the vector space associated to this free module via tensor 

product with the fraction field of the base ring. 

 

EXAMPLES:: 

 

sage: A = ZZ^3; A 

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

sage: A.vector_space() 

Vector space of dimension 3 over Rational Field 

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

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1/3 2 3/7] 

[ 4 5 6] 

sage: M.vector_space() 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[1/3 2 3/7] 

[ 4 5 6] 

""" 

if base_field is None: 

K = self.base_ring().fraction_field() 

V = self.ambient_vector_space() 

return V.submodule_with_basis(self.basis()) 

return self.change_ring(base_field) 

 

def ambient_vector_space(self): 

""" 

Return the ambient vector space in which this free module is 

embedded. 

 

EXAMPLES:: 

 

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

Vector space of dimension 3 over Rational Field 

 

:: 

 

sage: N = M.span_of_basis([[1,2,'1/5']]) 

sage: N 

Free module of degree 3 and rank 1 over Integer Ring 

User basis matrix: 

[ 1 2 1/5] 

sage: M.ambient_vector_space() 

Vector space of dimension 3 over Rational Field 

sage: M.ambient_vector_space() is N.ambient_vector_space() 

True 

 

If an inner product on the module is specified, then this 

is preserved on the ambient vector space. 

 

:: 

 

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

sage: V = M.ambient_vector_space() 

sage: V 

Ambient quadratic space of dimension 4 over Rational Field 

Inner product matrix: 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

sage: N = M.submodule([[1,-1,0,0],[0,1,-1,0],[0,0,1,-1]]) 

sage: N.gram_matrix() 

[2 1 1] 

[1 2 1] 

[1 1 2] 

sage: V == N.ambient_vector_space() 

True 

""" 

return self.ambient_module().ambient_vector_space() 

 

def basis(self): 

""" 

Return the user basis for this free module. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

sage: V.basis() 

[ 

(1, 0, 0), 

(0, 1, 0), 

(0, 0, 1) 

] 

sage: M = V.span_of_basis([['1/8',2,1]]) 

sage: M.basis() 

[ 

(1/8, 2, 1) 

] 

""" 

return self.__basis 

 

def change_ring(self, R): 

""" 

Return the free module over `R` obtained by coercing each 

element of the basis of ``self`` into a vector over the 

fraction field of `R`, then taking the resulting `R`-module. 

 

INPUT: 

 

- ``R`` - a principal ideal domain 

 

EXAMPLES:: 

 

sage: V = QQ^3 

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

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: M = (ZZ^2) * (1/2) 

sage: N = M.change_ring(QQ) 

sage: N 

Vector space of degree 2 and dimension 2 over Rational Field 

Basis matrix: 

[1 0] 

[0 1] 

sage: N = M.change_ring(QQ['x']) 

sage: N 

Free module of degree 2 and rank 2 over Univariate Polynomial Ring in x over Rational Field 

Echelon basis matrix: 

[1/2 0] 

[ 0 1/2] 

sage: N.coordinate_ring() 

Univariate Polynomial Ring in x over Rational Field 

 

The ring must be a principal ideal domain:: 

 

sage: M.change_ring(ZZ['x']) 

Traceback (most recent call last): 

... 

TypeError: the new ring Univariate Polynomial Ring in x over Integer Ring should be a principal ideal domain 

""" 

if self.base_ring() is R: 

return self 

if R not in PrincipalIdealDomains(): 

raise TypeError("the new ring %r should be a principal ideal domain" % R) 

 

K = R.fraction_field() 

V = VectorSpace(K, self.degree()) 

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

M = self.ambient_module().change_ring(R) 

if self.has_user_basis(): 

return M.span_of_basis(B) 

else: 

return M.span(B) 

 

def coordinate_vector(self, v, check=True): 

""" 

Write `v` in terms of the user basis for self. 

 

INPUT: 

 

- ``v`` -- vector 

 

- ``check`` -- bool (default: True); if True, also verify that 

`v` is really in self. 

 

OUTPUT: list 

 

Returns a vector `c` such that if `B` is the basis for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

sage: M = V.span_of_basis([['1/8',2,1]]) 

sage: M.coordinate_vector([1,16,8]) 

(8) 

""" 

# First find the coordinates of v wrt echelon basis. 

w = self.echelon_coordinate_vector(v, check=check) 

# Next use transformation matrix from echelon basis to 

# user basis. 

T = self.echelon_to_user_matrix() 

return T.linear_combination_of_rows(w) 

 

def echelonized_basis(self): 

""" 

Return the basis for ``self`` in echelon form. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

sage: M = V.span_of_basis([['1/2',3,1], [0,'1/6',0]]) 

sage: M.basis() 

[ 

(1/2, 3, 1), 

(0, 1/6, 0) 

] 

sage: B = M.echelonized_basis(); B 

[ 

(1/2, 0, 1), 

(0, 1/6, 0) 

] 

sage: V.span(B) == M 

True 

""" 

return self.__echelonized_basis 

 

def echelon_coordinate_vector(self, v, check=True): 

""" 

Write `v` in terms of the echelonized basis for self. 

 

INPUT: 

 

 

- ``v`` - vector 

 

- ``check`` - bool (default: True); if True, also 

verify that v is really in self. 

 

Returns a list `c` such that if `B` is the echelonized basis 

for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

sage: M = V.span_of_basis([['1/2',3,1], [0,'1/6',0]]) 

sage: B = M.echelonized_basis(); B 

[ 

(1/2, 0, 1), 

(0, 1/6, 0) 

] 

sage: M.echelon_coordinate_vector(['1/2', 3, 1]) 

(1, 18) 

""" 

return FreeModule(self.base_ring().fraction_field(), self.rank())(self.echelon_coordinates(v, check=check)) 

 

def has_user_basis(self): 

""" 

Return ``True`` if the basis of this free module is 

specified by the user, as opposed to being the default echelon 

form. 

 

EXAMPLES:: 

 

sage: V = ZZ^3; V.has_user_basis() 

False 

sage: M = V.span_of_basis([[1,3,1]]); M.has_user_basis() 

True 

sage: M = V.span([[1,3,1]]); M.has_user_basis() 

False 

""" 

return True 

 

def linear_combination_of_basis(self, v): 

""" 

Return the linear combination of the basis for ``self`` obtained from 

the coordinates of v. 

 

INPUT: 

 

- ``v`` - list 

 

EXAMPLES:: 

 

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

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 2 3] 

[0 3 6] 

sage: V.linear_combination_of_basis([1,1]) 

(1, 5, 9) 

 

This should raise an error if the resulting element is not in self:: 

 

sage: W = (QQ**2).span([[2, 0], [0, 8]], ZZ) 

sage: W.linear_combination_of_basis([1, -1/2]) 

Traceback (most recent call last): 

... 

TypeError: element [2, -4] is not in free module 

""" 

R = self.base_ring() 

check = (not R.is_field()) and any([a not in R for a in list(v)]) 

return self(self.basis_matrix().linear_combination_of_rows(v), 

check=check, copy=False, coerce=False) 

 

 

class FreeModule_submodule_pid(FreeModule_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] 

 

Generic tests, including saving and loading submodules and elements:: 

 

sage: TestSuite(W).run() 

sage: v = W.0 + W.1 

sage: TestSuite(v).run() 

""" 

def __init__(self, ambient, gens, 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] 

""" 

FreeModule_submodule_with_basis_pid.__init__(self, ambient, basis=gens, 

echelonize=True, already_echelonized=already_echelonized) 

 

def _repr_(self): 

""" 

The printing representation of self. 

 

EXAMPLES:: 

 

sage: M = ZZ^8 

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 

 

def coordinate_vector(self, v, check=True): 

""" 

Write `v` in terms of the user basis for self. 

 

INPUT: 

 

- ``v`` -- vector 

 

- ``check`` -- bool (default: True); if True, also verify that 

`v` is really in self. 

 

OUTPUT: list 

 

Returns a list `c` such that if `B` is the basis for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: V = ZZ^3 

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

sage: W.coordinate_vector([1,5,9]) 

(5, -1) 

""" 

return self.echelon_coordinate_vector(v, check=check) 

 

def has_user_basis(self): 

r""" 

Return ``True`` if the basis of this free module is 

specified by the user, as opposed to being the default echelon 

form. 

 

EXAMPLES:: 

 

sage: A = ZZ^3; A 

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

sage: A.has_user_basis() 

False 

sage: W = A.span_of_basis([[2,'1/2',1]]) 

sage: W.has_user_basis() 

True 

sage: W = A.span([[2,'1/2',1]]) 

sage: W.has_user_basis() 

False 

""" 

return False 

 

 

class FreeModule_submodule_with_basis_field(FreeModule_generic_field, FreeModule_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) 

 

Generic tests, including saving and loading submodules and elements:: 

 

sage: TestSuite(W).run() 

 

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

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

sage: TestSuite(W).run() 

""" 

def __init__(self, ambient, basis, 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] 

""" 

FreeModule_submodule_with_basis_pid.__init__( 

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

echelonized_basis=echelonized_basis, already_echelonized=already_echelonized) 

 

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 _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: VV = VectorSpace(QQ,5,sparse=True) 

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

sage: UU # 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] 

 

(Now clean up again.) 

 

:: 

 

sage: U.reset_name() 

sage: U 

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 vector space of degree %s and dimension %s over %s\n"%( 

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

"User basis matrix:\n%r" % self.basis_matrix() 

else: 

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

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

"User basis matrix:\n%r" % self.basis_matrix() 

 

def _denominator(self, B): 

""" 

Given a list (of field elements) returns 1 as the common 

denominator. 

 

N.B.: This function is for internal use only! 

 

EXAMPLES:: 

 

sage: U = QQ^3 

sage: U 

Vector space of dimension 3 over Rational Field 

sage: U.denominator() 

1 

sage: V = U.span([[1,1/2,1/3], [-1/5,2/3,3]]) 

sage: V 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -5/3] 

[ 0 1 4] 

sage: W = U.submodule_with_basis([[1,1/2,1/3], [-1/5,2/3,3]]) 

sage: W 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[ 1 1/2 1/3] 

[-1/5 2/3 3] 

sage: W._denominator(W.echelonized_basis_matrix().list()) 

1 

""" 

return 1 

 

def _echelonized_basis(self, ambient, basis): 

""" 

Given the ambient space and a basis, construct and cache the 

echelonized basis matrix and returns its rows. 

 

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

 

EXAMPLES:: 

 

sage: M = ZZ^3 

sage: N = M.submodule_with_basis([[1,1,0],[0,2,1]]) 

sage: N._echelonized_basis(M,N.basis()) 

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

sage: V = QQ^3 

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

sage: W._echelonized_basis(V,W.basis()) 

[(1, 0, -1/2), (0, 1, 1/2)] 

""" 

MAT = sage.matrix.matrix_space.MatrixSpace( 

base_ring=ambient.base_ring(), 

nrows=len(basis), ncols=ambient.degree(), 

sparse=ambient.is_sparse()) 

A = MAT(basis) 

E = A.echelon_form() 

# Return the first rank rows (i.e., the nonzero rows). 

return E.rows()[:E.rank()] 

 

def is_ambient(self): 

""" 

Return False since this is not an ambient module. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: V.is_ambient() 

True 

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

sage: W.is_ambient() 

False 

""" 

return False 

 

 

class FreeModule_submodule_field(FreeModule_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, 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] 

""" 

if is_FreeModule(gens): 

gens = gens.gens() 

FreeModule_submodule_with_basis_field.__init__(self, ambient, basis=gens, check=check, 

echelonize=not already_echelonized, already_echelonized=already_echelonized) 

 

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 _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: VV = VectorSpace(QQ,5,sparse=True) 

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

sage: UU # 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] 

 

(Now clean up again.) 

 

:: 

 

sage: U.reset_name() 

sage: U 

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 vector space of degree %s and dimension %s over %s\n"%( 

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

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

else: 

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

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

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

 

def echelon_coordinates(self, v, check=True): 

""" 

Write `v` in terms of the echelonized basis of self. 

 

INPUT: 

 

 

- ``v`` - vector 

 

- ``check`` - bool (default: True); if True, also 

verify that v is really in self. 

 

 

OUTPUT: list 

 

Returns a list `c` such that if `B` is the basis for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

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] 

 

:: 

 

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) 

""" 

if not isinstance(v, free_module_element.FreeModuleElement): 

v = self.ambient_vector_space()(v) 

if v.degree() != self.degree(): 

raise ArithmeticError("v (=%s) is not in self"%v) 

E = self.echelonized_basis_matrix() 

P = E.pivots() 

if len(P) == 0: 

if check and v != 0: 

raise ArithmeticError("vector is not in free module") 

return [] 

w = v.list_from_positions(P) 

if not check: 

# It's really really easy. 

return w 

if v.parent() is self: # obvious that v is really in here. 

return w 

# the "linear_combination_of_rows" call dominates the runtime 

# of this function, in the check==False case when the parent 

# of v is not self. 

lc = E.linear_combination_of_rows(w) 

if lc.list() != v.list(): 

raise ArithmeticError("vector is not in free module") 

return w 

 

def coordinate_vector(self, v, check=True): 

""" 

Write `v` in terms of the user basis for self. 

 

INPUT: 

 

- ``v`` -- vector 

 

- ``check`` -- bool (default: True); if True, also verify that 

`v` is really in self. 

 

OUTPUT: list 

 

Returns a list `c` such that if `B` is the basis for self, then 

 

.. MATH:: 

 

\\sum c_i B_i = v. 

 

If `v` is not in self, raise an ``ArithmeticError`` exception. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

sage: W = V.span([[1,2,3],[4,5,6]]); 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.coordinate_vector(v) 

(1, 5) 

sage: W.coordinates(v) 

[1, 5] 

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

(1, 5, 9) 

 

:: 

 

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

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

sage: W.coordinate_vector([0,0,2,0,-1/2]) 

(0, 2) 

""" 

return self.echelon_coordinate_vector(v, check=check) 

 

def has_user_basis(self): 

""" 

Return ``True`` if the basis of this free module is 

specified by the user, as opposed to being the default echelon 

form. 

 

EXAMPLES:: 

 

sage: V = QQ^3 

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

sage: W.has_user_basis() 

False 

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

sage: W.has_user_basis() 

True 

""" 

return False 

 

def basis_seq(V, vecs): 

""" 

This converts a list vecs of vectors in V to an Sequence of 

immutable vectors. 

 

Should it? I.e. in most ``other`` parts of the system the return type 

of basis or generators is a tuple. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,2) 

sage: B = V.gens() 

sage: B 

((1, 0), (0, 1)) 

sage: v = B[0] 

sage: v[0] = 0 # immutable 

Traceback (most recent call last): 

... 

ValueError: vector is immutable; please change a copy instead (use copy()) 

sage: sage.modules.free_module.basis_seq(V, V.gens()) 

[ 

(1, 0), 

(0, 1) 

] 

""" 

for z in vecs: 

z.set_immutable() 

return Sequence(vecs, universe=V, check = False, immutable=True, cr=True) 

 

 

class RealDoubleVectorSpace_class(FreeModule_ambient_field): 

def __init__(self,n): 

FreeModule_ambient_field.__init__(self,sage.rings.real_double.RDF,n) 

 

def coordinates(self,v): 

return v 

 

class ComplexDoubleVectorSpace_class(FreeModule_ambient_field): 

def __init__(self,n): 

FreeModule_ambient_field.__init__(self,sage.rings.complex_double.CDF,n) 

 

def coordinates(self,v): 

return v 

 

 

 

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

 

def element_class(R, is_sparse): 

""" 

The class of the vectors (elements of a free module) with base ring 

R and boolean is_sparse. 

 

EXAMPLES:: 

 

sage: FF = FiniteField(2) 

sage: P = PolynomialRing(FF,'x') 

sage: sage.modules.free_module.element_class(QQ, is_sparse=True) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

sage: sage.modules.free_module.element_class(QQ, is_sparse=False) 

<type 'sage.modules.vector_rational_dense.Vector_rational_dense'> 

sage: sage.modules.free_module.element_class(ZZ, is_sparse=True) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

sage: sage.modules.free_module.element_class(ZZ, is_sparse=False) 

<type 'sage.modules.vector_integer_dense.Vector_integer_dense'> 

sage: sage.modules.free_module.element_class(FF, is_sparse=True) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

sage: sage.modules.free_module.element_class(FF, is_sparse=False) 

<type 'sage.modules.vector_mod2_dense.Vector_mod2_dense'> 

sage: sage.modules.free_module.element_class(GF(7), is_sparse=False) 

<type 'sage.modules.vector_modn_dense.Vector_modn_dense'> 

sage: sage.modules.free_module.element_class(P, is_sparse=True) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_sparse'> 

sage: sage.modules.free_module.element_class(P, is_sparse=False) 

<type 'sage.modules.free_module_element.FreeModuleElement_generic_dense'> 

""" 

import sage.modules.vector_real_double_dense 

import sage.modules.vector_complex_double_dense 

 

if sage.rings.integer_ring.is_IntegerRing(R) and not is_sparse: 

from .vector_integer_dense import Vector_integer_dense 

return Vector_integer_dense 

elif sage.rings.rational_field.is_RationalField(R) and not is_sparse: 

from .vector_rational_dense import Vector_rational_dense 

return Vector_rational_dense 

elif sage.rings.finite_rings.integer_mod_ring.is_IntegerModRing(R) and not is_sparse: 

from .vector_mod2_dense import Vector_mod2_dense 

if R.order() == 2: 

return Vector_mod2_dense 

from .vector_modn_dense import Vector_modn_dense, MAX_MODULUS 

if R.order() < MAX_MODULUS: 

return Vector_modn_dense 

else: 

return free_module_element.FreeModuleElement_generic_dense 

elif sage.rings.real_double.is_RealDoubleField(R) and not is_sparse: 

return sage.modules.vector_real_double_dense.Vector_real_double_dense 

elif sage.rings.complex_double.is_ComplexDoubleField(R) and not is_sparse: 

return sage.modules.vector_complex_double_dense.Vector_complex_double_dense 

elif sage.symbolic.ring.is_SymbolicExpressionRing(R) and not is_sparse: 

import sage.modules.vector_symbolic_dense 

return sage.modules.vector_symbolic_dense.Vector_symbolic_dense 

elif sage.symbolic.callable.is_CallableSymbolicExpressionRing(R) and not is_sparse: 

import sage.modules.vector_callable_symbolic_dense 

return sage.modules.vector_callable_symbolic_dense.Vector_callable_symbolic_dense 

else: 

if is_sparse: 

return free_module_element.FreeModuleElement_generic_sparse 

else: 

return free_module_element.FreeModuleElement_generic_dense 

raise NotImplementedError 

 

@richcmp_method 

class EchelonMatrixKey(object): 

r""" 

A total ordering on free modules for sorting. 

 

This class orders modules by their ambient spaces, then by dimension, 

then in order by their echelon matrices. If a function returns a list 

of free modules, this can be used to sort the output and thus render 

it deterministic. 

 

INPUT: 

 

- ``obj`` -- a free module 

 

EXAMPLES:: 

 

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

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

sage: X = span([[5,6,7]], ZZ).scale(1/11) 

sage: Y = CC^3 

sage: Z = ZZ^2 

sage: modules = [V,W,X,Y,Z] 

sage: modules_sorted = [Z,X,V,W,Y] 

sage: from sage.modules.free_module import EchelonMatrixKey 

sage: modules.sort(key=EchelonMatrixKey) 

sage: modules == modules_sorted 

True 

""" 

def __init__(self, obj): 

r""" 

Create a container for a free module with a total ordering. 

 

EXAMPLES:: 

 

sage: R.<x> = QQ[] 

sage: V = span(R,[[x,1+x],[x^2,2+x]]) 

sage: W = RR^2 

sage: from sage.modules.free_module import EchelonMatrixKey 

sage: V = EchelonMatrixKey(V) 

sage: W = EchelonMatrixKey(W) 

sage: V < W 

True 

""" 

self.obj = obj 

 

def __richcmp__(self, other, op): 

r""" 

A total ordering on free modules. 

 

TESTS:: 

 

sage: from sage.modules.free_module import EchelonMatrixKey 

sage: Y = EchelonMatrixKey(CC^3) 

sage: Z = EchelonMatrixKey(ZZ^2) 

sage: Z < Y 

True 

""" 

return self.obj._echelon_matrix_richcmp(other.obj, op)