With reference to my last post , I am trying to solve a set of non-linear equations known as the Coupled Cluster Equations in Quantum Chemistry using the NLSolve.jl package. As per the reply of @alfaromartino , in the same post, I have restructured my code in a way that is compatible with `nlsolve()`

method from NlSolve.jl. Below is the code

## First Code

```
using NLsolve
include("cc-helper.jl")
function compute_parameters()
R2u,R2,g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,K,fvv,foo,erhf = initialize_cc_variables();
tup = g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,fvv,foo,R2u,R2,erhf
return tup
end
function compute_result(tup)
g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,fvv,foo,R2u,R2,erhf = tup
function foo1(T2)
# This is a placeholder function. I had to write it this way because this is the only type pf function that nlsolve accepts
calculate_residual(g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,T2,fvv,foo,R2u,R2)
end
function nl_solve()
# Cannot understand what to write here, for now let's leave it empty
end
# OR here
T2 = initialize_t2_only();
sol=nlsolve(foo1, T2)
finalamps = sol.zero;
ECCD = calculate_ECCD(g_oovv,finalamps);
etot = ECCD[1]+erhf;
return finalamps,etot;
end
tup = compute_parameters();
@time finalamps,etot=compute_result(tup);
```

Now this produces the expected result without any error, however, this is not memory efficient as the residuals can be passed as an argument as given in the documentation of the package and also advised by @alfaromartino . So, I restructured my code to achieve the same as given below

## New code

```
using NLsolve
include("cc-helper.jl")
function compute_parameters()
R2u,R2,g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,K,fvv,foo,erhf = initialize_cc_variables();
tup = g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,fvv,foo,R2u,R2,erhf
return tup
end
function compute_result(tup)
g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,fvv,foo,R2u,R2,erhf = tup
function foo1(F::AbstractArray{Float64,4},T2::AbstractArray{Float64,4})
# This is a placeholder function. I had to write it this way because this is the only type pf function that nlsolve accepts
@views F[:,:,:,:]=calculate_residual(g_vvoo,g_voov,g_vovo,g_oovv,g_oooo,g_vvvv,T2,fvv,foo,R2u,R2)
end
function nl_solve()
# Cannot understand what to write here, for now let's leave it empty
end
# OR here
T2 = initialize_t2_only();
sol=nlsolve(foo1, T2)
finalamps = sol.zero;
ECCD = calculate_ECCD(g_oovv,finalamps);
etot = ECCD[1]+erhf;
return finalamps,etot;
end
tup = compute_parameters();
@time finalamps,etot=compute_result(tup);
```

However, the I observe no significant change in allocations between these two functions. Both report (excluding compilation time)

```
0.611511 seconds (3.78 M allocations: 286.140 MiB, 8.17% gc time)
```

I even used the `@views`

macro to ensure that the splicing syntax does not initialize a new tensor and instead reuses the memory. Any ideas what is going on up here and what I might do to fix this?