I’m trying to make a Matrix callable, in order to be able to use it, for instance, as a matrix of transformation functions. I was able to come up with the following, which works OK.
rotation = [cos (x->-sin(x)) ; sin cos]
function (z::Matrix{Function})(x)
map(f->f(x), z)
end
rotate_pi_6 = rotation(π/6)
rotate_pi_6 * [1,1]
#2-element Vector{Float64}:
# 0.36602540378443876
# 1.3660254037844386
So far, so good.
However, I discovered that if all the functions are the same, then the code no longer works.
[cos cos;cos cos](1)
> ERROR: MethodError: objects of type Matrix{typeof(cos)} are not callable
> Use square brackets [] for indexing an Array.
> Stacktrace:
> [1] top-level scope
> @ REPL[109]:1
It seems that since we now have a Matrix{typeof(cos)} rather than a Matrix{Function}, no suitable dispatch is found. Yet I would have expected this to work, since I imagine that ‘cos’ is a subtype of ‘Function’.
I discovered this while trying
trm = [0 1 ; 2 3]
fn_pwr = n->x->x^n
trfm = map(fn_pwr, trm)
trfm(2)
> ERROR: MethodError: objects of type Matrix{var"#18#20"{Int64}} are not callable, etc...
Where I’d expect something like [1 2 ; 4 8]
Any suggestions, corrections, ideas, pointers most welcome.