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

r""" 

Tools for generating lists of integers in lexicographic order 

 

IMPORTANT NOTE (2009/02): 

The internal functions in this file will be deprecated soon. 

Please only use them through :class:`IntegerListsLex`. 

 

AUTHORS: 

 

- Mike Hansen 

 

- Travis Scrimshaw (2012-05-12): Fixed errors when returning ``None`` from 

first. Added checks to make sure ``max_slope`` is satisfied. 

 

- Travis Scrimshaw (2012-10-29): Made ``IntegerListsLex`` into a parent with 

the element class ``IntegerListsLexElement``. 

""" 

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

# Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>, 

# Copyright (C) 2012 Travis Scrimshaw <tscrim@ucdavis.edu> 

# 

# 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 absolute_import 

from six.moves import builtins 

 

from sage.arith.all import binomial 

from sage.rings.integer_ring import ZZ 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.structure.parent import Parent 

from sage.structure.list_clone import ClonableArray 

from sage.misc.stopgap import stopgap 

 

 

def first(n, min_length, max_length, floor, ceiling, min_slope, max_slope): 

""" 

Returns the lexicographically smallest valid composition of `n` 

satisfying the conditions. 

 

.. warning:: 

 

INTERNAL FUNCTION! DO NOT USE DIRECTLY! 

 

.. TODO:: 

 

Move this into Cython. 

 

Preconditions: 

 

- ``floor`` and ``ceiling`` need to satisfy the slope constraints, 

e.g. be obtained ``fromcomp2floor`` or ``comp2ceil`` 

 

- ``floor`` must be below ``ceiling`` to ensure 

the existence a valid composition 

 

TESTS:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: f = lambda l: lambda i: l[i-1] 

sage: f([0,1,2,3,4,5])(1) 

0 

sage: integer_list.first(12, 4, 4, f([0,0,0,0]), f([4,4,4,4]), -1, 1) 

[4, 3, 3, 2] 

sage: integer_list.first(36, 9, 9, f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -1, 1) 

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

sage: integer_list.first(25, 9, 9, f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 1) 

[7, 6, 5, 4, 2, 1, 0, 0, 0] 

sage: integer_list.first(36, 9, 9, f([3,3,3,2,1,4,2,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 1) 

[7, 6, 5, 5, 5, 4, 3, 1, 0] 

 

:: 

 

sage: I = integer_list.IntegerListsLex(6, max_slope=2, min_slope=2) 

sage: list(I) 

[[6], [2, 4], [0, 2, 4]] 

""" 

stopgap("First uses the old implementation of IntegerListsLex, which does not allow for arbitrary input;" 

" non-allowed input can return wrong results," 

" please see the documentation for IntegerListsLex for details.", 

17548) 

# Check trivial cases, and standardize min_length to be at least 1 

if n < 0: 

return None 

if max_length <= 0: 

if n == 0: 

return [] 

return None 

if min_length <= 0: 

if n == 0: 

return [] 

min_length = 1 

 

#Increase min_length until n <= sum([ceiling(i) for i in range(min_length)]) 

#This may run forever! 

# Find the actual length the list needs to be 

N = 0 

for i in range(1,min_length+1): 

ceil = ceiling(i) 

if ceil < floor(i): 

return None 

N += ceil 

while N < n: 

min_length += 1 

if min_length > max_length: 

return None 

 

ceil = ceiling(min_length) 

if ceil == 0 and max_slope <= 0 or ceil < floor(min_length): 

return None 

 

N += ceil 

 

# Trivial case 

if min_length == 1: 

if n < floor(1): 

return None 

return [n] 

 

if max_slope < min_slope: 

return None 

 

# Compute the minimum values 

# We are constrained below by the max slope 

result = [floor(min_length)] 

n -= floor(min_length) 

for i in reversed(range(1, min_length)): 

result.insert(0, max(floor(i), result[0] - max_slope)) 

n -= result[0] 

if n < 0: 

return None 

 

if n == 0: # There is nothing more to do 

return result 

 

if min_slope == float('-inf'): 

for i in range(1, min_length+1): 

if n <= ceiling(i) - result[i-1]: #-1 for indexing 

result[i-1] += n 

break 

else: 

n -= ceiling(i) - result[i-1] 

result[i-1] = ceiling(i) 

else: 

low_x = 1 

low_y = result[0] 

high_x = 1 

high_y = result[0] 

 

while n > 0: 

#invariant after each iteration of the loop: 

#[low_x, low_y] is the coordinate of the rightmost point of the 

#current diagonal s.t. result[low_x] < low_y 

low_y += 1 

while low_x < min_length and low_y + min_slope > result[low_x]: 

low_x += 1 

low_y += min_slope 

 

high_y += 1 

while high_y > ceiling(high_x): 

high_x += 1 

high_y += min_slope 

 

n -= low_x - high_x + 1 

 

for j in range(1, high_x): 

result[j-1] = ceiling(j) 

for i in range(0, -n): 

result[high_x+i-1] = high_y + min_slope * i - 1 

for i in range(-n, low_x-high_x+1): 

result[high_x+i-1] = high_y + min_slope * i 

 

# Special check for equal slopes 

if min_slope == max_slope and any(val + min_slope != result[i+1] 

for i,val in enumerate(result[:-1])): 

return None 

 

return result 

 

