Ask Your Question

Dropping higher powers of a variable in an expression

asked 2011-09-29 16:58:31 +0100

indiajoe gravatar image

updated 2011-09-29 17:44:50 +0100

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.

edit retag flag offensive close merge delete


@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"

indiajoe gravatar imageindiajoe ( 2011-09-29 17:43:14 +0100 )edit

For 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..

indiajoe gravatar imageindiajoe ( 2011-09-30 07:32:53 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2011-09-29 17:34:17 +0100

kcrisman gravatar image

updated 2011-09-29 17:35:24 +0100

Try 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'.

edit flag offensive delete link more


Interestingly, the power series and symbolic expressions order their polynomials in exactly reverse order. I don't care, but it is ... interesting.

kcrisman gravatar imagekcrisman ( 2011-09-29 17:36:05 +0100 )edit

Thanks. 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?

indiajoe gravatar imageindiajoe ( 2011-09-29 17:39:19 +0100 )edit

By "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.

DSM gravatar imageDSM ( 2011-09-29 18:41:53 +0100 )edit

I 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)))

indiajoe gravatar imageindiajoe ( 2011-09-29 19:38:36 +0100 )edit

answered 2011-09-29 21:14:59 +0100

DSM gravatar image

Okay, 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: R.<x> = PolynomialRing(QQ)
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: 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]
[1 0 0]
[0 1 0]
[0 0 1]
[ 1  0  0]
[ 0  1 -x]
[ 0  x  1]
[           1            0            0]
[           0 -1/2*x^2 + 1           -x]
[           0            x -1/2*x^2 + 1]
[                1                 0                 0]
[                0 x^3 - 1/2*x^2 + 1                -x]
[                0                 x      -1/2*x^2 + 1]
edit flag offensive delete link more


Thankyou very much. The example you provided cleared all the confusions. The Ring concept in sage was not much clear to me before. :-)

indiajoe gravatar imageindiajoe ( 2011-09-30 07:26:14 +0100 )edit

I 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)))

indiajoe gravatar imageindiajoe ( 2011-09-30 07:28:34 +0100 )edit

answered 2011-09-29 20:56:55 +0100

benjaminfjones gravatar image

updated 2011-09-29 20:59:11 +0100

You 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.

edit flag offensive delete link more


Good point. I intentionally avoided polynomial rings since I wasn't sure if the poster wanted that or just polys within SR.

kcrisman gravatar imagekcrisman ( 2011-09-30 09:58:41 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-09-29 16:58:31 +0100

Seen: 956 times

Last updated: Sep 29 '11