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

r""" 

Group, ring, etc. actions on objects. 

  

The terminology and notation used is suggestive of groups acting on sets, 

but this framework can be used for modules, algebras, etc. 

  

A group action $G \times S \rightarrow S$ is a functor from $G$ to Sets. 

  

.. WARNING:: 

  

An :class:`Action` object only keeps a weak reference to the underlying set 

which is acted upon. This decision was made in :trac:`715` in order to 

allow garbage collection within the coercion framework (this is where 

actions are mainly used) and avoid memory leaks. 

  

:: 

  

sage: from sage.categories.action import Action 

sage: class P: pass 

sage: A = Action(P(),P()) 

sage: import gc 

sage: _ = gc.collect() 

sage: A 

<repr(<sage.categories.action.Action at 0x...>) failed: RuntimeError: This action acted on a set that became garbage collected> 

  

To avoid garbage collection of the underlying set, it is sufficient to 

create a strong reference to it before the action is created. 

  

:: 

  

sage: _ = gc.collect() 

sage: from sage.categories.action import Action 

sage: class P: pass 

sage: q = P() 

sage: A = Action(P(),q) 

sage: gc.collect() 

0 

sage: A 

Left action by <__main__.P instance at ...> on <__main__.P instance at ...> 

  

AUTHOR: 

  

- Robert Bradshaw: initial version 

""" 

  

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

# Copyright (C) 2007 Robert Bradshaw <robertwb@math.washington.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 

# is available at: 

# 

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

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

from __future__ import absolute_import 

  

from .functor cimport Functor 

from .morphism cimport Morphism 

from .map cimport Map 

from sage.structure.parent cimport Parent 

  

from . import homset 

import sage.structure.element 

from weakref import ref 

from sage.misc.constant_function import ConstantFunction 

  

  

cdef inline category(x): 

try: 

return x.category() 

except AttributeError: 

import sage.categories.all 

return sage.categories.all.Objects() 

  

cdef class Action(Functor): 

  

def __init__(self, G, S, bint is_left = 1, op=None): 

from .groupoid import Groupoid 

Functor.__init__(self, Groupoid(G), category(S)) 

self.G = G 

self.US = ref(S) 

self._is_left = is_left 

self.op = op 

  

def _apply_functor(self, x): 

return self(x) 

  

def __call__(self, *args): 

if len(args) == 1: 

g = args[0] 

if g in self.G: 

return ActionEndomorphism(self, self.G(g)) 

elif g == self.G: 

return self.underlying_set() 

else: 

raise TypeError("%s not an element of %s" % (g, self.G)) 

elif len(args) == 2: 

if self._is_left: 

return self._call_(self.G(args[0]), self.underlying_set()(args[1])) 

else: 

return self._call_(self.underlying_set()(args[0]), self.G(args[1])) 

  

cpdef _call_(self, a, b): 

raise NotImplementedError("Action not implemented.") 

  

def act(self, g, a): 

""" 

This is a consistent interface for acting on a by g, 

regardless of whether it's a left or right action. 

""" 

if self._is_left: 

return self._call_(g, a) 

else: 

return self._call_(a, g) 

  

def __invert__(self): 

return InverseAction(self) 

  

def is_left(self): 

return self._is_left 

  

def _repr_(self): 

side = "Left" if self._is_left else "Right" 

return "%s %s by %r on %r"%(side, self._repr_name_(), self.G, 

self.underlying_set()) 

  

def _repr_name_(self): 

return "action" 

  

def actor(self): 

return self.G 

  

cdef underlying_set(self): 

""" 

The set on which the actor acts (it is not necessarily the codomain of 

the action). 

  

.. NOTE:: 

  

Since this is a cdef'ed method, we can only provide an indirect doctest. 

  

EXAMPLES:: 

  

sage: P = QQ['x'] 

sage: R = (ZZ['x'])['y'] 

sage: A = R.get_action(P,operator.mul,True) 

sage: A # indirect doctest 

Right scalar multiplication by Univariate Polynomial Ring in x over 

Rational Field on Univariate Polynomial Ring in y over Univariate 

Polynomial Ring in x over Integer Ring 

  

In this example, the underlying set is the ring ``R``. This is the same 

as the left domain, which is different from the codomain of the action:: 

  

sage: A.codomain() 

Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field 

sage: A.codomain() == R 

False 

sage: A.left_domain() is R 

True 

  

By :trac:`715`, there is only a weak reference to the underlying set. 

Hence, the underlying set may be garbage collected, even when the 

action is still alive. This may result in a runtime error, as follows:: 

  

sage: from sage.categories.action import Action 

sage: class P: pass 

sage: p = P() 

sage: q = P() 

sage: A = Action(p,q) 

sage: A 

Left action by <__main__.P instance at ...> on <__main__.P instance at ...> 

sage: del q 

sage: import gc 

sage: _ = gc.collect() 

sage: A 

<repr(<sage.categories.action.Action at 0x...>) failed: RuntimeError: This action acted on a set that became garbage collected> 

""" 

