Ask Your Question
2

How to get the coefficient of $x^n$ in symbolic expression of matrix

asked 2011-09-27 15:50:38 -0500

I have the symbolic matrix:

x = var('x')
h = Matrix(SR, [[x,2],[3*x+5,4]])

How can I get the coefficients of $x^n$ in symbolic expression of matrix. I need to get the follow two matrix from that one:

[[1,0],[3,0]]
[[0,2],[5,4]]
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
6

answered 2011-09-27 16:03:01 -0500

DSM gravatar image

One way to do it is to use apply_map:

sage: x = var('x')
sage: h = Matrix(SR, [[x,2],[3*x+5,4]])
sage: 
sage: h0 = h.apply_map(lambda i: i.coeff(x,0))
sage: h1 = h.apply_map(lambda i: i.coeff(x,1))
sage: h0
[0 2]
[5 4]
sage: h1
[1 0]
[3 0]

Or you could use one of the Matrix constructors:

sage: Matrix(2,2,lambda i,j: h[i,j].coeff(x,0))
[0 2]
[5 4]
sage: Matrix(2,2,lambda i,j: h[i,j].coeff(x,1))
[1 0]
[3 0]
edit flag offensive delete link more

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-27 15:50:38 -0500

Seen: 249 times

Last updated: Sep 27 '11