My apologies if this question has been asked before, but for the first 50 or so questions on coefficientlist, I couldn't find a similar topic.
If I've an expression which involves a variable with indices, and I want to collect its coefficients, how can it be achieved? For example, if my expression is
-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]
then how can I collect {-27, -6, 9, 36, 8, -12, -9, -2, 3}
. The expression can have several other terms, but always of the form of Subscript[u, i, j]
.
And, if all these terms have a common denominator, how that situation can be handled? So far I was simply taking the numerator part, but if I've to compare the coefficients of many expressions, considering the full expression will further help. In this situation, I found that the answers of Marius and Alexei work straight away.
For example, if my full expression looks like -((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])/(16 hx))
then using Marius' and Alexei's suggestion, I get the expected answer {-(27/(16 hx)), -(3/(8 hx)), 9/(16 hx), 9/(4 hx), 1/( 2 hx), -(3/(4 hx)), -(9/(16 hx)), -(1/(8 hx)), 3/(16 hx)}.
CoefficientList
you can just extract them yourself:First /@ List @@ expression
$\endgroup$First /@ expression
was giving0
as the answer $\endgroup$1
. $\endgroup$