ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 25 Apr 2021 22:28:27 +0200factor symbolic expressionhttps://ask.sagemath.org/question/56805/factor-symbolic-expression/ In the following code, if I add `.collect(p)` to the `pp*A*qq` in the before last expression, I have an error. Why ?
LE=LatexExpr
x,y,p,q=SR.var('x, y, p, q')
A=matrix(SR,2,2,[x,-x,-x,0])
B=matrix(SR,2,2,[-x,y,x,0])
show(LE(r"\boldsymbol{A} = "),A, LE(r"\,\,\,\,\,\text{et}\,\,\,\,\,"),LE(r"\boldsymbol{B} = "),B)
pp = vector(SR,[p, 1-p])
qq = vector(SR,[q, 1-q])
show(LE(r"\boldsymbol{p} = "),pp, LE(r"\,\,\,\,\,\text{et}\,\,\,\,\,"),LE(r"\boldsymbol{q} = "),qq)
EGe0 = pp*A*qq.collect(p)
show(LE(r"\mathbb{E}G_e^0 = "),EGe0)
Sun, 25 Apr 2021 16:47:57 +0200https://ask.sagemath.org/question/56805/factor-symbolic-expression/Answer by Max Alekseyev for <p>In the following code, if I add <code>.collect(p)</code> to the <code>pp*A*qq</code> in the before last expression, I have an error. Why ?</p>
<pre><code>LE=LatexExpr
x,y,p,q=SR.var('x, y, p, q')
A=matrix(SR,2,2,[x,-x,-x,0])
B=matrix(SR,2,2,[-x,y,x,0])
show(LE(r"\boldsymbol{A} = "),A, LE(r"\,\,\,\,\,\text{et}\,\,\,\,\,"),LE(r"\boldsymbol{B} = "),B)
pp = vector(SR,[p, 1-p])
qq = vector(SR,[q, 1-q])
show(LE(r"\boldsymbol{p} = "),pp, LE(r"\,\,\,\,\,\text{et}\,\,\,\,\,"),LE(r"\boldsymbol{q} = "),qq)
EGe0 = pp*A*qq.collect(p)
show(LE(r"\mathbb{E}G_e^0 = "),EGe0)
</code></pre>
https://ask.sagemath.org/question/56805/factor-symbolic-expression/?answer=56808#post-id-56808Adding parentheses does the job:
`EGe0 = (pp*A*qq).collect(p)`Sun, 25 Apr 2021 17:33:02 +0200https://ask.sagemath.org/question/56805/factor-symbolic-expression/?answer=56808#post-id-56808Comment by Cyrille for <p>Adding parentheses does the job:</p>
<p><code>EGe0 = (pp*A*qq).collect(p)</code></p>
https://ask.sagemath.org/question/56805/factor-symbolic-expression/?comment=56812#post-id-56812Thanks it worksSun, 25 Apr 2021 22:28:27 +0200https://ask.sagemath.org/question/56805/factor-symbolic-expression/?comment=56812#post-id-56812Comment by Max Alekseyev for <p>Adding parentheses does the job:</p>
<p><code>EGe0 = (pp*A*qq).collect(p)</code></p>
https://ask.sagemath.org/question/56805/factor-symbolic-expression/?comment=56810#post-id-56810I'm not sure what problem you are talking about as your code runs without an issue at Sagecell: https://sagecell.sagemath.org/?q=fmhwanSun, 25 Apr 2021 18:30:58 +0200https://ask.sagemath.org/question/56805/factor-symbolic-expression/?comment=56810#post-id-56810Comment by Cyrille for <p>Adding parentheses does the job:</p>
<p><code>EGe0 = (pp*A*qq).collect(p)</code></p>
https://ask.sagemath.org/question/56805/factor-symbolic-expression/?comment=56809#post-id-56809Thanks but I have an other problem `y` is not recognized as a variable. I don't undersand since itis decalred. I have tried without comas. But nothing work so I can't see the effect of the parenthesis.Sun, 25 Apr 2021 17:58:13 +0200https://ask.sagemath.org/question/56805/factor-symbolic-expression/?comment=56809#post-id-56809