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

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

""" 

Indexed Generators 

""" 

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

# Copyright (C) 2013 Travis Scrimshaw <tcscrims at gmail.com> 

# 

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

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

 

from sage.rings.all import Integer 

from sage.structure.category_object import normalize_names 

 

class IndexedGenerators(object): 

r"""nodetex 

Abstract base class for parents whose elements consist of generators 

indexed by an arbitrary set. 

 

Options controlling the printing of elements: 

 

- ``prefix`` -- string, prefix used for printing elements of this 

module (optional, default 'x'). With the default, a monomial 

indexed by 'a' would be printed as ``x['a']``. 

 

- ``latex_prefix`` -- string or ``None``, prefix used in the `\LaTeX` 

representation of elements (optional, default ``None``). If this is 

anything except the empty string, it prints the index as a 

subscript. If this is None, it uses the setting for ``prefix``, 

so if ``prefix`` is set to "B", then a monomial indexed by 'a' 

would be printed as ``B_{a}``. If this is the empty string, then 

don't print monomials as subscripts: the monomial indexed by 'a' 

would be printed as ``a``, or as ``[a]`` if ``latex_bracket`` is 

True. 

 

- ``bracket`` -- ``None``, bool, string, or list or tuple of 

strings (optional, default ``None``): if ``None``, use the value of the 

attribute ``self._repr_option_bracket``, which has default value 

``True``. (``self._repr_option_bracket`` is available for backwards 

compatibility. Users should set ``bracket`` instead. If 

``bracket`` is set to anything except ``None``, it overrides 

the value of ``self._repr_option_bracket``.) If ``False``, do not 

include brackets when printing elements: a monomial indexed by 

'a' would be printed as ``B'a'``, and a monomial indexed by 

(1,2,3) would be printed as ``B(1,2,3)``. If True, use "[" and 

"]" as brackets. If it is one of "[", "(", or "{", use it and 

its partner as brackets. If it is any other string, use it as 

both brackets. If it is a list or tuple of strings, use the 

first entry as the left bracket and the second entry as the 

right bracket. 

 

- ``latex_bracket`` -- bool, string, or list or tuple of strings 

(optional, default False): if ``False``, do not include brackets in 

the LaTeX representation of elements. This option is only 

relevant if ``latex_prefix`` is the empty string; otherwise, 

brackets are not used regardless. If ``True``, use "\left[" and 

"\right]" as brackets. If this is one of "[", "(", "\\{", "|", 

or "||", use it and its partner, prepended with "\left" and 

"\right", as brackets. If this is any other string, use it as 

both brackets. If this is a list or tuple of strings, use the 

first entry as the left bracket and the second entry as the 

right bracket. 

 

- ``scalar_mult`` -- string to use for scalar multiplication in 

the print representation (optional, default "*") 

 

- ``latex_scalar_mult`` -- string or ``None`` (default: ``None``), 

string to use for scalar multiplication in the latex 

representation. If None, use the empty string if ``scalar_mult`` 

is set to "*", otherwise use the value of ``scalar_mult``. 

 

- ``tensor_symbol`` -- string or ``None`` (default: ``None``), 

string to use for tensor product in the print representation. If 

``None``, use ``sage.categories.tensor.symbol``. 

 

- ``sorting_key`` -- a key function (default: ``lambda x: x``), 

to use for sorting elements in the output of elements 

 

- ``sorting_reverse`` -- bool (default: ``False``), if ``True``  

sort elements in reverse order in the output of elements 

 

- ``string_quotes`` -- bool (default: ``True``), if ``True`` then 

display string indices with quotes 

 

.. NOTE:: 

 

These print options may also be accessed and modified using the 

:meth:`print_options` method, after the parent has been defined. 

 

EXAMPLES: 

 

We demonstrate a variety of the input options:: 

 

sage: from sage.structure.indexed_generators import IndexedGenerators 

sage: I = IndexedGenerators(ZZ, prefix='A') 

sage: I._repr_generator(2) 

'A[2]' 

sage: I._latex_generator(2) 

'A_{2}' 

 

sage: I = IndexedGenerators(ZZ, bracket='(') 

sage: I._repr_generator(2) 

'x(2)' 

sage: I._latex_generator(2) 

'x_{2}' 

 

sage: I = IndexedGenerators(ZZ, prefix="", latex_bracket='(') 

sage: I._repr_generator(2) 

'[2]' 

sage: I._latex_generator(2) 

\left( 2 \right) 

 

sage: I = IndexedGenerators(ZZ, bracket=['|', '>']) 

sage: I._repr_generator(2) 

'x|2>' 

""" 