def lower_regular(comp, min_slope, max_slope): 

""" 

Returns the uppest regular composition below ``comp`` 

 

TESTS:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: integer_list.lower_regular([4,2,6], -1, 1) 

[3, 2, 3] 

sage: integer_list.lower_regular([4,2,6], -1, infinity) 

[3, 2, 6] 

sage: integer_list.lower_regular([1,4,2], -1, 1) 

[1, 2, 2] 

sage: integer_list.lower_regular([4,2,6,3,7], -2, 1) 

[4, 2, 3, 3, 4] 

sage: integer_list.lower_regular([4,2,infinity,3,7], -2, 1) 

[4, 2, 3, 3, 4] 

sage: integer_list.lower_regular([1, infinity, 2], -1, 1) 

[1, 2, 2] 

sage: integer_list.lower_regular([infinity, 4, 2], -1, 1) 

[4, 3, 2] 

""" 

 

new_comp = comp[:] 

for i in range(1, len(new_comp)): 

new_comp[i] = min(new_comp[i], new_comp[i-1] + max_slope) 

 

for i in reversed(range(len(new_comp)-1)): 

new_comp[i] = min( new_comp[i], new_comp[i+1] - min_slope) 

 

return new_comp 

 

def rightmost_pivot(comp, min_length, max_length, floor, ceiling, min_slope, max_slope): 

""" 

TESTS:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: f = lambda l: lambda i: l[i-1] 

sage: integer_list.rightmost_pivot([7,6,5,5,4,3,3,2,1], 9, 9, f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -1, 0) 

[7, 2] 

sage: integer_list.rightmost_pivot([7,6,5,5,4,3,3,2,1], 9, 9,f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 0) 

[7, 1] 

sage: integer_list.rightmost_pivot([7,6,5,5,4,3,3,2,1], 9, 9,f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 4) 

[8, 1] 

sage: integer_list.rightmost_pivot([7,6,5,5,4,3,3,2,1], 9, 9,f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 1) 

[8, 1] 

sage: integer_list.rightmost_pivot([7,6,5,5,5,5,5,4,4], 9, 9,f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 1) 

sage: integer_list.rightmost_pivot([3,3,3,2,1,1,0,0,0], 9, 9,f([3,3,3,2,1,1,0,0,0]), f([7,6,5,5,5,5,5,4,4]), -2, 1) 

sage: g = lambda x: lambda i: x 

sage: integer_list.rightmost_pivot([1],1,1,g(0),g(2),-10, 10) 

sage: integer_list.rightmost_pivot([1,2],2,2,g(0),g(2),-10, 10) 

sage: integer_list.rightmost_pivot([1,2],2,2,g(1),g(2), -10, 10) 

sage: integer_list.rightmost_pivot([1,2],2,3,g(1),g(2), -10, 10) 

[2, 1] 

sage: integer_list.rightmost_pivot([2,2],2,3,g(2),g(2),-10, 10) 

sage: integer_list.rightmost_pivot([2,3],2,3,g(2),g(2),-10,+10) 

sage: integer_list.rightmost_pivot([3,2],2,3,g(2),g(2),-10,+10) 

sage: integer_list.rightmost_pivot([3,3],2,3,g(2),g(2),-10,+10) 

[1, 2] 

sage: integer_list.rightmost_pivot([6],1,3,g(0),g(6),-1,0) 

[1, 0] 

sage: integer_list.rightmost_pivot([6],1,3,g(0),g(6),-2,0) 

[1, 0] 

sage: integer_list.rightmost_pivot([7,9,8,7],1,5,g(0),g(10),-1,10) 

[2, 6] 

sage: integer_list.rightmost_pivot([7,9,8,7],1,5,g(5),g(10),-10,10) 

[3, 5] 

sage: integer_list.rightmost_pivot([7,9,8,7],1,5,g(5),g(10),-1,10) 

[2, 6] 

sage: integer_list.rightmost_pivot([7,9,8,7],1,5,g(4),g(10),-2,10) 

[3, 7] 

sage: integer_list.rightmost_pivot([9,8,7],1,4,g(4),g(10),-2,0) 

[1, 4] 

sage: integer_list.rightmost_pivot([1,3],1,5,lambda i: i,g(10),-10,10) 

sage: integer_list.rightmost_pivot([1,4],1,5,lambda i: i,g(10),-10,10) 

sage: integer_list.rightmost_pivot([2,4],1,5,lambda i: i,g(10),-10,10) 

[1, 1] 

""" 

if max_slope < min_slope: 

return None 

 

x = len(comp) 

if x == 0: 

return None 

 

y = len(comp) + 1 

while y <= max_length: 

if ceiling(y) > 0: 

break 

if max_slope <= 0: 

y = max_length + 1 

break 

y += 1 

 

ceilingx_x = comp[x-1]-1 

floorx_x = floor(x) 

if x > 1: 

floorx_x = max(floorx_x, comp[x-2]+min_slope) 

 

F = comp[x-1] - floorx_x 

G = ceilingx_x - comp[x-1] #this is -1 

 

highX = x 

lowX = x 

 

while not (ceilingx_x >= floorx_x and 

(G >= 0 or 

( y < max_length +1 and 

F - max(floor(y), floorx_x + (y-x)*min_slope) >= 0 and 

G + min(ceiling(y), ceilingx_x + (y-x)*max_slope) >= 0 ))): 

 

