|
| 1 | +# Subpackages |
| 2 | + |
| 3 | +RecursiveArrayTools.jl ships several optional subpackages under `lib/`. Each is a |
| 4 | +separate Julia package that adds functionality which was split out of the main |
| 5 | +package to avoid method invalidations that would increase load times for users who |
| 6 | +don't need that functionality. |
| 7 | + |
| 8 | +## RecursiveArrayToolsRaggedArrays |
| 9 | + |
| 10 | +True ragged (non-rectangular) array types that preserve exact structure without |
| 11 | +zero-padding. See the [Ragged Arrays](@ref) page for full documentation. |
| 12 | + |
| 13 | +```julia |
| 14 | +using RecursiveArrayToolsRaggedArrays |
| 15 | +``` |
| 16 | + |
| 17 | +## RecursiveArrayToolsShorthandConstructors |
| 18 | + |
| 19 | +Shorthand `VA[...]` and `AP[...]` constructor syntax for `VectorOfArray` and |
| 20 | +`ArrayPartition`. |
| 21 | + |
| 22 | +This is separated from the main package because the `getindex(::Type, ...)` method |
| 23 | +definitions invalidate compiled specializations of `Base.getindex(::Type{T}, vals...)` |
| 24 | +from Base, increasing load times for downstream packages that don't need the syntax. |
| 25 | + |
| 26 | +```julia |
| 27 | +using RecursiveArrayToolsShorthandConstructors |
| 28 | +``` |
| 29 | + |
| 30 | +### Usage |
| 31 | + |
| 32 | +```julia |
| 33 | +using RecursiveArrayTools |
| 34 | +using RecursiveArrayToolsShorthandConstructors |
| 35 | + |
| 36 | +# VectorOfArray shorthand (equivalent to VectorOfArray([[1,2,3], [4,5,6]])) |
| 37 | +u = VA[[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
| 38 | + |
| 39 | +# ArrayPartition shorthand (equivalent to ArrayPartition(x0, v0, a0)) |
| 40 | +x0, v0, a0 = rand(3, 3), rand(3, 3), rand(3, 3) |
| 41 | +u0 = AP[x0, v0, a0] |
| 42 | +u0.x[1] === x0 # true |
| 43 | + |
| 44 | +# Nesting works |
| 45 | +nested = VA[fill(1, 2, 3), VA[3ones(3), zeros(3)]] |
| 46 | +``` |
| 47 | + |
| 48 | +Without this package, use the equivalent explicit constructors: |
| 49 | + |
| 50 | +```julia |
| 51 | +u = VectorOfArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) |
| 52 | +u0 = ArrayPartition(x0, v0, a0) |
| 53 | +``` |
| 54 | + |
| 55 | +## RecursiveArrayToolsArrayPartitionAnyAll |
| 56 | + |
| 57 | +Optimized `any` and `all` for `ArrayPartition` that iterate partition-by-partition |
| 58 | +instead of element-by-element, giving ~1.5-1.8x speedup on full scans. |
| 59 | + |
| 60 | +This is separated from the main package because `any(f::Function, ::ArrayPartition)` |
| 61 | +invalidates ~780 compiled specializations of `any(f::Function, ::AbstractArray)`. |
| 62 | + |
| 63 | +```julia |
| 64 | +using RecursiveArrayToolsArrayPartitionAnyAll |
| 65 | +``` |
| 66 | + |
| 67 | +### Usage |
| 68 | + |
| 69 | +```julia |
| 70 | +using RecursiveArrayTools |
| 71 | +using RecursiveArrayToolsArrayPartitionAnyAll |
| 72 | + |
| 73 | +ap = ArrayPartition(rand(1000), rand(1000), rand(1000)) |
| 74 | + |
| 75 | +# These now use the optimized partition-by-partition iteration |
| 76 | +any(isnan, ap) # ~1.5x faster than default |
| 77 | +all(x -> x > 0, ap) # ~1.8x faster than default |
| 78 | +``` |
| 79 | + |
| 80 | +Without this package, `any`/`all` use the default `AbstractArray` implementation |
| 81 | +which is correct but slower due to per-element partition indexing overhead. |
| 82 | + |
| 83 | +### Why is it faster? |
| 84 | + |
| 85 | +`ArrayPartition` stores data as a tuple of arrays. The default `AbstractArray` |
| 86 | +`any`/`all` iterates element-by-element, which requires computing which partition |
| 87 | +each linear index falls into. The optimized methods iterate over each partition |
| 88 | +array directly, avoiding that lookup overhead entirely. |
0 commit comments