Skip to content

Commit a207ed7

Browse files
author
rokke
committed
adds convenient vector-like syntax
1 parent 428b1be commit a207ed7

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ vA = VectorOfArray(a)
3232
vB = VectorOfArray(b)
3333

3434
vA .* vB # Now all standard array stuff works!
35+
36+
# you can also create it directly with a vector-like syntax:
37+
c = VA[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
38+
d = VA[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
39+
40+
c .* d
3541
```
3642

3743
### ArrayPartition
@@ -44,15 +50,15 @@ pB = ArrayPartition(b)
4450

4551
pA .* pB # Now all standard array stuff works!
4652

47-
# or do:
53+
# or using the vector syntax:
4854
x0 = rand(3, 3)
4955
v0 = rand(3, 3)
5056
a0 = rand(3, 3)
51-
u0 = ArrayPartition(x0, v0, a0)
52-
u0.x[1] == x0 # true
57+
u0 = AP[x0, v0, a0]
58+
u0.x[1] === x0 # true
5359

5460
u0 .+= 1
55-
u0.x[2] == v0 # still true
61+
u0.x[2] === v0 # still true
5662

5763
# do some calculations creating a new partitioned array
5864
unew = u0 * 10

src/RecursiveArrayTools.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ module RecursiveArrayTools
133133
Base.convert(T::Type{<:GPUArraysCore.AnyGPUArray}, VA::AbstractVectorOfArray) = stack(VA.u)
134134
(T::Type{<:GPUArraysCore.AnyGPUArray})(VA::AbstractVectorOfArray) = T(Array(VA))
135135

136-
export VectorOfArray, DiffEqArray, AbstractVectorOfArray, AbstractDiffEqArray,
136+
export VectorOfArray, VA, DiffEqArray, AbstractVectorOfArray, AbstractDiffEqArray,
137137
AllObserved, vecarr_to_vectors, tuples
138138

139139
export recursivecopy, recursivecopy!, recursivefill!, vecvecapply, copyat_or_push!,
140140
vecvec_to_mat, recursive_one, recursive_mean, recursive_bottom_eltype,
141141
recursive_unitless_bottom_eltype, recursive_unitless_eltype
142142

143-
export ArrayPartition, NamedArrayPartition
143+
export ArrayPartition, AP, NamedArrayPartition
144144

145145
include("precompilation.jl")
146146

src/array_partition.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,6 @@ end
722722
function Adapt.adapt_structure(to, ap::ArrayPartition)
723723
return ArrayPartition(map(x -> Adapt.adapt(to, x), ap.x)...)
724724
end
725+
726+
struct AP end
727+
Base.getindex(::Type{AP}, xs...) = ArrayPartition(xs...)

src/vector_of_array.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,3 +1655,6 @@ end
16551655
end
16561656
unpack_args_voa(i, args::Tuple{Any}) = (unpack_voa(args[1], i),)
16571657
unpack_args_voa(::Any, args::Tuple{}) = ()
1658+
1659+
struct VA end
1660+
Base.getindex(::Type{VA}, xs...) = VectorOfArray(collect(xs))

0 commit comments

Comments
 (0)