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

""" 

Find isomorphisms between fans. 

""" 

 

 

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

# Copyright (C) 2012 Volker Braun <vbraun.name@gmail.com> 

# 

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

# 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 exceptions import Exception 

 

from sage.rings.all import ZZ 

from sage.matrix.constructor import column_matrix, matrix 

from sage.geometry.cone import Cone 

 

 

 

class FanNotIsomorphicError(Exception): 

""" 

Exception to return if there is no fan isomorphism 

""" 

pass 

 

 

def fan_isomorphic_necessary_conditions(fan1, fan2): 

""" 

Check necessary (but not sufficient) conditions for the fans to be isomorphic. 

 

INPUT: 

 

- ``fan1``, ``fan2`` -- two fans. 

 

OUTPUT: 

 

Boolean. ``False`` if the two fans cannot be isomorphic. ``True`` 

if the two fans may be isomorphic. 

 

EXAMPLES:: 

 

sage: fan1 = toric_varieties.P2().fan() 

sage: fan2 = toric_varieties.dP8().fan() 

sage: from sage.geometry.fan_isomorphism import fan_isomorphic_necessary_conditions 

sage: fan_isomorphic_necessary_conditions(fan1, fan2) 

False 

""" 

if fan1.lattice_dim() != fan2.lattice_dim(): 

return False 

if fan1.dim() != fan2.dim(): 

return False 

if fan1.nrays() != fan2.nrays(): 

return False 

if fan1.ngenerating_cones() != fan2.ngenerating_cones(): 

return False 

if fan1.is_complete() != fan2.is_complete(): 

return False 

return True 

 

 

def fan_isomorphism_generator(fan1, fan2): 

""" 

Iterate over the isomorphisms from ``fan1`` to ``fan2``. 

 

ALGORITHM: 

 

The :meth:`sage.geometry.fan.Fan.vertex_graph` of the two fans is 

compared. For each graph isomorphism, we attempt to lift it to an 

actual isomorphism of fans. 

 

INPUT: 

 

- ``fan1``, ``fan2`` -- two fans. 

 

OUTPUT: 

 

Yields the fan isomorphisms as matrices acting from the right on 

rays. 

 

EXAMPLES:: 

 

sage: fan = toric_varieties.P2().fan() 

sage: from sage.geometry.fan_isomorphism import fan_isomorphism_generator 

sage: tuple( fan_isomorphism_generator(fan, fan) ) 

( 

[1 0] [0 1] [ 1 0] [ 0 1] [-1 -1] [-1 -1] 

[0 1], [1 0], [-1 -1], [-1 -1], [ 1 0], [ 0 1] 

) 

 

sage: m1 = matrix([(1, 0), (0, -5), (-3, 4)]) 

sage: m2 = matrix([(3, 0), (1, 0), (-2, 1)]) 

sage: m1.elementary_divisors() == m2.elementary_divisors() == [1,1,0] 

True 

sage: fan1 = Fan([Cone([m1*vector([23, 14]), m1*vector([ 3,100])]), 

....: Cone([m1*vector([-1,-14]), m1*vector([-100, -5])])]) 

sage: fan2 = Fan([Cone([m2*vector([23, 14]), m2*vector([ 3,100])]), 

....: Cone([m2*vector([-1,-14]), m2*vector([-100, -5])])]) 

sage: next(fan_isomorphism_generator(fan1, fan2)) 

[18 1 -5] 

[ 4 0 -1] 

[ 5 0 -1] 

 

sage: m0 = identity_matrix(ZZ, 2) 

sage: m1 = matrix([(1, 0), (0, -5), (-3, 4)]) 

sage: m2 = matrix([(3, 0), (1, 0), (-2, 1)]) 

sage: m1.elementary_divisors() == m2.elementary_divisors() == [1,1,0] 

True 

sage: fan0 = Fan([Cone([m0*vector([1,0]), m0*vector([1,1])]), 

....: Cone([m0*vector([1,1]), m0*vector([0,1])])]) 

sage: fan1 = Fan([Cone([m1*vector([1,0]), m1*vector([1,1])]), 

....: Cone([m1*vector([1,1]), m1*vector([0,1])])]) 

sage: fan2 = Fan([Cone([m2*vector([1,0]), m2*vector([1,1])]), 

....: Cone([m2*vector([1,1]), m2*vector([0,1])])]) 

sage: tuple(fan_isomorphism_generator(fan0, fan0)) 

( 

[1 0] [0 1] 

[0 1], [1 0] 

) 

sage: tuple(fan_isomorphism_generator(fan1, fan1)) 

( 

[1 0 0] [ -3 -20 28] 

[0 1 0] [ -1 -4 7] 

[0 0 1], [ -1 -5 8] 

) 

sage: tuple(fan_isomorphism_generator(fan1, fan2)) 

( 

[18 1 -5] [ 6 -3 7] 

[ 4 0 -1] [ 1 -1 2] 

[ 5 0 -1], [ 2 -1 2] 

) 

sage: tuple(fan_isomorphism_generator(fan2, fan1)) 

( 

[ 0 -1 1] [ 0 -1 1] 

[ 1 -7 2] [ 2 -2 -5] 

[ 0 -5 4], [ 1 0 -3] 

) 

""" 

