Skip to content

Full shape match check for nested containers #1935

@nhuurre

Description

@nhuurre

Description

The Stan math library has several functions for checking that two containers have the same size.

  • check_matching_dims
  • check_matching_sizes
  • check_consistent_sizes
  • check_consistent_sizes_mvt

But these are limited. The first one only operates on matrices. The other three check only the outermost dimension.
Sometimes it's necessary to validate that all dimensions match. For example

std::vector<Eigen::VectorXd> a(2, Eigen::VectorXd::Constant(2, 1.0));
std::vector<Eigen::VectorXd> b(N, Eigen::VectorXd::Constant(K, 2.0));
stan::math::check_consistent_sizes("vectorized_add", "A", a, "B", b);
stan::math::apply_scalar_binary(a, b, [=](double x, double y) { return x + y; });
  • N=2,K=2: ok
  • N=3,K=2: throws exception "A has size = 2, but B has size 3; and they must be the same size." (as expected)
  • N=2,K=3: aborted: assertion failed (very bad)

I need something that can check arbitrary deep dimensions for stan-dev/stanc3#552

Current Version:

v3.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions