The fwd jacobian sizes both J and fx from the input dimension before evaluating f; J.col(0) = fx_fvar.d() then assigns an m-vector into an n-row column:
struct F { // R^2 -> R^3
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1> operator()(
const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) const {
Eigen::Matrix<T, Eigen::Dynamic, 1> out(3);
out << x(0), x(0) + x(1), x(0) * x(1);
return out;
}
};
stan::math::jacobian(F{}, x, fx, J); // x = [1.5, 2.0]
// fwd: Eigen assertion "DenseBase::resize() does not actually allow to resize." -> abort
The rev jacobian sizes from the output and handles the identical functor correctly (fx = 3-vector, J = 3×2). With assertions off, the fwd version writes past the storage instead of aborting.
Probably needs to evaluate f first and size from fx_fvar.size() simliar to the rev implementation.
The fwd
jacobiansizes bothJandfxfrom the input dimension before evaluatingf;J.col(0) = fx_fvar.d()then assigns an m-vector into an n-row column:The rev
jacobiansizes from the output and handles the identical functor correctly (fx = 3-vector, J = 3×2). With assertions off, the fwd version writes past the storage instead of aborting.Probably needs to evaluate
ffirst and size fromfx_fvar.size()simliar to the rev implementation.