Skip to content

Commit 7bdbcfd

Browse files
committed
show methods updates
1 parent 84bf6f9 commit 7bdbcfd

File tree

3 files changed

+61
-63
lines changed

3 files changed

+61
-63
lines changed

NEWS.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# ComponentArrays.jl NEWS
2+
3+
## What's new with ComponentArrays.jl?
4+
Notes on new features (minor releases). For more details on bugfixes and non-feature-adding changes (patch releases), check out the [releases page](https://github.com/jonniedie/ComponentArrays.jl/releases).
5+
6+
### v0.7.0
7+
- Much faster (and lazier) arrays of subcomponents!
8+
```julia-repl
9+
julia> ca = ComponentArray(a=5, b=(a=zeros(4,4), b=0), c=(a=[(a=1, b=2), (a=3, b=1), (a=1, b=2), (a=3, b=1)], b=[1., 2., 4]));
10+
11+
julia> @btime sum(x.a + x.b for x in $ca.c.a);
12+
127.160 ns (2 allocations: 480 bytes)
13+
14+
julia> @btime sum(x.a + x.b for x in $ca.c.a);
15+
36.895 ns (0 allocations: 0 bytes)
16+
```
17+
18+
### v0.6.0
19+
- Easier DifferentialEquations plotting!
20+
- Automatic legend labeling!
21+
- `Symbol` and `String` support for the `vars` plot keyword!
22+
- See it in an action [here](https://github.com/jonniedie/ComponentArrays.jl/blob/master/docs/src/examples/adaptive_control.md)!
23+
24+
### v0.5.0
25+
- Constructor for making new `ComponentVector`s with additional fields! Watch out, it's slow!
26+
```julia
27+
julia> x = ComponentArray(a=5, b=[1, 2])
28+
ComponentVector{Int64}(a = 5, b = [1, 2])
29+
30+
julia> moar_x = ComponentArray(x; c=zeros(2,2), d=(a=2, b=10))
31+
ComponentVector{Int64}(a = 5, b = [1, 2], c = [0 0; 0 0], d = (a = 2, b = 10))
32+
```
33+
34+
### v0.4.0
35+
- Zygote rules for DiffEqFlux support! Check out [the docs](https://jonniedie.github.io/ComponentArrays.jl/dev/examples/DiffEqFlux/) for an example!
36+
37+
### v0.3.0
38+
- Matrix and higher-dimensional array components!
39+
40+
...and plenty more!

README.md

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,7 @@ in [DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl)
2727
flat vectors is fair game.
2828

2929
## New Features!
30-
### v0.7.0
31-
- Much faster (and lazier) arrays of subcomponents
32-
```julia-repl
33-
julia> ca = ComponentArray(a=5, b=(a=zeros(4,4), b=0), c=(a=[(a=1, b=2), (a=3, b=1), (a=1, b=2), (a=3, b=1)], b=[1., 2., 4]));
34-
35-
julia> @btime sum(x.a + x.b for x in $ca.c.a);
36-
127.160 ns (2 allocations: 480 bytes)
37-
38-
julia> @btime sum(x.a + x.b for x in $ca.c.a);
39-
36.895 ns (0 allocations: 0 bytes)
40-
```
41-
42-
### v0.6.0
43-
- Easier DifferentialEquations plotting!
44-
- Automatic legend labeling!
45-
- `Symbol` and `String` support for the `vars` plot keyword!
46-
- See it in an action [here](https://github.com/jonniedie/ComponentArrays.jl/blob/master/docs/src/examples/adaptive_control.md)!
47-
48-
### v0.5.0
49-
- Constructor for making new `ComponentVector`s with additional fields! Watch out, it's slow!
50-
```julia
51-
julia> x = ComponentArray(a=5, b=[1, 2])
52-
ComponentVector{Int64}(a = 5, b = [1, 2])
53-
54-
julia> moar_x = ComponentArray(x; c=zeros(2,2), d=(a=2, b=10))
55-
ComponentVector{Int64}(a = 5, b = [1, 2], c = [0 0; 0 0], d = (a = 2, b = 10))
56-
```
57-
58-
### v0.4.0
59-
- Zygote rules for DiffEqFlux support! Check out [the docs](https://jonniedie.github.io/ComponentArrays.jl/dev/examples/DiffEqFlux/) for an example!
60-
61-
### v0.3.0
62-
- Matrix and higher-dimensional array components!
63-
64-
...and plenty more!
30+
See the [NEWS page](https://github.com/jonniedie/ComponentArrays.jl/blob/master/NEWS.md) for new features!
6531

6632
## General use
6733
The easiest way to construct 1-dimensional ```ComponentArray```s (aliased as `ComponentVector`) is as if they were ```NamedTuple```s. In fact, a good way to think about them is as arbitrarily nested, mutable ```NamedTuple```s that can be passed through a solver.
@@ -96,11 +62,11 @@ true
9662

9763
Higher dimensional ```ComponentArray```s can be created too, but it's a little messy at the moment. The nice thing for modeling is that dimension expansion through broadcasted operations can create higher-dimensional ```ComponentArray```s automatically, so Jacobian cache arrays that are created internally with ```false .* x .* x'``` will be two-dimensional ```ComponentArray```s (aliased as `ComponentMatrix`) with proper axes. Check out the [ODE with Jacobian](https://github.com/jonniedie/ComponentArrays.jl/blob/master/examples/ODE_jac_example.jl) example in the examples folder to see how this looks in practice.
9864
```julia
99-
julia> x = ComponentArray(a=1, b=[2, 1, 4], c=c)
65+
julia> x = ComponentArray(a=1, b=[2, 1, 4.0], c=c)
10066
ComponentVector{Float64}(a = 1.0, b = [2.0, 1.0, 4.0], c = (a = 2.0, b = [1.0, 2.0]))
10167

10268
julia> x2 = x .* x'
103-
7×7 ComponentArray{Tuple{Axis{(a = 1, b = 2:4, c = (5:7, (a = 1, b = 2:3)))},Axis{(a = 1, b = 2:4, c = (5:7, (a = 1, b = 2:3)))}},Float64,2,Array{Float64,2}}:
69+
7×7 ComponentMatrix{Float64} with axes Axis(a = 1, b = 2:4, c = ViewAxis(5:7, Axis(a = 1, b = 2:3))) × Axis(a = 1, b = 2:4, c = ViewAxis(5:7, Axis(a = 1, b = 2:3)))
10470
1.0 2.0 1.0 4.0 2.0 1.0 2.0
10571
2.0 4.0 2.0 8.0 4.0 2.0 4.0
10672
1.0 2.0 1.0 4.0 2.0 1.0 2.0
@@ -110,7 +76,7 @@ julia> x2 = x .* x'
11076
2.0 4.0 2.0 8.0 4.0 2.0 4.0
11177

11278
julia> x2[:c,:c]
113-
3×3 ComponentArray{Tuple{Axis{(a = 1, b = 2:3)},Axis{(a = 1, b = 2:3)}},Float64,2,SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}:
79+
3×3 ComponentMatrix{Float64,SubArray...} with axes Axis(a = 1, b = 2:3) × Axis(a = 1, b = 2:3)
11480
4.0 2.0 4.0
11581
2.0 1.0 2.0
11682
4.0 2.0 4.0
@@ -119,10 +85,10 @@ julia> x2[:a,:a]
11985
1.0
12086

12187
julia> x2[:a,:c]
122-
ComponentVector{Float64}(a = 2.0, b = [1.0, 2.0])
88+
ComponentVector{Float64,SubArray...}(a = 2.0, b = [1.0, 2.0])
12389

12490
julia> x2[:b,:c]
125-
3×3 ComponentArray{Tuple{Axis{NamedTuple()},Axis{(a = 1, b = 2:3)}},Float64,2,SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}:
91+
3×3 ComponentMatrix{Float64,SubArray...} with axes FlatAxis() × Axis(a = 1, b = 2:3)
12692
4.0 2.0 4.0
12793
2.0 1.0 2.0
12894
8.0 4.0 8.0

src/show.jl

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Show AbstractAxis types
22
Base.show(io::IO, ::MIME"text/plain", ::Axis{IdxMap}) where IdxMap = print(io, "Axis$IdxMap")
3-
Base.show(io::IO, ::Axis{IdxMap}) where IdxMap = print(io, "Axis($IdxMap)")
3+
Base.show(io::IO, ::Axis{IdxMap}) where IdxMap = print(io, "Axis$IdxMap")
44

55
Base.show(io::IO, ::FlatAxis) = print(io, "FlatAxis()")
66

@@ -26,9 +26,13 @@ Base.show(io::IO, ci::ComponentIndex) = print(io, "ComponentIndex($(ci.idx), $(c
2626

2727
# Show ComponentArrays
2828
Base.show(io::IO, ::MIME"text/plain", ::Type{ComponentArray{T,N,A,Ax}}) where {T,N,A,Ax} = print(io, "ComponentArray{$T,$N,$A,$Ax}") # make `typeof(u)` show the full type
29-
Base.show(io::IO, ::Type{<:ComponentArray{T, N}}) where {T, N} = print(io, "ComponentArray{$T, $N}") # do not pollute the stacktrace with verbose type printing
30-
Base.show(io::IO, ::Type{<:ComponentArray{T, 1}}) where T = print(io, "ComponentVector{$T}")
31-
Base.show(io::IO, ::Type{<:ComponentArray{T, 2}}) where T = print(io, "ComponentMatrix{$T}")
29+
Base.show(io::IO, ::Type{<:ComponentArray{T,N,<:Array}}) where {T,N} = print(io, "ComponentArray{$T,$N}") # do not pollute the stacktrace with verbose type printing
30+
Base.show(io::IO, ::Type{<:ComponentArray{T,1,<:Array}}) where T = print(io, "ComponentVector{$T}")
31+
Base.show(io::IO, ::Type{<:ComponentArray{T,2,<:Array}}) where T = print(io, "ComponentMatrix{$T}")
32+
Base.show(io::IO, ::Type{<:ComponentArray{T,N,A}}) where {T,N,A} = print(io, "ComponentArray{$T,$N,$(A.name)...}")
33+
Base.show(io::IO, ::Type{<:ComponentArray{T,1,A}}) where {T,A} = print(io, "ComponentVector{$T,$(A.name)...}")
34+
Base.show(io::IO, ::Type{<:ComponentArray{T,2,A}}) where {T,A} = print(io, "ComponentMatrix{$T,$(A.name)...}")
35+
3236
function Base.show(io::IO, x::ComponentVector)
3337
print(io, "(")
3438
for (i,key) in enumerate(keys(x))
@@ -42,16 +46,13 @@ function Base.show(io::IO, x::ComponentVector)
4246
print(io, ")")
4347
return nothing
4448
end
45-
function Base.show(io::IO, ::MIME"text/plain", x::ComponentVector{T,A,Axes}) where {A<:Vector{T},Axes} where T
46-
print(io, "ComponentVector{" , T, "}")
47-
show(io, x)
48-
return nothing
49-
end
50-
function Base.show(io::IO, ::MIME"text/plain", x::ComponentVector{T,A,Axes}) where {T,A,Axes}
51-
print(io, "ComponentVector{" , A, "}")
49+
50+
function Base.show(io::IO, ::MIME"text/plain", x::ComponentVector)
51+
show(io, typeof(x))
5252
show(io, x)
5353
return nothing
5454
end
55+
5556
function Base.show(io::IO, a::AbstractVector{<:T}) where T<:ComponentVector
5657
elem = a[1]
5758
print(io, "[$elem")
@@ -62,22 +63,13 @@ function Base.show(io::IO, a::AbstractVector{<:T}) where T<:ComponentVector
6263
return nothing
6364
end
6465

65-
66-
function Base.show(io::IO, ::MIME"text/plain", x::ComponentMatrix{T,A,Axes}) where {A<:Matrix{T},Axes} where T
67-
if !haskey(io, :compact) && length(axes(x, 2)) > 1
68-
io = IOContext(io, :compact => true)
69-
end
70-
axs = indexmap.(getaxes(x))
71-
println(io, "ComponentMatrix{" , T, "} with axes $(axs[1]) × $(axs[2])")
72-
Base.print_matrix(io, getdata(x))
73-
return nothing
74-
end
7566
function Base.show(io::IO, ::MIME"text/plain", x::ComponentMatrix{T,A,Axes}) where {T,A,Axes}
7667
if !haskey(io, :compact) && length(axes(x, 2)) > 1
7768
io = IOContext(io, :compact => true)
7869
end
79-
axs = indexmap.(getaxes(x))
80-
println(io, "ComponentMatrix{" , A, "} with axes $(axs[1]) × $(axs[2])")
70+
axs = getaxes(x)
71+
sz = size(x)
72+
println(io, "$(sz[1])×$(sz[2]) $(typeof(x)) with axes $(axs[1]) × $(axs[2])")
8173
Base.print_matrix(io, getdata(x))
8274
return nothing
8375
end

0 commit comments

Comments
 (0)