# Julia's equivalent to Fortran elemental functions

**URL:** https://discourse.julialang.org/t/julias-equivalent-to-fortran-elemental-functions/2576
**Category:** General Usage
**Created:** [March 9, 2017, 8:07pm UTC](https://discourse.julialang.org/t/julias-equivalent-to-fortran-elemental-functions/2576 "2017-03-09T20:07:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![PieterjanRobbe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pieterjanrobbe/32/3298_2.png) [@PieterjanRobbe](https://discourse.julialang.org/u/PieterjanRobbe)
#### Post date: [March 9, 2017, 8:07pm UTC](https://discourse.julialang.org/t/julias-equivalent-to-fortran-elemental-functions/2576/1 "2017-03-09T20:07:10Z")

</div>

Fortran has [elemental functions](http://fortranwiki.org/fortran/show/elemental).

This means that you can define a function for _primitive_ types (int, double…) and they will automatically work for vectors and matrices that contain these primitive types.

Consider the _elemental_ function `pos(x::Number) = x < 0 ? zero(x) : x`

This is the closest I get using Julia:

```julia
pos(x) = reshape([pos(x[i]) for i = 1:length(x)],size(x))

```

Now `pos` works for scalars of type `Number`, vectors, matrices, ranges…

Are there alternatives?

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [March 9, 2017, 8:18pm UTC](https://discourse.julialang.org/t/julias-equivalent-to-fortran-elemental-functions/2576/2 "2017-03-09T20:18:36Z")

</div>

You can vectorize any function by adding a dot:

```julia
pos.(my_array)

```

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [March 9, 2017, 8:19pm UTC](https://discourse.julialang.org/t/julias-equivalent-to-fortran-elemental-functions/2576/3 "2017-03-09T20:19:35Z")

</div>

In 0.5 and later, this is exactly what `.` broadcasting is for:

```julia
julia> pos(x::Number) = x < 0 ? zero(x) : x
pos (generic function with 1 method)

julia> pos.(randn(10))
10-element Array{Float64,1}:
 0.0     
 0.0     
 0.0     
 0.0     
 0.0     
 0.455044
 0.0     
 0.639169
 0.636522
 1.07678 

```

See [Steven G. Johnson’s excellent blog post](http://julialang.org/blog/2017/01/moredots) for more details.

---

<div class="post-metadata">

### Author: ![PieterjanRobbe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pieterjanrobbe/32/3298_2.png) [@PieterjanRobbe](https://discourse.julialang.org/u/PieterjanRobbe)
#### Post date: [March 9, 2017, 8:50pm UTC](https://discourse.julialang.org/t/julias-equivalent-to-fortran-elemental-functions/2576/4 "2017-03-09T20:50:43Z")

</div>

Simply amazing.

Thanks!
