# Revision history [back]

The code:

X = [1,2,3,4,5]
R = [ [1,2], [2,3], [3,5], [1,4], [4,5] ]
P = Poset( (X,R) )

def str_prod_for_path( path ):
"""given a list / "path", e.g. [1,3,7,9,1991]
return something like x13*x37*x39*x91991 .
(Not our problem here to distinguish between [1,3,7,9,1991] and [1,3,7,91,991].)
(The caller insures len(path) >= 2.)
"""
return "*".join( [ "x%s%s" % ( path[k], path[k+1] ) for k in range(len(path)-1) ] )

def generate_gap_code( poset ):
"""The poset must be a poset of 1, 2, ... , n
where ...
"""
n = len( poset.list() )

covers = poset.cover_relations()
str1_Q = ( 'Q := Quiver( %s, [%s] );'
% ( n,
','.join( [ '[%s,%s,"x%s%s"]' % (j,k,j,k)
for j,k in covers ] )
) )
str2_kQ  = 'kQ := PathAlgebra( Rationals, Q );'
str3_AGV = 'AssignGeneratorVariables( kQ );'

all_chains  = [ ch for ch in poset.chains() if len(ch) >= 2 ]    # you possibly need only == 3
chains      = []    # and we append
for ch in all_chains:
ok = True
for k in range(len(ch)-1):
if [ ch[k], ch[k+1] ] not in covers:
ok = False
break    # the for k loop
if ok:
chains.append( ch )
chains_keys = [ (ch[0], ch[-1]) for ch in chains ]
chains_dic  = dict( [ (key, [ ch
for ch in chains
if  ch[0]==key[0] and ch[-1]==key[-1] ] )
for key in chains_keys
] )
chains_rels = []    # and we append
for key, paths in chains_dic.iteritems():
if len( paths ) <= 1 :    continue
str_prod_for_path0 = str_prod_for_path( paths[0] )
for k in range(1, len(paths)):
str_prod_for_pathk = str_prod_for_path( paths[k] )
chains_rels.append( "%s - %s" % ( str_prod_for_path0 ,
str_prod_for_pathk ) )
str4_rel = '[ %s ]' % ( ', '.join( chains_rels ) )
str5_A   = 'A := kQ/rel;'

return ( '\n'.join( [ str1_Q  ,
str2_kQ ,
str3_AGV,
str4_rel,
str5_A  ] ) )

print generate_gap_code( P )


produces

Q := Quiver( 5, [[1,2,"x12"],[1,4,"x14"],[2,3,"x23"],[3,5,"x35"],[4,5,"x45"]] );
kQ := PathAlgebra( Rationals, Q );
AssignGeneratorVariables( kQ );
[ x12*x23*x35 - x14*x45 ]
A := kQ/rel;


Comment:

In any comment to the above - if any - please describe first what should be installed in gap to make this or the to-be-final code work. Please give a relevant example with relations that make the difference, so that the lack of specification can be covered by common sense. It does not help to give an example with one relation from a link with an unlabeled picture, that is copied as a second relation. If the really needed posets have only quadratic relations, then specify it! You know what you need, but the question is so general, so that a potential helper gets problems where there aren't any. Is the following poset relevant?

  2
/ \
1   3---6
\   \   \
\   5---8
\ /   /
4---7


Which relations should be extracted from it? It may be, that

The code:

X = [1,2,3,4,5]
R = [ [1,2], [2,3], [3,5], [1,4], [4,5] ]
P = Poset( (X,R) )

def str_prod_for_path( path ):
"""given a list / "path", e.g. [1,3,7,9,1991]
return something like x13*x37*x39*x91991 .
(Not our problem here to distinguish between [1,3,7,9,1991] and [1,3,7,91,991].)
(The caller insures len(path) >= 2.)
"""
return "*".join( [ "x%s%s" % ( path[k], path[k+1] ) for k in range(len(path)-1) ] )

def generate_gap_code( poset ):
"""The poset must be a poset of 1, 2, ... , n
where ...
"""
n = len( poset.list() )

covers = poset.cover_relations()
str1_Q = ( 'Q := Quiver( %s, [%s] );'
% ( n,
','.join( [ '[%s,%s,"x%s%s"]' % (j,k,j,k)
for j,k in covers ] )
) )
str2_kQ  = 'kQ := PathAlgebra( Rationals, Q );'
str3_AGV = 'AssignGeneratorVariables( kQ );'

all_chains  = [ ch for ch in poset.chains() if len(ch) >= 2 ]    # you possibly need only == 3
chains      = []    # and we append
for ch in all_chains:
ok = True
for k in range(len(ch)-1):
if [ ch[k], ch[k+1] ] not in covers:
ok = False
break    # the for k loop
if ok:
chains.append( ch )
chains_keys = [ (ch[0], ch[-1]) for ch in chains ]
chains_dic  = dict( [ (key, [ ch
for ch in chains
if  ch[0]==key[0] and ch[-1]==key[-1] ] )
for key in chains_keys
] )
chains_rels = []    # and we append
for key, paths in chains_dic.iteritems():
if len( paths ) <= 1 :    continue
str_prod_for_path0 = str_prod_for_path( paths[0] )
for k in range(1, len(paths)):
str_prod_for_pathk = str_prod_for_path( paths[k] )
chains_rels.append( "%s - %s" % ( str_prod_for_path0 ,
str_prod_for_pathk ) )
str4_rel = '[ %s ]' % ( ', '.join( chains_rels ) )
str5_A   = 'A := kQ/rel;'

