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.Wed, 05 Sep 2012 09:21:18 +0200Extract terms from a sumhttps://ask.sagemath.org/question/9295/extract-terms-from-a-sum/Is there some way to programmatically extract the first, second, third, and so on terms from a sum of symbolic terms (or equivalently, turn such a sum into the list of its summands)? For example, after
sage: A,B,C = var('A'), var('B'), var('C')
sage: F = A*B+C; F
A*B + C
Is there some method you can call on F to extract A*B (or C)? This would be useful especially for displaying sage in LaTeX via sageTeX when a formula that is the sum of 4 terms (say) runs over the margin and has to be split up somehow. I would also be interested in a workaround in sageTeX that would allow you to flexibly insert a linebreak in a formula under the circumstances that the formula generated by Sage got too long for a line.
Wed, 05 Sep 2012 00:05:14 +0200https://ask.sagemath.org/question/9295/extract-terms-from-a-sum/Comment by kcrisman for <p>Is there some way to programmatically extract the first, second, third, and so on terms from a sum of symbolic terms (or equivalently, turn such a sum into the list of its summands)? For example, after</p>
<pre><code>sage: A,B,C = var('A'), var('B'), var('C')
sage: F = A*B+C; F
A*B + C
</code></pre>
<p>Is there some method you can call on F to extract A*B (or C)? This would be useful especially for displaying sage in LaTeX via sageTeX when a formula that is the sum of 4 terms (say) runs over the margin and has to be split up somehow. I would also be interested in a workaround in sageTeX that would allow you to flexibly insert a linebreak in a formula under the circumstances that the formula generated by Sage got too long for a line.</p>
https://ask.sagemath.org/question/9295/extract-terms-from-a-sum/?comment=19111#post-id-19111Can you start a different question about the SageTeX thing? It's sort of buried here, and should really have the honor of its own question.Wed, 05 Sep 2012 09:21:18 +0200https://ask.sagemath.org/question/9295/extract-terms-from-a-sum/?comment=19111#post-id-19111Answer by benjaminfjones for <p>Is there some way to programmatically extract the first, second, third, and so on terms from a sum of symbolic terms (or equivalently, turn such a sum into the list of its summands)? For example, after</p>
<pre><code>sage: A,B,C = var('A'), var('B'), var('C')
sage: F = A*B+C; F
A*B + C
</code></pre>
<p>Is there some method you can call on F to extract A*B (or C)? This would be useful especially for displaying sage in LaTeX via sageTeX when a formula that is the sum of 4 terms (say) runs over the margin and has to be split up somehow. I would also be interested in a workaround in sageTeX that would allow you to flexibly insert a linebreak in a formula under the circumstances that the formula generated by Sage got too long for a line.</p>
https://ask.sagemath.org/question/9295/extract-terms-from-a-sum/?answer=14001#post-id-14001Look at the `operator()` and `operands()` built-in methods of the Symbolic Expression class:
sage: F.operator()
<built-in function add>
sage: F.operands()
[A*B, C]
sage: F == F.operator()(*F.operands())
A*B + C == A*B + C
sage: bool(F == F.operator()(*F.operands()))
TrueWed, 05 Sep 2012 01:20:40 +0200https://ask.sagemath.org/question/9295/extract-terms-from-a-sum/?answer=14001#post-id-14001