I really like conforming/broadcasting, especially when it is explicit and when the scalar version still makes sense.
I wonder if Lil allows conforming 3 or more lists together. If it does, what is the rule for which lists to reshape?
Here’s how explicit broadcasting can work in Julia
# A naive, scalar linear interpolation function
julia> lerp(x, y, z) = x + z*(y - x)
lerp (generic function with 1 method)
julia> x = [10 20 30]
1×3 Matrix{Int64}:
10 20 30
julia> y = [40 10 20]
1×3 Matrix{Int64}:
40 10 20
# A range literal, converted to a column vector so that you can see it
# is a different shape than x and y. Need row and column vectors
# because that's how matrix math works.
julia> z = [0:.2:1;]
6-element Vector{Float64}:
0.0
0.2
0.4
0.6
0.8
1.0
# Doesn't work because z*(y-x) is a 6x3 matrix and you can't add a 1x3 vector
# to that because addition is undefined for matrixes with different shapes by
# the conventional rules of linear algebra.
julia> lerp(x, y, z)
ERROR: DimensionMismatch: dimensions must match: a has dims (Base.OneTo(1), Base.OneTo(3)), b has dims (Base.OneTo(6), Base.OneTo(3)), mismatch at 1
Stacktrace:
[1] promote_shape
@ ./indices.jl:178 [inlined]
[2] promote_shape(a::Matrix{Int64}, b::Matrix{Float64})
@ Base ./indices.jl:169
[3] +(A::Matrix{Int64}, Bs::Matrix{Float64})
@ Base ./arraymath.jl:14
[4] lerp(x::Matrix{Int64}, y::Matrix{Int64}, z::Vector{Float64})
@ Main ./REPL[34]:1
[5] top-level scope
@ REPL[35]:1
# But you can get the right result if you explicitly broadcast by adding a dot
# before the parenthesis.
julia> lerp.(x, y, z)
6×3 Matrix{Float64}:
10.0 20.0 30.0
16.0 18.0 28.0
22.0 16.0 26.0
28.0 14.0 24.0
34.0 12.0 22.0
40.0 10.0 20.0
# Or the function author could have written a function that works for both the
# scalar and non-scalar cases by broadcasting the addition and subtraction:
julia> lerp(x, y, z) = x .+ z*(y .- x)
lerp (generic function with 1 method)
I really like conforming/broadcasting, especially when it is explicit and when the scalar version still makes sense.
I wonder if Lil allows conforming 3 or more lists together. If it does, what is the rule for which lists to reshape?
Here’s how explicit broadcasting can work in Julia