Skip to content

Commit fb21c74

Browse files
committed
map change and constructor from Dict. From #68
1 parent 178d0b4 commit fb21c74

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ComponentArrays"
22
uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
33
authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"]
4-
version = "0.8.22"
4+
version = "0.9.0"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/broadcasting.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ end
7171

7272
Base.Broadcast.broadcasted(f, x::ComponentArray) = ComponentArray(map(f, getdata(x)), getaxes(x))
7373

74+
# Need a special case here because `map` doesn't follow same rules as normal broadcasting. To be safe and avoid ambiguities,
75+
# we'll just handle the case where everything is a ComponentArray. Else it falls back to a plain Array output.
76+
function Base.map(f, xs::ComponentArray{<:Any, <:Any, <:Any, Axes}...) where Axes
77+
return ComponentArray(map(f, getdata.(xs)...), getaxes(Axes))
78+
end
7479

7580
# function Base.copy(bc::BC.Broadcasted{<:CAStyle{InnerStyle, Axes, N}}) where {InnerStyle, Axes, N}
7681
# return ComponentArray{Axes}(Base.copy(BC.broadcasted(bc.f, map(getdata, bc.args)...)))

src/componentarray.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ function ComponentArray(data, ax::AbstractAxis...)
5757
return LazyArray(ComponentArray(x, axs) for x in part_data)
5858
end
5959

60-
# Entry from NamedTuple or kwargs
60+
# Entry from NamedTuple, Dict, or kwargs
6161
ComponentArray{T}(nt::NamedTuple) where T = ComponentArray(make_carray_args(T, nt)...)
6262
ComponentArray(nt::NamedTuple) = ComponentArray(make_carray_args(nt)...)
63+
ComponentArray(d::AbstractDict) = ComponentArray(NamedTuple(d))
6364
ComponentArray{T}(;kwargs...) where T = ComponentArray{T}((;kwargs...))
6465
ComponentArray(;kwargs...) = ComponentArray((;kwargs...))
6566

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ end
5656
@test typeof(ComponentArray{Float32}(undef, (ax,))) == typeof(ca_Float32)
5757
@test typeof(ComponentArray{MVector{10,Float64}}(undef, (ax,))) == typeof(ca_MVector)
5858

59+
# Entry from Dict
60+
dict1 = Dict(:a=>rand(5), :b=>rand(5,5))
61+
dict2 = Dict(:a=>3, :b=>dict1)
62+
@test ComponentArray(dict1) isa ComponentArray
63+
@test ComponentArray(dict2).b isa ComponentArray
64+
5965
@test ca == ComponentVector(a=100, b=[4, 1.3], c=(a=(a=1, b=[1.0, 4.4]), b=[0.4, 2, 1, 45]))
6066
@test cmat == ComponentMatrix(a .* a', ax, ax)
6167
@test_throws DimensionMismatch ComponentVector(sq_mat, ax)
@@ -277,6 +283,12 @@ end
277283
@test getaxes(x1mat + xmat) == (getaxes(x1)[1], FlatAxis())
278284
@test getaxes(x1mat + xmat') == (FlatAxis(), getaxes(x1)[1])
279285

286+
@test map(sqrt, ca) isa ComponentArray
287+
@test map(+, ca, sqrt.(ca)) isa ComponentArray
288+
@test map(+, sqrt.(ca), Float32.(ca), ca) isa ComponentArray
289+
@test map(+, ca, getdata(ca)) isa Array
290+
@test map(+, ca, ComponentArray(v=getdata(ca))) isa Array
291+
280292
x1 .+= x2
281293
@test getdata(x1) == 2getdata(x2)
282294

0 commit comments

Comments
 (0)