Skip to content

OmniTools.ForArray

Exported

OmniTools.ForArray.lower_off_diagonal_elements Function
julia
lower_off_diagonal_elements(A::AbstractMatrix)

returns a vector comprising of below diagonal elements of a matrix

Examples

julia
julia> using OmniTools

julia> lower_off_diagonal_elements([1 2; 3 4])
1-element Vector{Int64}:
 3
Code
julia
function lower_off_diagonal_elements(A::AbstractMatrix)
    collect(@view A[[ι for ι  CartesianIndices(A) if ι[1] > ι[2]]])
end
OmniTools.ForArray.lower_triangle_mask Function
julia
lower_triangle_mask(A::AbstractMatrix)

returns a matrix of same shape as input with 1 for all below diagonal elements and 0 elsewhere

Examples

julia
julia> using OmniTools

julia> lower_triangle_mask([1 2; 3 4])
2×2 Matrix{Float64}:
 0.0  0.0
 1.0  0.0
Code
julia
function lower_triangle_mask(A::AbstractMatrix)
    o_mat = zeros(size(A))
    for ι  CartesianIndices(A)
        if ι[1] > ι[2]
            o_mat[ι] = 1
        end
    end
    return o_mat
end
OmniTools.ForArray.off_diagonal_elements Function
julia
off_diagonal_elements(A::AbstractMatrix)

returns a vector comprising of off diagonal elements of a matrix

Examples

julia
julia> using OmniTools

julia> off_diagonal_elements([1 2; 3 4])
2-element Vector{Int64}:
 3
 2
Code
julia
function off_diagonal_elements(A::AbstractMatrix)
    collect(@view A[[ι for ι  CartesianIndices(A) if ι[1]  ι[2]]])
end
OmniTools.ForArray.off_diagonal_mask Function
julia
off_diagonal_mask(A::AbstractMatrix)

returns a matrix of same shape as input with 1 for all non diagonal elements

Examples

julia
julia> using OmniTools

julia> off_diagonal_mask([1 2; 3 4])
2×2 Matrix{Float64}:
 0.0  1.0
 1.0  0.0
Code
julia
function off_diagonal_mask(A::AbstractMatrix)
    o_mat = zeros(size(A))
    for ι  CartesianIndices(A)
        if ι[1]  ι[2]
            o_mat[ι] = 1
        end
    end
    return o_mat
end
OmniTools.ForArray.positive_mask Function
julia
positive_mask(_array)

Converts an array into a boolean array where elements greater than zero are true.

Arguments:

  • _array: The input array to be converted.

Returns:

A boolean array with the same dimensions as _array.

Examples

julia
julia> using OmniTools

julia> positive_mask([1.0, 0.0, -1.0])
3-element BitVector:
 1
 0
 0
Code
julia
function positive_mask(arr)
    fill_value = 0.0
    arr = map(x -> replace_invalid_number(x, fill_value), arr)
    arr_bits = arr .> fill_value
    return arr_bits
end
OmniTools.ForArray.stack_as_columns Function
julia
stack_as_columns(arr)

Stacks a collection of arrays along the first dimension.

Arguments:

  • arr: A collection of arrays to be stacked. All arrays must have the same size along their non-stacked dimensions.

Returns:

  • A single array where the input arrays are stacked along the first dimension.

  • If the arrays are 1D, the result is a vector.

Notes:

  • The function uses hcat to horizontally concatenate the arrays and then creates a view to stack them along the first dimension.

  • If the first dimension of the input arrays has a size of 1, the result is flattened into a vector.

  • This function is efficient and avoids unnecessary data copying.

Examples

julia
julia> using OmniTools

julia> stack_as_columns(([1, 2], [3, 4]))
2×2 Matrix{Int64}:
 1  3
 2  4
Code
julia
function stack_as_columns(arr)
    mat = reduce(hcat, arr)
    return length(arr[1]) == 1 ? vec(mat) : mat
end
OmniTools.ForArray.upper_off_diagonal_elements Function
julia
upper_off_diagonal_elements(A::AbstractMatrix)

returns a vector comprising of above diagonal elements of a matrix

Examples

julia
julia> using OmniTools

julia> upper_off_diagonal_elements([1 2; 3 4])
1-element Vector{Int64}:
 2
Code
julia
function upper_off_diagonal_elements(A::AbstractMatrix)
    collect(@view A[[ι for ι  CartesianIndices(A) if ι[1] < ι[2]]])
end
OmniTools.ForArray.upper_triangle_mask Function
julia
upper_triangle_mask(A::AbstractMatrix)

returns a matrix of same shape as input with 1 for all above diagonal elements and 0 elsewhere

Examples

julia
julia> using OmniTools

julia> upper_triangle_mask([1 2; 3 4])
2×2 Matrix{Float64}:
 0.0  1.0
 0.0  0.0
Code
julia
function upper_triangle_mask(A::AbstractMatrix)
    o_mat = zeros(size(A))
    for ι  CartesianIndices(A)
        if ι[1] < ι[2]
            o_mat[ι] = 1
        end
    end
    return o_mat
end
OmniTools.ForArray.view_at_trailing_indices Function
julia
view_at_trailing_indices(data::AbstractArray{<:Any, N}, idxs::Tuple{Vararg{Int}}) where N

Creates a view of the input array _dat based on the provided indices tuple inds.

Arguments:

  • _dat: The input array from which a view is created. Can be of any dimensionality.

  • inds: A tuple of integer indices specifying the spatial or temporal dimensions to slice.

Returns:

  • A SubArray view of _dat corresponding to the specified indices.

Notes:

  • The function supports arrays of arbitrary dimensions (N).

  • For arrays with fewer dimensions than the size of inds, an error is thrown.

  • For higher-dimensional arrays, the indices are applied to the last dimensions, while earlier dimensions are accessed using Colon() (i.e., all elements are included).

  • This function avoids copying data by creating a view, which is efficient for large arrays.

Error Handling:

  • Throws an error if the dimensionality of _dat is less than the size of inds.

Examples

julia
julia> using OmniTools

julia> A = Matrix(reshape(1:9, 3, 3))
3×3 Matrix{Int64}:
 1  4  7
 2  5  8
 3  6  9

julia> view_at_trailing_indices(A, (2, 3))[]
8
Code
julia
function view_at_trailing_indices end

function view_at_trailing_indices(data::AbstractArray{<:Any,N}, idxs::Tuple{Int}) where N
    if N == 1
        view(data, first(idxs))
    else
        dim = 1 
        d_size = size(data)
        view_inds = map(d_size) do _
            vi = dim == length(d_size) ? first(idxs) : Colon()
            dim += 1 
            vi
        end
        view(data, view_inds...)
    end
end

Internal

OmniTools.jl - Foundational utilities for Julia development