Re: Extracting Coefficients and Powers

• To: mathgroup at smc.vnet.net
• Subject: [mg48272] Re: [mg48254] Extracting Coefficients and Powers
• From: "Bruce W. Colletti" <bcolletti at compuserve.com>
• Date: Thu, 20 May 2004 04:03:51 -0400 (EDT)
• References: <001601c43dcd\$5cb8ee30\$6501a8c0@HolyCow>
• Sender: owner-wri-mathgroup at wolfram.com

```Bobby

The group may also like the approach below taken by Bruce Miller (Wolfram
Tech Support).  He was kind enough this morning to look at this pressing
question, and I gathered the key parts of his reply (and made a few cosmetic
changes for my sake) into the following fragment.

p = Expand[(1 + t^-1 + 3t^3)(2 + t + t^-2) + t^7 - t^8];

p = (1.0 Apply[List,p]) /. t->t^1.0; (* establish 1.0 coefficients and 1.0
exponents *)

xconstant = Cases[p, _?NumericQ]; (* at most one constant term *)

L = Cases[p, c_ t^n_ ->{n,c}];

Sort[If[Length[xconstant] > 0, Join[L,{{0,xconstant[[1]]}}], L]]

The expanded "polynomial" is

3 + 1/t^3 + 1 / t^2 + 2/t + 4t + 6t^3 + 3t^4 + t^7 - t^8

The list of {exponent,coefficient} pairs (lo-to-hi degree) is:

{{-3., 1.}, {-2., 1.}, {-1., 2.}, {0, 3.}, {1., 4.}, {3., 6.}, {4., 3.},
{7.,
1.}, {8., -1.}}

Bruce

----- Original Message -----
From: "DrBob" <drbob at bigfoot.com>
To: mathgroup at smc.vnet.net
<mathgroup at smc.vnet.net>
Subject: [mg48272] RE: [mg48254] Extracting Coefficients and Powers

>
> 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
> Sent: Wednesday, May 19, 2004 4:21 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg48272] [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
>
>
>

```

• Prev by Date: Fw: Finding errors in my code?
• Next by Date: Re: Any Tucker-3 public domain, GNU GPL or similar implemenation?
• Previous by thread: RE: Extracting Coefficients and Powers
• Next by thread: the line problem