S = self.US() 

if S is None: 

raise RuntimeError("This action acted on a set that became garbage collected") 

return S 

  

def codomain(self): 

return self.underlying_set() 

  

def domain(self): 

return self.underlying_set() 

  

def left_domain(self): 

if self._is_left: 

return self.G 

else: 

return self.domain() 

  

def right_domain(self): 

if self._is_left: 

return self.domain() 

else: 

return self.G 

  

def operation(self): 

return self.op 

  

  

cdef class InverseAction(Action): 

""" 

An action that acts as the inverse of the given action. 

  

EXAMPLES:: 

  

sage: V = QQ^3 

sage: v = V((1, 2, 3)) 

sage: cm = get_coercion_model() 

  

sage: a = cm.get_action(V, QQ, operator.mul) 

sage: a 

Right scalar multiplication by Rational Field on Vector space of dimension 3 over Rational Field 

sage: ~a 

Right inverse action by Rational Field on Vector space of dimension 3 over Rational Field 

sage: (~a)(v, 1/3) 

(3, 6, 9) 

  

sage: b = cm.get_action(QQ, V, operator.mul) 

sage: b 

Left scalar multiplication by Rational Field on Vector space of dimension 3 over Rational Field 

sage: ~b 

Left inverse action by Rational Field on Vector space of dimension 3 over Rational Field 

sage: (~b)(1/3, v) 

(3, 6, 9) 

  

sage: c = cm.get_action(ZZ, list, operator.mul) 

sage: c 

Left action by Integer Ring on <... 'list'> 

sage: ~c 

Traceback (most recent call last): 

... 

TypeError: no inverse defined for Left action by Integer Ring on <... 'list'> 

  

TESTS: 

  

sage: x = polygen(QQ,'x') 

sage: a = 2*x^2+2; a 

2*x^2 + 2 

sage: a / 2 

x^2 + 1 

sage: a /= 2 

sage: a 

x^2 + 1 

""" 

def __init__(self, Action action): 

G = action.G 

try: 

from sage.groups.group import is_Group 

# We must be in the case that parent(~a) == parent(a) 

# so we can invert in _call_ code below. 

if (is_Group(G) and G.is_multiplicative()) or G.is_field(): 

Action.__init__(self, G, action.underlying_set(), action._is_left) 

self._action = action 

return 

except (AttributeError, NotImplementedError): 

pass 

raise TypeError(f"no inverse defined for {action!r}") 

  

cpdef _call_(self, a, b): 

if self._action._is_left: 

if self.S_precomposition is not None: 

b = self.S_precomposition(b) 

return self._action._call_(~a, b) 

else: 

if self.S_precomposition is not None: 

a = self.S_precomposition(a) 

return self._action._call_(a, ~b) 

  

def codomain(self): 

return self._action.codomain() 

  

def __invert__(self): 

return self._action 

  

def _repr_name_(self): 

return "inverse action" 

  

cdef class PrecomposedAction(Action): 

""" 

A precomposed action first applies given maps, and then applying an action 

to the return values of the maps. 

  

EXAMPLES: 

  

We demonstrate that an example discussed on :trac:`14711` did not become a 

problem:: 

  

sage: E = ModularSymbols(11).2 

sage: s = E.modular_symbol_rep() 

sage: del E,s 

sage: import gc 

sage: _ = gc.collect() 

sage: E = ModularSymbols(11).2 

sage: v = E.manin_symbol_rep() 

sage: c,x = v[0] 

sage: y = x.modular_symbol_rep() 

sage: A = y.parent().get_action(QQ, self_on_left=False, op=operator.mul) 

sage: A 

Left scalar multiplication by Rational Field on Abelian Group of all Formal Finite Sums over Rational Field 

with precomposition on right by Coercion map: 

From: Abelian Group of all Formal Finite Sums over Integer Ring 

To: Abelian Group of all Formal Finite Sums over Rational Field 

""" 

def __init__(self, Action action, Map left_precomposition, Map right_precomposition): 

left = action.left_domain() 

right = action.right_domain() 

US = action.underlying_set() 

cdef Parent lco, rco 

if left_precomposition is not None: 

lco = left_precomposition._codomain 

if lco is not left: 

left_precomposition = homset.Hom(lco, left).natural_map() * left_precomposition 

