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.Fri, 30 Sep 2011 09:58:41 +0200Dropping higher powers of a variable in an expressionhttps://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/Many a times in symbolic manipulations, I end up with lots of terms of higher powers of a variable. Is there a command to drop all the terms above a given power from a symbolic expression?
Eg: from an expression f(x)= x+ x^2 + x^3 + x^4
I want to get only upto 2nd order. i.e. g(x)= x + x^2
I also want to do this on a symbolic matrix.
This will be very useful for simplifying expressions upto lower order terms for further calculations.Thu, 29 Sep 2011 16:58:31 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/Comment by indiajoe for <p>Many a times in symbolic manipulations, I end up with lots of terms of higher powers of a variable. Is there a command to drop all the terms above a given power from a symbolic expression?</p>
<p>Eg: from an expression f(x)= x+ x^2 + x^3 + x^4</p>
<p>I want to get only upto 2nd order. i.e. g(x)= x + x^2</p>
<p>I also want to do this on a symbolic matrix.</p>
<p>This will be very useful for simplifying expressions upto lower order terms for further calculations.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21183#post-id-21183For the matrix. The solution provided by @DSM worked (by changing the ring of matrix elements)..
Apart from that the following command also worked.
R=matrix([[1,0,0],[0,1-x^2/2 +x^3,-x],[0,x,1- x^2/2]])
show(R.apply_map(lambda e: taylor(e,x,0,2)))
Thanks to everyone for all the answers..Fri, 30 Sep 2011 07:32:53 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21183#post-id-21183Comment by indiajoe for <p>Many a times in symbolic manipulations, I end up with lots of terms of higher powers of a variable. Is there a command to drop all the terms above a given power from a symbolic expression?</p>
<p>Eg: from an expression f(x)= x+ x^2 + x^3 + x^4</p>
<p>I want to get only upto 2nd order. i.e. g(x)= x + x^2</p>
<p>I also want to do this on a symbolic matrix.</p>
<p>This will be very useful for simplifying expressions upto lower order terms for further calculations.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21188#post-id-21188@kcrisman 's solution works.. How do I apply them on a symbolic matrix? when I try them on a symbolic matrix I am getting "Attribute Error" Thu, 29 Sep 2011 17:43:14 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21188#post-id-21188Answer by DSM for <p>Many a times in symbolic manipulations, I end up with lots of terms of higher powers of a variable. Is there a command to drop all the terms above a given power from a symbolic expression?</p>
<p>Eg: from an expression f(x)= x+ x^2 + x^3 + x^4</p>
<p>I want to get only upto 2nd order. i.e. g(x)= x + x^2</p>
<p>I also want to do this on a symbolic matrix.</p>
<p>This will be very useful for simplifying expressions upto lower order terms for further calculations.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?answer=12705#post-id-12705Okay, combining the other two answers, how about this:
sage: M = matrix([[1,0,0],[0,1-x^2/2 +x^3,-x],[0,x,1- x^2/2]])
sage: parent(M)
Full MatrixSpace of 3 by 3 dense matrices over Symbolic Ring
sage:
sage: R.<x> = PolynomialRing(QQ)
sage:
sage: # change the base ring of M
sage: M = M.change_ring(R)
sage: parent(M)
Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Rational Field
sage:
sage: for i in (0..4):
....: print i
....: print M.apply_map(lambda x: x.truncate(i))
....:
0
[0 0 0]
[0 0 0]
[0 0 0]
1
[1 0 0]
[0 1 0]
[0 0 1]
2
[ 1 0 0]
[ 0 1 -x]
[ 0 x 1]
3
[ 1 0 0]
[ 0 -1/2*x^2 + 1 -x]
[ 0 x -1/2*x^2 + 1]
4
[ 1 0 0]
[ 0 x^3 - 1/2*x^2 + 1 -x]
[ 0 x -1/2*x^2 + 1]
Thu, 29 Sep 2011 21:14:59 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?answer=12705#post-id-12705Comment by indiajoe for <p>Okay, combining the other two answers, how about this:</p>
<pre><code>sage: M = matrix([[1,0,0],[0,1-x^2/2 +x^3,-x],[0,x,1- x^2/2]])
sage: parent(M)
Full MatrixSpace of 3 by 3 dense matrices over Symbolic Ring
sage:
sage: R.<x> = PolynomialRing(QQ)
sage:
sage: # change the base ring of M
sage: M = M.change_ring(R)
sage: parent(M)
Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Rational Field
sage:
sage: for i in (0..4):
....: print i
....: print M.apply_map(lambda x: x.truncate(i))
....:
0
[0 0 0]
[0 0 0]
[0 0 0]
1
[1 0 0]
[0 1 0]
[0 0 1]
2
[ 1 0 0]
[ 0 1 -x]
[ 0 x 1]
3
[ 1 0 0]
[ 0 -1/2*x^2 + 1 -x]
[ 0 x -1/2*x^2 + 1]
4
[ 1 0 0]
[ 0 x^3 - 1/2*x^2 + 1 -x]
[ 0 x -1/2*x^2 + 1]
</code></pre>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21184#post-id-21184I shall also add that the following command too worked for me without changing the ring.
R=matrix([[1,0,0],[0,1-x^2/2 +x^3,-x],[0,x,1- x^2/2]])
show(R.apply_map(lambda e: taylor(e,x,0,2)))
Fri, 30 Sep 2011 07:28:34 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21184#post-id-21184Comment by indiajoe for <p>Okay, combining the other two answers, how about this:</p>
<pre><code>sage: M = matrix([[1,0,0],[0,1-x^2/2 +x^3,-x],[0,x,1- x^2/2]])
sage: parent(M)
Full MatrixSpace of 3 by 3 dense matrices over Symbolic Ring
sage:
sage: R.<x> = PolynomialRing(QQ)
sage:
sage: # change the base ring of M
sage: M = M.change_ring(R)
sage: parent(M)
Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Rational Field
sage:
sage: for i in (0..4):
....: print i
....: print M.apply_map(lambda x: x.truncate(i))
....:
0
[0 0 0]
[0 0 0]
[0 0 0]
1
[1 0 0]
[0 1 0]
[0 0 1]
2
[ 1 0 0]
[ 0 1 -x]
[ 0 x 1]
3
[ 1 0 0]
[ 0 -1/2*x^2 + 1 -x]
[ 0 x -1/2*x^2 + 1]
4
[ 1 0 0]
[ 0 x^3 - 1/2*x^2 + 1 -x]
[ 0 x -1/2*x^2 + 1]
</code></pre>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21185#post-id-21185Thankyou very much. The example you provided cleared all the confusions. The Ring concept in sage was not much clear to me before. :-)Fri, 30 Sep 2011 07:26:14 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21185#post-id-21185Answer by benjaminfjones for <p>Many a times in symbolic manipulations, I end up with lots of terms of higher powers of a variable. Is there a command to drop all the terms above a given power from a symbolic expression?</p>
<p>Eg: from an expression f(x)= x+ x^2 + x^3 + x^4</p>
<p>I want to get only upto 2nd order. i.e. g(x)= x + x^2</p>
<p>I also want to do this on a symbolic matrix.</p>
<p>This will be very useful for simplifying expressions upto lower order terms for further calculations.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?answer=12704#post-id-12704You could also work in the quotient ring of your ring of polynomials by the ideal `(x^3)` as follows:
sage: R.<x> = PolynomialRing(QQ)
sage: S = R.quotient(R.ideal(x^3))
sage: f(x) = x+x^2+x^3+x^4
sage: S(f)
xbar^2 + xbar
sage: S(f).lift()
x^2 + x
In the expression for `S(f)` the variable `xbar` is the image of `x` in the quotient ring `S`.
For another use of the `lift` method, see [this question](http://ask.sagemath.org/question/776/find-polynomial-in-terms-of-ideal#1275).Thu, 29 Sep 2011 20:56:55 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?answer=12704#post-id-12704Comment by kcrisman for <p>You could also work in the quotient ring of your ring of polynomials by the ideal <code>(x^3)</code> as follows:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: S = R.quotient(R.ideal(x^3))
sage: f(x) = x+x^2+x^3+x^4
sage: S(f)
xbar^2 + xbar
sage: S(f).lift()
x^2 + x
</code></pre>
<p>In the expression for <code>S(f)</code> the variable <code>xbar</code> is the image of <code>x</code> in the quotient ring <code>S</code>.</p>
<p>For another use of the <code>lift</code> method, see <a href="http://ask.sagemath.org/question/776/find-polynomial-in-terms-of-ideal#1275">this question</a>.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21182#post-id-21182Good point. I intentionally avoided polynomial rings since I wasn't sure if the poster wanted that or just polys within SR.Fri, 30 Sep 2011 09:58:41 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21182#post-id-21182Answer by kcrisman for <p>Many a times in symbolic manipulations, I end up with lots of terms of higher powers of a variable. Is there a command to drop all the terms above a given power from a symbolic expression?</p>
<p>Eg: from an expression f(x)= x+ x^2 + x^3 + x^4</p>
<p>I want to get only upto 2nd order. i.e. g(x)= x + x^2</p>
<p>I also want to do this on a symbolic matrix.</p>
<p>This will be very useful for simplifying expressions upto lower order terms for further calculations.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?answer=12703#post-id-12703Try this.
sage: f(x)= x+ x^2 + x^3 + x^4
sage: g = f.power_series(ZZ)
sage: g
x + x^2 + x^3 + x^4 + O(x^5)
sage: g.truncate(3)
x^2 + x
I learned something, because I wasn't sure if one could make this work nicely, but it does. Note that you *must* provide a ring for the power series command, and the truncation works in the sense of the 3 meaning `+O(x^3)`.
Or, if you need that, you can use the following similar command.
sage: g.truncate_powerseries(3)
x + x^2 + O(x^3)
I thought about whether this should be a once-off method, but I think it's better to require the sending to power series, because generic symbolic expressions don't have a meaningful sense for 'truncation'.
Thu, 29 Sep 2011 17:34:17 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?answer=12703#post-id-12703Comment by kcrisman for <p>Try this.</p>
<pre><code>sage: f(x)= x+ x^2 + x^3 + x^4
sage: g = f.power_series(ZZ)
sage: g
x + x^2 + x^3 + x^4 + O(x^5)
sage: g.truncate(3)
x^2 + x
</code></pre>
<p>I learned something, because I wasn't sure if one could make this work nicely, but it does. Note that you <em>must</em> provide a ring for the power series command, and the truncation works in the sense of the 3 meaning <code>+O(x^3)</code>.</p>
<p>Or, if you need that, you can use the following similar command.</p>
<pre><code>sage: g.truncate_powerseries(3)
x + x^2 + O(x^3)
</code></pre>
<p>I thought about whether this should be a once-off method, but I think it's better to require the sending to power series, because generic symbolic expressions don't have a meaningful sense for 'truncation'.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21190#post-id-21190Interestingly, the power series and symbolic expressions order their polynomials in exactly reverse order. I don't care, but it is ... interesting.Thu, 29 Sep 2011 17:36:05 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21190#post-id-21190Comment by indiajoe for <p>Try this.</p>
<pre><code>sage: f(x)= x+ x^2 + x^3 + x^4
sage: g = f.power_series(ZZ)
sage: g
x + x^2 + x^3 + x^4 + O(x^5)
sage: g.truncate(3)
x^2 + x
</code></pre>
<p>I learned something, because I wasn't sure if one could make this work nicely, but it does. Note that you <em>must</em> provide a ring for the power series command, and the truncation works in the sense of the 3 meaning <code>+O(x^3)</code>.</p>
<p>Or, if you need that, you can use the following similar command.</p>
<pre><code>sage: g.truncate_powerseries(3)
x + x^2 + O(x^3)
</code></pre>
<p>I thought about whether this should be a once-off method, but I think it's better to require the sending to power series, because generic symbolic expressions don't have a meaningful sense for 'truncation'.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21189#post-id-21189Thanks. That works.. I was trying out the taylor(x,0, ) command also. But now I want to use this on a matrix. Matrix.power_series(ZZ) or Matrix.taylor(x,0,3) etc are all giving be "Attribute Error". How do I apply it on a symbolic matrix?Thu, 29 Sep 2011 17:39:19 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21189#post-id-21189Comment by indiajoe for <p>Try this.</p>
<pre><code>sage: f(x)= x+ x^2 + x^3 + x^4
sage: g = f.power_series(ZZ)
sage: g
x + x^2 + x^3 + x^4 + O(x^5)
sage: g.truncate(3)
x^2 + x
</code></pre>
<p>I learned something, because I wasn't sure if one could make this work nicely, but it does. Note that you <em>must</em> provide a ring for the power series command, and the truncation works in the sense of the 3 meaning <code>+O(x^3)</code>.</p>
<p>Or, if you need that, you can use the following similar command.</p>
<pre><code>sage: g.truncate_powerseries(3)
x + x^2 + O(x^3)
</code></pre>
<p>I thought about whether this should be a once-off method, but I think it's better to require the sending to power series, because generic symbolic expressions don't have a meaningful sense for 'truncation'.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21186#post-id-21186I tried that, but I am getting a ValueError. Following is the code I tried.
R=matrix([[1,0,0],[0,1-x^2/2 +x^3,-x],[0,x,1- x^2/2]])
show(R.apply_map(lambda e: (e.power_series(ZZ)).truncate(2)))
Thu, 29 Sep 2011 19:38:36 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21186#post-id-21186Comment by DSM for <p>Try this.</p>
<pre><code>sage: f(x)= x+ x^2 + x^3 + x^4
sage: g = f.power_series(ZZ)
sage: g
x + x^2 + x^3 + x^4 + O(x^5)
sage: g.truncate(3)
x^2 + x
</code></pre>
<p>I learned something, because I wasn't sure if one could make this work nicely, but it does. Note that you <em>must</em> provide a ring for the power series command, and the truncation works in the sense of the 3 meaning <code>+O(x^3)</code>.</p>
<p>Or, if you need that, you can use the following similar command.</p>
<pre><code>sage: g.truncate_powerseries(3)
x + x^2 + O(x^3)
</code></pre>
<p>I thought about whether this should be a once-off method, but I think it's better to require the sending to power series, because generic symbolic expressions don't have a meaningful sense for 'truncation'.</p>
https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21187#post-id-21187By "use this on a matrix", do you mean that you want to apply it to each element of the matrix? If so, you can use the method .apply_map of the Matrix object.Thu, 29 Sep 2011 18:41:53 +0200https://ask.sagemath.org/question/8329/dropping-higher-powers-of-a-variable-in-an-expression/?comment=21187#post-id-21187