ReplaceAll problem
I think you've found a bug in pattern matcher.
This problem can be reduced to matching sequence of length one with named BlankSequence
patterns in Orderless
functions, it stopped working in v10.1. In previous versions your replacement rule works (as noted by belisarius).
Minimal example of this behavior is:
ClearAll[f, a]
SetAttributes[f, {Orderless}]
$Version
MatchQ[{f[a], a}, {f[x__], x__}]
In different versions I get:
"8.0 for Linux x86 (64-bit) (November 7, 2010)"
True
"9.0 for Linux x86 (64-bit) (February 7, 2013)"
True
"10.0 for Linux x86 (64-bit) (December 4, 2014)"
True
"10.1.0 for Linux x86 (64-bit) (March 24, 2015)"
False
"10.2.0 for Linux x86 (64-bit) (July 6, 2015)"
False
If we use Blank
instead of BlankSequence
, remove name from one of patterns or use non-Orderless
function, then pattern matches in all versions:
ClearAll[g]
MatchQ[{f[a], a}, {f[x_], x_}]
MatchQ[{f[a], a}, {f[x__], __}]
MatchQ[{g[a], a}, {g[x__], x__}]
(* True
True
True *)
As to your original replacement, as noted by Bill due to Flat
, OneIdentity
and Orderless
attributes of Plus
you can use:
a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d] /. term_ + Transpose[term_] :> 2*term
(* 2 a.b.c.d + a.ss.e.g.r *)
To fix this problem on Mma 10.1 on OS X 10.10.4 I took off one of the blanks on term, i.e.
ReplaceAll[a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d],
{Plus[front___, term__, middle___, Transpose[term__], end___] :>
Plus[front, middle, end, 2*term]}]
a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d]
ReplaceAll[a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d],
{Plus[front___, term_, middle___, Transpose[term_], end___] :>
Plus[front, middle, end, 2*term]}]
2 a.b.c.d + a.ss.e.g.r