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

""" 

Data structures for maps between finite sets 

  

This module implements several fast Cython data structures for maps 

between two finite set. Those classes are not intended to be used 

directly. Instead, such a map should be constructed via its parent, 

using the class :class:`~sage.sets.finite_set_maps.FiniteSetMaps`. 

  

EXAMPLES: 

  

To create a map between two sets, one first creates the set of such maps:: 

  

sage: M = FiniteSetMaps(["a", "b"], [3, 4, 5]) 

  

The map can then be constructed either from a function:: 

  

sage: f1 = M(lambda c: ord(c)-94); f1 

map: a -> 3, b -> 4 

  

or from a dictionary:: 

  

sage: f2 = M.from_dict({'a':3, 'b':4}); f2 

map: a -> 3, b -> 4 

  

The two created maps are equal:: 

  

sage: f1 == f2 

True 

  

Internally, maps are represented as the list of the ranks of the 

images ``f(x)`` in the co-domain, in the order of the domain:: 

  

sage: list(f2) 

[0, 1] 

  

A third fast way to create a map it to use such a list. it should be 

kept for internal use:: 

  

sage: f3 = M._from_list_([0, 1]); f3 

map: a -> 3, b -> 4 

sage: f1 == f3 

True 

  

AUTHORS: 

  

- Florent Hivert 

""" 

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

# Copyright (C) 2010 Florent Hivert <Florent.Hivert@univ-rouen.fr>, 

# 

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

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

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

from __future__ import absolute_import 

  

import sage 

from sage.structure.list_clone cimport ClonableIntArray 

from sage.structure.parent cimport Parent 

from sage.arith.power cimport generic_power 

from sage.sets.set import Set_object_enumerated 

  

  

cpdef fibers(f, domain): 

r""" 

Returns the fibers of the function ``f`` on the finite set ``domain`` 

  

INPUT: 

  

- ``f`` -- a function or callable 

- ``domain`` -- a finite iterable 

  

OUTPUT: 

  

- a dictionary ``d`` such that ``d[y]`` is the set of all ``x`` in 

``domain`` such that ``f(x) = y`` 

  

EXAMPLES:: 

  

sage: from sage.sets.finite_set_map_cy import fibers, fibers_args 

sage: fibers(lambda x: 1, []) 

{} 

sage: fibers(lambda x: x^2, [-1, 2, -3, 1, 3, 4]) 

{1: {1, -1}, 4: {2}, 9: {3, -3}, 16: {4}} 

sage: fibers(lambda x: 1, [-1, 2, -3, 1, 3, 4]) 

{1: {1, 2, 3, 4, -3, -1}} 

sage: fibers(lambda x: 1, [1,1,1]) 

{1: {1}} 

  

.. SEEALSO:: :func:`fibers_args` if one needs to pass extra 

arguments to ``f``. 

""" 

result = {} 

for x in domain: 

y = f(x) 

result.setdefault(y, set()).add(x) 

for x, v in result.iteritems(): 

result[x] = Set_object_enumerated(v) 

return result 

  

  

def fibers_args(f, domain, *args, **opts): 

r""" 

Returns the fibers of the function ``f`` on the finite set ``domain`` 

  

It is the same as :func:`fibers` except that one can pass extra 

argument for ``f`` (with a small overhead) 

  

EXAMPLES:: 

  

sage: from sage.sets.finite_set_map_cy import fibers_args 

sage: fibers_args(operator.pow, [-1, 2, -3, 1, 3, 4], 2) 

{1: {1, -1}, 4: {2}, 9: {3, -3}, 16: {4}} 

""" 

return fibers(lambda x: f(x, *args, **opts), domain) 

  

  

cdef class FiniteSetMap_MN(ClonableIntArray): 

""" 

Data structure for maps from ``range(m)`` to ``range(n)``. 

  

We assume that the parent given as argument is such that: 

  

- ``m`` is stored in ``self.parent()._m`` 

- ``n`` is stored in ``self.parent()._n`` 

- the domain is in ``self.parent().domain()`` 

- the codomain is in ``self.parent().codomain()`` 

""" 

  

def __call__(self, int i): 