if x == 1: 

return None 

 

x -= 1 

 

oldfloorx_x = floorx_x 

ceilingx_x = comp[x-1] - 1 

floorx_x = floor(x) 

if x > 1: 

floorx_x = max(floorx_x, comp[x-2]+min_slope) 

 

min_slope_lowX = min_slope*(lowX - x) 

max_slope_highX = max_slope*(highX - x) 

 

 

#Update G 

if max_slope == float('+inf'): 

#In this case, we have 

# -- ceiling_x(i) = ceiling(i) for i > x 

# --G >= 0 or G = -1 

G += ceiling(x+1)-comp[x] 

else: 

G += (highX - x)*( (comp[x-1]+max_slope) - comp[x]) - 1 

temp = (ceilingx_x + max_slope_highX) - ceiling(highX) 

while highX > x and ( temp >= 0 ): 

G -= temp 

highX -= 1 

max_slope_highX = max_slope*(highX-x) 

temp = (ceilingx_x + max_slope_highX) - ceiling(highX) 

 

if G >= 0 and comp[x-1] > floorx_x: 

#By case 1, x is at the rightmost pivot position 

break 

 

#Update F 

if y < max_length+1: 

F += comp[x-1] - floorx_x 

if min_slope != float('-inf'): 

F += (lowX - x) * (oldfloorx_x - (floorx_x + min_slope)) 

temp = floor(lowX) - (floorx_x + min_slope_lowX) 

while lowX > x and temp >= 0: 

F -= temp 

lowX -= 1 

min_slope_lowX = min_slope*(lowX-x) 

temp = floor(lowX) - (floorx_x + min_slope_lowX) 

 

return [x, floorx_x] 

 

 

def next(comp, min_length, max_length, floor, ceiling, min_slope, max_slope): 

""" 

Returns the next integer list after ``comp`` that satisfies the 

constraints. 

 

.. WARNING:: 

 

INTERNAL FUNCTION! DO NOT USE DIRECTLY! 

 

EXAMPLES:: 

 

sage: from sage.combinat.integer_list_old import next 

sage: IV = sage.combinat.integer_list_old.IntegerListsLex(n=2,length=3,min_slope=0) 

sage: next([0,1,1], 3, 3, lambda i: 0, lambda i: 5, 0, 10) 

[0, 0, 2] 

""" 

stopgap("Next uses the old implementation of IntegerListsLex, which does not allow for arbitrary input;" 

" non-allowed input can return wrong results," 

" please see the documentation for IntegerListsLex for details.", 

17548) 

x = rightmost_pivot( comp, min_length, max_length, floor, ceiling, min_slope, max_slope) 

if x is None: 

return None 

[x, low] = x 

high = comp[x-1]-1 

 

## // Build wrappers around floor and ceiling to take into 

## // account the new constraints on the value of compo[x]. 

## // 

## // Efficiency note: they are not wrapped more than once, since 

## // the method Next calls first, but not the converse. 

 

if min_slope == float('-inf'): 

new_floor = lambda i: floor(x+(i-1)) 

else: 

new_floor = lambda i: max(floor(x+(i-1)), low+(i-1)*min_slope) 

 

if max_slope == float('+inf'): 

new_ceiling = lambda i: comp[x-1] - 1 if i == 1 else ceiling(x+(i-1)) 

else: 

new_ceiling = lambda i: min(ceiling(x+(i-1)), high+(i-1)*max_slope) 

 

res = [] 

res += comp[:x-1] 

f = first(sum(comp[x-1:]), max(min_length-x+1, 0), max_length-x+1, 

new_floor, new_ceiling, min_slope, max_slope) 

if f is None: # Check to make sure it is valid 

return None 

res += f 

return res 

 

 

def iterator(n, min_length, max_length, floor, ceiling, min_slope, max_slope): 

""" 

.. WARNING:: 

 

INTERNAL FUNCTION! DO NOT USE DIRECTLY! 

 

EXAMPLES:: 

 

sage: from sage.combinat.integer_list_old import iterator 

sage: IV = sage.combinat.integer_list_old.IntegerListsLex(n=2,length=3,min_slope=0) 

sage: list(iterator(2, 3, 3, lambda i: 0, lambda i: 5, 0, 10)) 

[[0, 1, 1], [0, 0, 2]] 

""" 

stopgap("Iterator uses the old implementation of IntegerListsLex, which does not allow for arbitrary input;" 

" non-allowed input can return wrong results," 

" please see the documentation for IntegerListsLex for details.", 

17548) 

succ = lambda x: next(x, min_length, max_length, floor, ceiling, min_slope, max_slope) 

 

#Handle the case where n is a list of integers 

if isinstance(n, builtins.list): 

for i in range(n[0], min(n[1]+1,upper_bound(min_length, max_length, floor, ceiling, min_slope, max_slope))): 

for el in iterator(i, min_length, max_length, floor, ceiling, min_slope, max_slope): 

yield el 

else: 

f = first(n, min_length, max_length, floor, ceiling, min_slope, max_slope) 

while not f is None: 

yield f 

f = succ(f) 

 

def upper_regular(comp, min_slope, max_slope): 

