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

""" 

Space of homomorphisms between two rings 

""" 

 

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

# Copyright (C) 2006 William Stein <wstein@gmail.com> 

# 

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

# 

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

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

from __future__ import absolute_import 

 

from sage.categories.homset import HomsetWithBase 

from sage.categories.rings import Rings 

_Rings = Rings() 

 

from . import morphism 

from . import quotient_ring 

 

def is_RingHomset(H): 

""" 

Return ``True`` if ``H`` is a space of homomorphisms between two rings. 

 

EXAMPLES:: 

 

sage: from sage.rings.homset import is_RingHomset as is_RH 

sage: is_RH(Hom(ZZ, QQ)) 

True 

sage: is_RH(ZZ) 

False 

sage: is_RH(Hom(RR, CC)) 

True 

sage: is_RH(Hom(FreeModule(ZZ,1), FreeModule(QQ,1))) 

False 

""" 

return isinstance(H, RingHomset_generic) 

 

 

def RingHomset(R, S, category = None): 

""" 

Construct a space of homomorphisms between the rings ``R`` and ``S``. 

 

For more on homsets, see :func:`Hom()`. 

 

EXAMPLES:: 

 

sage: Hom(ZZ, QQ) # indirect doctest 

Set of Homomorphisms from Integer Ring to Rational Field 

 

""" 

if quotient_ring.is_QuotientRing(R): 

return RingHomset_quo_ring(R, S, category = category) 

return RingHomset_generic(R, S, category = category) 

 

 

class RingHomset_generic(HomsetWithBase): 

""" 

A generic space of homomorphisms between two rings. 

 

EXAMPLES:: 

 

sage: Hom(ZZ, QQ) 

Set of Homomorphisms from Integer Ring to Rational Field 

sage: QQ.Hom(ZZ) 

Set of Homomorphisms from Rational Field to Integer Ring 

""" 

def __init__(self, R, S, category = None): 

""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: Hom(ZZ, QQ) 

Set of Homomorphisms from Integer Ring to Rational Field 

""" 

if category is None: 

category = _Rings 

HomsetWithBase.__init__(self, R, S, category) 

 

def _repr_(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: Hom(ZZ, QQ) # indirect doctest 

Set of Homomorphisms from Integer Ring to Rational Field 

""" 

return "Set of Homomorphisms from %s to %s"%(self.domain(), self.codomain()) 

 

def has_coerce_map_from(self, x): 

""" 

The default for coercion maps between ring homomorphism spaces is 

very restrictive (until more implementation work is done). 

 

Currently this checks if the domains and the codomains are equal. 

 

EXAMPLES:: 

 

sage: H = Hom(ZZ, QQ) 

sage: H2 = Hom(QQ, ZZ) 

sage: H.has_coerce_map_from(H2) 

False 

""" 

return (x.domain() == self.domain() and x.codomain() == self.codomain()) 

 

def _coerce_impl(self, x): 

""" 

Check to see if we can coerce ``x`` into a homomorphism with the 

correct rings. 

 

EXAMPLES:: 

 

sage: H = Hom(ZZ, QQ) 

sage: phi = H([1]) 

sage: H2 = Hom(QQ, QQ) 

sage: phi2 = H2(phi); phi2 # indirect doctest 

Ring endomorphism of Rational Field 

Defn: 1 |--> 1 

sage: H(phi2) # indirect doctest 

Ring morphism: 

From: Integer Ring 

To: Rational Field 

Defn: 1 |--> 1 

""" 

from sage.categories.map import Map 

if not (isinstance(x, Map) and x.category_for().is_subcategory(Rings())): 

raise TypeError 

if x.parent() is self: 

return x 

# Case 1: the parent fits 

if x.parent() == self: 

if isinstance(x, morphism.RingHomomorphism_im_gens): 

return morphism.RingHomomorphism_im_gens(self, x.im_gens()) 

elif isinstance(x, morphism.RingHomomorphism_cover): 

return morphism.RingHomomorphism_cover(self) 

elif isinstance(x, morphism.RingHomomorphism_from_base): 

return morphism.RingHomomorphism_from_base(self, x.underlying_map()) 

# Case 2: unique extension via fraction field 

try: 

if isinstance(x, morphism.RingHomomorphism_im_gens) and x.domain().fraction_field().has_coerce_map_from(self.domain()): 

return morphism.RingHomomorphism_im_gens(self, x.im_gens()) 

except Exception: 

pass 

# Case 3: the homomorphism can be extended by coercion 

try: 

return x.extend_codomain(self.codomain()).extend_domain(self.domain()) 

except Exception: 

pass 

# Last resort, case 4: the homomorphism is induced from the base ring 

if self.domain()==self.domain().base() or self.codomain()==self.codomain().base(): 

raise TypeError 

try: 

x = self.domain().base().Hom(self.codomain().base())(x) 

return morphism.RingHomomorphism_from_base(self, x) 

except Exception: 

raise TypeError 

 

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

