Hi, I am having some issues with custom broadcasting working with the power (^) operator when I am trying to use an integer literal as the exponent. My broadcast methods seem to work fine for other methods such as + or * or - with integer literals.
The exponent works with literal floats, or explicit integers, but when using an integer float it seems to be trying to broadcast with a Base.RefValue{Val{2}} instead of just an integer, which doesn’t happen with the other methods. I’m not sure why this is happening, I am sure there are workarounds I can do (such as just using float literals) but I’d like to be able to just do .^2 on my struct as expected.
I have uploaded a full reproducible file on gist, and below is a snippet of the relevant code and the stacktrace.
function Base.broadcasted(::VariableStyle, ::typeof(+), a::Variable, b::Union{AbstractArray,Number})
Variable(a.data .+ b)
end
println(Variable([1 2 3]) .+ 5) # Works
function Base.broadcasted(::VariableStyle, ::typeof(^), a::Variable, b::Number)
Variable(a.data .^ b)
end
println("1 ", Variable([1 2 3]) .^ 2.) # Works
println("2 ", Variable([1 2 3]) .^ convert(Int, 2)) # Works
two = 2
println("3 ", Variable([1 2 3]) .^ two) # Works
println("4 ", [1 2 3] .^ 2) # Works
println("5 ", Variable([1 2 3]) .^ 2) # Doesn't work
This prints up until print("5.... upon which it throws an error
ERROR: LoadError: MethodError: no method matching size(::VariableArray)
Closest candidates are:
size(::Union{LinearAlgebra.Adjoint{T, var"#s972"}, LinearAlgebra.Transpose{T, var"#s972"}} where {T, var"#s972"<:(AbstractVector)})
@ LinearAlgebra ~/.julia/juliaup/julia-1.9.3+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:296
size(::Union{LinearAlgebra.Adjoint{T, var"#s972"}, LinearAlgebra.Transpose{T, var"#s972"}} where {T, var"#s972"<:(AbstractMatrix)})
@ LinearAlgebra ~/.julia/juliaup/julia-1.9.3+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:297
size(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted})
@ LinearAlgebra ~/.julia/juliaup/julia-1.9.3+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/qr.jl:582
...
Stacktrace:
[1] axes
@ ./abstractarray.jl:98 [inlined]
[2] combine_axes
@ ./broadcast.jl:512 [inlined]
[3] combine_axes
@ ./broadcast.jl:511 [inlined]
[4] instantiate
@ ./broadcast.jl:294 [inlined]
[5] materialize(bc::Base.Broadcast.Broadcasted{VariableStyle, Nothing, typeof(Base.literal_pow), Tuple{Base.RefValue{typeof(^)}, VariableArray, Base.RefValue{Val{2}}}})
@ Base.Broadcast ./broadcast.jl:873
[6] top-level scope
@ ~/code/Bijoulia/reproducable.jl:52
in expression starting at /home/ybbat/code/Bijoulia/reproducable.jl:52
(I am aware that this error is caused by not having a size method, which I did not include in the reproducible file, but it is stacktrace [5] I am concerned about because this should not be what is being called)
Throwing a random error within the + broadcast method reveals a much different stacktrace, wherein broadcasted is called as expected with the function, VariableArray and Int64.