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

r""" 

Point collections 

  

This module was designed as a part of framework for toric varieties 

(:mod:`~sage.schemes.toric.variety`, 

:mod:`~sage.schemes.toric.fano_variety`). 

  

AUTHORS: 

  

- Andrey Novoseltsev (2011-04-25): initial version, based on cone module. 

  

- Andrey Novoseltsev (2012-03-06): additions and doctest changes while 

switching cones to use point collections. 

  

EXAMPLES: 

  

The idea behind :class:`point collections <PointCollection>` is to have a 

container for points of the same space that 

  

* behaves like a tuple *without significant performance penalty*:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c[1] 

N(1, 0, 1) 

sage: for point in c: point 

N(0, 0, 1) 

N(1, 0, 1) 

N(0, 1, 1) 

N(1, 1, 1) 

  

* prints in a convenient way and with clear indication of the ambient space:: 

  

sage: c 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1) 

in 3-d lattice N 

  

* allows (cached) access to alternative representations:: 

  

sage: c.set() 

frozenset({N(0, 0, 1), N(0, 1, 1), N(1, 0, 1), N(1, 1, 1)}) 

  

* allows introduction of additional methods:: 

  

sage: c.basis() 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1) 

in 3-d lattice N 

  

Examples of natural point collections include ray and line generators of cones, 

vertices and points of polytopes, normals to facets, their subcollections, etc. 

  

Using this class for all of the above cases allows for unified interface *and* 

cache sharing. Suppose that `\Delta` is a reflexive polytope. Then the same 

point collection can be linked as 

  

#. vertices of `\Delta`; 

#. facet normals of its polar `\Delta^\circ`; 

#. ray generators of the face fan of `\Delta`; 

#. ray generators of the normal fan of `\Delta`. 

  

If all these objects are in use and, say, a matrix representation was computed 

for one of them, it becomes available to all others as well, eliminating the 

need to spend time and memory four times. 

""" 

  

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

# Copyright (C) 2012 Andrey Novoseltsev <novoselt@gmail.com> 

# 

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

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

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

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

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

from __future__ import print_function 

  

from sage.structure.sage_object cimport SageObject 

from sage.structure.richcmp cimport richcmp_not_equal, richcmp 

  

from sage.geometry.toric_lattice import ToricLattice 

from sage.matrix.all import matrix 

from sage.misc.all import latex 

  

  

def is_PointCollection(x): 

r""" 

Check if ``x`` is a :class:`point collection <PointCollection>`. 

  

INPUT: 

  

- ``x`` -- anything. 

  

OUTPUT: 

  

- ``True`` if ``x`` is a point collection and ``False`` otherwise. 

  

EXAMPLES:: 

  

sage: from sage.geometry.point_collection import is_PointCollection 

sage: is_PointCollection(1) 

False 

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]) 

sage: is_PointCollection(c.rays()) 

True 

""" 

return isinstance(x, PointCollection) 

  

  

_output_format = "default" 

  

  

cdef class PointCollection(SageObject): 

r""" 

Create a point collection. 

  

.. WARNING:: 

  

No correctness check or normalization is performed on the input data. 

This class is designed for internal operations and you probably should 

not use it directly. 

  

Point collections are immutable, but cache most of the returned values. 

  

INPUT: 

  

- ``points`` -- an iterable structure of immutable elements of ``module``, 

if ``points`` are already accessible to you as a :class:`tuple`, it is 

preferable to use it for speed and memory consumption reasons; 

  

- ``module`` -- an ambient module for ``points``. If ``None``, it will be 

determined as :func:`parent` of the first point. Of course, this cannot 

be done if there are no points, so in this case you must give an 

appropriate ``module`` directly. Note that ``None`` is *not* the default 

value - you always *must* give this argument explicitly, even if it is 

``None``. 

  

OUTPUT: 

  

- a point collection. 

""" 

  

cdef: 

tuple _points 

object _module 

# cache attributes 

PointCollection _basis 

object _matrix 

frozenset _set 

  

def __init__(self, points, module=None): 

r""" 

See :class:`PointCollection` for documentation. 

  

TESTS:: 

  

sage: from sage.geometry.point_collection import PointCollection 

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

sage: v.set_immutable() 

sage: c = PointCollection([v], ZZ^2) 

sage: c.module() 

Ambient free module of rank 2 

over the principal ideal domain Integer Ring 

sage: c = PointCollection([v], None) 

sage: c.module() # Determined automatically 

Ambient free module of rank 2 

over the principal ideal domain Integer Ring 

sage: TestSuite(c).run() 

""" 

super(PointCollection, self).__init__() 

self._points = tuple(points) 

self._module = self._points[0].parent() if module is None else module 

  

def __add__(left, right): 

r""" 

Return the joint point collection. 

  

INPUT: 

  

- ``left`` -- a :class:`PointCollection`; 

  

- ``right`` -- a :class:`PointCollection`. 

  

OUTPUT: 

  

- a :class:`PointCollection`. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c + c 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1), 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1) 

in 3-d lattice N 

""" 

if not (isinstance(left, PointCollection) and 

isinstance(right, PointCollection)): 

raise NotImplementedError 

cdef PointCollection left_pc = left 

cdef PointCollection right_pc = right 

if not left_pc._module is right_pc._module: 

raise NotImplementedError 

return PointCollection(left_pc._points + right_pc._points, 

left_pc._module) 

  

def __call__(self, *args): 

r""" 

Return a subcollection of ``self``. 

  

INPUT: 

  

- a list of integers (as a single or many arguments). 

  

OUTPUT: 

  

- a :class:`point collection <PointCollection>`. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c() 

Empty collection 

in 3-d lattice N 

sage: c(2,1) 

N(0, 1, 1), 

N(1, 0, 1) 

in 3-d lattice N 

sage: c(range(4)) == c 

True 

""" 

if len(args) == 1: 

try: 

args = tuple(args[0]) 

except TypeError: 

pass 

# Avoid creating a copy of self 

if len(args) == len(self) and args == tuple(range(len(self))): 

return self 

else: 

return PointCollection([self[i] for i in args], self._module) 

  

def __richcmp__(self, right, op): 

r""" 

Compare ``self`` and ``right`` according to the operator ``op``. 

  

INPUT: 

  

- ``right`` -- another PointCollection 

  

OUTPUT: 

  

boolean 

  

First compare according to the underlying :meth:`module` 

and then according to the list of points. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: d = Cone([(0,1,2), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c == c 

True 

sage: c == d 

False 

""" 

cdef PointCollection left_pc, right_pc 

try: 

left_pc = <PointCollection?>self 

right_pc = <PointCollection?>right 

except TypeError: 

return NotImplemented 

  

left_m = left_pc._module 

right_m = right_pc._module 

if left_m != right_m: 

return richcmp_not_equal(left_m, right_m, op) 

return richcmp(left_pc._points, right_pc._points, op) 

  

def __getitem__(self, n): 

r""" 

Return the ``n``-th point of ``self``. 

  

INPUT: 

  

- ``n`` -- an integer. 

  

OUTPUT: 

  

- a point, an element of the ambient :meth:`module` of ``self``. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c[0] 

N(0, 0, 1) 

""" 

return self._points[n] 

  

def __hash__(self): 

r""" 

Return the hash of ``self``. 

  

OUTPUT: 

  

- an integer. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: hash(c) == hash(c) 

True 

""" 

return hash(self._points) 

  

def __iter__(self): 

r""" 

Return an iterator over points of ``self``. 

  

OUTPUT: 

  

- an iterator. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: for point in c: print(point) 

N(0, 0, 1) 

N(1, 0, 1) 

N(0, 1, 1) 

N(1, 1, 1) 

""" 

return iter(self._points) 

  

def __len__(self): 

r""" 

Return the number of points in ``self``. 

  

OUTPUT: 

  

- an integer. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: len(c) 

4 

""" 

return len(self._points) 

  

def __list__(self): 

r""" 

Return a list of points of ``self``. 

  

OUTPUT: 

  

- a list. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: list(c) 

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

""" 

