IndexFunArrays.jl
Here you can find the docstrings of all functions. We also provide several concrete generators.
IndexFunArray Interface
The abstract IndexFunArray
definition
IndexFunArrays.IndexFunArray
— TypeIndexFunArray([T], gen::F, size::NTuple{N,Int}) where {N,F}
Generate a IndexFunArray object which behaves like an array but does not allocate the full array. Instead it calculates the elements when needed. This is useful to prevent array allocations. gen
is a function which takes the array indices wrapped as tuple as input. The output of gen
determines the element type of the resulting array. size
is the output size of the resulting array. T
can be the optional element type of the arrays. gen
needs to have T
as return type, otherwise the IndexFunArray might be type unstable.
Examples
julia> IndexFunArray(x -> sum(x), (3, 3))
3×3 IndexFunArray{Int64, 2, var"#182#183"}:
2 3 4
3 4 5
4 5 6
julia> IndexFunArray(x -> sum(abs2.(x)), (3, 3))
3×3 IndexFunArray{Int64, 2, var"#184#185"}:
2 5 10
5 8 13
10 13 18
julia> IndexFunArray(x -> (x[1], x[2], "Julia"), (3,3))
3×3 IndexFunArray{Tuple{Int64, Int64, String}, 2, var"#18#19"}:
(1, 1, "Julia") (1, 2, "Julia") (1, 3, "Julia")
(2, 1, "Julia") (2, 2, "Julia") (2, 3, "Julia")
(3, 1, "Julia") (3, 2, "Julia") (3, 3, "Julia")
Indices with certain type
IndexFunArrays.idx
— Functionidx([T=Float64], size::NTuple{N, Int};
offset=CtrFT,
dims=ntuple(+, N),
scale=ScaUnit)
See rr2
for a description of all options.
Returns basically the CartesianIndices
but as a tuple and accounting for optional scale
, offset
and data type. Note that T
is enforced element-wise for the return tuple elements.
julia> idx(Int, (3,3), offset=CtrCorner)
3×3 IndexFunArray{Tuple{Int64, Int64}, 2, IndexFunArrays.var"#39#40"{Int64, Tuple{Float64, Float64}, Tuple{Int64, Int64}}}:
(0, 0) (0, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (2, 1) (2, 2)
julia> idx(Int, (3,3), offset=(0,0))
3×3 IndexFunArray{Tuple{Int64, Int64}, 2, IndexFunArrays.var"#39#40"{Int64, Tuple{Int64, Int64}, Tuple{Int64, Int64}}}:
(1, 1) (1, 2) (1, 3)
(2, 1) (2, 2) (2, 3)
(3, 1) (3, 2) (3, 3)
julia> idx(Float32, (3,3), offset=(0,0))
3×3 IndexFunArray{Tuple{Float32, Float32}, 2, IndexFunArrays.var"#39#40"{Float32, Tuple{Int64, Int64}, Tuple{Int64, Int64}}}:
(1.0, 1.0) (1.0, 2.0) (1.0, 3.0)
(2.0, 1.0) (2.0, 2.0) (2.0, 3.0)
(3.0, 1.0) (3.0, 2.0) (3.0, 3.0)
IndexFunArrays.cpx
— Functioncpx([T=Float64], size::NTuple{N, Int};
offset=CtrFT,
dims=ntuple(+, N),
scale=ScaUnit)
See rr2
for a description of all options.
Returns an IndexFunArray where each positon corresponds to a complex value according to its position. The parameters offset
and scale
can be used accordingly (see rr2
). Note that T
is enforced element-wise for the return tuple elements.
julia> cpx(Int, (3,3), offset=CtrCorner)
3×3 Matrix{Complex{Int64}}:
0+0im 0+1im 0+2im
1+0im 1+1im 1+2im
2+0im 2+1im 2+2im
julia> cpx(Int, (3,3), offset=(0,0))
3×3 Matrix{Complex{Int64}}:
1+1im 1+2im 1+3im
2+1im 2+2im 2+3im
3+1im 3+2im 3+3im
julia> cpx((3,3), offset=(0,0))
3×3 Matrix{ComplexF64}:
1.0+1.0im 1.0+2.0im 1.0+3.0im
2.0+1.0im 2.0+2.0im 2.0+3.0im
3.0+1.0im 3.0+2.0im 3.0+3.0im
Helpful Array Functions
In addition to normal size
one can imagine a selectsizes
which returns the sizes of several dimensions simultaneously.
IndexFunArrays.selectsizes
— Functionselectsizes(x::AbstractArray, dim; keep_dims=true)
Additional size method to access the size at several dimensions in one call. keep_dims
allows to return the other dimensions as singletons.
Examples
julia> x = ones((2,4,6,8, 10));
julia> selectsizes(x, (2,3))
(1, 4, 6, 1, 1)
julia> selectsizes(x, 5)
(1, 1, 1, 1, 10)
julia> selectsizes(x, (5,))
(1, 1, 1, 1, 10)
julia> selectsizes(x, (2,3,4), keep_dims=false)
(4, 6, 8)
julia> selectsizes(x, (4,3,2), keep_dims=false)
(8, 6, 4)
IndexFunArrays.single_dim_size
— Functionsingle_dim_size(dim::Int,dim_size::Int)
Returns a tuple (length dim
) of singleton sizes except at the final position dim
, which contains dim_size
Example
julia> IndexFunArrays.single_dim_size(4, 3)
(1, 1, 1, 3)
julia> IndexFunArrays.single_dim_size(4, 5)
(1, 1, 1, 5)
julia> IndexFunArrays.single_dim_size(2, 5)
(1, 5)