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

r""" 

Free String Monoids 

 

AUTHORS: 

 

- David Kohel <kohel@maths.usyd.edu.au>, 2007-01 

 

Sage supports a wide range of specific free string monoids. 

""" 

from __future__ import absolute_import 

 

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

# Copyright (C) 2007 David Kohel <kohel@maths.usyd.edu.au> 

# 

# 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 .free_monoid import FreeMonoid_class 

from .string_monoid_element import StringMonoidElement 

from .string_ops import strip_encoding 

 

import weakref 

 

_cache = {} 

 

def BinaryStrings(): 

r""" 

Returns the free binary string monoid on generators `\{ 0, 1 \}`. 

 

OUTPUT: 

 

- Free binary string monoid. 

 

EXAMPLES:: 

 

sage: S = BinaryStrings(); S 

Free binary string monoid 

sage: u = S('') 

sage: u 

 

sage: x = S('0') 

sage: x 

0 

sage: y = S('1') 

sage: y 

1 

sage: z = S('01110') 

sage: z 

01110 

sage: x*y^3*x == z 

True 

sage: u*x == x*u 

True 

""" 

# Here we cache the binary strings to make them unique 

if 2 in _cache: 

S = _cache[2]() 

if not S is None: 

return S 

S = BinaryStringMonoid() 

_cache[2] = weakref.ref(S) 

return S 

 

def OctalStrings(): 

r""" 

Returns the free octal string monoid on generators `\{ 0, 1, \dots, 7 \}`. 

 

OUTPUT: 

 

- Free octal string monoid. 

 

EXAMPLES:: 

 

sage: S = OctalStrings(); S 

Free octal string monoid 

sage: x = S.gens() 

sage: x[0] 

0 

sage: x[7] 

7 

sage: x[0] * x[3]^3 * x[5]^4 * x[6] 

033355556 

""" 

# Here we cache the octal strings to make them unique 

if 8 in _cache: 

S = _cache[8]() 

if not S is None: 

return S 

S = OctalStringMonoid() 

_cache[8] = weakref.ref(S) 

return S 

 

def HexadecimalStrings(): 

r""" 

Returns the free hexadecimal string monoid on generators 

`\{ 0, 1, \dots , 9, a, b, c, d, e, f \}`. 

 

OUTPUT: 

 

- Free hexadecimal string monoid. 

 

EXAMPLES:: 

 

sage: S = HexadecimalStrings(); S 

Free hexadecimal string monoid 

sage: x = S.gen(0) 

sage: y = S.gen(10) 

sage: z = S.gen(15) 

sage: z 

f 

sage: x*y^3*z 

0aaaf 

""" 

# Here we cache the hexadecimal strings to make them unique 

if 16 in _cache: 

S = _cache[16]() 

if not S is None: 

return S 

S = HexadecimalStringMonoid() 

_cache[16] = weakref.ref(S) 

return S 

 

def Radix64Strings(): 

r""" 

Returns the free radix 64 string monoid on 64 generators 

 

:: 

 

A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z, 

a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, 

0,1,2,3,4,5,6,7,8,9,+,/ 

 

OUTPUT: 

 

- Free radix 64 string monoid. 

 

EXAMPLES:: 

 

sage: S = Radix64Strings(); S 

Free radix 64 string monoid 

sage: x = S.gens() 

sage: x[0] 

A 

sage: x[62] 

+ 

sage: x[63] 

/ 

""" 

# Here we cache the radix-64 strings to make them unique 

if 64 in _cache: 

S = _cache[64]() 

if not S is None: 

return S 

S = Radix64StringMonoid() 

_cache[64] = weakref.ref(S) 

return S 

 

def AlphabeticStrings(): 

r""" 

Returns the string monoid on generators A-Z: 

`\{ A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z \}`. 

 

OUTPUT: 

 

- Free alphabetic string monoid on A-Z. 

 

EXAMPLES:: 

 

sage: S = AlphabeticStrings(); S 

Free alphabetic string monoid on A-Z 

sage: x = S.gens() 

sage: x[0] 

A 

sage: x[25] 

Z 

""" 

