Skip to content

Add VectorElement: scalar view of a RealVector element #60

@alexeid

Description

@alexeid

Summary

Add a VectorElement<D extends Real> class to beast.base.spec.inference.parameter that presents a single element of a RealVector<D> as a RealScalar<D>.

This is a fundamental operation on the spec type system: wherever a RealScalar is required (e.g., SiteModel.mutationRate), a VectorElement can provide a scalar view into an element of a vector parameter.

Motivation

In LPhyBeast, when a vector parameter (e.g., mutation rates from WeightedDirichlet) needs to be split across multiple consumers (e.g., per-partition SiteModel), the current approach creates individual RealParameter state nodes and joins them with feast's Concatenate. This is backwards — it builds parts then reassembles them.

With VectorElement, the natural approach is:

  1. Create a single RealVectorParam as the state node
  2. Use VectorElement to extract scalar views where needed

This eliminates the need for Concatenate in converters and replaces the old beastlabs.core.util.Slice (which doesn't exist in beast3) with a strongly-typed equivalent.

Proposed API

package beast.base.spec.inference.parameter;

@Description("Scalar view of a single element in a RealVector")
public class VectorElement<D extends Real> extends CalculationNode implements RealScalar<D> {
    Input<RealVector<?>> vectorInput;  // wildcard required by BEAST's Input reflection
    Input<Integer> indexInput;         // index of the element

    double get();        // delegates to vector.get(index)
    D getDomain();       // delegates to vector.getDomain() (unchecked cast, safe via typed constructor)
}

Note: the vectorInput uses RealVector<?> rather than RealVector<D> because BEAST's Input.determineClass() cannot resolve type variables via reflection. The domain type safety is preserved through the typed constructor VectorElement(RealVector<D>, int).

Example XML

<stateNode id="r" spec="spec.inference.parameter.RealVectorParam"
           domain="PositiveReal" value="0.33 0.33 0.33"/>

<mutationRate id="r_0" spec="spec.inference.parameter.VectorElement"
              vector="@r" index="0"/>
<mutationRate id="r_1" spec="spec.inference.parameter.VectorElement"
              vector="@r" index="1"/>

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