Arraymancer icon indicating copy to clipboard operation
Arraymancer copied to clipboard

Automatically overload custom procedures to take tensors and scalars

Open cmacmackin opened this issue 4 years ago • 1 comments

In Fortran it is possible to declare a procedure "elemental", meaning that though it is defined in terms of scalar arguments and return types, it can also be applied element-wise for arrays. What is particularly nice about this is the compiler will let you mix array and scalar arguments in a function call. The "vectorize" decorator in NumPy behaves similarly (although is horrendously slow). Currently the closest available functions in Arraymancer are map and apply_ufunc, but neither is quite as powerful as what I'm describing. It would be nice to have a feature like this with Arraymancer, perhaps as a pragma we could use when defining our own procedures. I'm not sure how hard this would be to implement.

A simpler approach might be to overload all of your element-wise operators to work when all arguments are scalars and then define some sort of implicitly generic type which can be either a scalar or an array. E.g:

type
    MaybeTensor[T] = T | Tensor[T]
    MaybeCudaTensor[T] = T | CudaTensor[T]
    MaybeClTensor[T] = T | ClTensor[T]
    MaybeAnyTensor[T] = T | AnyTensor[T]

Then a user can simply define a procedure with arguments of this implicitly generic type.

cmacmackin avatar Dec 27 '20 19:12 cmacmackin

linked to https://github.com/mratsim/Arraymancer/issues/87

mratsim avatar Jan 01 '21 17:01 mratsim