# Here we cache the alphabetic strings to make them unique 

if 26 in _cache: 

S = _cache[26]() 

if not S is None: 

return S 

S = AlphabeticStringMonoid() 

_cache[26] = weakref.ref(S) 

return S 

 

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

 

class StringMonoid_class(FreeMonoid_class): 

r""" 

A free string monoid on `n` generators. 

""" 

 

def __init__(self, n, alphabet=()): 

r""" 

Create free binary string monoid on `n` generators. 

 

INPUT: 

 

- ``n`` -- Integer 

 

- ``alphabet`` -- String or tuple whose characters or elements denote 

the generators. 

 

EXAMPLES:: 

 

sage: S = BinaryStrings(); S 

Free binary string monoid 

sage: x = S.gens() 

sage: x[0]*x[1]**5 * (x[0]*x[1]) 

01111101 

""" 

# Names must be alphabetical -- omitted since printing is 

# defined locally. 

# FreeMonoid_class.__init__(self, n, names = alphabet) 

FreeMonoid_class.__init__(self, n) 

self._alphabet = alphabet 

 

def __contains__(self, x): 

return isinstance(x, StringMonoidElement) and x.parent() == self 

 

def alphabet(self): 

return tuple(self._alphabet) 

 

def one(self): 

r""" 

Return the identity element of ``self``. 

 

EXAMPLES:: 

 

sage: b = BinaryStrings(); b 

Free binary string monoid 

sage: b.one() * b('1011') 

1011 

sage: b.one() * b('110') == b('110') 

True 

sage: b('10101') * b.one() == b('101011') 

False 

""" 

return StringMonoidElement(self, '') 

 

def gen(self, i=0): 

r""" 

The `i`-th generator of the monoid. 

 

INPUT: 

 

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

 

EXAMPLES:: 

 

sage: S = BinaryStrings() 

sage: S.gen(0) 

0 

sage: S.gen(1) 

1 

sage: S.gen(2) 

Traceback (most recent call last): 

... 

IndexError: Argument i (= 2) must be between 0 and 1. 

sage: S = HexadecimalStrings() 

sage: S.gen(0) 

0 

sage: S.gen(12) 

c 

sage: S.gen(16) 

Traceback (most recent call last): 

... 

IndexError: Argument i (= 16) must be between 0 and 15. 

""" 

n = self.ngens() 

if i < 0 or not i < n: 

raise IndexError( 

"Argument i (= %s) must be between 0 and %s." % (i, n-1)) 

return StringMonoidElement(self, [int(i)]) 

 

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

# Specific global string monoids 

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

 

class BinaryStringMonoid(StringMonoid_class): 

r""" 

The free binary string monoid on generators `\{ 0, 1 \}`. 

""" 

 

def __init__(self): 

r""" 

Create free binary string monoid on generators `\{ 0, 1 \}`. 

 

EXAMPLES:: 

 

sage: S = BinaryStrings(); S 

Free binary string monoid 

sage: x = S.gens() 

sage: x[0]*x[1]**5 * (x[0]*x[1]) 

01111101 

""" 

StringMonoid_class.__init__(self, 2, ['0', '1']) 

 

def __cmp__(self, other): 

if not isinstance(other, BinaryStringMonoid): 

return -1 

return 0 

 

def __repr__(self): 

return "Free binary string monoid" 

 

def __call__(self, x, check=True): 

r""" 

Return ``x`` coerced into this free monoid. 

 

One can create a free binary string monoid element from a 

Python string of 0's and 1's or list of integers. 

 

NOTE: Due to the ambiguity of the second generator '1' with 

the identity element '' of the monoid, the syntax S(1) is not 

permissible. 

 

EXAMPLES:: 

 

sage: S = BinaryStrings() 

sage: S('101') 

101 

sage: S.gen(0) 

0 

sage: S.gen(1) 

1 

""" 

## There should really some careful type checking here... 