def __init__(self, indices, prefix="x", **kwds): 

""" 

Initialize ``self``. 

 

EXAMPLES: 

 

This is a mixin class, so don't need pickling equality:: 

 

sage: I = sage.structure.indexed_generators.IndexedGenerators(ZZ) 

sage: TestSuite(I).run(skip='_test_pickling') 

""" 

self._indices = indices 

 

# printing options for elements (set when initializing self). 

# This includes self._repr_option_bracket (kept for backwards 

# compatibility, declared to be True by default, needs to be 

# overridden explicitly). 

self._print_options = {'prefix': prefix, 

'bracket': None, 

'latex_bracket': False, 

'latex_prefix': None, 

'scalar_mult': "*", 

'latex_scalar_mult': None, 

'tensor_symbol': None, 

'string_quotes': True, 

'sorting_key': lambda x: x, 

'sorting_reverse': False} 

# 'bracket': its default value here is None, meaning that 

# the value of self._repr_option_bracket is used; the default 

# value of that attribute is True -- see immediately before 

# the method _repr_generator. If 'bracket' is any value 

# except None, then it overrides the value of 

# self._repr_option_bracket. Future users might consider 

# using 'bracket' instead of _repr_option_bracket. 

self.print_options(**kwds) 

 

def indices(self): 

""" 

Return the indices of ``self``. 

 

EXAMPLES:: 

 

sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 

sage: F.indices() 

{'a', 'b', 'c'} 

""" 

return self._indices 

 

def prefix(self): 

""" 

Return the prefix used when displaying elements of self. 

 

EXAMPLES:: 

 

sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 

sage: F.prefix() 

'B' 

 

:: 

 

sage: X = SchubertPolynomialRing(QQ) 

sage: X.prefix() 

'X' 

""" 

return self._print_options['prefix'] 

 

def print_options(self, **kwds): 

""" 

Return the current print options, or set an option. 

 

INPUT: all of the input is optional; if present, it should be 

in the form of keyword pairs, such as 

``latex_bracket='('``. The allowable keywords are: 

 

- ``prefix`` 

- ``latex_prefix`` 

- ``bracket`` 

- ``latex_bracket`` 

- ``scalar_mult`` 

- ``latex_scalar_mult`` 

- ``tensor_symbol`` 

- ``string_quotes`` 

- ``sorting_key`` 

- ``sorting_reverse`` 

 

See the documentation for :class:`IndexedGenerators` for 

descriptions of the effects of setting each of these options. 

 

OUTPUT: if the user provides any input, set the appropriate 

option(s) and return nothing. Otherwise, return the 

dictionary of settings for print and LaTeX representations. 

 

EXAMPLES:: 

 

sage: F = CombinatorialFreeModule(ZZ, [1,2,3], prefix='x') 

sage: F.print_options() 

{...'prefix': 'x'...} 

sage: F.print_options(bracket='(') 

sage: F.print_options() 

{...'bracket': '('...} 

 

TESTS:: 

 

sage: sorted(F.print_options().items()) 

[('bracket', '('), 

('latex_bracket', False), ('latex_prefix', None), 

('latex_scalar_mult', None), ('prefix', 'x'), 

('scalar_mult', '*'), 

('sorting_key', <function <lambda> at ...>), 

('sorting_reverse', False), ('string_quotes', True), 

('tensor_symbol', None)] 

sage: F.print_options(bracket='[') # reset 

""" 

# don't just use kwds.get(...) because I want to distinguish 

# between an argument like "option=None" and the option not 

# being there altogether. 

