JuliennedArrays.jl
JuliennedArrays.Align
JuliennedArrays.Align
JuliennedArrays.False
JuliennedArrays.Slices
JuliennedArrays.Slices
JuliennedArrays.True
JuliennedArrays.Align
— MethodAlign(slices, alongs::Int...)
Alternative syntax: alongs
is which dimensions will be taken up by the inner arrays.
julia> using JuliennedArrays
julia> input = reshape(1:8, 2, 2, 2)
2×2×2 reshape(::UnitRange{Int64}, 2, 2, 2) with eltype Int64:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> slices = Slices(input, 1, 3)
2-element Slices{SubArray{Int64, 2}, 1}:
[1 5; 2 6]
[3 7; 4 8]
julia> Align(slices, 1, 3)
2×2×2 Align{Int64, 3} with eltype Int64:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
You must include one along
for each inner dimension. alongs
should also be strictly increasing.
julia> Align(slices, 1)
ERROR: ArgumentError: (1,) is not of length inner dimensions (2).
[...]
Julia can infer the result if alongs
is constant.
julia> using Test: @inferred
julia> align_1_3(x) = Align(x, 1, 3);
julia> @inferred align_1_3(slices)
2×2×2 Align{Int64, 3} with eltype Int64:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
slice_axes
is the axes of one slice. If slices
is empty, you can specify slice_axes
manually.
julia> Align([rand(3) for _ in 1:0], 1)
ERROR: BoundsError: attempt to access 0-element Vector{Vector{Float64}} at index [1]
[...]
julia> Align([rand(3) for _ in 1:0], 1; slice_axes = axes(rand(3)))
3×0 Align{Float64, 2} with eltype Float64
JuliennedArrays.Align
— MethodAlign(slices, alongs::TypedBool...; slice_axes = axes(first(slices)))
Align
an array of arrays, all with the same size.
alongs
, made of True
and False
objects, shows which dimensions will be taken up by the inner arrays. Inverse of Slices
.
julia> using JuliennedArrays
julia> slices = [[1, 2], [3, 4]];
julia> aligned = Align(slices, False(), True())
2×2 Align{Int64, 2} with eltype Int64:
1 2
3 4
julia> aligned[1, :] == slices[1]
true
julia> aligned[1, 1] = 0;
julia> slices
2-element Vector{Vector{Int64}}:
[0, 2]
[3, 4]
Will throw an error if you try to align slices with different axes. Use @inbounds
to skip this check.
julia> unequal = [[1], [1, 2]];
julia> Align(unequal, False(), True())
ERROR: ArgumentError: Slice [1, 2] does not have slice_axes (Base.OneTo(1),)
[...]
slice_axes
is the axes of one slice. If slices
is empty, you can specify slice_axes
manually.
julia> Align([rand(3) for _ in 1:0], True(), False())
ERROR: BoundsError: attempt to access 0-element Vector{Vector{Float64}} at index [1]
[...]
julia> Align([rand(3) for _ in 1:0], True(), False(); slice_axes = axes(rand(3)))
3×0 Align{Float64, 2} with eltype Float64
JuliennedArrays.False
— Typestruct False
Typed false
JuliennedArrays.Slices
— MethodSlices(whole, alongs::TypedBool...)
Slice whole
into view
s.
alongs
, made of True
and False
objects, shows which dimensions will be replaced with :
when slicing.
julia> using JuliennedArrays
julia> whole = [1 2; 3 4];
julia> slices = Slices(whole, False(), True())
2-element Slices{SubArray{Int64, 1}, 1}:
[1, 2]
[3, 4]
julia> slices[1] == whole[1, :]
true
julia> slices[1] = [2, 1];
julia> whole
2×2 Matrix{Int64}:
2 1
3 4
julia> larger = rand(5, 5, 5);
julia> larger_slices = Slices(larger, True(), False(), False());
julia> size(first(larger_slices))
(5,)
You must include one along for each dimension of whole
.
julia> using Test: @test_throws
julia> @test_throws MethodError Slices(whole, True());
JuliennedArrays.Slices
— MethodSlices(whole, alongs::Int...)
Alternative syntax: alongs
is which dimensions will be replaced with :
when slicing.
julia> using JuliennedArrays
julia> input = reshape(1:8, 2, 2, 2)
2×2×2 reshape(::UnitRange{Int64}, 2, 2, 2) with eltype Int64:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> s = Slices(input, 1, 3)
2-element Slices{SubArray{Int64, 2}, 1}:
[1 5; 2 6]
[3 7; 4 8]
julia> map(sum, s)
2-element Vector{Int64}:
14
22
No along should be greater than the number of dimensions of whole
. alongs
should also be strictly increasing.
julia> Slices(input, 4)
ERROR: ArgumentError: 4, a dimension number, is out of bounds or out of order.
[...]
You can infer the result if the dimensions are constant.
julia> using Test: @inferred
julia> slices_1_3(x) = Slices(x, 1, 3);
julia> @inferred slices_1_3(input)
2-element Slices{SubArray{Int64, 2}, 1}:
[1 5; 2 6]
[3 7; 4 8]
JuliennedArrays.True
— Typestruct True
Typed true