Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reworked logic
r"""
Link class
"""
#*****************************************************************************
# Copyright (C) 2014
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.groups.free_group import FreeGroupElement
from sage.groups.braid import Braid
from sage.matrix.constructor import matrix
from sage.rings.integer_ring import ZZ
class Link:
r"""
The base class for Link, taking input in three formats namely Briadword, gauss_code, dt_code
"""
def __init__(self, input = None, gauss_code = None, dt_code = None):
if type(input) == Braid:
self._braid = input
self._gauss_code = None
self._dt_code = None
elif gauss_code != None:
self._braid = None
self._gauss_code = gauss_code
self._dt_code = None
elif dt_code != None:
self._braid = None
self._gauss_code = None
self._dt_code = dt_code
else:
raise Exception("Invalid input")
def braidword(self):
r"""
Returns the braidword
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Braidword representation of the INPUT
EXAMPLES::
"""
if self._braid != None:
return list(self._braid.Tietze())
elif self._gauss_code != None:
return "Not implemented Error"
elif self._dt_code != None:
return "Not Implemented Error"
def braid(self):
r"""
Returns the braid
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Braid representation of the INPUT
EXAMPLES::
"""
if self._braid != None:
return self._braid
elif self._gauss_code != None:
return "Not implemented Error"
elif self._dt_code != None:
return "Not Implemented Error"
def gauss_code(self):
r"""
Returns the gauss_code
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Gauss code representation of the INPUT
EXAMPLES::
"""
if self._gauss_code != None:
return self._gauss_code
elif self._braid != None:
return "Not Implemented Error"
elif self._dt_code != None:
return "Not Implemented Error"
def dt_code(self):
r"""
Returns the dt_code
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- DT Code representation of the INPUT
EXAMPLES::
"""
if self._dt_code != None:
return self._dt_code
elif self._braid != None:
return "Not Implemented Error"
elif self._gauss_code != None:
return "Not Implemented Error"
def _braidwordcomponents(self):
r"""
Returns the braid components in an array
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Array containing the components is returned
EXAMPLES::
sage: from sage.knots import link
sage: B = BraidGroup(4)
sage: L = link.Link(B([-1, 3, 1, 3]))
sage: L._braidwordcomponents()
[[-1, 1], [3, 3]]
"""
b = self.braid()
ml = list(b.Tietze())
l = list(set([abs(k) for k in ml]))
sorting1 = list(set(range(min(l),max(l)+1)) - set(l))
sorting = list(set(range(min(l),max(l)+1)) - set(sorting1))
missing = list(set(range(sorting[0],sorting[-1]+1)) - set(sorting))
if len(missing) == 0:
return ml
else:
x = [[] for i in range(len(missing) + 1)]
for i in range(len(missing)):
for j in range(len(ml)):
if(ml[j] != 0 and abs(ml[j]) < missing[i]):
x[i].append(ml[j])
ml[j] = 0
elif(ml[j] != 0 and ml[j] > missing[-1]):
x[-1].append(ml[j])
ml[j] = 0
y2 = [x for x in x if x != []]
return y2
def _braidwordcomponentsvector(self):
r"""
From braidwordcomponents it is converted to a vector
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Vector containing non-zero values
EXAMPLES::
sage: from sage.knots import link
sage: B = BraidGroup(4)
sage: L = link.Link(B([-1, 3, 1, 3]))
sage: L._braidwordcomponentsvector()
[-1, 1, 3, 3]
"""
y2 = self._braidwordcomponents()
if len(y2) == len(self.braid().Tietze()):
return y2
else:
y3 = []
for i in range(len(y2)):
y = y2[i]
for j in range(len(y)):
y3.append(y[j])
return y3
def homology_generators(self):
r"""
Returns the homology generators
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- The homology generators relating to the braid word representation
EXAMPLES::
sage: from sage.knots import link
sage: B = BraidGroup(4)
sage: L = link.Link(B([-1, 3, 1, 3]))
sage: L.homology_generators()
[1, 0, 3]
"""
x4 = self._braidwordcomponentsvector()
hom_gen = []
for j in range(len(x4)-1):
a = abs(x4[j])
for i in range(j+1, len(x4)):
if(a == abs(x4[i])):
hom_gen.append(i)
break
else:
hom_gen.append(0)
return hom_gen
def Seifert_Matrix(self):
r"""
Returns the Seifert Matrix
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Returns the Seifert Matrix of the link.
EXAMPLES::
sage: from sage.knots import link
sage: B = BraidGroup(4)
sage: L = link.Link(B([-1, 3, 1, 3]))
sage: L.Seifert_Matrix()
[ 0 0]
[ 0 -1]
"""
x5 = self._braidwordcomponentsvector()
h = self.homology_generators()
hl = len(h)
A = matrix(ZZ, hl, hl)
for i in range(hl):
if h[i] != 0:
for j in range(i,hl):
if i == j:
A[i,j] = -cmp((x5[i] + x5[h[i]]),0)
elif (h[i] > h[j]):
A[i,j] = 0
A[j,i] = 0
elif (h[i] < j):
A[i,j] = 0
A[j,i] = 0
elif (h[i] == j):
if(x5[j] > 0):
A[i,j] = 0
A[j,i] = 1
else:
A[i,j] = -1
A[j,i] = 0
elif abs(abs(x5[i]) - abs(x5[j])) > 1:
A[i,j] = 0
elif (abs(x5[i]) - abs(x5[j]) == 1):
A[i,j] = 0
A[j,i] = -1
elif (abs(x5[j])- abs(x5[i]) == 1):
A[i,j] = 1
A[j,i] = 0
else: #debug
A[i,j] = 2
A[j,i] = 2
else:
for k in range(hl):
A[k,i] = 0
A[i,k] = 0
k = []
for i in range(hl):
if h[i] == 0:
k.append(i)
for i in reversed(k):
A = A.delete_rows([i])
A = A.delete_columns([i])
return A
def link_number(self):
r"""
Returns the link number
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Link number of the link
EXAMPLES::
sage: from sage.knots import link
sage: B = BraidGroup(4)
sage: L = link.Link(B([-1, 3, 1, 3]))
sage: L.link_number()
4
"""
count = 0
b = self.braid().permutation()
c = [0 for i in range(len(b))]
for i in range(len(b)):
if c[i] == 0:
c[i] = 1
k = b[i]
while((k-1) != i):
c[(k-1)] = 1
k = b[k-1]
count = count + 1
return count
def smallest_equivalent(self):
r"""
Returns the braidword
INPUT:
- Either a braidword, gauss_code, dt_code
OUTPUT:
- Smallest equivalent of the given braid word representation.
EXAMPLES::
sage: from sage.knots import link
sage: B = BraidGroup(4)
sage: L = link.Link(B([-1, 3, 1, 3]))
sage: L.smallest_equivalent()
[-1, 3, 1, 3]
"""
b = list(self.braid().Tietze())
b1 = min([abs(k) for k in b])
for i in range(len(b)):
if b[i] > 0:
b[i] = b[i] - b1 + 1
else:
b[i] = b[i] + b1 - 1
return b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment