# Initialise array without specific types

**URL:** <https://discourse.julialang.org/t/initialise-array-without-specific-types/60204>\
**Category:** Performance\
**Tags:** question\
**Created:** [April 28, 2021, 9:30pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204 "2021-04-28T21:30:43Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [April 28, 2021, 9:47pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/3 "2021-04-28T21:47:45Z")

</div>

> [@baptnz](#):
>
> `Array{Float64}(undef,(3,4))`

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

I think you need something like

```julia
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

```julia
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

> [@ForwardDiff cannot assign ForwardDiff.Dual to input array](https://discourse.julialang.org/t/forwarddiff-cannot-assign-forwarddiff-dual-to-input-array/37522/4):
>
> Try using eltype() to debug. This often how I catch errors when I’m using ForwardDiff in my code. It’s likely that one of the many parameters might be having its type forced into something like an array of floats. Same code using eltype() below. Notice that special ForwardDiff.Dual type being created. This is why you want to avoid forcing the type of a parameter to be Float64 because Julia can’t convert ForwardDiff.Dual type to a Float64, hence the "“cannot convert ForwardDiff.Dual to Float64 e…

> [@ForwardDiff - slowed down by Real vs. Float operations](https://discourse.julialang.org/t/forwarddiff-slowed-down-by-real-vs-float-operations/36987/4):
>
> Building off on @Elrod’s suggestion, note that you can also do julia\> function foo(x::Array{T}) where {T\<:Real} M = zeros(T, 1, 1, 1) # do stuff with M end foo (generic function with 1 methods) julia\> ForwardDiff.gradient(foo, rand(31)) Note that parametrizing the type of the input function will also lead to the kind of specialized code which I think @Sukera is hinting at in his reply. The current issue seems to be that the conversion to Real is happening at run-t…

---

_[View the full topic](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204)._
