Sagemath表达式无法解决。

huangapple go评论59阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月10日 18:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定