# Pattern matching product with arbitrary number of terms

I am trying to write a function to convert an expression of the form

$$\alpha x_1^{n_1} x_2^{n_2} ... x_N^{n_N}$$

(with some coefficient $$\alpha\in\mathbb{R}$$) into a list of indices and powers

$$\{\{1,n_1\},\{2,n_2\},...,\{N,n_N\}\}.$$

for expressions of varying length (number of products $$x_i^{n_i}$$).

My natural choice for this was the Cases function with pattern matching. Something along the lines of, for a three-term product,

pattern = α_ x{i_}^ni_. x{j_}^nj_. x{k_}^nk_.
Cases[expr, pattern -> {{i,ni},{j,nj},{k,nk}}],


where I write x{i_} to denote Subscript[x,i_].

My question is how to match a product of arbitrary length. Is there a way to write a pattern to match

$$\alpha \prod_j x_{i_j}^{n_{i_j}}$$

for an arbitrary $$j$$ and $$i_j\in\mathbb{Z}$$ not predefined.

Alternatively, if we assume the maximum $$j$$ is known, is there a way I can write a single pattern (along the lines of pattern above) such that each term is optional? I.e. modify pattern to also match 2-term and 1-term products.

• I should comment that although this question is primarily about pattern matching, I am not tied to this approach if it is the 'wrong' way to attack problems like this in Mathematica – Cailean Wilkinson Jul 22 at 11:31
• Can you provide the Mathematica code corresponding to the expression you have? Is it written as a Product, as a Times, as a Dot, or what? Your first equation is Times, your pattern has Dots, but you ask about a Product – bRost03 Jul 22 at 14:46
• list =Array[x, 5]; mono = a * x^q1 x^q2 x^q3 x^1 x^0 ; Cases[mono *Apply[Times,list]^m // PowerExpand, x_^n_ :> {x, n}] /. m -> 0 – I.M. Jul 22 at 15:09
• @bRost03, yes, I accept the way I had written the pattern was confusing - especially as in a rush I had used latex-style x_i to denote a subscript. The dots in the pattern should read x^in_. - not for a Dot but for an optional part of the pattern to include x^1 terms. I've corrected the pattern now. – Cailean Wilkinson Jul 22 at 15:47
• I use product in the mathematical sense as interchangeable with times, not because the expression used a Product function. I believe they will use Times. – Cailean Wilkinson Jul 22 at 15:48

Update: "to apply the Times/Power -> List replacement only to powers of $$x_i$$"

expr2 = -2 a^2 expr expr2 /. m -> 5 /. Times[a___, p : (Power[_x, _] ..)] :>
{Times @ a, Sequence @@ ({p} /. Power -> List /. x -> Identity)}

{-2 a^2 α, {1, n}, {2, n}, {3, n}, {4, n}, {5, n}}


expr = α Product[Power[x[i], n[i]], {i, 1, m}] With numeric m you can simply replace Times and Power with List:

Rest[expr /. m -> 5 /. Times | Power -> List] /. x -> Identity

 {{1, n}, {2, n}, {3, n}, {4, n}, {5, n}}


Also

Block[{Power = List, Times = List},
Transpose @ First @ Rest[expr /. m -> 5]] /. x -> Identity

 {{1, n}, {2, n}, {3, n}, {4, n}, {5, n}}


and

First @ Cases[expr /. m -> 5,
Times[α, pat : __Power] :> ReplaceAll[x -> Identity] @* List @@@ {pat}, All]

{{1, n}, {2, n}, {3, n}, {4, n}, {5, n}}

• @CaileanWilkinson, please see the update. – kglr Jul 22 at 16:24
• Thanks! Sorry, I removed the comment asking for that when I realised I could just 'correct' for it by piecing the coefficient back together afterwards. Didn't realise you'd already seen it / started on an answer. Seems like a nicer approach to just not break up the coefficient in the first place as per your update. Thanks again! – Cailean Wilkinson Jul 22 at 16:34

If we can assume the variables are always in the form $$x[i]$$ then we have

f = With[{v = #}, {First[#], Exponent[v, #]} & /@ Cases[v, x[_], ∞]] &;


EDIT: for variables in the form $$x_i$$ just use f = With[{v = #}, {# /. Subscript[x, a__] -> a, Exponent[v, #]} & /@ Cases[v, Subscript[x,_],∞]] &;

This handles the initial coefficient more robustly than the other answer. The idea is the Cases identifies the variables and the Map gives back the format you like. So something like

test=8a^2 x^q1 x^q2 x^q3 x
f@test


gives

{{1,q1},{2,q2},{3,q3},{4,1}}


Note that the coefficient is filtered automatically and it works for an exponent of 1.

If we cannot assume the variables are always in the form $$x_i$$ and, like in a now deleted comment, the coefficient may be something like $$8a^2$$, then it's obviously impossible since we can't know if $$a$$ belongs to the coefficient or the variables. That aside, we can do something like

f[exp_, cof_] := Block[{exp2, vars, ind},
exp2 = (exp/cof) /. Times -> List;
vars = exp2 /. Times -> List /. Power[a_, b_] -> a;
MapIndexed[(ind = First[#2]; {ind, Exponent[#1, vars[[ind]]]}) &, exp2]]


which takes in the coefficient as an argument. Again

test=8a^2 x^q1 x^q2 x^q3 x
f@test


gives

{{1,q1},{2,q2},{3,q3},{4,1}}


as desired.

If you don't know the coefficient apriori, you can put in 1 or simply use

f[exp_] := Block[{Times=List, vars, ind},
vars = exp /. Power[a_, b_] -> a;
MapIndexed[(ind = First[#2]; {ind, Exponent[#1, vars[[ind]]]}) &, exp]]


then filter the coefficient later like you said you can do in the comments.