Add `check_foreach(...)` function that does multiple checks at once on an array or matrix

オープン
#3,417 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
cpp
領域
backend

調査の方向性

Start by reading the existing check_not_nan, check_positive, and check_finite implementations, including their vectorized overloads and index-reporting behavior. Define the lambda interfaces and confirm how check_foreach should handle arrays and Eigen matrices; it is done when one traversal performs all requested checks while preserving the failing index in errors.

索引モデルが issue の本文から書いたものです。

説明

code cleanup new function

Often we have code that does a series of checks on an std::vector or Eigen matrix like the following

   check_not_nan(function, "log odds parameter", beta);    
   check_positive(function, "log odds parameter", beta);
   check_finite(function, "log odds parameter", beta);

The issue is that this causes three separate passes over beta. If beta is a large vector this can be kind of expensive.

We could make this nicer via one of two schems

  1. Just use a loop for these types of checks
for (auto&& beta_i : beta) 
   check_not_nan(function, "log odds parameter", beta_i);    
   check_positive(function, "log odds parameter", beta_i);
   check_finite(function, "log odds parameter", beta_i);
}

But then we lose the nice information that the vectorized checks do where it also tells us what index caused the errors.

  1. A check_foreach function that checks multiple things at every line.
    For this we would also need lambdas for each of the check functions. The user would call something like this
check_foreach(function, "log odds parameter", beta, check_not_nan_p, check_positive_p, check_finite_p);

Under the hood this would expand into a loop that handles all of the indexing so that the end error still has the correct index information.

for (Eigen::Index = 0; i < x.size(); i++) {
  check_not_nan_p(function, name, x[i], i);  
  check_positive_p(function, name, x[i], i);  
  check_finite_p(function, name, x[i], i);  
}

So then we only do one pass over the memory and still get the pretty checks

主要言語
C++
スター
845
フォーク
225
平均マージ
3日 9時間
マージ済み PR(30日)
12

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

stan-dev/math のほかの issue

stan-dev/math の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。