""" 

Returns the image of ``i`` under the map ``self`` 

  

INPUT: 

  

- ``i`` -- an int 

  

OUTPUT: 

  

an int 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(4, 3)([1, 0, 2, 1]) 

sage: fs(0), fs(1), fs(2), fs(3) 

(1, 0, 2, 1) 

""" 

return self._getitem(i) 

  

# Needed by generic power which refuses to compute 0^0 

def __nonzero__(self): 

""" 

Returns whether ``self`` is non zero; this is always ``True``. 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(4, 3)([1, 0, 2, 1]) 

sage: bool(fs) 

True 

""" 

return True 

  

cpdef domain(self): 

""" 

Returns the domain of ``self`` 

  

EXAMPLES:: 

  

sage: FiniteSetMaps(4, 3)([1, 0, 2, 1]).domain() 

{0, 1, 2, 3} 

""" 

return self._parent.domain() 

  

cpdef codomain(self): 

""" 

Returns the codomain of ``self`` 

  

EXAMPLES:: 

  

sage: FiniteSetMaps(4, 3)([1, 0, 2, 1]).codomain() 

{0, 1, 2} 

""" 

return self._parent.codomain() 

  

cpdef _setimage(self, int i, int j): 

""" 

Set the image of ``i`` as ``j`` in ``self`` 

  

This is a fast internal version where ``i`` and ``j`` are both int's. 

  

.. warning:: ``self`` must be mutable; otherwise an exception is raised. 

  

INPUT: 

  

- ``i``, ``j`` -- two ``int``'s 

  

OUTPUT: ``None`` 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(4, 3)([1, 0, 2, 1]) 

sage: fs2 = copy(fs) 

sage: fs2._setimage(2, 1) 

sage: fs2 

[1, 0, 1, 1] 

sage: with fs.clone() as fs3: 

....: fs3._setimage(0, 2) 

....: fs3._setimage(1, 2) 

sage: fs3 

[2, 2, 2, 1] 

  

TESTS:: 

  

sage: with fs.clone() as fs3: 

....: fs3._setimage(6, 2) 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

sage: with fs.clone() as fs3: 

....: fs3._setimage(1, 4) 

Traceback (most recent call last): 

... 

AssertionError: Wrong value self(1) = 4 

""" 

self._setitem(i, j) 

  

cpdef _getimage(self, int i): 

""" 

Returns the image of ``i`` by ``self`` 

  

This is a fast internal version where ``i`` is an int. 

  

INPUT: 

  

- ``i`` -- an ``int`` 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(4, 3)([1, 0, 2, 1]) 

sage: fs._getimage(0), fs._getimage(1), fs._getimage(2), fs._getimage(3) 

(1, 0, 2, 1) 

""" 

return self._getitem(i) 

  

cpdef setimage(self, i, j): 

""" 

Set the image of ``i`` as ``j`` in ``self`` 

  

.. warning:: ``self`` must be mutable; otherwise an exception is raised. 

  

INPUT: 

  

- ``i``, ``j`` -- two ``object``'s 

  

OUTPUT: ``None`` 

  

.. NOTE:: if you need speed, please use instead :meth:`_setimage` 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(4, 3)([1, 0, 2, 1]) 

sage: fs2 = copy(fs) 

sage: fs2.setimage(2, 1) 

sage: fs2 

[1, 0, 1, 1] 

sage: with fs.clone() as fs3: 

....: fs3.setimage(0, 2) 

....: fs3.setimage(1, 2) 

sage: fs3 

[2, 2, 2, 1] 

""" 

self._setitem(int(i), int(j)) 

  

cpdef getimage(self, i): 

""" 

Returns the image of ``i`` by ``self`` 

  

INPUT: 

  

- ``i`` -- any object. 

  

.. NOTE:: if you need speed, please use instead :meth:`_getimage` 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(4, 3)([1, 0, 2, 1]) 

sage: fs.getimage(0), fs.getimage(1), fs.getimage(2), fs.getimage(3) 

(1, 0, 2, 1) 

""" 

return self._getitem(int(i)) 

  

cpdef image_set(self): 

""" 

Returns the image set of ``self`` 

  

EXAMPLES:: 

  

sage: FiniteSetMaps(4, 3)([1, 0, 2, 1]).image_set() 

{0, 1, 2} 

sage: FiniteSetMaps(4, 3)([1, 0, 0, 1]).image_set() 

{0, 1} 

""" 