if not fan_isomorphic_necessary_conditions(fan1, fan2): 

return 

 

graph1 = fan1.vertex_graph() 

graph2 = fan2.vertex_graph() 

graph_iso = graph1.is_isomorphic(graph2, edge_labels=True, certificate=True) 

if not graph_iso[0]: 

return 

graph_iso = graph_iso[1] 

 

# Pick a basis of rays in fan1 

max_cone = fan1(fan1.dim())[0] 

fan1_pivot_rays = max_cone.rays() 

fan1_basis = fan1_pivot_rays + fan1.virtual_rays() # A QQ-basis for N_1 

fan1_pivot_cones = [ fan1.embed(Cone([r])) for r in fan1_pivot_rays ] 

 

# The fan2 cones as set(set(ray indices)) 

fan2_cones = frozenset( 

frozenset(cone.ambient_ray_indices()) 

for cone in fan2.generating_cones() ) 

 

# iterate over all graph isomorphisms graph1 -> graph2 

for perm in graph2.automorphism_group(edge_labels=True): 

# find a candidate m that maps fan1_basis to the image rays under the graph isomorphism 

fan2_pivot_cones = [ perm(graph_iso[c]) for c in fan1_pivot_cones ] 

fan2_pivot_rays = fan2.rays([ c.ambient_ray_indices()[0] for c in fan2_pivot_cones ]) 

fan2_basis = fan2_pivot_rays + fan2.virtual_rays() 

try: 

m = matrix(ZZ, fan1_basis).solve_right(matrix(ZZ, fan2_basis)) 

m = m.change_ring(ZZ) 

except (ValueError, TypeError): 

continue # no solution 

 

# check that the candidate m lifts the vertex graph homomorphism 

graph_image_ray_indices = [ perm(graph_iso[c]).ambient_ray_indices()[0] for c in fan1(1) ] 

try: 

matrix_image_ray_indices = [ fan2.rays().index(r*m) for r in fan1.rays() ] 

except ValueError: 

continue 

if graph_image_ray_indices != matrix_image_ray_indices: 

continue 

 

# check that the candidate m maps generating cone to generating cone 

image_cones = frozenset( # The image(fan1) cones as set(set(integers) 

frozenset(graph_image_ray_indices[i] 

for i in cone.ambient_ray_indices()) 

for cone in fan1.generating_cones() ) 

if image_cones == fan2_cones: 

m.set_immutable() 

yield m 

 

 

def find_isomorphism(fan1, fan2, check=False): 