if isinstance(x, StringMonoidElement) and x.parent() == self: 

return x 

elif isinstance(x, list): 

return StringMonoidElement(self, x, check) 

elif isinstance(x, str): 

return StringMonoidElement(self, x, check) 

else: 

raise TypeError("Argument x (= %s) is of the wrong type." % x) 

 

def encoding(self, S, padic=False): 

r""" 

The binary encoding of the string ``S``, as a binary string element. 

 

The default is to keep the standard ASCII byte encoding, e.g. 

 

:: 

 

A = 65 -> 01000001 

B = 66 -> 01000010 

. 

. 

. 

Z = 90 -> 01001110 

 

rather than a 2-adic representation 65 -> 10000010. 

 

Set ``padic=True`` to reverse the bit string. 

 

EXAMPLES:: 

 

sage: S = BinaryStrings() 

sage: S.encoding('A') 

01000001 

sage: S.encoding('A',padic=True) 

10000010 

sage: S.encoding(' ',padic=True) 

00000100 

""" 

bit_string = [] 

for i in range(len(S)): 

n = ord(S[i]) 

bits = [] 

for i in range(8): 

bits.append(n%2) 

n = n >> 1 

if not padic: 

bits.reverse() 

bit_string.extend(bits) 

return self(bit_string) 

 

# def ngens(self): 

# r""" 

# Return the number of generators of this free binary string monoid. 

# There are only 2 elements in the binary number system. Hence, this 

# is the number of generators. 

 

# EXAMPLES:: 

 

# sage: S = BinaryStrings() 

# sage: S.ngens() 

# 2 

# """ 

# return 2 

 

class OctalStringMonoid(StringMonoid_class): 

r""" 

The free octal string monoid on generators `\{ 0, 1, \dots, 7 \}`. 

""" 

 

def __init__(self): 

r""" 

Create free octal string monoid on generators `\{ 0, 1, \dots, 7 \}`. 

 

EXAMPLES:: 

 

sage: S = OctalStrings(); S 

Free octal string monoid 

sage: x = S.gens() 

sage: (x[0]*x[7])**3 * (x[0]*x[1]*x[6]*x[5])**2 

07070701650165 

sage: S([ i for i in range(8) ]) 

01234567 

""" 

StringMonoid_class.__init__(self, 8, [ str(i) for i in range(8) ]) 

 

def __cmp__(self, other): 

if not isinstance(other, OctalStringMonoid): 

return -1 

return 0 

 

def __repr__(self): 

return "Free octal string monoid" 

 

def __call__(self, x, check=True): 

r""" 

Return ``x`` coerced into this free monoid. 

 

One can create a free octal string monoid element from a 

Python string of 0's to 7's or list of integers. 

 

EXAMPLES:: 

 

sage: S = OctalStrings() 

sage: S('07070701650165') 

07070701650165 

sage: S.gen(0) 

0 

sage: S.gen(1) 

1 

sage: S([ i for i in range(8) ]) 

01234567 

""" 

## There should really some careful type checking here... 

if isinstance(x, StringMonoidElement) and x.parent() == self: 

return x 

elif isinstance(x, list): 

return StringMonoidElement(self, x, check) 

elif isinstance(x, str): 

return StringMonoidElement(self, x, check) 

else: 

raise TypeError("Argument x (= %s) is of the wrong type." % x) 

 

class HexadecimalStringMonoid(StringMonoid_class): 

r""" 

The free hexadecimal string monoid on generators 

`\{ 0, 1, \dots, 9, a, b, c, d, e, f \}`. 

""" 

 

def __init__(self): 

r""" 

Create free hexadecimal string monoid on generators 

`\{ 0, 1, \dots, 9, a, b, c, d, e, f \}`. 

 

EXAMPLES:: 

 

sage: S = HexadecimalStrings(); S 

Free hexadecimal string monoid 

sage: x = S.gens() 

sage: (x[0]*x[10])**3 * (x[0]*x[1]*x[9]*x[15])**2 

0a0a0a019f019f 

sage: S([ i for i in range(16) ]) 

0123456789abcdef 

""" 