""" 

Create a homomorphism. 

 

EXAMPLES:: 

 

sage: H = Hom(ZZ, QQ) 

sage: H([1]) 

Ring morphism: 

From: Integer Ring 

To: Rational Field 

Defn: 1 |--> 1 

 

TESTS:: 

 

sage: H = Hom(ZZ, QQ) 

sage: H == loads(dumps(H)) 

True 

""" 

from sage.categories.map import Map 

from sage.categories.all import Rings 

if isinstance(im_gens, Map): 

return self._coerce_impl(im_gens) 

else: 

return morphism.RingHomomorphism_im_gens(self, im_gens, check=check) 

 

def natural_map(self): 

""" 

Returns the natural map from the domain to the codomain. 

 

The natural map is the coercion map from the domain ring to the 

codomain ring. 

 

EXAMPLES:: 

 

sage: H = Hom(ZZ, QQ) 

sage: H.natural_map() 

Natural morphism: 

From: Integer Ring 

To: Rational Field 

""" 

f = self.codomain().coerce_map_from(self.domain()) 

if f is None: 

raise TypeError("natural coercion morphism from %s to %s not defined"%(self.domain(), self.codomain())) 

return f 

 

def zero(self): 

r""" 

Return the zero element of this homset. 

 

EXAMPLES: 

 

Since a ring homomorphism maps 1 to 1, there can only be a zero 

morphism when mapping to the trivial ring:: 

 

sage: Hom(ZZ, Zmod(1)).zero() 

Ring morphism: 

From: Integer Ring 

To: Ring of integers modulo 1 

Defn: 1 |--> 0 

sage: Hom(ZZ, Zmod(2)).zero() 

Traceback (most recent call last): 

... 

ValueError: homset has no zero element 

 

""" 

if not self.codomain().is_zero(): 

raise ValueError("homset has no zero element") 

# there is only one map in this homset 

return self.an_element() 

 

 

class RingHomset_quo_ring(RingHomset_generic): 

""" 

Space of ring homomorphisms where the domain is a (formal) quotient 

ring. 

 

EXAMPLES:: 

 

sage: R.<x,y> = PolynomialRing(QQ, 2) 

sage: S.<a,b> = R.quotient(x^2 + y^2) 

sage: phi = S.hom([b,a]); phi 

Ring endomorphism of Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2) 

Defn: a |--> b 

b |--> a 

sage: phi(a) 

b 

sage: phi(b) 

a 

 

TESTS: 

 

We test pickling of a homset from a quotient. 

 

:: 

 

sage: R.<x,y> = PolynomialRing(QQ, 2) 

sage: S.<a,b> = R.quotient(x^2 + y^2) 

sage: H = S.Hom(R) 

sage: H == loads(dumps(H)) 

True 

 

We test pickling of actual homomorphisms in a quotient:: 

 

sage: phi = S.hom([b,a]) 

sage: phi == loads(dumps(phi)) 

True 

""" 

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

""" 

Create a homomorphism. 

 

EXAMPLES:: 

 

sage: R.<x,y> = PolynomialRing(QQ, 2) 

sage: S.<a,b> = R.quotient(x^2 + y^2) 

sage: H = S.Hom(R) 

sage: phi = H([b,a]); phi 

Ring morphism: 

From: Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2) 

To: Multivariate Polynomial Ring in x, y over Rational Field 

Defn: a |--> b 

b |--> a 

 

""" 

if isinstance(im_gens, morphism.RingHomomorphism_from_quotient): 

return morphism.RingHomomorphism_from_quotient(self, im_gens._phi()) 

try: 

pi = self.domain().cover() 

phi = pi.domain().hom(im_gens, check=check) 

return morphism.RingHomomorphism_from_quotient(self, phi) 

except (NotImplementedError, ValueError) as err: 

try: 

return self._coerce_impl(im_gens) 

except TypeError: 

raise TypeError("images do not define a valid homomorphism") 

 

def _coerce_impl(self, x): 

""" 

Check to see if we can coerce ``x`` into a homomorphism with the 

correct rings. 

 

EXAMPLES:: 

 

sage: R.<x,y> = PolynomialRing(QQ, 2) 

sage: S.<a,b> = R.quotient(x^2 + y^2) 

sage: H = S.Hom(R) 

sage: phi = H([b,a]) 

sage: R2.<x,y> = PolynomialRing(ZZ, 2) 

sage: H2 = Hom(R2, S) 

sage: H2(phi) # indirect doctest 

Composite map: 

From: Multivariate Polynomial Ring in x, y over Integer Ring 

To: Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2) 

Defn: Coercion map: 

From: Multivariate Polynomial Ring in x, y over Integer Ring 

To: Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2) 

then 

Ring morphism: 

From: Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2) 

To: Multivariate Polynomial Ring in x, y over Rational Field 

Defn: a |--> b 

b |--> a 

then 

Coercion map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2) 

 

""" 

if not isinstance(x, morphism.RingHomomorphism_from_quotient): 

raise TypeError 

if x.parent() is self: 

return x 

if x.parent() == self: 

return morphism.RingHomomorphism_from_quotient(self, x._phi()) 

raise TypeError