# 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:** 6\
**Page:** 2

<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 29, 2021, 11:15pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/21 "2021-04-29T23:15:03Z")

</div>

I believe he was asking in a specific context (copied below). In this case, he needs to pass an _instance_.

```julia
# function do_something(x::Int, T) where {T<:Real} # fails
function do_something(x::Int, y::T) where {T<:Real} 

  
    intermediate_thing = Array{T}(undef,(3,4))

    intermediate_thing[1,1] = 3.0
    
   return(intermediate_thing[1,1] * x)
end

```

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [April 29, 2021, 11:19pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/22 "2021-04-29T23:19:23Z")

</div>

> [@bashonubuntu](#):
>
> You don’t need a function signature in any of these cases, btw.

By signature I only meant the list of arguments, with or without types. I guess the terminology is different in different languages (I’m used to R, where types are never specified).

> [@bashonubuntu](#):
>
> But I don’t understand why you call it a “dummy” type. You will be passing `x` anyway, so you don’t have to create an extra argument just to pass an instance of a type (in case that’s your worry).

What I mean is in the case where `T` is not the type of `x`, as in my example below, it would seem I should be adding a dummy variable `y::T` to the function to pass the type, which feels inelegant.

My current solution is as follows:

```julia
function update_matrix!(m)

  x = [1.2, 3.4]
  m[1,1] = exp(1im*x[1]) + sin(x[2])
  m[2,1] = exp(1im*x[2]) + sin(x[2])
  m[1,2] = exp(1im*x[1]) + sin(x[1])
  m[2,2] = exp(1im*x[2]) + sin(x[1])

end

function high_level(a, x)

  m = Matrix{Complex{Real}}(undef, 2,2)

  for i in 1:a
    update_matrix!(m)
  end

  return(real(m[1,1])*x)
end

high_level(2, 1.5)

```

Here thankfully I can probably derive the type of `m` from the type of `x`, as `m = Matrix{Complex{typeof(x)}}(undef, 2,2)` but if I needed to define the type of `m` in a way that is unrelated to `a` and `x`, with a parametric type T, I understood that would require introducing a dummy variable `y::T`? I guess it’s not a problem in practice, since pretty much always the type of internal objects will depend on the type of the input; unfortunately it can be hard for a newcomer to grasp what kinds of operations can be done on types – here for example I just need to do `Complex{typeof(x)}`, but part of me wonders what I would do if the relation between both types wasn’t so trivial.

I probably misunderstood that it was suggested I could add an argument `y`, which I could consider dummy, just for the purpose of using its type within the function,

```julia
function high_level(a, x, y::T) where {T<:Real}

  m = Matrix{Complex{T}}(undef, 2,2)

  for i in 1:a
    update_matrix!(m)
  end

  return(real(m[1,1])*x)
end

```

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [April 29, 2021, 11:21pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/23 "2021-04-29T23:21:40Z")

</div>

Yep, I think you hit the nail on its head – I’m worrying (needlessly, because this case is solved) that I just don’t know how to operate on types, to derive one from another etc. The promote examples are great because it shows me the way to think about these kinds of problem in the future.

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [April 29, 2021, 11:23pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/24 "2021-04-29T23:23:09Z")

</div>

Ah, interesting, thanks for the clarification. I’m learning a lot in this thread – hard to find these answers by oneself.

---

<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 29, 2021, 11:57pm UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/25 "2021-04-29T23:57:22Z")

</div>

You can do this

```julia
julia> function update_matrix!(m::Array{Complex{T}}) where {T<:Real}

         x = [1.2 3.4]
         m[1,1] = exp(1im*x[1]) + sin(x[2])
         m[2,1] = exp(1im*x[2]) + sin(x[2])
         m[1,2] = exp(1im*x[1]) + sin(x[1])
         m[2,2] = exp(1im*x[2]) + sin(x[1])

         return m
       end
update_matrix! (generic function with 1 method)

julia> function high_level(a::S, y::Array{T}) where {S<:Int, T<:Real}
         m = Array{Complex{T}}(repeat([0 + 0im], outer = [2, 2]))
         for i in 1:a
           update_matrix!(m)
         end
         return(real(m[1,1])*y[1])
       end
high_level (generic function with 1 method)

julia> ForwardDiff.gradient(x -> high_level(1, x), [1.7])
1-element Array{Float64,1}:
 0.1068166524498424

```

or this (notice I removed the ``\<:` where {.....}`)

```julia
julia> function update_matrix_1!(m::Array{Complex{T}}) where {T}

         x = [1.2 3.4]
         m[1,1] = exp(1im*x[1]) + sin(x[2])
         m[2,1] = exp(1im*x[2]) + sin(x[2])
         m[1,2] = exp(1im*x[1]) + sin(x[1])
         m[2,2] = exp(1im*x[2]) + sin(x[1])

         return m
       end
update_matrix_1! (generic function with 1 method)

julia> function high_level_1(a::S, y::Array{T}) where {S, T}
         m = Array{Complex{T}}(repeat([0 + 0im], outer = [2, 2]))
         for i in 1:a
           update_matrix!(m)
         end
         return(real(m[1,1])*y[1])
       end
high_level_1 (generic function with 1 method)

julia> ForwardDiff.gradient(x -> high_level_1(1, x), [1.7])
1-element Array{Float64,1}:
 0.1068166524498424

```

or even this (no declaration of type signatures in function arguments). This also coincides with your solution 🙂

```julia
function update_matrix_2!(m) 
  
  x = [1.2 3.4]
  m[1,1] = exp(1im*x[1]) + sin(x[2])
  m[2,1] = exp(1im*x[2]) + sin(x[2])
  m[1,2] = exp(1im*x[1]) + sin(x[1])
  m[2,2] = exp(1im*x[2]) + sin(x[1])
  
  return m
end

function high_level_2(a, y) 

  m = repeat([0.0 + 0.0im], outer = [2, 2])

  for i in 1:a
    update_matrix!(m)
  end

  return(real(m[1,1])*y[1])
end

julia> ForwardDiff.gradient(x -> high_level_2(1, x), [1.7])
1-element Array{Float64,1}:
 0.1068166524498424

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 30, 2021, 12:51am UTC](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204/26 "2021-04-30T00:51:36Z")

</div>

Just for the record,

```julia
m[1,1] = exp(1im*x[1]) + sin(x[2])
m[2,1] = exp(1im*x[2]) + sin(x[2])
m[1,2] = exp(1im*x[1]) + sin(x[1])
m[2,2] = exp(1im*x[2]) + sin(x[1])

```

could be written as  
`@. m = exp(im*x) + sin(x')` (or `@. m = cis(x) + sin(x')` which will likely be a little faster).

[Previous page](https://discourse.julialang.org/t/initialise-array-without-specific-types/60204.md?page=1)