if kwds: 

for option in kwds: 

if option in self._print_options: 

self._print_options[option] = kwds[option] 

else: 

raise ValueError('{} is not a valid print option.'.format(option)) 

return 

return self._print_options 

 

_repr_option_bracket = True 

 

def _repr_generator(self, m): 

""" 

Return a string representing the generator indexed by ``m``. 

 

The output can be customized by setting any of the following 

options when initializing the parent: 

 

- ``prefix`` 

- ``bracket`` 

- ``scalar_mult`` 

 

Alternatively, one can use the :meth:`print_options` method 

to achieve the same effect. To modify the bracket setting, 

one can also set ``self._repr_option_bracket`` as long as one 

has *not* set the ``bracket`` option: if the 

``bracket`` option is anything but ``None``, it overrides 

the value of ``self._repr_option_bracket``. 

 

See the documentation for :class:`CombinatorialFreeModule` for 

details on the initialization options. 

 

EXAMPLES:: 

 

sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 

sage: e = F.basis() 

sage: e['a'] + 2*e['b'] # indirect doctest 

B['a'] + 2*B['b'] 

 

sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c'], prefix="F") 

sage: e = F.basis() 

sage: e['a'] + 2*e['b'] # indirect doctest 

F['a'] + 2*F['b'] 

sage: F.print_options(string_quotes=False) 

sage: e['a'] + 2*e['b'] 

F[a] + 2*F[b] 

 

sage: QS3 = CombinatorialFreeModule(QQ, Permutations(3), prefix="") 

sage: original_print_options = QS3.print_options() 

sage: a = 2*QS3([1,2,3])+4*QS3([3,2,1]) 

sage: a # indirect doctest 

2*[[1, 2, 3]] + 4*[[3, 2, 1]] 

 

sage: QS3.print_options(bracket = False) 

sage: a # indirect doctest 

2*[1, 2, 3] + 4*[3, 2, 1] 

 

sage: QS3.print_options(prefix='') 

sage: a # indirect doctest 

2*[1, 2, 3] + 4*[3, 2, 1] 

 

sage: QS3.print_options(bracket="|", scalar_mult=" *@* ") 

sage: a # indirect doctest 

2 *@* |[1, 2, 3]| + 4 *@* |[3, 2, 1]| 

 

sage: QS3.print_options(**original_print_options) # reset 

 

TESTS:: 

 

sage: F = CombinatorialFreeModule(QQ, [('a', 'b'), ('c','d')]) 

sage: e = F.basis() 

sage: e[('a','b')] + 2*e[('c','d')] # indirect doctest 

B[('a', 'b')] + 2*B[('c', 'd')] 

""" 

bracket = self._print_options.get('bracket', None) 

bracket_d = {"{": "}", "[": "]", "(": ")"} 

if bracket is None: 

bracket = self._repr_option_bracket 

if bracket is True: 

left = "[" 

right = "]" 

elif bracket is False: 

left = "" 

right = "" 

elif isinstance(bracket, (tuple, list)): 

left = bracket[0] 

right = bracket[1] 

elif bracket in bracket_d: 

left = bracket 

right = bracket_d[bracket] 

else: 

left = bracket 

right = bracket 

quotes = self._print_options.get('string_quotes', True) 

if not quotes and isinstance(m, str): 

return self.prefix() + left + m + right 

return self.prefix() + left + repr(m) + right # mind the (m), to accept a tuple for m 

 

def _ascii_art_generator(self, m): 

r""" 

Return an ascii art representing the generator indexed by ``m``. 

 

TESTS:: 

 

sage: R = NonCommutativeSymmetricFunctions(QQ).R() 

sage: ascii_art(R[1,2,2,4]) 

R 

**** 

** 

** 

* 

sage: Partitions.options(diagram_str="#", convention="french") 

sage: ascii_art(R[1,2,2,4]) 

R 

# 

## 

## 

#### 

sage: Partitions.options._reset() 

""" 

from sage.typeset.ascii_art import AsciiArt, ascii_art 

pref = AsciiArt([self.prefix()]) 

r = pref * (AsciiArt([" " * len(pref)]) + ascii_art(m)) 

r._baseline = r._h - 1 

return r 

 

def _unicode_art_generator(self, m): 

r""" 

Return an unicode art representing the generator indexed by ``m``. 

 

TESTS:: 

 

sage: R = NonCommutativeSymmetricFunctions(QQ).R() 

sage: unicode_art(R[1,2,2,4]) 

R 

┌┬┬┬┐ 

┌┼┼┴┴┘ 

┌┼┼┘ 

├┼┘ 

└┘ 

sage: Partitions.options.convention="french" 

sage: unicode_art(R[1,2,2,4]) 

R 

┌┐ 

├┼┐ 

└┼┼┐ 

└┼┼┬┬┐ 

└┴┴┴┘ 

sage: Partitions.options._reset() 

""" 

from sage.typeset.unicode_art import UnicodeArt, unicode_art 

pref = UnicodeArt([self.prefix()]) 

r = pref * (UnicodeArt([" " * len(pref)]) + unicode_art(m)) 

r._baseline = r._h - 1 

return r 

 

def _latex_generator(self, m): 

r""" 

Return a `\LaTeX` for the generator indexed by ``m``. 

 

The output can be customized by setting any of the following 

options when initializing the parent: 

 

- ``prefix`` 

- ``latex_prefix`` 

- ``latex_bracket`` 

 

(Alternatively, one can use the :meth:`print_options` method 

to achieve the same effect.) 

 

See the documentation for :class:`CombinatorialFreeModule` for 

details on the initialization options. 

 

EXAMPLES:: 

 

sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 

sage: e = F.basis() 

sage: latex(e['a'] + 2*e['b']) # indirect doctest 

B_{a} + 2B_{b} 

 

sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c'], prefix="C") 

sage: e = F.basis() 

sage: latex(e['a'] + 2*e['b']) # indirect doctest 

C_{a} + 2C_{b} 

 

sage: QS3 = CombinatorialFreeModule(QQ, Permutations(3), prefix="", scalar_mult="*") 

sage: original_print_options = QS3.print_options() 

sage: a = 2*QS3([1,2,3])+4*QS3([3,2,1]) 

sage: latex(a) # indirect doctest 

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

sage: QS3.print_options(latex_bracket=True) 

sage: latex(a) # indirect doctest 

2\left[ [1, 2, 3] \right] + 4\left[ [3, 2, 1] \right] 

sage: QS3.print_options(latex_bracket="(") 

sage: latex(a) # indirect doctest 

2\left( [1, 2, 3] \right) + 4\left( [3, 2, 1] \right) 

sage: QS3.print_options(latex_bracket=('\\myleftbracket', '\\myrightbracket')) 

sage: latex(a) # indirect doctest 

2\myleftbracket [1, 2, 3] \myrightbracket + 4\myleftbracket [3, 2, 1] \myrightbracket 

sage: QS3.print_options(**original_print_options) # reset 

 

TESTS:: 

 

sage: F = CombinatorialFreeModule(QQ, [('a', 'b'), (0,1,2)]) 

sage: e = F.basis() 

sage: latex(e[('a','b')]) # indirect doctest 

B_{('a', 'b')} 

sage: latex(2*e[(0,1,2)]) # indirect doctest 

2B_{\left(0, 1, 2\right)} 

sage: F = CombinatorialFreeModule(QQ, [('a', 'b'), (0,1,2)], prefix="") 

sage: e = F.basis() 

sage: latex(2*e[(0,1,2)]) # indirect doctest 

2\left(0, 1, 2\right) 

""" 

from sage.misc.latex import latex 

 

s = latex(m) 

if s.find('\\text{\\textt') != -1: 

# m contains "non-LaTeXed" strings, use string representation 

s = str(m) 

 

# dictionary with left-right pairs of "brackets". put pairs 

# in here accept \\left and \\right as prefixes. 

bracket_d = {"{": "\\}", "[": "]", "(": ")", "\\{": "\\}", 

"|": "|", "||": "||"} 

bracket = self._print_options.get('latex_bracket', False) 

if bracket is True: 

left = "\\left[" 

right = "\\right]" 

elif bracket is False: 

left = "" 

right = "" 

elif isinstance(bracket, (tuple, list)): 

left = bracket[0] 

right = bracket[1] 

elif bracket in bracket_d: 

left = bracket 

right = bracket_d[bracket] 

if left == "{": 

left = "\\{" 

left = "\\left" + left 

right = "\\right" + right 

else: 

left = bracket 

right = bracket 

prefix = self._print_options.get('latex_prefix') 

if prefix is None: 

prefix = self._print_options.get('prefix') 

if prefix == "": 

return left + s + right 

return "%s_{%s}" % (prefix, s) 

 

def split_index_keywords(kwds): 

""" 

Split the dictionary ``kwds`` into two dictionaries, one containing 

keywords for :class:`IndexedGenerators`, and the other is everything else. 

 

OUTPUT: 

 

The dictionary containing only they keywords 

for :class:`IndexedGenerators`. This modifies the dictionary ``kwds``. 

 

.. WARNING:: 

 

This modifies the input dictionary ``kwds``. 

 

EXAMPLES:: 

 

sage: from sage.structure.indexed_generators import split_index_keywords 

sage: d = {'string_quotes': False, 'bracket': None, 'base': QQ} 

sage: split_index_keywords(d) 

{'bracket': None, 'string_quotes': False} 

sage: d 

{'base': Rational Field} 

""" 

ret = {} 

for option in ['prefix', 'latex_prefix', 'bracket', 'latex_bracket', 

'scalar_mult', 'latex_scalar_mult', 'tensor_symbol', 

'sorting_key', 'sorting_reverse', 

'string_quotes']: 

try: 

ret[option] = kwds.pop(option) 

except KeyError: 

pass 

return ret 

 

def parse_indices_names(names, index_set, prefix, kwds={}): 

""" 

Parse the names, index set, and prefix input, along with setting 

default values for keyword arguments ``kwds``. 

 

OUTPUT: 

 

The triple ``(N, I, p)``: 

 

- ``N`` is the tuple of variable names, 

- ``I`` is the index set, and 

- ``p`` is the prefix. 

 

This modifies the dictionary ``kwds``. 

 

.. NOTE:: 

 

When the indices, names, or prefix have not been given, it 

should be passed to this function as ``None``. 

 

.. NOTE:: 

 

For handling default prefixes, if the result will be ``None`` if 

it is not processed in this function. 

 

EXAMPLES:: 

 

sage: from sage.structure.indexed_generators import parse_indices_names 

sage: d = {} 

sage: parse_indices_names('x,y,z', ZZ, None, d) 

(('x', 'y', 'z'), Integer Ring, None) 

sage: d 

{} 

sage: d = {} 

sage: parse_indices_names('x,y,z', None, None, d) 

(('x', 'y', 'z'), {'x', 'y', 'z'}, '') 

sage: d 

{'bracket': False, 'string_quotes': False} 

sage: d = {} 

sage: parse_indices_names(None, ZZ, None, d) 

(None, Integer Ring, None) 

sage: d 

{} 

 

:: 

 

sage: d = {'string_quotes':True, 'bracket':'['} 

sage: parse_indices_names(['a','b','c'], ZZ, 'x', d) 

(('a', 'b', 'c'), Integer Ring, 'x') 

sage: d 

{'bracket': '[', 'string_quotes': True} 

sage: parse_indices_names('x,y,z', None, 'A', d) 

(('x', 'y', 'z'), {'x', 'y', 'z'}, 'A') 

sage: d 

{'bracket': '[', 'string_quotes': True} 

""" 

if index_set is None: 

if names is None: 

raise ValueError("either the indices or names must be given") 

 

if prefix is None: 

prefix = '' 

kwds.setdefault('string_quotes', False) 

kwds.setdefault('bracket', False) 

 

names, index_set = standardize_names_index_set(names, index_set, -1) 

 

return (names, index_set, prefix) 

 

def standardize_names_index_set(names=None, index_set=None, ngens=None): 

