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.
Product
, as aTimes
, as aDot
, or what? Your first equation isTimes
, yourpattern
hasDot
s, but you ask about aProduct
$\endgroup$list =Array[x, 5]; mono = a * x[1]^q1 x[2]^q2 x[3]^q3 x[4]^1 x[5]^0 ; Cases[mono *Apply[Times,list]^m // PowerExpand, x_^n_ :> {x, n}] /. m -> 0
$\endgroup$x^in_.
- not for aDot
but for an optional part of the pattern to includex^1
terms. I've corrected the pattern now. $\endgroup$Product
function. I believe they will useTimes
. $\endgroup$