Skip to content

unpack columns of a ComponentMatrix #315

@bgctw

Description

@bgctw

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions