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.
Association
s are a new feature that put you really close to what you're trying to build there usingList
s ofRule
s. Check outAssociation
andMerge
. $\endgroup$Plus
is a seriously bad idea. All kinds of (un-)foreseeable consequences. I would useAssociation
s as recommended above or use replacement rules, something like{x -> 1} + {x -> 2} /. Rule[x_, y_] + Rule[x_, z_] :> Rule[x, y + z]
. $\endgroup$Plus
is 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? $\endgroup$