return list(self._points) 

  

def __mul__(left, right): 

r""" 

Return the product ``left * right``. 

  

INPUT: 

  

- a :class:`point collection <PointCollection>` and something that can 

act both on ``self.module().zero()`` and either ``self.matrix()`` from 

the right or ``self.column_matrix()`` from the left. 

  

OUTPUT: 

  

- the result of ``self.matrix() * right``, provided that 

``self.module().zero() * right`` can be computed. 

  

The idea of this method is to provide a shortcut for matrix 

multiplication with appropriate type checks, in particular, it is not 

possible to multiply by a point of the same toric lattice as elements of 

``self``. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.matrix() 

[0 0 1] 

[1 0 1] 

[0 1 1] 

[1 1 1] 

sage: c * c[0] 

Traceback (most recent call last): 

... 

TypeError: elements of the same toric lattice cannot be multiplied! 

  

If you really need such a product, state it explicitly:: 

  

sage: c.matrix() * c[0] 

(1, 1, 1, 1) 

  

Multiplication by matrices works as well:: 

  

sage: c * c.column_matrix() 

[1 1 1 1] 

[1 2 1 2] 

[1 1 2 2] 

[1 2 2 3] 

""" 

cdef PointCollection pc 

if isinstance(left, PointCollection): 

pc = left 

# Check that it is possible to act on points. 

pc._module.zero() * right 

return pc.matrix() * right 

if isinstance(right, PointCollection): 

pc = right 

# Check that it is possible to act on points. 

left * pc._module.zero() 

return left * pc.column_matrix() 

raise NotImplementedError 

  

def __reduce__(self): 

r""" 

Prepare ``self`` for pickling. 

  

OUTPUT: 

  

- a tuple, currently the class name and a tuple consisting of points 

and the ambient module. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: loads(dumps(c)) 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1) 

in 3-d lattice N 

sage: loads(dumps(c)) == c 

True 

""" 

return (PointCollection, (self._points, self._module)) 

  

def __tuple__(self): 

r""" 

Return the tuple of points of ``self``. 

  

OUTPUT: 

  

- a tuple. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: tuple(c) 

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

""" 

return self._points 

  

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

  

OUTPUT: 

  

- a string. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: print(c._latex_()) 

\left(\left(0,\,0,\,1\right)_{N}, \left(1,\,0,\,1\right)_{N}, 

\left(0,\,1,\,1\right)_{N}, \left(1,\,1,\,1\right)_{N}\right)_{N} 

""" 

global _output_format 

if _output_format in ["default", "tuple"]: 

r = latex(tuple(self)) 

elif _output_format == "matrix": 

r = latex(self.matrix()) 

elif _output_format == "column matrix": 

r = latex(self.column_matrix()) 

elif _output_format == "separated column matrix": 

r = latex(self.column_matrix()) 

r = r.replace("r" * len(self), "|".join("r" * len(self))) 

return r"%s_{%s}" % (r, latex(self.module())) 

  

def _matrix_(self, ring=None): 

r""" 

Return a matrix whose rows are points of ``self``. 

  

INPUT: 

  

- ``ring`` -- a base ring for the returned matrix (default: base ring of 

:meth:`module` of ``self``). 

  

OUTPUT: 

  

- a :class:`matrix <Matrix>`. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: matrix(c) # indirect doctest 

[0 0 1] 

[1 0 1] 

[0 1 1] 

[1 1 1] 

""" 

if ring is None: 

return self.matrix() 

else: 

return self.matrix().change_ring(ring) 

  

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

  

OUTPUT: 

  

- a string. 

  

TESTS:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: print(c._repr_()) 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1) 

in 3-d lattice N 

""" 

global _output_format 

if _output_format == "default": 

r = map(repr, self) 

r = [point.split(",") for point in r] 

if not r: 

r = "Empty collection" 

else: 

if "(" in r[0][0]: 

delimiter = "(" 

elif "[" in r[0][0]: 

delimiter = "[" 

else: 

raise ValueError("cannot parse point representation!") 

heads = [] 

for point in r: 