alph = '0123456789abcdef' 

StringMonoid_class.__init__(self, 16, [ alph[i] for i in range(16) ]) 

 

def __cmp__(self, other): 

if not isinstance(other, HexadecimalStringMonoid): 

return -1 

return 0 

 

def __repr__(self): 

return "Free hexadecimal string monoid" 

 

def __call__(self, x, check=True): 

r""" 

Return ``x`` coerced into this free monoid. 

 

One can create a free hexadecimal string monoid element from a 

Python string of a list of integers in `\{ 0, \dots, 15 \}`. 

 

EXAMPLES:: 

 

sage: S = HexadecimalStrings() 

sage: S('0a0a0a019f019f') 

0a0a0a019f019f 

sage: S.gen(0) 

0 

sage: S.gen(1) 

1 

sage: S([ i for i in range(16) ]) 

0123456789abcdef 

""" 

## There should really some careful type checking here... 

if isinstance(x, StringMonoidElement) and x.parent() == self: 

return x 

elif isinstance(x, list): 

return StringMonoidElement(self, x, check) 

elif isinstance(x, str): 

return StringMonoidElement(self, x, check) 

else: 

raise TypeError("Argument x (= %s) is of the wrong type." % x) 

 

def encoding(self, S, padic=False): 

r""" 

The encoding of the string ``S`` as a hexadecimal string element. 

 

The default is to keep the standard right-to-left byte encoding, e.g. 

 

:: 

 

A = '\x41' -> 41 

B = '\x42' -> 42 

. 

. 

. 

Z = '\x5a' -> 5a 

 

rather than a left-to-right representation A = 65 -> 14. 

Although standard (e.g., in the Python constructor '\xhh'), 

this can be confusing when the string reads left-to-right. 

 

Set ``padic=True`` to reverse the character encoding. 

 

EXAMPLES:: 

 

sage: S = HexadecimalStrings() 

sage: S.encoding('A') 

41 

sage: S.encoding('A',padic=True) 

14 

sage: S.encoding(' ',padic=False) 

20 

sage: S.encoding(' ',padic=True) 

02 

""" 

hex_string = [] 

for i in range(len(S)): 

n = ord(S[i]) 

n0 = n % 16 

n1 = n // 16 

if not padic: 

hex_chars = [n1, n0] 

else: 

hex_chars = [n0, n1] 

hex_string.extend(hex_chars) 

return self(hex_string) 

 

class Radix64StringMonoid(StringMonoid_class): 

r""" 

The free radix 64 string monoid on 64 generators. 

""" 

 

def __init__(self): 

r""" 

Create free radix 64 string monoid on 64 generators. 

 

EXAMPLES:: 

 

sage: S = Radix64Strings(); S 

Free radix 64 string monoid 

sage: x = S.gens() 

sage: (x[50]*x[10])**3 * (x[60]*x[1]*x[19]*x[35])**2 

yKyKyK8BTj8BTj 

sage: S([ i for i in range(64) ]) 

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ 

""" 

alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' 

StringMonoid_class.__init__(self, 64, [ alph[i] for i in range(64) ]) 

 

def __cmp__(self, other): 

if not isinstance(other, Radix64StringMonoid): 

return -1 

return 0 

 

def __repr__(self): 

return "Free radix 64 string monoid" 

 

def __call__(self, x, check=True): 

r""" 

Return ``x`` coerced into this free monoid. 

 

One can create a free radix 64 string monoid element from a 

Python string or a list of integers in `0, \dots, 63`, as for 

generic ``FreeMonoids``. 

 

EXAMPLES:: 

 

sage: S = Radix64Strings() 

sage: S.gen(0) 

A 

sage: S.gen(1) 

B 

sage: S.gen(62) 

+ 

sage: S.gen(63) 

/ 

sage: S([ i for i in range(64) ]) 

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ 

""" 

## There should really some careful type checking here... 

