Ask Your Question
0

Extracting coefficients from symbolic expressions

asked 2016-02-05 10:10:48 -0500

Peter Luschny gravatar image

Assume a list of symbolic expressions (x0, x1,... are symbolic variables) like

[0, -15*x1^3/x0^7 + 10*x1*x2/x0^6 - x3/x0^5, 15*x1^2/x0^6 - 4*x2/x0^5, -6*x1/x0^5, x0^(-4)]

I want to extract the integer coefficients and get as result

[[0], [-15, 10, -1], [15, -4], [-6], [1]]

What is the most efficient/elegant method to accomplish this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-02-05 11:31:59 -0500

tmonteil gravatar image

updated 2016-02-05 11:32:45 -0500

A possibility is to transform each element as a fraction and look at the coefficients of the numerator. The main issue is about the leading 0 which is not an element of the symbolic ring, and whose list of coefficients is empty. So here is a way (the fill function transforms [] into [0]):

sage: L  = [0, -15*x1^3/x0^7 + 10*x1*x2/x0^6 - x3/x0^5, 15*x1^2/x0^6 - 4*x2/x0^5, -6*x1/x0^5, x0^(-4)]

sage: fill = lambda x : [0] if not x else x
sage: [fill(SR(p).fraction(ZZ).numerator().coefficients()) for p in L]
[[0], [-15, 10, -1], [15, -4], [-6], [1]]
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

1 follower

Stats

Asked: 2016-02-05 10:10:48 -0500

Seen: 338 times

Last updated: Feb 05 '16