# How to know the size of the mutated array in f!(F, x)?

**URL:** <https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 18, 2022, 9:40am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789 "2022-01-18T09:40:49Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 18, 2022, 9:40am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/1 "2022-01-18T09:40:49Z")

</div>

Is there a way to know the size of `F` in

```julia
function f!(F, x)
    F[1] = (x[1] + 3) * (x[2]^3 - 7) + 18
    F[2] = sin(x[2] * exp(x[1]) - 1)
    F[3] = x[1] + x[2]
    return F
end

```

without knowing `F` in advance?

My is to program my own non-linear equation solver, so I have to be flexible to program it in a way that works for any size of `F`. For example, by executing `customnlsolver(f!, x0)` I need to know what size `F` has to be able to configure the Jacobian for AD with `ForwardDiff`.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 18, 2022, 9:50am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/2 "2022-01-18T09:50:39Z")

</div>

does size(F) not work ?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 18, 2022, 9:52am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/3 "2022-01-18T09:52:05Z")

</div>

For that I’d have to know `F` in advance. Starting from something like `customnlsolver(f!, x0)`, I want to know `size(F)`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 18, 2022, 10:36am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/4 "2022-01-18T10:36:14Z")

</div>

Where does `F` come from in `customnlsolver(f!, x0)`?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 18, 2022, 10:41am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/5 "2022-01-18T10:41:07Z")

</div>

I’m trying to reproduce this behavior:

```julia
using NLsolve

nlsolve(f!, [1.0, 1.0, 1.0])

```

I looked at the code of `nlsolve` but I don’t understand how they do it.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 18, 2022, 11:29am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/6 "2022-01-18T11:29:14Z")

</div>

I don’t think it “knows” the dimensions - if you take the example from the Readme with a function with two inputs, you get:

```julia
julia> nlsolve(f!, j!, [0.1; 1.2]) # This is the readme example
Results of Nonlinear Solver Algorithm
(...)
 * Zero: [-3.487552479724522e-16, 1.0000000000000002]
(...)

julia> nlsolve(f!, j!, [0.1 1.2 1.5]) # Passing three initial guesses
Results of Nonlinear Solver Algorithm
(...)
 * Zero: [-3.487552479724522e-16, 1.0000000000000002]
(...)

julia> nlsolve(f!, j!, [0.1]) # Passing a lenght one initial guess
ERROR: BoundsError: attempt to access 1-element Vector{Float64} at index [2]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 18, 2022, 12:53pm UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/7 "2022-01-18T12:53:14Z")

</div>

> [@amrods](#):
>
> My is to program my own non-linear equation solver, so I have to be flexible to program it in a way that works for any size of `F` .

In your nonlinear-equation solver, just infer the dimension of the problem from the size of the initial guess that the user passes to your solver (not from the function object).

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 19, 2022, 3:52am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/8 "2022-01-19T03:52:16Z")

</div>

but for systems in which the number of variables is different than the number of equations, how do I do that?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 19, 2022, 2:07pm UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/9 "2022-01-19T14:07:05Z")

</div>

> [@amrods](#):
>
> but for systems in which the number of variables is different than the number of equations, how do I do that?

If the number of variables doesn’t match the number of equations then you aren’t doing root-finding, you are doing optimization (e.g. least-squares), and you will need an objective function that returns a scalar. In that case, you will be computing the gradient, not the Jacobian, and you don’t need to know the size of the internal state of your scalar objective function.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 19, 2022, 8:59pm UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/10 "2022-01-19T20:59:00Z")

</div>

Initially I got confused by your wording, but now I see what you mean, that a way to proceed is to minimize the norm of the residuals of the system. Thank you.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 20, 2022, 12:12am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/11 "2022-01-20T00:12:28Z")

</div>

In a system F: \mathbb{R}^n \rightarrow \mathbb{R}^n, what are the conditions under which it is better to try to solve F(x)=\vec{0} using root-finding algorithms vs solving the fixed point G(x) = x, with G(x) = F(x) + x?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 20, 2022, 1:47am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/12 "2022-01-20T01:47:12Z")

</div>

Your question makes no sense, because solving a fixed-point equation _is_ a root-finding problem.

Do you mean the specific algorithm of [fixed-point iterations?](https://en.m.wikipedia.org/wiki/Fixed-point_iteration)

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 20, 2022, 1:49am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/13 "2022-01-20T01:49:12Z")

</div>

Yes, the fixed point of G corresponds to the zeros of F, no?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 20, 2022, 3:13am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/14 "2022-01-20T03:13:24Z")

</div>

> [@amrods](#):
>
> Yes, the fixed point of G corresponds to the zeros of F , no?

Yes, that is why I said they are equivalent problems.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 20, 2022, 3:20am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/15 "2022-01-20T03:20:33Z")

</div>

Rephrasing the question (at the risk of putting my foot in my mouth again), what is a “better” approach (say in terms of accuracy, or speed of convergence), directly solving for the zero of F using a nonlinear solver, or iterating on G until an fixed point is found?

---

<div class="post-metadata">

**Author:** ![artkuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artkuo/32/28577_2.png) [@artkuo](https://discourse.julialang.org/u/artkuo)\
**Post date:** [January 20, 2022, 7:00am UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/16 "2022-01-20T07:00:16Z")

</div>

“what is a ‘better’ approach” depends on the characteristics of G. Fixed point iteration can only work if the system is stable, and convergence is only as fast as the stable system’s dynamics (e.g. eigenvalues of linearized G). That’s not an issue for nonlinear solvers, but most will work best if F has a smooth gradient or other known properties. I believe it would take extreme luck for iterations of G to outperform a good nonlinear solver of F, but only you could know.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 20, 2022, 1:24pm UTC](https://discourse.julialang.org/t/how-to-know-the-size-of-the-mutated-array-in-f-f-x/74789/17 "2022-01-20T13:24:01Z")

</div>

> [@amrods](#):
>
> directly solving for the zero of F using a nonlinear solver, or iterating on G until an fixed point is found?

“Iterating on G” with fixed-point iterations _is_ a nonlinear solver. So your question still doesn’t make much literal sense. But I think you mean, “How does fixed-point iteration compare to other nonlinear-solver algorithms?”

[Fixed-point iteration](https://en.wikipedia.org/wiki/Fixed-point_iteration) is one of the least reliable nonlinear-solver algorithms — it only works for contractive maps, and even then is relatively slowly converging compared to e.g. Newton’s method. The main advantages of a fixed-point iteration are that it is easy to implement (which is nontrivial — computer time is cheaper than programmer time), requires minimal storage (no extraneous vectors or matrices), and doesn’t require you to supply a Jacobian matrix.

I would use Newton’s method as my first choice if you have easy/cheap access to the Jacobian (e.g. via [AD](https://en.wikipedia.org/wiki/Automatic_differentiation)). Even without a Jacobian, there are a variety of Jacobian-free algorithms that are probably faster than fixed-point iteration, e.g. [Anderson acceleration](https://en.wikipedia.org/wiki/Anderson_acceleration), the closely related [Broyden’s method](https://en.wikipedia.org/wiki/Broyden%27s_method), or [Newton–Krylov methods](https://en.wikipedia.org/wiki/Newton%E2%80%93Krylov_method) (which only require a [directional derivative](https://en.wikipedia.org/wiki/Directional_derivative) that can be computed by finite differences if needed). These algorithms are more complicated to implement, but that’s what we have libraries for.