head, point[0] = point[0].rsplit(delimiter, 1) 

heads.append(head + delimiter) 

format = "{{:<{}}}".format(max(map(len, heads))) 

widths = [0] * len(r[0]) 

for point in r: 

for i, coordinate in enumerate(point): 

widths[i] = max(widths[i], len(coordinate)) 

format += ",".join("{{:>{}}}".format(width) for width in widths) 

r = ",\n".join([format.format(head, *point) 

for head, point in zip(heads, r)]) 

elif _output_format == "tuple": 

r = tuple(self) 

elif _output_format == "matrix": 

r = self.matrix() 

else: 

r = self.column_matrix() 

return "{}\nin {}".format(r, self.module()) 

  

def basis(self): 

r""" 

Return a linearly independent subset of points of ``self``. 

  

OUTPUT: 

  

- a :class:`point collection <PointCollection>` giving a random (but 

fixed) choice of an `\RR`-basis for the vector space spanned by the 

points of ``self``. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.basis() 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1) 

in 3-d lattice N 

  

Calling this method twice will always return *exactly the same* point 

collection:: 

  

sage: c.basis().basis() is c.basis() 

True 

""" 

if self._basis is None: 

self._basis = self(self.matrix().pivot_rows()) 

return self._basis 

  

def cardinality(self): 

r""" 

Return the number of points in ``self``. 

  

OUTPUT: 

  

- an integer. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.cardinality() 

4 

""" 

return len(self._points) 

  

def cartesian_product(self, other, module=None): 

r""" 

Return the Cartesian product of ``self`` with ``other``. 

  

INPUT: 

  

- ``other`` -- a :class:`point collection <PointCollection>`; 

  

- ``module`` -- (optional) the ambient module for the result. By 

default, the direct sum of the ambient modules of ``self`` and 

``other`` is constructed. 

  

OUTPUT: 

  

- a :class:`point collection <PointCollection>`. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,1,1)]).rays() 

sage: c.cartesian_product(c) 

N+N(0, 0, 1, 0, 0, 1), 

N+N(1, 1, 1, 0, 0, 1), 

N+N(0, 0, 1, 1, 1, 1), 

N+N(1, 1, 1, 1, 1, 1) 

in 6-d lattice N+N 

""" 

assert is_PointCollection(other) 

if module is None: 

module = self._module.direct_sum(other.module()) 

P = [list(p) for p in self] 

Q = [list(q) for q in other] 

PQ = [module(p + q) for q in Q for p in P] 

for pq in PQ: 

pq.set_immutable() 

return PointCollection(PQ, module) 

  

def column_matrix(self): 

r""" 

Return a matrix whose columns are points of ``self``. 

  

OUTPUT: 

  

- a :class:`matrix <Matrix>`. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.column_matrix() 

[0 1 0 1] 

[0 0 1 1] 

[1 1 1 1] 

""" 

return self.matrix().transpose() 

  

def dimension(self): 

r""" 

Return the dimension of the space spanned by points of ``self``. 

  

.. NOTE:: You can use either :meth:`dim` or :meth:`dimension`. 

  

OUTPUT: 

  

- an integer. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,1,1)]).rays() 

sage: c.dimension() 

2 

sage: c.dim() 

2 

""" 

return self.matrix().rank() 

  

dim = dimension 

  

def dual_module(self): 

r""" 

Return the dual of the ambient module of ``self``. 

  

OUTPUT: 

  

- a :class:`module <FreeModule_generic>`. If possible (that is, if the 

ambient :meth:`module` `M` of ``self`` has a ``dual()`` method), the 

dual module is returned. Otherwise, `R^n` is returned, where `n` is 

the dimension of `M` and `R` is its base ring. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.dual_module() 

3-d lattice M 

""" 

M = self._module 

try: 

return M.dual() 

except AttributeError: 

# TODO: add support for torsion modules as well? 

return M.base_ring() ** M.dimension() 

  

def index(self, *args): 

