constant standard vector and Eigen matrix initializers
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 839
- Forks
- 220
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 14
Description
Description
For initialization in the language and for general use, we need functions that will initialize constant containers without allocating new vari and without placing those vari on the stack to have their chain() methods called. What we need is something like this with an explicit template parameter,
template <typename T>
std::vector<T> constant_vector(size_t N, double value);
that would be implemented someting like this:
template <typename T>
inline std::vector<T> init_std_vector(size_t n) {
return std::vector<T>(n, std::numeric_limits<double>::quiet_Nan());
}
template <>
inline std::vector<var> init_std_vector<var>(size_t n) {
std::vector<var> x;
x.reserve(n);
vari* dummy_vi = new vari(std::numeric_limits<double>::quiet_Nan(), false);
for (size_t i = 0; i < n; ++i)
x.emplace_back(dummy_vi);
return x;
}
That creates a std::vector<var> with only a single vari that's not on the autodiff stack.
We want to do the same thing for Eigen::Matrix<T, R, C>.
I'm not sure how this will interact with the existing functions that have recently been added. Those do not have template parameters and default to primitive types (double and int).
I also think we can get rid of the no-chain stack as I don't think it's being used for anything, but that's a different PR.
This came up in an issue in stanc3
Example
Current Version:
v3.1.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the existing constant-container initialization functions referenced in the issue and the std::vector/Eigen initialization entry points; first compare their behavior with the no-chain stack and vari allocation described here. Done means templated constant initializers exist for std::vector and Eigen::Matrix, use one non-stacked vari as intended, and their interaction with existing primitive-default helpers is resolved.
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