Skip to content

Commit

Permalink
pathlib in simplicial sets examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Sep 9, 2024
1 parent 24698e7 commit dda7e93
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/sage/topology/simplicial_set_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# ****************************************************************************

import re
import os
from pathlib import Path

from sage.env import SAGE_ENV
from sage.misc.cachefunc import cached_method, cached_function
Expand All @@ -48,6 +48,8 @@
from sage.misc.lazy_import import lazy_import
lazy_import('sage.categories.simplicial_sets', 'SimplicialSets')

kenzo_path = Path(SAGE_ENV['SAGE_EXTCODE']) / 'kenzo'


# ######################################################################
# The nerve of a finite monoid, used in sage.categories.finite_monoid.
Expand Down Expand Up @@ -96,7 +98,7 @@ def __init__(self, monoid):
# of monoid elements). Omit the base point.
self._simplex_data = ()

def __eq__(self, other):
def __eq__(self, other) -> bool:
"""
Return ``True`` if ``self`` and ``other`` are equal.
Expand All @@ -119,7 +121,7 @@ def __eq__(self, other):
and self._monoid == other._monoid
and self.base_point() == other.base_point())

def __ne__(self, other):
def __ne__(self, other) -> bool:
"""
Return the negation of `__eq__`.
Expand Down Expand Up @@ -214,13 +216,13 @@ def n_skeleton(self, n):
face_dict[(g,)] = x
start = 1

for d in range(start+1, n+1):
for d in range(start + 1, n + 1):
for g in monoid:
if g == one:
continue
new_faces = {}
for t in face_dict.keys():
if len(t) != d-1:
if len(t) != d - 1:
continue
# chain: chain of group elements to multiply,
# as a tuple.
Expand All @@ -235,8 +237,8 @@ def n_skeleton(self, n):

# Compute faces of x.
faces = [face_dict[chain[1:]]]
for i in range(d-1):
product = chain[i] * chain[i+1]
for i in range(d - 1):
product = chain[i] * chain[i + 1]
if product == one:
# Degenerate.
if d == 2:
Expand Down Expand Up @@ -294,11 +296,11 @@ def Sphere(n):
w_0 = AbstractSimplex(0, name='w_0')
return SimplicialSet_finite({v_0: None, w_0: None}, base_point=v_0,
name='S^0')
degens = range(n-2, -1, -1)
degens = range(n - 2, -1, -1)
degen_v = v_0.apply_degeneracies(*degens)
sigma = AbstractSimplex(n, name='sigma_{}'.format(n),
latex_name='\\sigma_{}'.format(n))
return SimplicialSet_finite({sigma: [degen_v] * (n+1)}, base_point=v_0,
return SimplicialSet_finite({sigma: [degen_v] * (n + 1)}, base_point=v_0,
name='S^{}'.format(n),
latex_name='S^{{{}}}'.format(n))

Expand Down Expand Up @@ -616,22 +618,20 @@ def ComplexProjectiveSpace(n):
latex_name='CP^{2}')
return K
if n == 3:
file = os.path.join(SAGE_ENV['SAGE_EXTCODE'], 'kenzo', 'CP3.txt')
file = kenzo_path / 'CP3.txt'
data = simplicial_data_from_kenzo_output(file)
v = [_ for _ in data.keys() if _.dimension() == 0][0]
K = SimplicialSet_finite(data, base_point=v, name='CP^3',
latex_name='CP^{3}')
return K
v = [sigma for sigma in data if sigma.dimension() == 0][0]
return SimplicialSet_finite(data, base_point=v, name='CP^3',
latex_name='CP^{3}')
if n == 4:
file = os.path.join(SAGE_ENV['SAGE_EXTCODE'], 'kenzo', 'CP4.txt')
file = kenzo_path / 'CP4.txt'
data = simplicial_data_from_kenzo_output(file)
v = [_ for _ in data.keys() if _.dimension() == 0][0]
K = SimplicialSet_finite(data, base_point=v, name='CP^4',
latex_name='CP^{4}')
return K
v = [sigma for sigma in data if sigma.dimension() == 0][0]
return SimplicialSet_finite(data, base_point=v, name='CP^4',
latex_name='CP^{4}')


def simplicial_data_from_kenzo_output(filename):
def simplicial_data_from_kenzo_output(filename) -> dict:
"""
Return data to construct a simplicial set, given Kenzo output.
Expand All @@ -649,7 +649,8 @@ def simplicial_data_from_kenzo_output(filename):
sage: from sage.topology.simplicial_set_examples import simplicial_data_from_kenzo_output
sage: from sage.topology.simplicial_set import SimplicialSet
sage: sphere = os.path.join(SAGE_ENV['SAGE_EXTCODE'], 'kenzo', 'S4.txt')
sage: from pathlib import Path
sage: sphere = Path(SAGE_ENV['SAGE_EXTCODE']) / 'kenzo' /'S4.txt'
sage: S4 = SimplicialSet(simplicial_data_from_kenzo_output(sphere)) # needs pyparsing
sage: S4.homology(reduced=False) # needs pyparsing
{0: Z, 1: 0, 2: 0, 3: 0, 4: Z}
Expand All @@ -667,7 +668,7 @@ def simplicial_data_from_kenzo_output(filename):
dim_idx = data.find('Dimension = {}:'.format(dim), start)
while dim_idx != -1:
start = dim_idx + len('Dimension = {}:'.format(dim))
new_dim_idx = data.find('Dimension = {}:'.format(dim+1), start)
new_dim_idx = data.find('Dimension = {}:'.format(dim + 1), start)
if new_dim_idx == -1:
end = len(data)
else:
Expand Down

0 comments on commit dda7e93

Please sign in to comment.