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

r""" 

Toric lattice elements 

  

This module was designed as a part of the framework for toric varieties 

(:mod:`~sage.schemes.toric.variety`, 

:mod:`~sage.schemes.toric.fano_variety`). 

  

AUTHORS: 

  

- Andrey Novoseltsev (2010-05-27): initial version. 

  

TESTS: 

  

Let's create some lattices first:: 

  

sage: N = ToricLattice(3) 

sage: M = N.dual() 

  

Now we are ready to create elements of toric lattices:: 

  

sage: n = N([1,2,3]) 

sage: n 

N(1, 2, 3) 

sage: m = M(1,2,3) 

sage: m 

M(1, 2, 3) 

  

Dual lattices can act on each other:: 

  

sage: n * m 

14 

sage: m * n 

14 

  

You can also add elements of the same lattice or scale them:: 

  

sage: 2 * n 

N(2, 4, 6) 

sage: n * 2 

N(2, 4, 6) 

sage: n + n 

N(2, 4, 6) 

  

However, you cannot "mix wrong lattices" in your expressions:: 

  

sage: n + m 

Traceback (most recent call last): 

... 

TypeError: unsupported operand parent(s) for +: 

'3-d lattice N' and '3-d lattice M' 

sage: n * n 

Traceback (most recent call last): 

... 

TypeError: elements of the same toric lattice cannot be multiplied! 

sage: n == m 

False 

  

Note that ``n`` and ``m`` are not equal to each other even though they are 

both "just (1,2,3)." Moreover, you cannot easily convert elements between 

toric lattices:: 

  

sage: M(n) 

Traceback (most recent call last): 

... 

TypeError: N(1, 2, 3) cannot be converted to 3-d lattice M! 

  

If you really need to consider elements of one lattice as elements of another, 

you can either use intermediate conversion to "just a vector":: 

  

sage: ZZ3 = ZZ^3 

sage: n_in_M = M(ZZ3(n)) 

sage: n_in_M 

M(1, 2, 3) 

sage: n == n_in_M 

False 

sage: n_in_M == m 

True 

  

Or you can create a homomorphism from one lattice to any other:: 

  

sage: h = N.hom(identity_matrix(3), M) 

sage: h(n) 

M(1, 2, 3) 

""" 

# The "tutorial" above is a truncated version of one in toric_lattice.py. 

  

  

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

# Copyright (C) 2010 Andrey Novoseltsev <novoselt@gmail.com> 

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

# 

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

# 

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

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

  

from sage.libs.gmp.mpz cimport * 

  

from sage.geometry.toric_plotter import ToricPlotter 

from sage.modules.vector_integer_dense cimport Vector_integer_dense 

from sage.structure.coerce_exceptions import CoercionException 

from sage.structure.element cimport Element, Vector 

from sage.rings.integer cimport Integer 

from sage.structure.richcmp cimport richcmp_not_equal, richcmp 

  

  

def is_ToricLatticeElement(x): 

r""" 

Check if ``x`` is an element of a toric lattice. 

  

INPUT: 

  

- ``x`` -- anything. 

  

OUTPUT: 

  

- ``True`` if ``x`` is an element of a toric lattice, ``False`` otherwise. 

  

EXAMPLES:: 

  

sage: from sage.geometry.toric_lattice_element import ( 

....: is_ToricLatticeElement) 

sage: is_ToricLatticeElement(1) 

False 

sage: e = ToricLattice(3).an_element() 

sage: e 

N(1, 0, 0) 

sage: is_ToricLatticeElement(e) 

True 

""" 

return isinstance(x, ToricLatticeElement) 

  

  

# Why do we need a special class: 

# - customize output to include lattice name 

# - prohibit operations mixing "wrong" lattices 

cdef class ToricLatticeElement(Vector_integer_dense): 

