1
$\begingroup$

I have an indexed variable Subscript[p, s, m, t] established using the Notation method. There are various expressions where s, m, t take on various integer values. I want to be able to select the parts of the expression where m == 2, for example. I tried the Coefficient[] function but it just produces 0.

$\endgroup$

closed as off-topic by Alexey Popkov, Henrik Schumacher, bbgodfrey, gwr, LCarvalho Dec 30 '17 at 0:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Alexey Popkov, Henrik Schumacher, bbgodfrey, gwr, LCarvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

2
$\begingroup$

Since there's no working example, I'll start by building a list of indexed variables:

samplelist = RandomSample[Flatten[Array[Subscript[p, #1, #2, #3] &, {3, 3, 3}]], 5]

(* {Subscript[p, 2, 2, 2], Subscript[p, 3, 3, 2], Subscript[p, 1, 2, 3],
    Subscript[p, 1, 3, 3], Subscript[p, 1, 3, 1]} *)

Selecting the elements for which m = 2 (with m being the second index) is a straightforward case of pattern matching using Cases

Cases[samplelist, Subscript[p, _, 2, _]]

(* {Subscript[p, 2, 2, 2], Subscript[p, 1, 2, 3]} *)

Assuming your expressions are more complicated than that, you can use a similar approach for, say, a sum:

samplesum = samplelist.RandomInteger[{-5, 5}, 5]

(* -2 Subscript[p, 1, 2, 3] + 5 Subscript[p, 1, 3, 1] + Subscript[p, 1, 3, 3] + 
    3 Subscript[p, 2, 2, 2] + 4 Subscript[p, 3, 3, 2] *)

Cases[samplesum, _ Subscript[p, _, 2, _]]

(* {-2 Subscript[p, 1, 2, 3], 3 Subscript[p, 2, 2, 2]} *)

where the extra _ is a pattern that can stand for any Mathematica expression; in this case, the integer coefficient on the relevant variables (so you could change the _ to _Integer if you just wanted to get at all the variables with integer coefficients in an expression).

Note that Cases also has a levelspec argument. So, for example, if you just want to pull the variables (no coefficients) out of samplesum

Cases[samplesum, Subscript[p, _, 2, _]]

will just give you {}, but

Cases[samplesum, Subscript[p, _, 2, _], 2]

will return {Subscript[p, 1, 2, 3], Subscript[p, 2, 2, 2]}, as desired.

You can use TreeForm to explore the levels that various subexpressions appear at. If you give some details about the expressions you're looking at, someone might be able to provide more specific help.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.