# How to overload and compile pure functions operating on associations

I am writing a small package for myself for operating on zonotopes. I am storing the generator matrix and the center of the zonotope in an association. I decided to write a small constructor:

Zonotope = <| G-> #1, c->#2 |>&;
Z1 = Zonotope[{{1,1,1,1},{1,1,1,1}},{0,0}];


I can now define a function:

AffineTransformZonotope = <|G -> #1.(#2[G]), c -> #1.(#2[c])|>] &;


such that calling AffineTransformZonotope[ A, Z1 ] (where A is a matrix of appropriate dimensions) gives me Z1 transformed by A.

Now, I want to do several overloads on my functions, for example I want a construction that takes an integer n and returns a unit n-dimensional cube:

Zonotope = <|G->IdentityMatrix[n], c->ConstantArray[0,n]|>&;


as well as overloading AffineTransformZonotope[{A,b},Z1] to take a matrix A and the vector b and return the appropriate zonotope. Of course, I want to return an error message whenever there is no appropriate overloaded function.

I know I could to this defining several rules, e.g.

Zonotope[n_Integer /; n > 1] := <|G->IdentityMatrix[n],c->ConstantArray[0,n]|>;
Zonotope[Gmat_ /; MatrixQ[Gmat], cvec_ /; VectorQ[cvec]] :=
If[ Dimensions[Gmat][[1]] == Length[cvec] ,
<|G -> Gmat, c -> cvec|>,
];


Similar rules can be defined for the AffineTransformZonotope function.

I would like to know if I can avoid rules and work entirely with pure functions: I want to later compile and thread lists of zonotopes and affine transformations and then perhaps take the Minkowski sum over the list...

Furthermore, I was wondering if there is any way to compile to C when using Associations. I noticed that when I tried it was calling the uncompiled version. If there is no support, is there any better way to emulate such a structure?.

P.S. I wanted to use the tag: function-overloading, but I don't have enough reputation.

• Function overloading is not possible with pure functions. Of couse, you can use pure functions and do the type checking in a humongous If/Which construction. I would not advise doing so. Moreover, Associations are not compilable. So you will have to formulate everything in terms of arrays to compile it later. – Henrik Schumacher Jun 24 '18 at 13:43
• Hey @HenrikSchumacher, thanks for the info. I did try the Which/Switch thing but as you correctly point out just gets horrible. So there is no way of emulating structures and compile to C? What do you mean by formulate everything in terms of arrays and compile later?. – Mario E. Villanueva. Jun 24 '18 at 14:29
• When using Compile, you have to restrict to a few classical C trype: Arrays of real and complex numbers (both machine precision), arrays of integers (long long IIRC), and True and False (for booleans). In particular, Compile does not like Associations. But that is usually not a big restriction as CompiledFunctions should only be created for very localized tasks that involve a big deal of number crunching (and only for those taks that cannot be done more efficiently by linear algebra). – Henrik Schumacher Jun 24 '18 at 14:36