A package I'm working on uses rule solutions (like the output of Solve) as inputs to functions. I'd like to be able to easily manipulate these rule solutions by overloading Plus. Here are some examples of the behavior what I want:
{x -> 1} + {x -> 2}
(* {x -> 3} *)
{x -> 1} + {y -> 2}
(* {x -> 1, y -> 2} *)
{x -> 1, y -> 2} + {x -> 2}
(* {x -> 3, y -> 2} *)
Is there an easy way to implement this? Right now I'm getting some of this functionality using a function called Tweak to tweak one coordinate:
Tweak[point_,var_Symbol,h_]:=Append[Select[point,#[[1]]=!=var&],var->((var/.point)+h)];
but would rather have the syntax above.
Associations are a new feature that put you really close to what you're trying to build there usingLists ofRules. Check outAssociationandMerge. – sblom Oct 24 '16 at 18:22Plusis a seriously bad idea. All kinds of (un-)foreseeable consequences. I would useAssociations as recommended above or use replacement rules, something like{x -> 1} + {x -> 2} /. Rule[x_, y_] + Rule[x_, z_] :> Rule[x, y + z]. – march Oct 24 '16 at 18:44Plusis a bad idea (especially if it's in a package I unleash on the unsuspecting :). Reading through some other posts, I see there are some operators without built-in meanings, such asCirclePlus. Do you think that's OK to define in such a context? – Chris K Oct 25 '16 at 01:29