英文:
Sagemath expression does not solve
问题
I am trying to implement this equation in SageMath as a function:
def prob_k(p_uv_list, tw):
probs = []
w, k = var('w, k')
# compute probability of generating k entanglements, k=1..w
for tk in range(tw):
pk = binomial(w, k)*(p_uv_list[tk]**k)*(1-p_uv_list[tk])**(w-k).subs({x:tw,k:tk})
probs += [pk]
return probs
The input is a list of probabilities and an integer. I want the output to be a list of probabilities computed given the formula for pk. What I get is:
[0.500000000000000^k*0.500000000000000^w*binomial(w, k),
0.500000000000000^k*0.500000000000000^(w - 1)*binomial(w, k),
0.500000000000000^k*0.500000000000000^(w - 2)*binomial(w, k)]
Why does it not substitute w and k for tw and tk?
英文:
I am trying to implement this equation in SageMath as a function:
def prob_k(p_uv_list, tw):
probs = []
w, k = var('w, k')
# compute probability of generating k entanglements, k=1..w
for tk in range(tw):
pk = binomial(w, k)*(p_uv_list[tk]**k)*(1-p_uv_list[tk])**(w-k).subs({x:tw,k:tk})
probs += [pk]
return probs
The input is a list of probabilities and an integer. I want the output to be a list of probabilities computed given the formula for pk. What I get is:
[0.500000000000000^k*0.500000000000000^w*binomial(w, k),
0.500000000000000^k*0.500000000000000^(w - 1)*binomial(w, k),
0.500000000000000^k*0.500000000000000^(w - 2)*binomial(w, k)]
Why does it not substitute w and k for tw and tk ?
答案1
得分: 1
Instead of using subs, you can simply substitute the values of tw and tk directly in the expression using parentheses and the replace method.
You can try this code:
def prob_k(p_uv_list, tw):
probs = []
w, k = var('w k')
# compute probability of generating k entanglements, k=1..w
for tk in range(tw):
pk = binomial(w, k)*(p_uv_list[tk]**k)*(1-p_uv_list[tk])**(w-k)
pk = pk.replace(w, tw).replace(k, tk)
probs += [pk]
return probs
英文:
Instead of using subs, you can simply substitute the values of tw and tk directly in the expression using parentheses and the replace method
You can try this code :
def prob_k(p_uv_list, tw):
probs = []
w, k = var('w k')
# compute probability of generating k entanglements, k=1..w
for tk in range(tw):
pk = binomial(w, k)*(p_uv_list[tk]**k)*(1-p_uv_list[tk])**(w-k)
pk = pk.replace(w, tw).replace(k, tk)
probs += [pk]
return probs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论