r""" 

Create an element of a toric lattice. 

  

.. WARNING:: 

  

You probably should not construct such elements explicitly. 

  

INPUT: 

  

- same as for 

:class:`~sage.modules.vector_integer_dense.Vector_integer_dense`. 

  

OUTPUT: 

  

- element of a toric lattice. 

  

TESTS:: 

  

sage: N = ToricLattice(3) 

sage: from sage.geometry.toric_lattice_element import ( 

....: ToricLatticeElement) 

sage: e = ToricLatticeElement(N, [1,2,3]) 

sage: e 

N(1, 2, 3) 

sage: TestSuite(e).run() 

""" 

def __richcmp__(self, right, op): 

r""" 

Compare ``self`` and ``right`` according to the operator ``op``. 

  

INPUT: 

  

- ``right`` -- another ToricLatticeElement 

  

OUTPUT: 

  

boolean 

  

First compare the ambient toric lattice, then compare the vectors. 

  

TESTS:: 

  

sage: N = ToricLattice(3) 

sage: M = N.dual() 

sage: n = N(1,2,3) 

sage: m = M(1,2,3) 

sage: n == m 

False 

sage: n2 = N(1,2,3) 

sage: n == n2 

True 

sage: n is n2 

False 

""" 

if not is_ToricLatticeElement(right): 

return NotImplemented 

  

PL_ambient = self.parent().ambient_module() 

PR_ambient = right.parent().ambient_module() 

if PL_ambient != PR_ambient: 

return richcmp_not_equal(PL_ambient, PR_ambient, op) 

# Now use the real comparison of vectors 

return self._richcmp_(right, op) 

  

# For some reason, vectors work just fine without redefining this function 

# from the base class, but if it is not here, we get "unhashable type"... 

def __hash__(self): 

r""" 

Return the hash of ``self``. 

  

OUTPUT: 

  

- integer. 

  

TESTS:: 

  

sage: N = ToricLattice(3) 

sage: n = N(1,2,3) 

sage: hash(n) 

Traceback (most recent call last): 

... 

TypeError: mutable vectors are unhashable 

sage: n.set_immutable() 

sage: hash(n) == hash(n) 

True 

""" 

return Vector_integer_dense.__hash__(self) 

  

cpdef _act_on_(self, other, bint self_on_left): 

""" 

Act on ``other``. 

  

INPUT: 

  

- ``other`` - :class:`ToricLatticeElement`. 

  

OUTPUT: 

  

- integer, if ``other`` is an element of the dual lattice of ``self``; 

  

- ``CoercionException`` is raised if ``other`` is an element of 

an incompatible toric lattice; 

  

- standard output for ``self`` acting as an integral vector on 

``other`` if the latter one is not an element of a toric lattice. 

  

TESTS:: 

  

sage: N = ToricLattice(3) 

sage: M = N.dual() 

sage: n = N(1,2,3) 

sage: m = M(1,2,3) 

sage: n * m # indirect doctest 

14 

  

Now we test behaviour with other types:: 

  

sage: v = vector([1, 2, 3]) 

sage: v * n == n * v 

True 

sage: v = vector([1, 1/2, 3/4]) 

sage: v * n == n * v 

True 

sage: A = matrix(3, range(9)) 

sage: A * n 

(8, 26, 44) 

sage: n * A 

(24, 30, 36) 

sage: B = A / 3 

sage: B * n 

(8/3, 26/3, 44/3) 

sage: n * B 

(8, 10, 12) 

""" 

Ns = self.parent() 

# We try to deal only with the case of two lattice elements... 

if is_ToricLatticeElement(other): 

if other.parent().ambient_module() is Ns.ambient_module().dual(): 

# Our own _dot_product_ is disabled 

return Vector_integer_dense._dot_product_(self, other) 

raise CoercionException("only elements of dual toric lattices " 

"can act on each other!") 

# ... however we also need to treat the case when other is an integral 

# vector, since otherwise it will be coerced to the parent of self and 

# then the dot product will be called for elements of the same lattice 

if isinstance(other, Vector_integer_dense): 

return Vector_integer_dense._dot_product_(self, other) 

# We also allow action on elements of lattice quotients 

try: 

lift = other.lift() 

if is_ToricLatticeElement(lift): 

if other.parent().W().is_submodule(Ns.dual().W()): 

return Vector_integer_dense._dot_product_(self, lift) 

raise CoercionException("only elements of dual toric lattices " 

"can act on each other!") 

