Ask Your Question
2

Dropping higher powers of a variable in an expression

asked 2011-09-29 09:58:31 -0500

indiajoe gravatar image

updated 2011-09-29 10:44:50 -0500

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

Comments

@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 10:43:14 -0500 )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 00:32:53 -0500 )edit

3 answers

Sort by ยป oldest newest most voted
2

answered 2011-09-29 10:34:17 -0500

kcrisman gravatar image

updated 2011-09-29 10:35:24 -0500

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

Comments

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 10:36:05 -0500 )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 10:39:19 -0500 )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 11:41:53 -0500 )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 12:38:36 -0500 )edit
2

answered 2011-09-29 13:56:55 -0500

benjaminfjones gravatar image

updated 2011-09-29 13:59:11 -0500

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

Comments

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 02:58:41 -0500 )edit
2

answered 2011-09-29 14:14:59 -0500

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: 
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]
edit flag offensive delete link more

Comments

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 00:26:14 -0500 )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 00:28:34 -0500 )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

Stats

Asked: 2011-09-29 09:58:31 -0500

Seen: 434 times

Last updated: Sep 29 '11