""" 

Return the uppest regular composition above ``comp``. 

 

TESTS:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: integer_list.upper_regular([4,2,6],-1,1) 

[4, 5, 6] 

sage: integer_list.upper_regular([4,2,6],-2, 1) 

[4, 5, 6] 

sage: integer_list.upper_regular([4,2,6,3,7],-2, 1) 

[4, 5, 6, 6, 7] 

sage: integer_list.upper_regular([4,2,6,1], -2, 1) 

[4, 5, 6, 4] 

""" 

 

new_comp = comp[:] 

for i in range(1, len(new_comp)): 

new_comp[i] = max(new_comp[i], new_comp[i-1] + min_slope) 

 

for i in reversed(range(len(new_comp)-1)): 

new_comp[i] = max( new_comp[i], new_comp[i+1] - max_slope) 

 

return new_comp 

 

def comp2floor(f, min_slope, max_slope): 

""" 

Given a composition, returns the lowest regular function N->N above 

it. 

 

EXAMPLES:: 

 

sage: from sage.combinat.integer_list_old import comp2floor 

sage: f = comp2floor([2, 1, 1],-1,0) 

sage: [f(i) for i in range(10)] 

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

""" 

if len(f) == 0: return lambda i: 0 

floor = upper_regular(f, min_slope, max_slope) 

return lambda i: floor[i] if i < len(floor) else max(0, floor[-1]-(i-len(floor))*min_slope) 

 

 

def comp2ceil(c, min_slope, max_slope): 

""" 

Given a composition, returns the lowest regular function N->N below 

it. 

 

EXAMPLES:: 

 

sage: from sage.combinat.integer_list_old import comp2ceil 

sage: f = comp2ceil([2, 1, 1],-1,0) 

sage: [f(i) for i in range(10)] 

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

""" 

if len(c) == 0: return lambda i: 0 

ceil = lower_regular(c, min_slope, max_slope) 

return lambda i: ceil[i] if i < len(ceil) else max(0, ceil[-1]-(i-len(ceil))*min_slope) 

 

 

def upper_bound(min_length, max_length, floor, ceiling, min_slope, max_slope): 

""" 

Compute a coarse upper bound on the size of a vector satisfying the 

constraints. 

 

TESTS:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: f = lambda x: lambda i: x 

sage: integer_list.upper_bound(0,4,f(0), f(1),-infinity,infinity) 

4 

sage: integer_list.upper_bound(0, infinity, f(0), f(1), -infinity, infinity) 

inf 

sage: integer_list.upper_bound(0, infinity, f(0), f(1), -infinity, -1) 

1 

sage: integer_list.upper_bound(0, infinity, f(0), f(5), -infinity, -1) 

15 

sage: integer_list.upper_bound(0, infinity, f(0), f(5), -infinity, -2) 

9 

""" 

from sage.functions.all import floor as flr 

if max_length < float('inf'): 

return sum( [ ceiling(j) for j in range(max_length)] ) 

elif max_slope < 0 and ceiling(1) < float('inf'): 

maxl = flr(-ceiling(1)/max_slope) 

return ceiling(1)*(maxl+1) + binomial(maxl+1,2)*max_slope 

#FIXME: only checking the first 10000 values, but that should generally 

#be enough 

elif [ceiling(j) for j in range(10000)] == [0]*10000: 

return 0 

else: 

return float('inf') 

 

 

 

def is_a(comp, min_length, max_length, floor, ceiling, min_slope, max_slope): 

""" 

Returns ``True`` if ``comp`` meets the constraints imposed by the 

arguments. 

 

.. WARNING:: 

 

INTERNAL FUNCTION! DO NOT USE DIRECTLY! 

 

EXAMPLES:: 

 

sage: from sage.combinat.integer_list_old import is_a 

sage: IV = sage.combinat.integer_list_old.IntegerListsLex(n=2,length=3,min_slope=0) 

sage: all(is_a(iv, 3, 3, lambda i: 0, lambda i: 5, 0, 10) for iv in IV) 

True 

""" 

if len(comp) < min_length or len(comp) > max_length: 

return False 

for i in range(len(comp)): 

if comp[i] < floor(i+1): 

return False 

if comp[i] > ceiling(i+1): 

return False 

for i in range(len(comp)-1): 

slope = comp[i+1] - comp[i] 

if slope < min_slope or slope > max_slope: 

return False 

return True 

 

class IntegerListsLexElement(ClonableArray): 

""" 

Element class for :class:`IntegerListsLex`. 

""" 

def check(self): 

""" 

Check to make sure this is a valid element in its 

:class:`IntegerListsLex` parent. 

 

.. TODO:: Placeholder. Implement a proper check. 

 

EXAMPLES:: 

 

sage: C = IntegerListsLex(4) 

sage: C([4]).check() 

True 

""" 

return True 

 

class IntegerListsLex(Parent): 

