# How to vectorize only one argument in function call

**URL:** https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771
**Category:** General Usage
**Tags:** question, broadcasting
**Created:** [September 4, 2022, 8:36pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771 "2022-09-04T20:36:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [September 4, 2022, 8:36pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/1 "2022-09-04T20:36:22Z")

</div>

Say I have a function that accepts several vectors` f(a,b,c)`, but I have a vector of inputs for one of the arguments `a`. Can I call f like` f(.a,b,c)` such that f will vectorize only over `a`.  
Since if I call `f.(a,b,c)` f vectorizes over `a`, `b`, and `c` and returns nonsense.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 4, 2022, 8:43pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/2 "2022-09-04T20:43:12Z")

</div>

I’ve seen

`f.(a,Ref(b),Ref(c))`

---

<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: [September 4, 2022, 8:47pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/3 "2022-09-04T20:47:48Z")

</div>

You can protect arguments against broadcasting by wrapping them in a container. You should probably use `Ref` or a tuple, like this:

```julia
f.(a, Ref(b), Ref(c))
f.(a, (b,), (c,))

```

You could even use a vector:

```julia
f.(a, [b], [c]) 

```

though that would be less efficient.

When you wrap it in an outer container, broadcasting happens over the outer layer, leaving the contents as is.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [September 4, 2022, 8:50pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/4 "2022-09-04T20:50:10Z")

</div>

If `b` and `c` are scalars, broadcast works the way you hope:

```julia
julia> f(a, b, c) = b*a .+ c
f (generic function with 1 method)

julia> f.(1:3, 1, 0)
3-element Vector{Int64}:
 1
 2
 3

```

If one is a vector and you need to explicity treat it as a scalar, you can wrap it in a [`Ref`](https://docs.julialang.org/en/v1/base/c/#Core.Ref)

```julia
julia> f.(1:3, 1, [0, 1])
ERROR: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 3 and 2
<... omitted stack trace here ...>

julia> f.(1:3, 1, Ref([0, 1]))
3-element Vector{Vector{Int64}}:
 [1, 2]
 [2, 3]
 [3, 4]

```

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [September 5, 2022, 11:27am UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/5 "2022-09-05T11:27:18Z")

</div>

Why is it less efficient to wrap it in a vector?

---

<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: [September 5, 2022, 11:30am UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/6 "2022-09-05T11:30:14Z")

</div>

A vector allocates memory on the heap with a pointer to your array, and must eventually be garbage collected. It’s not so much, but it’s an unnecessary waste that can sometimes grow into a significant cost if it happens often enough, or which may stop some compiler optimizations from happening.

Wrapping it in, say, a tuple, is essentially zero-cost.

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [September 5, 2022, 12:46pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/7 "2022-09-05T12:46:56Z")

</div>

This is one area in which I wish I could use JAX’s notation for `vmap` i.e.  
`vmap(f)(a, b, c)` which by default broadcasts `f` over the first (axis of the first) input, or explicitly I could do  
`vmap(f, (0, 1, None))(a, b, c)` which broadcasts `f` over the first axis of `a`, the second axis of `b`, and doesn’t broadcast over `c` at all.

---

<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: [September 5, 2022, 3:44pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/8 "2022-09-05T15:44:51Z")

</div>

How do you designate broadcasting over all axes of an argument?

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [September 5, 2022, 4:07pm UTC](https://discourse.julialang.org/t/how-to-vectorize-only-one-argument-in-function-call/86771/9 "2022-09-05T16:07:39Z")

</div>

That’s actually restriction of JAX: you need to write one `vmap` per dimension of array to map over.

For my usecase, I actually wrote a helper function called `antivmap` which acts as a `vmap` over all but the specified axes and I’ve found it super useful
