From 3789a59cc6a7da357468e5092bfa8b4d873c8c22 Mon Sep 17 00:00:00 2001 From: Tal Kedar Date: Thu, 30 Jul 2020 13:12:02 -0400 Subject: [PATCH] [NFC] base_hmc const accessor to _z --- src/stan/mcmc/hmc/base_hmc.hpp | 23 +++++++++++++++++++++++ src/test/unit/mcmc/hmc/base_hmc_test.cpp | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/src/stan/mcmc/hmc/base_hmc.hpp b/src/stan/mcmc/hmc/base_hmc.hpp index a4c5de62e81..7713486fd1d 100644 --- a/src/stan/mcmc/hmc/base_hmc.hpp +++ b/src/stan/mcmc/hmc/base_hmc.hpp @@ -15,6 +15,15 @@ namespace stan { namespace mcmc { +/** + * Base class for Hamiltonian samplers. + * + * @tparam Model The type of the Stan model. + * @tparam Hamiltonian The type of Hamiltonians over the (unconstrained) + * parameter space. + * @tparam Integrator The type of integrator (e.g. leapfrog). + * @tparam BaseRNG The type of random number generator. + */ template class Hamiltonian, template class Integrator, class BaseRNG> class base_hmc : public base_mcmc { @@ -132,8 +141,22 @@ class base_hmc : public base_mcmc { this->z_.ps_point::operator=(z_init); } + /** + * Gets the current point in the (unconstrained) parameter space. + * + * @return The current point in the (unconstrained) parameter space. + */ typename Hamiltonian::PointType& z() { return z_; } + /** + * Gets the current point in the (unconstrained) parameters space. + * + * @return The current point in the (unconstrained) parameters space. + */ + const typename Hamiltonian::PointType& z() const noexcept { + return z_; + } + virtual void set_nominal_stepsize(double e) { if (e > 0) nom_epsilon_ = e; diff --git a/src/test/unit/mcmc/hmc/base_hmc_test.cpp b/src/test/unit/mcmc/hmc/base_hmc_test.cpp index abcf122aa30..00742532075 100644 --- a/src/test/unit/mcmc/hmc/base_hmc_test.cpp +++ b/src/test/unit/mcmc/hmc/base_hmc_test.cpp @@ -46,6 +46,13 @@ TEST(McmcBaseHMC, point_construction) { EXPECT_EQ(static_cast(q.size()), sampler.z().g.size()); } +TEST(McmcBaseHMC, point_access_from_const_hmc) { + rng_t base_rng(0); + const stan::mcmc::mock_hmc sampler(stan::mcmc::mock_model(2), base_rng); + EXPECT_EQ(2, sampler.z().q.size()); + EXPECT_EQ(2, sampler.z().g.size()); +} + TEST(McmcBaseHMC, seed) { rng_t base_rng(0);