r""" 

A combinatorial class `C` for integer lists satisfying certain 

sum, length, upper/lower bound and regularity constraints. The 

purpose of this tool is mostly to provide a Constant Amortized 

Time iterator through those lists, in lexicographic order. 

 

INPUT: 

 

- ``n`` -- a non negative integer 

- ``min_length`` -- a non negative integer 

- ``max_length`` -- an integer or `\infty` 

- ``length`` -- an integer; overrides min_length and max_length if 

specified 

- ``min_part`` -- the minimum value of each part; defaults to ``0`` 

- ``max_part`` -- the maximum value of each part; defaults to `+\infty` 

- ``floor`` -- a function `f` (or list); defaults to 

``lambda i: min_part`` 

- ``ceiling`` -- a function `f` (or list); defaults to 

``lambda i: max_part`` 

- ``min_slope`` -- an integer or `-\infty`; defaults to `-\infty` 

- ``max_slope`` -- an integer or `+\infty`; defaults to `+\infty` 

 

An *integer list* is a list `l` of nonnegative integers, its 

*parts*. The *length* of `l` is the number of its parts; 

the *sum* of `l` is the sum of its parts. 

 

.. NOTE:: 

 

Two valid integer lists are considered equivalent if they only 

differ by trailing zeroes. In this case, only the list with the 

least number of trailing zeroes will be produced. 

 

The constraints on the lists are as follow: 

 

- Sum: `sum(l) == n` 

 

- Length: ``min_length <= len(l) <= max_length`` 

 

- Lower and upper bounds: ``floor(i) <= l[i] <= ceiling(i)``, for 

``i`` from 0 to ``len(l)`` 

 

- Regularity condition: ``minSlope <= l[i+1]-l[i] <= maxSlope``, 

for ``i`` from 0 to ``len(l)-1`` 

 

This is a generic low level tool. The interface has been designed 

with efficiency in mind. It is subject to incompatible changes in 

the future. More user friendly interfaces are provided by high 

level tools like :class:`Partitions` or :class:`Compositions`. 

 

EXAMPLES: 

 

We create the combinatorial class of lists of length 3 and sum 2:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: C 

Integer lists of sum 2 satisfying certain constraints 

sage: C.cardinality() 

6 

sage: [p for p in C] 

[[2, 0, 0], [1, 1, 0], [1, 0, 1], [0, 2, 0], [0, 1, 1], [0, 0, 2]] 

 

sage: [2, 0, 0] in C 

True 

sage: [2, 0, 1] in C 

False 

sage: "a" in C 

False 

sage: ["a"] in C 

False 

 

sage: C.first() 

[2, 0, 0] 

 

One can specify lower and upper bound on each part:: 

 

sage: list(integer_list.IntegerListsLex(5, length = 3, floor = [1,2,0], ceiling = [3,2,3])) 

[[3, 2, 0], [2, 2, 1], [1, 2, 2]] 

 

Using the slope condition, one can generate integer partitions 

(but see :mod:`sage.combinat.partition.Partitions`):: 

 

sage: list(integer_list.IntegerListsLex(4, max_slope=0)) 

[[4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]] 

 

This is the list of all partitions of `7` with parts at least `2`:: 

 

sage: list(integer_list.IntegerListsLex(7, max_slope = 0, min_part = 2)) 

[[7], [5, 2], [4, 3], [3, 2, 2]] 

 

This is the list of all partitions of `5` and length at most 3 

which are bounded below by [2,1,1]:: 

 

sage: list(integer_list.IntegerListsLex(5, max_slope = 0, max_length = 3, floor = [2,1,1])) 

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

 

Note that ``[5]`` is considered valid, because the lower bound 

constraint only apply to existing positions in the list. To 

obtain instead the partitions containing ``[2,1,1]``, one need to 

use ``min_length``:: 

 

sage: list(integer_list.IntegerListsLex(5, max_slope = 0, min_length = 3, max_length = 3, floor = [2,1,1])) 

[[3, 1, 1], [2, 2, 1]] 

 

This is the list of all partitions of `5` which are contained in 

``[3,2,2]``:: 

 

sage: list(integer_list.IntegerListsLex(5, max_slope = 0, max_length = 3, ceiling = [3,2,2])) 

[[3, 2], [3, 1, 1], [2, 2, 1]] 

 

This is the list of all compositions of `4` (but see Compositions):: 

 

sage: list(integer_list.IntegerListsLex(4, min_part = 1)) 

[[4], [3, 1], [2, 2], [2, 1, 1], [1, 3], [1, 2, 1], [1, 1, 2], [1, 1, 1, 1]] 

 

This is the list of all integer vectors of sum `4` and length `3`:: 

 

sage: list(integer_list.IntegerListsLex(4, length = 3)) 

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

[2, 0, 2], [1, 3, 0], [1, 2, 1], [1, 1, 2], [1, 0, 3], 

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

 

There are all the lists of sum 4 and length 4 such that l[i] <= i:: 

 

sage: list(integer_list.IntegerListsLex(4, length=4, ceiling=lambda i: i)) 

[[0, 1, 2, 1], [0, 1, 1, 2], [0, 1, 0, 3], [0, 0, 2, 2], [0, 0, 1, 3]] 

 

This is the list of all monomials of degree `4` which divide the 

monomial `x^3y^1z^2` (a monomial being identified with its 

exponent vector):: 

 

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

sage: m = [3,1,2] 

sage: def term(exponents): 

....: return x^exponents[0] * y^exponents[1] * z^exponents[2] 

sage: list( integer_list.IntegerListsLex(4, length = len(m), ceiling = m, element_constructor = term) ) 

[x^3*y, x^3*z, x^2*y*z, x^2*z^2, x*y*z^2] 

 

Note the use of the element_constructor feature. 

 

In general, the complexity of the iteration algorithm is constant 

time amortized for each integer list produced. There is one 

degenerate case though where the algorithm may run forever without 

producing anything. If max_length is `+\infty` and max_slope `>0`, 

testing whether there exists a valid integer list of sum `n` may 

be only semi-decidable. In the following example, the algorithm 

will enter an infinite loop, because it needs to decide whether 

`ceiling(i)` is nonzero for some `i`:: 

 

sage: list( integer_list.IntegerListsLex(1, ceiling = lambda i: 0) ) # todo: not implemented 

 

.. NOTE:: 

 

Caveat: counting is done by brute force generation. In some 

special cases, it would be possible to do better by counting 

techniques for integral point in polytopes. 

 

.. NOTE:: 

 

Caveat: with the current implementation, the constraints should 

satisfy the following conditions: 

 

- The upper and lower bounds themselves should satisfy the 

slope constraints. 

 

- The maximal and minimal part values should not be equal. 

 

Those conditions are not always checked by the algorithm, and the 

result may be completely incorrect if they are not satisfied: 

 

In the following example, the floor conditions do not satisfy the 

slope conditions since the floor for the third part is also 3:: 

 

sage: I = integer_list.IntegerListsLex(16, min_length=2, min_part=3, max_slope=-1, floor=[5,3]) 

Traceback (most recent call last): 

... 

ValueError: floor does not satisfy the max slope condition 

 

Compare this with the following input, which is equivalent 

but it bypasses the checks because the floor is a function:: 

 

sage: f = lambda x: 5 if x == 0 else 3 

sage: I = integer_list.IntegerListsLex(16, min_length=2, max_slope=-1, floor=f) 

sage: list(I) 

[[13, 3], [12, 4], [11, 5], [10, 6]] 

 

With some work, this could be fixed without affecting the overall 

complexity and efficiency. Also, the generation algorithm could be 

extended to deal with non-constant slope constraints and with 

negative parts, as well as to accept a range parameter instead of 

a single integer for the sum `n` of the lists (the later was 

readily implemented in MuPAD-Combinat). Encouragements, 

suggestions, and help are welcome. 

 

.. TODO:: 

 

Integrate all remaining tests from 

http://mupad-combinat.svn.sourceforge.net/viewvc/mupad-combinat/trunk/MuPAD-Combinat/lib/COMBINAT/TEST/MachineIntegerListsLex.tst 

 

TESTS:: 

 

sage: g = lambda x: lambda i: x 

sage: list(integer_list.IntegerListsLex(0, floor = g(1), min_slope = 0)) 

[[]] 

sage: list(integer_list.IntegerListsLex(0, floor = g(1), min_slope = 0, max_slope = 0)) 

[[]] 

sage: list(integer_list.IntegerListsLex(0, max_length=0, floor = g(1), min_slope = 0, max_slope = 0)) 

[[]] 

sage: list(integer_list.IntegerListsLex(0, max_length=0, floor = g(0), min_slope = 0, max_slope = 0)) 

[[]] 

sage: list(integer_list.IntegerListsLex(0, min_part = 1, min_slope = 0)) 

[[]] 

sage: list(integer_list.IntegerListsLex(1, min_part = 1, min_slope = 0)) 

[[1]] 

sage: list(integer_list.IntegerListsLex(0, min_length = 1, min_part = 1, min_slope = 0)) 

[] 

sage: list(integer_list.IntegerListsLex(0, min_length = 1, min_slope = 0)) 

[[0]] 

sage: list(integer_list.IntegerListsLex(3, max_length=2, )) 

[[3], [2, 1], [1, 2], [0, 3]] 

sage: partitions = {"min_part": 1, "max_slope": 0} 

sage: partitions_min_2 = {"floor": g(2), "max_slope": 0} 

sage: compositions = {"min_part": 1} 

sage: integer_vectors = lambda l: {"length": l} 

sage: lower_monomials = lambda c: {"length": c, "floor": lambda i: c[i]} 

sage: upper_monomials = lambda c: {"length": c, "ceiling": lambda i: c[i]} 

sage: constraints = { "min_part":1, "min_slope": -1, "max_slope": 0} 

sage: list(integer_list.IntegerListsLex(6, **partitions)) 

[[6], 

[5, 1], 

[4, 2], 

[4, 1, 1], 

[3, 3], 

[3, 2, 1], 

[3, 1, 1, 1], 

[2, 2, 2], 

[2, 2, 1, 1], 

[2, 1, 1, 1, 1], 

[1, 1, 1, 1, 1, 1]] 

sage: list(integer_list.IntegerListsLex(6, **constraints)) 

[[6], 

[3, 3], 

[3, 2, 1], 

[2, 2, 2], 

[2, 2, 1, 1], 

[2, 1, 1, 1, 1], 

[1, 1, 1, 1, 1, 1]] 

sage: list(integer_list.IntegerListsLex(1, **partitions_min_2)) 

[] 

sage: list(integer_list.IntegerListsLex(2, **partitions_min_2)) 

[[2]] 

sage: list(integer_list.IntegerListsLex(3, **partitions_min_2)) 

[[3]] 

sage: list(integer_list.IntegerListsLex(4, **partitions_min_2)) 

[[4], [2, 2]] 

sage: list(integer_list.IntegerListsLex(5, **partitions_min_2)) 

[[5], [3, 2]] 

sage: list(integer_list.IntegerListsLex(6, **partitions_min_2)) 

[[6], [4, 2], [3, 3], [2, 2, 2]] 

sage: list(integer_list.IntegerListsLex(7, **partitions_min_2)) 

[[7], [5, 2], [4, 3], [3, 2, 2]] 

sage: list(integer_list.IntegerListsLex(9, **partitions_min_2)) 

[[9], [7, 2], [6, 3], [5, 4], [5, 2, 2], [4, 3, 2], [3, 3, 3], [3, 2, 2, 2]] 

sage: list(integer_list.IntegerListsLex(10, **partitions_min_2)) 

[[10], 

[8, 2], 

[7, 3], 

[6, 4], 

[6, 2, 2], 

[5, 5], 

[5, 3, 2], 

[4, 4, 2], 

[4, 3, 3], 

[4, 2, 2, 2], 

[3, 3, 2, 2], 

[2, 2, 2, 2, 2]] 

sage: list(integer_list.IntegerListsLex(4, **compositions)) 

[[4], [3, 1], [2, 2], [2, 1, 1], [1, 3], [1, 2, 1], [1, 1, 2], [1, 1, 1, 1]] 

sage: list(integer_list.IntegerListsLex(6, min_length=1, floor=[7])) 

[] 

 

Noted on :trac:`17898`:: 

 

sage: list(integer_list.IntegerListsLex(4, min_part=1, length=3, min_slope=1)) 

[] 

sage: integer_list.IntegerListsLex(6, ceiling=[4,2], floor=[3,3]).list() 

[] 

sage: integer_list.IntegerListsLex(6, min_part=1, max_part=3, max_slope=-4).list() 

[] 

""" 

