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.Thu, 19 Dec 2019 09:42:52 +0100factorize symbolic expressionhttps://ask.sagemath.org/question/49040/factorize-symbolic-expression/ (ax+bx).factor()=x(a+b)
but
(2a+2b).factor()=(2a+2b)
how to obtain 2(a+b) ?
Thanks in advance...Sun, 15 Dec 2019 15:36:29 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/Comment by Juanjo for <p>(ax+bx).factor()=x(a+b)
but
(2a+2b).factor()=(2a+2b)
how to obtain 2(a+b) ?</p>
<p>Thanks in advance...</p>
https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49046#post-id-49046There exists the `collect_common_factors` method that should do the trick, I think. But it seems a bit buggy.
This works as expected:
sage: a, b = var("a, b")
sage: (2*pi*a + 2*pi*b).collect_common_factors()
2*pi*(a + b)
sage: (2*a^2 + 2*a*b).collect_common_factors()
2*(a + b)*a
However, this fails:
sage: (2*a + 2*b).collect_common_factors()
2*a + 2*b
sage: (SR(2)*a + SR(2)*b).collect_common_factors()
2*a + 2*bMon, 16 Dec 2019 01:16:07 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49046#post-id-49046Comment by John Palmieri for <p>(ax+bx).factor()=x(a+b)
but
(2a+2b).factor()=(2a+2b)
how to obtain 2(a+b) ?</p>
<p>Thanks in advance...</p>
https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49069#post-id-49069Looks like a bug to me, since `(2*a*x + 2*b*x).factor()` returns `2*(a + b)*x`.Wed, 18 Dec 2019 18:44:28 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49069#post-id-49069Answer by John Palmieri for <p>(ax+bx).factor()=x(a+b)
but
(2a+2b).factor()=(2a+2b)
how to obtain 2(a+b) ?</p>
<p>Thanks in advance...</p>
https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?answer=49061#post-id-49061You can work with elements of a polynomial ring instead of with symbolic expressions:
sage: R.<a,b,c> = ZZ[] # polynomials with integer coefficients, variables a, b, c
sage: (a*2+b*2).factor()
2 * (a + b)
sage: (a*c+b*c).factor()
c * (a + b)
Tue, 17 Dec 2019 22:04:43 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?answer=49061#post-id-49061Comment by Jingenbl for <p>You can work with elements of a polynomial ring instead of with symbolic expressions:</p>
<pre><code>sage: R.<a,b,c> = ZZ[] # polynomials with integer coefficients, variables a, b, c
sage: (a*2+b*2).factor()
2 * (a + b)
sage: (a*c+b*c).factor()
c * (a + b)
</code></pre>
https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49079#post-id-49079ok I undersatnd....Thu, 19 Dec 2019 09:42:52 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49079#post-id-49079Comment by Jingenbl for <p>You can work with elements of a polynomial ring instead of with symbolic expressions:</p>
<pre><code>sage: R.<a,b,c> = ZZ[] # polynomials with integer coefficients, variables a, b, c
sage: (a*2+b*2).factor()
2 * (a + b)
sage: (a*c+b*c).factor()
c * (a + b)
</code></pre>
https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49063#post-id-49063Thanks for the replay.
A little bit heavy for a simple factorization of an expression...
Any particular reason for this restriction of the format method?
Big thanks anywayWed, 18 Dec 2019 10:33:25 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49063#post-id-49063Comment by John Palmieri for <p>You can work with elements of a polynomial ring instead of with symbolic expressions:</p>
<pre><code>sage: R.<a,b,c> = ZZ[] # polynomials with integer coefficients, variables a, b, c
sage: (a*2+b*2).factor()
2 * (a + b)
sage: (a*c+b*c).factor()
c * (a + b)
</code></pre>
https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49068#post-id-49068The notion of factorization is algebraic and depends on the ring in which you are working. For example `x^2+1` factors over the complex numbers but not the reals, and `x^2-2` factors over the reals but not the rationals. So when you're dealing with factorization, it's a good idea to precisely specify the ring.Wed, 18 Dec 2019 18:43:43 +0100https://ask.sagemath.org/question/49040/factorize-symbolic-expression/?comment=49068#post-id-49068