Ask Your Question
0

array numpy mixed division problem

asked 2018-05-28 18:42:16 +0200

ortollj gravatar image

Hi

why this line is ok:

DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])

Except the fact that I do not understand why it is written 0.8333333 instead of 5/6 as usual

but this one is ko ?:

 DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]] / int(len(DiceT[die])))])

in notebook Sagemath 8.2

forget()
import numpy as np
DiceT=[[3,3,3,3,3,6],[1,4,4,4,4,4] ,[2,2,2,5,5,5]]
DiceColorT=['Red','Green','White']
DiceValueFrequencyT=[]
#Rf=[[3,5/6],[6,1/6]];Gf=[[1,1/6],[4,5/6]];Wf=[[2,1/2],[5,1/2]];
for die in range(0,len(DiceT)) :
    freqT = np.array(DiceT[die])
    valueT = np.bincount(freqT)
    nonZeroValueT=np.nonzero(valueT)[0]

    show(valueT)
    show(nonZeroValueT)
    for value in range(0,len(nonZeroValueT)) :
        print int(len(DiceT[die]))
        # these Two lines are ok when uncommented
        #DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/6))])
        DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])
        # but this one give false results when uncommented!
        #DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]] / int(len(DiceT[die])))])

show (DiceValueFrequencyT)
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2018-05-29 18:31:42 +0200

nbruin gravatar image

This is worse than the usual mess. Your table entries are numpy.int64 and Sage integers don't "win" from those:

sage: int(5)/int(6)
0
sage: int(5)/Integer(6)
5/6
sage: np.int64(5)/Integer(6)
0

If you don't want the properties of numpy's word-length integers you should probably get rid of them as soon as possible, so do something like

valueT = [Integer(a) for a in np.bincount(freqT)]
edit flag offensive delete link more
1

answered 2018-05-29 13:50:28 +0200

Sébastien gravatar image

It all depends on the type of operands that you are using. Make sure you know the differences between Sage and Python when using Python integers:

sage: 1/2
1/2
sage: int(1)/int(2)
0

When using both of them, then Sage integers wins:

sage: int(1)/2
1/2
sage: 1/int(2)
1/2

If you really want the quotient, then, in Sage you must do:

sage: 1 // 2
0
edit flag offensive delete link more

Comments

Thank you Sebastien for answering me. Sorry but I do not understand anything about this integer mess. could you tell me what code line I must use to get 5/6 please.But if it is not possible . I would be satisfied with 0.8333333. it seems very complicated to do the division 5/6 ;-) I tried also these two lines below without success.

#DiceValueFrequencyT.append([nonZeroValueT[value],(int(valueT[nonZeroValueT[value]])// int(len(DiceT[die])))])
DiceValueFrequencyT.append([nonZeroValueT[value],((valueT[nonZeroValueT[value]])// (len(DiceT[die])))])
ortollj gravatar imageortollj ( 2018-05-29 18:11:20 +0200 )edit
0

answered 2018-05-29 19:07:11 +0200

ortollj gravatar image

Yes ! Thanks a lot You nbruin it is working like you said !!!

forget()
import numpy as np
DiceT=[[3,3,3,3,3,6],[1,4,4,4,4,4] ,[2,2,2,5,5,5]]
DiceColorT=['Red','Green','White']
DiceValueFrequencyT=[]
#Rf=[[3,5/6],[6,1/6]];Gf=[[1,1/6],[4,5/6]];Wf=[[2,1/2],[5,1/2]];
for die in range(0,len(DiceT)) :
    freqT = np.array(DiceT[die])
    #valueT = np.bincount(freqT)
    valueT = [Integer(a) for a in np.bincount(freqT)]
    #nonZeroValueT=np.nonzero(valueT)[0]
    nonZeroValueT=[Integer(a) for a in np.nonzero(valueT)[0]]

    show(valueT)
    show(nonZeroValueT)
    for value in range(0,len(nonZeroValueT)) :
        print int(len(DiceT[die]))
        # these Two lines are ok when uncommented
        #DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/6))])
        #DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])
        # but this one give false results when uncommented!
        #DiceValueFrequencyT.append([nonZeroValueT[value],(int(valueT[nonZeroValueT[value]])// int(len(DiceT[die])))])
        #DiceValueFrequencyT.append([nonZeroValueT[value],((valueT[nonZeroValueT[value]])// (len(DiceT[die])))])
        DiceValueFrequencyT.append([nonZeroValueT[value],((valueT[nonZeroValueT[value]])/ (len(DiceT[die])))])

show (DiceValueFrequencyT)
edit flag offensive delete link more

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: 2018-05-28 18:42:16 +0200

Seen: 310 times

Last updated: May 29 '18