""" 

Find an isomorphism of the two fans. 

 

INPUT: 

 

- ``fan1``, ``fan2`` -- two fans. 

 

- ``check`` -- boolean (default: False). Passed to the fan 

morphism constructor, see 

:func:`~sage.geometry.fan_morphism.FanMorphism`. 

 

OUTPUT: 

 

A fan isomorphism. If the fans are not isomorphic, a 

:class:`FanNotIsomorphicError` is raised. 

 

EXAMPLES:: 

 

sage: rays = ((1, 1), (0, 1), (-1, -1), (3, 1)) 

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

sage: fan1 = Fan(cones, rays) 

 

sage: m = matrix([[-2,3],[1,-1]]) 

sage: m.det() == -1 

True 

sage: fan2 = Fan(cones, [vector(r)*m for r in rays]) 

 

sage: from sage.geometry.fan_isomorphism import find_isomorphism 

sage: find_isomorphism(fan1, fan2, check=True) 

Fan morphism defined by the matrix 

[-2 3] 

[ 1 -1] 

Domain fan: Rational polyhedral fan in 2-d lattice N 

Codomain fan: Rational polyhedral fan in 2-d lattice N 

 

sage: find_isomorphism(fan1, toric_varieties.P2().fan()) 

Traceback (most recent call last): 

... 

FanNotIsomorphicError 

 

sage: fan1 = Fan(cones=[[1,3,4,5],[0,1,2,3],[2,3,4],[0,1,5]], 

....: rays=[(-1,-1,0),(-1,-1,3),(-1,1,-1),(-1,3,-1),(0,2,-1),(1,-1,1)]) 

sage: fan2 = Fan(cones=[[0,2,3,5],[0,1,4,5],[0,1,2],[3,4,5]], 

....: rays=[(-1,-1,-1),(-1,-1,0),(-1,1,-1),(0,2,-1),(1,-1,1),(3,-1,-1)]) 

sage: fan1.is_isomorphic(fan2) 

True 

""" 

generator = fan_isomorphism_generator(fan1, fan2) 

try: 

m = next(generator) 

except StopIteration: 

raise FanNotIsomorphicError 

 

from sage.geometry.fan_morphism import FanMorphism 

return FanMorphism(m, domain_fan=fan1, codomain=fan2, check=check) 

 

 

def fan_2d_cyclically_ordered_rays(fan): 

""" 

Return the rays of a 2-dimensional ``fan`` in cyclic order. 

 

INPUT: 

 

- ``fan`` -- a 2-dimensional fan. 

 

OUTPUT: 

 

A :class:`~sage.geometry.point_collection.PointCollection` 

containing the rays in one particular cyclic order. 

 

EXAMPLES:: 

 

sage: rays = ((1, 1), (-1, -1), (-1, 1), (1, -1)) 

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

sage: fan = Fan(cones, rays) 

sage: fan.rays() 

N( 1, 1), 

N(-1, -1), 

N(-1, 1), 

N( 1, -1) 

in 2-d lattice N 

sage: from sage.geometry.fan_isomorphism import fan_2d_cyclically_ordered_rays 

sage: fan_2d_cyclically_ordered_rays(fan) 

N(-1, -1), 

N(-1, 1), 

N( 1, 1), 

N( 1, -1) 

in 2-d lattice N 

 

TESTS:: 

 

sage: fan = Fan(cones=[], rays=[], lattice=ZZ^2) 

sage: from sage.geometry.fan_isomorphism import fan_2d_cyclically_ordered_rays 

sage: fan_2d_cyclically_ordered_rays(fan) 

Empty collection 

in Ambient free module of rank 2 over the principal ideal domain Integer Ring 

""" 

assert fan.lattice_dim() == 2 

import math 

rays = [ (math.atan2(r[0],r[1]), r) for r in fan.rays() ] 

rays = [ r[1] for r in sorted(rays) ] 