return Set_object_enumerated(self) 

  

cpdef fibers(self): 

""" 

Returns the fibers of ``self`` 

  

OUTPUT: 

  

a dictionary ``d`` such that ``d[y]`` is the set of all ``x`` in 

``domain`` such that ``f(x) = y`` 

  

EXAMPLES:: 

  

sage: FiniteSetMaps(4, 3)([1, 0, 2, 1]).fibers() 

{0: {1}, 1: {0, 3}, 2: {2}} 

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: F.from_dict({"a": "b", "b": "a", "c": "b"}).fibers() 

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

""" 

return fibers(self, self.domain()) 

  

cpdef items(self): 

""" 

The items of ``self`` 

  

Return the list of the ordered pairs ``(x, self(x))`` 

  

EXAMPLES:: 

  

sage: FiniteSetMaps(4, 3)([1, 0, 2, 1]).items() 

[(0, 1), (1, 0), (2, 2), (3, 1)] 

""" 

return [(i, self._getimage(i)) for i in self.domain()] 

  

cpdef check(self): 

""" 

Performs checks on ``self`` 

  

Check that ``self`` is a proper function and then calls 

``parent.check_element(self)`` where ``parent`` is the parent 

of ``self``. 

  

TESTS:: 

  

sage: fs = FiniteSetMaps(3, 2) 

sage: for el in fs: el.check() 

sage: fs([1,1]) 

Traceback (most recent call last): 

... 

AssertionError: Wrong number of values 

sage: fs([0,0,2]) 

Traceback (most recent call last): 

... 

AssertionError: Wrong value self(2) = 2 

""" 

cdef int i, m, n 

m = self._parent._m 

n = self._parent._n 

assert self._len == m, "Wrong number of values" 

for i in range(m): 

assert 0 <= self._list[i] < n, "Wrong value self(%i) = %i"%(i, self._list[i]) 

if hasattr(self._parent, 'check_element'): 

self._parent.check_element(self) 

  

cpdef FiniteSetMap_MN _compose_internal_(self, FiniteSetMap_MN other, 

Parent resParent): 

""" 

TESTS:: 

  

sage: FSM = FiniteSetMaps(3) 

sage: fs1 = FSM([1, 0, 2]) 

sage: fs2 = FSM([0, 1, 2]) 

sage: el = fs1*fs2; el 

[1, 0, 2] 

sage: el.check() 

""" 

cdef type t = type(self) 

cdef FiniteSetMap_MN res = t.__new__(t) 

res._parent = resParent 

res._alloc_(self._len) 

for i in range(self._len): 

res._list[i] = other._list[self._list[i]] 

res.set_immutable() 

return res 

  

  

cpdef FiniteSetMap_Set FiniteSetMap_Set_from_list(t, parent, lst): 

""" 

Creates a ``FiniteSetMap`` from a list 

  

.. warning:: no check is performed ! 

  

TESTS:: 

  

sage: from sage.sets.finite_set_map_cy import FiniteSetMap_Set_from_list as from_list 

sage: F = FiniteSetMaps(["a", "b"], [3, 4, 5]) 

sage: f = from_list(F.element_class, F, [0,2]); f.check(); f 

map: a -> 3, b -> 5 

sage: f.parent() is F 

True 

sage: f.is_immutable() 

True 

""" 

cdef FiniteSetMap_MN res 

cdef type cls = <type>t 

res = cls.__new__(cls) 

super(FiniteSetMap_MN, res).__init__(parent, lst) 

return res 

  

cpdef FiniteSetMap_Set FiniteSetMap_Set_from_dict(t, parent, d): 

""" 

Creates a ``FiniteSetMap`` from a dictionary 

  

.. warning:: no check is performed ! 

  

TESTS:: 

  

sage: from sage.sets.finite_set_map_cy import FiniteSetMap_Set_from_dict as from_dict 

sage: F = FiniteSetMaps(["a", "b"], [3, 4, 5]) 

sage: f = from_dict(F.element_class, F, {"a": 3, "b": 5}); f.check(); f 

map: a -> 3, b -> 5 

sage: f.parent() is F 

True 

sage: f.is_immutable() 

True 

""" 

cdef FiniteSetMap_Set res 

cdef type cls = <type>t 

res = cls.__new__(cls) 

res.__init__(parent, d.__getitem__) 

