Ask Your Question
2

Dropping higher powers of a variable in an expression

asked 13 years ago

indiajoe gravatar image

updated 13 years ago

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.

Preview: (hide)

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 ( 13 years ago )

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 ( 13 years ago )

3 Answers

Sort by » oldest newest most voted
2

answered 13 years ago

kcrisman gravatar image

updated 13 years ago

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

Preview: (hide)
link

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 ( 13 years ago )

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 ( 13 years ago )

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 ( 13 years ago )

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 ( 13 years ago )
2

answered 13 years ago

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]
Preview: (hide)
link

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 ( 13 years ago )

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 ( 13 years ago )
2

answered 13 years ago

benjaminfjones gravatar image

updated 13 years ago

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.

Preview: (hide)
link

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 ( 13 years ago )

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: 13 years ago

Seen: 1,261 times

Last updated: Sep 29 '11