Ask Your Question
2

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

asked 2011-09-27 22:50:38 +0100

Tengiz Sharafiev gravatar image

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 23:03:01 +0100

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 22:50:38 +0100

Seen: 562 times

Last updated: Sep 27 '11