return res 

  

  

cdef class FiniteSetMap_Set(FiniteSetMap_MN): 

""" 

Data structure for maps 

  

We assume that the parent given as argument is such that: 

  

- the domain is in ``parent.domain()`` 

- the codomain is in ``parent.codomain()`` 

- ``parent._m`` contains the cardinality of the domain 

- ``parent._n`` contains the cardinality of the codomain 

- ``parent._unrank_domain`` and ``parent._rank_domain`` is a pair of 

reciprocal rank and unrank functions beween the domain and 

``range(parent._m)``. 

- ``parent._unrank_codomain`` and ``parent._rank_codomain`` is a pair of 

reciprocal rank and unrank functions beween the codomain and 

``range(parent._n)``. 

""" 

  

def __init__(self, parent, fun, check=True): 

""" 

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b", "c", "d"], ["u", "v", "w"]) 

sage: F(lambda x: "v") 

map: a -> v, b -> v, c -> v, d -> v 

""" 

# For speed we initialize self by hand. 

# super(FiniteSetMap_Set, self).__init__(parent, [], check=False) 

self._parent = parent 

self._alloc_(int(parent._m)) 

for i, el in enumerate(parent.domain().list()): 

self._setitem(i, parent._rank_codomain(fun(el))) 

self.set_immutable() 

if check: self.check() 

  

from_list = classmethod(FiniteSetMap_Set_from_list) 

from_dict = classmethod(FiniteSetMap_Set_from_dict) 

  

def __call__(self, i): 

""" 

Returns the image of ``i`` under the map ``self`` 

  

INPUT: 

  

- ``i`` -- an int 

  

OUTPUT: 

  

an int 

  

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b"], [3, 4, 5]) 

sage: fs = F.from_dict({"a": 3, "b": 5}) 

sage: fs('a'), fs('b') 

(3, 5) 

""" 

parent = self._parent 

return parent._unrank_codomain(self._getitem(parent._rank_domain(i))) 

  

cpdef image_set(self): 

""" 

Returns the image set of ``self`` 

  

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: F.from_dict({"a": "b", "b": "a", "c": "b"}).image_set() 

{'a', 'b'} 

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: F(lambda x: "c").image_set() 

{'c'} 

""" 

image_i = self._parent._unrank_codomain 

return Set_object_enumerated([image_i(i) for i in self]) 

  

cpdef setimage(self, i, j): 

""" 

Set the image of ``i`` as ``j`` in ``self`` 

  

.. warning:: ``self`` must be mutable otherwise an exception is raised. 

  

INPUT: 

  

- ``i``, ``j`` -- two ``object``'s 

  

OUTPUT: ``None`` 

  

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b", "c", "d"], ["u", "v", "w"]) 

sage: fs = F(lambda x: "v") 

sage: fs2 = copy(fs) 

sage: fs2.setimage("a", "w") 

sage: fs2 

map: a -> w, b -> v, c -> v, d -> v 

sage: with fs.clone() as fs3: 

....: fs3.setimage("a", "u") 

....: fs3.setimage("c", "w") 

sage: fs3 

map: a -> u, b -> v, c -> w, d -> v 

  

TESTS:: 

  

sage: with fs.clone() as fs3: 

....: fs3.setimage("z", 2) 

Traceback (most recent call last): 

... 

ValueError: 'z' is not in dict 

  

sage: with fs.clone() as fs3: 

....: fs3.setimage(1, 4) 

Traceback (most recent call last): 

... 

ValueError: 1 is not in dict 

""" 

parent = self._parent 

return self._setitem(parent._rank_domain(i), parent._rank_codomain(j)) 

  

cpdef getimage(self, i): 

""" 

Returns the image of ``i`` by ``self`` 

  

INPUT: 

  

- ``i`` -- an ``int`` 

  

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b", "c", "d"], ["u", "v", "w"]) 

sage: fs = F._from_list_([1, 0, 2, 1]) 

sage: list(map(fs.getimage, ["a", "b", "c", "d"])) 

['v', 'u', 'w', 'v'] 

""" 

parent = self._parent 

return parent._unrank_codomain(self._getitem(parent._rank_domain(i))) 

  

cpdef items(self): 

