I am trying to write some numerical code that can work with both scalars and vectors (in this case, these are D and DV types, respectively, from DiffSharp). Sometimes I want to be able to use either so that I have defined a discriminatory union for them:
type IBroadcastable =
| Scalar of D
| Vect of DV
Many operators are already overloaded for both of these types, so for their use in IBroadcastableI am writing to add code like this to the union:
static member Exp x =
match x with
| Scalar x -> Scalar (exp x)
| Vect x -> Vect (exp x)
It seems very redundant. Is there any way to use the operator in a union without having to write a new overload for it? Or should I use a different scheme (i.e. non-discriminatory union)? An example of what I want to use for this type:
let ll (y: IBroadcastable) (theta: IBroadcastable) = y*theta-(exp theta)
* - ( ), , exp , . , y, DiffSharp theta.