OmniTools.ForArray
Exported
OmniTools.ForArray.lower_off_diagonal_elements Function
lower_off_diagonal_elements(A::AbstractMatrix)returns a vector comprising of below diagonal elements of a matrix
Examples
julia> using OmniTools
julia> lower_off_diagonal_elements([1 2; 3 4])
1-element Vector{Int64}:
3Code
function lower_off_diagonal_elements(A::AbstractMatrix)
collect(@view A[[ι for ι ∈ CartesianIndices(A) if ι[1] > ι[2]]])
endOmniTools.ForArray.lower_triangle_mask Function
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> using OmniTools
julia> lower_triangle_mask([1 2; 3 4])
2×2 Matrix{Float64}:
0.0 0.0
1.0 0.0Code
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
endOmniTools.ForArray.off_diagonal_elements Function
off_diagonal_elements(A::AbstractMatrix)returns a vector comprising of off diagonal elements of a matrix
Examples
julia> using OmniTools
julia> off_diagonal_elements([1 2; 3 4])
2-element Vector{Int64}:
3
2Code
function off_diagonal_elements(A::AbstractMatrix)
collect(@view A[[ι for ι ∈ CartesianIndices(A) if ι[1] ≠ ι[2]]])
endOmniTools.ForArray.off_diagonal_mask Function
off_diagonal_mask(A::AbstractMatrix)returns a matrix of same shape as input with 1 for all non diagonal elements
Examples
julia> using OmniTools
julia> off_diagonal_mask([1 2; 3 4])
2×2 Matrix{Float64}:
0.0 1.0
1.0 0.0Code
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
endOmniTools.ForArray.positive_mask Function
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> using OmniTools
julia> positive_mask([1.0, 0.0, -1.0])
3-element BitVector:
1
0
0Code
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
endOmniTools.ForArray.stack_as_columns Function
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
hcatto 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> using OmniTools
julia> stack_as_columns(([1, 2], [3, 4]))
2×2 Matrix{Int64}:
1 3
2 4Code
function stack_as_columns(arr)
mat = reduce(hcat, arr)
return length(arr[1]) == 1 ? vec(mat) : mat
endOmniTools.ForArray.upper_off_diagonal_elements Function
upper_off_diagonal_elements(A::AbstractMatrix)returns a vector comprising of above diagonal elements of a matrix
Examples
julia> using OmniTools
julia> upper_off_diagonal_elements([1 2; 3 4])
1-element Vector{Int64}:
2Code
function upper_off_diagonal_elements(A::AbstractMatrix)
collect(@view A[[ι for ι ∈ CartesianIndices(A) if ι[1] < ι[2]]])
endOmniTools.ForArray.upper_triangle_mask Function
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> using OmniTools
julia> upper_triangle_mask([1 2; 3 4])
2×2 Matrix{Float64}:
0.0 1.0
0.0 0.0Code
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
endOmniTools.ForArray.view_at_trailing_indices Function
view_at_trailing_indices(data::AbstractArray{<:Any, N}, idxs::Tuple{Vararg{Int}}) where NCreates 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
SubArrayview of_datcorresponding 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
_datis less than the size ofinds.
Examples
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))[]
8Code
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