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.Sat, 27 Apr 2019 23:11:59 +0200how to append to a symbolic expression in a loop?https://ask.sagemath.org/question/46377/how-to-append-to-a-symbolic-expression-in-a-loop/This must be super basic, but i don't get how to do the classic
for i in list:
expression = expression + i
symbolically. I want to add new terms to the expression, and the "==" notation does not mash well with that.
An .append() method would make sense, but i was not able to find one.Sat, 27 Apr 2019 22:18:13 +0200https://ask.sagemath.org/question/46377/how-to-append-to-a-symbolic-expression-in-a-loop/Comment by stockh0lm for <p>This must be super basic, but i don't get how to do the classic </p>
<pre><code>for i in list:
expression = expression + i
</code></pre>
<p>symbolically. I want to add new terms to the expression, and the "==" notation does not mash well with that. </p>
<p>An .append() method would make sense, but i was not able to find one.</p>
https://ask.sagemath.org/question/46377/how-to-append-to-a-symbolic-expression-in-a-loop/?comment=46385#post-id-46385the `= SR.zero()` is the solution. I had defined expression as a symbolic variable and then it didn't work.
can you please move this to a proper answer so i can upvote it?Sat, 27 Apr 2019 23:11:59 +0200https://ask.sagemath.org/question/46377/how-to-append-to-a-symbolic-expression-in-a-loop/?comment=46385#post-id-46385Answer by vdelecroix for <p>This must be super basic, but i don't get how to do the classic </p>
<pre><code>for i in list:
expression = expression + i
</code></pre>
<p>symbolically. I want to add new terms to the expression, and the "==" notation does not mash well with that. </p>
<p>An .append() method would make sense, but i was not able to find one.</p>
https://ask.sagemath.org/question/46377/how-to-append-to-a-symbolic-expression-in-a-loop/?answer=46380#post-id-46380Could you clarify your question? The following code works
sage: expression = SR.zero()
sage: for i in [SR.var('x'), SR.var('y'), SR.var('z')]:
....: expression = expression + i
sage: expression
x + y + zSat, 27 Apr 2019 22:31:18 +0200https://ask.sagemath.org/question/46377/how-to-append-to-a-symbolic-expression-in-a-loop/?answer=46380#post-id-46380