https://github.com/harmoniqs/trajectoryindexingutils.jl
Utility functions for indexing and slicing trajectory data vectors
https://github.com/harmoniqs/trajectoryindexingutils.jl
julia trajectories trajectory-optimization utility-functions
Last synced: 3 months ago
JSON representation
Utility functions for indexing and slicing trajectory data vectors
- Host: GitHub
- URL: https://github.com/harmoniqs/trajectoryindexingutils.jl
- Owner: harmoniqs
- License: mit
- Created: 2023-04-06T12:17:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-11-03T21:03:34.000Z (7 months ago)
- Last Synced: 2026-01-14T06:09:53.971Z (5 months ago)
- Topics: julia, trajectories, trajectory-optimization, utility-functions
- Language: Julia
- Homepage:
- Size: 336 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TrajectoryIndexingUtils.jl
[](https://harmoniqs.github.io/TrajectoryIndexingUtils.jl/stable/)
[](https://harmoniqs.github.io/TrajectoryIndexingUtils.jl/dev/)
[](https://github.com/harmoniqs/TrajectoryIndexingUtils.jl/actions/workflows/CI.yml?query=branch%3Amain)
[](https://codecov.io/gh/harmoniqs/TrajectoryIndexingUtils.jl)
This is a super lightweight package that exports two functions: `index` and `slice`. These functions have helped to ease the burden of handling messy indexing into trajectory data vectors of the form
```math
\vec Z = \text{vec}\left(z_1, z_2, \ldots, z_T\right) \in \mathbf{R}^{T \cdot d}
```
where each element $z_t$ is referred to as a *knot point* and normally contains *state* variables and *control* variables. In a simple situation we might have $z_t = \text{vec} (x_t, u_t) \in \mathbf{R}^{d = n+m}$, for the state $x_t \in \mathbf{R}^n$ and control $u_t \in \mathbf{R}^m$. In this case, with `dim = n + m`, we can use `slice` and `index`, to extract what we want from $\vec Z$ in the following way:
* extract $z_t$:
```julia
zₜ = Z⃗[slice(t, dim)]
```
* extract $x_t$:
```julia
xₜ = Z⃗[slice(t, 1:n, dim)]
```
* extract $u_t$:
```julia
uₜ = Z⃗[slice(t, (1:m) .+ n, dim)]
```
* extract $i$-th component of $x_t$:
```julia
xₜⁱ = Z⃗[index(t, i, dim)]
```
* extract $j$-th component of $u_t$:
```julia
uₜʲ = Z⃗[index(t, j + n, dim)]
```
With this, the user is still responsible for keeping track of the component indices for $x$ and $u$, and possibly other variables. To alleviate this nuisance, the package [NamedTrajectories.jl](https://github.com/harmoniqs/NamedTrajectories.jl) provides a richer alternative for handling trajectory data with arbitrarily named components, please check it out!
## Installation
This package is not yet registered. To install, use the following command:
```julia
using Pkg
Pkg.add(url="https://github.com/harmoniqs/TrajectoryIndexingUtils.jl", rev="main")
```
## Methods
### The `index` function
```julia
index(t::Int, dim::Int) -> zₜ[dim]
index(t::Int, pos::Int, dim::Int) -> zₜ[pos]
```
### The `slice` function
```julia
slice(t::Int, dim::Int; stretch=0) -> zₜ[1:dim + stretch] # can be used to extract, e.g., [xₜ; xₜ₊₁], with stretch = dim
slice(t::Int, pos::Int, dim::Int) -> zₜ[1:pos]
slice(t::Int, pos1::Int, pos2::Int, dim::Int) -> zₜ[pos1:pos2]
slice(t::Int, indices::AbstractVector{Int}, dim::Int) -> zₜ[indices]
slice(ts::UnitRange{Int}, dim::Int) -> vec(zₜ for t ∈ ts)
```
## TODO
- [ ] Add tests
- [ ] Add examples
- [ ] Add documentation
- [ ] Add methods that take in trajectory vector as the first argument
- [ ] Add `block` function for accessing blocks of matrices with trajectory structure, e.g. jacobians & hessians