Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perrutquist/triangularindices.jl
https://github.com/perrutquist/triangularindices.jl
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/perrutquist/triangularindices.jl
- Owner: perrutquist
- License: mit
- Created: 2022-06-23T11:29:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-07-01T06:11:42.000Z (over 2 years ago)
- Last Synced: 2024-11-10T06:28:14.355Z (about 2 months ago)
- Language: Julia
- Size: 20.5 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build status](https://github.com/perrutquist/TriangularIndices.jl/workflows/CI/badge.svg)](https://github.com/perrutquist/TriangularIndices.jl/actions)
# TriangularIndices.jl
This small package is intended to help with iterating over the indices of triangular matrices. For example:
```julia
using TriangularIndicesz = zeros(Int, (3,3))
for (k, (i,j)) in enumerate(UpperTriangularIndices(z))
z[i,j] = k
end
```
will result in
```julia
z == [ 1 2 4
0 3 5
0 0 6 ]
```Of course, the above code could just as easily have been written
```julia
z = zeros(Int, (3,3))
k = 1
for j in 1:size(z,1)
for i in 1:j
z[i,j] = k
k += 1
end
end
```
which would not require the use of TriangularIndices.jl, and which might even be faster for single-threaded code. However, `UpperTriangularIndices` objects support `length` as well as partitioning using `getindex`, which makes them
useful with macros that distribute the iterator over threads or processes, such as `@distributed` from [Distributed](https://docs.julialang.org/en/v1/manual/distributed-computing/), `@threads` from [Threads](https://docs.julialang.org/en/v1/manual/multi-threading/), or `@floop` from [FLoops](https://github.com/JuliaFolds/FLoops.jl).For reverse indexing, there is a fast specialization of `findfirst`
```julia
findfirst(==((2,3)), UpperTriangularIndices(3)) # returns 5
```Note: There's currently no corresponding implementation of `LowerTriangularIndices`. If somebody is interested in writing one, the linear-to-cartesian indexing operation could use [this code](https://discourse.julialang.org/t/iterating-over-elements-of-upper-triangular-matrix-but-cartesian-indices-are-needed/65498/3).