""" 

The items of ``self`` 

  

Return the list of the couple ``(x, self(x))`` 

  

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: F.from_dict({"a": "b", "b": "a", "c": "b"}).items() 

[('a', 'b'), ('b', 'a'), ('c', 'b')] 

  

TESTS:: 

  

sage: all(F.from_dict(dict(f.items())) == f for f in F) 

True 

""" 

parent = self._parent 

return [(parent._unrank_domain(i), 

parent._unrank_codomain(self._getitem(i))) 

for i in range(parent._m)] 

  

def _repr_(self): 

""" 

TESTS:: 

  

sage: F = FiniteSetMaps(["a", "b"], [3, 4, 5]) 

sage: F._from_list_([0, 2]) 

map: a -> 3, b -> 5 

""" 

return "map: "+", ".join([("%s -> %s"%(i, self(i))) for i in self.domain()]) 

  

  

cdef class FiniteSetEndoMap_N(FiniteSetMap_MN): 

""" 

Maps from ``range(n)`` to itself. 

  

.. SEEALSO:: :class:`FiniteSetMap_MN` for assumptions on the parent 

  

TESTS:: 

  

sage: fs = FiniteSetMaps(3)([1, 0, 2]) 

sage: TestSuite(fs).run() 

""" 

  

def __mul__(FiniteSetEndoMap_N self, FiniteSetEndoMap_N other): 

""" 

TESTS:: 

  

sage: F = FiniteSetMaps(3) 

sage: F([1, 0, 2]) * F([2, 1, 0]) 

[2, 0, 1] 

sage: F = FiniteSetMaps(3, action="right") 

sage: F([1, 0, 2]) * F([2, 1, 0]) 

[1, 2, 0] 

""" 

assert(self._parent is other._parent), "Parent mismatch" 

if self._parent._action == "right": 

return self._compose_internal_(other, self._parent) 

else: 

return other._compose_internal_(self, self._parent) 

  

def __pow__(self, n, dummy): 

""" 

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

  

INPUT: 

  

- ``n`` -- a positive integer 

- ``dummy`` -- not used; must be set to ``None`` (for compatibility only). 

  

EXAMPLES:: 

  

sage: fs = FiniteSetMaps(3)([1,0,2]) 

sage: fs^2 

[0, 1, 2] 

sage: fs^0 

[0, 1, 2] 

sage: fs.__pow__(2) 

[0, 1, 2] 

""" 

if dummy is not None: 

raise RuntimeError("__pow__ dummy argument not used") 

return generic_power(self, n) 

  

  

cdef class FiniteSetEndoMap_Set(FiniteSetMap_Set): 

""" 

Maps from a set to itself 

  

.. SEEALSO:: :class:`FiniteSetMap_Set` for assumptions on the parent 

  

TESTS:: 

  

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: f = F.from_dict({"a": "b", "b": "a", "c": "b"}); f 

map: a -> b, b -> a, c -> b 

sage: TestSuite(f).run() 

""" 

  

def __mul__(FiniteSetEndoMap_Set self, FiniteSetEndoMap_Set other): 

""" 

TESTS:: 

  

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: f = F.from_dict({"a": "b", "b": "a", "c": "b"}); f 

map: a -> b, b -> a, c -> b 

sage: g = F.from_dict({"a": "c", "b": "c", "c": "a"}); g 

map: a -> c, b -> c, c -> a 

sage: f * g 

map: a -> b, b -> b, c -> b 

sage: g * f 

map: a -> c, b -> c, c -> c 

""" 

assert(self._parent is other._parent), "Parent mismatch" 

if self._parent._action == "right": 

return self._compose_internal_(other, self._parent) 

else: 

return other._compose_internal_(self, self._parent) 

  

def __pow__(self, n, dummy): 

""" 

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

  

INPUT: 

  

- ``n`` -- a positive integer 

- ``dummy`` -- not used; must be set to None (for compatibility only). 

  

EXAMPLES:: 

  

sage: F = FiniteSetMaps(["a", "b", "c"]) 

sage: f = F.from_dict({"a": "b", "b": "a", "c": "b"}); f 

map: a -> b, b -> a, c -> b 

sage: f^2 

map: a -> a, b -> b, c -> a 

sage: f^3 == f 

True 

""" 

if dummy is not None: 

raise RuntimeError("__pow__ dummy argument not used") 

return generic_power(self, n)