Skip to content

Commit 4c13809

Browse files
authored
Merge pull request #114 from SciML/myb/bc
Fix broadcast on range labels
2 parents fd78f2c + 91538d6 commit 4c13809

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/larray.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,18 @@ Base.BroadcastStyle(::Type{LArray{T,N,D,L}}) where {T,N,D,L} = LAStyle{T,N,L}()
118118
Base.BroadcastStyle(::LabelledArrays.LAStyle{T,N,L}, ::LabelledArrays.LAStyle{E,N,L}) where{T,E,N,L} =
119119
LAStyle{promote_type(T,E),N,L}()
120120

121+
@generated function labels2axes(::Val{t}) where t
122+
if t isa NamedTuple && all(x->x isa Union{Integer,UnitRange}, values(t)) # range labelling
123+
(Base.OneTo(maximum(Iterators.flatten(v for v in values(t)))), )
124+
elseif t isa NTuple{<:Any, Symbol}
125+
axes(t)
126+
else
127+
error("$t label isn't supported for broadcasting. Try to formulate it in terms of linear indexing.")
128+
end
129+
end
121130
function Base.similar(bc::Broadcast.Broadcasted{LAStyle{T,N,L}}, ::Type{ElType}) where {T,N,L,ElType}
122131
tmp = similar(Array{ElType},axes(bc))
123-
if axes(bc) != axes(Tuple(L))
132+
if axes(bc) != labels2axes(Val(L))
124133
return tmp
125134
else
126135
return LArray{ElType,N,typeof(tmp),L}(tmp)

test/larrays.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ end
168168
@test x .* x' isa Array
169169
end
170170

171+
@testset "broadcasting" begin
172+
n = 2
173+
lu_0 = @LArray fill(1000.0, 2 * n) (x = (1:n), y = (n+1:2*n))
174+
@test lu_0 ./ 1.0 isa LArray
175+
176+
x = @LArray fill(1000.0, 2 * n) (:x1, :x2, :y1, :y2)
177+
@test x ./ 1.0 isa LArray
178+
end
179+
171180
@testset "convert" begin
172181
A = @LArray [1,2,3] (:a,:b,:c)
173182
@test convert(AbstractVector{Int}, A) === A

0 commit comments

Comments
 (0)