I have problems extending a replacement rule.
Schematically, this is the situation: I have an expression and a rule, and they work perfectly together.
In[1] = result = expr /.rule
Out[1] = ...
Now I want to extend the rule to let it cope with extra multiplication factors. It should be such that
In[2] = 5 result - (5 expr /. ruleex)
Out[2] = 0
And
In[3] = 5 w result - (5 w expr /. ruleex)
Out[3] = 0
These are the expression and the rule I am working with:
In[4]:= rule = Times[f[x__][y__], z__] -> Times[-1, q, y, f[x][z]]
In[5]:= expr = f[-a][s[c, -b]]*f[a][s[-c, b]]
In[6]:= result = expr /. rule
Out[4]:= -s[c, -b] f[-a][f[a][s[-c, b]]]
To make the rule cope with an arbitrary number of multiplicative factors on the left, I guess adding a BlankNullSequence would do.
In[7]:= ruleex = Times[q___, f[x__][y__], z__] -> Times[-1, q, y, f[x][z]];
In[8]:= expr /.rulex
In[9]:= 5 expr /. ruleex
In[10]:= 5 w expr /. ruleex
Out[5]:= -s[c, -b] f[-a][f[a][s[-c, b]]]
Out[6]:= -s[c, -b] f[-a][5, f[a][s[-c, b]]]
Out[7]:= -s[c, -b] f[-a][5, w, f[a][s[-c, b]]]
Out[5] is as expected. The extra factor 5 ends up not at the front of the expression, but somewhere in the in f[a][...]. In Out[7] something similar occurs.
Why does this not work? How can I extend the rule to act in the way I described?