By βworkβ I think you mean you want it to broadcast. Be aware though that this is not the only meaning possible β Julia takes matrices (and vectors) seriously, and while in this case you get an error that raising a vector to a power doesnβt make sense, you can also silently get answers you may not expect. Raising a matrix to a power for instance does make sense, it means repeated *
:
julia> [1 2; 3 4]^2 # == [1 2; 3 4] * [1 2; 3 4]
2Γ2 Matrix{Int64}:
7 10
15 22
julia> [1 2; 3 4].^2
2Γ2 Matrix{Int64}:
1 4
9 16
Often the right solution will be to broadcast the whole function when you call it. But this wonβt work so well with un-packing pairs of arguments. You could change your function to avoid that. Or you can include broadcasting inside your function, which will do nothing in the scalar case:
julia> f1(x, (y, z)) = x * y^2 / z;
julia> f1(1, (2, 3))
1.3333333333333333
julia> f1(1, (2, [3,4,5])) # this already has a meaning
1Γ3 transpose(::Vector{Float64}) with eltype Float64:
0.24 0.32 0.4
julia> f1(1, ([2,3,4], 5)) # as in question
ERROR: MethodError: no method matching ^(::Vector{Int64}, ::Int64)
julia> f1.(1, ([2,3,4], 5)) # can't broadcast the whole function
ERROR: BoundsError: attempt to access Int64 at index [2]
julia> f2(x, (y, z)) = @. x * y^2 / z;
julia> f2(1, (2, 3)) # same as f1
1.3333333333333333
julia> f2(1, (2, [3,4,5])) # NB, this is elementwise ./ not linearsolve /
3-element Vector{Float64}:
1.3333333333333333
1.0
0.8
julia> f2(1, ([2,3,4], 5))
3-element Vector{Float64}:
0.8
1.8
3.2
julia> @code_llvm debuginfo=:none f1(1, (2, 3)) # compare this to next...
define double @julia_f1_9765(i64 signext %0, [2 x i64]* nocapture nonnull readonly align 8 dereferenceable(16) %1) #0 {
top:
%2 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0
%3 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 1
%4 = load i64, i64* %2, align 8
%5 = mul i64 %4, %0
%6 = mul i64 %5, %4
%7 = sitofp i64 %6 to double
%8 = load i64, i64* %3, align 8
%9 = sitofp i64 %8 to double
%10 = fdiv double %7, %9
ret double %10
}
julia> @code_llvm debuginfo=:none f2(1, (2, 3)) # ... check that broadcast compiles away
define double @julia_f2_9769(i64 signext %0, [2 x i64]* nocapture nonnull readonly align 8 dereferenceable(16) %1) #0 {
top:
%2 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0
%3 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 1
%4 = load i64, i64* %2, align 8
%5 = mul i64 %4, %0
%6 = mul i64 %5, %4
%7 = sitofp i64 %6 to double
%8 = load i64, i64* %3, align 8
%9 = sitofp i64 %8 to double
%10 = fdiv double %7, %9
ret double %10
}