Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix and test broadcast promotedims #918

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/Lookups/lookup_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -852,21 +852,23 @@ promote_first(x1, x2, xs...) =
# Fallback NoLookup if not identical type
promote_first(l1::Lookup) = l1
promote_first(l1::L, ls::L...) where L<:Lookup = rebuild(l1; metadata=NoMetadata)
function promote_first(l1::L, ls::Lookup...) where {L<:Lookup}
ls = _remove(Length1NoLookup, l1, ls...)
if length(ls) > 1
l1, ls... = ls
else
function promote_first(l1::Lookup, ls1::Lookup...)
ls = _remove(Length1NoLookup, l1, ls1...)
if length(ls) != length(ls1) + 1
# If anything was removed, start again
return promote_first(ls...)
elseif length(ls) == 1
# If there is only one left, use it
return first(ls)
end
if all(map(l -> typeof(l) == L, ls))
if length(ls) > 0
rebuild(l1; metadata=NoMetadata())
else
l1 # Keep metadata if there is only one lookup
end
# Otherwise see if these have the same type
l2, ls2... = ls
if all(map(l -> typeof(l) == typeof(l2), ls2))
# If so, just simplify the metadata
rebuild(l2; metadata=NoMetadata())
else
NoLookup(Base.OneTo(length(l1)))
# And if not, use NoLookup
NoLookup(Base.OneTo(length(l2)))
end
end
# Categorical lookups
Expand Down
5 changes: 3 additions & 2 deletions src/array/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ macro d(expr::Expr, options::Union{Expr,Nothing}=nothing)
dims = $DimensionalData.dims(found_dims, order_dims)
end
else
:(dims = _find_dims(vars))
quote
dims = _find_dims(vars)
end
end
quote
let
Expand Down Expand Up @@ -404,7 +406,6 @@ _broadcasted_dims(a) = ()
# its dimensions to match the rest of the @d broadcast, otherwise do nothing.
_maybe_dimensional_broadcast(x, _, _) = x
function _maybe_dimensional_broadcast(A::AbstractBasicDimArray, dest_dims, options)
len1s = basedims(otherdims(dest_dims, dims(A)))
# Reshape first to avoid a ReshapedArray wrapper if possible
A1 = _maybe_insert_length_one_dims(A, dest_dims)
# Then permute and reorder
Expand Down
15 changes: 15 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ end
p(da1, da2, permutedims(da3, (X, Y, Z)))
end

@testset "Lookups are maintained" begin
x = format(X(-5.0:5.0))
y = format(Y(-10.0:2:12.0))
z = format(Z(-3.0:0.5:4.0))

u = @d x .* y
v = @d x .* z
w = @d y .* z

f(u, v, w) = u + v + w

A = @d f.(u, v, w)
@test dims(A) == (x, y, z)
end

@testset "strict" begin
@test_nowarn @d rand(X(1:3)) .* rand(X([:a, :b, :c])) strict=false
@test_throws DimensionMismatch @d rand(X(1:3)) .* rand(X([:a, :b, :c])) strict=true
Expand Down
Loading