How to unvectorize a vector to use it as an argument of a function

Hello.

I am trying to use a vector output of some function as an input of another function.
For example, if ϕₛₑᵣᵢₑₛ₄ is a function that has 17 inputs and coef is a vector of 15 elements, I want to write down ϕₛₑᵣᵢₑₛ₄(coef,input_16,input_17).
But when I do this I get an error that shows that the last 14 inputs are not being used, which means coef is just passed as a single argument.
I think this would be very elementary question, but can someone let me know how to put coef as an input easily than just writing down (coef[1],coef[2],...,coef[15])?
Please let me know.

This is what “splatting” is for. This should work:

ϕₛₑᵣᵢₑₛ₄(coef..., input_16,input_17)
2 Likes

It worked! Thanks a lot!!

Best,
Wooheon.

1 Like

As a remark, splatting a Vector almost always results in suboptimal performance (due to dynamic dispatch). You might instead consider indexing coef inside the function rather than splatting outside. I.e., rather than

function ϕ(coef1, coef2, coef3, coef4, input1, input2)
  # do stuff
end

do something like

function ϕ(coefs, input1, input2)
  coef1 = coefs[1] # destructure by indexing
  coef2 = coefs[2]
  coef3 = coefs[3]
  coef4 = coefs[4]
  # do stuff
end

or

function ϕ(coefs, input1, input2)
  coef1, coef2, coef3, coef4 = coefs # destructure by iteration
  # do stuff
end

This isn’t too different to define as a function than one that takes each coef as its own input. But this avoids the performance pitfalls of splatting.

Alternatively, if you’re going to splat, consider using a Tuple instead of Vector and splatting that instead. Since a Tuple’s type defines its size, they tend to splat more performantly at small sizes.

3 Likes

Thanks a lot for the instructive examples and explanation. I appreciate it a lot!

Best,
Wooheon