9
$\begingroup$

I use a custom function/head Operator[low, high] which encodes a generic polynomial expression split into "low" and "high" summands following considerations which are irrelevant to this question.

Two Operator objects should sum in the following way:

Operator[a,b] + Operator[c,d] -> Operator[a+c,b+d]

which is just standard element–wise summation that List does automatically. How can I tell Mathematica that this kind of automatic simplification or rearrangement is not only allowed, but desirable?

$\endgroup$

1 Answer 1

10
$\begingroup$

One of the standard ways for such things is using TagSetDelayed

Operator /: Operator[a_, b_] + Operator[c_, d_] := Operator[a + c, b + d]
$\endgroup$
1
  • $\begingroup$ Thanks, that works really well! I'm a bit mystified about semantics behind /: but I'll read up. $\endgroup$
    – Dom Miketa
    Dec 21, 2018 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.