stan-dev / stan-dev/math

Deeply nested containers and error checks

Open
#1,635 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

code cleanup feature
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

check_not_nan (and friends) work element-wise on containers, but we only allow them to work on containers that are one level deep. Its easy to allow them to work on deeper containers, but we want indices in the error message and that makes things a bit more complicated. Basically, recursively traverse the containers and check for nans at the bottom, then, if we find a nan, collect indices on the way back up the call stack and build the error message at the top.

If we don't find a nan, then we don't want to be building up error messages that we end up not using, which makes this sort of implementation unsatisfactory:

void check_not_nan(function, name, y) {
  for (size_t n = 0; n < size(y); n++) {
    std::string name_ = std::string(name) + "[" + std::to_string(n) + "]";
    check_not_nan(function, name_.c_str(), stan::get(y, n));
  }
}

Also, parameterize the check so the other check functions can use the same code.

I think I remember @bob-carpenter talking about this recently but I can't find where. Anyway it came up in #1626

Example

std::vector<std::vector<double> > x;
x.push_back({1, 2, 3});
x.push_back({1, 2, 3, 4});
x.push_back({1, 2, stan::math::NOT_A_NUMBER});
x.push_back({1, 2});
check_not_nan("some_function", "x", x);

Expected Output

"some_function: x[2][2] is nan, but must not be nan!"

Current Version:

v3.0.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with check_not_nan and the related check functions, using the nested-vector example in the issue to reproduce the current limitation. Trace how container elements and error names are handled, then verify that a deep NaN reports its full index path and that the same approach supports the other checks without unnecessary error construction.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.