Skip to content

Commit 9374445

Browse files
authored
Merge pull request #129 from mcabbott/rf/broadcast-ambiguities
Fix #128 broadcast ambiguities
2 parents cf650e2 + 46db5c9 commit 9374445

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AxisKeys"
22
uuid = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
33
license = "MIT"
4-
version = "0.2.8"
4+
version = "0.2.9"
55

66
[deps]
77
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/broadcast.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,28 @@ Base.BroadcastStyle(::Type{<:KeyedArray{T,N,AT}}) where {T,N,AT} =
2121
Base.BroadcastStyle(::KeyedStyle{A}, ::KeyedStyle{B}) where {A, B} = KeyedStyle(A(), B())
2222
Base.BroadcastStyle(::KeyedStyle{A}, b::B) where {A, B} = KeyedStyle(A(), b)
2323
Base.BroadcastStyle(a::A, ::KeyedStyle{B}) where {A, B} = KeyedStyle(a, B())
24-
Base.BroadcastStyle(::KeyedStyle{A}, b::DefaultArrayStyle) where {A} = KeyedStyle(A(), b)
25-
Base.BroadcastStyle(a::AbstractArrayStyle{M}, ::KeyedStyle{B}) where {B,M} = KeyedStyle(a, B())
2624

2725
using NamedDims: NamedDimsStyle
2826
# this resolves in favour of KeyedArray(NamedDimsArray())
2927
Base.BroadcastStyle(a::NamedDimsStyle, ::KeyedStyle{B}) where {B} = KeyedStyle(a, B())
3028
Base.BroadcastStyle(::KeyedStyle{A}, b::NamedDimsStyle) where {A} = KeyedStyle(A(), b)
3129

30+
# Resolve ambiguities
31+
# for all these cases, we define that we win to be the outer style regardless of order
32+
for B in (
33+
:BroadcastStyle, :DefaultArrayStyle, :AbstractArrayStyle, :(Broadcast.Style{Tuple}),
34+
)
35+
@eval function Base.BroadcastStyle(::KeyedStyle{A}, b::$B) where A
36+
return KeyedStyle(A(), b)
37+
end
38+
@eval function Base.BroadcastStyle(b::$B, ::KeyedStyle{A}) where A
39+
return KeyedStyle(b, A())
40+
end
41+
end
42+
3243
function unwrap_broadcasted(bc::Broadcasted{KeyedStyle{S}}) where {S}
3344
inner_args = map(unwrap_broadcasted, bc.args)
34-
Broadcasted{S}(bc.f, inner_args)
45+
Broadcasted{S}(bc.f, inner_args, axes(bc))
3546
end
3647
unwrap_broadcasted(x) = x
3748
unwrap_broadcasted(x::KeyedArray) = parent(x)

test/_basic.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ end
259259
@test_throws Exception unify_keys((1:2,), ([3,4],))
260260

261261
end
262+
@testset "https://github.com/mcabbott/AxisKeys.jl/issues/128" begin
263+
using LinearAlgebra
264+
265+
# Really simple test that broadcasting with linalg array types works.
266+
v = rand(10)
267+
σ = wrapdims(v; time=-4:5)
268+
L = LowerTriangular(reshape(1.0:1.0:100, (10, 10)))
269+
σ .* L
270+
v .* L
271+
@test σ .* L v .* L
272+
end
262273
end
263274
@testset "bitarray" begin
264275

0 commit comments

Comments
 (0)