left = left_precomposition.domain() 

if right_precomposition is not None: 

rco = right_precomposition._codomain 

if rco is not right: 

right_precomposition = homset.Hom(rco, right).natural_map() * right_precomposition 

right = right_precomposition.domain() 

if action._is_left: 

Action.__init__(self, left, US, 1) 

else: 

Action.__init__(self, right, US, 0) 

self._action = action 

self.left_precomposition = left_precomposition 

self.right_precomposition = right_precomposition 

  

cpdef _call_(self, a, b): 

if self.left_precomposition is not None: 

a = self.left_precomposition._call_(a) 

if self.right_precomposition is not None: 

b = self.right_precomposition._call_(b) 

return self._action._call_(a, b) 

  

def domain(self): 

if self._is_left and self.right_precomposition is not None: 

return self.right_precomposition.domain() 

elif not self._is_left and self.left_precomposition is not None: 

return self.left_precomposition.domain() 

else: 

return self._action.domain() 

  

def codomain(self): 

return self._action.codomain() 

  

def __invert__(self): 

return PrecomposedAction(~self._action, self.left_precomposition, self.right_precomposition) 

  

def _repr_(self): 

s = repr(self._action) 

if self.left_precomposition is not None: 

s += "\nwith precomposition on left by %s" % self.left_precomposition._default_repr_() 

if self.right_precomposition is not None: 

s += "\nwith precomposition on right by %s" % self.right_precomposition._default_repr_() 

return s 

  

  

cdef class ActionEndomorphism(Morphism): 

""" 

The endomorphism defined by the action of one element. 

  

EXAMPLES:: 

  

sage: A = ZZ['x'].get_action(QQ, self_on_left=False, op=operator.mul) 

sage: A 

Left scalar multiplication by Rational Field on Univariate Polynomial 

Ring in x over Integer Ring 

sage: A(1/2) 

Action of 1/2 on Univariate Polynomial Ring in x over Integer Ring 

under Left scalar multiplication by Rational Field on Univariate 

Polynomial Ring in x over Integer Ring. 

""" 

def __init__(self, Action action, g): 

Morphism.__init__(self, homset.Hom(action.underlying_set(), 

action.underlying_set())) 

self._action = action 

self._g = g 

  

cdef dict _extra_slots(self): 

""" 

Helper for pickling and copying. 

  

TESTS:: 

  

sage: P.<x> = ZZ[] 

sage: A = P.get_action(QQ, self_on_left=False, op=operator.mul) 

sage: phi = A(1/2) 

sage: psi = copy(phi) # indirect doctest 

sage: psi 

Action of 1/2 on Univariate Polynomial Ring in x over 

Integer Ring under Left scalar multiplication by Rational 

Field on Univariate Polynomial Ring in x over Integer Ring. 

sage: psi(x) == phi(x) 

True 

""" 

slots = Morphism._extra_slots(self) 

slots['_action'] = self._action 

slots['_g'] = self._g 

return slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for pickling and copying. 

  

TESTS:: 

  

sage: P.<x> = ZZ[] 

sage: A = P.get_action(QQ, self_on_left=False, op=operator.mul) 

sage: phi = A(1/2) 

sage: psi = copy(phi) # indirect doctest 

sage: psi 

Action of 1/2 on Univariate Polynomial Ring in x over 

Integer Ring under Left scalar multiplication by Rational 

Field on Univariate Polynomial Ring in x over Integer Ring. 

sage: psi(x) == phi(x) 

True 

""" 

self._action = _slots['_action'] 

self._g = _slots['_g'] 

Morphism._update_slots(self, _slots) 

  

cpdef Element _call_(self, x): 

if self._action._is_left: 

return self._action._call_(self._g, x) 

else: 

return self._action._call_(x, self._g) 

  

def _repr_(self): 

return "Action of %s on %s under %s."%(self._g, 

self._action.underlying_set(), self._action) 

  

def __mul__(left, right): 

cdef ActionEndomorphism left_c, right_c 

if isinstance(left, ActionEndomorphism) and isinstance(right, ActionEndomorphism): 

left_c = left 

right_c = right 

if left_c._action is right_c._action: 

if left_c._action._is_left: 

return ActionEndomorphism(left_c._action, left_c._g * right_c._g) 

else: 

return ActionEndomorphism(left_c._action, right_c._g * left_c._g) 

return Morphism.__mul__(left, right) 

  

def __invert__(self): 

inv_g = ~self._g 

if sage.structure.element.parent(inv_g) is sage.structure.element.parent(self._g): 

return ActionEndomorphism(self._action, inv_g) 

else: 

return (~self._action)(self._g)