Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi Pendavingh committed Jun 20, 2015
1 parent cf36955 commit a04a1bb
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 12 deletions.
120 changes: 120 additions & 0 deletions src/sage/matroids/linear_matroid.pyx
Expand Up @@ -4442,6 +4442,66 @@ cdef class TernaryMatroid(LinearMatroid):
"""
return True

# representability

cpdef ternary_matroid(self, randomized_tests=1, verify = True):
r"""
Return a ternary matroid representing ``self``.
INPUT:
- ``randomized_tests`` -- Ignored.
- ``verify`` -- Ignored
OUTPUT:
A binary matroid.
ALGORITHM:
``self`` is a ternary matroid, so just return ``self``.
.. SEEALSO::
:meth:`M.ternary_matroid()
<sage.matroids.matroid.Matroid.ternary_matroid>`
EXAMPLES::
sage: N = matroids.named_matroids.NonFano()
sage: N.binary_matroid() is N
True
"""
return self

cpdef is_ternary(self, randomized_tests=1):
r"""
Decide if ``self`` is a binary matroid.
INPUT:
- ``randomized_tests`` -- Ignored.
OUTPUT:
A Boolean.
ALGORITHM:
``self`` is a ternary matroid, so just return ``True``.
.. SEEALSO::
:meth:`M.is_ternary() <sage.matroids.matroid.Matroid.is_ternary>`
EXAMPLES::
sage: N = matroids.named_matroids.NonFano()
sage: N.is_ternary()
True
"""
return True

def __copy__(self):
"""
Create a shallow copy.
Expand Down Expand Up @@ -5981,6 +6041,66 @@ cdef class RegularMatroid(LinearMatroid):
"""
return True

cpdef ternary_matroid(self, randomized_tests=1, verify = True):
r"""
Return a ternary matroid representing ``self``.
INPUT:
- ``randomized_tests`` -- Ignored.
- ``verify`` -- Ignored
OUTPUT:
A ternary matroid.
ALGORITHM:
``self`` is a regular matroid, so just cast ``self`` to a TernaryMatroid.
.. SEEALSO::
:meth:`M.ternary_matroid()
<sage.matroids.matroid.Matroid.ternary_matroid>`
EXAMPLES::
sage: N = matroids.named_matroids.R10()
sage: N.ternary_matroid()
Ternary matroid of rank 5 on 10 elements, type 4+
"""
A, E = self.representation(B = self.basis(), reduced = False, labels = True)
return TernaryMatroid(matrix = A, groundset = E)

cpdef is_ternary(self, randomized_tests=1):
r"""
Decide if ``self`` is a ternary matroid.
INPUT:
- ``randomized_tests`` -- Ignored.
OUTPUT:
A Boolean.
ALGORITHM:
``self`` is a regular matroid, so just return ``True``.
.. SEEALSO::
:meth:`M.is_ternary() <sage.matroids.matroid.Matroid.is_ternary>`
EXAMPLES::
sage: N = matroids.named_matroids.R10()
sage: N.is_ternary()
True
"""
return True

# Copying, loading, saving

def __copy__(self):
Expand Down
3 changes: 3 additions & 0 deletions src/sage/matroids/matroid.pxd
Expand Up @@ -146,6 +146,9 @@ cdef class Matroid(SageObject):
cpdef _local_binary_matroid(self, basis=*)
cpdef binary_matroid(self, randomized_tests=*, verify=*)
cpdef is_binary(self, randomized_tests=*)
cpdef _local_ternary_matroid(self, basis=*)
cpdef ternary_matroid(self, randomized_tests=*, verify=*)
cpdef is_ternary(self, randomized_tests=*)

# matroid k-closed
cpdef is_k_closed(self, int k)
Expand Down
34 changes: 22 additions & 12 deletions src/sage/matroids/matroid.pyx
Expand Up @@ -5129,38 +5129,48 @@ cdef class Matroid(SageObject):
"""
if basis is None:
basis = self.basis()
basis = list(basis)
bdx = {basis[i]:i for i in range(len(B))}
E = list(self.groundset())
basis = sorted(basis)
bdx = {basis[i]:i for i in range(len(basis))}
E = sorted(self.groundset())
idx = { E[i]:i for i in range(len(E)) }
A = TernaryMatrix(len(basis), len(E))
for e in basis:
A.set(bdx[e], idx[e],1)
entries = [(e, f) for e in basis for f in self._fundamental_cocircuit(basis, e).difference([e])]
G = Graph(edges = entries)
A.set(bdx[e], idx[e], 1)
entries = [(e, f, (e,f)) for e in basis for f in self._fundamental_cocircuit(basis, e).difference([e])]
G = Graph(entries)
T = G.min_spanning_tree()
for edge in T:
A.set(bdx[edge[0]],idx[edge[1]],1)
e,f = edge[2]
A.set(bdx[e],idx[f], 1)
W = set(G.edges()) - set(T)
H = G.subgraph(edges = T)
while W:
edge = W.pop()
path = H.shortest_path(edge[0], edge[1])
e,f = edge[2]
path = H.shortest_path(e, f)
retry = True
while retry:
#print edge, path
retry = False
for edge2 in W:
if edge2[0] in path and edge2[1] in path:
W.add(edge)
edge = edge2
W.remove(edge)
path = H.shortest_path(edge[0], edge[1])
e,f = edge[2]
path = H.shortest_path(e, f)
retry = True
break
if (len(path) % 4 == 0) is not self.is_independent(basis.symmetric_difference(path)):
A.set(bdx[edge[0]],idx[edge[1]],1)
x = 1
for i in range(len(path)-1):
if i%2 == 0:
x = x * A.get(bdx[path[i]], idx[path[i+1]])
else:
x = x * A.get(bdx[path[i+1]], idx[path[i]])
if (len(path) % 4 == 0) == self.is_dependent(set(basis).symmetric_difference(path)):
A.set(bdx[e],idx[f],x)
else:
A.set(bdx[edge[0]],idx[edge[1]],-1)
A.set(bdx[e],idx[f],-x)
H.add_edge(edge)
from sage.matroids.linear_matroid import TernaryMatroid
return TernaryMatroid(groundset=E, matrix=A, basis=basis, keep_initial_representation=False)
Expand Down

0 comments on commit a04a1bb

Please sign in to comment.