# Help with dot macro broadcasting

**URL:** <https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729>\
**Category:** General Usage\
**Tags:** optim, broadcasting\
**Created:** [April 4, 2019, 2:52am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729 "2019-04-04T02:52:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mosiman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mosiman/32/6959_2.png) [@mosiman](https://discourse.julialang.org/u/mosiman)\
**Post date:** [April 4, 2019, 2:52am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729/1 "2019-04-04T02:52:01Z")

</div>

Hi there,

I’m trying to minimize a residual function `f` using LsqFit.jl since it is the only package that uses Levenberg-Marquardt (trying to do a course where the instructions and assignments assume Matlab usage, and we were explicitly asked to use LM).

```julia
function residual(t,k,p)
    ...
end

```

which is a function of two variables `t` and `k`, and I want to optimize the parameter set `p`.

I’ve run through the single variable example, but still trying to wrap my head around the multivariate example.

I’ve tried doing something like this:

```julia
@. model(x,p) = residual(x[:,1], x[:,2], p)
xdata = rand(2,2)
ydata = rand(2)
p = rand(6)
fit = curve_fit(model, xdata, ydata, p)

```

But end up with a Dimension Mismatch error during broadcasting.

I’ve narrowed the dimension mismatch error to just calling the model

```julia
model(xdata, p)

```

but I have no idea why.

Simple functions like

```julia
@. model(x,p) = x[:,1] + x[:,2] + p[1]

```

evaluate correctly. I think I’m just not understanding the `@.` broadcasting stuff? Would be great if I could get some clarification.

Thanks!

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [April 4, 2019, 3:44am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729/2 "2019-04-04T03:44:31Z")

</div>

`@.` converts every function call into a dot call. So you are effectively calling `residual.(x[:,1], x[:,2], p)`. Here you have the dimension mismatch: `x[:,1]` and `x[:,2]` are length 2 but `p` is length 6.

---

<div class="post-metadata">

**Author:** ![mosiman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mosiman/32/6959_2.png) [@mosiman](https://discourse.julialang.org/u/mosiman)\
**Post date:** [April 4, 2019, 3:50am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729/3 "2019-04-04T03:50:00Z")

</div>

Is there a way to leave `p` out of the dot call?

edit:

To be more clear, the example on `LsqFit.jl` has us do

```julia
@. model(x, p) = p[1]*exp(-x*p[2])
xdata = range(0, stop=10, length=20)
ydata = model(xdata, [1.0 2.0]) + 0.01*randn(length(xdata))
p0 = [0.5, 0.5]

```

and evaluating works fine

```julia
model(xdata, p0)

```

even though `xdata` and `p0` have different dimensions, so why isn’t a dimension mismatch happening here?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 4, 2019, 3:54am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729/4 "2019-04-04T03:54:10Z")

</div>

for what i understand of broadcasting, normaly there is broadcast with one variable, so, if you have:

```julia
f(x)=2x

```

the broadcasting works by applying the function over the entire vector:

```julia
f.(rand(6,6))= 2*rand(6,6)

```

in LM, you have a vector f(x,p) = y where x an y are values, and p is a set of parameters(where X and Y can have a lot of values, the length of the vector p is defined by the formulation of the function. by broadcasting, you are setting an operation that executes the operation f over a array of values(lets say X), giving an array of results, (Y).

Now, i will suppose that` residual(t,k,p)` operates over a single value `t` and `k`, using the parameters in `p`., using the example in Lsqfit.jl:

```julia
residual(t,k,p) = (x, p) = p[1]*exp(-t*p[2]+k*p[3]) #without the @. macro, 
#just from single values t and k to a single value y = residual(t,k,p) 

```

now coming to julia, and understanding that we iterate over x and not over p, we can do a simple loop:

```julia
function multimodel(x,p) 
y = zeros(size(x)[1])
    for i = 1:size(x)[1] #length of x
    y[i] = residual(x[i,1],x[i,2],p)
    end
return y
end

```

there are ways to vectorize and mantain p, like using `Ref()` , but I’m not an expert julia programer myself 😄 , so i will go with a for loop, because loops in julia are fast.

finaly, using the same data:

```julia
xdata = rand(2,2)
ydata = rand(2)
p = rand(3)
fit = curve_fit(model, xdata, ydata, p)

```

can give you a result. (it gave me one, at least)

Maybe is longer that expected, but it works, and you understand what is happening

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [April 4, 2019, 4:14am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729/5 "2019-04-04T04:14:58Z")

</div>

As [longemen3000](https://discourse.julialang.org/u/longemen3000) said, you can use `Ref`

```julia
model(x,p) = residual.(x[:,1], x[:,2], Ref(p))

```

`LsqFit.jl` example is slightly different. There, the right hand side is simply converted to

```julia
p[1].*exp.(-x.*p[2])

```

So `p[1]` and `p[2]` are broadcasted as scalars, i.e they are expanded to match the dimension of `x`.

---

<div class="post-metadata">

**Author:** ![mosiman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mosiman/32/6959_2.png) [@mosiman](https://discourse.julialang.org/u/mosiman)\
**Post date:** [April 4, 2019, 4:16am UTC](https://discourse.julialang.org/t/help-with-dot-macro-broadcasting/22729/6 "2019-04-04T04:16:09Z")

</div>

Thank you! your `multimodel` function explained to me clearly what the `@.` is doing
