Here's part of the code I'm working on using Python: (A follow-up question of my previous post)
import random
pairs = [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
]
alphasori = [(random.choice([1, -1]) * random.uniform(5, 15), pairs[n]) for n in range(4)]
binum = np.random.randint(2, size=4).tolist()
d = dict(zip([0,1,2,3], binum))
alpbi = [(i, tuple(d[j] for j in c)) for i, c in alphasori]
print(alpbi)
Here's a sample output (call this list alpbi):
[(-6.16111614207135, (1, 1)), (-9.39824028732309, (1, 1)), (12.1294338553467, (1, 0)), (8.192565262190904, (0, 1))]
Now I'm hoping to calculate a linear combination of the random numbers (the first terms) inside each tuple in alpbi, which is followed by the following rules:
- if the inner tuple is (1,1) or (0,0), then the random number is multiplied by (-1)
- if the inner tuple is (0,1) or (1,0), then keep the original number.
- From 1&2, We're calculating the linear combination of those random numbers.
Here's the code I have to figure out a single case:
S = 0
for i in range (len(alpbi)):
if alpbi[i][1][0] == alpbi[i][1][1]:
S += (-1)*alpbi[i][0]
else:
S += alpbi[i][0]
print(S)
However, given that '1's and '0's in the inner tuple are random binary numbers, how can I calculate all the possible values of S? (there're 8 of them, I'm wondering is there a way I can write a function to return all the possible values at the same time?)
Thanks a lot for reading my question, I really appreciate your help:)
Aucun commentaire:
Enregistrer un commentaire