def __init__(self, 

n, 

length = None, min_length=0, max_length=float('+inf'), 

floor=None, ceiling = None, 

min_part = 0, max_part = float('+inf'), 

min_slope=float('-inf'), max_slope=float('+inf'), 

name = None, 

element_constructor = None, 

element_class = None, 

global_options = None): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: C == loads(dumps(C)) 

True 

sage: C == loads(dumps(C)) # this did fail at some point, really! 

True 

sage: C is loads(dumps(C)) # todo: not implemented 

True 

sage: C.cardinality().parent() is ZZ 

True 

sage: TestSuite(C).run() 

""" 

stopgap("The old implementation of IntegerListsLex does not allow for arbitrary input;" 

" non-allowed input can return wrong results," 

" please see the documentation for IntegerListsLex for details.", 

17548) 

# Convert to float infinity 

from sage.rings.infinity import infinity 

if max_slope == infinity: 

max_slope = float('+inf') 

if min_slope == -infinity: 

min_slope = float('-inf') 

if max_length == infinity: 

max_length = float('inf') 

if max_part == infinity: 

max_part = float('+inf') 

 

if floor is None: 

self.floor_list = [] 

else: 

try: 

# Is ``floor`` an iterable? 

# Not ``floor[:]`` because we want ``self.floor_list`` 

# mutable, and applying [:] to a tuple gives a tuple. 

self.floor_list = builtins.list(floor) 

# Make sure the floor list will make the list satisfy the constraints 

if min_slope != float('-inf'): 

for i in range(1, len(self.floor_list)): 

self.floor_list[i] = max(self.floor_list[i], self.floor_list[i-1] + min_slope) 

 

# Some input checking 

for i in range(1, len(self.floor_list)): 

if self.floor_list[i] - self.floor_list[i-1] > max_slope: 

raise ValueError("floor does not satisfy the max slope condition") 

if self.floor_list and min_part - self.floor_list[-1] > max_slope: 

raise ValueError("floor does not satisfy the max slope condition") 

except TypeError: 

self.floor = floor 

if ceiling is None: 

self.ceiling_list = [] 

else: 

try: 

# Is ``ceiling`` an iterable? 

self.ceiling_list = builtins.list(ceiling) 

# Make sure the ceiling list will make the list satisfy the constraints 

if max_slope != float('+inf'): 

for i in range(1, len(self.ceiling_list)): 

self.ceiling_list[i] = min(self.ceiling_list[i], self.ceiling_list[i-1] + max_slope) 

 

# Some input checking 

for i in range(1, len(self.ceiling_list)): 

if self.ceiling_list[i] - self.ceiling_list[i-1] < min_slope: 

raise ValueError("ceiling does not satisfy the min slope condition") 

if self.ceiling_list and max_part - self.ceiling_list[-1] < min_slope: 

raise ValueError("ceiling does not satisfy the min slope condition") 

except TypeError: 

# ``ceiling`` is not an iterable. 

self.ceiling = ceiling 

if name is not None: 

self.rename(name) 

if n in ZZ: 

self.n = n 

self.n_range = [n] 

else: 

self.n_range = n 

if length is not None: 

min_length = length 

max_length = length 

self.min_length = min_length 

self.max_length = max_length 

self.min_part = min_part 

self.max_part = max_part 

# FIXME: the internal functions currently assume that floor and ceiling start at 1 

# this is a workaround 

self.max_slope = max_slope 

self.min_slope = min_slope 

if element_constructor is not None: 

self._element_constructor_ = element_constructor 

if element_class is not None: 

self.Element = element_class 

if global_options is not None: 

self.global_options = global_options 

Parent.__init__(self, category=FiniteEnumeratedSets()) 

 

Element = IntegerListsLexElement 

 

def _element_constructor_(self, lst): 

""" 

