Skip to content

Instantly share code, notes, and snippets.

@ernestyalumni
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ernestyalumni/7879af1ee1bc336af6c8 to your computer and use it in GitHub Desktop.
Save ernestyalumni/7879af1ee1bc336af6c8 to your computer and use it in GitHub Desktop.
homology.sage In Sage Math, implementation of simplicial complexes for computing chain complexes and homology cf. https://www.math.hmc.edu/~su/pcmi/projects/simplicial_module/simplicial_pcmi.pdf and J.M. Lee's Introduction to Topological Manifolds
## homology.sage
## This is my implementation of Algebraic Topology, Homology, Simplicial Complexes
## utilizing
## Sage Math
## from
##
## The main reference that I'll liberally copy from is from is
## Introduction to Topological Manifolds
## by J.M. Lee
################################################################################
## Copyleft 2015, Ernest Yeung <ernestyalumni@gmail.com>
##
## 20150515
##
## This program, along with all its code, is free software; you can redistribute
## it and/or modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the License,
## or (at your option) any later version.
##
## This program 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.
##
## You can have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software Foundation, Inc.,
## S1 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA
##
## Governing the ethics of using this program, I default to the Caltech
## Honor Code:
## ``No member of the Caltech community shall take unfair advantage of
## any other member of the Caltech community.''
##
## If you like what I'm doing and would like to help and contribute support,
## please take a look at my crowdfunding campaign at ernestyalumni.tilt.com
## and Patreon, read my mission statement and give your financial support,
## no matter how small or large, if you can and to keep checking my
## ernestyalumni.wordpress.com blog and various social media channels
## for updates as I try to keep putting out great stuff.
##
## Fund Science! & Help Ernest in Physics Research! :
## quantum super-A-polynomials - researched by Ernest Yeung
##
## ernestyalumni.tilt.com
##
## Facebook : ernestyalumni
## gmail : ernestyalumni
## google : ernestyalumni
## linkedin : ernestyalumni
## Patreon : ernestyalumni
## Tilt/Open : ernestyalumni
## tumblr : ernestyalumni
## twitter : ernestyalumni
## youtube : ernestyalumni
## wordpress : ernestyalumni
##
##
################################################################################
# cf. Sage Reference Manual: Cell complexes and their homology, Release 6.6
# homology.pdf
# Ch. 4 Finite Simplicial Complexes
closedinterval = SimplicialComplex((('a','b'), ['a','b']))
from itertools import combinations
def create_n_simplex_all(n=1,n_fill=1,n_0 = 0):
"""
create_n_simplex_all = create_n_simplex_all(n=1,n_fill,n_0=0)
Creates a n-simplex that has all possible combinations of the vertices for the faces
e.g. Example of usage
cinqsimpl = create_n_simplex_all(5,5)
cinqsimpl.cells()
cinqsimpl.dimension() # 5
cinqsimpl.euler_characteristic() # 1
sage: len(create_n_simplex_all(5,5).cells()[0])
6
sage: len(create_n_simplex_all(5,5).cells()[1])
15
sage: len(create_n_simplex_all(5,5).cells()[2])
20
sage: len(create_n_simplex_all(5,5).cells()[3])
15
sage: len(create_n_simplex_all(5,5).cells()[4])
6
sage: len(create_n_simplex_all(5,5).cells()[5])
1
create_n_simplex_all(2,2,1)
"""
assert n>=n_0
assert n>=n_fill
vertices = [ (i,) for i in range(n_0,n+1+n_0)]
arg = vertices+[combo for i in range(2,n_fill+2) for combo in [I for I in combinations(range(n_0,n+1+n_0),i)]]
return SimplicialComplex(arg)
def create_thin_n_polygon(n=1,n_0 = 0):
"""
create_thin_n_polygon = create_thin_n_polygon(n=1,n_0=0)
Creates a 1-simplex that's path connected and is really just a n-polygon without any filling
e.g. Example of usage
create_thin_n_polygon(2).homology() # {0: 0, 1: Z}
sage: create_thin_n_polygon(3).homology() # {0: 0, 1: Z}
sage: create_thin_n_polygon(5).homology() # {0: 0, 1: Z}
# as expected
"""
assert n>=n_0
vertices = [ (i,) for i in range(n_0,n+1)]
arg = vertices + [(i,i+1) if i < n else (i,n_0) for i in range(n_0,n+1) ]
return SimplicialComplex(arg)
# cf. https://www.math.hmc.edu/~su/pcmi/projects/simplicial_module/simplicial_pcmi.pdf
# Demonstrating Simplicial Homology, (augmented) chain complexes, and ith reduced homology pp. 2 of the pdf, the closed interval and hollow triangle
# closed interval
create_thin_n_polygon(1)
create_thin_n_polygon(1).faces() # {-1: {()}, 0: {(0,), (1,)}, 1: {(0, 1)}}
create_thin_n_polygon(1).chain_complex()
create_thin_n_polygon(1).chain_complex().differential()
# {0: [], 1: [-1]
# [ 1], 2: []}
create_thin_n_polygon(2).chain_complex().differential()
create_thin_n_polygon(2).chain_complex().differential()[1]
ascii_art( create_thin_n_polygon(2).chain_complex() )
Matrix(create_thin_n_polygon(2).chain_complex().differential()[1]).rank() # 2
Matrix(create_thin_n_polygon(2).chain_complex().differential()[1])
# cf. https://www.math.hmc.edu/~su/pcmi/projects/simplicial_module/simplicial_pcmi.pdf
# Example on [5] which is 5 vertices
hmcExample5 = create_n_simplex_all(2,2,1)
for face in create_thin_n_polygon(4,2).faces()[1]:
hmcExample5.add_face(face)
hmcExample5.add_face((5,))
hmcExample5.homology() # sage: X.homology() # {0: Z, 1: Z, 2: 0}
hmcExample5.chain_complex().differential() # {0: [], 1: [ 1 1 0 0 0]
# [ 0 0 0 0 0]
# [ 0 -1 1 1 0]
# [ 0 0 0 -1 -1]
# [-1 0 -1 0 1], 2: [ 0]
# [ 0]
# [ 1]
# [-1]
# [ 1], 3: []}
hmcExample5.chain_complex().differential()[1].rank() # 3
##
# 2-Sphere
# from built-in 2-Sphere simplicial complex for Sage Math
##
simplicial_complexes.Sphere(2).faces()
simplicial_complexes.Sphere(2).homology()
scS2 = simplicial_complexes.Sphere(2)
scS2.chain_complex().differential()
scS2.chain_complex().differential()[2].rank() # 3
scS2.chain_complex().differential()[3].rank() # 3
# Exercise 5.
# cf. https://www.math.hmc.edu/~su/pcmi/projects/simplicial_module/simplicial_pcmi.pdf
hmcExercise5 = SimplicialComplex( [(i,) for i in range(1,6)] + [(1,3),(1,4),(3,4),
(1,5),(4,5),(1,6),(3,6),(2,3),(2,4),
(2,5),(2,6),(5,6)] +
[
(1,4,5),(1,3,6),(2,3,4),
(2,4,5),(1,5,6),(2,3,6),
(2,5,6),
(1,3,4)] )
hmcExercise5.homology() # {0: 0, 1: 0, 2: Z}
hmcExercise5.homology() == scS2.homology() # True
# Exercise 6
# cf. https://www.math.hmc.edu/~su/pcmi/projects/simplicial_module/simplicial_pcmi.pdf
# Using the built-in Torus simplicial complex minimal triangulation
simplicial_complexes.Torus().homology() # {0: 0, 1: Z x Z, 2: Z}
hmcExercise6 = SimplicialComplex( [(i,) for i in range(1,10)] +
[
(1,2),(1,3),(2,3),
(1,4),(1,5),(4,5),
(1,6),(1,7),(2,6),(2,7),(3,7),
(1,9),(4,6),(4,9),(5,9),
(1,8),(2,9),(3,8),(3,9),
(4,7),(5,7),(5,8),
(6,7),(6,9),(7,8),(8,9),
(6,8)] +
[
(1,4,7),(1,3,7),(2,3,7),(2,6,7),(1,2,6),(1,4,6),
(4,6,9),(4,5,9),(1,2,9),(1,5,9),
(2,3,9),(3,8,9),(1,3,8),(1,5,8),
(4,5,7),(5,7,8),
(6,7,8),(6,8,9)
]
)
hmcExercise6.homology() # {0: 0, 1: Z x Z, 2: Z}
# Exercise 7
simplicial_complexes.KleinBottle().homology() # {0: 0, 1: Z x C2, 2: 0}
simplicial_complexes.RealProjectiveSpace(2)
simplicial_complexes.RealProjectiveSpace(2).homology()
simplicial_complexes.RealProjectivePlane()
simplicial_complexes.RealProjectivePlane().homology()
simplicial_complexes.ProjectivePlane()
simplicial_complexes.ProjectivePlane().homology()
hmcExercise7_Kleinbottle = SimplicialComplex( [(i,) for i in range(1,10)] +
[
(1,2),(1,3),(2,3),
(1,4),(1,5),(4,5),
(1,6),(1,7),(2,6),(2,7),(3,7),
(1,9),(4,6),(4,9),(5,9),
(1,8),(2,9),(2,8),(3,9),
(4,7),(5,7),(5,8),
(6,7),(6,9),(7,8),(8,9),
(6,8)] +
[
(1,4,7),(1,3,7),(2,3,7),(2,6,7),(1,2,6),(1,4,6),
(4,6,9),(4,5,9),(1,3,9),(1,5,9),
(2,3,9),(2,8,9),(1,2,8),(1,5,8),
(4,5,7),(5,7,8),
(6,7,8),(6,8,9)
]
)
hmcExercise7_Kleinbottle.homology()
hmcExercise7_Kleinbottle.chain_complex().differential()
hmcExercise7_RP2 = SimplicialComplex( [(i,) for i in range(1,10)] +
[
(1,2),(1,3),(2,3),
(1,4),(1,5),(4,5),
(1,6),(1,7),(2,6),(2,7),(3,7),
(1,9),(4,6),(4,9),(5,9),
(1,8),(2,9),(2,8),(3,9),
(4,7),(5,7),(4,8),
(6,7),(6,9),(7,8),(8,9),
(6,8)] +
[
(1,5,7),(1,3,7),(2,3,7),(2,6,7),(1,2,6),(1,4,6),
(4,6,9),(4,5,9),(1,3,9),(1,5,9),
(2,3,9),(2,8,9),(1,2,8),(1,4,8),
(4,5,7),(4,7,8),
(6,7,8),(6,8,9)
]
)
hmcExercise7_RP2.homology()
hmcExercise7_RP2.chain_complex().differential()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment