RE: Extracting Coefficients and Powers
- To: mathgroup at smc.vnet.net
- Subject: [mg48271] RE: [mg48254] Extracting Coefficients and Powers
- From: "DrBob" <drbob at bigfoot.com>
- Date: Thu, 20 May 2004 04:03:49 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Here's one method: expr = 5 - 3/x^5.2 + x^3.2 - x^5; rules = {(a_)?(FreeQ[#1, x] & )*(b_:1) :> {a, b}, b_:1 :> {1, b}}; expr /. Plus -> List Transpose[Replace[%, rules, 1]] {5, -(3/x^5.2), x^3.2, -x^5} {{5, -3, 1, -1}, {1, 1/x^5.2, x^3.2, x^5}} If there's no constant term, it requires a little fix-up to get 0 in there. This might do: expr = -(3/x^5.2) + x^3.2 - x^5; rules = {(a_)?(FreeQ[#1, x] & )*(b_:1) :> {a, b}, b_:1 :> {1, b}}; expr /. Plus -> List Transpose[Replace[%, rules, 1]] If[MemberQ[Last[%], 1], %, Transpose[Prepend[Transpose[%], {0, 1}]]] {-(3/x^5.2), x^3.2, -x^5} {{-3, 1, -1}, {1/x^5.2, x^3.2, x^5}} {{0, -3, 1, -1}, {1, 1/x^5.2, x^3.2, x^5}} DrBob www.eclecticdreams.net -----Original Message----- From: Bruce W. Colletti [mailto:bcolletti at compuserve.com] To: mathgroup at smc.vnet.net Subject: [mg48271] [mg48254] Extracting Coefficients and Powers I have a sum of terms, each of the form "a * x^r" or "a * 1/x^r", where a and (positive) r are real numbers (if a is missing, it's presumably 1 or -1 and if there is no x-term, r is understood to be 0). In my application, the complete expression is given by Expand[p], where p is a product of terms having form a t^b + c t^d (p is actually a factorial moment generating function). I want to extract all coefficients into a list (including 1 or -1 coefficients), and exponents into another (to include 0 for any constant term). For instance, x^3.2 + 5 - 3 / x^5.2 - x^5 yields the coefficients' list {1, 5, -3, -1} and exponents' list {3.2, 0, -5.2,5}. How would I build these lists? Must Cases[ ] be used or are there built-in functions? Thanks. Bruce