Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env sage -python
- import sys
- from sage.all import *
- #Let Mi = (Si,Ii)
- #Matroid Union M = M1 v M2 v M3 v ... v Mk is defined as (S1 U S2 U S3 U ... U Sk,I1 v I2 v I3 v ... v Ik )
- #where I1 v I2 v I3 v ... v Ik = {i1 U i2 U i3 U ... U ik | ij \in Ij }
- def MatroidUnion(*matroids):
- #Checking if the arguments given are valid
- if len(matroids) < 2:
- raise ValueError("Atleast two arguments expected")
- for i in range(len(matroids)):
- if not (isinstance(matroids[i],sage.matroids.matroid.Matroid) and matroids[i].is_valid()):
- raise ValueError("Argument "+str(i+1)+" doesn't seem to be a matroid")
- # if not (matroids[i].base_ring == ZZ or matroids[i].base_ring().is_field()):
- # raise NotImplementedError("MatroidUnion is only implemented for fields or integer ring")
- #Creating a matroid using the independent sets
- prev_ind_sets = set([])
- prev_matroid = matroids[0]
- rank = prev_matroid.rank()
- for r in range(rank+1):
- for it in prev_matroid.independent_r_sets(r):
- prev_ind_sets.add(it) #Adding bases of first matroid
- temp_ind_sets = set([]) #Using Set to eliminate duplicates
- index=1
- while index < len(matroids):
- curr_matroid = matroids[index]
- rank = curr_matroid.rank()
- for r in range(rank+1):
- for it in curr_matroid.independent_r_sets(r):
- for s in prev_ind_sets:
- n = frozenset(set(s).union(it))
- temp_ind_sets.add(n)
- prev_ind_sets=prev_ind_sets.union(temp_ind_sets)
- index = index + 1
- M = Matroid(independent_sets=prev_ind_sets)
- return M
- ##Testing
- # M1=matroids.named_matroids.Fano()
- # M2=matroids.named_matroids.Vamos()
- # M=MatroidUnion(M1,M2)
Advertisement
Add Comment
Please, Sign In to add comment