if isinstance(x, StringMonoidElement) and x.parent() == self: 

return x 

elif isinstance(x, list): 

return StringMonoidElement(self, x, check) 

elif isinstance(x, str): 

return StringMonoidElement(self, x, check) 

else: 

raise TypeError("Argument x (= %s) is of the wrong type." % x) 

 

class AlphabeticStringMonoid(StringMonoid_class): 

""" 

The free alphabetic string monoid on generators A-Z. 

 

EXAMPLES:: 

 

sage: S = AlphabeticStrings(); S 

Free alphabetic string monoid on A-Z 

sage: S.gen(0) 

A 

sage: S.gen(25) 

Z 

sage: S([ i for i in range(26) ]) 

ABCDEFGHIJKLMNOPQRSTUVWXYZ 

""" 

 

def __init__(self): 

r""" 

Create free alphabetic string monoid on generators A-Z. 

 

EXAMPLES:: 

 

sage: S = AlphabeticStrings(); S 

Free alphabetic string monoid on A-Z 

sage: S.gen(0) 

A 

sage: S.gen(25) 

Z 

sage: S([ i for i in range(26) ]) 

ABCDEFGHIJKLMNOPQRSTUVWXYZ 

""" 

from sage.rings.all import RealField 

RR = RealField() 

# The characteristic frequency probability distribution of 

# Robert Edward Lewand. 

self._characteristic_frequency_lewand = { 

"A": RR(0.08167), "B": RR(0.01492), 

"C": RR(0.02782), "D": RR(0.04253), 

"E": RR(0.12702), "F": RR(0.02228), 

"G": RR(0.02015), "H": RR(0.06094), 

"I": RR(0.06966), "J": RR(0.00153), 

"K": RR(0.00772), "L": RR(0.04025), 

"M": RR(0.02406), "N": RR(0.06749), 

"O": RR(0.07507), "P": RR(0.01929), 

"Q": RR(0.00095), "R": RR(0.05987), 

"S": RR(0.06327), "T": RR(0.09056), 

"U": RR(0.02758), "V": RR(0.00978), 

"W": RR(0.02360), "X": RR(0.00150), 

"Y": RR(0.01974), "Z": RR(0.00074)} 

# The characteristic frequency probability distribution of 

# H. Beker and F. Piper. 

self._characteristic_frequency_beker_piper = { 

"A": RR(0.082), "B": RR(0.015), 

"C": RR(0.028), "D": RR(0.043), 

"E": RR(0.127), "F": RR(0.022), 

"G": RR(0.020), "H": RR(0.061), 

"I": RR(0.070), "J": RR(0.002), 

"K": RR(0.008), "L": RR(0.040), 

"M": RR(0.024), "N": RR(0.067), 

"O": RR(0.075), "P": RR(0.019), 

"Q": RR(0.001), "R": RR(0.060), 

"S": RR(0.063), "T": RR(0.091), 

"U": RR(0.028), "V": RR(0.010), 

"W": RR(0.023), "X": RR(0.001), 

"Y": RR(0.020), "Z": RR(0.001)} 

alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 

StringMonoid_class.__init__(self, 26, [ alph[i] for i in range(26) ]) 

 

def __cmp__(self, other): 

if not isinstance(other, AlphabeticStringMonoid): 

return -1 

return 0 

 

def __repr__(self): 

return "Free alphabetic string monoid on A-Z" 

 

def __call__(self, x, check=True): 

r""" 

Return ``x`` coerced into this free monoid. 

 

One can create a free alphabetic string monoid element from a 

Python string, or a list of integers in `0, \dots,25`. 

 

EXAMPLES:: 

 

sage: S = AlphabeticStrings() 

sage: S.gen(0) 

A 

sage: S.gen(1) 

B 

sage: S.gen(25) 

Z 

sage: S([ i for i in range(26) ]) 

ABCDEFGHIJKLMNOPQRSTUVWXYZ 

""" 

## There should really some careful type checking here... 

if isinstance(x, StringMonoidElement) and x.parent() == self: 

