Initialise array without specific types

The array needs to be a subtype of Real (but no smaller) for automatic differentiation to work.

I think you need something like

julia> function fill_mat_new(x::T) where {T<:Real} 
           Array{T}(undef,(3,4)) 
end

fill_mat_new (generic function with 1 method)

julia> fill_mat_new(2)
3×4 Array{Int64,2}:
 0  0  0  0
 0  0  0  0
 0  0  0  0

julia> fill_mat_new(2.3)
3×4 Array{Float64,2}:
 0.0  0.0  0.0  0.0  
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

Here is a quick example which may be helpful

function f_new(meta::Array{T}, init) where {T<:Real}
    params = Array{T}(undef, size(init)[1], 1)
    for i=1:5
        params[i] = init[i]
    end
    for i=2:5
        params[i] = meta[1] * params[i-1]
    end
    return sum(params)
end

s = [1.,2.,3.,4.,5.]
ForwardDiff.gradient(x -> f_new(x, s), [2.0])

julia> ForwardDiff.gradient(x -> f_new(x, s), [2.0])
1-element Array{Float64,1}:
 49.0

You may also find these threads helpful

2 Likes