from sage.geometry.point_collection import PointCollection 

return PointCollection(rays, fan.lattice()) 

 

 

def fan_2d_echelon_forms(fan): 

""" 

Return echelon forms of all cyclically ordered ray matrices. 

 

Note that the echelon form of the ordered ray matrices are unique 

up to different cyclic orderings. 

 

INPUT: 

 

- ``fan`` -- a fan. 

 

OUTPUT: 

 

A set of matrices. The set of all echelon forms for all different 

cyclic orderings. 

 

EXAMPLES:: 

 

sage: fan = toric_varieties.P2().fan() 

sage: from sage.geometry.fan_isomorphism import fan_2d_echelon_forms 

sage: fan_2d_echelon_forms(fan) 

frozenset({[ 1 0 -1] 

[ 0 1 -1]}) 

 

sage: fan = toric_varieties.dP7().fan() 

sage: list(fan_2d_echelon_forms(fan)) 

[ 

[ 1 0 -1 -1 1] [ 1 0 -1 -1 0] [ 1 0 -1 0 1] [ 1 0 -1 -1 0] 

[ 0 1 1 0 -1], [ 0 1 1 0 -1], [ 0 1 1 -1 -1], [ 0 1 0 -1 -1], 

<BLANKLINE> 

[ 1 0 -1 0 1] 

[ 0 1 0 -1 -1] 

] 

 

TESTS:: 

 

sage: rays = [(1, 1), (-1, -1), (-1, 1), (1, -1)] 

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

sage: fan1 = Fan(cones, rays) 

sage: from sage.geometry.fan_isomorphism import fan_2d_echelon_form, fan_2d_echelon_forms 

sage: echelon_forms = fan_2d_echelon_forms(fan1) 

sage: S4 = CyclicPermutationGroup(4) 

sage: rays.reverse() 

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

sage: for i in range(100): 

....: m = random_matrix(ZZ,2,2) 

....: if abs(det(m)) != 1: continue 

....: perm = S4.random_element() 

....: perm_cones = [ (perm(c[0]+1)-1, perm(c[1]+1)-1) for c in cones ] 

....: perm_rays = [ rays[perm(i+1)-1] for i in range(len(rays)) ] 

....: fan2 = Fan(perm_cones, rays=[m*vector(r) for r in perm_rays]) 

....: assert fan_2d_echelon_form(fan2) in echelon_forms 

 

The trivial case was fixed in :trac:`18613`:: 

 

sage: fan = Fan([], lattice=ToricLattice(2)) 

sage: fan_2d_echelon_forms(fan) 

frozenset({[]}) 

sage: parent(list(_)[0]) 

Full MatrixSpace of 2 by 0 dense matrices over Integer Ring 

""" 

if fan.nrays() == 0: 

return frozenset([fan_2d_echelon_form(fan)]) 

rays = list(fan_2d_cyclically_ordered_rays(fan)) 

echelon_forms = [] 

for i in range(2): 

for j in range(len(rays)): 

echelon_forms.append(column_matrix(rays).echelon_form()) 

first = rays.pop(0) 

rays.append(first) 

rays.reverse() 

return frozenset(echelon_forms) 

 

 

def fan_2d_echelon_form(fan): 

""" 

Return echelon form of a cyclically ordered ray matrix. 

 

INPUT: 

 

- ``fan`` -- a fan. 

 

OUTPUT: 

 

A matrix. The echelon form of the rays in one particular cyclic 

order. 

 

EXAMPLES:: 

 

sage: fan = toric_varieties.P2().fan() 

sage: from sage.geometry.fan_isomorphism import fan_2d_echelon_form 

sage: fan_2d_echelon_form(fan) 

[ 1 0 -1] 

[ 0 1 -1] 

""" 

ray_matrix = fan_2d_cyclically_ordered_rays(fan).column_matrix() 

return ray_matrix.echelon_form()