8
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ 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}} $\endgroup$ Nov 15, 2023 at 16:58

8 Answers 8

9
$\begingroup$
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}}

$\endgroup$
3
  • $\begingroup$ 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. >> $\endgroup$ Nov 15, 2023 at 15:28
  • $\begingroup$ @MohamedMostafa I removed the Transpose, and that fixed the issue and yields the same result as OP has. $\endgroup$
    – march
    Nov 16, 2023 at 5:18
  • 1
    $\begingroup$ Somehow both variations work for me. The one suggested by @March is indeed the better one. {Exponent[#, x], # /. x -> 1} & /@ List @@ expr $\endgroup$
    – Syed
    Nov 16, 2023 at 5:22
7
$\begingroup$
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}}
$\endgroup$
6
$\begingroup$

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}}
$\endgroup$
5
$\begingroup$
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}}
$\endgroup$
4
$\begingroup$

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}    *)
$\endgroup$
4
$\begingroup$

? Maybe:

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


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

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}}*)
$\endgroup$
2
$\begingroup$

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}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.