except AttributeError: # No lift 

pass 

# Now let the standard framework work... 

return Vector_integer_dense._act_on_(self, other, self_on_left) 

  

# We need to override this function to prohibit default behaviour. 

# It seems to be called when right is in the same lattice as self, which 

# is wrong from our point of view. 

cpdef _dot_product_(self, Vector right): 

""" 

Raise a ``TypeError`` exception. 

  

Dot product is not defined on toric lattices (there are actions of 

dual lattices on each other instead). 

  

INPUT: 

  

- ``right`` - vector. 

  

OUTPUT: 

  

- ``TypeError`` exception is raised. 

  

TESTS:: 

  

sage: N = ToricLattice(3) 

sage: M = N.dual() 

sage: n = N(1,2,3) 

sage: m = M(1,2,3) 

sage: n * n # indirect doctest 

Traceback (most recent call last): 

... 

TypeError: elements of the same 

toric lattice cannot be multiplied! 

""" 

raise TypeError("elements of the same toric lattice cannot be " 

"multiplied!") 

  

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

  

OUTPUT: 

  

- string. 

  

TESTS:: 

  

sage: Ld = ToricLattice(3, "L").dual() 

sage: e = Ld(1,2,3) 

sage: e._latex_() 

'\\left(1,\\,2,\\,3\\right)_{L^*}' 

""" 

return "%s_{%s}" % (super(ToricLatticeElement, self)._latex_(), 

self.parent().ambient_module()._latex_name) 

  

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

  

OUTPUT: 

  

- string. 

  

TESTS:: 

  

sage: Ld = ToricLattice(3, "L").dual() 

sage: e = Ld(1,2,3) 

sage: e._repr_() 

'L*(1, 2, 3)' 

""" 

return (self.parent().ambient_module()._name 

+ super(ToricLatticeElement, self)._repr_()) 

  

def __reduce__(self): 

""" 

Override the base ``__reduce__`` to correctly pickle/unpickle elements. 

  

EXAMPLES:: 

  

sage: N = ToricLattice(3) 

sage: loads(dumps(N(1,2,3))) 

N(1, 2, 3) 

""" 

return (unpickle_v1, (self._parent, self.list(), self._degree, self._is_mutable)) 

  

def plot(self, **options): 

r""" 

Plot ``self``. 

  

INPUT: 

  

- any options for toric plots (see :func:`toric_plotter.options 

<sage.geometry.toric_plotter.options>`), none are mandatory. 

  

OUTPUT: 

  

- a plot. 

  

EXAMPLES:: 

  

sage: N = ToricLattice(3) 

sage: n = N(1,2,3) 

sage: n.plot() 

Graphics3d Object 

""" 

tp = ToricPlotter(options, self.parent().degree()) 

tp.adjust_options() 

return tp.plot_points([self]) 

  

  

def unpickle_v1(parent, entries, degree, is_mutable): 

""" 

Unpickle a :class:`ToricLatticeElement` 

  

INPUT: 

  

- ``parent`` -- The parent toric lattice. 

  

- ``entries`` -- a list. The coordinates of the lattice point. 

  

- ``degree`` -- integer. the dimension of the toric lattice. 

  

- ``is_mutable`` -- boolean. Whether the lattice element is 

mutable. 

  

OUTPUT: 

  

The :class:`ToricLatticeElement` determined by the input data. 

  

EXAMPLES:: 

  

sage: N = ToricLattice(3, "lattice") 

sage: loads(dumps(N(1,2,3))) # indirect test 

lattice(1, 2, 3) 

sage: from sage.geometry.toric_lattice_element import unpickle_v1 

sage: unpickle_v1(N,[1,2,3],3,True) 

lattice(1, 2, 3) 

""" 

cdef ToricLatticeElement v 

v = ToricLatticeElement.__new__(ToricLatticeElement) 

v._init(degree, parent) 

cdef Integer z 

cdef Py_ssize_t i 

for i in range(degree): 

z = Integer(entries[i]) 

mpz_set(v._entries[i], z.value) 

v._is_mutable = is_mutable 

return v