Let's say I have a function named trig, which returns two outputs:
function trig(x)
return(sin(x), cos(x))
end
If I want to evaluate trig over many values, I can use the map function:
julia> out = map(trig, (0:(pi/12):(pi/2)))
out is a 7 element array and in each element, there is a tuple containing two elements:
julia> out
7-element Array{Tuple{Float64,Float64},1}:
(0.0,1.0)
(0.258819,0.965926)
(0.5,0.866025)
(0.707107,0.707107)
(0.866025,0.5)
(0.965926,0.258819)
(1.0,6.12323e-17)
My question is: What is the best way to disentangle my sines and cosines so that I have two arrays with 7 elements each? Is it possible to broadcast trig without creating the superfluous array of tuples and instead directly create the two arrays that I am actually interested in?
For the time being, I am invoking map again in order to extract values from out in order to populate the arrays I want, but I don't think it is the best way to do this:
sines = map(x->x[1], out)
cosines = map(x->x[2], out)
For the purpose of this question, assume trig is a computationally expensive function. So, please do not give me an answer that requires trig to be evaluated more than once.