Construct an element with ``self`` as parent. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(4) 

sage: C([4]) 

[4] 

""" 

return self.element_class(self, lst) 

 

def __eq__(self, x): 

""" 

Compares two different :class:`IntegerListsLex`. 

 

For now, the comparison is done just on their repr's which is 

not robust! 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: D = integer_list.IntegerListsLex(4, length=3) 

sage: repr(C) == repr(D) 

False 

sage: C == D 

False 

""" 

return repr(self) == repr(x) 

 

def __ne__(self, other): 

""" 

Compares two different :class:`IntegerListsLex`. 

 

For now, the comparison is done just on their repr's which is 

not robust! 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: D = integer_list.IntegerListsLex(4, length=3) 

sage: C != D 

True 

""" 

return not self.__eq__(other) 

 

def _repr_(self): 

""" 

Returns the name of this combinatorial class. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: C # indirect doctest 

Integer lists of sum 2 satisfying certain constraints 

 

sage: C = integer_list.IntegerListsLex([1,2,4], length=3) 

sage: C # indirect doctest 

Integer lists of sum in [1, 2, 4] satisfying certain constraints 

 

sage: C = integer_list.IntegerListsLex([1,2,4], length=3, name="A given name") 

sage: C 

A given name 

""" 

if hasattr(self, "n"): 

return "Integer lists of sum %s satisfying certain constraints"%self.n 

 

return "Integer lists of sum in %s satisfying certain constraints"%self.n_range 

 

def floor(self, i): 

""" 

