Coefficient list of an expression with indices
Yet another way to do this, using Coefficient
expr = -27 Subscript[u, 1, 1] - 6 Subscript[u, 1, 2] +
9 Subscript[u, 1, 3] + 36 Subscript[u, 2, 1] +
8 Subscript[u, 2, 2] - 12 Subscript[u, 2, 3] -
9 Subscript[u, 3, 1] - 2 Subscript[u, 3, 2] +
3 Subscript[u, 3, 3];
Flatten@Array[Coefficient[expr, Subscript[u, #1, #2]] &, {3, 3}]
(* {-27, -6, 9, 36, 8, -12, -9, -2, 3} *)
Edit
Here's a way to get there using CoefficientList
,
CoefficientList[expr, Variables[expr]] // Flatten //
DeleteDuplicates // Reverse // Most
(* {-27, -6, 9, 36, 8, -12, -9, -2, 3} *)
Since all terms have Subscript[something]
, we can use a pattern:
expr /. Plus -> List /. c_.*Subscript[__] :> c
{-27, -6, 9, 36, 8, -12, -9, -2, 3}
This assumes the expression has been Expand
ed so that the Head
is indeed Plus
. This will also give the coefficients in the same order as they appeared in the expression. If you also would like which coefficients belong with which coefficients, do:
expr /. Plus -> List /. c_.*Subscript[x__] :> {c, Subscript[x]}
{{-27, Subscript[u, 1, 1]}, {-6, Subscript[u, 1, 2]}, {9, Subscript[u, 1, 3]}, {36, Subscript[u, 2, 1]}, {8, Subscript[u, 2, 2]}, {-12, Subscript[u, 2, 3]}, {-9, Subscript[u, 3, 1]}, {-2, Subscript[u, 3, 2]}, {3, Subscript[u, 3, 3]}}
EDIT:
Explanation of replacement: First, all terms in expr
are put in a list. Then, we replace all instances of expressions in that list that match the pattern c_.*Subscript[__]
with just c
. This pattern matches expressions where some expression c
is multiplied by a Subscript
expression with any arguments (at least one). The period in the pattern c_.
lets us include the default value in multiplication, which is 1. If we had omitted it, the pattern would not match e.g. the last term in
3 Subscript[u,1,1] + Subscript[u,1,2]
because that term is not strictly "some expression multiplied with Subscript
.
As for the ordering, yes, the ordering will be the same as that in expr
as long as it is a "flat" sum, i.e. fully expanded.
Turning my comment into an answer. If you know that this is exactly the form of the expression, you can just pick out the coefficients manually with First
. It's important though that you first turn the sum into a list, because otherwise the coefficients will be summed up immediately:
First /@ List @@ expression
The List @@
replaces the head Plus
with List
. Then we simply pick out each coefficient with First
, because Mathematica orders products with the coefficient first and the variable second.
Note that this doesn't work reliably if some of the terms have a coefficient of 1
(in that case, the variable itself would be returned). You could fix that with something like:
Replace[First /@ List @@ expression, n_ /; ! NumericQ@n -> 1, {1}]
But at that point it might be simpler to use one of the other answers.
Alternatively, as garej suggested in a comment, if you have a variable a
that you know is unused, you could do:
First /@ List @@ Expand[a*expr] /. a -> 1