# Why is the argument of vectorized in-place function is marked as not used?

**URL:** https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356
**Category:** New to Julia
**Tags:** question, function, functions, ordinarydiffeq
**Created:** [October 24, 2023, 7:45pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356 "2023-10-24T19:45:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 24, 2023, 7:45pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356/1 "2023-10-24T19:45:15Z")

</div>

Following the [tutorial](https://docs.sciml.ai/DiffEqDocs/stable/getting_started/#Defining-Parameterized-Functions), I want to use OrdinaryDiffEq to solve an equation:

```julia
using OrdinaryDiffEq

function A(t, p)
    ω, ε = p
    return [[0.0 1.0]; [-(ω + ε * cos(t)) 0.0]]
end

function mathieu!(du, u, p, t)
    du = A(t, p) * u
end

u0 = [1.0, 1.0]
p = [1.0, 1.0]
tspan = (0.0, 20 * π)
prob = ODEProblem(mathieu!, u0, tspan, p)
sol = solve(prob, Tsit5())

```

Such definition doesn’t produce the correct result, and the linter in VS Code warns about `du`:

> An argument is included in a function signature but not used within its body.Julia(UnusedFunctionArgument)

However, if I define the system like in the tutorial

```julia
function mathieu!(du, u, p, t)
    du[1] = u[2]
    du[2] = # ...
end

```

everything works fine.

Why does my definition not work? If the `u` vector had 1000 of components, would I have to write every one of them by hand?

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [October 24, 2023, 7:48pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356/2 "2023-10-24T19:48:01Z")

</div>

You need to use `.=` instead of `=` to reuse the memory in `du`—using `=` will just create a new local variable called `du`.

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 24, 2023, 7:49pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356/3 "2023-10-24T19:49:57Z")

</div>

That is, assignment operators are different for scalars and vectors, and only in in-place functions?

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [October 24, 2023, 8:00pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356/4 "2023-10-24T20:00:49Z")

</div>

Not just in in-place functions or for vectors. In general, `x = …` says “attach the label `x` to `…`.” If `x` is an object that already exists, this is different from changing/mutating the object `x`!

What `.=` says instead is “do `x[i] = …` for every `i`. Unlike `x = …`, `x[i] = ...` really does change `x`!

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [October 24, 2023, 8:23pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356/5 "2023-10-24T20:23:25Z")

</div>

> [@homocomputeris](#):
>
> `du = A(t, p) * u`

While `du .= A(t, p) * u` would work here, use `mul!(du, A(t, p), u)` to avoid allocation in that operation altogether. You will need to `using LinearAlgebra` to access that function.

> [@homocomputeris](#):
>
> `return [[0.0 1.0]; [-(ω + ε * cos(t)) 0.0]]`

You should be able to write this more simply (and likely with fewer allocations) as `return [0 1; -(ω + ε * cos(t)) 0]`. Or, if you don’t mind it being a little less “clean” you could write out the operation manually as

```julia
function mathieu!(du, u, p, t)
    du[1] = u[2]
    du[2] = -(p[1] + p[2] * cos(t)) * u[1]
    return du
end

```

Since you’re working with tiny vectors and matrices, you might eventually check out [`StaticArrays.jl`](https://github.com/JuliaArrays/StaticArrays.jl). They can be considerably faster at small sizes. Although it may take a little work to adapt this to it, so only try that once you have this working and if you think a little extra performance is useful.

---

<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: [October 24, 2023, 8:26pm UTC](https://discourse.julialang.org/t/why-is-the-argument-of-vectorized-in-place-function-is-marked-as-not-used/105356/6 "2023-10-24T20:26:40Z")

</div>

> [@homocomputeris](#):
>
> That is, assignment operators are different for scalars and vectors, and only in in-place functions?

I think you’re misunderstanding what assignment does. See the [manual section on “assignment vs mutation”](https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions).