Returns the minimum part that can appear at the `i^{th}` position of 

any list produced. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(4, length=2, min_part=1) 

sage: C.floor(0) 

1 

sage: C = integer_list.IntegerListsLex(4, length=2, floor=[1,2]) 

sage: C.floor(0) 

1 

sage: C.floor(1) 

2 

""" 

if i < len(self.floor_list): 

return max(self.min_part, self.floor_list[i]) 

if self.min_slope != float('-inf') and self.min_slope > 0: 

return self.min_part + (i - len(self.floor_list)) * self.min_slope 

return self.min_part 

 

def ceiling(self, i): 

""" 

Returns the maximum part that can appear in the `i^{th}` 

position in any list produced. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(4, length=2, max_part=3) 

sage: C.ceiling(0) 

3 

sage: C = integer_list.IntegerListsLex(4, length=2, ceiling=[3,2]) 

sage: C.ceiling(0) 

3 

sage: C.ceiling(1) 

2 

""" 

if i < len(self.ceiling_list): 

return min(self.max_part, self.ceiling_list[i]) 

if self.max_slope != float('inf') and self.max_slope < 0: 

return self.max_part + (i - len(self.ceiling_list)) * self.max_slope 

return self.max_part 

 

# Temporary adapter to use the preexisting list/iterator/is_a function above. 

# FIXME: fix their specs so that floor and ceiling start from 0 instead of 1... 

# FIXME: integrate them as methods of this class 

def build_args(self): 

""" 

Returns a list of arguments that can be passed into the pre-existing 

``first``, ``next``, ``is_a``, ... functions in this module. 

 

``n`` is currently not included in this list. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: C.build_args() 

[3, 

3, 

<function <lambda> at 0x...>, 

<function <lambda> at 0x...>, 

-inf, 

inf] 

 

""" 

return [self.min_length, self.max_length, 

lambda i: self.floor(i-1), lambda i: self.ceiling(i-1), 

self.min_slope, self.max_slope] 

 

def first(self): 

""" 

Returns the lexicographically maximal element in ``self``. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: C.first() 

[2, 0, 0] 

""" 

# Make sure we have a valid return 

f = first(self.n_range[0], *(self.build_args())) 

if f is None: 

return None 

return self._element_constructor_(f) 

 

def __iter__(self): 

""" 

Returns an iterator for the elements of ``self``. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: list(C) #indirect doctest 

[[2, 0, 0], [1, 1, 0], [1, 0, 1], [0, 2, 0], [0, 1, 1], [0, 0, 2]] 

""" 

args = self.build_args() 

for n in self.n_range: 

l = first(n, *args) 

while l is not None: 

yield self._element_constructor_(l) 

l = next(l, *args) 

 

def count(self): 

""" 

Default brute force implementation of count by iteration 

through all the objects. 

 

Note that this skips the call to ``_element_constructor_``, 

unlike the default implementation. 

 

.. TODO:: 

 

Do the iteration in place to save on copying time 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: C.cardinality() == C.count() 

True 

""" 

args = self.build_args() 

c = ZZ(0) 

for n in self.n_range: 

l = first(n, *args) 

while l is not None: 

c += 1 

l = next(l, *args) 

return c 

 

def __contains__(self, v): 

""" 

Returns ``True`` if and only if ``v`` is in ``self``. 

 

EXAMPLES:: 

 

sage: import sage.combinat.integer_list_old as integer_list 

sage: C = integer_list.IntegerListsLex(2, length=3) 

sage: [2, 0, 0] in C 

True 

sage: [2, 0] in C 

False 

sage: [3, 0, 0] in C 

False 

sage: all(v in C for v in C) 

True 

""" 

if isinstance(v, self.element_class) or isinstance(v, builtins.list): 

return is_a(v, *(self.build_args())) and sum(v) in self.n_range 

return False