# Scalar function for reals and arrays: single function definition

**URL:** https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886
**Category:** Performance
**Tags:** question
**Created:** [February 28, 2024, 10:51am UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886 "2024-02-28T10:51:57Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![abehersan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abehersan/32/47221_2.png) [@abehersan](https://discourse.julialang.org/u/abehersan)
#### Post date: [February 28, 2024, 10:51am UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/1 "2024-02-28T10:51:57Z")

</div>

I often find myself writing two methods for a simple scalar function. E.g.

```julia
function gauss(x::Real; a=1.0, c=0.0, s=0.7)
    return a/s/(sqrt(2pi))*exp(-((x-c)^2/(2*s^2)))
end

function gauss(x::AbstractArray{<:Real}; a=1.0, c=0.0, s=0.7)
    return [gauss(xx, a=a, c=c, s=s) for xx in x]
end

xs = -5:0.01:5
ys = gauss(xs)

```

Could this be reduced to a single function definition that dispatches on single scalar types and arrays of reals?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [February 28, 2024, 11:01am UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/2 "2024-02-28T11:01:04Z")

</div>

Could you use broadcasting `ys = gauss.(xs)`? That’d be the most common idiomatic way to make scalar functions work on each element of an array. Otherwise, I think you’d need to make extra forwarding methods like so.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 28, 2024, 11:18am UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/3 "2024-02-28T11:18:32Z")

</div>

The point being: don’t make a special version for arrays at all (not even one that uses broadcasting internally), only make the scalar version, and _call_ it with dot broadcasting.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [February 28, 2024, 11:25am UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/4 "2024-02-28T11:25:54Z")

</div>

Exception: an array, likely with a static size, is the indivisible data point, in which case you’d make a function working on them and could broadcast it over arrays of such arrays.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 28, 2024, 11:49am UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/5 "2024-02-28T11:49:26Z")

</div>

Oh, sure, functions that are inherently meant to take arrays as a fundamental working unit (like e.g. linear algebra, too). But functions that are inherently scalar ‘by nature’ should just be defined scalars, and broadcasted at the call site.

Another possible exception is if there are common calculations that can be avoided by specializing, or other significant optimization opportunities.

---

<div class="post-metadata">

### Author: ![abehersan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abehersan/32/47221_2.png) [@abehersan](https://discourse.julialang.org/u/abehersan)
#### Post date: [February 28, 2024, 12:10pm UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/6 "2024-02-28T12:10:05Z")

</div>

Thanks a lot for your comments! (:

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 28, 2024, 12:13pm UTC](https://discourse.julialang.org/t/scalar-function-for-reals-and-arrays-single-function-definition/110886/7 "2024-02-28T12:13:33Z")

</div>

Conflating 1-element vectors and scalars is tempting, and useful in many cases, but keeping stricter separation promotes clarity and may circumvent some bugs through static analysis and better design. The latter seems to weigh heavier in most cases.
