@@ -214,7 +214,7 @@ Returns list of Associations (complete index->field mappings for surviving terms
214214 );
215215
216216LTrunc [setup_ , expr_ FEx ] :=
217- Join @@ Map [CTrunc [setup , # ]& , List @@ expr ];
217+ Join @@ Map [LTrunc [setup , # ]& , List @@ expr ];
218218
219219(* LTrunc returns a list of bare lists (each = one surviving term's factors).
220220 FTruncate handles wrapping back into FTerm/FEx after BalancedMap.
@@ -657,15 +657,29 @@ Returns list of Associations (complete index->field mappings for surviving terms
657657 Module [{compatible },
658658 compatible = Select [alts , And @@ MapThread [(#2 [[1 ]] === AnyField || #1 === #2 )& , {List @@ # , List @@ partials [[qi ]]}]& ];
659659 If [compatible =!= {},
660- current =
661- Plus @@
662- Map [
663- Module [{rules = buildCTruncResolveRules [# ]},
664- (current /. partials [[qi ]] -> # ) /. rules
665- ]&
666- ,
667- compatible
668- ];
660+ (*Split current into terms that contain the partial and terms that do not.
661+ Resolve rules must only be applied to the terms WITH the partial,
662+ otherwise they leak into unrelated branches and corrupt AnyField slots
663+ that should be resolved by a different alternative later.*)
664+ Module [{withPartial , withoutPartial },
665+ If [Head [current ] === Plus ,
666+ withPartial = Plus @@ Select [List @@ current , ! FreeQ [# , partials [[qi ]]]& ];
667+ withoutPartial = Plus @@ Select [List @@ current , FreeQ [# , partials [[qi ]]]& ];
668+ ,
669+ withPartial = current ;
670+ withoutPartial = 0 ;
671+ ];
672+ current =
673+ withoutPartial +
674+ Plus @@
675+ Map [
676+ Module [{rules = buildCTruncResolveRules [# ]},
677+ (withPartial /. partials [[qi ]] -> # ) /. rules
678+ ]&
679+ ,
680+ compatible
681+ ];
682+ ];
669683 current = Distribute [current , Plus , NonCommutativeMultiply ];
670684 current = current /. vertexKillRules ;
671685 current = current /. x_ NonCommutativeMultiply /; ! FreeQ [x , 0 ] :> 0 ;
0 commit comments