(Below is a big simplification of my actual use-case which reproduces the problem with SequenceReplace. Please assume my use of SequenceReplace is necessary).

I have a list of symbols with subscripts and square brackets. E.g.

{ Subscript[a,1][0], Subscript[b,0][2], Subscript[b,1][1], Subscript[c,2][0]] }

or better visually:

enter image description here

I wish to modify the list such that neighbouring b symbols with the same subscript value are removed. I am attempting this using the pattern

SequenceReplace[mylist, {Subscript[b, i_][_], Subscript[b, i_][_]} :> Nothing]

though this incurs a strange error. When the subscripts do match, it removes them fine:

enter image description here

but when they don't match, it undesirably puts them in their own sub-list!

enter image description here

Here, the expected output was the unchanged mylist.

This seems like a strange and undocumented behaviour; is this a bug, or am I not understanding something?

I notice this only occurs when both subscripts and [args] in present in my pattern. For example, without the args (e.g. Subscript[b,_], Subscript[b,_] :> Nothing) this sublist behaviour does not occur.

Furthermore, this behaviour seems specific to SequenceReplacement and not my pattern, since the unnecessarily verbose use of ReplaceAll doesn't exhibit it!

enter image description here

  • 1
    $\begingroup$ It does seem like either a bug or something that should be explained in the documentation. It's probably worth reporting to Wolfram and seeing what they have to say. $\endgroup$ – MassDefect Jun 26 at 18:10

Not sure why that happens, but here's a workaround:

list1 = {Subscript[a,1][0],Subscript[b,0][2],Subscript[b,1][1],Subscript[c,2][0]};
list2 = {Subscript[a,1][0],Subscript[b,0][2],Subscript[b,0][1],Subscript[c,2][0]};

SequenceReplace[list1, {PatternSequence[Subscript[b,i_][_], Subscript[b,i_][_]]} -> Nothing]
SequenceReplace[list2, {PatternSequence[Subscript[b,i_][_], Subscript[b,i_][_]]} -> Nothing]

{Subscript[a, 1][0], Subscript[b, 0][2], Subscript[b, 1][1], Subscript[c, 2][0]}

{Subscript[a, 1][0], Subscript[c, 2][0]}

  • $\begingroup$ Thanks very much! Miraculously, though my 'MWE' still shows this bug, my original use-case has suddenly started working (I think due to some other unrelated rules in SequenceReplace). I think I need to lie down $\endgroup$ – Anti Earth Jun 26 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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