# How to extract coefficients and powers from expressions with non integer powers?

I want to extract coefficients and powers of the expression

$$3-2x+7x^{2.1}+x^{2.4}-5x^3$$

Code:

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;


To get a result such as:

{{0,3},{1,-2},{2.1,7},{2.4,1},{3,-5}}

I tried to use Coefficient but that didn't work.

• In the special case where all exponents are rational you can do this. In[54]:= expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3; Map[{# /. x -> 1, Exponent[#, x]} &, Collect[Rationalize@expr, x, Plus, List]] Out[55]= {{3, 0}, {-2, 1}, {7, 21/10}, {1, 12/5}, {-5, 3}} Commented Nov 15, 2023 at 16:58

Clear["Global*"];
expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;
Transpose[{Exponent[#, x], # /. x -> 1}] & /@ List @@ expr


{{0, 3}, {1, -2}, {2.1, 7.}, {2.4, 1.}, {3, -5}}

• Thanks for your answer, but the code gives me more than one error: Transpose::nmtx: The first two levels of {0,3} cannot be transposed. >> Transpose::nmtx: The first two levels of {1,-2} cannot be transposed. >> Transpose::nmtx: The first two levels of {2.1,7.} cannot be transposed. >> General::stop: Further output of Transpose::nmtx will be suppressed during this calculation. >> Commented Nov 15, 2023 at 15:28
• @MohamedMostafa I removed the Transpose, and that fixed the issue and yields the same result as OP has. Commented Nov 16, 2023 at 5:18
• Somehow both variations work for me. The one suggested by @March is indeed the better one. {Exponent[#, x], # /. x -> 1} & /@ List @@ expr
– Syed
Commented Nov 16, 2023 at 5:22

You may use pattern matching to achieve what you want:

Clear["Global*"];
expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;

expr /. Plus -> List /. {(x2_ : 1  ) Power[x_, x1_] :> {x1, x2},
x1_ x :> {1, x1}, x_ /; NumericQ[x] -> {0, x}}

{{0, 3}, {1, -2}, {2.1, 7}, {2.4, 1}, {3, -5}}

ce = Apply[List/*Map[List]/*ReplaceAll[a_. x^b_. :> Splice @ {b, a}]/*PadLeft];

ce[3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3]

{{0, 3}, {1, -2}, {2.1, 7}, {2.4, 1}, {3, -5}}

coefList = List @@ # /. #2 -> 1 &;

expList = Exponent[##, List] &;

expCoefList = Transpose @* Through @* {expList, coefList}


Examples:

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;

coefList[expr, x]

{3, -2, 7., 1., -5}

expList[expr, x]

{0, 1, 2.1, 2.4, 3}

expCoefList[expr, x]

{{0, 3}, {1, -2}, {2.1, 7.}, {2.4, 1.}, {3, -5}}


I'd expand the exponents until they are integers:

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;

expr /. x -> y^10 // PowerExpand // Rationalize
(*    3 - 2 y^10 + 7 y^21 + y^24 - 5 y^30    *)

CoefficientRules[%, y]
(*    {{30} -> -5, {24} -> 1, {21} -> 7, {10} -> -2, {0} -> 3}    *)


? Maybe:

MapAt[Total@*(Exponent[#,x]&/@Variables[expr] #&),1]/@List@@@CoefficientRules[expr]

(* {{3.,-5.},{1.,-2.},{2.1,7.},{2.4,1.},{0.,3.}} *)


Using Cases:

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;

Cases[Flatten@{List @@ expr}, s_ :> {Exponent[s, x], Rationalize[s /. x -> 1]}]

(*{{0, 3}, {1, -2}, {2.1, 7}, {2.4, 1}, {3, -5}}*)


Or using ReplaceAll:

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;

Transpose[List @@ expr /. s_ :> {Exponent[s, x], Rationalize[s /. x -> 1]}]

(*{{0, 3}, {1, -2}, {2.1, 7}, {2.4, 1}, {3, -5}}*)


Direct pattern-matching solution:

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;

List@@expr /. {a_. x^b_. :> {b, a}, c_?NumberQ :> {0, c}}

expr = 3 - 2 x + 7 x^2.1 + x^2.4 - 5 x^3;


Using ReplaceAll

expr /.
{Plus :> List,
a_?NumericQ :> {0, a},
_[a_, x] :> {1, a},
_[x, a_] :> {a, 1},
_[a_, _[x, b_]] :> {b, a}}


{{0, 3}, {1, -2}, {2.1, 7}, {2.4, 1}, {3, -5}}