# Type problems in ForwardDiff with preallocations

**URL:** https://discourse.julialang.org/t/type-problems-in-forwarddiff-with-preallocations/118131
**Category:** Numerics
**Tags:** question, forwarddiff, nlsolve
**Created:** [August 13, 2024, 11:37am UTC](https://discourse.julialang.org/t/type-problems-in-forwarddiff-with-preallocations/118131 "2024-08-13T11:37:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Chandrasekhar](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Chandrasekhar](https://discourse.julialang.org/u/Chandrasekhar)
#### Post date: [August 13, 2024, 11:37am UTC](https://discourse.julialang.org/t/type-problems-in-forwarddiff-with-preallocations/118131/1 "2024-08-13T11:37:46Z")

</div>

Hi everyone!  
I am solving a system of non-linear equations by using nlsolve. I want to use ForwardDiff to calculate the Jacobian inside nlsolve. The code which is running without any errors is the following:

```julia
function getvals2D(ϵᶜ, tᶜ, ϵᶠ, tᶠ, V0, guess)
	p=ceil(Int,N/2)
	array_size=Int(p*(p+1)/2)
	function f(F, x::AbstractArray{T}) where T
		be = zeros(eltype(x),N,N)
		λe = zeros(eltype(x),N,N)
		Hp=zeros(Complex{T},2,2,N,N,2,2,N,N)
		Gp=zeros(Complex{T},size,size)
		Gbp=zeros(Complex{T},2,2,N,N,2,2,N,N)
		be=x[:,:,1]
        λe=x[:,:,2]
		for indices in Iterators.product(ranges1...)
			a = equationst(ϵᶜ, tᶜ, ϵᶠ, tᶠ, V0, be, λe, indices, Hp, Gp, Gbp)
    		        F[indices...,1] = real(a[1])
			F[indices...,2] = real(a[2])
		end
	end

	sol = nlsolve(f, guess, method=:newton, autodiff=:forward, iterations = 10, show_trace=true)
	return sol.zero
end

```

I want to remove the allocations from the function f (namely my preallocations of be, Hp, etc.) but I can’t figure out how to do this because I always run into method errors connected to the Dual type of ForwardDiff as soon as I try to define my tensors outside of f. Is there any way I can remove these allocations from f? I can’t figure out what type I have to define them as because even copying the Datatype from the working Code above doesn’t solve the issue.  
Thanks a lot for your help!

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 13, 2024, 12:04pm UTC](https://discourse.julialang.org/t/type-problems-in-forwarddiff-with-preallocations/118131/2 "2024-08-13T12:04:33Z")

</div>

A tip: try to reduce your code to the bare minimum that reproduces the issue. That increases your odds of getting a useful answer.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [August 13, 2024, 12:32pm UTC](https://discourse.julialang.org/t/type-problems-in-forwarddiff-with-preallocations/118131/3 "2024-08-13T12:32:46Z")

</div>

Welcome to the forum! I think this is the package you’re looking for at the moment:

> **[GitHub - SciML/PreallocationTools.jl: Tools for building non-allocating pre-cached...](https://github.com/SciML/PreallocationTools.jl)**
>
> Tools for building non-allocating pre-cached functions in Julia, allowing for GC-free usage of automatic differentiation in complex codes
