Builder

Fluent API for building block/scope sparsity hierarchies.

Example:

builder = (
    SparsityBuilder()
    .add_block(param_u, (2, 2, 2, 2), name="U")
    .add_block(param_v, (2, 2, 2, 2), name="V")
    .add_scope("U", scope_shape=(1, 1), name="gU")
    .add_scope("V", scope_shape=(1, 4), name="gV")
    .couple_scopes(
        ["gU", "gV"],
        orders=[(0,1,2,3), (1,0,2,3)],
        name="UV",
    )
)
coupling = builder.get_scope("UV")
class sparsekit.builder.SparsityBuilder[source]

Bases: object

Fluent builder for constructing BlockSpec/ScopeSpec hierarchies.

All mutating methods return self for method chaining.

add_block(param, block_shape, name)[source]

Register a single parameter with its block decomposition.

Parameters:
couple_blocks(block_names, orders, name)[source]

Create a BlockCoupling from previously added blocks.

Parameters:
get_block(name)[source]

Retrieve a BlockSpec or BlockCoupling by name.

Parameters:

name (str)

Return type:

BlockCoupling | BlockSpec

add_scope(block_name, scope_shape, name)[source]

Add a ScopeSpec over an existing block or coupling.

Parameters:
couple_scopes(scope_names, orders, name)[source]

Create a ScopeCoupling from previously added scopes.

Scopes are consumed (popped) from the builder when coupled.

Parameters:
get_scope(name)[source]

Retrieve a ScopeSpec or ScopeCoupling by name.

Parameters:

name (str)

Return type:

ScopeSpec | ScopeCoupling

get_all_scopes()[source]

Return all uncoupled scopes and scope couplings.

Return type:

Iterable[ScopeSpec | ScopeCoupling]