1
$\begingroup$

I want to extract coefficients.

Subscript[x, 1, 2] + Subscript[a, 2] Subscript[x, 4, 2] 
{ {{1,2},1} , {{4,2},Subscript[a, 2]}  } 


Subscript[x, 1, 2]
{ {{1,2},1}  }


Subscript[a, 2] Subscript[x, 4, 2]
{  {{4,2},Subscript[a, 2]}  }


Subscript[x, 2]
{ {{2}, 1}  }

How to extract the coefficients? I tried CoefficientList and CoefficientRules, but I failed.

Any help would be appreciated.

$\endgroup$

2 Answers 2

3
$\begingroup$
Subscript[x, 1, 2] + Subscript[a, 2] Subscript[x, 4, 2] //
Apply[List] //
# /.a_. * Subscript[x, m_?NumericQ, n_?NumericQ ] :> {{m,n},a} &

{{{1, 2}, 1}, {{4, 2}, Subscript[a, 2]}}

Subscript[x, 1, 2] /.a_.* Subscript[x, m_?NumericQ, n_?NumericQ ] :> {{m,n},a}

{{1, 2}, 1}

notice missing a bracket.

Subscript[a, 2] Subscript[x, 4, 2] /.a_.* Subscript[x, m_?NumericQ, n_?NumericQ ] :> {{m,n},a}

{{4, 2}, Subscript[a, 2]}

$\endgroup$
1
$\begingroup$

Succinct verion

Thanks to the help of @AsukaMinato

Clear["Global`*"];
hasMultipleTerms[expr_] := ListQ[expr /. Plus -> List];
transformExpression[expr_] := 
  Module[{isMultipleTerms, myOptionList, resultToBeRefined}, 
   isMultipleTerms = hasMultipleTerms@expr;
   myOptionList = If[isMultipleTerms, List @@ expr, expr];
   resultToBeRefined = 
    myOptionList /. 
     a_.*Subscript[x, subsripts__] :> {List[subsripts], a};
   If[isMultipleTerms, {#} & /@ 
     resultToBeRefined, {{resultToBeRefined}}]];

transformExpression[
 Subscript[x, 1, 2] + Subscript[a, 2] Subscript[x, 4, 2]]
transformExpression[Subscript[x, 1, 2]]
transformExpression[Subscript[a, 2] Subscript[x, 4, 2]]
transformExpression[Subscript[x, 2]]
transformExpression[Subscript[x, 2, 3, 5]]

Previous version

So verbose, whereas definitely true...

Clear[getCoefficients]
getCoefficients[expr_] := 
 Module[{terms, coefficients, matched, unmatched, result, k},
  result = {};
  terms = expr /. Plus -> List;
  If[ListQ[terms] == False,
   {matched1 = 
     Cases[{terms}, 
      coeff_*Subscript[x, subscripts__] :> {List[subscripts], coeff}];
    matched2 = 
     Cases[{terms}, 
      Subscript[x, subscripts__] :> {List[subscripts], 1}];
    (*Print[{terms}];
    Print[matched1,"match",matched2];*)
    If[Length@matched1 == 1, {result = result~Append~matched1, 
      Return[result];}];
    If[Length@matched2 == 1, {result = result~Append~matched2, 
      Return[result];}];
    Return[result];},
   {For[k = 1, k <= Length@terms, k++,
     term = terms[[k]];
     matched1 = 
      Cases[{term}, 
       coeff_*Subscript[x, subscripts__] :> {List[subscripts], coeff}];
     matched2 = 
      Cases[{term}, 
       Subscript[x, subscripts__] :> {List[subscripts], 1}];
     (*Print[{term}];
     Print[matched1,"match",matched2];*)
     If[Length@matched1 == 1, {result = result~Append~matched1, 
       Continue[]}];
     If[Length@matched2 == 1, {result = result~Append~matched2, 
       Continue[]}];
     ];
    Return[result];}
   ];
  result
  ]


getCoefficients[
 Subscript[x, 1, 2] + Subscript[a, 2] Subscript[x, 4, 2]]
getCoefficients[Subscript[x, 1, 2]]
getCoefficients[Subscript[a, 2] Subscript[x, 4, 2]]
getCoefficients[Subscript[x, 2]]
$\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.