# Problem with ForwardDiff and pre-allocating vector

**URL:** <https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741>\
**Category:** Numerics\
**Created:** [December 13, 2017, 4:08pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741 "2017-12-13T16:08:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [December 13, 2017, 4:08pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/1 "2017-12-13T16:08:19Z")

</div>

I’m attempting to use ForwardDiff in a function that involves several instances of pre-allocating vectors. I’ve boiled the function down to the cause of the bug

```julia
using ForwardDiff

function f(x) 
    tmp = zeros(2)

    for i in 1:2
        tmp[i] = x[i] * i
    end

    return tmp
end

ForwardDiff.jacobian(f, [4.7 5.7])

```

Any ideas on how I can fix this issue? In this case, rewriting the function as

```julia
function f(x) 
    tmp = [x[1] * 1 x[2] * 2]

    return tmp
end

```

fixes the problem, but in my use case, this solution isn’t feasible since the size of the output vector depends on other inputs.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 13, 2017, 4:15pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/2 "2017-12-13T16:15:03Z")

</div>

```julia
function f(x::T) where T
  tmp = zeros(T, 2)
...

```

that way tmp has the same type as `x`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 13, 2017, 4:22pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/3 "2017-12-13T16:22:46Z")

</div>

> [@jacobcvt12](#):
>
> I’ve boiled the function down to the cause of the bug

It’s not a bug. It’s because you need to have things typed to handle Dual numbers when `x` is `Dual`. If you want to pre-allocate, you need to pre-allocate for both the `Dual` and non-dual type, and use dispatch to choose the cache. You’ll likely need to disable tagging for that to work, or use a non-function-based tag.

Note that supposedly a Cassette.jl version of ForwardDiff.jl won’t have the Dual typing constraint, so that may make this easier when it comes out.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [December 13, 2017, 5:09pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/4 "2017-12-13T17:09:22Z")

</div>

I think you need `zeros(eltype(x), 2)` since `x` itself is a `Vector` in the example.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 13, 2017, 5:14pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/5 "2017-12-13T17:14:57Z")

</div>

> [@tkoolen](#):
>
> I think you need zeros(eltype(x), 2) since x itself is a Vector in the example.

You can also just do `zeros(x)`.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [December 13, 2017, 7:47pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/6 "2017-12-13T19:47:54Z")

</div>

> You can also just do zeros(x).

Yes, in 0.6.1. It seems to be going away in 0.7, however. (Or did I get this wrong?)

---

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [December 14, 2017, 8:37pm UTC](https://discourse.julialang.org/t/problem-with-forwarddiff-and-pre-allocating-vector/7741/7 "2017-12-14T20:37:42Z")

</div>

Thanks for the responses everyone! I was able to get my problem working!
