How can I create a new data type, interval, with different behavior than the built-in one. I want to write [a, b] and perform operator overloading, as in C ++? (I have to use a different algebra than that of the built-in Interval)

Example of an operation (same as dafault, but I would like to implement it):

[a, b] + [c, d] = [a + c, b + d];

I am using Mathematica V.8

  • $\begingroup$ You might be interested in this $\endgroup$ Commented Mar 5, 2016 at 11:17
  • $\begingroup$ J.M. I can not understand the topic covered in this post, so it's hard to understand :( $\endgroup$
    – plus91
    Commented Mar 5, 2016 at 11:23
  • $\begingroup$ The point of that post is that you define how the arithmetic operations affect your objects with TagSetDelayed[]. For instance: myInterval /: myInterval[a_, b_] + myInterval[c_, d_] := myInterval[a + c, b + d]. $\endgroup$ Commented Mar 5, 2016 at 11:26
  • $\begingroup$ J.M. It seems to be interesting , at this point how can I declare a variable with this new type and algebra ? myInterval[2,3]+myInterval[4,5] => myInterval[6,8] Ok! but if the different fossere variables? It is also possible to define some properties, for example the associative? I'm sorry but I'm trying to understand how it works. Thanks. $\endgroup$
    – plus91
    Commented Mar 5, 2016 at 11:32
  • $\begingroup$ Plus[] already has the Flat (associative) attribute, so it should already work with the definition I gave. $\endgroup$ Commented Mar 5, 2016 at 11:41

1 Answer 1


As suggested in comments, TagSetDelayed (i.e. the combination of /: and :=) allows you to impose your desired behavior upon symbols in particular situations. For instance, below I define the additive property you asked for. You can also define the behavior of built in functions when used in combination with your myInterval object; for example "teach" the built-in Min and Max what to return when dealing with one such object.


(* If the bounds are explicitly numerical, reorder them with the lower bound first *)
myInterval[a_, b_] /; a > b := myInterval[b, a]

(* Define the sum property *)
myInterval /: myInterval[a_, b_] + myInterval[c_, d_] := myInterval[a + c, b + d]

(* Define the behavior of Max, Min, MinMax with your myInterval object *)
myInterval /: Min[myInterval[a_, b_]] := a
myInterval /: Max[myInterval[a_, b_]] := b
myInterval /: MinMax[myInterval[a_, b_]] := MinMax[{a, b}]

You can now evaluate the following expressions:

myInterval[3, 5] + myInterval[5, 6] (* myInterval[8, 11] *)

myInterval[5, 4]                    (* myInterval[4, 5] *)

Min @ myInterval[5, 4]              (* 4 *)
Max @ myInterval[5, 4]              (* 5 *)

MinMax @ myInterval[5, 4]           (* {4, 5} *)
  • $\begingroup$ I would like to define the following function F [ a_ +b_ * Subscript[e, j_], if F is called F[a_] -> myInterval[a,a,0] else if it is called F[a+b*Subscript[e, j]]->myInterval[a-b,a+b,j], I tried so few but nothing. any idea? Thanks in Advice $\endgroup$
    – plus91
    Commented Mar 14, 2016 at 22:05
  • $\begingroup$ It would seem that the following should work: F[a_] := myInterval[a, a, 0]; F[a_ + b_*Subscript[e_, j_]] := myInterval[a - b, a + b, j]. If it doesn't, I'd suggest that you post a different question so you can explain your requirements more fully and perhaps add some examples as well. $\endgroup$
    – MarcoB
    Commented Mar 15, 2016 at 1:20

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