""" 

Standardize the ``names`` and ``index_set`` inputs. 

 

INPUT: 

 

- ``names`` -- (optional) the variable names 

- ``index_set`` -- (optional) the index set 

- ``ngens`` -- (optional) the number of generators 

 

If ``ngens`` is a negative number, then this does not check that 

the number of variable names matches the size of the index set. 

 

OUTPUT: 

 

A pair ``(names_std, index_set_std)``, where ``names_std`` is either 

``None`` or a tuple of strings, and where ``index_set_std`` is a finite 

enumerated set. 

The purpose of ``index_set_std`` is to index the generators of some object 

(e.g., the basis of a module); the strings in ``names_std``, when they 

exist, are used for printing these indices. The ``ngens`` 

 

If ``names`` contains exactly one name ``X`` and ``ngens`` is greater than 

1, then ``names_std`` are ``Xi`` for ``i`` in ``range(ngens)``. 

 

TESTS:: 

 

sage: from sage.structure.indexed_generators import standardize_names_index_set 

sage: standardize_names_index_set('x,y') 

(('x', 'y'), {'x', 'y'}) 

sage: standardize_names_index_set(['x','y']) 

(('x', 'y'), {'x', 'y'}) 

sage: standardize_names_index_set(['x','y'], ['a','b']) 

(('x', 'y'), {'a', 'b'}) 

sage: standardize_names_index_set('x,y', ngens=2) 

(('x', 'y'), {'x', 'y'}) 

sage: standardize_names_index_set(index_set=['a','b'], ngens=2) 

(None, {'a', 'b'}) 

sage: standardize_names_index_set('x', ngens=3) 

(('x0', 'x1', 'x2'), {'x0', 'x1', 'x2'}) 

 

sage: standardize_names_index_set() 

Traceback (most recent call last): 

... 

ValueError: the index_set, names, or number of generators must be specified 

sage: standardize_names_index_set(['x'], ['a', 'b']) 

Traceback (most recent call last): 

... 

IndexError: the number of names must equal the size of the indexing set 

sage: standardize_names_index_set('x,y', ['a']) 

Traceback (most recent call last): 

... 

IndexError: the number of names must equal the size of the indexing set 

sage: standardize_names_index_set('x,y,z', ngens=2) 

Traceback (most recent call last): 

... 

IndexError: the number of names must equal the number of generators 

sage: standardize_names_index_set(index_set=['a'], ngens=2) 

Traceback (most recent call last): 

... 

IndexError: the size of the indexing set must equal the number of generators 

""" 

if names is not None: 

if ngens is None or ngens < 0: 

names = normalize_names(-1, names) 

else: 

names = normalize_names(ngens, names) 

 

if index_set is None: 

if names is None: 

# If neither is specified, we make range(ngens) the index set 

if ngens is None: 

raise ValueError("the index_set, names, or number of" 

" generators must be specified") 

index_set = tuple(range(ngens)) 

else: 

# If only the names are specified, then we make the indexing set 

# be the names 

index_set = tuple(names) 

 

from sage.sets.finite_enumerated_set import FiniteEnumeratedSet 

if isinstance(index_set, dict): # dict of {name: index} -- not likely to be used 

if names is not None: 

raise ValueError("cannot give index_set as a dict and names") 

names = normalize_names(-1, tuple(index_set.keys())) 

index_set = FiniteEnumeratedSet([index_set[n] for n in names]) 

elif isinstance(index_set, str): 

index_set = FiniteEnumeratedSet(list(index_set)) 

elif isinstance(index_set, (tuple, list)): 

index_set = FiniteEnumeratedSet(index_set) 

 

if ngens is None or ngens >= 0: 

if names is not None: 

if len(names) != index_set.cardinality(): 

raise IndexError("the number of names must equal" 

" the size of the indexing set") 

if ngens is not None and len(names) != ngens: 

raise IndexError("the number of names must equal the" 

" number of generators") 

elif ngens is not None and index_set.cardinality() != ngens: 

raise IndexError("the size of the indexing set must equal" 

" the number of generators") 

 

return (names, index_set)