Ask Your Question
0

Pb with headers in table()

asked 2021-01-07 09:39:32 +0100

Cyrille gravatar image

updated 2021-01-07 16:07:45 +0100

tmonteil gravatar image

Here is the forgotten function which list all the preferences on a list of candidates

def All_pref(cand=["A","B","C","D"],code=1) :
    ncand=len(cand)
    Scand=sorted(Set(cand))
    all_pref=Arrangements(Scand,ncand).list()
    all_pref1=[str(Word(x)) for x in all_pref]
    if code==1 :
        return ncand
    if code==2 :
        return all_pref1

I have a problem with the following function :

# Borda(cand=["A","B","C","D"],ne=[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],cond=0)
# cond = 0 redonne la répartition des préférences
# cond = 1 donne le classement des candidats sur chaque type de préférences
# cond = 2 donne les scores sur chaque candidat
# cond = 2 donne l'accumulation des scores sur chaque candidat
# cond = 3 déclare le vainqueur au sens de Borda.
def Borda(cand=["A","B","C","D"],ne=[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],cond=0) :
    Ap=All_pref(cand,2) 
    cond1=['$\\tiny${}{}{}{}$'.format(*w) for w in Ap]
    cond2 =[""]+ [r'${}$'.format(*w) for w in cand]
    cond3 =[r'${}$'.format(*w) for w in cand]
    t1=table([ne])#,header_row=cond1)
    rank=[[x.find(l) for x in Ap] for l in cand] 
    score_de_Borda=[[abs(x.find(l)-len(cand)) for x in Ap] for l in cand]
    t2=table(rank)#,header_row=cond1,header_column=cond2)
    total_score_de_Borda=[add([score_de_Borda[j][i]*ne[i] for i in range(24)]) for j in range(len(cand))]
    t3=table(score_de_Borda)#,header_column=cond3)
    max_total_score_de_Borda=max(total_score_de_Borda)
    ind_max_total_score_de_Borda=total_score_de_Borda.index(max_total_score_de_Borda)
    vainqueur_de_Borda=cand[ind_max_total_score_de_Borda] 
    if cond==0 :
        return t1
    if cond==1 :
        return t2
    if cond==2 :
        return t3
    if cond==3 :
        return total_score_de_Borda
    if cond==4 :
        return vainqueur_de_Borda

and here is a call to the different variations.

show(LatexExpr(r"\text{Le tableau des rangs est :}"))
show(Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],1))
show(LatexExpr(r"\text{Le tableau des scores est :}"))
show(Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],2))
show(LatexExpr(r"\text{Les scores totaux de chaque candidat sont :}"))
Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],3)
show(LatexExpr(r"\text{Le vainqueur d'un décompte à la Borda est : }"),
Borda(["A","B","C","D"],[18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5],4),LatexExpr(r"."))

All is fine but if inside the function i remove the# and allow the header(s), I have an error which I do not understand since some time it works and some time not. (I am working with sagemath 9.2 under windows).

edit retag flag offensive close merge delete

Comments

Some code is missiing:

NameError: name 'All_pref' is not defined

What 'All_pref' ?

tmonteil gravatar imagetmonteil ( 2021-01-07 12:35:28 +0100 )edit

Sorry, my notebook become hudge because I am trying to compute election result from all systems.

Cyrille gravatar imageCyrille ( 2021-01-07 15:10:08 +0100 )edit

In any case, when you have some issue, you should provide a minimal example showing the problem.

tmonteil gravatar imagetmonteil ( 2021-01-07 16:04:51 +0100 )edit

It works for me, i can not reproduce you issue when i remove the two #

tmonteil gravatar imagetmonteil ( 2021-01-07 16:07:36 +0100 )edit

I will try with an other computer. But the main thing is that it works for you.

Cyrille gravatar imageCyrille ( 2021-01-07 17:15:08 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-01-07 20:02:44 +0100

Juanjo gravatar image

I think you can greatly simplify your code:

  • use f-strings instead of the format method;
  • use HTML instead of LaTeX to format headers, since elements in headers are not math expressions;
  • place conditionals at proper places, not at the end.

This works for me in a Jupyter notebook:

def All_pref(cand=["A","B","C","D"], code=1) :
    ncand = len(cand)
    Scand = sorted(Set(cand))
    all_pref = Arrangements(Scand,ncand).list()
    all_pref1 = [str(Word(x)) for x in all_pref]
    if code==1 :
        return ncand
    if code==2 :
        return all_pref1

def Borda(cand, ne, cond=0) :
    Ap=All_pref(cand,2) 
    cond1 = [w for w in Ap]
    cond3 = [f"<em>{w}</em>" for w in cand]
    cond2 = [""] + cond3
    if cond==0 :
        return table([ne], header_row=cond1)
    rank = [[x.find(l) for x in Ap] for l in cand] 
    score_de_Borda = [[abs(x.find(l)-len(cand)) for x in Ap] for l in cand]
    if cond==1 :
        return table(rank, header_row=cond1, header_column=cond2)
    total_score_de_Borda = [add([score_de_Borda[j][i]*ne[i] 
                                 for i in range(24)]) for j in range(len(cand))]
    if cond==3 :
        return total_score_de_Borda
    if cond==2 :
        return table(score_de_Borda, header_column=cond3)
    max_total_score_de_Borda = max(total_score_de_Borda)
    ind_max_total_score_de_Borda = total_score_de_Borda.index(max_total_score_de_Borda)
    if cond==4 : # vainqueur_de_Borda
        return cand[ind_max_total_score_de_Borda]

cand = ["A","B","C","D"]
ne = [18,16,14,12,11,20,19,14,16,12,2,1,0,0,20,16,13,15,11,10,9,8,7,5]
show(html("Le tableau des rangs est :"))
show(Borda(cand,ne,1))
show(html("Le tableau des scores est :"))
show(Borda(cand,ne,2))
show(html(f"Les scores totaux de chaque candidat sont : {Borda(cand,ne,3)}"))
show(html(f"Le vainqueur d'un décompte à la Borda est : {Borda(cand,ne,4)}."))
edit flag offensive delete link more

Comments

I am trying to also post a link to a SageMath Cell with the above code, but, for unknown reasons, I get an error each time a write the long permalink. Anyway, this is the short temporary link: https://sagecell.sagemath.org/?q=apzuww

Juanjo gravatar imageJuanjo ( 2021-01-07 20:12:02 +0100 )edit

Hello, @Juanjo! In case you are trying to post the permalink as a comment, unfortunately, the link tends to be longer than the allowed number of characters for a comment. The odd thing is that sometimes there is no warning when that happens: Ask SageMath simply refuses to accept the publication.

In any case, is it my impression or more characters are allowed now?

dsejas gravatar imagedsejas ( 2021-01-08 01:20:18 +0100 )edit

I first tried to add the permalink at the end of the answer, but I got an "internal server error" which prevented it to be published. Comments seem to be limited to 800 characters, too few for a permalink if the code is a bit complex.

Juanjo gravatar imageJuanjo ( 2021-01-08 01:42:50 +0100 )edit

Thanks I was not aware of f-strings. And what about permalink. If I have an html page that can be usefull tosagemath users is there a way to publish it publicly ?

Cyrille gravatar imageCyrille ( 2021-01-08 15:16:42 +0100 )edit

Thanks a lot but I am surprise by the behavior of this little modification of 'cond 3' which gives the image added in the question.

table(total_score_de_Borda,header_row=cond3)

Cyrille gravatar imageCyrille ( 2021-01-08 16:57:27 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-01-07 09:39:32 +0100

Seen: 208 times

Last updated: Jan 07 '21