r""" 

Return the index of the first occurrence of ``point`` in ``self``. 

  

INPUT: 

  

- ``point`` -- a point of ``self``; 

  

- ``start`` -- (optional) an integer, if given, the search will start 

at this position; 

  

- ``stop`` -- (optional) an integer, if given, the search will stop 

at this position. 

  

OUTPUT: 

  

- an integer if ``point`` is in ``self[start:stop]``, otherwise a 

``ValueError`` exception is raised. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.index((0,1,1)) 

Traceback (most recent call last): 

... 

ValueError: tuple.index(x): x not in tuple 

  

Note that this was not a mistake: the *tuple* ``(0,1,1)`` is *not* a 

point of ``c``! We need to pass actual element of the ambient module of 

``c`` to get their indices:: 

  

sage: N = c.module() 

sage: c.index(N(0,1,1)) 

2 

sage: c[2] 

N(0, 1, 1) 

""" 

return self._points.index(*args) 

  

def matrix(self): 

r""" 

Return a matrix whose rows are points of ``self``. 

  

OUTPUT: 

  

- a :class:`matrix <Matrix>`. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.matrix() 

[0 0 1] 

[1 0 1] 

[0 1 1] 

[1 1 1] 

""" 

if self._matrix is None: 

M = matrix(self._module.base_ring(), len(self._points), 

self._module.degree(), self._points) 

M.set_immutable() 

self._matrix = M 

return self._matrix 

  

def module(self): 

r""" 

Return the ambient module of ``self``. 

  

OUTPUT: 

  

- a :class:`module <FreeModule_generic>`. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.module() 

3-d lattice N 

""" 

return self._module 

  

@staticmethod 

def output_format(format=None): 

r""" 

Return or set the output format for **ALL** point collections. 

  

INPUT: 

  

- ``format`` -- (optional) if given, must be one of the strings 

* "default" -- output one point per line with vertical alignment of 

coordinates in text mode, same as "tuple" for LaTeX; 

* "tuple" -- output ``tuple(self)`` with lattice information; 

* "matrix" -- output :meth:`matrix` with lattice information; 

* "column matrix" -- output :meth:`column_matrix` with lattice 

information; 

* "separated column matrix" -- same as "column matrix" for text 

mode, for LaTeX separate columns by lines (not shown by jsMath). 

  

OUTPUT: 

  

- a string with the current format (only if ``format`` was omitted). 

  

This function affects both regular and LaTeX output. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1) 

in 3-d lattice N 

sage: c.output_format() 

'default' 

sage: c.output_format("tuple") 

sage: c 

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

in 3-d lattice N 

sage: c.output_format("matrix") 

sage: c 

[0 0 1] 

[1 0 1] 

[0 1 1] 

[1 1 1] 

in 3-d lattice N 

sage: c.output_format("column matrix") 

sage: c 

[0 1 0 1] 

[0 0 1 1] 

[1 1 1 1] 

in 3-d lattice N 

sage: c.output_format("separated column matrix") 

sage: c 

[0 1 0 1] 

[0 0 1 1] 

[1 1 1 1] 

in 3-d lattice N 

  

Note that the last two outputs are identical, separators are only 

inserted in the LaTeX mode:: 

  

sage: latex(c) 

\left(\begin{array}{r|r|r|r} 

0 & 1 & 0 & 1 \\ 

0 & 0 & 1 & 1 \\ 

1 & 1 & 1 & 1 

\end{array}\right)_{N} 

  

Since this is a static method, you can call it for the class directly:: 

  

sage: from sage.geometry.point_collection import PointCollection 

sage: PointCollection.output_format("default") 

sage: c 

N(0, 0, 1), 

N(1, 0, 1), 

N(0, 1, 1), 

N(1, 1, 1) 

in 3-d lattice N 

""" 

global _output_format 

if format is None: 

return _output_format 

assert format in ["default", "tuple", "matrix", "column matrix", 

"separated column matrix"] 

_output_format = format 

  

def set(self): 

