SciML / SciML/ComponentArrays.jl
unpack columns of a ComponentMatrix
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 375
- Forks
- 42
- Avg merge
- 7h 25m
- Merged PRs (30d)
- 17
Description
Is your feature request related to a problem? Please describe.
I use ComponentArrays to pass driver data using a DataLoader to a cost function of a process based model optimization, where I extract several sub-matrices defined by named column ranges. Currently, I repeat lines for each submatrix - variable pair.
var1 = ca[:,Val(:key1)]
var2 = ca[:,Val(:key2)]
...
Describe the solution you’d like
Hence, I would like a macro similar to @static_unpack that extracts columns from a ComponentMatrix.
xs = ComponentMatrix(collect(reshape(1:(2*5),2,5)), (Shaped1DAxis((2,)), Axis(a=1, b=2:4, c=5:5)))
@static_unpack_col a, b, c = xs # known size
@test a isa SVector{2, Int64}
@test a == getdata(xs)[:, 1]
Or for larger Arrays a similar macro that provides views rather than creating StaticArrays.
Describe alternatives you’ve considered
I tried mapping across keys, but that introduces dynamic dispatch and/or type instability.
This is because, the Val(keyvar) is not a literal any more.
Omitting the Val inside the map, introduces dynamic dispatch and unstable return value.
Additional context
I provide an implementation by adapting the @static_unpack macro and an example in a test
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read the existing @static_unpack adaptation in src/compat/static_arrays.jl around line 71, then inspect the example in test/runtests.jl around line 705. Run the relevant tests and verify that unpacking the ComponentMatrix columns produces the expected values and types, including the view-based behavior for larger arrays if supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- data
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100