# Dot notation / vectorized code syntax

**URL:** <https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194>\
**Category:** New to Julia\
**Created:** [June 19, 2021, 8:01pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194 "2021-06-19T20:01:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [June 19, 2021, 8:01pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194/1 "2021-06-19T20:01:04Z")

</div>

Hi all,

I’m having trouble with dot notation. Can someone show me how to write the below code without expanding the square? I included a working example below, and then the code I would like to write that’s throwing an error.

This works:  
‘’’  
model(x)=exp.(x.^2)  
model([1,1])  
‘’’  
however the following fails  
model(x)=exp.((x.-2)^2)  
model([1,1])

Thanks!

---

<div class="post-metadata">

**Author:** ![LinasBa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/linasba/32/20957_2.png) [@LinasBa](https://discourse.julialang.org/u/LinasBa)\
**Post date:** [June 19, 2021, 8:11pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194/2 "2021-06-19T20:11:17Z")

</div>

> [@406700](#):
>
> model(x)=exp.((x.-2)^2)  
> model([1,1])

```julia
 model(x)=exp.((x.-2).^2)

```

or

```julia
model(x)= @. exp((x-2)^2)

```

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [June 19, 2021, 8:13pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194/3 "2021-06-19T20:13:24Z")

</div>

Great, thanks for the quick reply! I need to take some time to review the dot notation properly.

---

<div class="post-metadata">

**Author:** ![ggarza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggarza/32/2654_2.png) [@ggarza](https://discourse.julialang.org/u/ggarza)\
**Post date:** [June 19, 2021, 8:18pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194/4 "2021-06-19T20:18:41Z")

</div>

The best way to write this is to write the function without the “dot” syntax. Then apply the dot when calling the function.

```julia
model(x)=exp((x-2)^2)
model.([1,1])

```

It’s also easier to reason about the function this way. You can easily test whether or not the function gives the expected output.

---

<div class="post-metadata">

**Author:** ![ggarza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggarza/32/2654_2.png) [@ggarza](https://discourse.julialang.org/u/ggarza)\
**Post date:** [June 19, 2021, 8:37pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194/5 "2021-06-19T20:37:51Z")

</div>

Another example: suppose you were studying the function f(x)=x^2. It is more useful to define f as

```julia
f(x)=x^2

```

than it is to define f as

```julia
f(x) = x.^2

```

With the first form you can evaluate f at a single value or over an array of values.

```julia
f(0)
# or 
f.([1, 2, 3])

```

You also can’t easily plot f if you use the second form.

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [June 19, 2021, 8:38pm UTC](https://discourse.julialang.org/t/dot-notation-vectorized-code-syntax/63194/6 "2021-06-19T20:38:44Z")

</div>

In this case I’m using the model in the LsqFit package, so I don’t have control over how it’s called. This way does look cleaner to me, so thanks for pointing out this alternative.