r""" 

Return points of ``self`` as a :class:`frozenset`. 

  

OUTPUT: 

  

- a :class:`frozenset`. 

  

EXAMPLES:: 

  

sage: c = Cone([(0,0,1), (1,0,1), (0,1,1), (1,1,1)]).rays() 

sage: c.set() 

frozenset({N(0, 0, 1), N(0, 1, 1), N(1, 0, 1), N(1, 1, 1)}) 

""" 

if self._set is None: 

self._set = frozenset(self._points) 

return self._set 

  

def write_for_palp(self, f): 

r""" 

Write ``self`` into an open file ``f`` in PALP format. 

  

INPUT: 

  

- ``f`` -- a file opened for writing. 

  

EXAMPLES:: 

  

sage: o = lattice_polytope.cross_polytope(3) 

sage: from six import StringIO 

sage: f = StringIO() 

sage: o.vertices().write_for_palp(f) 

sage: print(f.getvalue()) 

6 3 

1 0 0 

0 1 0 

0 0 1 

-1 0 0 

0 -1 0 

0 0 -1 

""" 

f.write('{} {}\n'.format(len(self), self._module.rank())) 

f.write('\n'.join(' '.join(str(c) for c in p) for p in self)) 

f.write('\n') 

  

  

def read_palp_point_collection(f, lattice=None, permutation=False): 

r""" 

Read and return a point collection from an opened file. 

  

Data must be in PALP format: 

 

* the first input line starts with two integers `m` and `n`, the number 

of points and the number of components of each; 

 

* the rest of the first line may contain a permutation; 

 

* the next `m` lines contain `n` numbers each. 

 

.. NOTE:: 

 

If `m` < `n`, it is assumed (for compatibility with PALP) that the 

matrix is transposed, i.e. that each column is a point. 

  

INPUT: 

  

- ``f`` -- an opened file with PALP output. 

 

- ``lattice`` -- the lattice for points. If not given, the 

:class:`toric lattice <sage.geometry.toric_lattice.ToricLatticeFactory>` 

`M` of dimension `n` will be used. 

  

- ``permutation`` -- (default: ``False``) if ``True``, try to retrieve 

the permutation. This parameter makes sense only when PALP computed the 

normal form of a lattice polytope. 

  

OUTPUT: 

  

- a :class:`point collection <PointCollection>`, optionally followed by 

a permutation. ``None`` if EOF is reached. 

  

EXAMPLES:: 

  

sage: data = "3 2 regular\n1 2\n3 4\n5 6\n2 3 transposed\n1 2 3\n4 5 6" 

sage: print(data) 

3 2 regular 

1 2 

3 4 

5 6 

2 3 transposed 

1 2 3 

4 5 6 

sage: from six import StringIO 

sage: f = StringIO(data) 

sage: from sage.geometry.point_collection \ 

....: import read_palp_point_collection 

sage: read_palp_point_collection(f) 

M(1, 2), 

M(3, 4), 

M(5, 6) 

in 2-d lattice M 

sage: read_palp_point_collection(f) 

M(1, 4), 

M(2, 5), 

M(3, 6) 

in 2-d lattice M 

sage: read_palp_point_collection(f) is None 

True 

""" 

cdef int i, j, m, n 

first_line = f.readline() 

if first_line == "": 

return None 

first_line = first_line.split() 

m = int(first_line[0]) 

n = int(first_line[1]) 

if m >= n: 

# Typical situation: a point on each line 

lattice = lattice or ToricLattice(n).dual() 

points = [lattice.element_class(lattice, f.readline().split()) 

for i in range(m)] 

else: 

# Also may appear as PALP output, e.g. points of 3-d polytopes 

lattice = lattice or ToricLattice(m).dual() 

data = [f.readline().split() for j in range(m)] 

points = [lattice.element_class(lattice, [data[j][i] for j in range(m)]) 

for i in range(n)] 

for p in points: 

p.set_immutable() 

pc = PointCollection(points, lattice) 

if permutation: 

last_piece = first_line[-1].split('=') 

if last_piece[0] != 'perm': 

raise ValueError('permutation was requested but not found') 

from sage.geometry.lattice_polytope import _palp_convert_permutation 

p = _palp_convert_permutation(last_piece[1]) 

return (pc, p) 

else: 

return pc