return x 

elif isinstance(x, list): 

return StringMonoidElement(self, x, check) 

elif isinstance(x, str): 

return StringMonoidElement(self, x, check) 

else: 

raise TypeError("Argument x (= %s) is of the wrong type." % x) 

 

def characteristic_frequency(self, table_name="beker_piper"): 

r""" 

Return a table of the characteristic frequency probability 

distribution of the English alphabet. In written English, various 

letters of the English alphabet occur more frequently than others. 

For example, the letter "E" appears more often than other 

vowels such as "A", "I", "O", and "U". In long works of written 

English such as books, the probability of a letter occurring tends 

to stabilize around a value. We call this value the characteristic 

frequency probability of the letter under consideration. When this 

probability is considered for each letter of the English alphabet, 

the resulting probabilities for all letters of this alphabet is 

referred to as the characteristic frequency probability distribution. 

Various studies report slightly different values for the 

characteristic frequency probability of an English letter. For 

instance, [Lew2000]_ reports that "E" has a characteristic 

frequency probability of 0.12702, while [BP1982]_ reports this 

value as 0.127. The concepts of characteristic frequency probability 

and characteristic frequency probability distribution can also be 

applied to non-empty alphabets other than the English alphabet. 

 

The output of this method is different from that of the method 

:func:`frequency_distribution() 

<sage.monoids.string_monoid_element.StringMonoidElement.frequency_distribution>`. 

One can think of the characteristic frequency probability of an 

element in an alphabet `A` as the expected probability of that element 

occurring. Let `S` be a string encoded using elements of `A`. The 

frequency probability distribution corresponding to `S` provides us 

with the frequency probability of each element of `A` as observed 

occurring in `S`. Thus one distribution provides expected 

probabilities, while the other provides observed probabilities. 

 

INPUT: 

 

- ``table_name`` -- (default ``"beker_piper"``) the table of 

characteristic frequency probability distribution to use. The 

following tables are supported: 

 

- ``"beker_piper"`` -- the table of characteristic frequency 

probability distribution by Beker and Piper [BP1982]_. This is 

the default table to use. 

 

- ``"lewand"`` -- the table of characteristic frequency 

probability distribution by Lewand as described on page 36 

of [Lew2000]_. 

 

OUTPUT: 

 

- A table of the characteristic frequency probability distribution 

of the English alphabet. This is a dictionary of letter/probability 

pairs. 

 

EXAMPLES: 

 

The characteristic frequency probability distribution table of 

Beker and Piper [BP1982]_:: 

 

sage: A = AlphabeticStrings() 

sage: table = A.characteristic_frequency(table_name="beker_piper") 

sage: sorted(table.items()) 

<BLANKLINE> 

[('A', 0.0820000000000000), 

('B', 0.0150000000000000), 

('C', 0.0280000000000000), 

('D', 0.0430000000000000), 

('E', 0.127000000000000), 

('F', 0.0220000000000000), 

('G', 0.0200000000000000), 

('H', 0.0610000000000000), 

('I', 0.0700000000000000), 

('J', 0.00200000000000000), 

('K', 0.00800000000000000), 

('L', 0.0400000000000000), 

('M', 0.0240000000000000), 

('N', 0.0670000000000000), 

('O', 0.0750000000000000), 

('P', 0.0190000000000000), 

('Q', 0.00100000000000000), 

('R', 0.0600000000000000), 

('S', 0.0630000000000000), 

('T', 0.0910000000000000), 

('U', 0.0280000000000000), 

('V', 0.0100000000000000), 

('W', 0.0230000000000000), 

('X', 0.00100000000000000), 

('Y', 0.0200000000000000), 

('Z', 0.00100000000000000)] 

 

The characteristic frequency probability distribution table 

of Lewand [Lew2000]_:: 

 

sage: table = A.characteristic_frequency(table_name="lewand") 

sage: sorted(table.items()) 

<BLANKLINE> 

[('A', 0.0816700000000000), 

('B', 0.0149200000000000), 

('C', 0.0278200000000000), 

('D', 0.0425300000000000), 

('E', 0.127020000000000), 

('F', 0.0222800000000000), 

('G', 0.0201500000000000), 

('H', 0.0609400000000000), 

('I', 0.0696600000000000), 

('J', 0.00153000000000000), 

('K', 0.00772000000000000), 

('L', 0.0402500000000000), 

('M', 0.0240600000000000), 

('N', 0.0674900000000000), 

('O', 0.0750700000000000), 

('P', 0.0192900000000000), 

('Q', 0.000950000000000000), 

('R', 0.0598700000000000), 

('S', 0.0632700000000000), 

('T', 0.0905600000000000), 

('U', 0.0275800000000000), 

('V', 0.00978000000000000), 

('W', 0.0236000000000000), 

('X', 0.00150000000000000), 

('Y', 0.0197400000000000), 

('Z', 0.000740000000000000)] 

 

Illustrating the difference between :func:`characteristic_frequency` 

and :func:`frequency_distribution() <sage.monoids.string_monoid_element.StringMonoidElement.frequency_distribution>`:: 

 

sage: A = AlphabeticStrings() 

sage: M = A.encoding("abcd") 

sage: FD = M.frequency_distribution().function() 

sage: sorted(FD.items()) 

<BLANKLINE> 

[(A, 0.250000000000000), 

(B, 0.250000000000000), 

(C, 0.250000000000000), 

(D, 0.250000000000000)] 

sage: CF = A.characteristic_frequency() 

sage: sorted(CF.items()) 

<BLANKLINE> 

[('A', 0.0820000000000000), 

('B', 0.0150000000000000), 

('C', 0.0280000000000000), 

('D', 0.0430000000000000), 

('E', 0.127000000000000), 

('F', 0.0220000000000000), 

('G', 0.0200000000000000), 

('H', 0.0610000000000000), 

('I', 0.0700000000000000), 

('J', 0.00200000000000000), 

('K', 0.00800000000000000), 

('L', 0.0400000000000000), 

('M', 0.0240000000000000), 

('N', 0.0670000000000000), 

('O', 0.0750000000000000), 

('P', 0.0190000000000000), 

('Q', 0.00100000000000000), 

('R', 0.0600000000000000), 

('S', 0.0630000000000000), 

('T', 0.0910000000000000), 

('U', 0.0280000000000000), 

('V', 0.0100000000000000), 

('W', 0.0230000000000000), 

('X', 0.00100000000000000), 

('Y', 0.0200000000000000), 

('Z', 0.00100000000000000)] 

 

TESTS: 

 

The table name must be either "beker_piper" or "lewand":: 

 

sage: table = A.characteristic_frequency(table_name="") 

Traceback (most recent call last): 

... 

ValueError: Table name must be either 'beker_piper' or 'lewand'. 

sage: table = A.characteristic_frequency(table_name="none") 

Traceback (most recent call last): 

... 

ValueError: Table name must be either 'beker_piper' or 'lewand'. 

""" 

supported_tables = ["beker_piper", "lewand"] 

if table_name not in supported_tables: 

raise ValueError( 

"Table name must be either 'beker_piper' or 'lewand'.") 

from copy import copy 

if table_name == "beker_piper": 

return copy(self._characteristic_frequency_beker_piper) 

if table_name == "lewand": 

return copy(self._characteristic_frequency_lewand) 

 

def encoding(self, S): 

r""" 

The encoding of the string ``S`` in the alphabetic string monoid, 

obtained by the monoid homomorphism 

 

:: 

 

A -> A, ..., Z -> Z, a -> A, ..., z -> Z 

 

and stripping away all other characters. It should be noted that 

this is a non-injective monoid homomorphism. 

 

EXAMPLES:: 

 

sage: S = AlphabeticStrings() 

sage: s = S.encoding("The cat in the hat."); s 

THECATINTHEHAT 

sage: s.decoding() 

'THECATINTHEHAT' 

""" 

return self(strip_encoding(S))