Implementing new algebra for list-like objects [duplicate]

I would like to implement an algebra (addition, multiplication, etc.) for what is called "truncated power series" (TPSA) (Differential Algebraic description of beam dynamics). .

These 'objects' (sets of complex numbers) have special rules for addition and multiplication. Of course the easy way to go is just to use plain lists and redefine functions like myAdd[], myMult[], etc. acting on these lists.

However in most implementations in other languages these objects are represented as real objects (OO) of a 'TPSA' class, and operator overloading is used to manipulate them.

My question is: I know the last solution is not the way to go with Mathematica, but the first easy way doesn't seem the best one either. How would you advice to implement that with Mathematica ?

Edit: The root of my question was apparently "what is an up-value", the answer below about dual number also points in that direction.

• intersting question. am i right that you ask for overloading these operators, changing the commutativity? if this is so please have a look at NonCommutativeMultiply (shorthand **) and for instance this post (mathematica.stackexchange.com/questions/22824/…) Jun 25, 2013 at 8:41
• @Stefan I want to overload explicitly. From what I understand, in the link you gave, the operator is not defined, just a set of rules to use the properties of the operator is defined. Here I want to explicitly define it, working component by component (I can make use of the indices in the list). Jun 25, 2013 at 9:51
• ok. the link wasn't your specific solution, just thought as a hint for your topic. last try, before i try to get into the details...how about that one? math.ucsd.edu/~ncalg Jun 25, 2013 at 10:01
• @Stefan I think that's overcomplicating the problem. I think my question is mainly about overloading. Jun 25, 2013 at 13:32
• Do the objects have matrix representation? Dec 17, 2021 at 15:50

Ok so, as I said in the comments, your algebra is isomorphic to a "generalised complex" algebra. That, of so-called dual numbers. The paper you link to gives the following opreations ($+$, multiplication by scalar, composition $\cdot$):

• $(a_1,b_1) + (a_2,b_2) = (a_1+ a_2, b_1 + b_2), \;\; a_1, a_2, b_1, b_2 \in \mathbb{R}$
• $t (a_1,b_1) = (t a_1, t a_2),\;\; t, a_1, a_2 \in \mathbb{R}$
• $(a_1,b_1)\cdot(a_2,b_2) = (a_1 a_2, a_1 b_2 + a_2 b_1),\;\; a_1, a_2, b_1, b_2\in \mathbb{R}.$

If you think of $(a,b)$ as being complex numbers $a + \lambda b$ with a generalised complex unit that is nilpotent $\lambda^2 = 0,$ the above are satisfied. You may implement these in Mathematica (these are commutative numbers) by overloading Plus, Times and . w.r.t. the head genComp:

genComp[x_, y_] + genComp[z_, w_] ^:= genComp[x + z, y + w];
t_ genComp[x_, y_] ^:= genComp[t x, t y];
genComp[x_, y_].genComp[z_, w_] ^:= genComp[x z, {x, y}.{w, z}];


now the generalised complex number genComp[a,b] can be written

genComp[a,b] == (a genComp[1, 0] + b genComp[0, 1])
(*True*)


which is a different way of rewriting these pairs as $(a , b) = a + \lambda b.$ Here the role of $\lambda$ is played by genComp[0,1] which "squares" (under composition) to zero:

genComp[0,1].genComp[0,1] == 0
(*True*)


this is true for any pair genComp[0,a], genComp[0,b] which then leads to there being zero divisors so, unlike "normal" complex numbers, dual numbers don't form a group.

• OK, thanks, I understood what you're talking about. Very good point. From the point of view of automatic differentiation considering that we go to higher order (of derivatives), or with more than one variable, then I think the rules get too complex to be represented that simply (dual numbers), but anyway I can create a new Head and define the rules. Jun 26, 2013 at 8:34
• I had a look for "dual numbers" here and there is quite a list - you might be interested in mathematica.stackexchange.com/questions/13912/… especially. Also there is a demo on automatic differentiation and dual numbers: demonstrations.wolfram.com/AutomaticDifferentiation
– gpap
Jun 26, 2013 at 11:04