return ( '\n'.join( [ str1_Q  ,
str2_kQ ,
str3_AGV,
str4_rel,
str5_A  ] ) )

print generate_gap_code( P )


produces

Q := Quiver( 5, [[1,2,"x12"],[1,4,"x14"],[2,3,"x23"],[3,5,"x35"],[4,5,"x45"]] );
kQ := PathAlgebra( Rationals, Q );
AssignGeneratorVariables( kQ );
[ x12*x23*x35 - x14*x45 ]
A := kQ/rel;


Comment:

In any comment to the above - if any - please describe first what should be installed in gap to make this or the to-be-final code work. Please give a relevant example with relations that make the difference, so that the lack of specification can be covered by common sense. It does not help to give an example with one relation from a link with an unlabeled picture, that is copied as a second relation. If the really needed posets have only quadratic relations, then specify it! You know what you need, but the question is so general, so that a potential helper gets problems where there aren't any. Is the following poset relevant?

  2
/ \
1   3---6
\   \   \
\   5---8
\ /   /
4---7


Which relations should be extracted from it? It may be, that

    all_chains  = [ ch for ch in poset.chains() if len(ch) >= 2 ]    # you possibly need only == 3


should be simply:

    all_chains  = [ ch for ch in poset.chains() if len(ch) == 3 ]


The code:

X = [1,2,3,4,5]
R = [ [1,2], [2,3], [3,5], [1,4], [4,5] ]
P = Poset( (X,R) )

def str_prod_for_path( path ):
"""given a list / "path", e.g. [1,3,7,9,1991]
return something like x13*x37*x39*x91991 .
(Not our problem here to distinguish between [1,3,7,9,1991] and [1,3,7,91,991].)
(The caller insures len(path) >= 2.)
"""
return "*".join( [ "x%s%s" % ( path[k], path[k+1] ) for k in range(len(path)-1) ] )

def generate_gap_code( poset ):
"""The poset must be a poset of 1, 2, ... , n
where ...
"""
n = len( poset.list() )

covers = poset.cover_relations()
str1_Q = ( 'Q := Quiver( %s, [%s] );'
% ( n,
','.join( [ '[%s,%s,"x%s%s"]' % (j,k,j,k)
for j,k in covers ] )
) )
str2_kQ  = 'kQ := PathAlgebra( Rationals, Q );'
str3_AGV = 'AssignGeneratorVariables( kQ );'

all_chains  = [ ch for ch in poset.chains() if len(ch) >= 2 ]    # you possibly need only == 3
chains      = []    # and we append
for ch in all_chains:
ok = True
for k in range(len(ch)-1):
if [ ch[k], ch[k+1] ] not in covers:
ok = False
break    # the for k loop
if ok:
chains.append( ch )
chains_keys = [ (ch[0], ch[-1]) for ch in chains ]
chains_dic  = dict( [ (key, [ ch
for ch in chains
if  ch[0]==key[0] and ch[-1]==key[-1] ] )
for key in chains_keys
] )
chains_rels = []    # and we append
for key, paths in chains_dic.iteritems():
if len( paths ) <= 1 :    continue
str_prod_for_path0 = str_prod_for_path( paths[0] )
for k in range(1, len(paths)):
str_prod_for_pathk = str_prod_for_path( paths[k] )
chains_rels.append( "%s - %s" % ( str_prod_for_path0 ,
str_prod_for_pathk ) )
str4_rel = '[ 'rels := [ %s ]' % ( ', '.join( chains_rels ) )
str5_A   = 'A := kQ/rel;'
kQ/rels;'

return ( '\n'.join( [ str1_Q  ,
str2_kQ ,
str3_AGV,
str4_rel,
str5_A  ] ) )

print generate_gap_code( P )


produces

Q := Quiver( 5, [[1,2,"x12"],[1,4,"x14"],[2,3,"x23"],[3,5,"x35"],[4,5,"x45"]] );
kQ := PathAlgebra( Rationals, Q );
AssignGeneratorVariables( kQ );
rels := [ x12*x23*x35 - x14*x45 ]
A := kQ/rel;
kQ/rels;


Comment:

In any comment to the above - if any - please describe first what should be installed in gap to make this or the to-be-final code work. Please give a relevant example with relations that make the difference, so that the lack of specification can be covered by common sense. It does not help to give an example with one relation from a link with an unlabeled picture, that is copied as a second relation. If the really needed posets have only quadratic relations, then specify it! You know what you need, but the question is so general, so that a potential helper gets problems where there aren't any. Is the following poset relevant?

  2
/ \
1   3---6
\   \   \
\   5---8
\ /   /
4---7


Which relations should be extracted from it? It may be, that

    all_chains  = [ ch for ch in poset.chains() if len(ch) >= 2 ]    # you possibly need only == 3


should be simply:

    all_chains  = [ ch for ch in poset.chains() if len(ch) == 3 ]