From 4727ac706803fc9eedf29cd0bcc28555421f9334 Mon Sep 17 00:00:00 2001 From: Nicusor Serban <48496524+serban-nicusor-toptal@users.noreply.github.com> Date: Thu, 18 Jul 2019 23:24:46 +0200 Subject: [PATCH 001/167] Fixed typo in release notes --- RELEASE-NOTES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index b378bfecdae..1f0b2660ae0 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,7 +1,7 @@ Stan Math Library Release Notes ====================================================================== -v2.19.2 (18 July 2019) +v2.20.0 (18 July 2019) ====================================================================== New Features From a7070395b70198647ae5e80a7417a9523f4655af Mon Sep 17 00:00:00 2001 From: Nicusor Serban Date: Fri, 18 Oct 2019 21:49:02 +0200 Subject: [PATCH 002/167] Revert "release/v2.21.0: updating version numbers" This reverts commit a50deb9f6dbc35ba221e3685b9d8a19202a80fd9. --- .github/ISSUE_TEMPLATE.md | 2 +- RELEASE-NOTES.txt | 84 --------------------------------------- doxygen/doxygen.cfg | 2 +- stan/math/version.hpp | 2 +- 4 files changed, 3 insertions(+), 87 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 963186bcf5d..6127caf2694 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -25,4 +25,4 @@ If this is a **feature request**, show what you expect to happen if the feature #### Current Version: -v2.21.0 +v2.20.0 diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 8351a003dfc..1f0b2660ae0 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,89 +1,5 @@ Stan Math Library Release Notes -====================================================================== -v2.21.0 (18 October 2019) -====================================================================== - -## Features - -PR [#1180](https://github.com/stan-dev/math/pull/1180) brought the [Intel TBB](https://github.com/intel/tbb) into Stan as a dependency, which we will be using in the future for CPU parallelism all across Stan! The TBB is an excellent framework that will let Stan utilize nested parallelism across the algorithms and gradient evaluations. The licensing for the Intel TBB library is under the Apache 2.0 license. This dependency implies an additional restriction as compared to the new BSD license alone. The Apache 2.0 license is incompatible with GPL-2 licensed code if the software if distributing the software as a unitary binary. Refer to the Apache 2.0 evaluation page on the Stan Math [wiki](https://github.com/stan-dev/math/wiki/Apache-2.0-License-Evaluation). - -With @t4c1's large contributions, we now have GPU/OpenCL support for many of our glm functions! @rok-cesnovar added an OpenCL reverse mode specialization for `multiplication` and `mdivide_left_tri` while @t4c1 added the OpenCL specialization for `gp_exp_quad_cov`. - -Some other nice features include @andrjohns vectorizing the Dirichlet distribution, @IvanYashchuk implementing a reverse mode specialization for inverse, and @yizhang-yiz with @charlesm93 adding fixed point algebra solvers based on Sundial's KINSOL scheme. - -Internally, @bob-carpenter added a new AD testing framework, which both replaced 18,539 of code with 2,500 and simultaneously increased our test coverage! @Stevebronder added a type traits metaprogramming scheme so that we can make use of more generic templating in a lot of our code. Last but not least, @andrjohns standardized a lot of our code to use standard library functions instead of our hand-rolled methods. - -## Fixes - -@wds15 patched the way we use `lgamma` so that it's faster in concurrent settings. A speedy patch came in from @t4c1 when @jgabry reported intercept only glm specializations with size zero matrices could give the wrong output. We had several patches and code cleanups in the OpenCL code, mostly testing and improving the type trait system around the OpenCL methods. @nhuurre patched `log_sum_exp` and `log_diff_exp` so that the methods respected boundary conditions a bit better. Stan also now uses `clang-tidy,` which gives us an automated way to keep the code base standardized. - -## Features List - -|Contributor |Title | -|:---------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------| -|[bob-carpenter](https://github.com/bob-carpenter) : ([#1384](https://github.com/stan-dev/math/pull/1384)) |Feature/1382 remove fvar nan checks | -|[wds15](https://github.com/wds15) : ([#1376](https://github.com/stan-dev/math/pull/1376)) |integrate Intel TBB | -|[yizhang-yiz](https://github.com/yizhang-yiz) : ([#1371](https://github.com/stan-dev/math/pull/1371)) |Feature fp solver | -|[t4c1](https://github.com/t4c1) : ([#1366](https://github.com/stan-dev/math/pull/1366)) |Gpu ordered_logistic_glm_lpmf and categorical_logit_glm_lpmf | -|[t4c1](https://github.com/t4c1) : ([#1365](https://github.com/stan-dev/math/pull/1365)) |Gpu neg_binomial_2_log_glm | -|[andrjohns](https://github.com/andrjohns) : ([#1363](https://github.com/stan-dev/math/pull/1363)) |Issue 1362 - Vectorised Dirichlet distribution | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1355](https://github.com/stan-dev/math/pull/1355)) |Feature/issue 1354 Implement matrix_cl overloads for rep_vector, rep_row_vector and rep_matrix | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1353](https://github.com/stan-dev/math/pull/1353)) |Revert GPU caching | -|[t4c1](https://github.com/t4c1) : ([#1350](https://github.com/stan-dev/math/pull/1350)) |Gpu poisson bernoulli glms | -|[SteveBronder](https://github.com/SteveBronder) : ([#1344](https://github.com/stan-dev/math/pull/1344)) |Adds require_* template type traits | -|[charlesm93](https://github.com/charlesm93) : ([#1339](https://github.com/stan-dev/math/pull/1339)) |Feature/issue 1115 newton solver | -|[IvanYashchuk](https://github.com/IvanYashchuk) : ([#1334](https://github.com/stan-dev/math/pull/1334)) |Implemented reverse mode for inverse | -|[t4c1](https://github.com/t4c1) : ([#1333](https://github.com/stan-dev/math/pull/1333)) |Implement normal_id_glm_lpdf in OpenCL | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1329](https://github.com/stan-dev/math/pull/1329)) |Feature/Issue 1294 Rewrite the test-math-dependencies script in Python | -|[SteveBronder](https://github.com/SteveBronder) : ([#1323](https://github.com/stan-dev/math/pull/1323)) |Adds const ref and ref returns for to_var/fvar methods | -|[andrjohns](https://github.com/andrjohns) : ([#1318](https://github.com/stan-dev/math/pull/1318)) |Issue 1010 - Replace hand-coded math with standard library c++11 functions | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1303](https://github.com/stan-dev/math/pull/1303)) |Feature/issue 1221 Use OpenCL in rev/mdivide_left_tri | -|[andrjohns](https://github.com/andrjohns) : ([#1296](https://github.com/stan-dev/math/pull/1296)) |issue 1279 - Remove deprecated Eigen content from math headers | -|[t4c1](https://github.com/t4c1) : ([#1293](https://github.com/stan-dev/math/pull/1293)) |OpenCL matrix multiplication optimizations | -|[andrjohns](https://github.com/andrjohns) : ([#1283](https://github.com/stan-dev/math/pull/1283)) |Refactor rev/mat with eigen plugin methods | -|[SteveBronder](https://github.com/SteveBronder) : ([#1281](https://github.com/stan-dev/math/pull/1281)) |Add a double template to matrix_cl | -|[bob-carpenter](https://github.com/bob-carpenter) : ([#1262](https://github.com/stan-dev/math/pull/1262)) |Feature/1258 ad test core | -|[t4c1](https://github.com/t4c1) : ([#1252](https://github.com/stan-dev/math/pull/1252)) |Implement ordinal regression GLM (ordered_logistic_glm_lpmf) | -|[t4c1](https://github.com/t4c1) : ([#1206](https://github.com/stan-dev/math/pull/1206)) |opencl prim gp_exp_quad_cov | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1305](https://github.com/stan-dev/math/pull/1305)) |Feature/issue 1221 Use OpenCL in rev/multiply | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1278](https://github.com/stan-dev/math/pull/1278)) |Feature/1221 OpenCL primitive multiply | -|[t4c1](https://github.com/t4c1) : ([#1299](https://github.com/stan-dev/math/pull/1299)) |mdivide_right_tri can use OpenCL | -|[wds15](https://github.com/wds15) : ([#1180](https://github.com/stan-dev/math/pull/1180)) |Feature/intel tbb lib | - -## Fixes - -|Contributor |Title | -|:-------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------| -|[wds15](https://github.com/wds15) : ([#1401](https://github.com/stan-dev/math/pull/1401)) |Bugfix/tbb cleanup | -|[t4c1](https://github.com/t4c1) : ([#1399](https://github.com/stan-dev/math/pull/1399)) |bugfix intercept only GLMs | -|[wds15](https://github.com/wds15) : ([#1395](https://github.com/stan-dev/math/pull/1395)) |allow spaces in path leading to stan-directory in makefiles | -|[SteveBronder](https://github.com/SteveBronder) : ([#1392](https://github.com/stan-dev/math/pull/1392)) |Add /lib/tbb/** to the .gitignore | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1375](https://github.com/stan-dev/math/pull/1375)) |Fix bug in stack_alloc_test | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1369](https://github.com/stan-dev/math/pull/1369)) |Bugfix/remove unused vectorize test | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1364](https://github.com/stan-dev/math/pull/1364)) |Reorganize /opencl and add missing matrix_cl overloads | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1361](https://github.com/stan-dev/math/pull/1361)) |Remove const qualifier from matrix_cl rows & cols | -|[t4c1](https://github.com/t4c1) : ([#1358](https://github.com/stan-dev/math/pull/1358)) |Split opencl glm function | -|[SteveBronder](https://github.com/SteveBronder) : ([#1356](https://github.com/stan-dev/math/pull/1356)) |Bugfix for making matrix_cls from temporaries | -|[SteveBronder](https://github.com/SteveBronder) : ([#1341](https://github.com/stan-dev/math/pull/1341)) |Refactor Type Traits | -|[SteveBronder](https://github.com/SteveBronder) : ([#1340](https://github.com/stan-dev/math/pull/1340)) |Refactor/clang tidy cleanup | -|[SteveBronder](https://github.com/SteveBronder) : ([#1337](https://github.com/stan-dev/math/pull/1337)) |Update OpenCL Headers | -|[SteveBronder](https://github.com/SteveBronder) : ([#1331](https://github.com/stan-dev/math/pull/1331)) |Moves if statements for scal/prob/beta-binomial out of for loops | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1330](https://github.com/stan-dev/math/pull/1330)) |Remove EXPECT_DEATH unit tests that fail when -NDEBUG is set | -|[SteveBronder](https://github.com/SteveBronder) : ([#1327](https://github.com/stan-dev/math/pull/1327)) |Adds clang-tidy to makefile | -|[t4c1](https://github.com/t4c1) : ([#1314](https://github.com/stan-dev/math/pull/1314)) |fix matrix_cl_view test | -|[t4c1](https://github.com/t4c1) : ([#1311](https://github.com/stan-dev/math/pull/1311)) |Fixed matrix_cl copying and moving | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1310](https://github.com/stan-dev/math/pull/1310)) |Cleanup/issue #1301 remove unnecessary Boost and other compiler flags | -|[rok-cesnovar](https://github.com/rok-cesnovar) : ([#1304](https://github.com/stan-dev/math/pull/1304)) |Re-apply #1278 OpenCL prim multiply | -|[SteveBronder](https://github.com/SteveBronder) : ([#1298](https://github.com/stan-dev/math/pull/1298)) |make key of map for opencl kernel options into a string | -|[SteveBronder](https://github.com/SteveBronder) : ([#1291](https://github.com/stan-dev/math/pull/1291)) |Changes all prim files to use *_return_type_t instead of typename *_return_type | -|[nhuurre](https://github.com/nhuurre) : ([#1290](https://github.com/stan-dev/math/pull/1290)) |Bugfix/646 log_sum_exp and log_diff_exp boundaries | -|[SteveBronder](https://github.com/SteveBronder) : ([#1289](https://github.com/stan-dev/math/pull/1289)) |Refactor for enable_if functions | -|[SteveBronder](https://github.com/SteveBronder) : ([#1286](https://github.com/stan-dev/math/pull/1286)) |Removes extra loops in Jacobian calculations | -|[t4c1](https://github.com/t4c1) : ([#1266](https://github.com/stan-dev/math/pull/1266)) |Added triangularity attribute to matrix_cl | -|[t4c1](https://github.com/t4c1) : ([#1261](https://github.com/stan-dev/math/pull/1261)) |GLM tests improvements | -|[wds15](https://github.com/wds15) : ([#1255](https://github.com/stan-dev/math/pull/1255)) |Bugfix/issue 1250 lgamma | - ====================================================================== v2.20.0 (18 July 2019) ====================================================================== diff --git a/doxygen/doxygen.cfg b/doxygen/doxygen.cfg index be71ec3b7fc..9ac53d0280e 100644 --- a/doxygen/doxygen.cfg +++ b/doxygen/doxygen.cfg @@ -38,7 +38,7 @@ PROJECT_NAME = "Stan Math Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 2.21.0 +PROJECT_NUMBER = 2.20.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/stan/math/version.hpp b/stan/math/version.hpp index d70c4badca3..b64aff29ae8 100644 --- a/stan/math/version.hpp +++ b/stan/math/version.hpp @@ -12,7 +12,7 @@ #endif #define STAN_MATH_MAJOR 2 -#define STAN_MATH_MINOR 21 +#define STAN_MATH_MINOR 20 #define STAN_MATH_PATCH 0 namespace stan { From d4e4f15aea1b2883efe700048e216bac68b225d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Ce=C5=A1novar?= Date: Wed, 29 Jan 2020 19:05:54 +0100 Subject: [PATCH 003/167] Fixed conflict in stan/math/prim/fun/log_softmax.hpp --- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 4 ++-- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 4 ++-- stan/math/prim/fun/inv_cloglog.hpp | 4 ++-- stan/math/prim/fun/inv_sqrt.hpp | 4 ++-- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log_softmax.hpp | 11 ++++++----- stan/math/prim/fun/minus.hpp | 4 +++- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 4 ++-- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- stan/math/prim/vectorize/apply_scalar_unary.hpp | 7 +++++-- test/unit/math/prim/fun/Phi_approx_test.cpp | 8 ++++++++ test/unit/math/prim/fun/Phi_test.cpp | 8 ++++++++ test/unit/math/prim/fun/acos_test.cpp | 8 ++++++++ test/unit/math/prim/fun/acosh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/asin_test.cpp | 8 ++++++++ test/unit/math/prim/fun/asinh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/atan_test.cpp | 8 ++++++++ test/unit/math/prim/fun/atanh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cbrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/ceil_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cos_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cosh_test.cpp | 10 ++++++++++ test/unit/math/prim/fun/digamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/erf_test.cpp | 8 ++++++++ test/unit/math/prim/fun/erfc_test.cpp | 8 ++++++++ test/unit/math/prim/fun/exp2_test.cpp | 8 ++++++++ test/unit/math/prim/fun/exp_test.cpp | 8 ++++++++ test/unit/math/prim/fun/expm1_test.cpp | 8 ++++++++ test/unit/math/prim/fun/fabs_test.cpp | 8 ++++++++ test/unit/math/prim/fun/floor_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_Phi_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_cloglog_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_sqrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_square_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_test.cpp | 8 ++++++++ test/unit/math/prim/fun/lgamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log10_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1m_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1p_exp_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1p_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log2_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_inv_logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_softmax_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_test.cpp | 8 ++++++++ test/unit/math/prim/fun/logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/minus_test.cpp | 8 ++++++++ test/unit/math/prim/fun/round_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sin_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sinh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sqrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/square_test.cpp | 10 +++++++++- test/unit/math/prim/fun/tan_test.cpp | 10 ++++++++++ test/unit/math/prim/fun/tanh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/tgamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/trigamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/trunc_test.cpp | 8 ++++++++ 70 files changed, 420 insertions(+), 34 deletions(-) create mode 100644 test/unit/math/prim/fun/cosh_test.cpp create mode 100644 test/unit/math/prim/fun/tan_test.cpp diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 709864e1db6..e5960f1b758 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -45,7 +45,7 @@ inline auto acos(const T& x) { template > inline auto acos(const Eigen::MatrixBase& x) { - return x.derived().array().acos().matrix(); + return x.derived().array().acos().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 81a22dcf60b..0f5d172a29b 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -45,7 +45,7 @@ inline auto asin(const T& x) { template > inline auto asin(const Eigen::MatrixBase& x) { - return x.derived().array().asin().matrix(); + return x.derived().array().asin().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 28a05b08d6a..9576c99240a 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t atan(const T& x) { template > inline auto atan(const Eigen::MatrixBase& x) { - return x.derived().array().atan().matrix(); + return x.derived().array().atan().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 7a78566ac1d..2de0ddb72e7 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -45,7 +45,7 @@ inline auto ceil(const T& x) { template > inline auto ceil(const Eigen::MatrixBase& x) { - return x.derived().array().ceil().matrix(); + return x.derived().array().ceil().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 0c20e53f935..bf9761ace9f 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -45,7 +45,7 @@ inline auto cos(const T& x) { template > inline auto cos(const Eigen::MatrixBase& x) { - return x.derived().array().cos().matrix(); + return x.derived().array().cos().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 3dee3eae2a3..2a0402fe9c2 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t cosh(const T& x) { template > inline auto cosh(const Eigen::MatrixBase& x) { - return x.derived().array().cosh().matrix(); + return x.derived().array().cosh().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 4d60b4c04b5..54486d9e394 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -60,7 +60,7 @@ inline auto exp(const T& x) { template > inline auto exp(const Eigen::MatrixBase& x) { - return x.derived().array().exp().matrix(); + return x.derived().array().exp().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 1dc51b0ee3c..2bfc97071d2 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t fabs(const T& x) { template > inline auto fabs(const Eigen::MatrixBase& x) { - return x.derived().array().abs().matrix(); + return x.derived().array().abs().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto fabs(const Eigen::MatrixBase& x) { template > inline auto fabs(const Eigen::ArrayBase& x) { - return x.derived().abs(); + return x.derived().abs().eval(); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 7f1f37317cd..c53433cf356 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -45,7 +45,7 @@ inline auto floor(const T& x) { template > inline auto floor(const Eigen::MatrixBase& x) { - return x.derived().array().floor().matrix(); + return x.derived().array().floor().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index f50f17f784c..439dc962778 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -45,7 +45,7 @@ inline auto inv(const T& x) { template > inline auto inv(const Eigen::MatrixBase& x) { - return x.derived().array().inverse().matrix(); + return x.derived().array().inverse().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto inv(const Eigen::MatrixBase& x) { template > inline auto inv(const Eigen::ArrayBase& x) { - return x.derived().inverse(); + return x.derived().inverse().eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 945417aa0f9..44c6dfdc8d7 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -87,7 +87,7 @@ inline auto inv_cloglog(const T& x) { template > inline auto inv_cloglog(const Eigen::MatrixBase& x) { - return (1 - exp(-exp(x.derived().array()))).matrix(); + return (1 - exp(-exp(x.derived().array()))).matrix().eval(); } /** @@ -99,7 +99,7 @@ inline auto inv_cloglog(const Eigen::MatrixBase& x) { template > inline auto inv_cloglog(const Eigen::ArrayBase& x) { - return 1 - exp(-exp(x.derived())); + return (1 - exp(-exp(x.derived()))).eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index aa6e0d7ffdf..66b41ee7138 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -50,7 +50,7 @@ inline auto inv_sqrt(const T& x) { template > inline auto inv_sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().rsqrt().matrix(); + return x.derived().array().rsqrt().matrix().eval(); } /** @@ -62,7 +62,7 @@ inline auto inv_sqrt(const Eigen::MatrixBase& x) { template > inline auto inv_sqrt(const Eigen::ArrayBase& x) { - return x.derived().rsqrt(); + return x.derived().rsqrt().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 2e32bc5aa4b..3814d0a5ae5 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -60,7 +60,7 @@ inline auto log(const T& x) { template > inline auto log(const Eigen::MatrixBase& x) { - return x.derived().array().log().matrix(); + return x.derived().array().log().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 808bdb85bad..10b7b0fa1b0 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -45,7 +45,7 @@ inline auto log10(const T& x) { template > inline auto log10(const Eigen::MatrixBase& x) { - return x.derived().array().log10().matrix(); + return x.derived().array().log10().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index a51370d0b1a..8c4951df779 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -36,11 +36,12 @@ namespace math { * @param[in] v Vector to transform. * @return Unit simplex result of the softmax transform of the vector. */ -template -inline Eigen::Matrix log_softmax( - const Eigen::Matrix& v) { - check_nonzero_size("log_softmax", "v", v); - return v.array() - log_sum_exp(v); +template >>...> +inline auto log_softmax(const T& x) { + return apply_vector_unary::apply(x, [&](const auto& v) { + check_nonzero_size("log_softmax", "v", v); + return (v.array() - log_sum_exp(v)).matrix().eval(); + }); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 7f374462fc0..13fe7d090ed 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -1,6 +1,8 @@ #ifndef STAN_MATH_PRIM_FUN_MINUS_HPP #define STAN_MATH_PRIM_FUN_MINUS_HPP +#include + namespace stan { namespace math { @@ -12,7 +14,7 @@ namespace math { * @return Negation of subtrahend. */ template -inline auto minus(const T& x) { +inline plain_type_t minus(const T& x) { return -x; } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 66b2d6a428c..e5cc48efd18 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -62,7 +62,7 @@ inline auto round(const T& x) { template > inline auto round(const Eigen::MatrixBase& x) { - return x.derived().array().round().matrix(); + return x.derived().array().round().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index f2ed9102f97..0b783ffeb20 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -45,7 +45,7 @@ inline auto sin(const T& x) { template > inline auto sin(const Eigen::MatrixBase& x) { - return x.derived().array().sin().matrix(); + return x.derived().array().sin().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index 5e13a03993e..e4d1252295d 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -45,7 +45,7 @@ inline auto sinh(const T& x) { template > inline auto sinh(const Eigen::MatrixBase& x) { - return x.derived().array().sinh().matrix(); + return x.derived().array().sinh().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto sinh(const Eigen::MatrixBase& x) { template > inline auto sinh(const Eigen::ArrayBase& x) { - return x.derived().sinh(); + return x.derived().sinh().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index ae1b6dace93..fe81fcf75b6 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -54,7 +54,7 @@ inline auto sqrt(const T& x) { template > inline auto sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().sqrt().matrix(); + return x.derived().array().sqrt().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index 7064300e695..efc03beb94b 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -45,7 +45,7 @@ inline auto tan(const T& x) { template > inline auto tan(const Eigen::MatrixBase& x) { - return x.derived().array().tan().matrix(); + return x.derived().array().tan().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 60c4d405fb2..2fe9f4c5a25 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -45,7 +45,7 @@ inline auto tanh(const T& x) { template > inline auto tanh(const Eigen::MatrixBase& x) { - return x.derived().array().tanh().matrix(); + return x.derived().array().tanh().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/vectorize/apply_scalar_unary.hpp b/stan/math/prim/vectorize/apply_scalar_unary.hpp index ed31037a207..8cc283cd4ac 100644 --- a/stan/math/prim/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/vectorize/apply_scalar_unary.hpp @@ -59,8 +59,11 @@ struct apply_scalar_unary> { * by F to the specified matrix. */ static inline auto apply(const T& x) { - return x.unaryExpr( - [](scalar_t x) { return apply_scalar_unary::apply(x); }); + return x + .unaryExpr([](scalar_t x) { + return apply_scalar_unary::apply(x); + }) + .eval(); } /** diff --git a/test/unit/math/prim/fun/Phi_approx_test.cpp b/test/unit/math/prim/fun/Phi_approx_test.cpp index dbc4078352d..4a29923c572 100644 --- a/test/unit/math/prim/fun/Phi_approx_test.cpp +++ b/test/unit/math/prim/fun/Phi_approx_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, Phi_approx_nan) { EXPECT_TRUE(std::isnan(stan::math::Phi_approx(nan))); } + +TEST(MathFunctions, Phi_approx__works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::Phi_approx(b)); +} diff --git a/test/unit/math/prim/fun/Phi_test.cpp b/test/unit/math/prim/fun/Phi_test.cpp index c43e260bf88..1273ad24628 100644 --- a/test/unit/math/prim/fun/Phi_test.cpp +++ b/test/unit/math/prim/fun/Phi_test.cpp @@ -118,3 +118,11 @@ TEST(MathFunctions, Phi_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_THROW(stan::math::Phi(nan), std::domain_error); } + +TEST(MathFunctions, Phi_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::Phi(b)); +} diff --git a/test/unit/math/prim/fun/acos_test.cpp b/test/unit/math/prim/fun/acos_test.cpp index c66c4c49a02..7570f52cc71 100644 --- a/test/unit/math/prim/fun/acos_test.cpp +++ b/test/unit/math/prim/fun/acos_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, acos) { stan::test::expect_common_prim([](auto x) { return std::acos(x); }, [](auto x) { return stan::math::acos(x); }); } + +TEST(MathFunctions, acos_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::acos(b)); +} diff --git a/test/unit/math/prim/fun/acosh_test.cpp b/test/unit/math/prim/fun/acosh_test.cpp index 2d5bc742d3c..1f9e8ab7bb5 100644 --- a/test/unit/math/prim/fun/acosh_test.cpp +++ b/test/unit/math/prim/fun/acosh_test.cpp @@ -26,3 +26,11 @@ TEST(MathFunctions, acosh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::acosh(nan))); } + +TEST(MathFunctions, acosh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::acosh(b)); +} diff --git a/test/unit/math/prim/fun/asin_test.cpp b/test/unit/math/prim/fun/asin_test.cpp index a14245b58c2..858be5fbbbd 100644 --- a/test/unit/math/prim/fun/asin_test.cpp +++ b/test/unit/math/prim/fun/asin_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, asin) { stan::test::expect_common_prim([](auto x) { return std::asin(x); }, [](auto x) { return stan::math::asin(x); }); } + +TEST(MathFunctions, asin_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::asin(b)); +} diff --git a/test/unit/math/prim/fun/asinh_test.cpp b/test/unit/math/prim/fun/asinh_test.cpp index 151afc2b3ef..e10228b0af0 100644 --- a/test/unit/math/prim/fun/asinh_test.cpp +++ b/test/unit/math/prim/fun/asinh_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, asinh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::asinh(nan))); } + +TEST(MathFunctions, asinh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::asinh(b)); +} diff --git a/test/unit/math/prim/fun/atan_test.cpp b/test/unit/math/prim/fun/atan_test.cpp index 467ceefff7d..54861be5151 100644 --- a/test/unit/math/prim/fun/atan_test.cpp +++ b/test/unit/math/prim/fun/atan_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, atan) { stan::test::expect_common_prim([](auto x) { return std::atan(x); }, [](auto x) { return stan::math::atan(x); }); } + +TEST(MathFunctions, atan_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::atan(b)); +} diff --git a/test/unit/math/prim/fun/atanh_test.cpp b/test/unit/math/prim/fun/atanh_test.cpp index ca60e585e86..18ae151e23c 100644 --- a/test/unit/math/prim/fun/atanh_test.cpp +++ b/test/unit/math/prim/fun/atanh_test.cpp @@ -27,3 +27,11 @@ TEST(MathFunctions, atanh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::atanh(nan))); } + +TEST(MathFunctions, atanh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::atanh(b)); +} diff --git a/test/unit/math/prim/fun/cbrt_test.cpp b/test/unit/math/prim/fun/cbrt_test.cpp index 2ca66bbde53..854853943a6 100644 --- a/test/unit/math/prim/fun/cbrt_test.cpp +++ b/test/unit/math/prim/fun/cbrt_test.cpp @@ -24,3 +24,11 @@ TEST(MathFunctions, cbrt_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::cbrt(nan))); } + +TEST(MathFunctions, cbrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cbrt(b)); +} diff --git a/test/unit/math/prim/fun/ceil_test.cpp b/test/unit/math/prim/fun/ceil_test.cpp index f26f0068eb1..57a7dce8c41 100644 --- a/test/unit/math/prim/fun/ceil_test.cpp +++ b/test/unit/math/prim/fun/ceil_test.cpp @@ -4,3 +4,11 @@ TEST(primScalFun, ceil) { stan::test::expect_common_prim([](auto x) { return std::ceil(x); }, [](auto x) { return stan::math::ceil(x); }); } + +TEST(MathFunctions, ceil_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::ceil(b)); +} diff --git a/test/unit/math/prim/fun/cos_test.cpp b/test/unit/math/prim/fun/cos_test.cpp index 53daaa398de..94989f9703f 100644 --- a/test/unit/math/prim/fun/cos_test.cpp +++ b/test/unit/math/prim/fun/cos_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, cos) { stan::test::expect_common_prim([](auto x) { return std::cos(x); }, [](auto x) { return stan::math::cos(x); }); } + +TEST(MathFunctions, cos_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cos(b)); +} diff --git a/test/unit/math/prim/fun/cosh_test.cpp b/test/unit/math/prim/fun/cosh_test.cpp new file mode 100644 index 00000000000..d1433637b36 --- /dev/null +++ b/test/unit/math/prim/fun/cosh_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +TEST(MathFunctions, cosh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cosh(b)); +} diff --git a/test/unit/math/prim/fun/digamma_test.cpp b/test/unit/math/prim/fun/digamma_test.cpp index 2a3a7ea150b..b0d37f98b7e 100644 --- a/test/unit/math/prim/fun/digamma_test.cpp +++ b/test/unit/math/prim/fun/digamma_test.cpp @@ -18,3 +18,11 @@ TEST(MathFunctions, digamma_nan) { EXPECT_TRUE(std::isnormal(stan::math::digamma(1.0E50))); } + +TEST(MathFunctions, digamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::digamma(b)); +} diff --git a/test/unit/math/prim/fun/erf_test.cpp b/test/unit/math/prim/fun/erf_test.cpp index f01b192b8be..0c06057587c 100644 --- a/test/unit/math/prim/fun/erf_test.cpp +++ b/test/unit/math/prim/fun/erf_test.cpp @@ -20,3 +20,11 @@ TEST(MathFunctions, erfNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erf(nan))); } + +TEST(MathFunctions, erf_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::erf(b)); +} diff --git a/test/unit/math/prim/fun/erfc_test.cpp b/test/unit/math/prim/fun/erfc_test.cpp index aad40784135..2e19d0fe333 100644 --- a/test/unit/math/prim/fun/erfc_test.cpp +++ b/test/unit/math/prim/fun/erfc_test.cpp @@ -21,3 +21,11 @@ TEST(MathFunctions, erfcNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erfc(nan))); } + +TEST(MathFunctions, erfc_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::erfc(b)); +} diff --git a/test/unit/math/prim/fun/exp2_test.cpp b/test/unit/math/prim/fun/exp2_test.cpp index ae1c93c1e22..b5c8f356425 100644 --- a/test/unit/math/prim/fun/exp2_test.cpp +++ b/test/unit/math/prim/fun/exp2_test.cpp @@ -35,3 +35,11 @@ TEST(MathFunctions, exp2_nan) { EXPECT_TRUE(std::isnan(stan::math::exp2(nan))); } + +TEST(MathFunctions, exp2_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::exp2(b)); +} diff --git a/test/unit/math/prim/fun/exp_test.cpp b/test/unit/math/prim/fun/exp_test.cpp index a19d8643a9f..292f72baf10 100644 --- a/test/unit/math/prim/fun/exp_test.cpp +++ b/test/unit/math/prim/fun/exp_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, expInt) { EXPECT_FLOAT_EQ(std::exp(3), exp(3)); EXPECT_FLOAT_EQ(std::exp(3.0), exp(3.0)); } + +TEST(MathFunctions, exp_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::exp(b)); +} diff --git a/test/unit/math/prim/fun/expm1_test.cpp b/test/unit/math/prim/fun/expm1_test.cpp index 3e35d9873c1..a92bbc91f45 100644 --- a/test/unit/math/prim/fun/expm1_test.cpp +++ b/test/unit/math/prim/fun/expm1_test.cpp @@ -23,3 +23,11 @@ TEST(MathFunctions, expm1_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::expm1(nan))); } + +TEST(MathFunctions, expm1_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::expm1(b)); +} diff --git a/test/unit/math/prim/fun/fabs_test.cpp b/test/unit/math/prim/fun/fabs_test.cpp index 985f1426eda..e4450169d0a 100644 --- a/test/unit/math/prim/fun/fabs_test.cpp +++ b/test/unit/math/prim/fun/fabs_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, fabs) { stan::test::expect_common_prim([](auto x) { return std::fabs(x); }, [](auto x) { return stan::math::fabs(x); }); } + +TEST(MathFunctions, fabs_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::fabs(b)); +} diff --git a/test/unit/math/prim/fun/floor_test.cpp b/test/unit/math/prim/fun/floor_test.cpp index 6c283c47dbc..90c6dca6090 100644 --- a/test/unit/math/prim/fun/floor_test.cpp +++ b/test/unit/math/prim/fun/floor_test.cpp @@ -4,3 +4,11 @@ TEST(primScalFun, floor) { stan::test::expect_common_prim([](auto x) { return std::floor(x); }, [](auto x) { return stan::math::floor(x); }); } + +TEST(MathFunctions, floor_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::floor(b)); +} diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index 13d81adbb7f..9538f7151d6 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -34,3 +34,11 @@ TEST(MathFunctions, inv_Phi_nan) { EXPECT_THROW(inv_Phi(-2.0), std::domain_error); EXPECT_THROW(inv_Phi(2.0), std::domain_error); } + +TEST(MathFunctions, inv_Phi_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::inv_Phi(b)); +} diff --git a/test/unit/math/prim/fun/inv_cloglog_test.cpp b/test/unit/math/prim/fun/inv_cloglog_test.cpp index 67118b3b9e6..3f8c3fa6c7b 100644 --- a/test/unit/math/prim/fun/inv_cloglog_test.cpp +++ b/test/unit/math/prim/fun/inv_cloglog_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, inv_cloglog_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_cloglog(nan))); } + +TEST(MathFunctions, inv_cloglog_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_cloglog(b)); +} diff --git a/test/unit/math/prim/fun/inv_logit_test.cpp b/test/unit/math/prim/fun/inv_logit_test.cpp index b1e0de78766..a7e272f19b5 100644 --- a/test/unit/math/prim/fun/inv_logit_test.cpp +++ b/test/unit/math/prim/fun/inv_logit_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_logit(nan))); } + +TEST(MathFunctions, inv_logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_logit(b)); +} diff --git a/test/unit/math/prim/fun/inv_sqrt_test.cpp b/test/unit/math/prim/fun/inv_sqrt_test.cpp index 645957931a2..1a7deb94def 100644 --- a/test/unit/math/prim/fun/inv_sqrt_test.cpp +++ b/test/unit/math/prim/fun/inv_sqrt_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, inv_sqrt_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_sqrt(nan))); } + +TEST(MathFunctions, inv_sqrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_sqrt(b)); +} diff --git a/test/unit/math/prim/fun/inv_square_test.cpp b/test/unit/math/prim/fun/inv_square_test.cpp index 32c17c48cf2..a3cb3588a07 100644 --- a/test/unit/math/prim/fun/inv_square_test.cpp +++ b/test/unit/math/prim/fun/inv_square_test.cpp @@ -19,3 +19,11 @@ TEST(MathFunctions, inv_square_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_square(nan))); } + +TEST(MathFunctions, inv_square_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_square(b)); +} diff --git a/test/unit/math/prim/fun/inv_test.cpp b/test/unit/math/prim/fun/inv_test.cpp index f418be35db1..99f9d21a969 100644 --- a/test/unit/math/prim/fun/inv_test.cpp +++ b/test/unit/math/prim/fun/inv_test.cpp @@ -19,3 +19,11 @@ TEST(MathFunctions, inv_nan) { EXPECT_TRUE(std::isnan(stan::math::inv(nan))); } + +TEST(MathFunctions, inv_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv(b)); +} diff --git a/test/unit/math/prim/fun/lgamma_test.cpp b/test/unit/math/prim/fun/lgamma_test.cpp index d252006d5d5..77e52134c33 100644 --- a/test/unit/math/prim/fun/lgamma_test.cpp +++ b/test/unit/math/prim/fun/lgamma_test.cpp @@ -26,3 +26,11 @@ TEST(MathFunctions, lgamma_nan) { EXPECT_TRUE( std::isnormal(boost::math::lgamma(1.0E50, stan::math::boost_policy_t()))); } + +TEST(MathFunctions, lgamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::lgamma(b)); +} diff --git a/test/unit/math/prim/fun/log10_test.cpp b/test/unit/math/prim/fun/log10_test.cpp index 1a67f75a75c..4e3e79e94fc 100644 --- a/test/unit/math/prim/fun/log10_test.cpp +++ b/test/unit/math/prim/fun/log10_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, log10) { stan::test::expect_common_prim([](auto x) { return std::log10(x); }, [](auto x) { return stan::math::log10(x); }); } + +TEST(MathFunctions, log10_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log10(b)); +} diff --git a/test/unit/math/prim/fun/log1m_test.cpp b/test/unit/math/prim/fun/log1m_test.cpp index a0cf248f987..2c68c5a825e 100644 --- a/test/unit/math/prim/fun/log1m_test.cpp +++ b/test/unit/math/prim/fun/log1m_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, log1m_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1m(nan))); } + +TEST(MathFunctions, log1m_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::log1m(b)); +} diff --git a/test/unit/math/prim/fun/log1p_exp_test.cpp b/test/unit/math/prim/fun/log1p_exp_test.cpp index 6930db3a1eb..aa4b3fd0303 100644 --- a/test/unit/math/prim/fun/log1p_exp_test.cpp +++ b/test/unit/math/prim/fun/log1p_exp_test.cpp @@ -16,3 +16,11 @@ TEST(MathFunctions, log1p_exp_nan) { EXPECT_TRUE(std::isnan(stan::math::log1p_exp(nan))); } + +TEST(MathFunctions, log1p_exp_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log1p_exp(b)); +} diff --git a/test/unit/math/prim/fun/log1p_test.cpp b/test/unit/math/prim/fun/log1p_test.cpp index 35f90f46452..714958a1745 100644 --- a/test/unit/math/prim/fun/log1p_test.cpp +++ b/test/unit/math/prim/fun/log1p_test.cpp @@ -40,3 +40,11 @@ TEST(MathFunctions, log1p_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1p(nan))); } + +TEST(MathFunctions, log1p_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log1p(b)); +} diff --git a/test/unit/math/prim/fun/log2_test.cpp b/test/unit/math/prim/fun/log2_test.cpp index 031a50afa97..cf726f78ceb 100644 --- a/test/unit/math/prim/fun/log2_test.cpp +++ b/test/unit/math/prim/fun/log2_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, log2_nan) { EXPECT_TRUE(std::isnan(stan::math::log2(nan))); } + +TEST(MathFunctions, log2_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log2(b)); +} diff --git a/test/unit/math/prim/fun/log_inv_logit_test.cpp b/test/unit/math/prim/fun/log_inv_logit_test.cpp index 774d044a4dd..bb13d370b2a 100644 --- a/test/unit/math/prim/fun/log_inv_logit_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_test.cpp @@ -18,3 +18,11 @@ TEST(MathFunctions, log_inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::log_inv_logit(nan))); } + +TEST(MathFunctions, log_inv_logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log_inv_logit(b)); +} diff --git a/test/unit/math/prim/fun/log_softmax_test.cpp b/test/unit/math/prim/fun/log_softmax_test.cpp index ba26ba1ce20..eff97db3cd9 100644 --- a/test/unit/math/prim/fun/log_softmax_test.cpp +++ b/test/unit/math/prim/fun/log_softmax_test.cpp @@ -47,3 +47,11 @@ TEST(MathMatrixPrimMat, log_softmax_exception) { EXPECT_THROW(log_softmax(v0), std::invalid_argument); } + +TEST(MathFunctions, log_softmax_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log_softmax(b)); +} diff --git a/test/unit/math/prim/fun/log_test.cpp b/test/unit/math/prim/fun/log_test.cpp index 77563092631..f7163dbcf1a 100644 --- a/test/unit/math/prim/fun/log_test.cpp +++ b/test/unit/math/prim/fun/log_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, logInt) { EXPECT_FLOAT_EQ(std::log(3), log(3)); EXPECT_FLOAT_EQ(std::log(3.0), log(3.0)); } + +TEST(MathFunctions, log_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log(b)); +} diff --git a/test/unit/math/prim/fun/logit_test.cpp b/test/unit/math/prim/fun/logit_test.cpp index b6820aa4b7f..566cbcd88ad 100644 --- a/test/unit/math/prim/fun/logit_test.cpp +++ b/test/unit/math/prim/fun/logit_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, logit_nan) { EXPECT_TRUE(std::isnan(stan::math::logit(nan))); } + +TEST(MathFunctions, logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::logit(b)); +} diff --git a/test/unit/math/prim/fun/minus_test.cpp b/test/unit/math/prim/fun/minus_test.cpp index e9535ca4781..d3dd9bdad85 100644 --- a/test/unit/math/prim/fun/minus_test.cpp +++ b/test/unit/math/prim/fun/minus_test.cpp @@ -10,3 +10,11 @@ TEST(MathMatrixPrimMat, minus) { EXPECT_EQ(0, stan::math::minus(rv0).size()); EXPECT_EQ(0, stan::math::minus(m0).size()); } + +TEST(MathFunctions, minus_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::minus(b)); +} diff --git a/test/unit/math/prim/fun/round_test.cpp b/test/unit/math/prim/fun/round_test.cpp index 40cc1a8334f..3cabf152e67 100644 --- a/test/unit/math/prim/fun/round_test.cpp +++ b/test/unit/math/prim/fun/round_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, roundNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::round(nan))); } + +TEST(MathFunctions, round_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::round(b)); +} diff --git a/test/unit/math/prim/fun/sin_test.cpp b/test/unit/math/prim/fun/sin_test.cpp index 3a564bfea3b..4125f5e22f8 100644 --- a/test/unit/math/prim/fun/sin_test.cpp +++ b/test/unit/math/prim/fun/sin_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, sin) { stan::test::expect_common_prim([](auto x) { return std::sin(x); }, [](auto x) { return stan::math::sin(x); }); } + +TEST(MathFunctions, sin_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sin(b)); +} diff --git a/test/unit/math/prim/fun/sinh_test.cpp b/test/unit/math/prim/fun/sinh_test.cpp index 72b18d36f94..72e3f143e5d 100644 --- a/test/unit/math/prim/fun/sinh_test.cpp +++ b/test/unit/math/prim/fun/sinh_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, sinh) { stan::test::expect_common_prim([](auto x) { return std::sinh(x); }, [](auto x) { return stan::math::sinh(x); }); } + +TEST(MathFunctions, sinh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sinh(b)); +} diff --git a/test/unit/math/prim/fun/sqrt_test.cpp b/test/unit/math/prim/fun/sqrt_test.cpp index 43c362d0cc9..9896911dbf5 100644 --- a/test/unit/math/prim/fun/sqrt_test.cpp +++ b/test/unit/math/prim/fun/sqrt_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, sqrtInt) { EXPECT_FLOAT_EQ(std::sqrt(3.0), sqrt(3)); EXPECT_TRUE(stan::math::is_nan(sqrt(-2))); } + +TEST(MathFunctions, sqrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sqrt(b)); +} diff --git a/test/unit/math/prim/fun/square_test.cpp b/test/unit/math/prim/fun/square_test.cpp index aadd3009906..13b66189bcf 100644 --- a/test/unit/math/prim/fun/square_test.cpp +++ b/test/unit/math/prim/fun/square_test.cpp @@ -3,7 +3,7 @@ #include #include -TEST(MathsFunctions, square) { +TEST(MathFunctions, square) { double y = 2.0; EXPECT_FLOAT_EQ(y * y, stan::math::square(y)); @@ -19,3 +19,11 @@ TEST(MathFunctions, square_nan) { EXPECT_TRUE(std::isnan(stan::math::square(nan))); } + +TEST(MathFunctions, square_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::square(b)); +} diff --git a/test/unit/math/prim/fun/tan_test.cpp b/test/unit/math/prim/fun/tan_test.cpp new file mode 100644 index 00000000000..5b9e861cabc --- /dev/null +++ b/test/unit/math/prim/fun/tan_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +TEST(MathFunctions, tan_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tan(b)); +} diff --git a/test/unit/math/prim/fun/tanh_test.cpp b/test/unit/math/prim/fun/tanh_test.cpp index bd4da5755c9..594d0e37ed5 100644 --- a/test/unit/math/prim/fun/tanh_test.cpp +++ b/test/unit/math/prim/fun/tanh_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, tanh) { stan::test::expect_common_prim([](auto x) { return std::tanh(x); }, [](auto x) { return stan::math::tanh(x); }); } + +TEST(MathFunctions, tanh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tanh(b)); +} diff --git a/test/unit/math/prim/fun/tgamma_test.cpp b/test/unit/math/prim/fun/tgamma_test.cpp index 51e5b84261c..2ecb0bd51cc 100644 --- a/test/unit/math/prim/fun/tgamma_test.cpp +++ b/test/unit/math/prim/fun/tgamma_test.cpp @@ -20,3 +20,11 @@ TEST(MathFunctions, tgamma_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::tgamma(nan))); } + +TEST(MathFunctions, tgamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tgamma(b)); +} diff --git a/test/unit/math/prim/fun/trigamma_test.cpp b/test/unit/math/prim/fun/trigamma_test.cpp index d2066598d32..c2d4484d1f0 100644 --- a/test/unit/math/prim/fun/trigamma_test.cpp +++ b/test/unit/math/prim/fun/trigamma_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, trigamma_nan) { EXPECT_TRUE(std::isnan(stan::math::trigamma(nan))); } + +TEST(MathFunctions, trigamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::trigamma(b)); +} diff --git a/test/unit/math/prim/fun/trunc_test.cpp b/test/unit/math/prim/fun/trunc_test.cpp index 8aa3a7c6a00..2f64da18b03 100644 --- a/test/unit/math/prim/fun/trunc_test.cpp +++ b/test/unit/math/prim/fun/trunc_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, truncNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::trunc(nan))); } + +TEST(MathFunctions, trunc_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::trunc(b)); +} From 3f8fae424ff1e1252f4aed2080d4c2d7b86e24c3 Mon Sep 17 00:00:00 2001 From: Nicusor Serban Date: Wed, 29 Jan 2020 22:31:53 +0200 Subject: [PATCH 004/167] Updated math version --- .github/ISSUE_TEMPLATE.md | 2 +- doxygen/contributor_help_pages/windows_devnotes.md | 6 +++--- doxygen/doxygen.cfg | 2 +- stan/math/version.hpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 0e35de62263..8f112ab89d3 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -25,4 +25,4 @@ If this is a **feature request**, show what you expect to happen if the feature #### Current Version: -v3.1.0 +v3.1.1 diff --git a/doxygen/contributor_help_pages/windows_devnotes.md b/doxygen/contributor_help_pages/windows_devnotes.md index 1082a514b7e..2c8e7137315 100644 --- a/doxygen/contributor_help_pages/windows_devnotes.md +++ b/doxygen/contributor_help_pages/windows_devnotes.md @@ -51,7 +51,7 @@ BIT=32 ``` If the test suite isn't built first, client code using numerical integration routines such as `integrate_ode_bdf` may fail to link because the libraries haven't been built yet. Obviously these files and directories would need to be added to the downstream project makefile's LDLIBS variable, or equivalent. ``` -$ make lib/sundials_3.1.0/lib/libsundials_cvodes.a lib/sundials_3.1.0/lib/libsundials_nvecserial.a -make: 'lib/sundials_3.1.0/lib/libsundials_cvodes.a' is up to date. -make: 'lib/sundials_3.1.0/lib/libsundials_nvecserial.a' is up to date. +$ make lib/sundials_3.1.1/lib/libsundials_cvodes.a lib/sundials_3.1.1/lib/libsundials_nvecserial.a +make: 'lib/sundials_3.1.1/lib/libsundials_cvodes.a' is up to date. +make: 'lib/sundials_3.1.1/lib/libsundials_nvecserial.a' is up to date. ``` diff --git a/doxygen/doxygen.cfg b/doxygen/doxygen.cfg index 8fee6f17d59..e4b5060f7d0 100644 --- a/doxygen/doxygen.cfg +++ b/doxygen/doxygen.cfg @@ -38,7 +38,7 @@ PROJECT_NAME = "Stan Math Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 3.1.0 +PROJECT_NUMBER = 3.1.1 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/stan/math/version.hpp b/stan/math/version.hpp index bf8599edc07..d5ba5648855 100644 --- a/stan/math/version.hpp +++ b/stan/math/version.hpp @@ -13,7 +13,7 @@ #define STAN_MATH_MAJOR 3 #define STAN_MATH_MINOR 1 -#define STAN_MATH_PATCH 0 +#define STAN_MATH_PATCH 1 namespace stan { namespace math { From 067add1de9793d0647c17c7f65fcd5b0ad18ed8a Mon Sep 17 00:00:00 2001 From: Nicusor Serban Date: Wed, 29 Jan 2020 22:49:18 +0200 Subject: [PATCH 005/167] Reverted release of 3.1.1 --- .github/ISSUE_TEMPLATE.md | 2 +- doxygen/contributor_help_pages/windows_devnotes.md | 6 +++--- doxygen/doxygen.cfg | 2 +- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 4 ++-- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 4 ++-- stan/math/prim/fun/inv_cloglog.hpp | 4 ++-- stan/math/prim/fun/inv_sqrt.hpp | 4 ++-- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log_softmax.hpp | 11 +++++------ stan/math/prim/fun/minus.hpp | 4 +--- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 4 ++-- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- stan/math/prim/vectorize/apply_scalar_unary.hpp | 7 ++----- stan/math/version.hpp | 2 +- test/unit/math/prim/fun/Phi_approx_test.cpp | 8 -------- test/unit/math/prim/fun/Phi_test.cpp | 8 -------- test/unit/math/prim/fun/acos_test.cpp | 8 -------- test/unit/math/prim/fun/acosh_test.cpp | 8 -------- test/unit/math/prim/fun/asin_test.cpp | 8 -------- test/unit/math/prim/fun/asinh_test.cpp | 8 -------- test/unit/math/prim/fun/atan_test.cpp | 8 -------- test/unit/math/prim/fun/atanh_test.cpp | 8 -------- test/unit/math/prim/fun/cbrt_test.cpp | 8 -------- test/unit/math/prim/fun/ceil_test.cpp | 8 -------- test/unit/math/prim/fun/cos_test.cpp | 8 -------- test/unit/math/prim/fun/cosh_test.cpp | 10 ---------- test/unit/math/prim/fun/digamma_test.cpp | 8 -------- test/unit/math/prim/fun/erf_test.cpp | 8 -------- test/unit/math/prim/fun/erfc_test.cpp | 8 -------- test/unit/math/prim/fun/exp2_test.cpp | 8 -------- test/unit/math/prim/fun/exp_test.cpp | 8 -------- test/unit/math/prim/fun/expm1_test.cpp | 8 -------- test/unit/math/prim/fun/fabs_test.cpp | 8 -------- test/unit/math/prim/fun/floor_test.cpp | 8 -------- test/unit/math/prim/fun/inv_Phi_test.cpp | 8 -------- test/unit/math/prim/fun/inv_cloglog_test.cpp | 8 -------- test/unit/math/prim/fun/inv_logit_test.cpp | 8 -------- test/unit/math/prim/fun/inv_sqrt_test.cpp | 8 -------- test/unit/math/prim/fun/inv_square_test.cpp | 8 -------- test/unit/math/prim/fun/inv_test.cpp | 8 -------- test/unit/math/prim/fun/lgamma_test.cpp | 8 -------- test/unit/math/prim/fun/log10_test.cpp | 8 -------- test/unit/math/prim/fun/log1m_test.cpp | 8 -------- test/unit/math/prim/fun/log1p_exp_test.cpp | 8 -------- test/unit/math/prim/fun/log1p_test.cpp | 8 -------- test/unit/math/prim/fun/log2_test.cpp | 8 -------- test/unit/math/prim/fun/log_inv_logit_test.cpp | 8 -------- test/unit/math/prim/fun/log_softmax_test.cpp | 8 -------- test/unit/math/prim/fun/log_test.cpp | 8 -------- test/unit/math/prim/fun/logit_test.cpp | 8 -------- test/unit/math/prim/fun/minus_test.cpp | 8 -------- test/unit/math/prim/fun/round_test.cpp | 8 -------- test/unit/math/prim/fun/sin_test.cpp | 8 -------- test/unit/math/prim/fun/sinh_test.cpp | 8 -------- test/unit/math/prim/fun/sqrt_test.cpp | 8 -------- test/unit/math/prim/fun/square_test.cpp | 10 +--------- test/unit/math/prim/fun/tan_test.cpp | 10 ---------- test/unit/math/prim/fun/tanh_test.cpp | 8 -------- test/unit/math/prim/fun/tgamma_test.cpp | 8 -------- test/unit/math/prim/fun/trigamma_test.cpp | 8 -------- test/unit/math/prim/fun/trunc_test.cpp | 8 -------- 74 files changed, 40 insertions(+), 426 deletions(-) delete mode 100644 test/unit/math/prim/fun/cosh_test.cpp delete mode 100644 test/unit/math/prim/fun/tan_test.cpp diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 8f112ab89d3..0e35de62263 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -25,4 +25,4 @@ If this is a **feature request**, show what you expect to happen if the feature #### Current Version: -v3.1.1 +v3.1.0 diff --git a/doxygen/contributor_help_pages/windows_devnotes.md b/doxygen/contributor_help_pages/windows_devnotes.md index 2c8e7137315..1082a514b7e 100644 --- a/doxygen/contributor_help_pages/windows_devnotes.md +++ b/doxygen/contributor_help_pages/windows_devnotes.md @@ -51,7 +51,7 @@ BIT=32 ``` If the test suite isn't built first, client code using numerical integration routines such as `integrate_ode_bdf` may fail to link because the libraries haven't been built yet. Obviously these files and directories would need to be added to the downstream project makefile's LDLIBS variable, or equivalent. ``` -$ make lib/sundials_3.1.1/lib/libsundials_cvodes.a lib/sundials_3.1.1/lib/libsundials_nvecserial.a -make: 'lib/sundials_3.1.1/lib/libsundials_cvodes.a' is up to date. -make: 'lib/sundials_3.1.1/lib/libsundials_nvecserial.a' is up to date. +$ make lib/sundials_3.1.0/lib/libsundials_cvodes.a lib/sundials_3.1.0/lib/libsundials_nvecserial.a +make: 'lib/sundials_3.1.0/lib/libsundials_cvodes.a' is up to date. +make: 'lib/sundials_3.1.0/lib/libsundials_nvecserial.a' is up to date. ``` diff --git a/doxygen/doxygen.cfg b/doxygen/doxygen.cfg index e4b5060f7d0..8fee6f17d59 100644 --- a/doxygen/doxygen.cfg +++ b/doxygen/doxygen.cfg @@ -38,7 +38,7 @@ PROJECT_NAME = "Stan Math Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 3.1.1 +PROJECT_NUMBER = 3.1.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index e5960f1b758..709864e1db6 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -45,7 +45,7 @@ inline auto acos(const T& x) { template > inline auto acos(const Eigen::MatrixBase& x) { - return x.derived().array().acos().matrix().eval(); + return x.derived().array().acos().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 0f5d172a29b..81a22dcf60b 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -45,7 +45,7 @@ inline auto asin(const T& x) { template > inline auto asin(const Eigen::MatrixBase& x) { - return x.derived().array().asin().matrix().eval(); + return x.derived().array().asin().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 9576c99240a..28a05b08d6a 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t atan(const T& x) { template > inline auto atan(const Eigen::MatrixBase& x) { - return x.derived().array().atan().matrix().eval(); + return x.derived().array().atan().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 2de0ddb72e7..7a78566ac1d 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -45,7 +45,7 @@ inline auto ceil(const T& x) { template > inline auto ceil(const Eigen::MatrixBase& x) { - return x.derived().array().ceil().matrix().eval(); + return x.derived().array().ceil().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index bf9761ace9f..0c20e53f935 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -45,7 +45,7 @@ inline auto cos(const T& x) { template > inline auto cos(const Eigen::MatrixBase& x) { - return x.derived().array().cos().matrix().eval(); + return x.derived().array().cos().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 2a0402fe9c2..3dee3eae2a3 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t cosh(const T& x) { template > inline auto cosh(const Eigen::MatrixBase& x) { - return x.derived().array().cosh().matrix().eval(); + return x.derived().array().cosh().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 54486d9e394..4d60b4c04b5 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -60,7 +60,7 @@ inline auto exp(const T& x) { template > inline auto exp(const Eigen::MatrixBase& x) { - return x.derived().array().exp().matrix().eval(); + return x.derived().array().exp().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 2bfc97071d2..1dc51b0ee3c 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t fabs(const T& x) { template > inline auto fabs(const Eigen::MatrixBase& x) { - return x.derived().array().abs().matrix().eval(); + return x.derived().array().abs().matrix(); } /** @@ -57,7 +57,7 @@ inline auto fabs(const Eigen::MatrixBase& x) { template > inline auto fabs(const Eigen::ArrayBase& x) { - return x.derived().abs().eval(); + return x.derived().abs(); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index c53433cf356..7f1f37317cd 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -45,7 +45,7 @@ inline auto floor(const T& x) { template > inline auto floor(const Eigen::MatrixBase& x) { - return x.derived().array().floor().matrix().eval(); + return x.derived().array().floor().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 439dc962778..f50f17f784c 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -45,7 +45,7 @@ inline auto inv(const T& x) { template > inline auto inv(const Eigen::MatrixBase& x) { - return x.derived().array().inverse().matrix().eval(); + return x.derived().array().inverse().matrix(); } /** @@ -57,7 +57,7 @@ inline auto inv(const Eigen::MatrixBase& x) { template > inline auto inv(const Eigen::ArrayBase& x) { - return x.derived().inverse().eval(); + return x.derived().inverse(); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 44c6dfdc8d7..945417aa0f9 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -87,7 +87,7 @@ inline auto inv_cloglog(const T& x) { template > inline auto inv_cloglog(const Eigen::MatrixBase& x) { - return (1 - exp(-exp(x.derived().array()))).matrix().eval(); + return (1 - exp(-exp(x.derived().array()))).matrix(); } /** @@ -99,7 +99,7 @@ inline auto inv_cloglog(const Eigen::MatrixBase& x) { template > inline auto inv_cloglog(const Eigen::ArrayBase& x) { - return (1 - exp(-exp(x.derived()))).eval(); + return 1 - exp(-exp(x.derived())); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index 66b41ee7138..aa6e0d7ffdf 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -50,7 +50,7 @@ inline auto inv_sqrt(const T& x) { template > inline auto inv_sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().rsqrt().matrix().eval(); + return x.derived().array().rsqrt().matrix(); } /** @@ -62,7 +62,7 @@ inline auto inv_sqrt(const Eigen::MatrixBase& x) { template > inline auto inv_sqrt(const Eigen::ArrayBase& x) { - return x.derived().rsqrt().eval(); + return x.derived().rsqrt(); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 3814d0a5ae5..2e32bc5aa4b 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -60,7 +60,7 @@ inline auto log(const T& x) { template > inline auto log(const Eigen::MatrixBase& x) { - return x.derived().array().log().matrix().eval(); + return x.derived().array().log().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 10b7b0fa1b0..808bdb85bad 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -45,7 +45,7 @@ inline auto log10(const T& x) { template > inline auto log10(const Eigen::MatrixBase& x) { - return x.derived().array().log10().matrix().eval(); + return x.derived().array().log10().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index 8c4951df779..a51370d0b1a 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -36,12 +36,11 @@ namespace math { * @param[in] v Vector to transform. * @return Unit simplex result of the softmax transform of the vector. */ -template >>...> -inline auto log_softmax(const T& x) { - return apply_vector_unary::apply(x, [&](const auto& v) { - check_nonzero_size("log_softmax", "v", v); - return (v.array() - log_sum_exp(v)).matrix().eval(); - }); +template +inline Eigen::Matrix log_softmax( + const Eigen::Matrix& v) { + check_nonzero_size("log_softmax", "v", v); + return v.array() - log_sum_exp(v); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 13fe7d090ed..7f374462fc0 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -1,8 +1,6 @@ #ifndef STAN_MATH_PRIM_FUN_MINUS_HPP #define STAN_MATH_PRIM_FUN_MINUS_HPP -#include - namespace stan { namespace math { @@ -14,7 +12,7 @@ namespace math { * @return Negation of subtrahend. */ template -inline plain_type_t minus(const T& x) { +inline auto minus(const T& x) { return -x; } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index e5cc48efd18..66b2d6a428c 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -62,7 +62,7 @@ inline auto round(const T& x) { template > inline auto round(const Eigen::MatrixBase& x) { - return x.derived().array().round().matrix().eval(); + return x.derived().array().round().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index 0b783ffeb20..f2ed9102f97 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -45,7 +45,7 @@ inline auto sin(const T& x) { template > inline auto sin(const Eigen::MatrixBase& x) { - return x.derived().array().sin().matrix().eval(); + return x.derived().array().sin().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index e4d1252295d..5e13a03993e 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -45,7 +45,7 @@ inline auto sinh(const T& x) { template > inline auto sinh(const Eigen::MatrixBase& x) { - return x.derived().array().sinh().matrix().eval(); + return x.derived().array().sinh().matrix(); } /** @@ -57,7 +57,7 @@ inline auto sinh(const Eigen::MatrixBase& x) { template > inline auto sinh(const Eigen::ArrayBase& x) { - return x.derived().sinh().eval(); + return x.derived().sinh(); } } // namespace math diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index fe81fcf75b6..ae1b6dace93 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -54,7 +54,7 @@ inline auto sqrt(const T& x) { template > inline auto sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().sqrt().matrix().eval(); + return x.derived().array().sqrt().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index efc03beb94b..7064300e695 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -45,7 +45,7 @@ inline auto tan(const T& x) { template > inline auto tan(const Eigen::MatrixBase& x) { - return x.derived().array().tan().matrix().eval(); + return x.derived().array().tan().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 2fe9f4c5a25..60c4d405fb2 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -45,7 +45,7 @@ inline auto tanh(const T& x) { template > inline auto tanh(const Eigen::MatrixBase& x) { - return x.derived().array().tanh().matrix().eval(); + return x.derived().array().tanh().matrix(); } } // namespace math diff --git a/stan/math/prim/vectorize/apply_scalar_unary.hpp b/stan/math/prim/vectorize/apply_scalar_unary.hpp index 8cc283cd4ac..ed31037a207 100644 --- a/stan/math/prim/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/vectorize/apply_scalar_unary.hpp @@ -59,11 +59,8 @@ struct apply_scalar_unary> { * by F to the specified matrix. */ static inline auto apply(const T& x) { - return x - .unaryExpr([](scalar_t x) { - return apply_scalar_unary::apply(x); - }) - .eval(); + return x.unaryExpr( + [](scalar_t x) { return apply_scalar_unary::apply(x); }); } /** diff --git a/stan/math/version.hpp b/stan/math/version.hpp index d5ba5648855..bf8599edc07 100644 --- a/stan/math/version.hpp +++ b/stan/math/version.hpp @@ -13,7 +13,7 @@ #define STAN_MATH_MAJOR 3 #define STAN_MATH_MINOR 1 -#define STAN_MATH_PATCH 1 +#define STAN_MATH_PATCH 0 namespace stan { namespace math { diff --git a/test/unit/math/prim/fun/Phi_approx_test.cpp b/test/unit/math/prim/fun/Phi_approx_test.cpp index 4a29923c572..dbc4078352d 100644 --- a/test/unit/math/prim/fun/Phi_approx_test.cpp +++ b/test/unit/math/prim/fun/Phi_approx_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, Phi_approx_nan) { EXPECT_TRUE(std::isnan(stan::math::Phi_approx(nan))); } - -TEST(MathFunctions, Phi_approx__works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::Phi_approx(b)); -} diff --git a/test/unit/math/prim/fun/Phi_test.cpp b/test/unit/math/prim/fun/Phi_test.cpp index 1273ad24628..c43e260bf88 100644 --- a/test/unit/math/prim/fun/Phi_test.cpp +++ b/test/unit/math/prim/fun/Phi_test.cpp @@ -118,11 +118,3 @@ TEST(MathFunctions, Phi_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_THROW(stan::math::Phi(nan), std::domain_error); } - -TEST(MathFunctions, Phi_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::Phi(b)); -} diff --git a/test/unit/math/prim/fun/acos_test.cpp b/test/unit/math/prim/fun/acos_test.cpp index 7570f52cc71..c66c4c49a02 100644 --- a/test/unit/math/prim/fun/acos_test.cpp +++ b/test/unit/math/prim/fun/acos_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, acos) { stan::test::expect_common_prim([](auto x) { return std::acos(x); }, [](auto x) { return stan::math::acos(x); }); } - -TEST(MathFunctions, acos_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::acos(b)); -} diff --git a/test/unit/math/prim/fun/acosh_test.cpp b/test/unit/math/prim/fun/acosh_test.cpp index 1f9e8ab7bb5..2d5bc742d3c 100644 --- a/test/unit/math/prim/fun/acosh_test.cpp +++ b/test/unit/math/prim/fun/acosh_test.cpp @@ -26,11 +26,3 @@ TEST(MathFunctions, acosh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::acosh(nan))); } - -TEST(MathFunctions, acosh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::acosh(b)); -} diff --git a/test/unit/math/prim/fun/asin_test.cpp b/test/unit/math/prim/fun/asin_test.cpp index 858be5fbbbd..a14245b58c2 100644 --- a/test/unit/math/prim/fun/asin_test.cpp +++ b/test/unit/math/prim/fun/asin_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, asin) { stan::test::expect_common_prim([](auto x) { return std::asin(x); }, [](auto x) { return stan::math::asin(x); }); } - -TEST(MathFunctions, asin_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::asin(b)); -} diff --git a/test/unit/math/prim/fun/asinh_test.cpp b/test/unit/math/prim/fun/asinh_test.cpp index e10228b0af0..151afc2b3ef 100644 --- a/test/unit/math/prim/fun/asinh_test.cpp +++ b/test/unit/math/prim/fun/asinh_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, asinh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::asinh(nan))); } - -TEST(MathFunctions, asinh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::asinh(b)); -} diff --git a/test/unit/math/prim/fun/atan_test.cpp b/test/unit/math/prim/fun/atan_test.cpp index 54861be5151..467ceefff7d 100644 --- a/test/unit/math/prim/fun/atan_test.cpp +++ b/test/unit/math/prim/fun/atan_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, atan) { stan::test::expect_common_prim([](auto x) { return std::atan(x); }, [](auto x) { return stan::math::atan(x); }); } - -TEST(MathFunctions, atan_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::atan(b)); -} diff --git a/test/unit/math/prim/fun/atanh_test.cpp b/test/unit/math/prim/fun/atanh_test.cpp index 18ae151e23c..ca60e585e86 100644 --- a/test/unit/math/prim/fun/atanh_test.cpp +++ b/test/unit/math/prim/fun/atanh_test.cpp @@ -27,11 +27,3 @@ TEST(MathFunctions, atanh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::atanh(nan))); } - -TEST(MathFunctions, atanh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::atanh(b)); -} diff --git a/test/unit/math/prim/fun/cbrt_test.cpp b/test/unit/math/prim/fun/cbrt_test.cpp index 854853943a6..2ca66bbde53 100644 --- a/test/unit/math/prim/fun/cbrt_test.cpp +++ b/test/unit/math/prim/fun/cbrt_test.cpp @@ -24,11 +24,3 @@ TEST(MathFunctions, cbrt_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::cbrt(nan))); } - -TEST(MathFunctions, cbrt_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::cbrt(b)); -} diff --git a/test/unit/math/prim/fun/ceil_test.cpp b/test/unit/math/prim/fun/ceil_test.cpp index 57a7dce8c41..f26f0068eb1 100644 --- a/test/unit/math/prim/fun/ceil_test.cpp +++ b/test/unit/math/prim/fun/ceil_test.cpp @@ -4,11 +4,3 @@ TEST(primScalFun, ceil) { stan::test::expect_common_prim([](auto x) { return std::ceil(x); }, [](auto x) { return stan::math::ceil(x); }); } - -TEST(MathFunctions, ceil_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::ceil(b)); -} diff --git a/test/unit/math/prim/fun/cos_test.cpp b/test/unit/math/prim/fun/cos_test.cpp index 94989f9703f..53daaa398de 100644 --- a/test/unit/math/prim/fun/cos_test.cpp +++ b/test/unit/math/prim/fun/cos_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, cos) { stan::test::expect_common_prim([](auto x) { return std::cos(x); }, [](auto x) { return stan::math::cos(x); }); } - -TEST(MathFunctions, cos_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::cos(b)); -} diff --git a/test/unit/math/prim/fun/cosh_test.cpp b/test/unit/math/prim/fun/cosh_test.cpp deleted file mode 100644 index d1433637b36..00000000000 --- a/test/unit/math/prim/fun/cosh_test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -TEST(MathFunctions, cosh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::cosh(b)); -} diff --git a/test/unit/math/prim/fun/digamma_test.cpp b/test/unit/math/prim/fun/digamma_test.cpp index b0d37f98b7e..2a3a7ea150b 100644 --- a/test/unit/math/prim/fun/digamma_test.cpp +++ b/test/unit/math/prim/fun/digamma_test.cpp @@ -18,11 +18,3 @@ TEST(MathFunctions, digamma_nan) { EXPECT_TRUE(std::isnormal(stan::math::digamma(1.0E50))); } - -TEST(MathFunctions, digamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::digamma(b)); -} diff --git a/test/unit/math/prim/fun/erf_test.cpp b/test/unit/math/prim/fun/erf_test.cpp index 0c06057587c..f01b192b8be 100644 --- a/test/unit/math/prim/fun/erf_test.cpp +++ b/test/unit/math/prim/fun/erf_test.cpp @@ -20,11 +20,3 @@ TEST(MathFunctions, erfNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erf(nan))); } - -TEST(MathFunctions, erf_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::erf(b)); -} diff --git a/test/unit/math/prim/fun/erfc_test.cpp b/test/unit/math/prim/fun/erfc_test.cpp index 2e19d0fe333..aad40784135 100644 --- a/test/unit/math/prim/fun/erfc_test.cpp +++ b/test/unit/math/prim/fun/erfc_test.cpp @@ -21,11 +21,3 @@ TEST(MathFunctions, erfcNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erfc(nan))); } - -TEST(MathFunctions, erfc_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::erfc(b)); -} diff --git a/test/unit/math/prim/fun/exp2_test.cpp b/test/unit/math/prim/fun/exp2_test.cpp index b5c8f356425..ae1c93c1e22 100644 --- a/test/unit/math/prim/fun/exp2_test.cpp +++ b/test/unit/math/prim/fun/exp2_test.cpp @@ -35,11 +35,3 @@ TEST(MathFunctions, exp2_nan) { EXPECT_TRUE(std::isnan(stan::math::exp2(nan))); } - -TEST(MathFunctions, exp2_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::exp2(b)); -} diff --git a/test/unit/math/prim/fun/exp_test.cpp b/test/unit/math/prim/fun/exp_test.cpp index 292f72baf10..a19d8643a9f 100644 --- a/test/unit/math/prim/fun/exp_test.cpp +++ b/test/unit/math/prim/fun/exp_test.cpp @@ -7,11 +7,3 @@ TEST(MathFunctions, expInt) { EXPECT_FLOAT_EQ(std::exp(3), exp(3)); EXPECT_FLOAT_EQ(std::exp(3.0), exp(3.0)); } - -TEST(MathFunctions, exp_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::exp(b)); -} diff --git a/test/unit/math/prim/fun/expm1_test.cpp b/test/unit/math/prim/fun/expm1_test.cpp index a92bbc91f45..3e35d9873c1 100644 --- a/test/unit/math/prim/fun/expm1_test.cpp +++ b/test/unit/math/prim/fun/expm1_test.cpp @@ -23,11 +23,3 @@ TEST(MathFunctions, expm1_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::expm1(nan))); } - -TEST(MathFunctions, expm1_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::expm1(b)); -} diff --git a/test/unit/math/prim/fun/fabs_test.cpp b/test/unit/math/prim/fun/fabs_test.cpp index e4450169d0a..985f1426eda 100644 --- a/test/unit/math/prim/fun/fabs_test.cpp +++ b/test/unit/math/prim/fun/fabs_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, fabs) { stan::test::expect_common_prim([](auto x) { return std::fabs(x); }, [](auto x) { return stan::math::fabs(x); }); } - -TEST(MathFunctions, fabs_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::fabs(b)); -} diff --git a/test/unit/math/prim/fun/floor_test.cpp b/test/unit/math/prim/fun/floor_test.cpp index 90c6dca6090..6c283c47dbc 100644 --- a/test/unit/math/prim/fun/floor_test.cpp +++ b/test/unit/math/prim/fun/floor_test.cpp @@ -4,11 +4,3 @@ TEST(primScalFun, floor) { stan::test::expect_common_prim([](auto x) { return std::floor(x); }, [](auto x) { return stan::math::floor(x); }); } - -TEST(MathFunctions, floor_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::floor(b)); -} diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index 9538f7151d6..13d81adbb7f 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -34,11 +34,3 @@ TEST(MathFunctions, inv_Phi_nan) { EXPECT_THROW(inv_Phi(-2.0), std::domain_error); EXPECT_THROW(inv_Phi(2.0), std::domain_error); } - -TEST(MathFunctions, inv_Phi_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::inv_Phi(b)); -} diff --git a/test/unit/math/prim/fun/inv_cloglog_test.cpp b/test/unit/math/prim/fun/inv_cloglog_test.cpp index 3f8c3fa6c7b..67118b3b9e6 100644 --- a/test/unit/math/prim/fun/inv_cloglog_test.cpp +++ b/test/unit/math/prim/fun/inv_cloglog_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, inv_cloglog_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_cloglog(nan))); } - -TEST(MathFunctions, inv_cloglog_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_cloglog(b)); -} diff --git a/test/unit/math/prim/fun/inv_logit_test.cpp b/test/unit/math/prim/fun/inv_logit_test.cpp index a7e272f19b5..b1e0de78766 100644 --- a/test/unit/math/prim/fun/inv_logit_test.cpp +++ b/test/unit/math/prim/fun/inv_logit_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_logit(nan))); } - -TEST(MathFunctions, inv_logit_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_logit(b)); -} diff --git a/test/unit/math/prim/fun/inv_sqrt_test.cpp b/test/unit/math/prim/fun/inv_sqrt_test.cpp index 1a7deb94def..645957931a2 100644 --- a/test/unit/math/prim/fun/inv_sqrt_test.cpp +++ b/test/unit/math/prim/fun/inv_sqrt_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, inv_sqrt_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_sqrt(nan))); } - -TEST(MathFunctions, inv_sqrt_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_sqrt(b)); -} diff --git a/test/unit/math/prim/fun/inv_square_test.cpp b/test/unit/math/prim/fun/inv_square_test.cpp index a3cb3588a07..32c17c48cf2 100644 --- a/test/unit/math/prim/fun/inv_square_test.cpp +++ b/test/unit/math/prim/fun/inv_square_test.cpp @@ -19,11 +19,3 @@ TEST(MathFunctions, inv_square_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_square(nan))); } - -TEST(MathFunctions, inv_square_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_square(b)); -} diff --git a/test/unit/math/prim/fun/inv_test.cpp b/test/unit/math/prim/fun/inv_test.cpp index 99f9d21a969..f418be35db1 100644 --- a/test/unit/math/prim/fun/inv_test.cpp +++ b/test/unit/math/prim/fun/inv_test.cpp @@ -19,11 +19,3 @@ TEST(MathFunctions, inv_nan) { EXPECT_TRUE(std::isnan(stan::math::inv(nan))); } - -TEST(MathFunctions, inv_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv(b)); -} diff --git a/test/unit/math/prim/fun/lgamma_test.cpp b/test/unit/math/prim/fun/lgamma_test.cpp index 77e52134c33..d252006d5d5 100644 --- a/test/unit/math/prim/fun/lgamma_test.cpp +++ b/test/unit/math/prim/fun/lgamma_test.cpp @@ -26,11 +26,3 @@ TEST(MathFunctions, lgamma_nan) { EXPECT_TRUE( std::isnormal(boost::math::lgamma(1.0E50, stan::math::boost_policy_t()))); } - -TEST(MathFunctions, lgamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::lgamma(b)); -} diff --git a/test/unit/math/prim/fun/log10_test.cpp b/test/unit/math/prim/fun/log10_test.cpp index 4e3e79e94fc..1a67f75a75c 100644 --- a/test/unit/math/prim/fun/log10_test.cpp +++ b/test/unit/math/prim/fun/log10_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, log10) { stan::test::expect_common_prim([](auto x) { return std::log10(x); }, [](auto x) { return stan::math::log10(x); }); } - -TEST(MathFunctions, log10_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log10(b)); -} diff --git a/test/unit/math/prim/fun/log1m_test.cpp b/test/unit/math/prim/fun/log1m_test.cpp index 2c68c5a825e..a0cf248f987 100644 --- a/test/unit/math/prim/fun/log1m_test.cpp +++ b/test/unit/math/prim/fun/log1m_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, log1m_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1m(nan))); } - -TEST(MathFunctions, log1m_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::log1m(b)); -} diff --git a/test/unit/math/prim/fun/log1p_exp_test.cpp b/test/unit/math/prim/fun/log1p_exp_test.cpp index aa4b3fd0303..6930db3a1eb 100644 --- a/test/unit/math/prim/fun/log1p_exp_test.cpp +++ b/test/unit/math/prim/fun/log1p_exp_test.cpp @@ -16,11 +16,3 @@ TEST(MathFunctions, log1p_exp_nan) { EXPECT_TRUE(std::isnan(stan::math::log1p_exp(nan))); } - -TEST(MathFunctions, log1p_exp_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log1p_exp(b)); -} diff --git a/test/unit/math/prim/fun/log1p_test.cpp b/test/unit/math/prim/fun/log1p_test.cpp index 714958a1745..35f90f46452 100644 --- a/test/unit/math/prim/fun/log1p_test.cpp +++ b/test/unit/math/prim/fun/log1p_test.cpp @@ -40,11 +40,3 @@ TEST(MathFunctions, log1p_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1p(nan))); } - -TEST(MathFunctions, log1p_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log1p(b)); -} diff --git a/test/unit/math/prim/fun/log2_test.cpp b/test/unit/math/prim/fun/log2_test.cpp index cf726f78ceb..031a50afa97 100644 --- a/test/unit/math/prim/fun/log2_test.cpp +++ b/test/unit/math/prim/fun/log2_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, log2_nan) { EXPECT_TRUE(std::isnan(stan::math::log2(nan))); } - -TEST(MathFunctions, log2_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log2(b)); -} diff --git a/test/unit/math/prim/fun/log_inv_logit_test.cpp b/test/unit/math/prim/fun/log_inv_logit_test.cpp index bb13d370b2a..774d044a4dd 100644 --- a/test/unit/math/prim/fun/log_inv_logit_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_test.cpp @@ -18,11 +18,3 @@ TEST(MathFunctions, log_inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::log_inv_logit(nan))); } - -TEST(MathFunctions, log_inv_logit_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log_inv_logit(b)); -} diff --git a/test/unit/math/prim/fun/log_softmax_test.cpp b/test/unit/math/prim/fun/log_softmax_test.cpp index eff97db3cd9..ba26ba1ce20 100644 --- a/test/unit/math/prim/fun/log_softmax_test.cpp +++ b/test/unit/math/prim/fun/log_softmax_test.cpp @@ -47,11 +47,3 @@ TEST(MathMatrixPrimMat, log_softmax_exception) { EXPECT_THROW(log_softmax(v0), std::invalid_argument); } - -TEST(MathFunctions, log_softmax_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log_softmax(b)); -} diff --git a/test/unit/math/prim/fun/log_test.cpp b/test/unit/math/prim/fun/log_test.cpp index f7163dbcf1a..77563092631 100644 --- a/test/unit/math/prim/fun/log_test.cpp +++ b/test/unit/math/prim/fun/log_test.cpp @@ -7,11 +7,3 @@ TEST(MathFunctions, logInt) { EXPECT_FLOAT_EQ(std::log(3), log(3)); EXPECT_FLOAT_EQ(std::log(3.0), log(3.0)); } - -TEST(MathFunctions, log_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log(b)); -} diff --git a/test/unit/math/prim/fun/logit_test.cpp b/test/unit/math/prim/fun/logit_test.cpp index 566cbcd88ad..b6820aa4b7f 100644 --- a/test/unit/math/prim/fun/logit_test.cpp +++ b/test/unit/math/prim/fun/logit_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, logit_nan) { EXPECT_TRUE(std::isnan(stan::math::logit(nan))); } - -TEST(MathFunctions, logit_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::logit(b)); -} diff --git a/test/unit/math/prim/fun/minus_test.cpp b/test/unit/math/prim/fun/minus_test.cpp index d3dd9bdad85..e9535ca4781 100644 --- a/test/unit/math/prim/fun/minus_test.cpp +++ b/test/unit/math/prim/fun/minus_test.cpp @@ -10,11 +10,3 @@ TEST(MathMatrixPrimMat, minus) { EXPECT_EQ(0, stan::math::minus(rv0).size()); EXPECT_EQ(0, stan::math::minus(m0).size()); } - -TEST(MathFunctions, minus_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::minus(b)); -} diff --git a/test/unit/math/prim/fun/round_test.cpp b/test/unit/math/prim/fun/round_test.cpp index 3cabf152e67..40cc1a8334f 100644 --- a/test/unit/math/prim/fun/round_test.cpp +++ b/test/unit/math/prim/fun/round_test.cpp @@ -17,11 +17,3 @@ TEST(MathFunctions, roundNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::round(nan))); } - -TEST(MathFunctions, round_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::round(b)); -} diff --git a/test/unit/math/prim/fun/sin_test.cpp b/test/unit/math/prim/fun/sin_test.cpp index 4125f5e22f8..3a564bfea3b 100644 --- a/test/unit/math/prim/fun/sin_test.cpp +++ b/test/unit/math/prim/fun/sin_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, sin) { stan::test::expect_common_prim([](auto x) { return std::sin(x); }, [](auto x) { return stan::math::sin(x); }); } - -TEST(MathFunctions, sin_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::sin(b)); -} diff --git a/test/unit/math/prim/fun/sinh_test.cpp b/test/unit/math/prim/fun/sinh_test.cpp index 72e3f143e5d..72b18d36f94 100644 --- a/test/unit/math/prim/fun/sinh_test.cpp +++ b/test/unit/math/prim/fun/sinh_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, sinh) { stan::test::expect_common_prim([](auto x) { return std::sinh(x); }, [](auto x) { return stan::math::sinh(x); }); } - -TEST(MathFunctions, sinh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::sinh(b)); -} diff --git a/test/unit/math/prim/fun/sqrt_test.cpp b/test/unit/math/prim/fun/sqrt_test.cpp index 9896911dbf5..43c362d0cc9 100644 --- a/test/unit/math/prim/fun/sqrt_test.cpp +++ b/test/unit/math/prim/fun/sqrt_test.cpp @@ -7,11 +7,3 @@ TEST(MathFunctions, sqrtInt) { EXPECT_FLOAT_EQ(std::sqrt(3.0), sqrt(3)); EXPECT_TRUE(stan::math::is_nan(sqrt(-2))); } - -TEST(MathFunctions, sqrt_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::sqrt(b)); -} diff --git a/test/unit/math/prim/fun/square_test.cpp b/test/unit/math/prim/fun/square_test.cpp index 13b66189bcf..aadd3009906 100644 --- a/test/unit/math/prim/fun/square_test.cpp +++ b/test/unit/math/prim/fun/square_test.cpp @@ -3,7 +3,7 @@ #include #include -TEST(MathFunctions, square) { +TEST(MathsFunctions, square) { double y = 2.0; EXPECT_FLOAT_EQ(y * y, stan::math::square(y)); @@ -19,11 +19,3 @@ TEST(MathFunctions, square_nan) { EXPECT_TRUE(std::isnan(stan::math::square(nan))); } - -TEST(MathFunctions, square_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::square(b)); -} diff --git a/test/unit/math/prim/fun/tan_test.cpp b/test/unit/math/prim/fun/tan_test.cpp deleted file mode 100644 index 5b9e861cabc..00000000000 --- a/test/unit/math/prim/fun/tan_test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -TEST(MathFunctions, tan_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::tan(b)); -} diff --git a/test/unit/math/prim/fun/tanh_test.cpp b/test/unit/math/prim/fun/tanh_test.cpp index 594d0e37ed5..bd4da5755c9 100644 --- a/test/unit/math/prim/fun/tanh_test.cpp +++ b/test/unit/math/prim/fun/tanh_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, tanh) { stan::test::expect_common_prim([](auto x) { return std::tanh(x); }, [](auto x) { return stan::math::tanh(x); }); } - -TEST(MathFunctions, tanh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::tanh(b)); -} diff --git a/test/unit/math/prim/fun/tgamma_test.cpp b/test/unit/math/prim/fun/tgamma_test.cpp index 2ecb0bd51cc..51e5b84261c 100644 --- a/test/unit/math/prim/fun/tgamma_test.cpp +++ b/test/unit/math/prim/fun/tgamma_test.cpp @@ -20,11 +20,3 @@ TEST(MathFunctions, tgamma_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::tgamma(nan))); } - -TEST(MathFunctions, tgamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::tgamma(b)); -} diff --git a/test/unit/math/prim/fun/trigamma_test.cpp b/test/unit/math/prim/fun/trigamma_test.cpp index c2d4484d1f0..d2066598d32 100644 --- a/test/unit/math/prim/fun/trigamma_test.cpp +++ b/test/unit/math/prim/fun/trigamma_test.cpp @@ -17,11 +17,3 @@ TEST(MathFunctions, trigamma_nan) { EXPECT_TRUE(std::isnan(stan::math::trigamma(nan))); } - -TEST(MathFunctions, trigamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::trigamma(b)); -} diff --git a/test/unit/math/prim/fun/trunc_test.cpp b/test/unit/math/prim/fun/trunc_test.cpp index 2f64da18b03..8aa3a7c6a00 100644 --- a/test/unit/math/prim/fun/trunc_test.cpp +++ b/test/unit/math/prim/fun/trunc_test.cpp @@ -17,11 +17,3 @@ TEST(MathFunctions, truncNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::trunc(nan))); } - -TEST(MathFunctions, trunc_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::trunc(b)); -} From b96e4018df7438f93ac19c0851a030fb5c7a73cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Ce=C5=A1novar?= Date: Wed, 29 Jan 2020 19:05:54 +0100 Subject: [PATCH 006/167] Fixed conflict in stan/math/prim/fun/log_softmax.hpp --- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 4 ++-- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 4 ++-- stan/math/prim/fun/inv_cloglog.hpp | 4 ++-- stan/math/prim/fun/inv_sqrt.hpp | 4 ++-- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log_softmax.hpp | 11 ++++++----- stan/math/prim/fun/minus.hpp | 4 +++- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 4 ++-- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- stan/math/prim/vectorize/apply_scalar_unary.hpp | 7 +++++-- test/unit/math/prim/fun/Phi_approx_test.cpp | 8 ++++++++ test/unit/math/prim/fun/Phi_test.cpp | 8 ++++++++ test/unit/math/prim/fun/acos_test.cpp | 8 ++++++++ test/unit/math/prim/fun/acosh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/asin_test.cpp | 8 ++++++++ test/unit/math/prim/fun/asinh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/atan_test.cpp | 8 ++++++++ test/unit/math/prim/fun/atanh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cbrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/ceil_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cos_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cosh_test.cpp | 10 ++++++++++ test/unit/math/prim/fun/digamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/erf_test.cpp | 8 ++++++++ test/unit/math/prim/fun/erfc_test.cpp | 8 ++++++++ test/unit/math/prim/fun/exp2_test.cpp | 8 ++++++++ test/unit/math/prim/fun/exp_test.cpp | 8 ++++++++ test/unit/math/prim/fun/expm1_test.cpp | 8 ++++++++ test/unit/math/prim/fun/fabs_test.cpp | 8 ++++++++ test/unit/math/prim/fun/floor_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_Phi_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_cloglog_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_sqrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_square_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_test.cpp | 8 ++++++++ test/unit/math/prim/fun/lgamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log10_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1m_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1p_exp_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1p_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log2_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_inv_logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_softmax_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_test.cpp | 8 ++++++++ test/unit/math/prim/fun/logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/minus_test.cpp | 8 ++++++++ test/unit/math/prim/fun/round_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sin_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sinh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sqrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/square_test.cpp | 10 +++++++++- test/unit/math/prim/fun/tan_test.cpp | 10 ++++++++++ test/unit/math/prim/fun/tanh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/tgamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/trigamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/trunc_test.cpp | 8 ++++++++ 70 files changed, 420 insertions(+), 34 deletions(-) create mode 100644 test/unit/math/prim/fun/cosh_test.cpp create mode 100644 test/unit/math/prim/fun/tan_test.cpp diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 709864e1db6..e5960f1b758 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -45,7 +45,7 @@ inline auto acos(const T& x) { template > inline auto acos(const Eigen::MatrixBase& x) { - return x.derived().array().acos().matrix(); + return x.derived().array().acos().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 81a22dcf60b..0f5d172a29b 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -45,7 +45,7 @@ inline auto asin(const T& x) { template > inline auto asin(const Eigen::MatrixBase& x) { - return x.derived().array().asin().matrix(); + return x.derived().array().asin().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 28a05b08d6a..9576c99240a 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t atan(const T& x) { template > inline auto atan(const Eigen::MatrixBase& x) { - return x.derived().array().atan().matrix(); + return x.derived().array().atan().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 7a78566ac1d..2de0ddb72e7 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -45,7 +45,7 @@ inline auto ceil(const T& x) { template > inline auto ceil(const Eigen::MatrixBase& x) { - return x.derived().array().ceil().matrix(); + return x.derived().array().ceil().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 0c20e53f935..bf9761ace9f 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -45,7 +45,7 @@ inline auto cos(const T& x) { template > inline auto cos(const Eigen::MatrixBase& x) { - return x.derived().array().cos().matrix(); + return x.derived().array().cos().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 3dee3eae2a3..2a0402fe9c2 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t cosh(const T& x) { template > inline auto cosh(const Eigen::MatrixBase& x) { - return x.derived().array().cosh().matrix(); + return x.derived().array().cosh().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 4d60b4c04b5..54486d9e394 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -60,7 +60,7 @@ inline auto exp(const T& x) { template > inline auto exp(const Eigen::MatrixBase& x) { - return x.derived().array().exp().matrix(); + return x.derived().array().exp().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 1dc51b0ee3c..2bfc97071d2 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t fabs(const T& x) { template > inline auto fabs(const Eigen::MatrixBase& x) { - return x.derived().array().abs().matrix(); + return x.derived().array().abs().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto fabs(const Eigen::MatrixBase& x) { template > inline auto fabs(const Eigen::ArrayBase& x) { - return x.derived().abs(); + return x.derived().abs().eval(); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 7f1f37317cd..c53433cf356 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -45,7 +45,7 @@ inline auto floor(const T& x) { template > inline auto floor(const Eigen::MatrixBase& x) { - return x.derived().array().floor().matrix(); + return x.derived().array().floor().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index f50f17f784c..439dc962778 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -45,7 +45,7 @@ inline auto inv(const T& x) { template > inline auto inv(const Eigen::MatrixBase& x) { - return x.derived().array().inverse().matrix(); + return x.derived().array().inverse().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto inv(const Eigen::MatrixBase& x) { template > inline auto inv(const Eigen::ArrayBase& x) { - return x.derived().inverse(); + return x.derived().inverse().eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 945417aa0f9..44c6dfdc8d7 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -87,7 +87,7 @@ inline auto inv_cloglog(const T& x) { template > inline auto inv_cloglog(const Eigen::MatrixBase& x) { - return (1 - exp(-exp(x.derived().array()))).matrix(); + return (1 - exp(-exp(x.derived().array()))).matrix().eval(); } /** @@ -99,7 +99,7 @@ inline auto inv_cloglog(const Eigen::MatrixBase& x) { template > inline auto inv_cloglog(const Eigen::ArrayBase& x) { - return 1 - exp(-exp(x.derived())); + return (1 - exp(-exp(x.derived()))).eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index aa6e0d7ffdf..66b41ee7138 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -50,7 +50,7 @@ inline auto inv_sqrt(const T& x) { template > inline auto inv_sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().rsqrt().matrix(); + return x.derived().array().rsqrt().matrix().eval(); } /** @@ -62,7 +62,7 @@ inline auto inv_sqrt(const Eigen::MatrixBase& x) { template > inline auto inv_sqrt(const Eigen::ArrayBase& x) { - return x.derived().rsqrt(); + return x.derived().rsqrt().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 2e32bc5aa4b..3814d0a5ae5 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -60,7 +60,7 @@ inline auto log(const T& x) { template > inline auto log(const Eigen::MatrixBase& x) { - return x.derived().array().log().matrix(); + return x.derived().array().log().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 808bdb85bad..10b7b0fa1b0 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -45,7 +45,7 @@ inline auto log10(const T& x) { template > inline auto log10(const Eigen::MatrixBase& x) { - return x.derived().array().log10().matrix(); + return x.derived().array().log10().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index a51370d0b1a..8c4951df779 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -36,11 +36,12 @@ namespace math { * @param[in] v Vector to transform. * @return Unit simplex result of the softmax transform of the vector. */ -template -inline Eigen::Matrix log_softmax( - const Eigen::Matrix& v) { - check_nonzero_size("log_softmax", "v", v); - return v.array() - log_sum_exp(v); +template >>...> +inline auto log_softmax(const T& x) { + return apply_vector_unary::apply(x, [&](const auto& v) { + check_nonzero_size("log_softmax", "v", v); + return (v.array() - log_sum_exp(v)).matrix().eval(); + }); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 7f374462fc0..13fe7d090ed 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -1,6 +1,8 @@ #ifndef STAN_MATH_PRIM_FUN_MINUS_HPP #define STAN_MATH_PRIM_FUN_MINUS_HPP +#include + namespace stan { namespace math { @@ -12,7 +14,7 @@ namespace math { * @return Negation of subtrahend. */ template -inline auto minus(const T& x) { +inline plain_type_t minus(const T& x) { return -x; } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 66b2d6a428c..e5cc48efd18 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -62,7 +62,7 @@ inline auto round(const T& x) { template > inline auto round(const Eigen::MatrixBase& x) { - return x.derived().array().round().matrix(); + return x.derived().array().round().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index f2ed9102f97..0b783ffeb20 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -45,7 +45,7 @@ inline auto sin(const T& x) { template > inline auto sin(const Eigen::MatrixBase& x) { - return x.derived().array().sin().matrix(); + return x.derived().array().sin().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index 5e13a03993e..e4d1252295d 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -45,7 +45,7 @@ inline auto sinh(const T& x) { template > inline auto sinh(const Eigen::MatrixBase& x) { - return x.derived().array().sinh().matrix(); + return x.derived().array().sinh().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto sinh(const Eigen::MatrixBase& x) { template > inline auto sinh(const Eigen::ArrayBase& x) { - return x.derived().sinh(); + return x.derived().sinh().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index ae1b6dace93..fe81fcf75b6 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -54,7 +54,7 @@ inline auto sqrt(const T& x) { template > inline auto sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().sqrt().matrix(); + return x.derived().array().sqrt().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index 7064300e695..efc03beb94b 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -45,7 +45,7 @@ inline auto tan(const T& x) { template > inline auto tan(const Eigen::MatrixBase& x) { - return x.derived().array().tan().matrix(); + return x.derived().array().tan().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 60c4d405fb2..2fe9f4c5a25 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -45,7 +45,7 @@ inline auto tanh(const T& x) { template > inline auto tanh(const Eigen::MatrixBase& x) { - return x.derived().array().tanh().matrix(); + return x.derived().array().tanh().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/vectorize/apply_scalar_unary.hpp b/stan/math/prim/vectorize/apply_scalar_unary.hpp index ed31037a207..8cc283cd4ac 100644 --- a/stan/math/prim/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/vectorize/apply_scalar_unary.hpp @@ -59,8 +59,11 @@ struct apply_scalar_unary> { * by F to the specified matrix. */ static inline auto apply(const T& x) { - return x.unaryExpr( - [](scalar_t x) { return apply_scalar_unary::apply(x); }); + return x + .unaryExpr([](scalar_t x) { + return apply_scalar_unary::apply(x); + }) + .eval(); } /** diff --git a/test/unit/math/prim/fun/Phi_approx_test.cpp b/test/unit/math/prim/fun/Phi_approx_test.cpp index dbc4078352d..4a29923c572 100644 --- a/test/unit/math/prim/fun/Phi_approx_test.cpp +++ b/test/unit/math/prim/fun/Phi_approx_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, Phi_approx_nan) { EXPECT_TRUE(std::isnan(stan::math::Phi_approx(nan))); } + +TEST(MathFunctions, Phi_approx__works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::Phi_approx(b)); +} diff --git a/test/unit/math/prim/fun/Phi_test.cpp b/test/unit/math/prim/fun/Phi_test.cpp index c43e260bf88..1273ad24628 100644 --- a/test/unit/math/prim/fun/Phi_test.cpp +++ b/test/unit/math/prim/fun/Phi_test.cpp @@ -118,3 +118,11 @@ TEST(MathFunctions, Phi_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_THROW(stan::math::Phi(nan), std::domain_error); } + +TEST(MathFunctions, Phi_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::Phi(b)); +} diff --git a/test/unit/math/prim/fun/acos_test.cpp b/test/unit/math/prim/fun/acos_test.cpp index c66c4c49a02..7570f52cc71 100644 --- a/test/unit/math/prim/fun/acos_test.cpp +++ b/test/unit/math/prim/fun/acos_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, acos) { stan::test::expect_common_prim([](auto x) { return std::acos(x); }, [](auto x) { return stan::math::acos(x); }); } + +TEST(MathFunctions, acos_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::acos(b)); +} diff --git a/test/unit/math/prim/fun/acosh_test.cpp b/test/unit/math/prim/fun/acosh_test.cpp index 2d5bc742d3c..1f9e8ab7bb5 100644 --- a/test/unit/math/prim/fun/acosh_test.cpp +++ b/test/unit/math/prim/fun/acosh_test.cpp @@ -26,3 +26,11 @@ TEST(MathFunctions, acosh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::acosh(nan))); } + +TEST(MathFunctions, acosh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::acosh(b)); +} diff --git a/test/unit/math/prim/fun/asin_test.cpp b/test/unit/math/prim/fun/asin_test.cpp index a14245b58c2..858be5fbbbd 100644 --- a/test/unit/math/prim/fun/asin_test.cpp +++ b/test/unit/math/prim/fun/asin_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, asin) { stan::test::expect_common_prim([](auto x) { return std::asin(x); }, [](auto x) { return stan::math::asin(x); }); } + +TEST(MathFunctions, asin_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::asin(b)); +} diff --git a/test/unit/math/prim/fun/asinh_test.cpp b/test/unit/math/prim/fun/asinh_test.cpp index 151afc2b3ef..e10228b0af0 100644 --- a/test/unit/math/prim/fun/asinh_test.cpp +++ b/test/unit/math/prim/fun/asinh_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, asinh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::asinh(nan))); } + +TEST(MathFunctions, asinh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::asinh(b)); +} diff --git a/test/unit/math/prim/fun/atan_test.cpp b/test/unit/math/prim/fun/atan_test.cpp index 467ceefff7d..54861be5151 100644 --- a/test/unit/math/prim/fun/atan_test.cpp +++ b/test/unit/math/prim/fun/atan_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, atan) { stan::test::expect_common_prim([](auto x) { return std::atan(x); }, [](auto x) { return stan::math::atan(x); }); } + +TEST(MathFunctions, atan_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::atan(b)); +} diff --git a/test/unit/math/prim/fun/atanh_test.cpp b/test/unit/math/prim/fun/atanh_test.cpp index ca60e585e86..18ae151e23c 100644 --- a/test/unit/math/prim/fun/atanh_test.cpp +++ b/test/unit/math/prim/fun/atanh_test.cpp @@ -27,3 +27,11 @@ TEST(MathFunctions, atanh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::atanh(nan))); } + +TEST(MathFunctions, atanh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::atanh(b)); +} diff --git a/test/unit/math/prim/fun/cbrt_test.cpp b/test/unit/math/prim/fun/cbrt_test.cpp index 2ca66bbde53..854853943a6 100644 --- a/test/unit/math/prim/fun/cbrt_test.cpp +++ b/test/unit/math/prim/fun/cbrt_test.cpp @@ -24,3 +24,11 @@ TEST(MathFunctions, cbrt_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::cbrt(nan))); } + +TEST(MathFunctions, cbrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cbrt(b)); +} diff --git a/test/unit/math/prim/fun/ceil_test.cpp b/test/unit/math/prim/fun/ceil_test.cpp index f26f0068eb1..57a7dce8c41 100644 --- a/test/unit/math/prim/fun/ceil_test.cpp +++ b/test/unit/math/prim/fun/ceil_test.cpp @@ -4,3 +4,11 @@ TEST(primScalFun, ceil) { stan::test::expect_common_prim([](auto x) { return std::ceil(x); }, [](auto x) { return stan::math::ceil(x); }); } + +TEST(MathFunctions, ceil_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::ceil(b)); +} diff --git a/test/unit/math/prim/fun/cos_test.cpp b/test/unit/math/prim/fun/cos_test.cpp index 53daaa398de..94989f9703f 100644 --- a/test/unit/math/prim/fun/cos_test.cpp +++ b/test/unit/math/prim/fun/cos_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, cos) { stan::test::expect_common_prim([](auto x) { return std::cos(x); }, [](auto x) { return stan::math::cos(x); }); } + +TEST(MathFunctions, cos_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cos(b)); +} diff --git a/test/unit/math/prim/fun/cosh_test.cpp b/test/unit/math/prim/fun/cosh_test.cpp new file mode 100644 index 00000000000..d1433637b36 --- /dev/null +++ b/test/unit/math/prim/fun/cosh_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +TEST(MathFunctions, cosh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cosh(b)); +} diff --git a/test/unit/math/prim/fun/digamma_test.cpp b/test/unit/math/prim/fun/digamma_test.cpp index 2a3a7ea150b..b0d37f98b7e 100644 --- a/test/unit/math/prim/fun/digamma_test.cpp +++ b/test/unit/math/prim/fun/digamma_test.cpp @@ -18,3 +18,11 @@ TEST(MathFunctions, digamma_nan) { EXPECT_TRUE(std::isnormal(stan::math::digamma(1.0E50))); } + +TEST(MathFunctions, digamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::digamma(b)); +} diff --git a/test/unit/math/prim/fun/erf_test.cpp b/test/unit/math/prim/fun/erf_test.cpp index f01b192b8be..0c06057587c 100644 --- a/test/unit/math/prim/fun/erf_test.cpp +++ b/test/unit/math/prim/fun/erf_test.cpp @@ -20,3 +20,11 @@ TEST(MathFunctions, erfNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erf(nan))); } + +TEST(MathFunctions, erf_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::erf(b)); +} diff --git a/test/unit/math/prim/fun/erfc_test.cpp b/test/unit/math/prim/fun/erfc_test.cpp index aad40784135..2e19d0fe333 100644 --- a/test/unit/math/prim/fun/erfc_test.cpp +++ b/test/unit/math/prim/fun/erfc_test.cpp @@ -21,3 +21,11 @@ TEST(MathFunctions, erfcNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erfc(nan))); } + +TEST(MathFunctions, erfc_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::erfc(b)); +} diff --git a/test/unit/math/prim/fun/exp2_test.cpp b/test/unit/math/prim/fun/exp2_test.cpp index ae1c93c1e22..b5c8f356425 100644 --- a/test/unit/math/prim/fun/exp2_test.cpp +++ b/test/unit/math/prim/fun/exp2_test.cpp @@ -35,3 +35,11 @@ TEST(MathFunctions, exp2_nan) { EXPECT_TRUE(std::isnan(stan::math::exp2(nan))); } + +TEST(MathFunctions, exp2_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::exp2(b)); +} diff --git a/test/unit/math/prim/fun/exp_test.cpp b/test/unit/math/prim/fun/exp_test.cpp index a19d8643a9f..292f72baf10 100644 --- a/test/unit/math/prim/fun/exp_test.cpp +++ b/test/unit/math/prim/fun/exp_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, expInt) { EXPECT_FLOAT_EQ(std::exp(3), exp(3)); EXPECT_FLOAT_EQ(std::exp(3.0), exp(3.0)); } + +TEST(MathFunctions, exp_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::exp(b)); +} diff --git a/test/unit/math/prim/fun/expm1_test.cpp b/test/unit/math/prim/fun/expm1_test.cpp index 3e35d9873c1..a92bbc91f45 100644 --- a/test/unit/math/prim/fun/expm1_test.cpp +++ b/test/unit/math/prim/fun/expm1_test.cpp @@ -23,3 +23,11 @@ TEST(MathFunctions, expm1_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::expm1(nan))); } + +TEST(MathFunctions, expm1_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::expm1(b)); +} diff --git a/test/unit/math/prim/fun/fabs_test.cpp b/test/unit/math/prim/fun/fabs_test.cpp index 985f1426eda..e4450169d0a 100644 --- a/test/unit/math/prim/fun/fabs_test.cpp +++ b/test/unit/math/prim/fun/fabs_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, fabs) { stan::test::expect_common_prim([](auto x) { return std::fabs(x); }, [](auto x) { return stan::math::fabs(x); }); } + +TEST(MathFunctions, fabs_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::fabs(b)); +} diff --git a/test/unit/math/prim/fun/floor_test.cpp b/test/unit/math/prim/fun/floor_test.cpp index 6c283c47dbc..90c6dca6090 100644 --- a/test/unit/math/prim/fun/floor_test.cpp +++ b/test/unit/math/prim/fun/floor_test.cpp @@ -4,3 +4,11 @@ TEST(primScalFun, floor) { stan::test::expect_common_prim([](auto x) { return std::floor(x); }, [](auto x) { return stan::math::floor(x); }); } + +TEST(MathFunctions, floor_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::floor(b)); +} diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index 13d81adbb7f..9538f7151d6 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -34,3 +34,11 @@ TEST(MathFunctions, inv_Phi_nan) { EXPECT_THROW(inv_Phi(-2.0), std::domain_error); EXPECT_THROW(inv_Phi(2.0), std::domain_error); } + +TEST(MathFunctions, inv_Phi_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::inv_Phi(b)); +} diff --git a/test/unit/math/prim/fun/inv_cloglog_test.cpp b/test/unit/math/prim/fun/inv_cloglog_test.cpp index 67118b3b9e6..3f8c3fa6c7b 100644 --- a/test/unit/math/prim/fun/inv_cloglog_test.cpp +++ b/test/unit/math/prim/fun/inv_cloglog_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, inv_cloglog_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_cloglog(nan))); } + +TEST(MathFunctions, inv_cloglog_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_cloglog(b)); +} diff --git a/test/unit/math/prim/fun/inv_logit_test.cpp b/test/unit/math/prim/fun/inv_logit_test.cpp index b1e0de78766..a7e272f19b5 100644 --- a/test/unit/math/prim/fun/inv_logit_test.cpp +++ b/test/unit/math/prim/fun/inv_logit_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_logit(nan))); } + +TEST(MathFunctions, inv_logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_logit(b)); +} diff --git a/test/unit/math/prim/fun/inv_sqrt_test.cpp b/test/unit/math/prim/fun/inv_sqrt_test.cpp index 645957931a2..1a7deb94def 100644 --- a/test/unit/math/prim/fun/inv_sqrt_test.cpp +++ b/test/unit/math/prim/fun/inv_sqrt_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, inv_sqrt_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_sqrt(nan))); } + +TEST(MathFunctions, inv_sqrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_sqrt(b)); +} diff --git a/test/unit/math/prim/fun/inv_square_test.cpp b/test/unit/math/prim/fun/inv_square_test.cpp index 32c17c48cf2..a3cb3588a07 100644 --- a/test/unit/math/prim/fun/inv_square_test.cpp +++ b/test/unit/math/prim/fun/inv_square_test.cpp @@ -19,3 +19,11 @@ TEST(MathFunctions, inv_square_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_square(nan))); } + +TEST(MathFunctions, inv_square_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_square(b)); +} diff --git a/test/unit/math/prim/fun/inv_test.cpp b/test/unit/math/prim/fun/inv_test.cpp index f418be35db1..99f9d21a969 100644 --- a/test/unit/math/prim/fun/inv_test.cpp +++ b/test/unit/math/prim/fun/inv_test.cpp @@ -19,3 +19,11 @@ TEST(MathFunctions, inv_nan) { EXPECT_TRUE(std::isnan(stan::math::inv(nan))); } + +TEST(MathFunctions, inv_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv(b)); +} diff --git a/test/unit/math/prim/fun/lgamma_test.cpp b/test/unit/math/prim/fun/lgamma_test.cpp index d252006d5d5..77e52134c33 100644 --- a/test/unit/math/prim/fun/lgamma_test.cpp +++ b/test/unit/math/prim/fun/lgamma_test.cpp @@ -26,3 +26,11 @@ TEST(MathFunctions, lgamma_nan) { EXPECT_TRUE( std::isnormal(boost::math::lgamma(1.0E50, stan::math::boost_policy_t()))); } + +TEST(MathFunctions, lgamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::lgamma(b)); +} diff --git a/test/unit/math/prim/fun/log10_test.cpp b/test/unit/math/prim/fun/log10_test.cpp index 1a67f75a75c..4e3e79e94fc 100644 --- a/test/unit/math/prim/fun/log10_test.cpp +++ b/test/unit/math/prim/fun/log10_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, log10) { stan::test::expect_common_prim([](auto x) { return std::log10(x); }, [](auto x) { return stan::math::log10(x); }); } + +TEST(MathFunctions, log10_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log10(b)); +} diff --git a/test/unit/math/prim/fun/log1m_test.cpp b/test/unit/math/prim/fun/log1m_test.cpp index a0cf248f987..2c68c5a825e 100644 --- a/test/unit/math/prim/fun/log1m_test.cpp +++ b/test/unit/math/prim/fun/log1m_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, log1m_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1m(nan))); } + +TEST(MathFunctions, log1m_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::log1m(b)); +} diff --git a/test/unit/math/prim/fun/log1p_exp_test.cpp b/test/unit/math/prim/fun/log1p_exp_test.cpp index 6930db3a1eb..aa4b3fd0303 100644 --- a/test/unit/math/prim/fun/log1p_exp_test.cpp +++ b/test/unit/math/prim/fun/log1p_exp_test.cpp @@ -16,3 +16,11 @@ TEST(MathFunctions, log1p_exp_nan) { EXPECT_TRUE(std::isnan(stan::math::log1p_exp(nan))); } + +TEST(MathFunctions, log1p_exp_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log1p_exp(b)); +} diff --git a/test/unit/math/prim/fun/log1p_test.cpp b/test/unit/math/prim/fun/log1p_test.cpp index 35f90f46452..714958a1745 100644 --- a/test/unit/math/prim/fun/log1p_test.cpp +++ b/test/unit/math/prim/fun/log1p_test.cpp @@ -40,3 +40,11 @@ TEST(MathFunctions, log1p_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1p(nan))); } + +TEST(MathFunctions, log1p_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log1p(b)); +} diff --git a/test/unit/math/prim/fun/log2_test.cpp b/test/unit/math/prim/fun/log2_test.cpp index 031a50afa97..cf726f78ceb 100644 --- a/test/unit/math/prim/fun/log2_test.cpp +++ b/test/unit/math/prim/fun/log2_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, log2_nan) { EXPECT_TRUE(std::isnan(stan::math::log2(nan))); } + +TEST(MathFunctions, log2_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log2(b)); +} diff --git a/test/unit/math/prim/fun/log_inv_logit_test.cpp b/test/unit/math/prim/fun/log_inv_logit_test.cpp index 774d044a4dd..bb13d370b2a 100644 --- a/test/unit/math/prim/fun/log_inv_logit_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_test.cpp @@ -18,3 +18,11 @@ TEST(MathFunctions, log_inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::log_inv_logit(nan))); } + +TEST(MathFunctions, log_inv_logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log_inv_logit(b)); +} diff --git a/test/unit/math/prim/fun/log_softmax_test.cpp b/test/unit/math/prim/fun/log_softmax_test.cpp index ba26ba1ce20..eff97db3cd9 100644 --- a/test/unit/math/prim/fun/log_softmax_test.cpp +++ b/test/unit/math/prim/fun/log_softmax_test.cpp @@ -47,3 +47,11 @@ TEST(MathMatrixPrimMat, log_softmax_exception) { EXPECT_THROW(log_softmax(v0), std::invalid_argument); } + +TEST(MathFunctions, log_softmax_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log_softmax(b)); +} diff --git a/test/unit/math/prim/fun/log_test.cpp b/test/unit/math/prim/fun/log_test.cpp index 77563092631..f7163dbcf1a 100644 --- a/test/unit/math/prim/fun/log_test.cpp +++ b/test/unit/math/prim/fun/log_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, logInt) { EXPECT_FLOAT_EQ(std::log(3), log(3)); EXPECT_FLOAT_EQ(std::log(3.0), log(3.0)); } + +TEST(MathFunctions, log_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log(b)); +} diff --git a/test/unit/math/prim/fun/logit_test.cpp b/test/unit/math/prim/fun/logit_test.cpp index b6820aa4b7f..566cbcd88ad 100644 --- a/test/unit/math/prim/fun/logit_test.cpp +++ b/test/unit/math/prim/fun/logit_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, logit_nan) { EXPECT_TRUE(std::isnan(stan::math::logit(nan))); } + +TEST(MathFunctions, logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::logit(b)); +} diff --git a/test/unit/math/prim/fun/minus_test.cpp b/test/unit/math/prim/fun/minus_test.cpp index e9535ca4781..d3dd9bdad85 100644 --- a/test/unit/math/prim/fun/minus_test.cpp +++ b/test/unit/math/prim/fun/minus_test.cpp @@ -10,3 +10,11 @@ TEST(MathMatrixPrimMat, minus) { EXPECT_EQ(0, stan::math::minus(rv0).size()); EXPECT_EQ(0, stan::math::minus(m0).size()); } + +TEST(MathFunctions, minus_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::minus(b)); +} diff --git a/test/unit/math/prim/fun/round_test.cpp b/test/unit/math/prim/fun/round_test.cpp index 40cc1a8334f..3cabf152e67 100644 --- a/test/unit/math/prim/fun/round_test.cpp +++ b/test/unit/math/prim/fun/round_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, roundNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::round(nan))); } + +TEST(MathFunctions, round_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::round(b)); +} diff --git a/test/unit/math/prim/fun/sin_test.cpp b/test/unit/math/prim/fun/sin_test.cpp index 3a564bfea3b..4125f5e22f8 100644 --- a/test/unit/math/prim/fun/sin_test.cpp +++ b/test/unit/math/prim/fun/sin_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, sin) { stan::test::expect_common_prim([](auto x) { return std::sin(x); }, [](auto x) { return stan::math::sin(x); }); } + +TEST(MathFunctions, sin_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sin(b)); +} diff --git a/test/unit/math/prim/fun/sinh_test.cpp b/test/unit/math/prim/fun/sinh_test.cpp index 72b18d36f94..72e3f143e5d 100644 --- a/test/unit/math/prim/fun/sinh_test.cpp +++ b/test/unit/math/prim/fun/sinh_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, sinh) { stan::test::expect_common_prim([](auto x) { return std::sinh(x); }, [](auto x) { return stan::math::sinh(x); }); } + +TEST(MathFunctions, sinh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sinh(b)); +} diff --git a/test/unit/math/prim/fun/sqrt_test.cpp b/test/unit/math/prim/fun/sqrt_test.cpp index 43c362d0cc9..9896911dbf5 100644 --- a/test/unit/math/prim/fun/sqrt_test.cpp +++ b/test/unit/math/prim/fun/sqrt_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, sqrtInt) { EXPECT_FLOAT_EQ(std::sqrt(3.0), sqrt(3)); EXPECT_TRUE(stan::math::is_nan(sqrt(-2))); } + +TEST(MathFunctions, sqrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sqrt(b)); +} diff --git a/test/unit/math/prim/fun/square_test.cpp b/test/unit/math/prim/fun/square_test.cpp index aadd3009906..13b66189bcf 100644 --- a/test/unit/math/prim/fun/square_test.cpp +++ b/test/unit/math/prim/fun/square_test.cpp @@ -3,7 +3,7 @@ #include #include -TEST(MathsFunctions, square) { +TEST(MathFunctions, square) { double y = 2.0; EXPECT_FLOAT_EQ(y * y, stan::math::square(y)); @@ -19,3 +19,11 @@ TEST(MathFunctions, square_nan) { EXPECT_TRUE(std::isnan(stan::math::square(nan))); } + +TEST(MathFunctions, square_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::square(b)); +} diff --git a/test/unit/math/prim/fun/tan_test.cpp b/test/unit/math/prim/fun/tan_test.cpp new file mode 100644 index 00000000000..5b9e861cabc --- /dev/null +++ b/test/unit/math/prim/fun/tan_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +TEST(MathFunctions, tan_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tan(b)); +} diff --git a/test/unit/math/prim/fun/tanh_test.cpp b/test/unit/math/prim/fun/tanh_test.cpp index bd4da5755c9..594d0e37ed5 100644 --- a/test/unit/math/prim/fun/tanh_test.cpp +++ b/test/unit/math/prim/fun/tanh_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, tanh) { stan::test::expect_common_prim([](auto x) { return std::tanh(x); }, [](auto x) { return stan::math::tanh(x); }); } + +TEST(MathFunctions, tanh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tanh(b)); +} diff --git a/test/unit/math/prim/fun/tgamma_test.cpp b/test/unit/math/prim/fun/tgamma_test.cpp index 51e5b84261c..2ecb0bd51cc 100644 --- a/test/unit/math/prim/fun/tgamma_test.cpp +++ b/test/unit/math/prim/fun/tgamma_test.cpp @@ -20,3 +20,11 @@ TEST(MathFunctions, tgamma_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::tgamma(nan))); } + +TEST(MathFunctions, tgamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tgamma(b)); +} diff --git a/test/unit/math/prim/fun/trigamma_test.cpp b/test/unit/math/prim/fun/trigamma_test.cpp index d2066598d32..c2d4484d1f0 100644 --- a/test/unit/math/prim/fun/trigamma_test.cpp +++ b/test/unit/math/prim/fun/trigamma_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, trigamma_nan) { EXPECT_TRUE(std::isnan(stan::math::trigamma(nan))); } + +TEST(MathFunctions, trigamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::trigamma(b)); +} diff --git a/test/unit/math/prim/fun/trunc_test.cpp b/test/unit/math/prim/fun/trunc_test.cpp index 8aa3a7c6a00..2f64da18b03 100644 --- a/test/unit/math/prim/fun/trunc_test.cpp +++ b/test/unit/math/prim/fun/trunc_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, truncNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::trunc(nan))); } + +TEST(MathFunctions, trunc_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::trunc(b)); +} From ab50cb5e0bf6cccf19511a8341ead4763683c0f2 Mon Sep 17 00:00:00 2001 From: rok-cesnovar Date: Wed, 29 Jan 2020 21:50:31 +0100 Subject: [PATCH 007/167] Fixed conflict in stan/math/prim/fun/log_softmax.hpp --- stan/math/prim/fun/log_softmax.hpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index 8c4951df779..a51370d0b1a 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -36,12 +36,11 @@ namespace math { * @param[in] v Vector to transform. * @return Unit simplex result of the softmax transform of the vector. */ -template >>...> -inline auto log_softmax(const T& x) { - return apply_vector_unary::apply(x, [&](const auto& v) { - check_nonzero_size("log_softmax", "v", v); - return (v.array() - log_sum_exp(v)).matrix().eval(); - }); +template +inline Eigen::Matrix log_softmax( + const Eigen::Matrix& v) { + check_nonzero_size("log_softmax", "v", v); + return v.array() - log_sum_exp(v); } } // namespace math From ba8e315d1b0317ef4e9c3cba31c69a8d38891ec8 Mon Sep 17 00:00:00 2001 From: Nicusor Serban Date: Wed, 29 Jan 2020 22:58:08 +0200 Subject: [PATCH 008/167] Updated math to v3.1.1 --- .github/ISSUE_TEMPLATE.md | 2 +- doxygen/doxygen.cfg | 2 +- stan/math/version.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 0e35de62263..8f112ab89d3 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -25,4 +25,4 @@ If this is a **feature request**, show what you expect to happen if the feature #### Current Version: -v3.1.0 +v3.1.1 diff --git a/doxygen/doxygen.cfg b/doxygen/doxygen.cfg index 8fee6f17d59..e4b5060f7d0 100644 --- a/doxygen/doxygen.cfg +++ b/doxygen/doxygen.cfg @@ -38,7 +38,7 @@ PROJECT_NAME = "Stan Math Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 3.1.0 +PROJECT_NUMBER = 3.1.1 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/stan/math/version.hpp b/stan/math/version.hpp index bf8599edc07..d5ba5648855 100644 --- a/stan/math/version.hpp +++ b/stan/math/version.hpp @@ -13,7 +13,7 @@ #define STAN_MATH_MAJOR 3 #define STAN_MATH_MINOR 1 -#define STAN_MATH_PATCH 0 +#define STAN_MATH_PATCH 1 namespace stan { namespace math { From ea955b842553cc9b19b10b2f5ead3276530ed54d Mon Sep 17 00:00:00 2001 From: Nicusor Serban Date: Thu, 30 Jan 2020 01:54:02 +0200 Subject: [PATCH 009/167] Reverted release 3.1.1 --- .github/ISSUE_TEMPLATE.md | 2 +- doxygen/doxygen.cfg | 2 +- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 4 ++-- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 4 ++-- stan/math/prim/fun/inv_cloglog.hpp | 4 ++-- stan/math/prim/fun/inv_sqrt.hpp | 4 ++-- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/minus.hpp | 4 +--- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 4 ++-- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- stan/math/prim/vectorize/apply_scalar_unary.hpp | 7 ++----- stan/math/version.hpp | 2 +- test/unit/math/prim/fun/Phi_approx_test.cpp | 8 -------- test/unit/math/prim/fun/Phi_test.cpp | 8 -------- test/unit/math/prim/fun/acos_test.cpp | 8 -------- test/unit/math/prim/fun/acosh_test.cpp | 8 -------- test/unit/math/prim/fun/asin_test.cpp | 8 -------- test/unit/math/prim/fun/asinh_test.cpp | 8 -------- test/unit/math/prim/fun/atan_test.cpp | 8 -------- test/unit/math/prim/fun/atanh_test.cpp | 8 -------- test/unit/math/prim/fun/cbrt_test.cpp | 8 -------- test/unit/math/prim/fun/ceil_test.cpp | 8 -------- test/unit/math/prim/fun/cos_test.cpp | 8 -------- test/unit/math/prim/fun/cosh_test.cpp | 10 ---------- test/unit/math/prim/fun/digamma_test.cpp | 8 -------- test/unit/math/prim/fun/erf_test.cpp | 8 -------- test/unit/math/prim/fun/erfc_test.cpp | 8 -------- test/unit/math/prim/fun/exp2_test.cpp | 8 -------- test/unit/math/prim/fun/exp_test.cpp | 8 -------- test/unit/math/prim/fun/expm1_test.cpp | 8 -------- test/unit/math/prim/fun/fabs_test.cpp | 8 -------- test/unit/math/prim/fun/floor_test.cpp | 8 -------- test/unit/math/prim/fun/inv_Phi_test.cpp | 8 -------- test/unit/math/prim/fun/inv_cloglog_test.cpp | 8 -------- test/unit/math/prim/fun/inv_logit_test.cpp | 8 -------- test/unit/math/prim/fun/inv_sqrt_test.cpp | 8 -------- test/unit/math/prim/fun/inv_square_test.cpp | 8 -------- test/unit/math/prim/fun/inv_test.cpp | 8 -------- test/unit/math/prim/fun/lgamma_test.cpp | 8 -------- test/unit/math/prim/fun/log10_test.cpp | 8 -------- test/unit/math/prim/fun/log1m_test.cpp | 8 -------- test/unit/math/prim/fun/log1p_exp_test.cpp | 8 -------- test/unit/math/prim/fun/log1p_test.cpp | 8 -------- test/unit/math/prim/fun/log2_test.cpp | 8 -------- test/unit/math/prim/fun/log_inv_logit_test.cpp | 8 -------- test/unit/math/prim/fun/log_softmax_test.cpp | 8 -------- test/unit/math/prim/fun/log_test.cpp | 8 -------- test/unit/math/prim/fun/logit_test.cpp | 8 -------- test/unit/math/prim/fun/minus_test.cpp | 8 -------- test/unit/math/prim/fun/round_test.cpp | 8 -------- test/unit/math/prim/fun/sin_test.cpp | 8 -------- test/unit/math/prim/fun/sinh_test.cpp | 8 -------- test/unit/math/prim/fun/sqrt_test.cpp | 8 -------- test/unit/math/prim/fun/square_test.cpp | 10 +--------- test/unit/math/prim/fun/tan_test.cpp | 10 ---------- test/unit/math/prim/fun/tanh_test.cpp | 8 -------- test/unit/math/prim/fun/tgamma_test.cpp | 8 -------- test/unit/math/prim/fun/trigamma_test.cpp | 8 -------- test/unit/math/prim/fun/trunc_test.cpp | 8 -------- 72 files changed, 32 insertions(+), 417 deletions(-) delete mode 100644 test/unit/math/prim/fun/cosh_test.cpp delete mode 100644 test/unit/math/prim/fun/tan_test.cpp diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 8f112ab89d3..0e35de62263 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -25,4 +25,4 @@ If this is a **feature request**, show what you expect to happen if the feature #### Current Version: -v3.1.1 +v3.1.0 diff --git a/doxygen/doxygen.cfg b/doxygen/doxygen.cfg index e4b5060f7d0..8fee6f17d59 100644 --- a/doxygen/doxygen.cfg +++ b/doxygen/doxygen.cfg @@ -38,7 +38,7 @@ PROJECT_NAME = "Stan Math Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 3.1.1 +PROJECT_NUMBER = 3.1.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index e5960f1b758..709864e1db6 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -45,7 +45,7 @@ inline auto acos(const T& x) { template > inline auto acos(const Eigen::MatrixBase& x) { - return x.derived().array().acos().matrix().eval(); + return x.derived().array().acos().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 0f5d172a29b..81a22dcf60b 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -45,7 +45,7 @@ inline auto asin(const T& x) { template > inline auto asin(const Eigen::MatrixBase& x) { - return x.derived().array().asin().matrix().eval(); + return x.derived().array().asin().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 9576c99240a..28a05b08d6a 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t atan(const T& x) { template > inline auto atan(const Eigen::MatrixBase& x) { - return x.derived().array().atan().matrix().eval(); + return x.derived().array().atan().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 2de0ddb72e7..7a78566ac1d 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -45,7 +45,7 @@ inline auto ceil(const T& x) { template > inline auto ceil(const Eigen::MatrixBase& x) { - return x.derived().array().ceil().matrix().eval(); + return x.derived().array().ceil().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index bf9761ace9f..0c20e53f935 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -45,7 +45,7 @@ inline auto cos(const T& x) { template > inline auto cos(const Eigen::MatrixBase& x) { - return x.derived().array().cos().matrix().eval(); + return x.derived().array().cos().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 2a0402fe9c2..3dee3eae2a3 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t cosh(const T& x) { template > inline auto cosh(const Eigen::MatrixBase& x) { - return x.derived().array().cosh().matrix().eval(); + return x.derived().array().cosh().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 54486d9e394..4d60b4c04b5 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -60,7 +60,7 @@ inline auto exp(const T& x) { template > inline auto exp(const Eigen::MatrixBase& x) { - return x.derived().array().exp().matrix().eval(); + return x.derived().array().exp().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 2bfc97071d2..1dc51b0ee3c 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t fabs(const T& x) { template > inline auto fabs(const Eigen::MatrixBase& x) { - return x.derived().array().abs().matrix().eval(); + return x.derived().array().abs().matrix(); } /** @@ -57,7 +57,7 @@ inline auto fabs(const Eigen::MatrixBase& x) { template > inline auto fabs(const Eigen::ArrayBase& x) { - return x.derived().abs().eval(); + return x.derived().abs(); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index c53433cf356..7f1f37317cd 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -45,7 +45,7 @@ inline auto floor(const T& x) { template > inline auto floor(const Eigen::MatrixBase& x) { - return x.derived().array().floor().matrix().eval(); + return x.derived().array().floor().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 439dc962778..f50f17f784c 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -45,7 +45,7 @@ inline auto inv(const T& x) { template > inline auto inv(const Eigen::MatrixBase& x) { - return x.derived().array().inverse().matrix().eval(); + return x.derived().array().inverse().matrix(); } /** @@ -57,7 +57,7 @@ inline auto inv(const Eigen::MatrixBase& x) { template > inline auto inv(const Eigen::ArrayBase& x) { - return x.derived().inverse().eval(); + return x.derived().inverse(); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 44c6dfdc8d7..945417aa0f9 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -87,7 +87,7 @@ inline auto inv_cloglog(const T& x) { template > inline auto inv_cloglog(const Eigen::MatrixBase& x) { - return (1 - exp(-exp(x.derived().array()))).matrix().eval(); + return (1 - exp(-exp(x.derived().array()))).matrix(); } /** @@ -99,7 +99,7 @@ inline auto inv_cloglog(const Eigen::MatrixBase& x) { template > inline auto inv_cloglog(const Eigen::ArrayBase& x) { - return (1 - exp(-exp(x.derived()))).eval(); + return 1 - exp(-exp(x.derived())); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index 66b41ee7138..aa6e0d7ffdf 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -50,7 +50,7 @@ inline auto inv_sqrt(const T& x) { template > inline auto inv_sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().rsqrt().matrix().eval(); + return x.derived().array().rsqrt().matrix(); } /** @@ -62,7 +62,7 @@ inline auto inv_sqrt(const Eigen::MatrixBase& x) { template > inline auto inv_sqrt(const Eigen::ArrayBase& x) { - return x.derived().rsqrt().eval(); + return x.derived().rsqrt(); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 3814d0a5ae5..2e32bc5aa4b 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -60,7 +60,7 @@ inline auto log(const T& x) { template > inline auto log(const Eigen::MatrixBase& x) { - return x.derived().array().log().matrix().eval(); + return x.derived().array().log().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 10b7b0fa1b0..808bdb85bad 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -45,7 +45,7 @@ inline auto log10(const T& x) { template > inline auto log10(const Eigen::MatrixBase& x) { - return x.derived().array().log10().matrix().eval(); + return x.derived().array().log10().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 13fe7d090ed..7f374462fc0 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -1,8 +1,6 @@ #ifndef STAN_MATH_PRIM_FUN_MINUS_HPP #define STAN_MATH_PRIM_FUN_MINUS_HPP -#include - namespace stan { namespace math { @@ -14,7 +12,7 @@ namespace math { * @return Negation of subtrahend. */ template -inline plain_type_t minus(const T& x) { +inline auto minus(const T& x) { return -x; } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index e5cc48efd18..66b2d6a428c 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -62,7 +62,7 @@ inline auto round(const T& x) { template > inline auto round(const Eigen::MatrixBase& x) { - return x.derived().array().round().matrix().eval(); + return x.derived().array().round().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index 0b783ffeb20..f2ed9102f97 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -45,7 +45,7 @@ inline auto sin(const T& x) { template > inline auto sin(const Eigen::MatrixBase& x) { - return x.derived().array().sin().matrix().eval(); + return x.derived().array().sin().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index e4d1252295d..5e13a03993e 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -45,7 +45,7 @@ inline auto sinh(const T& x) { template > inline auto sinh(const Eigen::MatrixBase& x) { - return x.derived().array().sinh().matrix().eval(); + return x.derived().array().sinh().matrix(); } /** @@ -57,7 +57,7 @@ inline auto sinh(const Eigen::MatrixBase& x) { template > inline auto sinh(const Eigen::ArrayBase& x) { - return x.derived().sinh().eval(); + return x.derived().sinh(); } } // namespace math diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index fe81fcf75b6..ae1b6dace93 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -54,7 +54,7 @@ inline auto sqrt(const T& x) { template > inline auto sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().sqrt().matrix().eval(); + return x.derived().array().sqrt().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index efc03beb94b..7064300e695 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -45,7 +45,7 @@ inline auto tan(const T& x) { template > inline auto tan(const Eigen::MatrixBase& x) { - return x.derived().array().tan().matrix().eval(); + return x.derived().array().tan().matrix(); } } // namespace math diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 2fe9f4c5a25..60c4d405fb2 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -45,7 +45,7 @@ inline auto tanh(const T& x) { template > inline auto tanh(const Eigen::MatrixBase& x) { - return x.derived().array().tanh().matrix().eval(); + return x.derived().array().tanh().matrix(); } } // namespace math diff --git a/stan/math/prim/vectorize/apply_scalar_unary.hpp b/stan/math/prim/vectorize/apply_scalar_unary.hpp index 8cc283cd4ac..ed31037a207 100644 --- a/stan/math/prim/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/vectorize/apply_scalar_unary.hpp @@ -59,11 +59,8 @@ struct apply_scalar_unary> { * by F to the specified matrix. */ static inline auto apply(const T& x) { - return x - .unaryExpr([](scalar_t x) { - return apply_scalar_unary::apply(x); - }) - .eval(); + return x.unaryExpr( + [](scalar_t x) { return apply_scalar_unary::apply(x); }); } /** diff --git a/stan/math/version.hpp b/stan/math/version.hpp index d5ba5648855..bf8599edc07 100644 --- a/stan/math/version.hpp +++ b/stan/math/version.hpp @@ -13,7 +13,7 @@ #define STAN_MATH_MAJOR 3 #define STAN_MATH_MINOR 1 -#define STAN_MATH_PATCH 1 +#define STAN_MATH_PATCH 0 namespace stan { namespace math { diff --git a/test/unit/math/prim/fun/Phi_approx_test.cpp b/test/unit/math/prim/fun/Phi_approx_test.cpp index 4a29923c572..dbc4078352d 100644 --- a/test/unit/math/prim/fun/Phi_approx_test.cpp +++ b/test/unit/math/prim/fun/Phi_approx_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, Phi_approx_nan) { EXPECT_TRUE(std::isnan(stan::math::Phi_approx(nan))); } - -TEST(MathFunctions, Phi_approx__works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::Phi_approx(b)); -} diff --git a/test/unit/math/prim/fun/Phi_test.cpp b/test/unit/math/prim/fun/Phi_test.cpp index 1273ad24628..c43e260bf88 100644 --- a/test/unit/math/prim/fun/Phi_test.cpp +++ b/test/unit/math/prim/fun/Phi_test.cpp @@ -118,11 +118,3 @@ TEST(MathFunctions, Phi_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_THROW(stan::math::Phi(nan), std::domain_error); } - -TEST(MathFunctions, Phi_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::Phi(b)); -} diff --git a/test/unit/math/prim/fun/acos_test.cpp b/test/unit/math/prim/fun/acos_test.cpp index 7570f52cc71..c66c4c49a02 100644 --- a/test/unit/math/prim/fun/acos_test.cpp +++ b/test/unit/math/prim/fun/acos_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, acos) { stan::test::expect_common_prim([](auto x) { return std::acos(x); }, [](auto x) { return stan::math::acos(x); }); } - -TEST(MathFunctions, acos_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::acos(b)); -} diff --git a/test/unit/math/prim/fun/acosh_test.cpp b/test/unit/math/prim/fun/acosh_test.cpp index 1f9e8ab7bb5..2d5bc742d3c 100644 --- a/test/unit/math/prim/fun/acosh_test.cpp +++ b/test/unit/math/prim/fun/acosh_test.cpp @@ -26,11 +26,3 @@ TEST(MathFunctions, acosh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::acosh(nan))); } - -TEST(MathFunctions, acosh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::acosh(b)); -} diff --git a/test/unit/math/prim/fun/asin_test.cpp b/test/unit/math/prim/fun/asin_test.cpp index 858be5fbbbd..a14245b58c2 100644 --- a/test/unit/math/prim/fun/asin_test.cpp +++ b/test/unit/math/prim/fun/asin_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, asin) { stan::test::expect_common_prim([](auto x) { return std::asin(x); }, [](auto x) { return stan::math::asin(x); }); } - -TEST(MathFunctions, asin_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::asin(b)); -} diff --git a/test/unit/math/prim/fun/asinh_test.cpp b/test/unit/math/prim/fun/asinh_test.cpp index e10228b0af0..151afc2b3ef 100644 --- a/test/unit/math/prim/fun/asinh_test.cpp +++ b/test/unit/math/prim/fun/asinh_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, asinh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::asinh(nan))); } - -TEST(MathFunctions, asinh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::asinh(b)); -} diff --git a/test/unit/math/prim/fun/atan_test.cpp b/test/unit/math/prim/fun/atan_test.cpp index 54861be5151..467ceefff7d 100644 --- a/test/unit/math/prim/fun/atan_test.cpp +++ b/test/unit/math/prim/fun/atan_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, atan) { stan::test::expect_common_prim([](auto x) { return std::atan(x); }, [](auto x) { return stan::math::atan(x); }); } - -TEST(MathFunctions, atan_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::atan(b)); -} diff --git a/test/unit/math/prim/fun/atanh_test.cpp b/test/unit/math/prim/fun/atanh_test.cpp index 18ae151e23c..ca60e585e86 100644 --- a/test/unit/math/prim/fun/atanh_test.cpp +++ b/test/unit/math/prim/fun/atanh_test.cpp @@ -27,11 +27,3 @@ TEST(MathFunctions, atanh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::atanh(nan))); } - -TEST(MathFunctions, atanh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::atanh(b)); -} diff --git a/test/unit/math/prim/fun/cbrt_test.cpp b/test/unit/math/prim/fun/cbrt_test.cpp index 854853943a6..2ca66bbde53 100644 --- a/test/unit/math/prim/fun/cbrt_test.cpp +++ b/test/unit/math/prim/fun/cbrt_test.cpp @@ -24,11 +24,3 @@ TEST(MathFunctions, cbrt_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::cbrt(nan))); } - -TEST(MathFunctions, cbrt_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::cbrt(b)); -} diff --git a/test/unit/math/prim/fun/ceil_test.cpp b/test/unit/math/prim/fun/ceil_test.cpp index 57a7dce8c41..f26f0068eb1 100644 --- a/test/unit/math/prim/fun/ceil_test.cpp +++ b/test/unit/math/prim/fun/ceil_test.cpp @@ -4,11 +4,3 @@ TEST(primScalFun, ceil) { stan::test::expect_common_prim([](auto x) { return std::ceil(x); }, [](auto x) { return stan::math::ceil(x); }); } - -TEST(MathFunctions, ceil_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::ceil(b)); -} diff --git a/test/unit/math/prim/fun/cos_test.cpp b/test/unit/math/prim/fun/cos_test.cpp index 94989f9703f..53daaa398de 100644 --- a/test/unit/math/prim/fun/cos_test.cpp +++ b/test/unit/math/prim/fun/cos_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, cos) { stan::test::expect_common_prim([](auto x) { return std::cos(x); }, [](auto x) { return stan::math::cos(x); }); } - -TEST(MathFunctions, cos_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::cos(b)); -} diff --git a/test/unit/math/prim/fun/cosh_test.cpp b/test/unit/math/prim/fun/cosh_test.cpp deleted file mode 100644 index d1433637b36..00000000000 --- a/test/unit/math/prim/fun/cosh_test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -TEST(MathFunctions, cosh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::cosh(b)); -} diff --git a/test/unit/math/prim/fun/digamma_test.cpp b/test/unit/math/prim/fun/digamma_test.cpp index b0d37f98b7e..2a3a7ea150b 100644 --- a/test/unit/math/prim/fun/digamma_test.cpp +++ b/test/unit/math/prim/fun/digamma_test.cpp @@ -18,11 +18,3 @@ TEST(MathFunctions, digamma_nan) { EXPECT_TRUE(std::isnormal(stan::math::digamma(1.0E50))); } - -TEST(MathFunctions, digamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::digamma(b)); -} diff --git a/test/unit/math/prim/fun/erf_test.cpp b/test/unit/math/prim/fun/erf_test.cpp index 0c06057587c..f01b192b8be 100644 --- a/test/unit/math/prim/fun/erf_test.cpp +++ b/test/unit/math/prim/fun/erf_test.cpp @@ -20,11 +20,3 @@ TEST(MathFunctions, erfNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erf(nan))); } - -TEST(MathFunctions, erf_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::erf(b)); -} diff --git a/test/unit/math/prim/fun/erfc_test.cpp b/test/unit/math/prim/fun/erfc_test.cpp index 2e19d0fe333..aad40784135 100644 --- a/test/unit/math/prim/fun/erfc_test.cpp +++ b/test/unit/math/prim/fun/erfc_test.cpp @@ -21,11 +21,3 @@ TEST(MathFunctions, erfcNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erfc(nan))); } - -TEST(MathFunctions, erfc_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::erfc(b)); -} diff --git a/test/unit/math/prim/fun/exp2_test.cpp b/test/unit/math/prim/fun/exp2_test.cpp index b5c8f356425..ae1c93c1e22 100644 --- a/test/unit/math/prim/fun/exp2_test.cpp +++ b/test/unit/math/prim/fun/exp2_test.cpp @@ -35,11 +35,3 @@ TEST(MathFunctions, exp2_nan) { EXPECT_TRUE(std::isnan(stan::math::exp2(nan))); } - -TEST(MathFunctions, exp2_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::exp2(b)); -} diff --git a/test/unit/math/prim/fun/exp_test.cpp b/test/unit/math/prim/fun/exp_test.cpp index 292f72baf10..a19d8643a9f 100644 --- a/test/unit/math/prim/fun/exp_test.cpp +++ b/test/unit/math/prim/fun/exp_test.cpp @@ -7,11 +7,3 @@ TEST(MathFunctions, expInt) { EXPECT_FLOAT_EQ(std::exp(3), exp(3)); EXPECT_FLOAT_EQ(std::exp(3.0), exp(3.0)); } - -TEST(MathFunctions, exp_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::exp(b)); -} diff --git a/test/unit/math/prim/fun/expm1_test.cpp b/test/unit/math/prim/fun/expm1_test.cpp index a92bbc91f45..3e35d9873c1 100644 --- a/test/unit/math/prim/fun/expm1_test.cpp +++ b/test/unit/math/prim/fun/expm1_test.cpp @@ -23,11 +23,3 @@ TEST(MathFunctions, expm1_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::expm1(nan))); } - -TEST(MathFunctions, expm1_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::expm1(b)); -} diff --git a/test/unit/math/prim/fun/fabs_test.cpp b/test/unit/math/prim/fun/fabs_test.cpp index e4450169d0a..985f1426eda 100644 --- a/test/unit/math/prim/fun/fabs_test.cpp +++ b/test/unit/math/prim/fun/fabs_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, fabs) { stan::test::expect_common_prim([](auto x) { return std::fabs(x); }, [](auto x) { return stan::math::fabs(x); }); } - -TEST(MathFunctions, fabs_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::fabs(b)); -} diff --git a/test/unit/math/prim/fun/floor_test.cpp b/test/unit/math/prim/fun/floor_test.cpp index 90c6dca6090..6c283c47dbc 100644 --- a/test/unit/math/prim/fun/floor_test.cpp +++ b/test/unit/math/prim/fun/floor_test.cpp @@ -4,11 +4,3 @@ TEST(primScalFun, floor) { stan::test::expect_common_prim([](auto x) { return std::floor(x); }, [](auto x) { return stan::math::floor(x); }); } - -TEST(MathFunctions, floor_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::floor(b)); -} diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index 9538f7151d6..13d81adbb7f 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -34,11 +34,3 @@ TEST(MathFunctions, inv_Phi_nan) { EXPECT_THROW(inv_Phi(-2.0), std::domain_error); EXPECT_THROW(inv_Phi(2.0), std::domain_error); } - -TEST(MathFunctions, inv_Phi_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::inv_Phi(b)); -} diff --git a/test/unit/math/prim/fun/inv_cloglog_test.cpp b/test/unit/math/prim/fun/inv_cloglog_test.cpp index 3f8c3fa6c7b..67118b3b9e6 100644 --- a/test/unit/math/prim/fun/inv_cloglog_test.cpp +++ b/test/unit/math/prim/fun/inv_cloglog_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, inv_cloglog_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_cloglog(nan))); } - -TEST(MathFunctions, inv_cloglog_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_cloglog(b)); -} diff --git a/test/unit/math/prim/fun/inv_logit_test.cpp b/test/unit/math/prim/fun/inv_logit_test.cpp index a7e272f19b5..b1e0de78766 100644 --- a/test/unit/math/prim/fun/inv_logit_test.cpp +++ b/test/unit/math/prim/fun/inv_logit_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_logit(nan))); } - -TEST(MathFunctions, inv_logit_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_logit(b)); -} diff --git a/test/unit/math/prim/fun/inv_sqrt_test.cpp b/test/unit/math/prim/fun/inv_sqrt_test.cpp index 1a7deb94def..645957931a2 100644 --- a/test/unit/math/prim/fun/inv_sqrt_test.cpp +++ b/test/unit/math/prim/fun/inv_sqrt_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, inv_sqrt_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_sqrt(nan))); } - -TEST(MathFunctions, inv_sqrt_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_sqrt(b)); -} diff --git a/test/unit/math/prim/fun/inv_square_test.cpp b/test/unit/math/prim/fun/inv_square_test.cpp index a3cb3588a07..32c17c48cf2 100644 --- a/test/unit/math/prim/fun/inv_square_test.cpp +++ b/test/unit/math/prim/fun/inv_square_test.cpp @@ -19,11 +19,3 @@ TEST(MathFunctions, inv_square_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_square(nan))); } - -TEST(MathFunctions, inv_square_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv_square(b)); -} diff --git a/test/unit/math/prim/fun/inv_test.cpp b/test/unit/math/prim/fun/inv_test.cpp index 99f9d21a969..f418be35db1 100644 --- a/test/unit/math/prim/fun/inv_test.cpp +++ b/test/unit/math/prim/fun/inv_test.cpp @@ -19,11 +19,3 @@ TEST(MathFunctions, inv_nan) { EXPECT_TRUE(std::isnan(stan::math::inv(nan))); } - -TEST(MathFunctions, inv_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::inv(b)); -} diff --git a/test/unit/math/prim/fun/lgamma_test.cpp b/test/unit/math/prim/fun/lgamma_test.cpp index 77e52134c33..d252006d5d5 100644 --- a/test/unit/math/prim/fun/lgamma_test.cpp +++ b/test/unit/math/prim/fun/lgamma_test.cpp @@ -26,11 +26,3 @@ TEST(MathFunctions, lgamma_nan) { EXPECT_TRUE( std::isnormal(boost::math::lgamma(1.0E50, stan::math::boost_policy_t()))); } - -TEST(MathFunctions, lgamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::lgamma(b)); -} diff --git a/test/unit/math/prim/fun/log10_test.cpp b/test/unit/math/prim/fun/log10_test.cpp index 4e3e79e94fc..1a67f75a75c 100644 --- a/test/unit/math/prim/fun/log10_test.cpp +++ b/test/unit/math/prim/fun/log10_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, log10) { stan::test::expect_common_prim([](auto x) { return std::log10(x); }, [](auto x) { return stan::math::log10(x); }); } - -TEST(MathFunctions, log10_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log10(b)); -} diff --git a/test/unit/math/prim/fun/log1m_test.cpp b/test/unit/math/prim/fun/log1m_test.cpp index 2c68c5a825e..a0cf248f987 100644 --- a/test/unit/math/prim/fun/log1m_test.cpp +++ b/test/unit/math/prim/fun/log1m_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, log1m_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1m(nan))); } - -TEST(MathFunctions, log1m_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 0.1, 0.2, 0.3, 0.4, 0.5; - Eigen::RowVectorXd b(5); - b << 0.1, 0.2, 0.3, 0.4, 0.5; - stan::math::multiply(a, stan::math::log1m(b)); -} diff --git a/test/unit/math/prim/fun/log1p_exp_test.cpp b/test/unit/math/prim/fun/log1p_exp_test.cpp index aa4b3fd0303..6930db3a1eb 100644 --- a/test/unit/math/prim/fun/log1p_exp_test.cpp +++ b/test/unit/math/prim/fun/log1p_exp_test.cpp @@ -16,11 +16,3 @@ TEST(MathFunctions, log1p_exp_nan) { EXPECT_TRUE(std::isnan(stan::math::log1p_exp(nan))); } - -TEST(MathFunctions, log1p_exp_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log1p_exp(b)); -} diff --git a/test/unit/math/prim/fun/log1p_test.cpp b/test/unit/math/prim/fun/log1p_test.cpp index 714958a1745..35f90f46452 100644 --- a/test/unit/math/prim/fun/log1p_test.cpp +++ b/test/unit/math/prim/fun/log1p_test.cpp @@ -40,11 +40,3 @@ TEST(MathFunctions, log1p_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1p(nan))); } - -TEST(MathFunctions, log1p_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log1p(b)); -} diff --git a/test/unit/math/prim/fun/log2_test.cpp b/test/unit/math/prim/fun/log2_test.cpp index cf726f78ceb..031a50afa97 100644 --- a/test/unit/math/prim/fun/log2_test.cpp +++ b/test/unit/math/prim/fun/log2_test.cpp @@ -22,11 +22,3 @@ TEST(MathFunctions, log2_nan) { EXPECT_TRUE(std::isnan(stan::math::log2(nan))); } - -TEST(MathFunctions, log2_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log2(b)); -} diff --git a/test/unit/math/prim/fun/log_inv_logit_test.cpp b/test/unit/math/prim/fun/log_inv_logit_test.cpp index bb13d370b2a..774d044a4dd 100644 --- a/test/unit/math/prim/fun/log_inv_logit_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_test.cpp @@ -18,11 +18,3 @@ TEST(MathFunctions, log_inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::log_inv_logit(nan))); } - -TEST(MathFunctions, log_inv_logit_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log_inv_logit(b)); -} diff --git a/test/unit/math/prim/fun/log_softmax_test.cpp b/test/unit/math/prim/fun/log_softmax_test.cpp index eff97db3cd9..ba26ba1ce20 100644 --- a/test/unit/math/prim/fun/log_softmax_test.cpp +++ b/test/unit/math/prim/fun/log_softmax_test.cpp @@ -47,11 +47,3 @@ TEST(MathMatrixPrimMat, log_softmax_exception) { EXPECT_THROW(log_softmax(v0), std::invalid_argument); } - -TEST(MathFunctions, log_softmax_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log_softmax(b)); -} diff --git a/test/unit/math/prim/fun/log_test.cpp b/test/unit/math/prim/fun/log_test.cpp index f7163dbcf1a..77563092631 100644 --- a/test/unit/math/prim/fun/log_test.cpp +++ b/test/unit/math/prim/fun/log_test.cpp @@ -7,11 +7,3 @@ TEST(MathFunctions, logInt) { EXPECT_FLOAT_EQ(std::log(3), log(3)); EXPECT_FLOAT_EQ(std::log(3.0), log(3.0)); } - -TEST(MathFunctions, log_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log(b)); -} diff --git a/test/unit/math/prim/fun/logit_test.cpp b/test/unit/math/prim/fun/logit_test.cpp index 566cbcd88ad..b6820aa4b7f 100644 --- a/test/unit/math/prim/fun/logit_test.cpp +++ b/test/unit/math/prim/fun/logit_test.cpp @@ -14,11 +14,3 @@ TEST(MathFunctions, logit_nan) { EXPECT_TRUE(std::isnan(stan::math::logit(nan))); } - -TEST(MathFunctions, logit_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::logit(b)); -} diff --git a/test/unit/math/prim/fun/minus_test.cpp b/test/unit/math/prim/fun/minus_test.cpp index d3dd9bdad85..e9535ca4781 100644 --- a/test/unit/math/prim/fun/minus_test.cpp +++ b/test/unit/math/prim/fun/minus_test.cpp @@ -10,11 +10,3 @@ TEST(MathMatrixPrimMat, minus) { EXPECT_EQ(0, stan::math::minus(rv0).size()); EXPECT_EQ(0, stan::math::minus(m0).size()); } - -TEST(MathFunctions, minus_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::minus(b)); -} diff --git a/test/unit/math/prim/fun/round_test.cpp b/test/unit/math/prim/fun/round_test.cpp index 3cabf152e67..40cc1a8334f 100644 --- a/test/unit/math/prim/fun/round_test.cpp +++ b/test/unit/math/prim/fun/round_test.cpp @@ -17,11 +17,3 @@ TEST(MathFunctions, roundNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::round(nan))); } - -TEST(MathFunctions, round_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::round(b)); -} diff --git a/test/unit/math/prim/fun/sin_test.cpp b/test/unit/math/prim/fun/sin_test.cpp index 4125f5e22f8..3a564bfea3b 100644 --- a/test/unit/math/prim/fun/sin_test.cpp +++ b/test/unit/math/prim/fun/sin_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, sin) { stan::test::expect_common_prim([](auto x) { return std::sin(x); }, [](auto x) { return stan::math::sin(x); }); } - -TEST(MathFunctions, sin_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::sin(b)); -} diff --git a/test/unit/math/prim/fun/sinh_test.cpp b/test/unit/math/prim/fun/sinh_test.cpp index 72e3f143e5d..72b18d36f94 100644 --- a/test/unit/math/prim/fun/sinh_test.cpp +++ b/test/unit/math/prim/fun/sinh_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, sinh) { stan::test::expect_common_prim([](auto x) { return std::sinh(x); }, [](auto x) { return stan::math::sinh(x); }); } - -TEST(MathFunctions, sinh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::sinh(b)); -} diff --git a/test/unit/math/prim/fun/sqrt_test.cpp b/test/unit/math/prim/fun/sqrt_test.cpp index 9896911dbf5..43c362d0cc9 100644 --- a/test/unit/math/prim/fun/sqrt_test.cpp +++ b/test/unit/math/prim/fun/sqrt_test.cpp @@ -7,11 +7,3 @@ TEST(MathFunctions, sqrtInt) { EXPECT_FLOAT_EQ(std::sqrt(3.0), sqrt(3)); EXPECT_TRUE(stan::math::is_nan(sqrt(-2))); } - -TEST(MathFunctions, sqrt_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::sqrt(b)); -} diff --git a/test/unit/math/prim/fun/square_test.cpp b/test/unit/math/prim/fun/square_test.cpp index 13b66189bcf..aadd3009906 100644 --- a/test/unit/math/prim/fun/square_test.cpp +++ b/test/unit/math/prim/fun/square_test.cpp @@ -3,7 +3,7 @@ #include #include -TEST(MathFunctions, square) { +TEST(MathsFunctions, square) { double y = 2.0; EXPECT_FLOAT_EQ(y * y, stan::math::square(y)); @@ -19,11 +19,3 @@ TEST(MathFunctions, square_nan) { EXPECT_TRUE(std::isnan(stan::math::square(nan))); } - -TEST(MathFunctions, square_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::square(b)); -} diff --git a/test/unit/math/prim/fun/tan_test.cpp b/test/unit/math/prim/fun/tan_test.cpp deleted file mode 100644 index 5b9e861cabc..00000000000 --- a/test/unit/math/prim/fun/tan_test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -TEST(MathFunctions, tan_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::tan(b)); -} diff --git a/test/unit/math/prim/fun/tanh_test.cpp b/test/unit/math/prim/fun/tanh_test.cpp index 594d0e37ed5..bd4da5755c9 100644 --- a/test/unit/math/prim/fun/tanh_test.cpp +++ b/test/unit/math/prim/fun/tanh_test.cpp @@ -5,11 +5,3 @@ TEST(primScalFun, tanh) { stan::test::expect_common_prim([](auto x) { return std::tanh(x); }, [](auto x) { return stan::math::tanh(x); }); } - -TEST(MathFunctions, tanh_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::tanh(b)); -} diff --git a/test/unit/math/prim/fun/tgamma_test.cpp b/test/unit/math/prim/fun/tgamma_test.cpp index 2ecb0bd51cc..51e5b84261c 100644 --- a/test/unit/math/prim/fun/tgamma_test.cpp +++ b/test/unit/math/prim/fun/tgamma_test.cpp @@ -20,11 +20,3 @@ TEST(MathFunctions, tgamma_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::tgamma(nan))); } - -TEST(MathFunctions, tgamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::tgamma(b)); -} diff --git a/test/unit/math/prim/fun/trigamma_test.cpp b/test/unit/math/prim/fun/trigamma_test.cpp index c2d4484d1f0..d2066598d32 100644 --- a/test/unit/math/prim/fun/trigamma_test.cpp +++ b/test/unit/math/prim/fun/trigamma_test.cpp @@ -17,11 +17,3 @@ TEST(MathFunctions, trigamma_nan) { EXPECT_TRUE(std::isnan(stan::math::trigamma(nan))); } - -TEST(MathFunctions, trigamma_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::trigamma(b)); -} diff --git a/test/unit/math/prim/fun/trunc_test.cpp b/test/unit/math/prim/fun/trunc_test.cpp index 2f64da18b03..8aa3a7c6a00 100644 --- a/test/unit/math/prim/fun/trunc_test.cpp +++ b/test/unit/math/prim/fun/trunc_test.cpp @@ -17,11 +17,3 @@ TEST(MathFunctions, truncNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::trunc(nan))); } - -TEST(MathFunctions, trunc_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::trunc(b)); -} From f899a98480800c889e0b36e81a2a928a5a77a8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Ce=C5=A1novar?= Date: Wed, 29 Jan 2020 19:05:54 +0100 Subject: [PATCH 010/167] Fixed conflict in stan/math/prim/fun/log_softmax.hpp --- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 4 ++-- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 4 ++-- stan/math/prim/fun/inv_cloglog.hpp | 4 ++-- stan/math/prim/fun/inv_sqrt.hpp | 4 ++-- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log_softmax.hpp | 11 ++++++----- stan/math/prim/fun/minus.hpp | 4 +++- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 4 ++-- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- stan/math/prim/vectorize/apply_scalar_unary.hpp | 7 +++++-- test/unit/math/prim/fun/Phi_approx_test.cpp | 8 ++++++++ test/unit/math/prim/fun/Phi_test.cpp | 8 ++++++++ test/unit/math/prim/fun/acos_test.cpp | 8 ++++++++ test/unit/math/prim/fun/acosh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/asin_test.cpp | 8 ++++++++ test/unit/math/prim/fun/asinh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/atan_test.cpp | 8 ++++++++ test/unit/math/prim/fun/atanh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cbrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/ceil_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cos_test.cpp | 8 ++++++++ test/unit/math/prim/fun/cosh_test.cpp | 10 ++++++++++ test/unit/math/prim/fun/digamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/erf_test.cpp | 8 ++++++++ test/unit/math/prim/fun/erfc_test.cpp | 8 ++++++++ test/unit/math/prim/fun/exp2_test.cpp | 8 ++++++++ test/unit/math/prim/fun/exp_test.cpp | 8 ++++++++ test/unit/math/prim/fun/expm1_test.cpp | 8 ++++++++ test/unit/math/prim/fun/fabs_test.cpp | 8 ++++++++ test/unit/math/prim/fun/floor_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_Phi_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_cloglog_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_sqrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_square_test.cpp | 8 ++++++++ test/unit/math/prim/fun/inv_test.cpp | 8 ++++++++ test/unit/math/prim/fun/lgamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log10_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1m_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1p_exp_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log1p_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log2_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_inv_logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_softmax_test.cpp | 8 ++++++++ test/unit/math/prim/fun/log_test.cpp | 8 ++++++++ test/unit/math/prim/fun/logit_test.cpp | 8 ++++++++ test/unit/math/prim/fun/minus_test.cpp | 8 ++++++++ test/unit/math/prim/fun/round_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sin_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sinh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/sqrt_test.cpp | 8 ++++++++ test/unit/math/prim/fun/square_test.cpp | 10 +++++++++- test/unit/math/prim/fun/tan_test.cpp | 10 ++++++++++ test/unit/math/prim/fun/tanh_test.cpp | 8 ++++++++ test/unit/math/prim/fun/tgamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/trigamma_test.cpp | 8 ++++++++ test/unit/math/prim/fun/trunc_test.cpp | 8 ++++++++ 70 files changed, 420 insertions(+), 34 deletions(-) create mode 100644 test/unit/math/prim/fun/cosh_test.cpp create mode 100644 test/unit/math/prim/fun/tan_test.cpp diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 709864e1db6..e5960f1b758 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -45,7 +45,7 @@ inline auto acos(const T& x) { template > inline auto acos(const Eigen::MatrixBase& x) { - return x.derived().array().acos().matrix(); + return x.derived().array().acos().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 81a22dcf60b..0f5d172a29b 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -45,7 +45,7 @@ inline auto asin(const T& x) { template > inline auto asin(const Eigen::MatrixBase& x) { - return x.derived().array().asin().matrix(); + return x.derived().array().asin().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 28a05b08d6a..9576c99240a 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t atan(const T& x) { template > inline auto atan(const Eigen::MatrixBase& x) { - return x.derived().array().atan().matrix(); + return x.derived().array().atan().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 7a78566ac1d..2de0ddb72e7 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -45,7 +45,7 @@ inline auto ceil(const T& x) { template > inline auto ceil(const Eigen::MatrixBase& x) { - return x.derived().array().ceil().matrix(); + return x.derived().array().ceil().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 0c20e53f935..bf9761ace9f 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -45,7 +45,7 @@ inline auto cos(const T& x) { template > inline auto cos(const Eigen::MatrixBase& x) { - return x.derived().array().cos().matrix(); + return x.derived().array().cos().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 3dee3eae2a3..2a0402fe9c2 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t cosh(const T& x) { template > inline auto cosh(const Eigen::MatrixBase& x) { - return x.derived().array().cosh().matrix(); + return x.derived().array().cosh().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 4d60b4c04b5..54486d9e394 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -60,7 +60,7 @@ inline auto exp(const T& x) { template > inline auto exp(const Eigen::MatrixBase& x) { - return x.derived().array().exp().matrix(); + return x.derived().array().exp().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 1dc51b0ee3c..2bfc97071d2 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -45,7 +45,7 @@ inline typename apply_scalar_unary::return_t fabs(const T& x) { template > inline auto fabs(const Eigen::MatrixBase& x) { - return x.derived().array().abs().matrix(); + return x.derived().array().abs().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto fabs(const Eigen::MatrixBase& x) { template > inline auto fabs(const Eigen::ArrayBase& x) { - return x.derived().abs(); + return x.derived().abs().eval(); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 7f1f37317cd..c53433cf356 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -45,7 +45,7 @@ inline auto floor(const T& x) { template > inline auto floor(const Eigen::MatrixBase& x) { - return x.derived().array().floor().matrix(); + return x.derived().array().floor().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index f50f17f784c..439dc962778 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -45,7 +45,7 @@ inline auto inv(const T& x) { template > inline auto inv(const Eigen::MatrixBase& x) { - return x.derived().array().inverse().matrix(); + return x.derived().array().inverse().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto inv(const Eigen::MatrixBase& x) { template > inline auto inv(const Eigen::ArrayBase& x) { - return x.derived().inverse(); + return x.derived().inverse().eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 945417aa0f9..44c6dfdc8d7 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -87,7 +87,7 @@ inline auto inv_cloglog(const T& x) { template > inline auto inv_cloglog(const Eigen::MatrixBase& x) { - return (1 - exp(-exp(x.derived().array()))).matrix(); + return (1 - exp(-exp(x.derived().array()))).matrix().eval(); } /** @@ -99,7 +99,7 @@ inline auto inv_cloglog(const Eigen::MatrixBase& x) { template > inline auto inv_cloglog(const Eigen::ArrayBase& x) { - return 1 - exp(-exp(x.derived())); + return (1 - exp(-exp(x.derived()))).eval(); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index aa6e0d7ffdf..66b41ee7138 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -50,7 +50,7 @@ inline auto inv_sqrt(const T& x) { template > inline auto inv_sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().rsqrt().matrix(); + return x.derived().array().rsqrt().matrix().eval(); } /** @@ -62,7 +62,7 @@ inline auto inv_sqrt(const Eigen::MatrixBase& x) { template > inline auto inv_sqrt(const Eigen::ArrayBase& x) { - return x.derived().rsqrt(); + return x.derived().rsqrt().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 2e32bc5aa4b..3814d0a5ae5 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -60,7 +60,7 @@ inline auto log(const T& x) { template > inline auto log(const Eigen::MatrixBase& x) { - return x.derived().array().log().matrix(); + return x.derived().array().log().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 808bdb85bad..10b7b0fa1b0 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -45,7 +45,7 @@ inline auto log10(const T& x) { template > inline auto log10(const Eigen::MatrixBase& x) { - return x.derived().array().log10().matrix(); + return x.derived().array().log10().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index a51370d0b1a..8c4951df779 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -36,11 +36,12 @@ namespace math { * @param[in] v Vector to transform. * @return Unit simplex result of the softmax transform of the vector. */ -template -inline Eigen::Matrix log_softmax( - const Eigen::Matrix& v) { - check_nonzero_size("log_softmax", "v", v); - return v.array() - log_sum_exp(v); +template >>...> +inline auto log_softmax(const T& x) { + return apply_vector_unary::apply(x, [&](const auto& v) { + check_nonzero_size("log_softmax", "v", v); + return (v.array() - log_sum_exp(v)).matrix().eval(); + }); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 7f374462fc0..13fe7d090ed 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -1,6 +1,8 @@ #ifndef STAN_MATH_PRIM_FUN_MINUS_HPP #define STAN_MATH_PRIM_FUN_MINUS_HPP +#include + namespace stan { namespace math { @@ -12,7 +14,7 @@ namespace math { * @return Negation of subtrahend. */ template -inline auto minus(const T& x) { +inline plain_type_t minus(const T& x) { return -x; } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 66b2d6a428c..e5cc48efd18 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -62,7 +62,7 @@ inline auto round(const T& x) { template > inline auto round(const Eigen::MatrixBase& x) { - return x.derived().array().round().matrix(); + return x.derived().array().round().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index f2ed9102f97..0b783ffeb20 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -45,7 +45,7 @@ inline auto sin(const T& x) { template > inline auto sin(const Eigen::MatrixBase& x) { - return x.derived().array().sin().matrix(); + return x.derived().array().sin().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index 5e13a03993e..e4d1252295d 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -45,7 +45,7 @@ inline auto sinh(const T& x) { template > inline auto sinh(const Eigen::MatrixBase& x) { - return x.derived().array().sinh().matrix(); + return x.derived().array().sinh().matrix().eval(); } /** @@ -57,7 +57,7 @@ inline auto sinh(const Eigen::MatrixBase& x) { template > inline auto sinh(const Eigen::ArrayBase& x) { - return x.derived().sinh(); + return x.derived().sinh().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index ae1b6dace93..fe81fcf75b6 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -54,7 +54,7 @@ inline auto sqrt(const T& x) { template > inline auto sqrt(const Eigen::MatrixBase& x) { - return x.derived().array().sqrt().matrix(); + return x.derived().array().sqrt().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index 7064300e695..efc03beb94b 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -45,7 +45,7 @@ inline auto tan(const T& x) { template > inline auto tan(const Eigen::MatrixBase& x) { - return x.derived().array().tan().matrix(); + return x.derived().array().tan().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 60c4d405fb2..2fe9f4c5a25 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -45,7 +45,7 @@ inline auto tanh(const T& x) { template > inline auto tanh(const Eigen::MatrixBase& x) { - return x.derived().array().tanh().matrix(); + return x.derived().array().tanh().matrix().eval(); } } // namespace math diff --git a/stan/math/prim/vectorize/apply_scalar_unary.hpp b/stan/math/prim/vectorize/apply_scalar_unary.hpp index ed31037a207..8cc283cd4ac 100644 --- a/stan/math/prim/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/vectorize/apply_scalar_unary.hpp @@ -59,8 +59,11 @@ struct apply_scalar_unary> { * by F to the specified matrix. */ static inline auto apply(const T& x) { - return x.unaryExpr( - [](scalar_t x) { return apply_scalar_unary::apply(x); }); + return x + .unaryExpr([](scalar_t x) { + return apply_scalar_unary::apply(x); + }) + .eval(); } /** diff --git a/test/unit/math/prim/fun/Phi_approx_test.cpp b/test/unit/math/prim/fun/Phi_approx_test.cpp index dbc4078352d..4a29923c572 100644 --- a/test/unit/math/prim/fun/Phi_approx_test.cpp +++ b/test/unit/math/prim/fun/Phi_approx_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, Phi_approx_nan) { EXPECT_TRUE(std::isnan(stan::math::Phi_approx(nan))); } + +TEST(MathFunctions, Phi_approx__works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::Phi_approx(b)); +} diff --git a/test/unit/math/prim/fun/Phi_test.cpp b/test/unit/math/prim/fun/Phi_test.cpp index c43e260bf88..1273ad24628 100644 --- a/test/unit/math/prim/fun/Phi_test.cpp +++ b/test/unit/math/prim/fun/Phi_test.cpp @@ -118,3 +118,11 @@ TEST(MathFunctions, Phi_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_THROW(stan::math::Phi(nan), std::domain_error); } + +TEST(MathFunctions, Phi_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::Phi(b)); +} diff --git a/test/unit/math/prim/fun/acos_test.cpp b/test/unit/math/prim/fun/acos_test.cpp index c66c4c49a02..7570f52cc71 100644 --- a/test/unit/math/prim/fun/acos_test.cpp +++ b/test/unit/math/prim/fun/acos_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, acos) { stan::test::expect_common_prim([](auto x) { return std::acos(x); }, [](auto x) { return stan::math::acos(x); }); } + +TEST(MathFunctions, acos_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::acos(b)); +} diff --git a/test/unit/math/prim/fun/acosh_test.cpp b/test/unit/math/prim/fun/acosh_test.cpp index 2d5bc742d3c..1f9e8ab7bb5 100644 --- a/test/unit/math/prim/fun/acosh_test.cpp +++ b/test/unit/math/prim/fun/acosh_test.cpp @@ -26,3 +26,11 @@ TEST(MathFunctions, acosh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::acosh(nan))); } + +TEST(MathFunctions, acosh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::acosh(b)); +} diff --git a/test/unit/math/prim/fun/asin_test.cpp b/test/unit/math/prim/fun/asin_test.cpp index a14245b58c2..858be5fbbbd 100644 --- a/test/unit/math/prim/fun/asin_test.cpp +++ b/test/unit/math/prim/fun/asin_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, asin) { stan::test::expect_common_prim([](auto x) { return std::asin(x); }, [](auto x) { return stan::math::asin(x); }); } + +TEST(MathFunctions, asin_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::asin(b)); +} diff --git a/test/unit/math/prim/fun/asinh_test.cpp b/test/unit/math/prim/fun/asinh_test.cpp index 151afc2b3ef..e10228b0af0 100644 --- a/test/unit/math/prim/fun/asinh_test.cpp +++ b/test/unit/math/prim/fun/asinh_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, asinh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::asinh(nan))); } + +TEST(MathFunctions, asinh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::asinh(b)); +} diff --git a/test/unit/math/prim/fun/atan_test.cpp b/test/unit/math/prim/fun/atan_test.cpp index 467ceefff7d..54861be5151 100644 --- a/test/unit/math/prim/fun/atan_test.cpp +++ b/test/unit/math/prim/fun/atan_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, atan) { stan::test::expect_common_prim([](auto x) { return std::atan(x); }, [](auto x) { return stan::math::atan(x); }); } + +TEST(MathFunctions, atan_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::atan(b)); +} diff --git a/test/unit/math/prim/fun/atanh_test.cpp b/test/unit/math/prim/fun/atanh_test.cpp index ca60e585e86..18ae151e23c 100644 --- a/test/unit/math/prim/fun/atanh_test.cpp +++ b/test/unit/math/prim/fun/atanh_test.cpp @@ -27,3 +27,11 @@ TEST(MathFunctions, atanh_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::atanh(nan))); } + +TEST(MathFunctions, atanh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::atanh(b)); +} diff --git a/test/unit/math/prim/fun/cbrt_test.cpp b/test/unit/math/prim/fun/cbrt_test.cpp index 2ca66bbde53..854853943a6 100644 --- a/test/unit/math/prim/fun/cbrt_test.cpp +++ b/test/unit/math/prim/fun/cbrt_test.cpp @@ -24,3 +24,11 @@ TEST(MathFunctions, cbrt_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::cbrt(nan))); } + +TEST(MathFunctions, cbrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cbrt(b)); +} diff --git a/test/unit/math/prim/fun/ceil_test.cpp b/test/unit/math/prim/fun/ceil_test.cpp index f26f0068eb1..57a7dce8c41 100644 --- a/test/unit/math/prim/fun/ceil_test.cpp +++ b/test/unit/math/prim/fun/ceil_test.cpp @@ -4,3 +4,11 @@ TEST(primScalFun, ceil) { stan::test::expect_common_prim([](auto x) { return std::ceil(x); }, [](auto x) { return stan::math::ceil(x); }); } + +TEST(MathFunctions, ceil_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::ceil(b)); +} diff --git a/test/unit/math/prim/fun/cos_test.cpp b/test/unit/math/prim/fun/cos_test.cpp index 53daaa398de..94989f9703f 100644 --- a/test/unit/math/prim/fun/cos_test.cpp +++ b/test/unit/math/prim/fun/cos_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, cos) { stan::test::expect_common_prim([](auto x) { return std::cos(x); }, [](auto x) { return stan::math::cos(x); }); } + +TEST(MathFunctions, cos_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cos(b)); +} diff --git a/test/unit/math/prim/fun/cosh_test.cpp b/test/unit/math/prim/fun/cosh_test.cpp new file mode 100644 index 00000000000..d1433637b36 --- /dev/null +++ b/test/unit/math/prim/fun/cosh_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +TEST(MathFunctions, cosh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::cosh(b)); +} diff --git a/test/unit/math/prim/fun/digamma_test.cpp b/test/unit/math/prim/fun/digamma_test.cpp index 2a3a7ea150b..b0d37f98b7e 100644 --- a/test/unit/math/prim/fun/digamma_test.cpp +++ b/test/unit/math/prim/fun/digamma_test.cpp @@ -18,3 +18,11 @@ TEST(MathFunctions, digamma_nan) { EXPECT_TRUE(std::isnormal(stan::math::digamma(1.0E50))); } + +TEST(MathFunctions, digamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::digamma(b)); +} diff --git a/test/unit/math/prim/fun/erf_test.cpp b/test/unit/math/prim/fun/erf_test.cpp index f01b192b8be..0c06057587c 100644 --- a/test/unit/math/prim/fun/erf_test.cpp +++ b/test/unit/math/prim/fun/erf_test.cpp @@ -20,3 +20,11 @@ TEST(MathFunctions, erfNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erf(nan))); } + +TEST(MathFunctions, erf_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::erf(b)); +} diff --git a/test/unit/math/prim/fun/erfc_test.cpp b/test/unit/math/prim/fun/erfc_test.cpp index aad40784135..2e19d0fe333 100644 --- a/test/unit/math/prim/fun/erfc_test.cpp +++ b/test/unit/math/prim/fun/erfc_test.cpp @@ -21,3 +21,11 @@ TEST(MathFunctions, erfcNan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::erfc(nan))); } + +TEST(MathFunctions, erfc_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::erfc(b)); +} diff --git a/test/unit/math/prim/fun/exp2_test.cpp b/test/unit/math/prim/fun/exp2_test.cpp index ae1c93c1e22..b5c8f356425 100644 --- a/test/unit/math/prim/fun/exp2_test.cpp +++ b/test/unit/math/prim/fun/exp2_test.cpp @@ -35,3 +35,11 @@ TEST(MathFunctions, exp2_nan) { EXPECT_TRUE(std::isnan(stan::math::exp2(nan))); } + +TEST(MathFunctions, exp2_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::exp2(b)); +} diff --git a/test/unit/math/prim/fun/exp_test.cpp b/test/unit/math/prim/fun/exp_test.cpp index a19d8643a9f..292f72baf10 100644 --- a/test/unit/math/prim/fun/exp_test.cpp +++ b/test/unit/math/prim/fun/exp_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, expInt) { EXPECT_FLOAT_EQ(std::exp(3), exp(3)); EXPECT_FLOAT_EQ(std::exp(3.0), exp(3.0)); } + +TEST(MathFunctions, exp_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::exp(b)); +} diff --git a/test/unit/math/prim/fun/expm1_test.cpp b/test/unit/math/prim/fun/expm1_test.cpp index 3e35d9873c1..a92bbc91f45 100644 --- a/test/unit/math/prim/fun/expm1_test.cpp +++ b/test/unit/math/prim/fun/expm1_test.cpp @@ -23,3 +23,11 @@ TEST(MathFunctions, expm1_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::expm1(nan))); } + +TEST(MathFunctions, expm1_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::expm1(b)); +} diff --git a/test/unit/math/prim/fun/fabs_test.cpp b/test/unit/math/prim/fun/fabs_test.cpp index 985f1426eda..e4450169d0a 100644 --- a/test/unit/math/prim/fun/fabs_test.cpp +++ b/test/unit/math/prim/fun/fabs_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, fabs) { stan::test::expect_common_prim([](auto x) { return std::fabs(x); }, [](auto x) { return stan::math::fabs(x); }); } + +TEST(MathFunctions, fabs_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::fabs(b)); +} diff --git a/test/unit/math/prim/fun/floor_test.cpp b/test/unit/math/prim/fun/floor_test.cpp index 6c283c47dbc..90c6dca6090 100644 --- a/test/unit/math/prim/fun/floor_test.cpp +++ b/test/unit/math/prim/fun/floor_test.cpp @@ -4,3 +4,11 @@ TEST(primScalFun, floor) { stan::test::expect_common_prim([](auto x) { return std::floor(x); }, [](auto x) { return stan::math::floor(x); }); } + +TEST(MathFunctions, floor_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::floor(b)); +} diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index 13d81adbb7f..9538f7151d6 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -34,3 +34,11 @@ TEST(MathFunctions, inv_Phi_nan) { EXPECT_THROW(inv_Phi(-2.0), std::domain_error); EXPECT_THROW(inv_Phi(2.0), std::domain_error); } + +TEST(MathFunctions, inv_Phi_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::inv_Phi(b)); +} diff --git a/test/unit/math/prim/fun/inv_cloglog_test.cpp b/test/unit/math/prim/fun/inv_cloglog_test.cpp index 67118b3b9e6..3f8c3fa6c7b 100644 --- a/test/unit/math/prim/fun/inv_cloglog_test.cpp +++ b/test/unit/math/prim/fun/inv_cloglog_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, inv_cloglog_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_cloglog(nan))); } + +TEST(MathFunctions, inv_cloglog_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_cloglog(b)); +} diff --git a/test/unit/math/prim/fun/inv_logit_test.cpp b/test/unit/math/prim/fun/inv_logit_test.cpp index b1e0de78766..a7e272f19b5 100644 --- a/test/unit/math/prim/fun/inv_logit_test.cpp +++ b/test/unit/math/prim/fun/inv_logit_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_logit(nan))); } + +TEST(MathFunctions, inv_logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_logit(b)); +} diff --git a/test/unit/math/prim/fun/inv_sqrt_test.cpp b/test/unit/math/prim/fun/inv_sqrt_test.cpp index 645957931a2..1a7deb94def 100644 --- a/test/unit/math/prim/fun/inv_sqrt_test.cpp +++ b/test/unit/math/prim/fun/inv_sqrt_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, inv_sqrt_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_sqrt(nan))); } + +TEST(MathFunctions, inv_sqrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_sqrt(b)); +} diff --git a/test/unit/math/prim/fun/inv_square_test.cpp b/test/unit/math/prim/fun/inv_square_test.cpp index 32c17c48cf2..a3cb3588a07 100644 --- a/test/unit/math/prim/fun/inv_square_test.cpp +++ b/test/unit/math/prim/fun/inv_square_test.cpp @@ -19,3 +19,11 @@ TEST(MathFunctions, inv_square_nan) { EXPECT_TRUE(std::isnan(stan::math::inv_square(nan))); } + +TEST(MathFunctions, inv_square_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv_square(b)); +} diff --git a/test/unit/math/prim/fun/inv_test.cpp b/test/unit/math/prim/fun/inv_test.cpp index f418be35db1..99f9d21a969 100644 --- a/test/unit/math/prim/fun/inv_test.cpp +++ b/test/unit/math/prim/fun/inv_test.cpp @@ -19,3 +19,11 @@ TEST(MathFunctions, inv_nan) { EXPECT_TRUE(std::isnan(stan::math::inv(nan))); } + +TEST(MathFunctions, inv_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::inv(b)); +} diff --git a/test/unit/math/prim/fun/lgamma_test.cpp b/test/unit/math/prim/fun/lgamma_test.cpp index d252006d5d5..77e52134c33 100644 --- a/test/unit/math/prim/fun/lgamma_test.cpp +++ b/test/unit/math/prim/fun/lgamma_test.cpp @@ -26,3 +26,11 @@ TEST(MathFunctions, lgamma_nan) { EXPECT_TRUE( std::isnormal(boost::math::lgamma(1.0E50, stan::math::boost_policy_t()))); } + +TEST(MathFunctions, lgamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::lgamma(b)); +} diff --git a/test/unit/math/prim/fun/log10_test.cpp b/test/unit/math/prim/fun/log10_test.cpp index 1a67f75a75c..4e3e79e94fc 100644 --- a/test/unit/math/prim/fun/log10_test.cpp +++ b/test/unit/math/prim/fun/log10_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, log10) { stan::test::expect_common_prim([](auto x) { return std::log10(x); }, [](auto x) { return stan::math::log10(x); }); } + +TEST(MathFunctions, log10_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log10(b)); +} diff --git a/test/unit/math/prim/fun/log1m_test.cpp b/test/unit/math/prim/fun/log1m_test.cpp index a0cf248f987..2c68c5a825e 100644 --- a/test/unit/math/prim/fun/log1m_test.cpp +++ b/test/unit/math/prim/fun/log1m_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, log1m_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1m(nan))); } + +TEST(MathFunctions, log1m_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 0.1, 0.2, 0.3, 0.4, 0.5; + Eigen::RowVectorXd b(5); + b << 0.1, 0.2, 0.3, 0.4, 0.5; + stan::math::multiply(a, stan::math::log1m(b)); +} diff --git a/test/unit/math/prim/fun/log1p_exp_test.cpp b/test/unit/math/prim/fun/log1p_exp_test.cpp index 6930db3a1eb..aa4b3fd0303 100644 --- a/test/unit/math/prim/fun/log1p_exp_test.cpp +++ b/test/unit/math/prim/fun/log1p_exp_test.cpp @@ -16,3 +16,11 @@ TEST(MathFunctions, log1p_exp_nan) { EXPECT_TRUE(std::isnan(stan::math::log1p_exp(nan))); } + +TEST(MathFunctions, log1p_exp_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log1p_exp(b)); +} diff --git a/test/unit/math/prim/fun/log1p_test.cpp b/test/unit/math/prim/fun/log1p_test.cpp index 35f90f46452..714958a1745 100644 --- a/test/unit/math/prim/fun/log1p_test.cpp +++ b/test/unit/math/prim/fun/log1p_test.cpp @@ -40,3 +40,11 @@ TEST(MathFunctions, log1p_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::log1p(nan))); } + +TEST(MathFunctions, log1p_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log1p(b)); +} diff --git a/test/unit/math/prim/fun/log2_test.cpp b/test/unit/math/prim/fun/log2_test.cpp index 031a50afa97..cf726f78ceb 100644 --- a/test/unit/math/prim/fun/log2_test.cpp +++ b/test/unit/math/prim/fun/log2_test.cpp @@ -22,3 +22,11 @@ TEST(MathFunctions, log2_nan) { EXPECT_TRUE(std::isnan(stan::math::log2(nan))); } + +TEST(MathFunctions, log2_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log2(b)); +} diff --git a/test/unit/math/prim/fun/log_inv_logit_test.cpp b/test/unit/math/prim/fun/log_inv_logit_test.cpp index 774d044a4dd..bb13d370b2a 100644 --- a/test/unit/math/prim/fun/log_inv_logit_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_test.cpp @@ -18,3 +18,11 @@ TEST(MathFunctions, log_inv_logit_nan) { EXPECT_TRUE(std::isnan(stan::math::log_inv_logit(nan))); } + +TEST(MathFunctions, log_inv_logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log_inv_logit(b)); +} diff --git a/test/unit/math/prim/fun/log_softmax_test.cpp b/test/unit/math/prim/fun/log_softmax_test.cpp index ba26ba1ce20..eff97db3cd9 100644 --- a/test/unit/math/prim/fun/log_softmax_test.cpp +++ b/test/unit/math/prim/fun/log_softmax_test.cpp @@ -47,3 +47,11 @@ TEST(MathMatrixPrimMat, log_softmax_exception) { EXPECT_THROW(log_softmax(v0), std::invalid_argument); } + +TEST(MathFunctions, log_softmax_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log_softmax(b)); +} diff --git a/test/unit/math/prim/fun/log_test.cpp b/test/unit/math/prim/fun/log_test.cpp index 77563092631..f7163dbcf1a 100644 --- a/test/unit/math/prim/fun/log_test.cpp +++ b/test/unit/math/prim/fun/log_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, logInt) { EXPECT_FLOAT_EQ(std::log(3), log(3)); EXPECT_FLOAT_EQ(std::log(3.0), log(3.0)); } + +TEST(MathFunctions, log_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::log(b)); +} diff --git a/test/unit/math/prim/fun/logit_test.cpp b/test/unit/math/prim/fun/logit_test.cpp index b6820aa4b7f..566cbcd88ad 100644 --- a/test/unit/math/prim/fun/logit_test.cpp +++ b/test/unit/math/prim/fun/logit_test.cpp @@ -14,3 +14,11 @@ TEST(MathFunctions, logit_nan) { EXPECT_TRUE(std::isnan(stan::math::logit(nan))); } + +TEST(MathFunctions, logit_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::logit(b)); +} diff --git a/test/unit/math/prim/fun/minus_test.cpp b/test/unit/math/prim/fun/minus_test.cpp index e9535ca4781..d3dd9bdad85 100644 --- a/test/unit/math/prim/fun/minus_test.cpp +++ b/test/unit/math/prim/fun/minus_test.cpp @@ -10,3 +10,11 @@ TEST(MathMatrixPrimMat, minus) { EXPECT_EQ(0, stan::math::minus(rv0).size()); EXPECT_EQ(0, stan::math::minus(m0).size()); } + +TEST(MathFunctions, minus_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::minus(b)); +} diff --git a/test/unit/math/prim/fun/round_test.cpp b/test/unit/math/prim/fun/round_test.cpp index 40cc1a8334f..3cabf152e67 100644 --- a/test/unit/math/prim/fun/round_test.cpp +++ b/test/unit/math/prim/fun/round_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, roundNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::round(nan))); } + +TEST(MathFunctions, round_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::round(b)); +} diff --git a/test/unit/math/prim/fun/sin_test.cpp b/test/unit/math/prim/fun/sin_test.cpp index 3a564bfea3b..4125f5e22f8 100644 --- a/test/unit/math/prim/fun/sin_test.cpp +++ b/test/unit/math/prim/fun/sin_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, sin) { stan::test::expect_common_prim([](auto x) { return std::sin(x); }, [](auto x) { return stan::math::sin(x); }); } + +TEST(MathFunctions, sin_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sin(b)); +} diff --git a/test/unit/math/prim/fun/sinh_test.cpp b/test/unit/math/prim/fun/sinh_test.cpp index 72b18d36f94..72e3f143e5d 100644 --- a/test/unit/math/prim/fun/sinh_test.cpp +++ b/test/unit/math/prim/fun/sinh_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, sinh) { stan::test::expect_common_prim([](auto x) { return std::sinh(x); }, [](auto x) { return stan::math::sinh(x); }); } + +TEST(MathFunctions, sinh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sinh(b)); +} diff --git a/test/unit/math/prim/fun/sqrt_test.cpp b/test/unit/math/prim/fun/sqrt_test.cpp index 43c362d0cc9..9896911dbf5 100644 --- a/test/unit/math/prim/fun/sqrt_test.cpp +++ b/test/unit/math/prim/fun/sqrt_test.cpp @@ -7,3 +7,11 @@ TEST(MathFunctions, sqrtInt) { EXPECT_FLOAT_EQ(std::sqrt(3.0), sqrt(3)); EXPECT_TRUE(stan::math::is_nan(sqrt(-2))); } + +TEST(MathFunctions, sqrt_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::sqrt(b)); +} diff --git a/test/unit/math/prim/fun/square_test.cpp b/test/unit/math/prim/fun/square_test.cpp index aadd3009906..13b66189bcf 100644 --- a/test/unit/math/prim/fun/square_test.cpp +++ b/test/unit/math/prim/fun/square_test.cpp @@ -3,7 +3,7 @@ #include #include -TEST(MathsFunctions, square) { +TEST(MathFunctions, square) { double y = 2.0; EXPECT_FLOAT_EQ(y * y, stan::math::square(y)); @@ -19,3 +19,11 @@ TEST(MathFunctions, square_nan) { EXPECT_TRUE(std::isnan(stan::math::square(nan))); } + +TEST(MathFunctions, square_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::square(b)); +} diff --git a/test/unit/math/prim/fun/tan_test.cpp b/test/unit/math/prim/fun/tan_test.cpp new file mode 100644 index 00000000000..5b9e861cabc --- /dev/null +++ b/test/unit/math/prim/fun/tan_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +TEST(MathFunctions, tan_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tan(b)); +} diff --git a/test/unit/math/prim/fun/tanh_test.cpp b/test/unit/math/prim/fun/tanh_test.cpp index bd4da5755c9..594d0e37ed5 100644 --- a/test/unit/math/prim/fun/tanh_test.cpp +++ b/test/unit/math/prim/fun/tanh_test.cpp @@ -5,3 +5,11 @@ TEST(primScalFun, tanh) { stan::test::expect_common_prim([](auto x) { return std::tanh(x); }, [](auto x) { return stan::math::tanh(x); }); } + +TEST(MathFunctions, tanh_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tanh(b)); +} diff --git a/test/unit/math/prim/fun/tgamma_test.cpp b/test/unit/math/prim/fun/tgamma_test.cpp index 51e5b84261c..2ecb0bd51cc 100644 --- a/test/unit/math/prim/fun/tgamma_test.cpp +++ b/test/unit/math/prim/fun/tgamma_test.cpp @@ -20,3 +20,11 @@ TEST(MathFunctions, tgamma_nan) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::tgamma(nan))); } + +TEST(MathFunctions, tgamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::tgamma(b)); +} diff --git a/test/unit/math/prim/fun/trigamma_test.cpp b/test/unit/math/prim/fun/trigamma_test.cpp index d2066598d32..c2d4484d1f0 100644 --- a/test/unit/math/prim/fun/trigamma_test.cpp +++ b/test/unit/math/prim/fun/trigamma_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, trigamma_nan) { EXPECT_TRUE(std::isnan(stan::math::trigamma(nan))); } + +TEST(MathFunctions, trigamma_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::trigamma(b)); +} diff --git a/test/unit/math/prim/fun/trunc_test.cpp b/test/unit/math/prim/fun/trunc_test.cpp index 8aa3a7c6a00..2f64da18b03 100644 --- a/test/unit/math/prim/fun/trunc_test.cpp +++ b/test/unit/math/prim/fun/trunc_test.cpp @@ -17,3 +17,11 @@ TEST(MathFunctions, truncNaN) { double nan = std::numeric_limits::quiet_NaN(); EXPECT_TRUE(std::isnan(stan::math::trunc(nan))); } + +TEST(MathFunctions, trunc_works_with_other_functions) { + Eigen::VectorXd a(5); + a << 1.1, 1.2, 1.3, 1.4, 1.5; + Eigen::RowVectorXd b(5); + b << 1.1, 1.2, 1.3, 1.4, 1.5; + stan::math::multiply(a, stan::math::trunc(b)); +} From a5d71b29fea3a93256cbec1312d4400eb535b9a9 Mon Sep 17 00:00:00 2001 From: rok-cesnovar Date: Thu, 30 Jan 2020 08:25:03 +0100 Subject: [PATCH 011/167] Fixed conflict in stan/math/prim/fun/log_softmax.hpp --- stan/math/prim/fun/log_softmax.hpp | 11 +++++------ test/unit/math/prim/fun/log_softmax_test.cpp | 8 -------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index 8c4951df779..a51370d0b1a 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -36,12 +36,11 @@ namespace math { * @param[in] v Vector to transform. * @return Unit simplex result of the softmax transform of the vector. */ -template >>...> -inline auto log_softmax(const T& x) { - return apply_vector_unary::apply(x, [&](const auto& v) { - check_nonzero_size("log_softmax", "v", v); - return (v.array() - log_sum_exp(v)).matrix().eval(); - }); +template +inline Eigen::Matrix log_softmax( + const Eigen::Matrix& v) { + check_nonzero_size("log_softmax", "v", v); + return v.array() - log_sum_exp(v); } } // namespace math diff --git a/test/unit/math/prim/fun/log_softmax_test.cpp b/test/unit/math/prim/fun/log_softmax_test.cpp index eff97db3cd9..ba26ba1ce20 100644 --- a/test/unit/math/prim/fun/log_softmax_test.cpp +++ b/test/unit/math/prim/fun/log_softmax_test.cpp @@ -47,11 +47,3 @@ TEST(MathMatrixPrimMat, log_softmax_exception) { EXPECT_THROW(log_softmax(v0), std::invalid_argument); } - -TEST(MathFunctions, log_softmax_works_with_other_functions) { - Eigen::VectorXd a(5); - a << 1.1, 1.2, 1.3, 1.4, 1.5; - Eigen::RowVectorXd b(5); - b << 1.1, 1.2, 1.3, 1.4, 1.5; - stan::math::multiply(a, stan::math::log_softmax(b)); -} From e081b4ee12f00f21b99eaeee36d9c72f6f4540fa Mon Sep 17 00:00:00 2001 From: Nicusor Serban Date: Thu, 30 Jan 2020 10:33:34 +0200 Subject: [PATCH 012/167] Release v3.1.1 --- .github/ISSUE_TEMPLATE.md | 2 +- doxygen/doxygen.cfg | 2 +- stan/math/version.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 0e35de62263..8f112ab89d3 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -25,4 +25,4 @@ If this is a **feature request**, show what you expect to happen if the feature #### Current Version: -v3.1.0 +v3.1.1 diff --git a/doxygen/doxygen.cfg b/doxygen/doxygen.cfg index 8fee6f17d59..e4b5060f7d0 100644 --- a/doxygen/doxygen.cfg +++ b/doxygen/doxygen.cfg @@ -38,7 +38,7 @@ PROJECT_NAME = "Stan Math Library" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 3.1.0 +PROJECT_NUMBER = 3.1.1 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/stan/math/version.hpp b/stan/math/version.hpp index bf8599edc07..d5ba5648855 100644 --- a/stan/math/version.hpp +++ b/stan/math/version.hpp @@ -13,7 +13,7 @@ #define STAN_MATH_MAJOR 3 #define STAN_MATH_MINOR 1 -#define STAN_MATH_PATCH 0 +#define STAN_MATH_PATCH 1 namespace stan { namespace math { From ebbfeb8eb6f25223396733bcd3a58bbdfd7517d5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 27 May 2020 23:12:27 -0400 Subject: [PATCH 013/167] Adds templates to vari and var as well as the var operators. Fixes std::numeric_limits so that it conforms to the C++11 definition --- .gitignore | 3 + stan/math/prim/fun/mdivide_right_ldlt.hpp | 11 +- .../prim/fun/scale_matrix_exp_multiply.hpp | 4 +- stan/math/prim/meta.hpp | 1 + stan/math/prim/meta/is_var.hpp | 7 + stan/math/prim/meta/is_vari.hpp | 19 ++ stan/math/rev/core/chainablestack.hpp | 4 +- stan/math/rev/core/grad.hpp | 4 +- stan/math/rev/core/matrix_vari.hpp | 2 +- stan/math/rev/core/operator_addition.hpp | 10 +- stan/math/rev/core/operator_divide_equal.hpp | 9 +- stan/math/rev/core/operator_division.hpp | 8 +- stan/math/rev/core/operator_equal.hpp | 10 +- stan/math/rev/core/operator_greater_than.hpp | 10 +- .../core/operator_greater_than_or_equal.hpp | 10 +- stan/math/rev/core/operator_less_than.hpp | 10 +- .../rev/core/operator_less_than_or_equal.hpp | 10 +- stan/math/rev/core/operator_logical_and.hpp | 10 +- stan/math/rev/core/operator_logical_or.hpp | 10 +- stan/math/rev/core/operator_minus_equal.hpp | 9 +- .../math/rev/core/operator_multiplication.hpp | 10 +- .../math/rev/core/operator_multiply_equal.hpp | 9 +- stan/math/rev/core/operator_not_equal.hpp | 10 +- stan/math/rev/core/operator_plus_equal.hpp | 9 +- stan/math/rev/core/operator_subtraction.hpp | 10 +- .../math/rev/core/operator_unary_negative.hpp | 2 +- stan/math/rev/core/operator_unary_not.hpp | 2 +- stan/math/rev/core/operator_unary_plus.hpp | 2 +- stan/math/rev/core/std_iterator_traits.hpp | 11 +- stan/math/rev/core/std_numeric_limits.hpp | 87 +++--- stan/math/rev/core/var.hpp | 277 +++++++----------- stan/math/rev/core/vari.hpp | 119 ++++++-- stan/math/rev/core/vector_vari.hpp | 4 +- stan/math/rev/fun/value_of.hpp | 3 +- stan/math/rev/fun/value_of_rec.hpp | 3 +- stan/math/rev/meta.hpp | 1 + stan/math/rev/meta/is_var.hpp | 14 +- stan/math/rev/meta/is_vari.hpp | 21 ++ stan/math/rev/meta/partials_type.hpp | 2 +- test/unit/math/rev/core/var_test.cpp | 110 +++---- test/unit/math/rev/core/vari_test.cpp | 7 + test/unit/pretty_print_types.hpp | 4 +- 42 files changed, 481 insertions(+), 397 deletions(-) create mode 100644 stan/math/prim/meta/is_vari.hpp create mode 100644 stan/math/rev/meta/is_vari.hpp diff --git a/.gitignore b/.gitignore index 70b24de41cb..4e157904b86 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,6 @@ compile_commands.json # Ignore things made with tbb lib/tbb/** + +# gdb +.gdb_history diff --git a/stan/math/prim/fun/mdivide_right_ldlt.hpp b/stan/math/prim/fun/mdivide_right_ldlt.hpp index ec801f9c4b6..422f41321a5 100644 --- a/stan/math/prim/fun/mdivide_right_ldlt.hpp +++ b/stan/math/prim/fun/mdivide_right_ldlt.hpp @@ -25,7 +25,7 @@ namespace math { */ template * = nullptr, - require_any_not_vt_same* = nullptr> + require_any_not_arithmetic_t, T>* = nullptr> inline Eigen::Matrix, EigMat::RowsAtCompileTime, C> mdivide_right_ldlt(const EigMat& b, const LDLT_factor& A) { check_multiplicable("mdivide_right_ldlt", "b", b, "A", A); @@ -36,10 +36,11 @@ mdivide_right_ldlt(const EigMat& b, const LDLT_factor& A) { return transpose(mdivide_left_ldlt(A, transpose(b))); } -template * = nullptr, - require_vt_same* = nullptr> -inline Eigen::Matrix mdivide_right_ldlt( - const EigMat& b, const LDLT_factor& A) { +template * = nullptr, + require_all_arithmetic_t, T>* = nullptr> +inline Eigen::Matrix mdivide_right_ldlt( + const EigMat& b, const LDLT_factor& A) { check_multiplicable("mdivide_right_ldlt", "b", b, "A", A); if (A.rows() == 0) { return {b.rows(), 0}; diff --git a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp index 1c025d7d47d..577248b9b05 100644 --- a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp +++ b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp @@ -26,7 +26,7 @@ namespace math { */ template * = nullptr, - require_all_vt_same* = nullptr> + require_all_same_t, value_type_t>* = nullptr> inline Eigen::Matrix scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { check_square("scale_matrix_exp_multiply", "input matrix", A); @@ -54,7 +54,7 @@ scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { */ template * = nullptr, - require_any_not_vt_same* = nullptr> + require_any_not_same_t, value_type_t>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, EigMat2::ColsAtCompileTime> scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) { diff --git a/stan/math/prim/meta.hpp b/stan/math/prim/meta.hpp index 25a3baca8de..4e0f966f848 100644 --- a/stan/math/prim/meta.hpp +++ b/stan/math/prim/meta.hpp @@ -193,6 +193,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/prim/meta/is_var.hpp b/stan/math/prim/meta/is_var.hpp index e9315a5ffb3..db87cc9e2ee 100644 --- a/stan/math/prim/meta/is_var.hpp +++ b/stan/math/prim/meta/is_var.hpp @@ -14,7 +14,14 @@ template struct is_var : std::false_type {}; STAN_ADD_REQUIRE_UNARY(var, is_var, require_stan_scalar_real); +STAN_ADD_REQUIRE_CONTAINER(var, is_var, require_stan_scalar_real); STAN_ADD_REQUIRE_UNARY_INNER(var, is_var, require_stan_scalar_real); + +template +struct value_type::value>> { + using type = typename std::decay_t::value_type; +}; + } // namespace stan #endif diff --git a/stan/math/prim/meta/is_vari.hpp b/stan/math/prim/meta/is_vari.hpp new file mode 100644 index 00000000000..db82abbf9a9 --- /dev/null +++ b/stan/math/prim/meta/is_vari.hpp @@ -0,0 +1,19 @@ +#ifndef STAN_MATH_PRIM_META_IS_VARI_HPP +#define STAN_MATH_PRIM_META_IS_VARI_HPP + +#include + +namespace stan { +/** \ingroup type_trait + * Specialization for checking if value of T minus cv qualifier and pointer is a + * vari. + */ +template +struct is_vari : std::false_type {}; + +STAN_ADD_REQUIRE_UNARY(vari, is_vari, require_stan_scalar_real); +STAN_ADD_REQUIRE_CONTAINER(vari, is_vari, require_stan_scalar_real); + + +} // namespace stan +#endif diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 92b18fe023e..57d49da3dbc 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -6,10 +6,10 @@ namespace stan { namespace math { -class vari; +class vari_base; class chainable_alloc; -using ChainableStack = AutodiffStackSingleton; +using ChainableStack = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 01b3b153fcc..6e5f56c2287 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -27,9 +27,9 @@ namespace math { * @param vi Variable implementation for root of partial * derivative propagation. */ -static void grad(vari* vi) { +static void grad(vari_base* vi) { vi->init_dependent(); - std::vector& var_stack = ChainableStack::instance_->var_stack_; + std::vector& var_stack = ChainableStack::instance_->var_stack_; size_t end = var_stack.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { diff --git a/stan/math/rev/core/matrix_vari.hpp b/stan/math/rev/core/matrix_vari.hpp index c9dcc4eb54b..69aa4a96068 100644 --- a/stan/math/rev/core/matrix_vari.hpp +++ b/stan/math/rev/core/matrix_vari.hpp @@ -17,7 +17,7 @@ class op_matrix_vari : public vari { vari** vis_; public: - template ...> + template * = nullptr> op_matrix_vari(double f, const T& vs) : vari(f), size_(vs.size()) { vis_ = ChainableStack::instance_->memalloc_.alloc_array(size_); Eigen::Map>(vis_, vs.rows(), vs.cols()) diff --git a/stan/math/rev/core/operator_addition.hpp b/stan/math/rev/core/operator_addition.hpp index 67b595a192b..190958f8301 100644 --- a/stan/math/rev/core/operator_addition.hpp +++ b/stan/math/rev/core/operator_addition.hpp @@ -78,7 +78,7 @@ class add_vd_vari : public op_vd_vari { * @param b Second variable operand. * @return Variable result of adding two variables. */ -inline var operator+(var a, var b) { +inline var operator+(const var& a, const var& b) { return {new internal::add_vv_vari(a.vi_, b.vi_)}; } @@ -94,8 +94,8 @@ inline var operator+(var a, var b) { * @param b Second scalar operand. * @return Result of adding variable and scalar. */ -template ...> -inline var operator+(var a, Arith b) { +template * = nullptr> +inline var operator+(const var& a, Arith b) { if (b == 0.0) { return a; } @@ -114,8 +114,8 @@ inline var operator+(var a, Arith b) { * @param b Second variable operand. * @return Result of adding variable and scalar. */ -template ...> -inline var operator+(Arith a, var b) { +template * = nullptr> +inline var operator+(Arith a, const var& b) { if (a == 0.0) { return b; } diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 88289b8ce67..531ee676004 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -7,13 +7,16 @@ namespace stan { namespace math { -inline var& var::operator/=(var b) { +template +template >*> +inline var_value& var_value::operator/=(const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } -template ...> -inline var& var::operator/=(Arith b) { +template +template *> +inline var_value& var_value::operator/=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_division.hpp b/stan/math/rev/core/operator_division.hpp index 420ec04d07d..69770cb15cc 100644 --- a/stan/math/rev/core/operator_division.hpp +++ b/stan/math/rev/core/operator_division.hpp @@ -115,8 +115,8 @@ inline var operator/(var dividend, var divisor) { * @param divisor Scalar operand. * @return Variable result of dividing the variable by the scalar. */ -template ...> -inline var operator/(var dividend, Arith divisor) { +template * = nullptr> +inline var operator/(const var& dividend, Arith divisor) { if (divisor == 1.0) { return dividend; } @@ -135,8 +135,8 @@ inline var operator/(var dividend, Arith divisor) { * @param divisor Variable operand. * @return Quotient of the dividend and divisor. */ -template ...> -inline var operator/(Arith dividend, var divisor) { +template * = nullptr> +inline var operator/(Arith dividend, const var& divisor) { return {new internal::divide_dv_vari(dividend, divisor.vi_)}; } diff --git a/stan/math/rev/core/operator_equal.hpp b/stan/math/rev/core/operator_equal.hpp index 1f123ac1da4..28140c8b484 100644 --- a/stan/math/rev/core/operator_equal.hpp +++ b/stan/math/rev/core/operator_equal.hpp @@ -24,7 +24,7 @@ namespace math { * @return True if the first variable's value is the same as the * second's. */ -inline bool operator==(var a, var b) { return a.val() == b.val(); } +inline bool operator==(const var& a, const var& b) { return a.val() == b.val(); } /** * Equality operator comparing a variable's value and a double @@ -36,8 +36,8 @@ inline bool operator==(var a, var b) { return a.val() == b.val(); } * @return True if the first variable's value is the same as the * second value. */ -template ...> -inline bool operator==(var a, Arith b) { +template * = nullptr> +inline bool operator==(const var& a, Arith b) { return a.val() == b; } @@ -50,8 +50,8 @@ inline bool operator==(var a, Arith b) { * @param b Second variable. * @return True if the variable's value is equal to the scalar. */ -template ...> -inline bool operator==(Arith a, var b) { +template * = nullptr> +inline bool operator==(Arith a, const var& b) { return a == b.val(); } diff --git a/stan/math/rev/core/operator_greater_than.hpp b/stan/math/rev/core/operator_greater_than.hpp index c0e07372562..bbffa4f4948 100644 --- a/stan/math/rev/core/operator_greater_than.hpp +++ b/stan/math/rev/core/operator_greater_than.hpp @@ -23,7 +23,7 @@ namespace math { * @param b Second variable. * @return True if first variable's value is greater than second's. */ -inline bool operator>(var a, var b) { return a.val() > b.val(); } +inline bool operator>(const var& a, const var& b) { return a.val() > b.val(); } /** * Greater than operator comparing variable's value and double @@ -34,8 +34,8 @@ inline bool operator>(var a, var b) { return a.val() > b.val(); } * @param b Second value. * @return True if first variable's value is greater than second value. */ -template ...> -inline bool operator>(var a, Arith b) { +template * = nullptr> +inline bool operator>(const var& a, Arith b) { return a.val() > b; } @@ -48,8 +48,8 @@ inline bool operator>(var a, Arith b) { * @param b Second variable. * @return True if first value is greater than second variable's value. */ -template ...> -inline bool operator>(Arith a, var b) { +template * = nullptr> +inline bool operator>(Arith a, const var& b) { return a > b.val(); } diff --git a/stan/math/rev/core/operator_greater_than_or_equal.hpp b/stan/math/rev/core/operator_greater_than_or_equal.hpp index d8c1a4619b2..356816f84c1 100644 --- a/stan/math/rev/core/operator_greater_than_or_equal.hpp +++ b/stan/math/rev/core/operator_greater_than_or_equal.hpp @@ -25,7 +25,7 @@ namespace math { * @return True if first variable's value is greater than or equal * to the second's. */ -inline bool operator>=(var a, var b) { return a.val() >= b.val(); } +inline bool operator>=(const var& a, const var& b) { return a.val() >= b.val(); } /** * Greater than or equal operator comparing variable's value and @@ -37,8 +37,8 @@ inline bool operator>=(var a, var b) { return a.val() >= b.val(); } * @return True if first variable's value is greater than or equal * to second value. */ -template ...> -inline bool operator>=(var a, Arith b) { +template * = nullptr> +inline bool operator>=(const var& a, Arith b) { return a.val() >= b; } @@ -52,8 +52,8 @@ inline bool operator>=(var a, Arith b) { * @return True if the first value is greater than or equal to the * second variable's value. */ -template ...> -inline bool operator>=(Arith a, var b) { +template * = nullptr> +inline bool operator>=(Arith a, const var& b) { return a >= b.val(); } diff --git a/stan/math/rev/core/operator_less_than.hpp b/stan/math/rev/core/operator_less_than.hpp index 7157c504759..387ec114f0c 100644 --- a/stan/math/rev/core/operator_less_than.hpp +++ b/stan/math/rev/core/operator_less_than.hpp @@ -22,7 +22,7 @@ namespace math { * @param b Second variable. * @return True if first variable's value is less than second's. */ -inline bool operator<(var a, var b) { return a.val() < b.val(); } +inline bool operator<(const var& a, const var& b) { return a.val() < b.val(); } /** * Less than operator comparing variable's value and a double @@ -33,8 +33,8 @@ inline bool operator<(var a, var b) { return a.val() < b.val(); } * @param b Second value. * @return True if first variable's value is less than second value. */ -template ...> -inline bool operator<(var a, Arith b) { +template * = nullptr> +inline bool operator<(const var& a, Arith b) { return a.val() < b; } @@ -47,8 +47,8 @@ inline bool operator<(var a, Arith b) { * @param b Second variable. * @return True if first value is less than second variable's value. */ -template ...> -inline bool operator<(Arith a, var b) { +template * = nullptr> +inline bool operator<(Arith a, const var& b) { return a < b.val(); } diff --git a/stan/math/rev/core/operator_less_than_or_equal.hpp b/stan/math/rev/core/operator_less_than_or_equal.hpp index e45c6e022b4..a5a50f8975c 100644 --- a/stan/math/rev/core/operator_less_than_or_equal.hpp +++ b/stan/math/rev/core/operator_less_than_or_equal.hpp @@ -24,7 +24,7 @@ namespace math { * @return True if first variable's value is less than or equal to * the second's. */ -inline bool operator<=(var a, var b) { return a.val() <= b.val(); } +inline bool operator<=(const var& a, const var& b) { return a.val() <= b.val(); } /** * Less than or equal operator comparing a variable's value and a @@ -36,8 +36,8 @@ inline bool operator<=(var a, var b) { return a.val() <= b.val(); } * @return True if first variable's value is less than or equal to * the second value. */ -template ...> -inline bool operator<=(var a, Arith b) { +template * = nullptr> +inline bool operator<=(const var& a, Arith b) { return a.val() <= b; } @@ -51,8 +51,8 @@ inline bool operator<=(var a, Arith b) { * @return True if first value is less than or equal to the second * variable's value. */ -template ...> -inline bool operator<=(Arith a, var b) { +template * = nullptr> +inline bool operator<=(Arith a, const var& b) { return a <= b.val(); } diff --git a/stan/math/rev/core/operator_logical_and.hpp b/stan/math/rev/core/operator_logical_and.hpp index 66360dd19ea..80cf247186d 100644 --- a/stan/math/rev/core/operator_logical_and.hpp +++ b/stan/math/rev/core/operator_logical_and.hpp @@ -15,7 +15,7 @@ namespace math { * @param[in] y second argument * @return conjunction of the arguments' values */ -inline bool operator&&(var x, var y) { return x.val() && y.val(); } +inline bool operator&&(const var& x, const var& y) { return x.val() && y.val(); } /** * Return the logical conjunction of the values of the two @@ -28,8 +28,8 @@ inline bool operator&&(var x, var y) { return x.val() && y.val(); } * @return conjunction of first argument's value and second * argument */ -template ...> -inline bool operator&&(var x, Arith y) { +template * = nullptr> +inline bool operator&&(const var& x, Arith y) { return x.val() && y; } @@ -44,8 +44,8 @@ inline bool operator&&(var x, Arith y) { * @return conjunction of first argument and second argument's * value */ -template ...> -inline bool operator&&(Arith x, var y) { +template * = nullptr> +inline bool operator&&(Arith x, const var& y) { return x && y.val(); } diff --git a/stan/math/rev/core/operator_logical_or.hpp b/stan/math/rev/core/operator_logical_or.hpp index 6aaf35b31df..a4f1f8ab14c 100644 --- a/stan/math/rev/core/operator_logical_or.hpp +++ b/stan/math/rev/core/operator_logical_or.hpp @@ -15,7 +15,7 @@ namespace math { * @param[in] y second argument * @return disjunction of the arguments' values */ -inline bool operator||(var x, var y) { return x.val() || y.val(); } +inline bool operator||(const var& x, const var& y) { return x.val() || y.val(); } /** * Return the logical disjunction of the values of the two @@ -27,8 +27,8 @@ inline bool operator||(var x, var y) { return x.val() || y.val(); } * @return disjunction of first argument's value and second * argument */ -template ...> -inline bool operator||(var x, Arith y) { +template * = nullptr> +inline bool operator||(const var& x, Arith y) { return x.val() || y; } @@ -42,8 +42,8 @@ inline bool operator||(var x, Arith y) { * @return disjunction of first argument and the second * argument's value */ -template ...> -inline bool operator||(Arith x, var y) { +template * = nullptr> +inline bool operator||(Arith x, const var& y) { return x || y.val(); } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 30cd5f2b299..3990c103f8b 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -8,13 +8,16 @@ namespace stan { namespace math { -inline var& var::operator-=(var b) { +template +template >*> +inline var_value& var_value::operator-=(const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } -template ...> -inline var& var::operator-=(Arith b) { +template +template *> +inline var_value& var_value::operator-=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiplication.hpp b/stan/math/rev/core/operator_multiplication.hpp index 080e0831feb..12744038693 100644 --- a/stan/math/rev/core/operator_multiplication.hpp +++ b/stan/math/rev/core/operator_multiplication.hpp @@ -79,7 +79,7 @@ class multiply_vd_vari : public op_vd_vari { * @param b Second variable operand. * @return Variable result of multiplying operands. */ -inline var operator*(var a, var b) { +inline var operator*(const var& a, const var& b) { return {new internal::multiply_vv_vari(a.vi_, b.vi_)}; } @@ -95,8 +95,8 @@ inline var operator*(var a, var b) { * @param b Scalar operand. * @return Variable result of multiplying operands. */ -template ...> -inline var operator*(var a, Arith b) { +template * = nullptr> +inline var operator*(const var& a, Arith b) { if (b == 1.0) { return a; } @@ -115,8 +115,8 @@ inline var operator*(var a, Arith b) { * @param b Variable operand. * @return Variable result of multiplying the operands. */ -template ...> -inline var operator*(Arith a, var b) { +template * = nullptr> +inline var operator*(Arith a, const var& b) { if (a == 1.0) { return b; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 3426d203040..302af7d612f 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -8,13 +8,16 @@ namespace stan { namespace math { -inline var& var::operator*=(var b) { +template +template >*> +inline var_value& var_value::operator*=(const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } -template ...> -inline var& var::operator*=(Arith b) { +template +template *> +inline var_value& var_value::operator*=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_not_equal.hpp b/stan/math/rev/core/operator_not_equal.hpp index 9a5148294fe..d7591eacfbb 100644 --- a/stan/math/rev/core/operator_not_equal.hpp +++ b/stan/math/rev/core/operator_not_equal.hpp @@ -27,7 +27,7 @@ namespace math { * @return True if the first variable's value is not the same as the * second's. */ -inline bool operator!=(var a, var b) { return a.val() != b.val(); } +inline bool operator!=(const var& a, const var& b) { return a.val() != b.val(); } /** * Inequality operator comparing a variable's value and a double @@ -39,8 +39,8 @@ inline bool operator!=(var a, var b) { return a.val() != b.val(); } * @return True if the first variable's value is not the same as the * second value. */ -template ...> -inline bool operator!=(var a, Arith b) { +template * = nullptr> +inline bool operator!=(const var& a, Arith b) { return a.val() != b; } @@ -54,8 +54,8 @@ inline bool operator!=(var a, Arith b) { * @return True if the first value is not the same as the * second variable's value. */ -template ...> -inline bool operator!=(Arith a, var b) { +template * = nullptr> +inline bool operator!=(Arith a, const var& b) { return a != b.val(); } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index cd629637bdd..85d91100d78 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -8,13 +8,16 @@ namespace stan { namespace math { -inline var& var::operator+=(var b) { +template +template >*> +inline var_value& var_value::operator+=(const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } -template ...> -inline var& var::operator+=(Arith b) { +template +template *> +inline var_value& var_value::operator+=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_subtraction.hpp b/stan/math/rev/core/operator_subtraction.hpp index 6eb66f4f690..eb4319e5f03 100644 --- a/stan/math/rev/core/operator_subtraction.hpp +++ b/stan/math/rev/core/operator_subtraction.hpp @@ -93,7 +93,7 @@ class subtract_dv_vari : public op_dv_vari { * @return Variable result of subtracting the second variable from * the first. */ -inline var operator-(var a, var b) { +inline var operator-(const var& a, const var& b) { return {new internal::subtract_vv_vari(a.vi_, b.vi_)}; } @@ -110,8 +110,8 @@ inline var operator-(var a, var b) { * @param b Second scalar operand. * @return Result of subtracting the scalar from the variable. */ -template ...> -inline var operator-(var a, Arith b) { +template * = nullptr> +inline var operator-(const var& a, Arith b) { if (b == 0.0) { return a; } @@ -131,8 +131,8 @@ inline var operator-(var a, Arith b) { * @param b Second variable operand. * @return Result of subtracting a variable from a scalar. */ -template ...> -inline var operator-(Arith a, var b) { +template * = nullptr> +inline var operator-(Arith a, const var& b) { return {new internal::subtract_dv_vari(a, b.vi_)}; } diff --git a/stan/math/rev/core/operator_unary_negative.hpp b/stan/math/rev/core/operator_unary_negative.hpp index 250ccc0f5a9..3b294f2250d 100644 --- a/stan/math/rev/core/operator_unary_negative.hpp +++ b/stan/math/rev/core/operator_unary_negative.hpp @@ -48,7 +48,7 @@ class neg_vari : public op_v_vari { * @param a Argument variable. * @return Negation of variable. */ -inline var operator-(var a) { return {new internal::neg_vari(a.vi_)}; } +inline var operator-(const var& a) { return {new internal::neg_vari(a.vi_)}; } } // namespace math } // namespace stan diff --git a/stan/math/rev/core/operator_unary_not.hpp b/stan/math/rev/core/operator_unary_not.hpp index 6a1f58d59c0..aa0234504ee 100644 --- a/stan/math/rev/core/operator_unary_not.hpp +++ b/stan/math/rev/core/operator_unary_not.hpp @@ -14,7 +14,7 @@ namespace math { * @param[in] x argument * @return negation of argument value */ -inline bool operator!(var x) { return !x.val(); } +inline bool operator!(const var& x) { return !x.val(); } } // namespace math } // namespace stan diff --git a/stan/math/rev/core/operator_unary_plus.hpp b/stan/math/rev/core/operator_unary_plus.hpp index e48685f4965..f2a1fe8e45b 100644 --- a/stan/math/rev/core/operator_unary_plus.hpp +++ b/stan/math/rev/core/operator_unary_plus.hpp @@ -41,7 +41,7 @@ namespace math { * @param a Argument variable. * @return The input reference. */ -inline var operator+(var a) { +inline var operator+(const var& a) { if (unlikely(is_nan(a.vi_->val_))) { return {new precomp_v_vari(NOT_A_NUMBER, a.vi_, NOT_A_NUMBER)}; } diff --git a/stan/math/rev/core/std_iterator_traits.hpp b/stan/math/rev/core/std_iterator_traits.hpp index 2bcda5ad91f..9194128965c 100644 --- a/stan/math/rev/core/std_iterator_traits.hpp +++ b/stan/math/rev/core/std_iterator_traits.hpp @@ -10,8 +10,8 @@ namespace std { * Specialization of iterator traits for Stan math. These all take * the form of typedefs. */ -template <> -struct iterator_traits { +template +struct iterator_traits> { /** * Iterator category for traits. */ @@ -25,17 +25,18 @@ struct iterator_traits { /** * Type for value of pointer to values. */ - typedef stan::math::var value_type; + typedef stan::math::var_value value_type; /** * Type of pointer to variables. */ - typedef stan::math::var* pointer; + typedef stan::math::var_value* pointer; /** * Type of reference to variables. */ - typedef stan::math::var& reference; + typedef stan::math::var_value& reference; + }; } // namespace std diff --git a/stan/math/rev/core/std_numeric_limits.hpp b/stan/math/rev/core/std_numeric_limits.hpp index a218696cca8..79fe01fc701 100644 --- a/stan/math/rev/core/std_numeric_limits.hpp +++ b/stan/math/rev/core/std_numeric_limits.hpp @@ -10,57 +10,62 @@ namespace std { * Specialization of numeric limits for var objects. * * This implementation of std::numeric_limits - * is used to treat var objects like doubles. + * is used to treat var objects like value_types. */ -template <> -struct numeric_limits { - static const bool is_specialized = true; - static stan::math::var min() { return numeric_limits::min(); } - static stan::math::var max() { return numeric_limits::max(); } - static const int digits = numeric_limits::digits; - static const int digits10 = numeric_limits::digits10; - static const bool is_signed = numeric_limits::is_signed; - static const bool is_integer = numeric_limits::is_integer; - static const bool is_exact = numeric_limits::is_exact; - static const int radix = numeric_limits::radix; - static stan::math::var epsilon() { return numeric_limits::epsilon(); } - static stan::math::var round_error() { - return numeric_limits::round_error(); +template +struct numeric_limits> { + typedef stan::math::internal::floating_point_promoter value_type; + static constexpr bool is_specialized = true; + static constexpr stan::math::var_value min() noexcept { return numeric_limits::min(); } + static constexpr stan::math::var_value max() noexcept { return numeric_limits::max(); } + static constexpr int digits = numeric_limits::digits; + static constexpr int digits10 = numeric_limits::digits10; + static constexpr int max_digits10 = numeric_limits::max_digits10; + static constexpr bool is_signed = numeric_limits::is_signed; + static constexpr bool is_integer = numeric_limits::is_integer; + static constexpr bool is_exact = numeric_limits::is_exact; + static constexpr int radix = numeric_limits::radix; + static constexpr stan::math::var_value epsilon() noexcept { return numeric_limits::epsilon(); } + static constexpr stan::math::var_value round_error() noexcept { + return numeric_limits::round_error(); } + static constexpr T lowest() noexcept { + return numeric_limits::lowest(); + }; - static const int min_exponent = numeric_limits::min_exponent; - static const int min_exponent10 = numeric_limits::min_exponent10; - static const int max_exponent = numeric_limits::max_exponent; - static const int max_exponent10 = numeric_limits::max_exponent10; + static constexpr int min_exponent = numeric_limits::min_exponent; + static constexpr int min_exponent10 = numeric_limits::min_exponent10; + static constexpr int max_exponent = numeric_limits::max_exponent; + static constexpr int max_exponent10 = numeric_limits::max_exponent10; - static const bool has_infinity = numeric_limits::has_infinity; - static const bool has_quiet_NaN = numeric_limits::has_quiet_NaN; - static const bool has_signaling_NaN - = numeric_limits::has_signaling_NaN; - static const float_denorm_style has_denorm - = numeric_limits::has_denorm; - static const bool has_denorm_loss = numeric_limits::has_denorm_loss; - static stan::math::var infinity() { - return numeric_limits::infinity(); + static constexpr bool has_infinity = numeric_limits::has_infinity; + static constexpr bool has_quiet_NaN = numeric_limits::has_quiet_NaN; + static constexpr bool has_signaling_NaN + = numeric_limits::has_signaling_NaN; + static constexpr float_denorm_style has_denorm + = numeric_limits::has_denorm; + static constexpr bool has_denorm_loss = numeric_limits::has_denorm_loss; + static constexpr stan::math::var_value infinity() noexcept { + return numeric_limits::infinity(); } - static stan::math::var quiet_NaN() { - return numeric_limits::quiet_NaN(); + static constexpr stan::math::var_value quiet_NaN() noexcept { + return numeric_limits::quiet_NaN(); } - static stan::math::var signaling_NaN() { - return numeric_limits::signaling_NaN(); + static constexpr stan::math::var_value signaling_NaN() noexcept { + return numeric_limits::signaling_NaN(); } - static stan::math::var denorm_min() { - return numeric_limits::denorm_min(); + static constexpr stan::math::var_value denorm_min() noexcept { + return numeric_limits::denorm_min(); } - static const bool is_iec559 = numeric_limits::is_iec559; - static const bool is_bounded = numeric_limits::is_bounded; - static const bool is_modulo = numeric_limits::is_modulo; + static constexpr bool is_iec559 = numeric_limits::is_iec559; + static constexpr bool is_bounded = numeric_limits::is_bounded; + static constexpr bool is_modulo = numeric_limits::is_modulo; - static const bool traps = numeric_limits::traps; - static const bool tinyness_before = numeric_limits::tinyness_before; - static const float_round_style round_style - = numeric_limits::round_style; + static constexpr bool traps = numeric_limits::traps; + static constexpr bool tinyness_before = numeric_limits::tinyness_before; + static constexpr float_round_style round_style + = numeric_limits::round_style; }; } // namespace std diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 1f9cb114000..6aae6529e66 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -5,18 +5,14 @@ #include #include #include -#include #include #include -#include -#include -#include namespace stan { namespace math { // forward declare -static void grad(vari* vi); +static void grad(vari_base* vi); /** * Independent (input) and dependent (output) variables for gradients. @@ -30,23 +26,31 @@ static void grad(vari* vi); * and subtraction, as well as a range of mathematical functions * like exponentiation and powers are overridden to operate on * var values objects. + * @tparam T An Arithmetic type. */ -class var { - public: - // FIXME: doc what this is for - using Scalar = double; +template +class var_value { + public: + /** FIXME: This changes integral (int etc) types to double and leaves the type + * untouched otherwise. Since this can be an Eigen matrix it's a pretty + * dumb name. Just for the purposes of readability it may be better to + * use SFINAE on var_value and have a scalar and eigen representation. + * that would also get rid of some of the weirder requires here. + */ + using value_type = internal::floating_point_promoter; + using vari_type = vari_value; /** * Pointer to the implementation of this variable. * * This value should not be modified, but may be accessed in - * var operators to construct vari + * var operators to construct `vari_value` * instances. */ - vari* vi_; + vari_type* vi_; /** - * Return true if this variable has been + * Return `true` if this variable has been * declared, but not been defined. Any attempt to use an * undefined variable's value or adjoint will result in a * segmentation fault. @@ -54,7 +58,9 @@ class var { * @return true if this variable does not yet have * a defined variable. */ - bool is_uninitialized() { return (vi_ == static_cast(nullptr)); } + bool is_uninitialized() { + return (vi_ == static_cast(nullptr)); + } /** * Construct a variable for later assignment. @@ -63,185 +69,94 @@ class var { * dangling. Before an assignment, the behavior is thus undefined just * as for a basic double. */ - var() : vi_(static_cast(nullptr)) {} + var_value() : vi_(static_cast(nullptr)) {} /** * Construct a variable from a pointer to a variable implementation. - * - * @param vi Variable implementation. - */ - var(vari* vi) : vi_(vi) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(float x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument as - * a value and a zero adjoint. - * - * @param x Value of the variable. - */ - var(double x) : vi_(new vari(x, false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. + * @tparam VariValue A vari_value whose pointer can be implicitly converted + * to vari_value*. + * @param vi Vari type. */ - var(long double x) : vi_(new vari(x, false)) {} // NOLINT + template * = nullptr> + var_value(vari_value* vi) : vi_(vi) {} // NOLINT /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(bool x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(char x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(short x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(int x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(long x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - var(unsigned char x) // NOLINT(runtime/explicit) - : vi_(new vari(static_cast(x), false)) {} - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - // NOLINTNEXTLINE - var(unsigned short x) : vi_(new vari(static_cast(x), false)) {} - - /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. - */ - // NOLINTNEXTLINE - var(unsigned int x) - : vi_(new vari(static_cast(x), false)) {} // NOLINT + * Construct a variable from a pointer to a variable implementation. + * @tparam VariValue A vari_value whose pointer can be implicitly converted + * to vari_value*. + * @param vi Vari type. + */ + template * = nullptr, + require_not_same_t* = nullptr> + var_value(vari_value* vi) : vi_(new vari_type(vi->val_, false)) { // NOLINT + this->vi_->adj_ = vi->adj_; + } /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * + * Construct a variable from the specified integral type argument + * by constructing a new `vari_value`. For integral types the + * `vari_value` will hold doubles. This constructor is only valid when + * `T` is arithmetic. + * @tparam IntegralT Integral type such as `int` + * @tparam T1 A dummy template whose value will always be `T` * @param x Value of the variable. */ - // NOLINTNEXTLINE - var(unsigned long x) - : vi_(new vari(static_cast(x), false)) {} // NOLINT - -#ifdef _WIN64 - // these two ctors are for Win64 to enable 64-bit signed - // and unsigned integers, because long and unsigned long - // are still 32-bit + template * = nullptr> + var_value(S x) : vi_(new vari_type(x, false)) {} // NOLINT /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. + * Construct from a var_value whose underlying value_type differs in type + * to this class's value_type. + * `var_value a(var_value(b));`` + * @tparam S An arithmetic type that is convertible to `T` but is not the + * same as the underlying value_type. + * param x a `var_value` whose underlying vari_type can be dynamically cast + * to `this::vari_value``. */ - var(size_t x) : vi_(new vari(static_cast(x), false)) {} // NOLINT - + template , + require_not_same_t* = nullptr> + var_value(var_value& x) : vi_(new vari_type(x.val(), false)) { // NOLINT + this->vi_->adj_ = x.adj(); + } /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. + * Same as the floating point */ - var(ptrdiff_t x) : vi_(new vari(static_cast(x), false)) {} // NOLINT -#endif - -#ifdef BOOST_MATH_USE_FLOAT128 - - // this ctor is for later GCCs that have the __float128 - // type enabled, because it gets enabled by boost + template , + require_not_same_t* = nullptr> + var_value(var_value&& x) : vi_(new vari_type(x.val(), false)) { // NOLINT + this->vi_->adj_ = x.adj(); + } /** - * Construct a variable from the specified arithmetic argument - * by constructing a new vari with the argument - * cast to double, and a zero adjoint. - * - * @param x Value of the variable. + * Constructor from `var_value` whose value_type is the same as this class's + * `value_type`. This is used in cases such as + * `var_value(var_value(4.0))` since the `value_type` for a + * `var_value` with an arithmetic type is a double. */ - var(__float128 x) : vi_(new vari(static_cast(x), false)) {} // NOLINT + template , + require_same_t* = nullptr> + var_value(var_value& x) : vi_(x.vi_) {} // NOLINT -#endif + template , + require_same_t* = nullptr> + var_value(var_value&& x) : vi_(x.vi_) {} // NOLINT /** * Return the value of this variable. * * @return The value of this variable. */ - inline double val() const { return vi_->val_; } + inline auto val() const { return vi_->val_; } /** * Return the derivative of the root expression with * respect to this expression. This method only works - * after one of the grad() methods has been + * after one of the `grad()` methods has been * called. * * @return Adjoint for this variable. */ - inline double adj() const { return vi_->adj_; } + inline auto adj() const { return vi_->adj_; } /** * Compute the gradient of this (dependent) variable with respect to @@ -255,7 +170,7 @@ class var { * @param g Gradient vector of partial derivatives of this * variable with respect to x. */ - void grad(std::vector& x, std::vector& g) { + inline void grad(std::vector>& x, std::vector& g) { stan::math::grad(vi_); g.resize(x.size()); for (size_t i = 0; i < x.size(); ++i) { @@ -285,7 +200,7 @@ class var { * * @return variable */ - inline vari& operator*() { return *vi_; } + inline vari_type& operator*() { return *vi_; } /** * Return a pointer to the underlying implementation of this variable. @@ -297,7 +212,7 @@ class var { * Warning: The returned result does not track changes to * this variable. */ - inline vari* operator->() { return vi_; } + inline vari_type* operator->() { return vi_; } // COMPOUND ASSIGNMENT OPERATORS @@ -308,10 +223,12 @@ class var { * then (a += b) behaves exactly the same way as (a = a + b), * creating an intermediate variable representing (a + b). * + * @tparam S a type that is convertible to `T` * @param b The variable to add to this variable. * @return The result of adding the specified variable to this variable. */ - inline var& operator+=(var b); + template >* = nullptr> + inline var_value& operator+=(const var_value& b); /** * The compound add/assignment operator for scalars (C++). @@ -323,8 +240,8 @@ class var { * @param b The scalar to add to this variable. * @return The result of adding the specified variable to this variable. */ - template ...> - inline var& operator+=(Arith b); + template * = nullptr> + inline var_value& operator+=(Arith b); /** * The compound subtract/assignment operator for variables (C++). @@ -333,11 +250,13 @@ class var { * then (a -= b) behaves exactly the same way as (a = a - b). * Note that the result is an assignable lvalue. * + * @tparam S a type that is convertible to `T` * @param b The variable to subtract from this variable. * @return The result of subtracting the specified variable from * this variable. */ - inline var& operator-=(var b); + template >* = nullptr> + inline var_value& operator-=(const var_value& b); /** * The compound subtract/assignment operator for scalars (C++). @@ -350,8 +269,8 @@ class var { * @return The result of subtracting the specified variable from this * variable. */ - template ...> - inline var& operator-=(Arith b); + template * = nullptr> + inline var_value& operator-=(Arith b); /** * The compound multiply/assignment operator for variables (C++). @@ -360,11 +279,13 @@ class var { * then (a *= b) behaves exactly the same way as (a = a * b). * Note that the result is an assignable lvalue. * + * @tparam S a type that is convertible to `T` * @param b The variable to multiply this variable by. * @return The result of multiplying this variable by the * specified variable. */ - inline var& operator*=(var b); + template >* = nullptr> + inline var_value& operator*=(const var_value& b); /** * The compound multiply/assignment operator for scalars (C++). @@ -377,8 +298,8 @@ class var { * @return The result of multiplying this variable by the specified * variable. */ - template ...> - inline var& operator*=(Arith b); + template * = nullptr> + inline var_value& operator*=(Arith b); /** * The compound divide/assignment operator for variables (C++). If this @@ -386,11 +307,13 @@ class var { * behaves exactly the same way as (a = a / b). Note that the * result is an assignable lvalue. * + * @tparam S a type that is convertible to `T` * @param b The variable to divide this variable by. * @return The result of dividing this variable by the * specified variable. */ - inline var& operator/=(var b); + template >* = nullptr> + inline var_value& operator/=(const var_value& b); /** * The compound divide/assignment operator for scalars (C++). @@ -403,8 +326,8 @@ class var { * @return The result of dividing this variable by the specified * variable. */ - template ...> - inline var& operator/=(Arith b); + template * = nullptr> + inline var_value& operator/=(Arith b); /** * Write the value of this autodiff variable and its adjoint to @@ -414,14 +337,18 @@ class var { * @param v Variable to write. * @return Reference to the specified output stream. */ - friend std::ostream& operator<<(std::ostream& os, const var& v) { + friend std::ostream& operator<<(std::ostream& os, const var_value& v) { if (v.vi_ == nullptr) { return os << "uninitialized"; } return os << v.val(); } + }; +// For backwards compatability the default value is double +using var = var_value; + } // namespace math } // namespace stan #endif diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index ababd029097..28092db0c63 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -4,12 +4,61 @@ #include #include #include +#include namespace stan { namespace math { // forward declaration of var -class var; +template +class var_value; + +/** + * Pure virtual class that all `vari_value` and it's derived classes inherit. + */ +class vari_base { + public: + /** + * Apply the chain rule to this variable based on the variables + * on which it depends. + */ + virtual void chain() {} + + /** + * Initialize the adjoint for this (dependent) variable to 1. + * This operation is applied to the dependent variable before + * propagating derivatives, setting the derivative of the + * result with respect to itself to be 1. + */ + virtual void init_dependent() {} + + /** + * Set the adjoint value of this variable to 0. This is used to + * reset adjoints before propagating derivatives again (for + * example in a Jacobian calculation). + */ + virtual void set_zero_adjoint() {} + + /** + * Throw an illegal argument exception. + * + * Warning: Destructors should never called for var objects. + * + * @throw Logic exception always. + */ + virtual ~vari_base() {} +}; + +namespace internal { + /** + * Promote integral types to double + * @tparam Val Any type + */ + template + using floating_point_promoter + = std::conditional_t>::value, double, + std::decay_t>; +} /** * The variable implementation base class. @@ -27,21 +76,28 @@ class var; * classes will store operand variables and propagate derivative * information via an implementation of chain(). */ -class vari { - private: - friend class var; +template +class vari_value; +template +class vari_value::value>> + : public vari_base { + private: + template + friend class var_value; public: + using Scalar = internal::floating_point_promoter; + using value_type = Scalar; /** * The value of this variable. */ - const double val_; + const Scalar val_; /** * The adjoint of this variable, which is the partial derivative * of this variable with respect to the root variable. */ - double adj_; + Scalar adj_; /** * Construct a variable implementation from a value. The @@ -53,13 +109,33 @@ class vari { * derivative propagation, the chain() method of each variable * will be called in the reverse order of construction. * + * @tparam S an Arithmetic type. * @param x Value of the constructed variable. */ - explicit vari(double x) : val_(x), adj_(0.0) { + template ::value>* = nullptr> + vari_value(S x) : val_(x), adj_(0.0) { ChainableStack::instance_->var_stack_.push_back(this); } - vari(double x, bool stacked) : val_(x), adj_(0.0) { + /** + * Construct a variable implementation from a value. The + * adjoint is initialized to zero and if `stacked` is `false` this vari + * will be moved to the nochain stack s.t. it's chain method will not be + * called when calling `grad()`. + * + * All constructed variables are added to the stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @tparam S an Arithmetic type. + * @param x Value of the constructed variable. + * @param stacked If true will put this this vari on the nochain stack so that + * it's `chain()` method is not called. + */ + template ::value>* = nullptr> + vari_value(S x, bool stacked) : val_(x), adj_(0.0) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -67,38 +143,20 @@ class vari { } } - /** - * Throw an illegal argument exception. - * - * Warning: Destructors should never called for var objects. - * - * @throw Logic exception always. - */ - virtual ~vari() { - // this will never get called - } - - /** - * Apply the chain rule to this variable based on the variables - * on which it depends. The base implementation in this class - * is a no-op. - */ - virtual void chain() {} - /** * Initialize the adjoint for this (dependent) variable to 1. * This operation is applied to the dependent variable before * propagating derivatives, setting the derivative of the * result with respect to itself to be 1. */ - void init_dependent() { adj_ = 1.0; } + void init_dependent() final { adj_ = 1.0; } /** * Set the adjoint value of this variable to 0. This is used to * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() { adj_ = 0.0; } + void set_zero_adjoint() final { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and @@ -109,7 +167,7 @@ class vari { * * @return The modified ostream. */ - friend std::ostream& operator<<(std::ostream& os, const vari* v) { + friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { return os << v->val_ << ":" << v->adj_; } @@ -142,6 +200,9 @@ class vari { } }; +// For backwards compatability the default is double +using vari = vari_value; + } // namespace math } // namespace stan #endif diff --git a/stan/math/rev/core/vector_vari.hpp b/stan/math/rev/core/vector_vari.hpp index 7e06bb91f27..55de53af4a8 100644 --- a/stan/math/rev/core/vector_vari.hpp +++ b/stan/math/rev/core/vector_vari.hpp @@ -15,8 +15,8 @@ class op_vector_vari : public vari { vari** vis_; public: - template ..., - require_vector_like_vt...> + template * = nullptr, + require_vector_like_vt* = nullptr> op_vector_vari(Arith f, VecVar&& vs) : vari(f), size_(vs.size()) { vis_ = reinterpret_cast(operator new(sizeof(vari*) * vs.size())); for (size_t i = 0; i < vs.size(); ++i) { diff --git a/stan/math/rev/fun/value_of.hpp b/stan/math/rev/fun/value_of.hpp index f3361cdf72c..162a9834618 100644 --- a/stan/math/rev/fun/value_of.hpp +++ b/stan/math/rev/fun/value_of.hpp @@ -20,7 +20,8 @@ namespace math { * @param v Variable. * @return Value of variable. */ -inline double value_of(const var& v) { return v.vi_->val_; } +template +inline auto value_of(const var_value& v) { return v.vi_->val_; } } // namespace math } // namespace stan diff --git a/stan/math/rev/fun/value_of_rec.hpp b/stan/math/rev/fun/value_of_rec.hpp index c75d7a2e931..5f6a477d86b 100644 --- a/stan/math/rev/fun/value_of_rec.hpp +++ b/stan/math/rev/fun/value_of_rec.hpp @@ -14,7 +14,8 @@ namespace math { * @param v Variable. * @return Value of variable. */ -inline double value_of_rec(const var& v) { return v.vi_->val_; } +template +inline auto value_of_rec(const var_value& v) { return v.vi_->val_; } } // namespace math } // namespace stan diff --git a/stan/math/rev/meta.hpp b/stan/math/rev/meta.hpp index e97380c2e1b..de7740d931e 100644 --- a/stan/math/rev/meta.hpp +++ b/stan/math/rev/meta.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/stan/math/rev/meta/is_var.hpp b/stan/math/rev/meta/is_var.hpp index 40cb61cf8d7..a44745393c9 100644 --- a/stan/math/rev/meta/is_var.hpp +++ b/stan/math/rev/meta/is_var.hpp @@ -6,13 +6,19 @@ #include namespace stan { + +namespace internal { + template + struct is_var_impl : std::false_type {}; + + template + struct is_var_impl> : std::true_type {}; +} /** \ingroup type_trait - * Specialization for checking if value of T minus cv qualifier is a var. + * Specialization for checking if value of T minus cv qualifier is a var_value. */ template -struct is_var>::value>> - : std::true_type {}; +struct is_var>::value>> : std::true_type {}; } // namespace stan #endif diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp new file mode 100644 index 00000000000..84c5d79cb85 --- /dev/null +++ b/stan/math/rev/meta/is_vari.hpp @@ -0,0 +1,21 @@ +#ifndef STAN_MATH_REV_META_IS_VARI_HPP +#define STAN_MATH_REV_META_IS_VARI_HPP + +#include +#include +#include + +namespace stan { +/** \ingroup type_trait + * Specialization for checking if value of T minus cv qualifier and pointer is a + * vari_value. + */ +template +struct is_vari< + T, std::enable_if_t>>::value>> + : std::true_type {}; + + +} // namespace stan +#endif diff --git a/stan/math/rev/meta/partials_type.hpp b/stan/math/rev/meta/partials_type.hpp index 51c10e928a9..823117fe062 100644 --- a/stan/math/rev/meta/partials_type.hpp +++ b/stan/math/rev/meta/partials_type.hpp @@ -13,7 +13,7 @@ namespace stan { */ template struct partials_type> { - using type = typename std::decay_t::Scalar; + using type = typename std::decay_t::value_type; }; } // namespace stan diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 54d66f75db0..6284ce983bd 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -13,62 +14,63 @@ struct AgradRev : public testing::Test { } }; -TEST_F(AgradRev, ctorOverloads) { - using stan::math::var; - using stan::math::vari; - +template +void ctor_overloads_impl() { + using stan::math::var_value; + using stan::math::vari_value; + using stan::math::test::type_name; // make sure copy ctor is used rather than casting vari* to unsigned int - EXPECT_FLOAT_EQ(12.3, var(new vari(12.3)).val()); + EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) << + "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; - // double - EXPECT_FLOAT_EQ(3.7, var(3.7).val()); + EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) << + "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; + EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) << + "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; + // double + EXPECT_FLOAT_EQ(3.7, var_value(3.7).val()) << + "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; // long double - EXPECT_FLOAT_EQ(3.7, var(static_cast(3.7)).val()); - - // float - EXPECT_FLOAT_EQ(3.7, var(static_cast(3.7)).val()); - - // bool - EXPECT_FLOAT_EQ(1, var(static_cast(true)).val()); - - // char - EXPECT_FLOAT_EQ(3, var(static_cast(3)).val()); - - // short - EXPECT_FLOAT_EQ(1, var(static_cast(1)).val()); - - // int - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - - // long - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - - // unsigned char - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); + EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) << + "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; + EXPECT_FLOAT_EQ(0, var_value(static_cast(0)).val()) << + "Failed For T: " << type_name() << " and S:" << type_name() << "\n"; - // unsigned short - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - - // unsigned int - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - - // unsigned int (test conflict with null pointer) - EXPECT_FLOAT_EQ(0, var(static_cast(0)).val()); - - // unsigned long - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - - // unsigned long (test for conflict with pointer) - EXPECT_FLOAT_EQ(0, var(static_cast(0)).val()); - - // size_t - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - EXPECT_FLOAT_EQ(0, var(static_cast(0)).val()); +} - // ptrdiff_t - EXPECT_FLOAT_EQ(37, var(static_cast(37)).val()); - EXPECT_FLOAT_EQ(0, var(static_cast(0)).val()); +template +void ctor_overloads() { + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); +} +TEST_F(AgradRev, ctorOverloads) { + using stan::math::var; + using stan::math::vari; + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); + ctor_overloads(); } TEST_F(AgradRev, a_eq_x) { @@ -281,3 +283,11 @@ TEST_F(AgradRev, grad) { EXPECT_FLOAT_EQ(11.0, a.adj()); EXPECT_FLOAT_EQ(5.0, b.adj()); } + +TEST_F(AgradRev, type_convs) { + using stan::math::var_value; + var_value a(1); + var_value b(a); + var_value c(a); + var_value d(a); +} diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index 0da07b91486..b0e72a07230 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -8,3 +8,10 @@ TEST(AgradRev, insertion_operator) { ss << &v; EXPECT_EQ("5:0", ss.str()); } + +TEST(AgradRev, insertion_operator) { + stan::math::vari_value v(5); + std::stringstream ss; + ss << &v; + EXPECT_EQ("5:0", ss.str()); +} diff --git a/test/unit/pretty_print_types.hpp b/test/unit/pretty_print_types.hpp index f468590de26..893e92b59a5 100644 --- a/test/unit/pretty_print_types.hpp +++ b/test/unit/pretty_print_types.hpp @@ -74,7 +74,7 @@ inline std::ostream& operator<<(std::ostream& os, static_string const& s) { * Prints out an input type. * @tparam T The type to print out. */ -template +template CONSTEXPR14_TN static_string type_name() { #ifdef __clang__ static_string p = __PRETTY_FUNCTION__; @@ -84,7 +84,7 @@ CONSTEXPR14_TN static_string type_name() { #if __cplusplus < 201402 return static_string(p.data() + 36, p.size() - 36 - 1); #else - return static_string(p.data() + 46, p.size() - 46 - 1); + return static_string(p.data() + 83, p.size() - 83 - 1); #endif #elif defined(_MSC_VER) static_string p = __FUNCSIG__; From d7e1d84bd90ac50dc1f00dc12df3f8eebb411d00 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 28 May 2020 13:05:39 -0400 Subject: [PATCH 014/167] Add a template to grad so that init_dependent can be non-virtual, make vari_base a pure virtual base class --- stan/math/rev/core/grad.hpp | 3 ++- stan/math/rev/core/vari.hpp | 14 +++----------- stan/math/rev/functor/coupled_ode_system.hpp | 4 ++-- test/unit/math/rev/core/vari_test.cpp | 2 +- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 6e5f56c2287..cb62d333772 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -27,7 +27,8 @@ namespace math { * @param vi Variable implementation for root of partial * derivative propagation. */ -static void grad(vari_base* vi) { +template * = nullptr> +static void grad(Vari* vi) { vi->init_dependent(); std::vector& var_stack = ChainableStack::instance_->var_stack_; size_t end = var_stack.size(); diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 28092db0c63..4e4bd589465 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -22,22 +22,14 @@ class vari_base { * Apply the chain rule to this variable based on the variables * on which it depends. */ - virtual void chain() {} - - /** - * Initialize the adjoint for this (dependent) variable to 1. - * This operation is applied to the dependent variable before - * propagating derivatives, setting the derivative of the - * result with respect to itself to be 1. - */ - virtual void init_dependent() {} + virtual void chain() {}; /** * Set the adjoint value of this variable to 0. This is used to * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - virtual void set_zero_adjoint() {} + virtual void set_zero_adjoint() = 0; /** * Throw an illegal argument exception. @@ -149,7 +141,7 @@ class vari_value::value>> * propagating derivatives, setting the derivative of the * result with respect to itself to be 1. */ - void init_dependent() final { adj_ = 1.0; } + void init_dependent() { adj_ = 1.0; } /** * Set the adjoint value of this variable to 0. This is used to diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 22669207494..0ec42273374 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -149,7 +149,7 @@ struct coupled_ode_system { // See efficiency note above on template specialization for more details // on this. for (size_t j = 0; j < M_; ++j) { - theta_nochain_[j].vi_->set_zero_adjoint(); + theta_nochain_[j].vi_->adj_ = 0.0; } } } @@ -484,7 +484,7 @@ struct coupled_ode_system { // See efficiency note above on template specialization for more details // on this. for (size_t j = 0; j < M_; ++j) { - theta_nochain_[j].vi_->set_zero_adjoint(); + theta_nochain_[j].vi_->adj_ = 0.0; } } } diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index b0e72a07230..d9d0fb6648f 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -9,7 +9,7 @@ TEST(AgradRev, insertion_operator) { EXPECT_EQ("5:0", ss.str()); } -TEST(AgradRev, insertion_operator) { +TEST(AgradRev, int_test) { stan::math::vari_value v(5); std::stringstream ss; ss << &v; From ba930da00d3768094c2b5d7435929c783a5a1909 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 28 May 2020 14:51:47 -0400 Subject: [PATCH 015/167] add final to classes inheriting op_varis so flto can more aggressivly devirtualize --- stan/math/rev/core/operator_addition.hpp | 4 ++-- stan/math/rev/core/operator_division.hpp | 6 +++--- stan/math/rev/core/operator_multiplication.hpp | 4 ++-- stan/math/rev/core/operator_subtraction.hpp | 6 +++--- stan/math/rev/core/operator_unary_decrement.hpp | 2 +- stan/math/rev/core/operator_unary_increment.hpp | 2 +- stan/math/rev/core/operator_unary_negative.hpp | 2 +- stan/math/rev/core/precomp_v_vari.hpp | 2 +- stan/math/rev/core/precomp_vvv_vari.hpp | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/stan/math/rev/core/operator_addition.hpp b/stan/math/rev/core/operator_addition.hpp index 190958f8301..6832d0a75c2 100644 --- a/stan/math/rev/core/operator_addition.hpp +++ b/stan/math/rev/core/operator_addition.hpp @@ -12,7 +12,7 @@ namespace stan { namespace math { namespace internal { -class add_vv_vari : public op_vv_vari { +class add_vv_vari final : public op_vv_vari { public: add_vv_vari(vari* avi, vari* bvi) : op_vv_vari(avi->val_ + bvi->val_, avi, bvi) {} @@ -27,7 +27,7 @@ class add_vv_vari : public op_vv_vari { } }; -class add_vd_vari : public op_vd_vari { +class add_vd_vari final : public op_vd_vari { public: add_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ + b, avi, b) {} void chain() { diff --git a/stan/math/rev/core/operator_division.hpp b/stan/math/rev/core/operator_division.hpp index 69770cb15cc..892ec27fb4d 100644 --- a/stan/math/rev/core/operator_division.hpp +++ b/stan/math/rev/core/operator_division.hpp @@ -23,7 +23,7 @@ namespace math { namespace internal { // (dividend/divisor)' = dividend' * (1 / divisor) - divisor' * (dividend / // [divisor * divisor]) -class divide_vv_vari : public op_vv_vari { +class divide_vv_vari final : public op_vv_vari { public: divide_vv_vari(vari* dividend_vi, vari* divisor_vi) : op_vv_vari(dividend_vi->val_ / divisor_vi->val_, dividend_vi, @@ -39,7 +39,7 @@ class divide_vv_vari : public op_vv_vari { } }; -class divide_vd_vari : public op_vd_vari { +class divide_vd_vari final : public op_vd_vari { public: divide_vd_vari(vari* dividend_vi, double divisor) : op_vd_vari(dividend_vi->val_ / divisor, dividend_vi, divisor) {} @@ -52,7 +52,7 @@ class divide_vd_vari : public op_vd_vari { } }; -class divide_dv_vari : public op_dv_vari { +class divide_dv_vari final : public op_dv_vari { public: divide_dv_vari(double dividend, vari* divisor_vi) : op_dv_vari(dividend / divisor_vi->val_, dividend, divisor_vi) {} diff --git a/stan/math/rev/core/operator_multiplication.hpp b/stan/math/rev/core/operator_multiplication.hpp index 12744038693..2d360d5a7f4 100644 --- a/stan/math/rev/core/operator_multiplication.hpp +++ b/stan/math/rev/core/operator_multiplication.hpp @@ -14,7 +14,7 @@ namespace stan { namespace math { namespace internal { -class multiply_vv_vari : public op_vv_vari { +class multiply_vv_vari final : public op_vv_vari { public: multiply_vv_vari(vari* avi, vari* bvi) : op_vv_vari(avi->val_ * bvi->val_, avi, bvi) {} @@ -29,7 +29,7 @@ class multiply_vv_vari : public op_vv_vari { } }; -class multiply_vd_vari : public op_vd_vari { +class multiply_vd_vari final : public op_vd_vari { public: multiply_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ * b, avi, b) {} void chain() { diff --git a/stan/math/rev/core/operator_subtraction.hpp b/stan/math/rev/core/operator_subtraction.hpp index eb4319e5f03..20a825c7383 100644 --- a/stan/math/rev/core/operator_subtraction.hpp +++ b/stan/math/rev/core/operator_subtraction.hpp @@ -13,7 +13,7 @@ namespace stan { namespace math { namespace internal { -class subtract_vv_vari : public op_vv_vari { +class subtract_vv_vari final : public op_vv_vari { public: subtract_vv_vari(vari* avi, vari* bvi) : op_vv_vari(avi->val_ - bvi->val_, avi, bvi) {} @@ -28,7 +28,7 @@ class subtract_vv_vari : public op_vv_vari { } }; -class subtract_vd_vari : public op_vd_vari { +class subtract_vd_vari final : public op_vd_vari { public: subtract_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {} void chain() { @@ -40,7 +40,7 @@ class subtract_vd_vari : public op_vd_vari { } }; -class subtract_dv_vari : public op_dv_vari { +class subtract_dv_vari final : public op_dv_vari { public: subtract_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {} void chain() { diff --git a/stan/math/rev/core/operator_unary_decrement.hpp b/stan/math/rev/core/operator_unary_decrement.hpp index df79c750674..bca1358792b 100644 --- a/stan/math/rev/core/operator_unary_decrement.hpp +++ b/stan/math/rev/core/operator_unary_decrement.hpp @@ -11,7 +11,7 @@ namespace stan { namespace math { namespace internal { -class decrement_vari : public op_v_vari { +class decrement_vari final : public op_v_vari { public: explicit decrement_vari(vari* avi) : op_v_vari(avi->val_ - 1.0, avi) {} void chain() { diff --git a/stan/math/rev/core/operator_unary_increment.hpp b/stan/math/rev/core/operator_unary_increment.hpp index 6c3e818f088..cb904cd3780 100644 --- a/stan/math/rev/core/operator_unary_increment.hpp +++ b/stan/math/rev/core/operator_unary_increment.hpp @@ -11,7 +11,7 @@ namespace stan { namespace math { namespace internal { -class increment_vari : public op_v_vari { +class increment_vari final : public op_v_vari { public: explicit increment_vari(vari* avi) : op_v_vari(avi->val_ + 1.0, avi) {} void chain() { diff --git a/stan/math/rev/core/operator_unary_negative.hpp b/stan/math/rev/core/operator_unary_negative.hpp index 3b294f2250d..4c5b9a40b49 100644 --- a/stan/math/rev/core/operator_unary_negative.hpp +++ b/stan/math/rev/core/operator_unary_negative.hpp @@ -11,7 +11,7 @@ namespace stan { namespace math { namespace internal { -class neg_vari : public op_v_vari { +class neg_vari final : public op_v_vari { public: explicit neg_vari(vari* avi) : op_v_vari(-(avi->val_), avi) {} void chain() { diff --git a/stan/math/rev/core/precomp_v_vari.hpp b/stan/math/rev/core/precomp_v_vari.hpp index a8fb5ecd0de..6da15f81796 100644 --- a/stan/math/rev/core/precomp_v_vari.hpp +++ b/stan/math/rev/core/precomp_v_vari.hpp @@ -8,7 +8,7 @@ namespace stan { namespace math { // use for single precomputed partials -class precomp_v_vari : public op_v_vari { +class precomp_v_vari final : public op_v_vari { protected: double da_; diff --git a/stan/math/rev/core/precomp_vvv_vari.hpp b/stan/math/rev/core/precomp_vvv_vari.hpp index 69af6ce4a61..e6c2d8a9ba6 100644 --- a/stan/math/rev/core/precomp_vvv_vari.hpp +++ b/stan/math/rev/core/precomp_vvv_vari.hpp @@ -8,7 +8,7 @@ namespace stan { namespace math { // use for single precomputed partials -class precomp_vvv_vari : public op_vvv_vari { +class precomp_vvv_vari final : public op_vvv_vari { protected: double da_; double db_; From 244620a6a98f819a0de6d6f3d692bc4e679c60f6 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 29 May 2020 17:56:10 -0400 Subject: [PATCH 016/167] cleanup templates in var and vari, clang-format, tests for is_var, is_vari, and avoiding copies for integral type vari_value --- .../prim/fun/scale_matrix_exp_multiply.hpp | 6 +- stan/math/rev/core/grad.hpp | 2 +- stan/math/rev/core/operator_divide_equal.hpp | 3 +- stan/math/rev/core/operator_equal.hpp | 4 +- .../core/operator_greater_than_or_equal.hpp | 4 +- .../rev/core/operator_less_than_or_equal.hpp | 4 +- stan/math/rev/core/operator_logical_and.hpp | 4 +- stan/math/rev/core/operator_logical_or.hpp | 4 +- stan/math/rev/core/operator_minus_equal.hpp | 3 +- .../math/rev/core/operator_multiply_equal.hpp | 3 +- stan/math/rev/core/operator_not_equal.hpp | 4 +- stan/math/rev/core/operator_plus_equal.hpp | 3 +- stan/math/rev/core/precomp_vv_vari.hpp | 2 +- stan/math/rev/core/std_iterator_traits.hpp | 1 - stan/math/rev/core/std_numeric_limits.hpp | 27 +++- stan/math/rev/core/var.hpp | 143 +++++++++--------- stan/math/rev/core/vari.hpp | 48 ++++-- stan/math/rev/core/vector_vari.hpp | 3 +- stan/math/rev/meta/is_var.hpp | 14 +- test/unit/math/rev/core/var_test.cpp | 36 ++--- test/unit/math/rev/meta/is_var_test.cpp | 7 + test/unit/math/rev/meta/is_vari_test.cpp | 14 ++ 22 files changed, 210 insertions(+), 129 deletions(-) create mode 100644 test/unit/math/rev/meta/is_vari_test.cpp diff --git a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp index 577248b9b05..217c7c8b6a2 100644 --- a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp +++ b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp @@ -26,7 +26,8 @@ namespace math { */ template * = nullptr, - require_all_same_t, value_type_t>* = nullptr> + require_all_same_t, + value_type_t>* = nullptr> inline Eigen::Matrix scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { check_square("scale_matrix_exp_multiply", "input matrix", A); @@ -54,7 +55,8 @@ scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { */ template * = nullptr, - require_any_not_same_t, value_type_t>* = nullptr> + require_any_not_same_t, + value_type_t>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, EigMat2::ColsAtCompileTime> scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) { diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index cb62d333772..cd22f8c9a04 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -27,7 +27,7 @@ namespace math { * @param vi Variable implementation for root of partial * derivative propagation. */ -template * = nullptr> +template static void grad(Vari* vi) { vi->init_dependent(); std::vector& var_stack = ChainableStack::instance_->var_stack_; diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 531ee676004..25d0ae082bd 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -8,7 +8,8 @@ namespace stan { namespace math { template -template >*> +template >*> inline var_value& var_value::operator/=(const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; diff --git a/stan/math/rev/core/operator_equal.hpp b/stan/math/rev/core/operator_equal.hpp index 28140c8b484..d47ea6f473a 100644 --- a/stan/math/rev/core/operator_equal.hpp +++ b/stan/math/rev/core/operator_equal.hpp @@ -24,7 +24,9 @@ namespace math { * @return True if the first variable's value is the same as the * second's. */ -inline bool operator==(const var& a, const var& b) { return a.val() == b.val(); } +inline bool operator==(const var& a, const var& b) { + return a.val() == b.val(); +} /** * Equality operator comparing a variable's value and a double diff --git a/stan/math/rev/core/operator_greater_than_or_equal.hpp b/stan/math/rev/core/operator_greater_than_or_equal.hpp index 356816f84c1..10607dba3ed 100644 --- a/stan/math/rev/core/operator_greater_than_or_equal.hpp +++ b/stan/math/rev/core/operator_greater_than_or_equal.hpp @@ -25,7 +25,9 @@ namespace math { * @return True if first variable's value is greater than or equal * to the second's. */ -inline bool operator>=(const var& a, const var& b) { return a.val() >= b.val(); } +inline bool operator>=(const var& a, const var& b) { + return a.val() >= b.val(); +} /** * Greater than or equal operator comparing variable's value and diff --git a/stan/math/rev/core/operator_less_than_or_equal.hpp b/stan/math/rev/core/operator_less_than_or_equal.hpp index a5a50f8975c..026af410cff 100644 --- a/stan/math/rev/core/operator_less_than_or_equal.hpp +++ b/stan/math/rev/core/operator_less_than_or_equal.hpp @@ -24,7 +24,9 @@ namespace math { * @return True if first variable's value is less than or equal to * the second's. */ -inline bool operator<=(const var& a, const var& b) { return a.val() <= b.val(); } +inline bool operator<=(const var& a, const var& b) { + return a.val() <= b.val(); +} /** * Less than or equal operator comparing a variable's value and a diff --git a/stan/math/rev/core/operator_logical_and.hpp b/stan/math/rev/core/operator_logical_and.hpp index 80cf247186d..02be3386298 100644 --- a/stan/math/rev/core/operator_logical_and.hpp +++ b/stan/math/rev/core/operator_logical_and.hpp @@ -15,7 +15,9 @@ namespace math { * @param[in] y second argument * @return conjunction of the arguments' values */ -inline bool operator&&(const var& x, const var& y) { return x.val() && y.val(); } +inline bool operator&&(const var& x, const var& y) { + return x.val() && y.val(); +} /** * Return the logical conjunction of the values of the two diff --git a/stan/math/rev/core/operator_logical_or.hpp b/stan/math/rev/core/operator_logical_or.hpp index a4f1f8ab14c..ee8b4e85f0a 100644 --- a/stan/math/rev/core/operator_logical_or.hpp +++ b/stan/math/rev/core/operator_logical_or.hpp @@ -15,7 +15,9 @@ namespace math { * @param[in] y second argument * @return disjunction of the arguments' values */ -inline bool operator||(const var& x, const var& y) { return x.val() || y.val(); } +inline bool operator||(const var& x, const var& y) { + return x.val() || y.val(); +} /** * Return the logical disjunction of the values of the two diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 3990c103f8b..23f6ef74bc7 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -9,7 +9,8 @@ namespace stan { namespace math { template -template >*> +template >*> inline var_value& var_value::operator-=(const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 302af7d612f..b8abd44df25 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -9,7 +9,8 @@ namespace stan { namespace math { template -template >*> +template >*> inline var_value& var_value::operator*=(const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; diff --git a/stan/math/rev/core/operator_not_equal.hpp b/stan/math/rev/core/operator_not_equal.hpp index d7591eacfbb..b4f41b576d8 100644 --- a/stan/math/rev/core/operator_not_equal.hpp +++ b/stan/math/rev/core/operator_not_equal.hpp @@ -27,7 +27,9 @@ namespace math { * @return True if the first variable's value is not the same as the * second's. */ -inline bool operator!=(const var& a, const var& b) { return a.val() != b.val(); } +inline bool operator!=(const var& a, const var& b) { + return a.val() != b.val(); +} /** * Inequality operator comparing a variable's value and a double diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index 85d91100d78..e9c38129c40 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -9,7 +9,8 @@ namespace stan { namespace math { template -template >*> +template >*> inline var_value& var_value::operator+=(const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; diff --git a/stan/math/rev/core/precomp_vv_vari.hpp b/stan/math/rev/core/precomp_vv_vari.hpp index b5d2e512bee..f65e41605be 100644 --- a/stan/math/rev/core/precomp_vv_vari.hpp +++ b/stan/math/rev/core/precomp_vv_vari.hpp @@ -8,7 +8,7 @@ namespace stan { namespace math { // use for single precomputed partials -class precomp_vv_vari : public op_vv_vari { +class precomp_vv_vari final : public op_vv_vari { protected: double da_; double db_; diff --git a/stan/math/rev/core/std_iterator_traits.hpp b/stan/math/rev/core/std_iterator_traits.hpp index 9194128965c..09560538e88 100644 --- a/stan/math/rev/core/std_iterator_traits.hpp +++ b/stan/math/rev/core/std_iterator_traits.hpp @@ -36,7 +36,6 @@ struct iterator_traits> { * Type of reference to variables. */ typedef stan::math::var_value& reference; - }; } // namespace std diff --git a/stan/math/rev/core/std_numeric_limits.hpp b/stan/math/rev/core/std_numeric_limits.hpp index 79fe01fc701..6e3ba21ead7 100644 --- a/stan/math/rev/core/std_numeric_limits.hpp +++ b/stan/math/rev/core/std_numeric_limits.hpp @@ -16,8 +16,12 @@ template struct numeric_limits> { typedef stan::math::internal::floating_point_promoter value_type; static constexpr bool is_specialized = true; - static constexpr stan::math::var_value min() noexcept { return numeric_limits::min(); } - static constexpr stan::math::var_value max() noexcept { return numeric_limits::max(); } + static constexpr stan::math::var_value min() noexcept { + return numeric_limits::min(); + } + static constexpr stan::math::var_value max() noexcept { + return numeric_limits::max(); + } static constexpr int digits = numeric_limits::digits; static constexpr int digits10 = numeric_limits::digits10; static constexpr int max_digits10 = numeric_limits::max_digits10; @@ -25,7 +29,9 @@ struct numeric_limits> { static constexpr bool is_integer = numeric_limits::is_integer; static constexpr bool is_exact = numeric_limits::is_exact; static constexpr int radix = numeric_limits::radix; - static constexpr stan::math::var_value epsilon() noexcept { return numeric_limits::epsilon(); } + static constexpr stan::math::var_value epsilon() noexcept { + return numeric_limits::epsilon(); + } static constexpr stan::math::var_value round_error() noexcept { return numeric_limits::round_error(); } @@ -34,17 +40,21 @@ struct numeric_limits> { }; static constexpr int min_exponent = numeric_limits::min_exponent; - static constexpr int min_exponent10 = numeric_limits::min_exponent10; + static constexpr int min_exponent10 + = numeric_limits::min_exponent10; static constexpr int max_exponent = numeric_limits::max_exponent; - static constexpr int max_exponent10 = numeric_limits::max_exponent10; + static constexpr int max_exponent10 + = numeric_limits::max_exponent10; static constexpr bool has_infinity = numeric_limits::has_infinity; - static constexpr bool has_quiet_NaN = numeric_limits::has_quiet_NaN; + static constexpr bool has_quiet_NaN + = numeric_limits::has_quiet_NaN; static constexpr bool has_signaling_NaN = numeric_limits::has_signaling_NaN; static constexpr float_denorm_style has_denorm = numeric_limits::has_denorm; - static constexpr bool has_denorm_loss = numeric_limits::has_denorm_loss; + static constexpr bool has_denorm_loss + = numeric_limits::has_denorm_loss; static constexpr stan::math::var_value infinity() noexcept { return numeric_limits::infinity(); } @@ -63,7 +73,8 @@ struct numeric_limits> { static constexpr bool is_modulo = numeric_limits::is_modulo; static constexpr bool traps = numeric_limits::traps; - static constexpr bool tinyness_before = numeric_limits::tinyness_before; + static constexpr bool tinyness_before + = numeric_limits::tinyness_before; static constexpr float_round_style round_style = numeric_limits::round_style; }; diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 6aae6529e66..76b18cd5497 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -12,7 +13,8 @@ namespace stan { namespace math { // forward declare -static void grad(vari_base* vi); +template +static void grad(Vari* vi); /** * Independent (input) and dependent (output) variables for gradients. @@ -21,7 +23,7 @@ static void grad(vari_base* vi); * an arena-based memory manager scoped to a single gradient * calculation. * - * A var is constructed with a double and used like any + * A var is constructed with a type `T` and used like any * other scalar. Arithmetical functions like negation, addition, * and subtraction, as well as a range of mathematical functions * like exponentiation and powers are overridden to operate on @@ -30,16 +32,18 @@ static void grad(vari_base* vi); */ template class var_value { - public: - /** FIXME: This changes integral (int etc) types to double and leaves the type - * untouched otherwise. Since this can be an Eigen matrix it's a pretty - * dumb name. Just for the purposes of readability it may be better to - * use SFINAE on var_value and have a scalar and eigen representation. - * that would also get rid of some of the weirder requires here. - */ + // The internal value_type is always a floating point type using value_type = internal::floating_point_promoter; using vari_type = vari_value; + + template + using require_vari_convertible_t + = require_t*, vari_type*>>; + template + using require_not_vari_convertible_t = require_t< + bool_constant*, vari_type*>::value>>; + /** * Pointer to the implementation of this variable. * @@ -58,9 +62,7 @@ class var_value { * @return true if this variable does not yet have * a defined variable. */ - bool is_uninitialized() { - return (vi_ == static_cast(nullptr)); - } + bool is_uninitialized() { return (vi_ == static_cast(nullptr)); } /** * Construct a variable for later assignment. @@ -72,74 +74,68 @@ class var_value { var_value() : vi_(static_cast(nullptr)) {} /** - * Construct a variable from a pointer to a variable implementation. - * @tparam VariValue A vari_value whose pointer can be implicitly converted - * to vari_value*. - * @param vi Vari type. + * Construct a variable from the specified arithmetic type argument + * by constructing a new `vari_value`. For integral types the + * `vari_value` will hold doubles. This constructor is only valid + * when `S` is convertible to this `vari_value`'s `value_type`. + * @tparam S A type that is convertible to `value_type`. + * @param x Value of the variable. */ - template * = nullptr> - var_value(vari_value* vi) : vi_(vi) {} // NOLINT + template * = nullptr> + var_value(S x) : vi_(new vari_type(x, false)) {} // NOLINT /** * Construct a variable from a pointer to a variable implementation. - * @tparam VariValue A vari_value whose pointer can be implicitly converted - * to vari_value*. - * @param vi Vari type. + * @tparam S The type in the vari_value pointer that has the same `value_type` + * as this `var_value`. For integral types a `vari_value` is the + * same as `vari_value` so can be `reinterpret_cast` without a copy. + * @param vi A vari_value pointer. */ - template * = nullptr, - require_not_same_t* = nullptr> - var_value(vari_value* vi) : vi_(new vari_type(vi->val_, false)) { // NOLINT - this->vi_->adj_ = vi->adj_; - } + template , + require_vari_convertible_t* = nullptr> + var_value(vari_value* vi) // NOLINT + : vi_(reinterpret_cast(vi)) {} /** - * Construct a variable from the specified integral type argument - * by constructing a new `vari_value`. For integral types the - * `vari_value` will hold doubles. This constructor is only valid when - * `T` is arithmetic. - * @tparam IntegralT Integral type such as `int` - * @tparam T1 A dummy template whose value will always be `T` - * @param x Value of the variable. + * Construct a variable from a pointer to a variable implementation when. + * the `value_type`s differ. This forces a copy of the vari + * @tparam S the type in the vari_value* that does not have the same type + * as the `value_type` of this `var_value`. + * @param vi A `vari_value`. */ - template * = nullptr> - var_value(S x) : vi_(new vari_type(x, false)) {} // NOLINT + template , + require_not_vari_convertible_t* = nullptr> + var_value(vari_value* vi) : vi_(new vari_type(*vi)) {} // NOLINT /** * Construct from a var_value whose underlying value_type differs in type - * to this class's value_type. - * `var_value a(var_value(b));`` - * @tparam S An arithmetic type that is convertible to `T` but is not the + * to this class's value_type. This is for cases such as + * `var_value a(5.0); var_value b(a);` + * @tparam S A type that is not the same as the underlying value type. * same as the underlying value_type. * param x a `var_value` whose underlying vari_type can be dynamically cast * to `this::vari_value``. */ - template , - require_not_same_t* = nullptr> - var_value(var_value& x) : vi_(new vari_type(x.val(), false)) { // NOLINT - this->vi_->adj_ = x.adj(); - } - /** - * Same as the floating point - */ - template , - require_not_same_t* = nullptr> - var_value(var_value&& x) : vi_(new vari_type(x.val(), false)) { // NOLINT - this->vi_->adj_ = x.adj(); - } + template , + require_not_vari_convertible_t* = nullptr> + var_value(var_value& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT + + template , + require_not_vari_convertible_t* = nullptr> + var_value(var_value&& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT /** * Constructor from `var_value` whose value_type is the same as this class's * `value_type`. This is used in cases such as - * `var_value(var_value(4.0))` since the `value_type` for a - * `var_value` with an arithmetic type is a double. + * `var_value a(4.0); var_value b(a)` since the `value_type` for + * a `var_value` with an integral type is a double. */ - template , - require_same_t* = nullptr> + template , + require_vari_convertible_t* = nullptr> var_value(var_value& x) : vi_(x.vi_) {} // NOLINT - - template , - require_same_t* = nullptr> - var_value(var_value&& x) : vi_(x.vi_) {} // NOLINT + template , + require_vari_convertible_t* = nullptr> + var_value(var_value&& x) : vi_(x.vi_) {} // NOLINT /** * Return the value of this variable. @@ -223,11 +219,13 @@ class var_value { * then (a += b) behaves exactly the same way as (a = a + b), * creating an intermediate variable representing (a + b). * - * @tparam S a type that is convertible to `T` + * @tparam S a type that is convertible to `value_type` * @param b The variable to add to this variable. * @return The result of adding the specified variable to this variable. */ - template >* = nullptr> + template >* = nullptr> inline var_value& operator+=(const var_value& b); /** @@ -237,6 +235,7 @@ class var_value { * (a += b) behaves exactly the same way as (a = a + b). Note * that the result is an assignable lvalue. * + * @tparam Arith An arithmetic type * @param b The scalar to add to this variable. * @return The result of adding the specified variable to this variable. */ @@ -250,12 +249,14 @@ class var_value { * then (a -= b) behaves exactly the same way as (a = a - b). * Note that the result is an assignable lvalue. * - * @tparam S a type that is convertible to `T` + * @tparam S a type that is convertible to `value_type` * @param b The variable to subtract from this variable. * @return The result of subtracting the specified variable from * this variable. */ - template >* = nullptr> + template >* = nullptr> inline var_value& operator-=(const var_value& b); /** @@ -265,6 +266,7 @@ class var_value { * (a -= b) behaves exactly the same way as (a = a - b). Note * that the result is an assignable lvalue. * + * @tparam Arith An arithmetic type * @param b The scalar to subtract from this variable. * @return The result of subtracting the specified variable from this * variable. @@ -279,12 +281,14 @@ class var_value { * then (a *= b) behaves exactly the same way as (a = a * b). * Note that the result is an assignable lvalue. * - * @tparam S a type that is convertible to `T` + * @tparam S a type that is convertible to `value_type` * @param b The variable to multiply this variable by. * @return The result of multiplying this variable by the * specified variable. */ - template >* = nullptr> + template >* = nullptr> inline var_value& operator*=(const var_value& b); /** @@ -294,6 +298,7 @@ class var_value { * (a *= b) behaves exactly the same way as (a = a * b). Note * that the result is an assignable lvalue. * + * @tparam Arith An arithmetic type * @param b The scalar to multiply this variable by. * @return The result of multiplying this variable by the specified * variable. @@ -307,12 +312,14 @@ class var_value { * behaves exactly the same way as (a = a / b). Note that the * result is an assignable lvalue. * - * @tparam S a type that is convertible to `T` + * @tparam S a type that is convertible to `value_type` * @param b The variable to divide this variable by. * @return The result of dividing this variable by the * specified variable. */ - template >* = nullptr> + template >* = nullptr> inline var_value& operator/=(const var_value& b); /** @@ -322,6 +329,7 @@ class var_value { * (a /= b) behaves exactly the same way as (a = a / b). Note * that the result is an assignable lvalue. * + * @tparam Arith An arithmetic type * @param b The scalar to divide this variable by. * @return The result of dividing this variable by the specified * variable. @@ -343,7 +351,6 @@ class var_value { } return os << v.val(); } - }; // For backwards compatability the default value is double diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 4e4bd589465..9720d7b8132 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -22,7 +22,7 @@ class vari_base { * Apply the chain rule to this variable based on the variables * on which it depends. */ - virtual void chain() {}; + virtual void chain() {} /** * Set the adjoint value of this variable to 0. This is used to @@ -42,15 +42,15 @@ class vari_base { }; namespace internal { - /** - * Promote integral types to double - * @tparam Val Any type - */ - template - using floating_point_promoter - = std::conditional_t>::value, double, - std::decay_t>; -} +/** + * Promote integral types to double + * @tparam Val Any type + */ +template +using floating_point_promoter + = std::conditional_t>::value, double, + std::decay_t>; +} // namespace internal /** * The variable implementation base class. @@ -77,6 +77,7 @@ class vari_value::value>> private: template friend class var_value; + public: using Scalar = internal::floating_point_promoter; using value_type = Scalar; @@ -104,8 +105,9 @@ class vari_value::value>> * @tparam S an Arithmetic type. * @param x Value of the constructed variable. */ - template ::value>* = nullptr> - vari_value(S x) : val_(x), adj_(0.0) { + template ::value>* = nullptr> + vari_value(S x) : val_(x), adj_(0.0) { // NOLINT ChainableStack::instance_->var_stack_.push_back(this); } @@ -126,8 +128,9 @@ class vari_value::value>> * @param stacked If true will put this this vari on the nochain stack so that * it's `chain()` method is not called. */ - template ::value>* = nullptr> - vari_value(S x, bool stacked) : val_(x), adj_(0.0) { + template ::value>* = nullptr> + vari_value(S x, bool stacked) : val_(x), adj_(0.0) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -135,6 +138,23 @@ class vari_value::value>> } } + /** + * Constructor from vari_value + * @tparam S An arithmetic type + * @param x A vari_value + */ + template ::value>* = nullptr> + vari_value(vari_value& x) : val_(x.val_), adj_(x.adj_) { + ChainableStack::instance_->var_stack_.push_back(this); + } + + template ::value>* = nullptr> + vari_value(vari_value&& x) : val_(x.val_), adj_(x.adj_) { + ChainableStack::instance_->var_stack_.push_back(this); + } + /** * Initialize the adjoint for this (dependent) variable to 1. * This operation is applied to the dependent variable before diff --git a/stan/math/rev/core/vector_vari.hpp b/stan/math/rev/core/vector_vari.hpp index 55de53af4a8..57f7ebed6c6 100644 --- a/stan/math/rev/core/vector_vari.hpp +++ b/stan/math/rev/core/vector_vari.hpp @@ -15,7 +15,8 @@ class op_vector_vari : public vari { vari** vis_; public: - template * = nullptr, + template * = nullptr, require_vector_like_vt* = nullptr> op_vector_vari(Arith f, VecVar&& vs) : vari(f), size_(vs.size()) { vis_ = reinterpret_cast(operator new(sizeof(vari*) * vs.size())); diff --git a/stan/math/rev/meta/is_var.hpp b/stan/math/rev/meta/is_var.hpp index a44745393c9..e4d9fa54fed 100644 --- a/stan/math/rev/meta/is_var.hpp +++ b/stan/math/rev/meta/is_var.hpp @@ -8,17 +8,19 @@ namespace stan { namespace internal { - template - struct is_var_impl : std::false_type {}; +template +struct is_var_impl : std::false_type {}; - template - struct is_var_impl> : std::true_type {}; -} +template +struct is_var_impl> : std::true_type {}; +} // namespace internal /** \ingroup type_trait * Specialization for checking if value of T minus cv qualifier is a var_value. */ template -struct is_var>::value>> : std::true_type {}; +struct is_var>::value>> + : std::true_type {}; } // namespace stan #endif diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 6284ce983bd..b33361cbd37 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -19,24 +19,26 @@ void ctor_overloads_impl() { using stan::math::var_value; using stan::math::vari_value; using stan::math::test::type_name; + // standard constructor + EXPECT_FLOAT_EQ(3.7, var_value(3.7).val()) + << "Failed For T: " << type_name() << " and S: " << type_name() + << "\n"; // make sure copy ctor is used rather than casting vari* to unsigned int - EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) << - "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; - - EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) << - "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; - - EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) << - "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; - // double - EXPECT_FLOAT_EQ(3.7, var_value(3.7).val()) << - "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; - // long double - EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) << - "Failed For T: " << type_name() << " and S: " << type_name() << "\n"; - EXPECT_FLOAT_EQ(0, var_value(static_cast(0)).val()) << - "Failed For T: " << type_name() << " and S:" << type_name() << "\n"; - + EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) + << "Failed For T: " << type_name() << " and S: " << type_name() + << "\n"; + // make sure rvalue var_value can be accepted + EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) + << "Failed For T: " << type_name() << " and S: " << type_name() + << "\n"; + // S type is preserved + EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) + << "Failed For T: " << type_name() << " and S: " << type_name() + << "\n"; + // Make sure integral types don't hold a nullptr instead of zero. + EXPECT_FLOAT_EQ(0, var_value(static_cast(0)).val()) + << "Failed For T: " << type_name() << " and S:" << type_name() + << "\n"; } template diff --git a/test/unit/math/rev/meta/is_var_test.cpp b/test/unit/math/rev/meta/is_var_test.cpp index 2a3f4e19a79..fd74125efa7 100644 --- a/test/unit/math/rev/meta/is_var_test.cpp +++ b/test/unit/math/rev/meta/is_var_test.cpp @@ -3,5 +3,12 @@ TEST(MetaTraitsRevScal, is_var) { using stan::is_var; + using stan::math::var_value; + using stan::math::var; EXPECT_TRUE(is_var::value); + EXPECT_TRUE((is_var>::value)); + EXPECT_TRUE((is_var>::value)); + EXPECT_FALSE(is_var::value); + EXPECT_FALSE((is_var::value)); + EXPECT_FALSE((is_var>::value)); } diff --git a/test/unit/math/rev/meta/is_vari_test.cpp b/test/unit/math/rev/meta/is_vari_test.cpp new file mode 100644 index 00000000000..14574c32a14 --- /dev/null +++ b/test/unit/math/rev/meta/is_vari_test.cpp @@ -0,0 +1,14 @@ +#include +#include + +TEST(MetaTraitsRevScal, is_vari) { + using stan::is_vari; + using stan::math::vari_value; + using stan::math::vari; + EXPECT_TRUE(is_vari::value); + EXPECT_TRUE((is_vari>::value)); + EXPECT_TRUE((is_vari>::value)); + EXPECT_FALSE(is_vari::value); + EXPECT_FALSE((is_vari::value)); + EXPECT_FALSE((is_vari>::value)); +} From 92630dd3d5dedc6696ca902c433fe73aa821e469 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 29 May 2020 17:58:42 -0400 Subject: [PATCH 017/167] add ref to require_convertible check in vari constructors --- stan/math/rev/core/vari.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 9720d7b8132..2f9ed9b56fc 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -106,7 +106,7 @@ class vari_value::value>> * @param x Value of the constructed variable. */ template ::value>* = nullptr> + std::enable_if_t::value>* = nullptr> vari_value(S x) : val_(x), adj_(0.0) { // NOLINT ChainableStack::instance_->var_stack_.push_back(this); } @@ -129,7 +129,7 @@ class vari_value::value>> * it's `chain()` method is not called. */ template ::value>* = nullptr> + std::enable_if_t::value>* = nullptr> vari_value(S x, bool stacked) : val_(x), adj_(0.0) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); From 2dfa329a9cd29fdf153966dc970788c791e52dea Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 29 May 2020 22:45:11 -0400 Subject: [PATCH 018/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/fun/scale_matrix_exp_multiply.hpp | 4 ++-- stan/math/prim/meta/is_var.hpp | 1 - stan/math/prim/meta/is_vari.hpp | 1 - stan/math/rev/core/var.hpp | 2 +- stan/math/rev/core/vari.hpp | 2 +- stan/math/rev/fun/value_of.hpp | 4 +++- stan/math/rev/fun/value_of_rec.hpp | 4 +++- stan/math/rev/meta/is_vari.hpp | 1 - test/unit/math/rev/meta/is_var_test.cpp | 2 +- test/unit/math/rev/meta/is_vari_test.cpp | 2 +- 10 files changed, 12 insertions(+), 11 deletions(-) diff --git a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp index 217c7c8b6a2..5b21af83058 100644 --- a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp +++ b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp @@ -27,7 +27,7 @@ namespace math { template * = nullptr, require_all_same_t, - value_type_t>* = nullptr> + value_type_t>* = nullptr> inline Eigen::Matrix scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { check_square("scale_matrix_exp_multiply", "input matrix", A); @@ -56,7 +56,7 @@ scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { template * = nullptr, require_any_not_same_t, - value_type_t>* = nullptr> + value_type_t>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, EigMat2::ColsAtCompileTime> scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) { diff --git a/stan/math/prim/meta/is_var.hpp b/stan/math/prim/meta/is_var.hpp index db87cc9e2ee..d9264d0afeb 100644 --- a/stan/math/prim/meta/is_var.hpp +++ b/stan/math/prim/meta/is_var.hpp @@ -17,7 +17,6 @@ STAN_ADD_REQUIRE_UNARY(var, is_var, require_stan_scalar_real); STAN_ADD_REQUIRE_CONTAINER(var, is_var, require_stan_scalar_real); STAN_ADD_REQUIRE_UNARY_INNER(var, is_var, require_stan_scalar_real); - template struct value_type::value>> { using type = typename std::decay_t::value_type; diff --git a/stan/math/prim/meta/is_vari.hpp b/stan/math/prim/meta/is_vari.hpp index db82abbf9a9..4ee384a303c 100644 --- a/stan/math/prim/meta/is_vari.hpp +++ b/stan/math/prim/meta/is_vari.hpp @@ -14,6 +14,5 @@ struct is_vari : std::false_type {}; STAN_ADD_REQUIRE_UNARY(vari, is_vari, require_stan_scalar_real); STAN_ADD_REQUIRE_CONTAINER(vari, is_vari, require_stan_scalar_real); - } // namespace stan #endif diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 76b18cd5497..564deb7b606 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -93,7 +93,7 @@ class var_value { */ template , require_vari_convertible_t* = nullptr> - var_value(vari_value* vi) // NOLINT + var_value(vari_value* vi) // NOLINT : vi_(reinterpret_cast(vi)) {} /** diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 2f9ed9b56fc..edfd0672a47 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -107,7 +107,7 @@ class vari_value::value>> */ template ::value>* = nullptr> - vari_value(S x) : val_(x), adj_(0.0) { // NOLINT + vari_value(S x) : val_(x), adj_(0.0) { // NOLINT ChainableStack::instance_->var_stack_.push_back(this); } diff --git a/stan/math/rev/fun/value_of.hpp b/stan/math/rev/fun/value_of.hpp index 162a9834618..b44578eb955 100644 --- a/stan/math/rev/fun/value_of.hpp +++ b/stan/math/rev/fun/value_of.hpp @@ -21,7 +21,9 @@ namespace math { * @return Value of variable. */ template -inline auto value_of(const var_value& v) { return v.vi_->val_; } +inline auto value_of(const var_value& v) { + return v.vi_->val_; +} } // namespace math } // namespace stan diff --git a/stan/math/rev/fun/value_of_rec.hpp b/stan/math/rev/fun/value_of_rec.hpp index 5f6a477d86b..e02ff980afe 100644 --- a/stan/math/rev/fun/value_of_rec.hpp +++ b/stan/math/rev/fun/value_of_rec.hpp @@ -15,7 +15,9 @@ namespace math { * @return Value of variable. */ template -inline auto value_of_rec(const var_value& v) { return v.vi_->val_; } +inline auto value_of_rec(const var_value& v) { + return v.vi_->val_; +} } // namespace math } // namespace stan diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index 84c5d79cb85..c91364bbe3c 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -16,6 +16,5 @@ struct is_vari< math::vari_base, std::remove_pointer_t>>::value>> : std::true_type {}; - } // namespace stan #endif diff --git a/test/unit/math/rev/meta/is_var_test.cpp b/test/unit/math/rev/meta/is_var_test.cpp index fd74125efa7..75ef61d9596 100644 --- a/test/unit/math/rev/meta/is_var_test.cpp +++ b/test/unit/math/rev/meta/is_var_test.cpp @@ -3,8 +3,8 @@ TEST(MetaTraitsRevScal, is_var) { using stan::is_var; - using stan::math::var_value; using stan::math::var; + using stan::math::var_value; EXPECT_TRUE(is_var::value); EXPECT_TRUE((is_var>::value)); EXPECT_TRUE((is_var>::value)); diff --git a/test/unit/math/rev/meta/is_vari_test.cpp b/test/unit/math/rev/meta/is_vari_test.cpp index 14574c32a14..9b5ab9e90c5 100644 --- a/test/unit/math/rev/meta/is_vari_test.cpp +++ b/test/unit/math/rev/meta/is_vari_test.cpp @@ -3,8 +3,8 @@ TEST(MetaTraitsRevScal, is_vari) { using stan::is_vari; - using stan::math::vari_value; using stan::math::vari; + using stan::math::vari_value; EXPECT_TRUE(is_vari::value); EXPECT_TRUE((is_vari>::value)); EXPECT_TRUE((is_vari>::value)); From f04527225d41a5f4ae3c79a0868651cdcf394df0 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 29 May 2020 23:44:09 -0400 Subject: [PATCH 019/167] fix header check --- stan/math/prim/meta/is_vari.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/stan/math/prim/meta/is_vari.hpp b/stan/math/prim/meta/is_vari.hpp index 4ee384a303c..b2f31cd27b3 100644 --- a/stan/math/prim/meta/is_vari.hpp +++ b/stan/math/prim/meta/is_vari.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_PRIM_META_IS_VARI_HPP #define STAN_MATH_PRIM_META_IS_VARI_HPP +#include #include namespace stan { From 4b5fc677510e76e1cc43ebeacf765eb8717271f3 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 30 May 2020 00:44:22 -0400 Subject: [PATCH 020/167] Fix templates for hmm --- stan/math/prim/prob/hmm_marginal_lpdf.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/prim/prob/hmm_marginal_lpdf.hpp b/stan/math/prim/prob/hmm_marginal_lpdf.hpp index 67968c6d9b9..90076649d30 100644 --- a/stan/math/prim/prob/hmm_marginal_lpdf.hpp +++ b/stan/math/prim/prob/hmm_marginal_lpdf.hpp @@ -20,7 +20,7 @@ template * = nullptr, require_all_eigen_col_vector_t* = nullptr, require_stan_scalar_t* = nullptr, - require_all_vt_same* = nullptr> + require_all_same_t, value_type_t, T_norm>* = nullptr> inline auto hmm_marginal_lpdf_val(const T_omega& omegas, const T_Gamma& Gamma_val, const T_rho& rho_val, T_alphas& alphas, From 1fe6e2b85abeb46e1227debdd418c8d2270a5267 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sat, 30 May 2020 00:45:08 -0400 Subject: [PATCH 021/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/prob/hmm_marginal_lpdf.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stan/math/prim/prob/hmm_marginal_lpdf.hpp b/stan/math/prim/prob/hmm_marginal_lpdf.hpp index 90076649d30..7166f524e34 100644 --- a/stan/math/prim/prob/hmm_marginal_lpdf.hpp +++ b/stan/math/prim/prob/hmm_marginal_lpdf.hpp @@ -20,7 +20,8 @@ template * = nullptr, require_all_eigen_col_vector_t* = nullptr, require_stan_scalar_t* = nullptr, - require_all_same_t, value_type_t, T_norm>* = nullptr> + require_all_same_t, + value_type_t, T_norm>* = nullptr> inline auto hmm_marginal_lpdf_val(const T_omega& omegas, const T_Gamma& Gamma_val, const T_rho& rho_val, T_alphas& alphas, From b63acdc3e535db419f4d0d4e2d0d668cd80a33b3 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 30 May 2020 02:47:23 -0400 Subject: [PATCH 022/167] add template to lp value in in positive_constrain --- stan/math/prim/fun/positive_constrain.hpp | 4 ++-- stan/math/rev/fun/mdivide_left_tri.hpp | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/stan/math/prim/fun/positive_constrain.hpp b/stan/math/prim/fun/positive_constrain.hpp index 87059c835af..8f4ad86965a 100644 --- a/stan/math/prim/fun/positive_constrain.hpp +++ b/stan/math/prim/fun/positive_constrain.hpp @@ -40,8 +40,8 @@ inline auto positive_constrain(const T& x) { * @param lp log density reference. * @return positive constrained version of unconstrained value(s) */ -template -inline auto positive_constrain(const T& x, value_type_t& lp) { +template +inline auto positive_constrain(const T& x, S& lp) { lp += sum(x); return exp(x); } diff --git a/stan/math/rev/fun/mdivide_left_tri.hpp b/stan/math/rev/fun/mdivide_left_tri.hpp index a0c6319a8d1..5858cace163 100644 --- a/stan/math/rev/fun/mdivide_left_tri.hpp +++ b/stan/math/rev/fun/mdivide_left_tri.hpp @@ -337,8 +337,7 @@ mdivide_left_tri(const T1 &A, const T2 &b) { return res; } template * = nullptr, - require_vt_same * = nullptr, + require_eigen_vt * = nullptr, require_eigen_vt * = nullptr> inline Eigen::Matrix mdivide_left_tri(const T1 &A, const T2 &b) { @@ -365,8 +364,7 @@ mdivide_left_tri(const T1 &A, const T2 &b) { } template * = nullptr, - require_eigen_t * = nullptr, - require_vt_same * = nullptr> + require_eigen_vt * = nullptr> inline Eigen::Matrix mdivide_left_tri(const T1 &A, const T2 &b) { check_square("mdivide_left_tri", "A", A); From f20d98aaa047ee64ee32586af763d1cf62771fb9 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 30 May 2020 17:16:46 -0400 Subject: [PATCH 023/167] replace vector func value types in apply_scalar_binary with return type of function --- stan/math/prim/fun/cholesky_decompose.hpp | 14 ++++++-------- stan/math/prim/meta/apply_scalar_binary.hpp | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index 571fa106f8c..e9e0fd5a3e7 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -29,9 +29,7 @@ namespace math { * @throw std::domain_error if m is not a symmetric matrix or * if m is not positive definite (if m has more than 0 elements) */ -template * = nullptr, - require_not_vt_same* = nullptr, - require_not_eigen_vt* = nullptr> +template * = nullptr> inline Eigen::Matrix, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime> cholesky_decompose(const EigMat& m) { @@ -61,11 +59,11 @@ cholesky_decompose(const EigMat& m) { * @throw std::domain_error if m is not a symmetric matrix or * if m is not positive definite (if m has more than 0 elements) */ -template * = nullptr, - require_vt_same* = nullptr> -inline Eigen::Matrix* = nullptr> +inline Eigen::Matrix, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime> cholesky_decompose(const EigMat& m) { + using eig_val = value_type_t; eval_return_type_t& m_eval = m.eval(); check_not_nan("cholesky_decompose", "m", m_eval); #ifdef STAN_OPENCL @@ -74,7 +72,7 @@ cholesky_decompose(const EigMat& m) { return from_matrix_cl(cholesky_decompose(m_cl)); } else { check_symmetric("cholesky_decompose", "m", m_eval); - Eigen::LLT> llt = m_eval.llt(); check_pos_definite("cholesky_decompose", "m", llt); @@ -82,7 +80,7 @@ cholesky_decompose(const EigMat& m) { } #else check_symmetric("cholesky_decompose", "m", m_eval); - Eigen::LLT> llt = m_eval.llt(); check_pos_definite("cholesky_decompose", "m", llt); diff --git a/stan/math/prim/meta/apply_scalar_binary.hpp b/stan/math/prim/meta/apply_scalar_binary.hpp index 8e755beb1df..0768fd9b29e 100644 --- a/stan/math/prim/meta/apply_scalar_binary.hpp +++ b/stan/math/prim/meta/apply_scalar_binary.hpp @@ -124,7 +124,7 @@ template ; + using T_return = std::decay_t; std::vector result(x.size()); Eigen::Map>(result.data(), result.size()) = x_vec.binaryExpr(y_vec, f); @@ -154,7 +154,7 @@ template * = nullptr> inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { decltype(auto) x_vec = as_column_vector_or_scalar(x); - using T_return = value_type_t; + using T_return = std::decay_t; std::vector result(x.size()); Eigen::Map>(result.data(), result.size()) = x_vec.unaryExpr([&f, &y](const auto& v) { return f(v, y); }); @@ -184,7 +184,7 @@ template * = nullptr> inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { decltype(auto) y_vec = as_column_vector_or_scalar(y); - using T_return = value_type_t; + using T_return = std::decay_t; std::vector result(y.size()); Eigen::Map>(result.data(), result.size()) = y_vec.unaryExpr([&f, &x](const auto& v) { return f(x, v); }); From 16db81b55989c118fa02e98a887515d2b0dc664b Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sat, 30 May 2020 17:17:34 -0400 Subject: [PATCH 024/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/fun/cholesky_decompose.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index e9e0fd5a3e7..aff4ad50235 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -59,7 +59,8 @@ cholesky_decompose(const EigMat& m) { * @throw std::domain_error if m is not a symmetric matrix or * if m is not positive definite (if m has more than 0 elements) */ -template * = nullptr> +template * = nullptr> inline Eigen::Matrix, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime> cholesky_decompose(const EigMat& m) { From 4cc404e424e0a6ef358a1967f86d274d7d570beb Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 1 Jun 2020 18:51:30 -0400 Subject: [PATCH 025/167] var and vari can only be templated with floating point types. Remove double_promoter and use promote_args instead. Add flto compiler flags for gcc and clang --- make/compiler_flags | 9 +-- stan/math/prim/fun/cholesky_decompose.hpp | 2 +- .../prim/fun/scale_matrix_exp_multiply.hpp | 7 +- stan/math/rev/core/operator_divide_equal.hpp | 6 +- stan/math/rev/core/operator_minus_equal.hpp | 6 +- .../math/rev/core/operator_multiply_equal.hpp | 6 +- stan/math/rev/core/operator_plus_equal.hpp | 6 +- stan/math/rev/core/std_numeric_limits.hpp | 2 +- stan/math/rev/core/var.hpp | 52 +++++++-------- stan/math/rev/core/vari.hpp | 32 +++------- test/unit/math/rev/core/var_test.cpp | 64 ++++++++----------- 11 files changed, 79 insertions(+), 113 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index de486bffc43..e64fa6f474f 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -28,7 +28,7 @@ ifeq ($(OS),Linux) LIBRARY_SUFFIX ?= .so endif -## Set default compiler +## Set default compiler and flto compiler flags ifeq (default,$(origin CXX)) ifeq ($(OS),Darwin) ## Darwin is Mac OS X CXX := clang++ @@ -47,15 +47,18 @@ endif # - CXX_MINOR: minor version of CXX ifneq (,$(findstring clang,$(CXX))) CXX_TYPE ?= clang + CXXFLAGS_FLTO := -flto -fwhole-program-vtables -fstrict-vtable-pointers endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc endif ifneq (,$(findstring gcc,$(CXX))) CXX_TYPE ?= gcc + CXXFLAGS_FLTO := -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta endif ifneq (,$(findstring g++,$(CXX))) CXX_TYPE ?= gcc + CXXFLAGS_FLTO := -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta endif CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) @@ -254,9 +257,7 @@ ifdef STAN_MPI CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor endif - - -CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) +CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_FLTO) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index aff4ad50235..e227d604eaa 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -68,7 +68,7 @@ cholesky_decompose(const EigMat& m) { eval_return_type_t& m_eval = m.eval(); check_not_nan("cholesky_decompose", "m", m_eval); #ifdef STAN_OPENCL - if (m.rows() >= opencl_context.tuning_opts().cholesky_size_worth_transfer) { + if (std::is_same::value && m.rows() >= opencl_context.tuning_opts().cholesky_size_worth_transfer) { matrix_cl m_cl(m_eval); return from_matrix_cl(cholesky_decompose(m_cl)); } else { diff --git a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp index 5b21af83058..f59aadbd816 100644 --- a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp +++ b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp @@ -25,9 +25,7 @@ namespace math { * @return exponential of At multiplied by B */ template * = nullptr, - require_all_same_t, - value_type_t>* = nullptr> + require_all_eigen_vt* = nullptr> inline Eigen::Matrix scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { check_square("scale_matrix_exp_multiply", "input matrix", A); @@ -55,8 +53,7 @@ scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { */ template * = nullptr, - require_any_not_same_t, - value_type_t>* = nullptr> + require_any_autodiff_t, value_type_t>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, EigMat2::ColsAtCompileTime> scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) { diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 25d0ae082bd..0a82b3c0e91 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -9,15 +9,15 @@ namespace stan { namespace math { template template >*> -inline var_value& var_value::operator/=(const var_value& b) { + require_convertible_t>*> +inline var_value>& var_value>::operator/=(const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value::operator/=(Arith b) { +inline var_value>& var_value>::operator/=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 23f6ef74bc7..fee058d7e9d 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -10,15 +10,15 @@ namespace math { template template >*> -inline var_value& var_value::operator-=(const var_value& b) { + require_convertible_t>*> +inline var_value>& var_value>::operator-=(const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value::operator-=(Arith b) { +inline var_value>& var_value>::operator-=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index b8abd44df25..95176df2ab2 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -10,15 +10,15 @@ namespace math { template template >*> -inline var_value& var_value::operator*=(const var_value& b) { + require_convertible_t>*> +inline var_value>& var_value>::operator*=(const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value::operator*=(Arith b) { +inline var_value>& var_value>::operator*=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index e9c38129c40..5edda2b3d88 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -10,15 +10,15 @@ namespace math { template template >*> -inline var_value& var_value::operator+=(const var_value& b) { + require_convertible_t>*> +inline var_value>& var_value>::operator+=(const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value::operator+=(Arith b) { +inline var_value>& var_value>::operator+=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/std_numeric_limits.hpp b/stan/math/rev/core/std_numeric_limits.hpp index 6e3ba21ead7..f7cf545f4e7 100644 --- a/stan/math/rev/core/std_numeric_limits.hpp +++ b/stan/math/rev/core/std_numeric_limits.hpp @@ -14,7 +14,7 @@ namespace std { */ template struct numeric_limits> { - typedef stan::math::internal::floating_point_promoter value_type; + typedef stan::promote_args_t value_type; static constexpr bool is_specialized = true; static constexpr stan::math::var_value min() noexcept { return numeric_limits::min(); diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 564deb7b606..c2fb3b1dc19 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -30,11 +30,14 @@ static void grad(Vari* vi); * var values objects. * @tparam T An Arithmetic type. */ +template +class var_value {}; + template -class var_value { +class var_value> { public: // The internal value_type is always a floating point type - using value_type = internal::floating_point_promoter; + using value_type = std::decay_t; using vari_type = vari_value; template @@ -91,10 +94,10 @@ class var_value { * same as `vari_value` so can be `reinterpret_cast` without a copy. * @param vi A vari_value pointer. */ - template , + template , require_vari_convertible_t* = nullptr> var_value(vari_value* vi) // NOLINT - : vi_(reinterpret_cast(vi)) {} + : vi_(vi) {} /** * Construct a variable from a pointer to a variable implementation when. @@ -103,7 +106,7 @@ class var_value { * as the `value_type` of this `var_value`. * @param vi A `vari_value`. */ - template , + template , require_not_vari_convertible_t* = nullptr> var_value(vari_value* vi) : vi_(new vari_type(*vi)) {} // NOLINT @@ -116,13 +119,9 @@ class var_value { * param x a `var_value` whose underlying vari_type can be dynamically cast * to `this::vari_value``. */ - template , - require_not_vari_convertible_t* = nullptr> - var_value(var_value& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT - - template , + template , require_not_vari_convertible_t* = nullptr> - var_value(var_value&& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT + var_value(const var_value& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT /** * Constructor from `var_value` whose value_type is the same as this class's @@ -130,12 +129,9 @@ class var_value { * `var_value a(4.0); var_value b(a)` since the `value_type` for * a `var_value` with an integral type is a double. */ - template , - require_vari_convertible_t* = nullptr> - var_value(var_value& x) : vi_(x.vi_) {} // NOLINT - template , + template , require_vari_convertible_t* = nullptr> - var_value(var_value&& x) : vi_(x.vi_) {} // NOLINT + var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT /** * Return the value of this variable. @@ -225,8 +221,8 @@ class var_value { */ template >* = nullptr> - inline var_value& operator+=(const var_value& b); + S&, promote_args_t>* = nullptr> + inline var_value>& operator+=(const var_value& b); /** * The compound add/assignment operator for scalars (C++). @@ -240,7 +236,7 @@ class var_value { * @return The result of adding the specified variable to this variable. */ template * = nullptr> - inline var_value& operator+=(Arith b); + inline var_value>& operator+=(Arith b); /** * The compound subtract/assignment operator for variables (C++). @@ -256,8 +252,8 @@ class var_value { */ template >* = nullptr> - inline var_value& operator-=(const var_value& b); + S&, promote_args_t>* = nullptr> + inline var_value>& operator-=(const var_value& b); /** * The compound subtract/assignment operator for scalars (C++). @@ -272,7 +268,7 @@ class var_value { * variable. */ template * = nullptr> - inline var_value& operator-=(Arith b); + inline var_value>& operator-=(Arith b); /** * The compound multiply/assignment operator for variables (C++). @@ -288,8 +284,8 @@ class var_value { */ template >* = nullptr> - inline var_value& operator*=(const var_value& b); + S&, promote_args_t>* = nullptr> + inline var_value>& operator*=(const var_value& b); /** * The compound multiply/assignment operator for scalars (C++). @@ -304,7 +300,7 @@ class var_value { * variable. */ template * = nullptr> - inline var_value& operator*=(Arith b); + inline var_value>& operator*=(Arith b); /** * The compound divide/assignment operator for variables (C++). If this @@ -319,8 +315,8 @@ class var_value { */ template >* = nullptr> - inline var_value& operator/=(const var_value& b); + S&, promote_args_t>* = nullptr> + inline var_value>& operator/=(const var_value& b); /** * The compound divide/assignment operator for scalars (C++). @@ -335,7 +331,7 @@ class var_value { * variable. */ template * = nullptr> - inline var_value& operator/=(Arith b); + inline var_value>& operator/=(Arith b); /** * Write the value of this autodiff variable and its adjoint to diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index edfd0672a47..726e785ba12 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -10,11 +11,11 @@ namespace stan { namespace math { // forward declaration of var -template +template class var_value; /** - * Pure virtual class that all `vari_value` and it's derived classes inherit. + * Abstract class that all `vari_value` and it's derived classes inherit. */ class vari_base { public: @@ -41,17 +42,6 @@ class vari_base { virtual ~vari_base() {} }; -namespace internal { -/** - * Promote integral types to double - * @tparam Val Any type - */ -template -using floating_point_promoter - = std::conditional_t>::value, double, - std::decay_t>; -} // namespace internal - /** * The variable implementation base class. * @@ -72,14 +62,14 @@ template class vari_value; template -class vari_value::value>> +class vari_value::value>> : public vari_base { private: - template + template friend class var_value; public: - using Scalar = internal::floating_point_promoter; + using Scalar = T; using value_type = Scalar; /** * The value of this variable. @@ -125,7 +115,7 @@ class vari_value::value>> * * @tparam S an Arithmetic type. * @param x Value of the constructed variable. - * @param stacked If true will put this this vari on the nochain stack so that + * @param stacked If false will put this this vari on the nochain stack so that * it's `chain()` method is not called. */ template ::value>> */ template ::value>* = nullptr> - vari_value(vari_value& x) : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.push_back(this); - } - - template ::value>* = nullptr> - vari_value(vari_value&& x) : val_(x.val_), adj_(x.adj_) { + vari_value(const vari_value& x) : val_(x.val_), adj_(x.adj_) { ChainableStack::instance_->var_stack_.push_back(this); } diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index b33361cbd37..74567e4aa73 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -14,22 +14,21 @@ struct AgradRev : public testing::Test { } }; -template -void ctor_overloads_impl() { +template +void ctor_overloads_impl_impl() { using stan::math::var_value; using stan::math::vari_value; using stan::math::test::type_name; // standard constructor EXPECT_FLOAT_EQ(3.7, var_value(3.7).val()) - << "Failed For T: " << type_name() << " and S: " << type_name() - << "\n"; + << "Failed For T: " << type_name() << "\n"; // make sure copy ctor is used rather than casting vari* to unsigned int - EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) - << "Failed For T: " << type_name() << " and S: " << type_name() + EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) + << "Failed For T: " << type_name() << " and K: " << type_name() << "\n"; // make sure rvalue var_value can be accepted - EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) - << "Failed For T: " << type_name() << " and S: " << type_name() + EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) + << "Failed For T: " << type_name() << " and K: " << type_name() << "\n"; // S type is preserved EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) @@ -41,38 +40,35 @@ void ctor_overloads_impl() { << "\n"; } +template +void ctor_overloads_impl() { + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); + ctor_overloads_impl_impl(); +} template void ctor_overloads() { + ctor_overloads_impl(); ctor_overloads_impl(); ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); } + TEST_F(AgradRev, ctorOverloads) { using stan::math::var; using stan::math::vari; + ctor_overloads(); ctor_overloads(); ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); } TEST_F(AgradRev, a_eq_x) { @@ -285,11 +281,3 @@ TEST_F(AgradRev, grad) { EXPECT_FLOAT_EQ(11.0, a.adj()); EXPECT_FLOAT_EQ(5.0, b.adj()); } - -TEST_F(AgradRev, type_convs) { - using stan::math::var_value; - var_value a(1); - var_value b(a); - var_value c(a); - var_value d(a); -} From 75910a11cf854851023d8a429bc4596ae3e92089 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 3 Jun 2020 13:01:15 -0400 Subject: [PATCH 026/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/fun/cholesky_decompose.hpp | 4 ++- .../prim/fun/scale_matrix_exp_multiply.hpp | 3 +- stan/math/rev/core/operator_divide_equal.hpp | 9 +++--- stan/math/rev/core/operator_minus_equal.hpp | 9 +++--- .../math/rev/core/operator_multiply_equal.hpp | 9 +++--- stan/math/rev/core/operator_plus_equal.hpp | 9 +++--- stan/math/rev/core/var.hpp | 28 ++++++++----------- stan/math/rev/core/vari.hpp | 4 +-- 8 files changed, 39 insertions(+), 36 deletions(-) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index e227d604eaa..fe64e168b81 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -68,7 +68,9 @@ cholesky_decompose(const EigMat& m) { eval_return_type_t& m_eval = m.eval(); check_not_nan("cholesky_decompose", "m", m_eval); #ifdef STAN_OPENCL - if (std::is_same::value && m.rows() >= opencl_context.tuning_opts().cholesky_size_worth_transfer) { + if (std::is_same::value + && m.rows() + >= opencl_context.tuning_opts().cholesky_size_worth_transfer) { matrix_cl m_cl(m_eval); return from_matrix_cl(cholesky_decompose(m_cl)); } else { diff --git a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp index f59aadbd816..7901aafc80e 100644 --- a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp +++ b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp @@ -53,7 +53,8 @@ scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) { */ template * = nullptr, - require_any_autodiff_t, value_type_t>* = nullptr> + require_any_autodiff_t, + value_type_t>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, EigMat2::ColsAtCompileTime> scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) { diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 0a82b3c0e91..3b158118805 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -8,16 +8,17 @@ namespace stan { namespace math { template -template >*> -inline var_value>& var_value>::operator/=(const var_value& b) { +template >*> +inline var_value>& +var_value>::operator/=(const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& var_value>::operator/=(Arith b) { +inline var_value>& +var_value>::operator/=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index fee058d7e9d..2792d11fbdb 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -9,16 +9,17 @@ namespace stan { namespace math { template -template >*> -inline var_value>& var_value>::operator-=(const var_value& b) { +template >*> +inline var_value>& +var_value>::operator-=(const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& var_value>::operator-=(Arith b) { +inline var_value>& +var_value>::operator-=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 95176df2ab2..876ec9c8372 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -9,16 +9,17 @@ namespace stan { namespace math { template -template >*> -inline var_value>& var_value>::operator*=(const var_value& b) { +template >*> +inline var_value>& +var_value>::operator*=(const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& var_value>::operator*=(Arith b) { +inline var_value>& +var_value>::operator*=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index 5edda2b3d88..12c4ddf2d72 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -9,16 +9,17 @@ namespace stan { namespace math { template -template >*> -inline var_value>& var_value>::operator+=(const var_value& b) { +template >*> +inline var_value>& +var_value>::operator+=(const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& var_value>::operator+=(Arith b) { +inline var_value>& +var_value>::operator+=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index c2fb3b1dc19..b74b7d35fc1 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -219,10 +219,9 @@ class var_value> { * @param b The variable to add to this variable. * @return The result of adding the specified variable to this variable. */ - template >* = nullptr> - inline var_value>& operator+=(const var_value& b); + template >* = nullptr> + inline var_value>& operator+=( + const var_value& b); /** * The compound add/assignment operator for scalars (C++). @@ -250,10 +249,9 @@ class var_value> { * @return The result of subtracting the specified variable from * this variable. */ - template >* = nullptr> - inline var_value>& operator-=(const var_value& b); + template >* = nullptr> + inline var_value>& operator-=( + const var_value& b); /** * The compound subtract/assignment operator for scalars (C++). @@ -282,10 +280,9 @@ class var_value> { * @return The result of multiplying this variable by the * specified variable. */ - template >* = nullptr> - inline var_value>& operator*=(const var_value& b); + template >* = nullptr> + inline var_value>& operator*=( + const var_value& b); /** * The compound multiply/assignment operator for scalars (C++). @@ -313,10 +310,9 @@ class var_value> { * @return The result of dividing this variable by the * specified variable. */ - template >* = nullptr> - inline var_value>& operator/=(const var_value& b); + template >* = nullptr> + inline var_value>& operator/=( + const var_value& b); /** * The compound divide/assignment operator for scalars (C++). diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 726e785ba12..2690ccbac3b 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -115,8 +115,8 @@ class vari_value::value>> * * @tparam S an Arithmetic type. * @param x Value of the constructed variable. - * @param stacked If false will put this this vari on the nochain stack so that - * it's `chain()` method is not called. + * @param stacked If false will put this this vari on the nochain stack so + * that it's `chain()` method is not called. */ template ::value>* = nullptr> From de2b7c5ffbef042df1863791acb5be7c56f9ac86 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 3 Jun 2020 14:48:12 -0400 Subject: [PATCH 027/167] add flto flag for mac --- make/compiler_flags | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/make/compiler_flags b/make/compiler_flags index e64fa6f474f..36729e0ff9e 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -64,6 +64,14 @@ CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) CXX_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f2) +ifeq (default,$(origin CXX)) + ifeq ($(OS),Darwin) ## Darwin is Mac OS X + # gcc on mac actually calls clang + CXXFLAGS_FLTO := -flto -fwhole-program-vtables -fstrict-vtable-pointers + endif +endif + + ################################################################################ # Set default compiler flags # From df7e0891d26f5108234b6f1488b8a4535f775eae Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 3 Jun 2020 15:00:03 -0400 Subject: [PATCH 028/167] use ? instead of : for flto flags --- make/compiler_flags | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index 36729e0ff9e..f802ff3b797 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -47,29 +47,23 @@ endif # - CXX_MINOR: minor version of CXX ifneq (,$(findstring clang,$(CXX))) CXX_TYPE ?= clang - CXXFLAGS_FLTO := -flto -fwhole-program-vtables -fstrict-vtable-pointers + CXXFLAGS_FLTO ?= -flto -fwhole-program-vtables -fstrict-vtable-pointers endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc endif ifneq (,$(findstring gcc,$(CXX))) CXX_TYPE ?= gcc - CXXFLAGS_FLTO := -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta + CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta endif ifneq (,$(findstring g++,$(CXX))) CXX_TYPE ?= gcc - CXXFLAGS_FLTO := -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta + CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta endif CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) CXX_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f2) -ifeq (default,$(origin CXX)) - ifeq ($(OS),Darwin) ## Darwin is Mac OS X - # gcc on mac actually calls clang - CXXFLAGS_FLTO := -flto -fwhole-program-vtables -fstrict-vtable-pointers - endif -endif ################################################################################ From b510337fd2245c008844db2c400aef1c1a9890ba Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 3 Jun 2020 15:44:34 -0400 Subject: [PATCH 029/167] update vari test since integral template types are no longer allowed --- test/unit/math/rev/meta/is_vari_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/math/rev/meta/is_vari_test.cpp b/test/unit/math/rev/meta/is_vari_test.cpp index 9b5ab9e90c5..818d6c3b4d7 100644 --- a/test/unit/math/rev/meta/is_vari_test.cpp +++ b/test/unit/math/rev/meta/is_vari_test.cpp @@ -7,8 +7,8 @@ TEST(MetaTraitsRevScal, is_vari) { using stan::math::vari_value; EXPECT_TRUE(is_vari::value); EXPECT_TRUE((is_vari>::value)); - EXPECT_TRUE((is_vari>::value)); + EXPECT_TRUE((is_vari>::value)); EXPECT_FALSE(is_vari::value); EXPECT_FALSE((is_vari::value)); - EXPECT_FALSE((is_vari>::value)); + EXPECT_FALSE((is_vari>::value)); } From 4424dc3cc001b8cb89acf766f6fa71c933b41625 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 3 Jun 2020 17:22:05 -0400 Subject: [PATCH 030/167] remove whole program vtable clang optimization --- make/compiler_flags | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/compiler_flags b/make/compiler_flags index f802ff3b797..c43cfd458b2 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -47,7 +47,7 @@ endif # - CXX_MINOR: minor version of CXX ifneq (,$(findstring clang,$(CXX))) CXX_TYPE ?= clang - CXXFLAGS_FLTO ?= -flto -fwhole-program-vtables -fstrict-vtable-pointers + CXXFLAGS_FLTO ?= -flto -fstrict-vtable-pointers endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc From 8ce09f4b1245c5bd0e0e0d6d7c72541d2fe5ac3e Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 3 Jun 2020 18:02:22 -0400 Subject: [PATCH 031/167] remove -fstrict-vtable-pointers from clang --- make/compiler_flags | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/compiler_flags b/make/compiler_flags index c43cfd458b2..dafdafb82aa 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -47,7 +47,7 @@ endif # - CXX_MINOR: minor version of CXX ifneq (,$(findstring clang,$(CXX))) CXX_TYPE ?= clang - CXXFLAGS_FLTO ?= -flto -fstrict-vtable-pointers + CXXFLAGS_FLTO ?= -flto endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc From b8de65f5978ac8769291cedcac72d9c07df84546 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 09:30:05 -0400 Subject: [PATCH 032/167] fix is_var test --- test/unit/math/rev/meta/is_var_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/math/rev/meta/is_var_test.cpp b/test/unit/math/rev/meta/is_var_test.cpp index 75ef61d9596..cb225ff7d1c 100644 --- a/test/unit/math/rev/meta/is_var_test.cpp +++ b/test/unit/math/rev/meta/is_var_test.cpp @@ -7,8 +7,8 @@ TEST(MetaTraitsRevScal, is_var) { using stan::math::var_value; EXPECT_TRUE(is_var::value); EXPECT_TRUE((is_var>::value)); - EXPECT_TRUE((is_var>::value)); + EXPECT_TRUE((is_var>::value)); EXPECT_FALSE(is_var::value); EXPECT_FALSE((is_var::value)); - EXPECT_FALSE((is_var>::value)); + EXPECT_FALSE((is_var>::value)); } From 8cf3bcc3d872e98d6018226d86d46ef205b1680b Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 10:16:09 -0400 Subject: [PATCH 033/167] remove unneeded templating in var_value operators --- stan/math/rev/core/operator_divide_equal.hpp | 6 ++---- stan/math/rev/core/operator_minus_equal.hpp | 6 ++---- .../math/rev/core/operator_multiply_equal.hpp | 6 ++---- stan/math/rev/core/operator_plus_equal.hpp | 6 ++---- stan/math/rev/core/var.hpp | 20 ++++++++----------- 5 files changed, 16 insertions(+), 28 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 3b158118805..e4e2507cc31 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -9,16 +9,14 @@ namespace stan { namespace math { template template >*> -inline var_value>& -var_value>::operator/=(const var_value& b) { +inline var_value& var_value>::operator/=(const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& -var_value>::operator/=(Arith b) { +inline var_value& var_value>::operator/=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 2792d11fbdb..c5a888504d7 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -10,16 +10,14 @@ namespace math { template template >*> -inline var_value>& -var_value>::operator-=(const var_value& b) { +inline var_value& var_value>::operator-=(const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& -var_value>::operator-=(Arith b) { +inline var_value& var_value>::operator-=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 876ec9c8372..ec2936539e3 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -10,16 +10,14 @@ namespace math { template template >*> -inline var_value>& -var_value>::operator*=(const var_value& b) { +inline var_value& var_value>::operator*=(const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& -var_value>::operator*=(Arith b) { +inline var_value& var_value>::operator*=(Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index 12c4ddf2d72..e74e440dfd7 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -10,16 +10,14 @@ namespace math { template template >*> -inline var_value>& -var_value>::operator+=(const var_value& b) { +inline var_value& var_value>::operator+=(const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value>& -var_value>::operator+=(Arith b) { +inline var_value& var_value>::operator+=(Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index b74b7d35fc1..0889712d782 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -220,8 +220,7 @@ class var_value> { * @return The result of adding the specified variable to this variable. */ template >* = nullptr> - inline var_value>& operator+=( - const var_value& b); + inline var_value& operator+=(const var_value& b); /** * The compound add/assignment operator for scalars (C++). @@ -235,7 +234,7 @@ class var_value> { * @return The result of adding the specified variable to this variable. */ template * = nullptr> - inline var_value>& operator+=(Arith b); + inline var_value& operator+=(Arith b); /** * The compound subtract/assignment operator for variables (C++). @@ -250,8 +249,7 @@ class var_value> { * this variable. */ template >* = nullptr> - inline var_value>& operator-=( - const var_value& b); + inline var_value& operator-=(const var_value& b); /** * The compound subtract/assignment operator for scalars (C++). @@ -266,7 +264,7 @@ class var_value> { * variable. */ template * = nullptr> - inline var_value>& operator-=(Arith b); + inline var_value& operator-=(Arith b); /** * The compound multiply/assignment operator for variables (C++). @@ -281,8 +279,7 @@ class var_value> { * specified variable. */ template >* = nullptr> - inline var_value>& operator*=( - const var_value& b); + inline var_value& operator*=(const var_value& b); /** * The compound multiply/assignment operator for scalars (C++). @@ -297,7 +294,7 @@ class var_value> { * variable. */ template * = nullptr> - inline var_value>& operator*=(Arith b); + inline var_value& operator*=(Arith b); /** * The compound divide/assignment operator for variables (C++). If this @@ -311,8 +308,7 @@ class var_value> { * specified variable. */ template >* = nullptr> - inline var_value>& operator/=( - const var_value& b); + inline var_value& operator/=(const var_value& b); /** * The compound divide/assignment operator for scalars (C++). @@ -327,7 +323,7 @@ class var_value> { * variable. */ template * = nullptr> - inline var_value>& operator/=(Arith b); + inline var_value& operator/=(Arith b); /** * Write the value of this autodiff variable and its adjoint to From a07140c371481b548a932abf78618dd40031b63d Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 4 Jun 2020 10:35:12 -0400 Subject: [PATCH 034/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/operator_divide_equal.hpp | 6 ++++-- stan/math/rev/core/operator_minus_equal.hpp | 6 ++++-- stan/math/rev/core/operator_multiply_equal.hpp | 6 ++++-- stan/math/rev/core/operator_plus_equal.hpp | 6 ++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index e4e2507cc31..a3ed35bbd3b 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -9,14 +9,16 @@ namespace stan { namespace math { template template >*> -inline var_value& var_value>::operator/=(const var_value& b) { +inline var_value& var_value>::operator/=( + const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value>::operator/=(Arith b) { +inline var_value& var_value>::operator/=( + Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index c5a888504d7..9c89c2578f4 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -10,14 +10,16 @@ namespace math { template template >*> -inline var_value& var_value>::operator-=(const var_value& b) { +inline var_value& var_value>::operator-=( + const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value>::operator-=(Arith b) { +inline var_value& var_value>::operator-=( + Arith b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index ec2936539e3..fa74c2f543d 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -10,14 +10,16 @@ namespace math { template template >*> -inline var_value& var_value>::operator*=(const var_value& b) { +inline var_value& var_value>::operator*=( + const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value>::operator*=(Arith b) { +inline var_value& var_value>::operator*=( + Arith b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index e74e440dfd7..c22bccd71d3 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -10,14 +10,16 @@ namespace math { template template >*> -inline var_value& var_value>::operator+=(const var_value& b) { +inline var_value& var_value>::operator+=( + const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template template *> -inline var_value& var_value>::operator+=(Arith b) { +inline var_value& var_value>::operator+=( + Arith b) { if (b == 0.0) { return *this; } From 7ed5d9973c609a18a44ea4878de649062d66dcc8 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 13:18:42 -0400 Subject: [PATCH 035/167] remove flto flag --- make/compiler_flags | 5 +- stan/math/rev/core/autodiffstackstorage.hpp | 4 +- stan/math/rev/core/chainablestack.hpp | 8 ++- stan/math/rev/core/grad.hpp | 4 +- stan/math/rev/core/print_stack.hpp | 6 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 4 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 4 +- stan/math/rev/core/vari.hpp | 60 +++++++++++++++---- 8 files changed, 63 insertions(+), 32 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index dafdafb82aa..5d20960a48a 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -47,18 +47,15 @@ endif # - CXX_MINOR: minor version of CXX ifneq (,$(findstring clang,$(CXX))) CXX_TYPE ?= clang - CXXFLAGS_FLTO ?= -flto endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc endif ifneq (,$(findstring gcc,$(CXX))) CXX_TYPE ?= gcc - CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta endif ifneq (,$(findstring g++,$(CXX))) CXX_TYPE ?= gcc - CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta endif CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) @@ -259,7 +256,7 @@ ifdef STAN_MPI CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor endif -CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_FLTO) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) +CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index fecdab186b5..9b2dc106fdf 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -101,8 +101,8 @@ struct AutodiffStackSingleton { struct AutodiffStackStorage { AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; - std::vector var_stack_; - std::vector var_nochain_stack_; + std::vector var_stack_; + std::vector var_nochain_stack_; std::vector var_alloc_stack_; stack_alloc memalloc_; diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 57d49da3dbc..c6e2c424c41 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -2,14 +2,18 @@ #define STAN_MATH_REV_CORE_CHAINABLESTACK_HPP #include - +#include namespace stan { namespace math { +template +class vari_value; class vari_base; class chainable_alloc; -using ChainableStack = AutodiffStackSingleton; +using vari_variant = boost::variant*, vari_value*, vari_value*>; + +using ChainableStack = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index cd22f8c9a04..00d9ef669f0 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -30,11 +30,11 @@ namespace math { template static void grad(Vari* vi) { vi->init_dependent(); - std::vector& var_stack = ChainableStack::instance_->var_stack_; + std::vector& var_stack = ChainableStack::instance_->var_stack_; size_t end = var_stack.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - var_stack[i]->chain(); + boost::apply_visitor(vari_chainer(), var_stack[i]); } } diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 70f12b759b8..4f394e13663 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -22,11 +22,7 @@ inline void print_stack(std::ostream& o) { << std::endl; // TODO(carpenter): this shouldn't need to be cast any more for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { - o << i << " " << ChainableStack::instance_->var_stack_[i] << " " - << (static_cast(ChainableStack::instance_->var_stack_[i]))->val_ - << " : " - << (static_cast(ChainableStack::instance_->var_stack_[i]))->adj_ - << std::endl; + boost::apply_visitor(vari_printer(o, i), ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 841ef6d5c60..55ea7f7ed45 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,10 +13,10 @@ namespace math { */ static void set_zero_all_adjoints() { for (auto &x : ChainableStack::instance_->var_stack_) { - x->set_zero_adjoint(); + boost::apply_visitor(vari_zero_adj(), x); } for (auto &x : ChainableStack::instance_->var_nochain_stack_) { - x->set_zero_adjoint(); + boost::apply_visitor(vari_zero_adj(), x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 34dc367556c..0cf1591721d 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,14 +27,14 @@ static void set_zero_all_adjoints_nested() { // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < ChainableStack::instance_->var_stack_.size(); ++i) { - ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); + boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_stack_[i]); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { - ChainableStack::instance_->var_nochain_stack_[i]->set_zero_adjoint(); + boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 2690ccbac3b..0b3d86ec168 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -25,13 +25,6 @@ class vari_base { */ virtual void chain() {} - /** - * Set the adjoint value of this variable to 0. This is used to - * reset adjoints before propagating derivatives again (for - * example in a Jacobian calculation). - */ - virtual void set_zero_adjoint() = 0; - /** * Throw an illegal argument exception. * @@ -58,7 +51,7 @@ class vari_base { * classes will store operand variables and propagate derivative * information via an implementation of chain(). */ -template +template class vari_value; template @@ -98,7 +91,7 @@ class vari_value::value>> template ::value>* = nullptr> vari_value(S x) : val_(x), adj_(0.0) { // NOLINT - ChainableStack::instance_->var_stack_.push_back(this); + ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -122,9 +115,9 @@ class vari_value::value>> std::enable_if_t::value>* = nullptr> vari_value(S x, bool stacked) : val_(x), adj_(0.0) { if (stacked) { - ChainableStack::instance_->var_stack_.push_back(this); + ChainableStack::instance_->var_stack_.emplace_back(this); } else { - ChainableStack::instance_->var_nochain_stack_.push_back(this); + ChainableStack::instance_->var_nochain_stack_.emplace_back(this); } } @@ -136,7 +129,7 @@ class vari_value::value>> template ::value>* = nullptr> vari_value(const vari_value& x) : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.push_back(this); + ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -152,7 +145,7 @@ class vari_value::value>> * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() final { adj_ = 0.0; } + void set_zero_adjoint() { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and @@ -199,6 +192,47 @@ class vari_value::value>> // For backwards compatability the default is double using vari = vari_value; +class vari_zero_adj : public boost::static_visitor<> { + public: + void operator()(vari_value*& x) const { + x->adj_ = 0.0; + } + void operator()(vari_value*& x) const { + x->adj_ = 0.0; + } + void operator()(vari_value*& x) const { + x->adj_ = 0.0; + } +}; + +class vari_chainer : public boost::static_visitor<> { + public: + void operator()(vari_value*& x) const { + x->chain(); + } + void operator()(vari_value*& x) const { + x->chain(); + } + void operator()(vari_value*& x) const { + x->chain(); + } +}; + +class vari_printer : public boost::static_visitor<> { +public: + int i_{0}; + std::ostream& o_; + vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} + template + void operator()(T*& x) const { + // TODO(carpenter): this shouldn't need to be cast any more + o_ << i_ << " " << x << " " + << x->val_ + << " : " + << x->adj_ + << std::endl; + } +}; } // namespace math } // namespace stan #endif From 52de946350aa3cd3923bff949dfa61f09421c0b3 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 13:57:27 -0400 Subject: [PATCH 036/167] use lambda instead of visitor pattern --- stan/math/rev/core/grad.hpp | 5 ++--- stan/math/rev/core/set_zero_all_adjoints.hpp | 4 ++-- .../rev/core/set_zero_all_adjoints_nested.hpp | 4 ++-- stan/math/rev/core/vari.hpp | 20 +++++++++---------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 00d9ef669f0..b45768e42dd 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -30,11 +30,10 @@ namespace math { template static void grad(Vari* vi) { vi->init_dependent(); - std::vector& var_stack = ChainableStack::instance_->var_stack_; - size_t end = var_stack.size(); + size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::apply_visitor(vari_chainer(), var_stack[i]); + boost::apply_visitor([](auto*& x) { x->chain();}, ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 55ea7f7ed45..08e000b51f5 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,10 +13,10 @@ namespace math { */ static void set_zero_all_adjoints() { for (auto &x : ChainableStack::instance_->var_stack_) { - boost::apply_visitor(vari_zero_adj(), x); + boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, x); } for (auto &x : ChainableStack::instance_->var_nochain_stack_) { - boost::apply_visitor(vari_zero_adj(), x); + boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 0cf1591721d..169fdfd0653 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,14 +27,14 @@ static void set_zero_all_adjoints_nested() { // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_stack_[i]); + boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, ChainableStack::instance_->var_stack_[i]); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { - boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_nochain_stack_[i]); + boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 0b3d86ec168..15323397781 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -91,7 +91,7 @@ class vari_value::value>> template ::value>* = nullptr> vari_value(S x) : val_(x), adj_(0.0) { // NOLINT - ChainableStack::instance_->var_stack_.emplace_back(this); + ChainableStack::instance_->var_stack_.push_back(this); } /** @@ -115,9 +115,9 @@ class vari_value::value>> std::enable_if_t::value>* = nullptr> vari_value(S x, bool stacked) : val_(x), adj_(0.0) { if (stacked) { - ChainableStack::instance_->var_stack_.emplace_back(this); + ChainableStack::instance_->var_stack_.push_back(this); } else { - ChainableStack::instance_->var_nochain_stack_.emplace_back(this); + ChainableStack::instance_->var_nochain_stack_.push_back(this); } } @@ -129,7 +129,7 @@ class vari_value::value>> template ::value>* = nullptr> vari_value(const vari_value& x) : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.emplace_back(this); + ChainableStack::instance_->var_stack_.push_back(this); } /** @@ -194,26 +194,26 @@ using vari = vari_value; class vari_zero_adj : public boost::static_visitor<> { public: - void operator()(vari_value*& x) const { + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } - void operator()(vari_value*& x) const { + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } - void operator()(vari_value*& x) const { + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } }; class vari_chainer : public boost::static_visitor<> { public: - void operator()(vari_value*& x) const { + inline void operator()(vari_value*& x) const { x->chain(); } - void operator()(vari_value*& x) const { + inline void operator()(vari_value*& x) const { x->chain(); } - void operator()(vari_value*& x) const { + inline void operator()(vari_value*& x) const { x->chain(); } }; From 6585a89cb355fc01900fd70fe4a923c7b0c92c07 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 14:08:27 -0400 Subject: [PATCH 037/167] fix vari_test --- test/unit/math/rev/core/vari_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index d9d0fb6648f..fc5a6e34ab5 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -9,8 +9,8 @@ TEST(AgradRev, insertion_operator) { EXPECT_EQ("5:0", ss.str()); } -TEST(AgradRev, int_test) { - stan::math::vari_value v(5); +TEST(AgradRev, long_double_test) { + stan::math::vari_value v(5); std::stringstream ss; ss << &v; EXPECT_EQ("5:0", ss.str()); From f42756c65b6c49e6010a98c9a77de273150db5e5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 17:17:55 -0400 Subject: [PATCH 038/167] only use fdevirtualize-at-ltrans for linux gcc --- make/compiler_flags | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index dafdafb82aa..f5ef6127406 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -54,11 +54,17 @@ ifneq (,$(findstring mingw32-g,$(CXX))) endif ifneq (,$(findstring gcc,$(CXX))) CXX_TYPE ?= gcc - CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta + CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fipa-pta + ifneq ($(OS),Windows_NT) + CXXFLAGS_FLTO += -fdevirtualize-at-ltrans + endif endif ifneq (,$(findstring g++,$(CXX))) CXX_TYPE ?= gcc - CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans -fipa-pta + CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fipa-pta + ifneq ($(OS),Windows_NT) + CXXFLAGS_FLTO += -fdevirtualize-at-ltrans + endif endif CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) From 12f9cafde02c9c11c911908b68900abebcba2f40 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 4 Jun 2020 17:26:41 -0400 Subject: [PATCH 039/167] use filter instead of findstring when matching on g++ in makefile --- make/compiler_flags | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index f5ef6127406..7fd2304369d 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -52,14 +52,14 @@ endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc endif -ifneq (,$(findstring gcc,$(CXX))) +ifneq ($(filter gcc,$(CXX)),) CXX_TYPE ?= gcc CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fipa-pta ifneq ($(OS),Windows_NT) CXXFLAGS_FLTO += -fdevirtualize-at-ltrans endif endif -ifneq (,$(findstring g++,$(CXX))) +ifneq ($(filter g++,$(CXX)),) CXX_TYPE ?= gcc CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fipa-pta ifneq ($(OS),Windows_NT) From 2c41d98ce45dbca951a7751c697165410d5e8653 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 7 Jun 2020 17:20:12 -0400 Subject: [PATCH 040/167] do not use flto for windows --- make/compiler_flags | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index 7fd2304369d..65d29413135 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -52,18 +52,16 @@ endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc endif -ifneq ($(filter gcc,$(CXX)),) +ifneq ($(filter gcc,$(CXX)),) CXX_TYPE ?= gcc - CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fipa-pta ifneq ($(OS),Windows_NT) - CXXFLAGS_FLTO += -fdevirtualize-at-ltrans + CXXFLAGS_FLTO ?= -fdevirtualize-at-ltrans -flto -fuse-linker-plugin -fipa-pta endif endif ifneq ($(filter g++,$(CXX)),) CXX_TYPE ?= gcc - CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fipa-pta ifneq ($(OS),Windows_NT) - CXXFLAGS_FLTO += -fdevirtualize-at-ltrans + CXXFLAGS_FLTO ?= -fdevirtualize-at-ltrans -flto -fuse-linker-plugin -fipa-pta endif endif CXX_TYPE ?= other @@ -292,6 +290,7 @@ print-compiler-flags: @echo ' - CXXFLAGS_WARNINGS ' $(CXXFLAGS_WARNINGS) @echo ' - CXXFLAGS_BOOST ' $(CXXFLAGS_BOOST) @echo ' - CXXFLAGS_EIGEN ' $(CXXFLAGS_EIGEN) + @echo ' - CXXFLAGS_FLTO ' $(CXXFLAGS_FLTO) @echo ' - CXXFLAGS_OS ' $(CXXFLAGS_OS) @echo ' - CXXFLAGS_GTEST ' $(CXXFLAGS_GTEST) @echo ' - CXXFLAGS_THREADS ' $(CXXFLAGS_THREADS) From 77f10598b9b05403fef0495cd57e6c2752a1f418 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 7 Jun 2020 17:44:06 -0400 Subject: [PATCH 041/167] fix compile_flags missing tab --- make/compiler_flags | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/compiler_flags b/make/compiler_flags index 65d29413135..77d4fbc40c9 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -290,7 +290,7 @@ print-compiler-flags: @echo ' - CXXFLAGS_WARNINGS ' $(CXXFLAGS_WARNINGS) @echo ' - CXXFLAGS_BOOST ' $(CXXFLAGS_BOOST) @echo ' - CXXFLAGS_EIGEN ' $(CXXFLAGS_EIGEN) - @echo ' - CXXFLAGS_FLTO ' $(CXXFLAGS_FLTO) + @echo ' - CXXFLAGS_FLTO ' $(CXXFLAGS_FLTO) @echo ' - CXXFLAGS_OS ' $(CXXFLAGS_OS) @echo ' - CXXFLAGS_GTEST ' $(CXXFLAGS_GTEST) @echo ' - CXXFLAGS_THREADS ' $(CXXFLAGS_THREADS) From e1f6af33891a83946b986e53348dd30c5baf3af7 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 8 Jun 2020 14:00:35 -0400 Subject: [PATCH 042/167] add flto flags to library flags --- make/compiler_flags | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/compiler_flags b/make/compiler_flags index 77d4fbc40c9..ae88fce48de 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -265,7 +265,7 @@ endif CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_FLTO) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) -LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) +LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(CXXFLAGS_FLTO) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) .PHONY: print-compiler-flags From fdae56f2cb4aad72bdb64a93434a19514921422a Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Jun 2020 02:11:46 -0400 Subject: [PATCH 043/167] remove flto changes --- make/compiler_flags | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index ae88fce48de..de486bffc43 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -28,7 +28,7 @@ ifeq ($(OS),Linux) LIBRARY_SUFFIX ?= .so endif -## Set default compiler and flto compiler flags +## Set default compiler ifeq (default,$(origin CXX)) ifeq ($(OS),Darwin) ## Darwin is Mac OS X CXX := clang++ @@ -47,29 +47,20 @@ endif # - CXX_MINOR: minor version of CXX ifneq (,$(findstring clang,$(CXX))) CXX_TYPE ?= clang - CXXFLAGS_FLTO ?= -flto endif ifneq (,$(findstring mingw32-g,$(CXX))) CXX_TYPE ?= mingw32-gcc endif -ifneq ($(filter gcc,$(CXX)),) +ifneq (,$(findstring gcc,$(CXX))) CXX_TYPE ?= gcc - ifneq ($(OS),Windows_NT) - CXXFLAGS_FLTO ?= -fdevirtualize-at-ltrans -flto -fuse-linker-plugin -fipa-pta - endif endif -ifneq ($(filter g++,$(CXX)),) +ifneq (,$(findstring g++,$(CXX))) CXX_TYPE ?= gcc - ifneq ($(OS),Windows_NT) - CXXFLAGS_FLTO ?= -fdevirtualize-at-ltrans -flto -fuse-linker-plugin -fipa-pta - endif endif CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) CXX_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f2) - - ################################################################################ # Set default compiler flags # @@ -263,9 +254,11 @@ ifdef STAN_MPI CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor endif -CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_FLTO) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) + + +CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) -LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(CXXFLAGS_FLTO) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) +LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) .PHONY: print-compiler-flags @@ -290,7 +283,6 @@ print-compiler-flags: @echo ' - CXXFLAGS_WARNINGS ' $(CXXFLAGS_WARNINGS) @echo ' - CXXFLAGS_BOOST ' $(CXXFLAGS_BOOST) @echo ' - CXXFLAGS_EIGEN ' $(CXXFLAGS_EIGEN) - @echo ' - CXXFLAGS_FLTO ' $(CXXFLAGS_FLTO) @echo ' - CXXFLAGS_OS ' $(CXXFLAGS_OS) @echo ' - CXXFLAGS_GTEST ' $(CXXFLAGS_GTEST) @echo ' - CXXFLAGS_THREADS ' $(CXXFLAGS_THREADS) From cc702da7e7f97fdbe73398d1c9dcb16ca3d73f77 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Jun 2020 16:32:21 -0400 Subject: [PATCH 044/167] add back flto flag for mac and linux --- make/compiler_flags | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index de486bffc43..9c191bfaa22 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -141,11 +141,15 @@ ifeq ($(OS),Darwin) ifeq (clang,$(CXX_TYPE)) CXXFLAGS_OS ?= -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare endif + CXXFLAGS_FLTO ?= -flto + LDFLAGS_FLTO ?= -flto endif ifeq ($(OS),Linux) CPPFLAGS_GTEST ?= -DGTEST_HAS_PTHREAD=0 CXXFLAGS_WARNINGS ?= -Wno-sign-compare + CXXFLAGS_FLTO ?= -flto + LDFLAGS_FLTO ?= -flto ifeq (gcc,$(CXX_TYPE)) CXXFLAGS_OS ?= -pthread endif @@ -256,9 +260,9 @@ endif -CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) +CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_FLTO) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) -LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) +LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(CXXFLAGS_FLTO) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) .PHONY: print-compiler-flags From c8b5eea0f52ab7b0e8d7f2e8cbf59a9c8f899c35 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Jun 2020 16:35:29 -0400 Subject: [PATCH 045/167] add flto to cppflags --- make/compiler_flags | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/compiler_flags b/make/compiler_flags index 9c191bfaa22..18439d3d09e 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -261,7 +261,7 @@ endif CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_FLTO) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) -CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) +CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CXXFLAGS_FLTO) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(CXXFLAGS_FLTO) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) From 8726f4c6565811345d1cf796548a8eb756d9979b Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Jun 2020 22:06:38 -0400 Subject: [PATCH 046/167] set ar by clang or gcc cxx_type --- make/compiler_flags | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/make/compiler_flags b/make/compiler_flags index 18439d3d09e..5c8d5e18210 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -61,6 +61,16 @@ CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) CXX_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f2) +# Set the ar to use +ifneq ($(OS),Windows_NT) + ifneq (,$(findstring clang,$(CXX_TYPE))) + AR := llvm-ar + endif + ifneq (,$(findstring gcc,$(CXX_TYPE))) + AR := gcc-ar + endif +endif + ################################################################################ # Set default compiler flags # From cba8d24b79585f60172308f5837b3191e957a647 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 10 Jun 2020 14:22:03 -0400 Subject: [PATCH 047/167] remove vari_base --- stan/math/rev/core/vari.hpp | 30 +++++------------------------- stan/math/rev/meta/is_vari.hpp | 12 +++++++++--- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 15323397781..790f8aeecff 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -14,27 +14,6 @@ namespace math { template class var_value; -/** - * Abstract class that all `vari_value` and it's derived classes inherit. - */ -class vari_base { - public: - /** - * Apply the chain rule to this variable based on the variables - * on which it depends. - */ - virtual void chain() {} - - /** - * Throw an illegal argument exception. - * - * Warning: Destructors should never called for var objects. - * - * @throw Logic exception always. - */ - virtual ~vari_base() {} -}; - /** * The variable implementation base class. * @@ -55,8 +34,7 @@ template class vari_value; template -class vari_value::value>> - : public vari_base { +class vari_value::value>> { private: template friend class var_value; @@ -138,14 +116,16 @@ class vari_value::value>> * propagating derivatives, setting the derivative of the * result with respect to itself to be 1. */ - void init_dependent() { adj_ = 1.0; } + inline void init_dependent() { adj_ = 1.0; } /** * Set the adjoint value of this variable to 0. This is used to * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() { adj_ = 0.0; } + inline void set_zero_adjoint() { adj_ = 0.0; } + + virtual void chain() {} /** * Insertion operator for vari. Prints the current value and diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index c91364bbe3c..9ba8b5e6e08 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -6,14 +6,20 @@ #include namespace stan { + +namespace internal { + template + struct is_vari_impl : std::false_type {}; + + template + struct is_vari_impl> : std::true_type {}; +} /** \ingroup type_trait * Specialization for checking if value of T minus cv qualifier and pointer is a * vari_value. */ template -struct is_vari< - T, std::enable_if_t>>::value>> +struct is_vari>::value>> : std::true_type {}; } // namespace stan From 4c671e127bd208d73f469a6a5596bbcf55fd950c Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 10 Jun 2020 14:49:55 -0400 Subject: [PATCH 048/167] use functors instead of lambdas --- make/compiler_flags | 2 +- stan/math/rev/core/grad.hpp | 2 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 4 ++-- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index 5d20960a48a..ac4df8907b2 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -28,7 +28,7 @@ ifeq ($(OS),Linux) LIBRARY_SUFFIX ?= .so endif -## Set default compiler and flto compiler flags +## Set default compiler ifeq (default,$(origin CXX)) ifeq ($(OS),Darwin) ## Darwin is Mac OS X CXX := clang++ diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index b45768e42dd..890f01bb983 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -33,7 +33,7 @@ static void grad(Vari* vi) { size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::apply_visitor([](auto*& x) { x->chain();}, ChainableStack::instance_->var_stack_[i]); + boost::apply_visitor(vari_chainer(), ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 08e000b51f5..55ea7f7ed45 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,10 +13,10 @@ namespace math { */ static void set_zero_all_adjoints() { for (auto &x : ChainableStack::instance_->var_stack_) { - boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, x); + boost::apply_visitor(vari_zero_adj(), x); } for (auto &x : ChainableStack::instance_->var_nochain_stack_) { - boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, x); + boost::apply_visitor(vari_zero_adj(), x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 169fdfd0653..0cf1591721d 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,14 +27,14 @@ static void set_zero_all_adjoints_nested() { // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, ChainableStack::instance_->var_stack_[i]); + boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_stack_[i]); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { - boost::apply_visitor([](auto*& x) { x->adj_ = 0.0;}, ChainableStack::instance_->var_nochain_stack_[i]); + boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_nochain_stack_[i]); } } From e4581ca2ac5ae001b85428dea87f109c0298a542 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 10 Jun 2020 15:08:33 -0400 Subject: [PATCH 049/167] remove flto from compiler_flags --- make/compiler_flags | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index 9c79acdd5d5..20a490c7fda 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -28,11 +28,7 @@ ifeq ($(OS),Linux) LIBRARY_SUFFIX ?= .so endif -<<<<<<< HEAD ## Set default compiler -======= -## Set default compiler ->>>>>>> feature/vari-template-variant ifeq (default,$(origin CXX)) ifeq ($(OS),Darwin) ## Darwin is Mac OS X CXX := clang++ @@ -65,15 +61,7 @@ CXX_TYPE ?= other CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1) CXX_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f2) -# Set the ar to use -ifneq ($(OS),Windows_NT) - ifneq (,$(findstring clang,$(CXX_TYPE))) - AR := llvm-ar - endif - ifneq (,$(findstring gcc,$(CXX_TYPE))) - AR := gcc-ar - endif -endif + ################################################################################ # Set default compiler flags From 52ae3b420ec7c88c37655530a588de777be5f9fc Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 10 Jun 2020 19:15:02 +0000 Subject: [PATCH 050/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/chainablestack.hpp | 3 +- stan/math/rev/core/grad.hpp | 3 +- stan/math/rev/core/print_stack.hpp | 3 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 6 ++- stan/math/rev/core/vari.hpp | 50 +++++++------------ stan/math/rev/meta/is_vari.hpp | 13 ++--- 6 files changed, 34 insertions(+), 44 deletions(-) diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index c6e2c424c41..e77bfd51e1d 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -11,7 +11,8 @@ class vari_value; class vari_base; class chainable_alloc; -using vari_variant = boost::variant*, vari_value*, vari_value*>; +using vari_variant = boost::variant*, vari_value*, + vari_value*>; using ChainableStack = AutodiffStackSingleton; diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 890f01bb983..2201794c91c 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -33,7 +33,8 @@ static void grad(Vari* vi) { size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::apply_visitor(vari_chainer(), ChainableStack::instance_->var_stack_[i]); + boost::apply_visitor(vari_chainer(), + ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 4f394e13663..d7dc55d185e 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -22,7 +22,8 @@ inline void print_stack(std::ostream& o) { << std::endl; // TODO(carpenter): this shouldn't need to be cast any more for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::apply_visitor(vari_printer(o, i), ChainableStack::instance_->var_stack_[i]); + boost::apply_visitor(vari_printer(o, i), + ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 0cf1591721d..f94fa618722 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,14 +27,16 @@ static void set_zero_all_adjoints_nested() { // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_stack_[i]); + boost::apply_visitor(vari_zero_adj(), + ChainableStack::instance_->var_stack_[i]); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { - boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_nochain_stack_[i]); + boost::apply_visitor(vari_zero_adj(), + ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 790f8aeecff..b90bb794c16 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -173,45 +173,29 @@ class vari_value::value>> { using vari = vari_value; class vari_zero_adj : public boost::static_visitor<> { - public: - inline void operator()(vari_value*& x) const { - x->adj_ = 0.0; - } - inline void operator()(vari_value*& x) const { - x->adj_ = 0.0; - } - inline void operator()(vari_value*& x) const { - x->adj_ = 0.0; - } + public: + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } }; class vari_chainer : public boost::static_visitor<> { - public: - inline void operator()(vari_value*& x) const { - x->chain(); - } - inline void operator()(vari_value*& x) const { - x->chain(); - } - inline void operator()(vari_value*& x) const { - x->chain(); - } + public: + inline void operator()(vari_value*& x) const { x->chain(); } + inline void operator()(vari_value*& x) const { x->chain(); } + inline void operator()(vari_value*& x) const { x->chain(); } }; class vari_printer : public boost::static_visitor<> { -public: - int i_{0}; - std::ostream& o_; - vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} - template - void operator()(T*& x) const { - // TODO(carpenter): this shouldn't need to be cast any more - o_ << i_ << " " << x << " " - << x->val_ - << " : " - << x->adj_ - << std::endl; - } + public: + int i_{0}; + std::ostream& o_; + vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} + template + void operator()(T*& x) const { + // TODO(carpenter): this shouldn't need to be cast any more + o_ << i_ << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; + } }; } // namespace math } // namespace stan diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index 9ba8b5e6e08..39fb79cb767 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -8,18 +8,19 @@ namespace stan { namespace internal { - template - struct is_vari_impl : std::false_type {}; +template +struct is_vari_impl : std::false_type {}; - template - struct is_vari_impl> : std::true_type {}; -} +template +struct is_vari_impl> : std::true_type {}; +} // namespace internal /** \ingroup type_trait * Specialization for checking if value of T minus cv qualifier and pointer is a * vari_value. */ template -struct is_vari>::value>> +struct is_vari>::value>> : std::true_type {}; } // namespace stan From 9358df7922731a9c558fcbe3400f83550b5bb2b3 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 10 Jun 2020 20:56:27 -0400 Subject: [PATCH 051/167] remove promote_args_t in var member function templates --- stan/math/rev/core/operator_divide_equal.hpp | 2 +- stan/math/rev/core/operator_minus_equal.hpp | 2 +- .../math/rev/core/operator_multiply_equal.hpp | 2 +- stan/math/rev/core/operator_plus_equal.hpp | 2 +- stan/math/rev/core/var.hpp | 26 ++++++++----------- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index a3ed35bbd3b..3dae0cc79cb 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -8,7 +8,7 @@ namespace stan { namespace math { template -template >*> +template *> inline var_value& var_value>::operator/=( const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 9c89c2578f4..e70a03bf69b 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -9,7 +9,7 @@ namespace stan { namespace math { template -template >*> +template *> inline var_value& var_value>::operator-=( const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index fa74c2f543d..c995b6fa905 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -9,7 +9,7 @@ namespace stan { namespace math { template -template >*> +template *> inline var_value& var_value>::operator*=( const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index c22bccd71d3..0da8b734b22 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -9,7 +9,7 @@ namespace stan { namespace math { template -template >*> +template *> inline var_value& var_value>::operator+=( const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 0889712d782..23353733b0a 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -39,13 +39,13 @@ class var_value> { // The internal value_type is always a floating point type using value_type = std::decay_t; using vari_type = vari_value; - + using vari_pointer = vari_type*; template using require_vari_convertible_t - = require_t*, vari_type*>>; + = require_t*, vari_pointer>>; template using require_not_vari_convertible_t = require_t< - bool_constant*, vari_type*>::value>>; + bool_constant*, vari_pointer>::value>>; /** * Pointer to the implementation of this variable. @@ -94,8 +94,7 @@ class var_value> { * same as `vari_value` so can be `reinterpret_cast` without a copy. * @param vi A vari_value pointer. */ - template , - require_vari_convertible_t* = nullptr> + template * = nullptr> var_value(vari_value* vi) // NOLINT : vi_(vi) {} @@ -106,8 +105,7 @@ class var_value> { * as the `value_type` of this `var_value`. * @param vi A `vari_value`. */ - template , - require_not_vari_convertible_t* = nullptr> + template * = nullptr> var_value(vari_value* vi) : vi_(new vari_type(*vi)) {} // NOLINT /** @@ -119,8 +117,7 @@ class var_value> { * param x a `var_value` whose underlying vari_type can be dynamically cast * to `this::vari_value``. */ - template , - require_not_vari_convertible_t* = nullptr> + template * = nullptr> var_value(const var_value& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT /** @@ -129,8 +126,7 @@ class var_value> { * `var_value a(4.0); var_value b(a)` since the `value_type` for * a `var_value` with an integral type is a double. */ - template , - require_vari_convertible_t* = nullptr> + template * = nullptr> var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT /** @@ -219,7 +215,7 @@ class var_value> { * @param b The variable to add to this variable. * @return The result of adding the specified variable to this variable. */ - template >* = nullptr> + template * = nullptr> inline var_value& operator+=(const var_value& b); /** @@ -248,7 +244,7 @@ class var_value> { * @return The result of subtracting the specified variable from * this variable. */ - template >* = nullptr> + template * = nullptr> inline var_value& operator-=(const var_value& b); /** @@ -278,7 +274,7 @@ class var_value> { * @return The result of multiplying this variable by the * specified variable. */ - template >* = nullptr> + template * = nullptr> inline var_value& operator*=(const var_value& b); /** @@ -307,7 +303,7 @@ class var_value> { * @return The result of dividing this variable by the * specified variable. */ - template >* = nullptr> + template * = nullptr> inline var_value& operator/=(const var_value& b); /** From eb18a98f4c99b262ddbb3f918e11cb75b2b5be31 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 11 Jun 2020 12:56:35 -0400 Subject: [PATCH 052/167] remove TODO comment and fix grammar --- stan/math/rev/core/vari.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index b90bb794c16..da31f91c90c 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -87,7 +87,7 @@ class vari_value::value>> { * @tparam S an Arithmetic type. * @param x Value of the constructed variable. * @param stacked If false will put this this vari on the nochain stack so - * that it's `chain()` method is not called. + * that its `chain()` method is not called. */ template ::value>* = nullptr> @@ -181,6 +181,7 @@ class vari_zero_adj : public boost::static_visitor<> { class vari_chainer : public boost::static_visitor<> { public: + template inline void operator()(vari_value*& x) const { x->chain(); } inline void operator()(vari_value*& x) const { x->chain(); } inline void operator()(vari_value*& x) const { x->chain(); } @@ -193,7 +194,6 @@ class vari_printer : public boost::static_visitor<> { vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} template void operator()(T*& x) const { - // TODO(carpenter): this shouldn't need to be cast any more o_ << i_ << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; } }; From 3a711018a12953cb5f6d4806950e2883102e78a0 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 11 Jun 2020 13:11:29 -0400 Subject: [PATCH 053/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index da31f91c90c..cd663001de0 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -182,7 +182,9 @@ class vari_zero_adj : public boost::static_visitor<> { class vari_chainer : public boost::static_visitor<> { public: template - inline void operator()(vari_value*& x) const { x->chain(); } + inline void operator()(vari_value*& x) const { + x->chain(); + } inline void operator()(vari_value*& x) const { x->chain(); } inline void operator()(vari_value*& x) const { x->chain(); } }; From 944134648aa3f1010cfb53580b833e63bdb79c3f Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 11 Jun 2020 13:20:26 -0400 Subject: [PATCH 054/167] make visitor classes more generic --- stan/math/rev/core/vari.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index da31f91c90c..134328fcaf5 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -174,17 +174,14 @@ using vari = vari_value; class vari_zero_adj : public boost::static_visitor<> { public: - inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } - inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } - inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } + template * = nullptr> + inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } }; class vari_chainer : public boost::static_visitor<> { public: template - inline void operator()(vari_value*& x) const { x->chain(); } - inline void operator()(vari_value*& x) const { x->chain(); } - inline void operator()(vari_value*& x) const { x->chain(); } + inline void operator()(vari_value*& x) const { x->chain(); } }; class vari_printer : public boost::static_visitor<> { From 30be7d221a06117c629b17faec769f1f5a33b377 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 11 Jun 2020 13:27:09 -0400 Subject: [PATCH 055/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 134328fcaf5..1198b6247cd 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -175,13 +175,17 @@ using vari = vari_value; class vari_zero_adj : public boost::static_visitor<> { public: template * = nullptr> - inline void operator()(vari_value*& x) const { x->adj_ = 0.0; } + inline void operator()(vari_value*& x) const { + x->adj_ = 0.0; + } }; class vari_chainer : public boost::static_visitor<> { public: template - inline void operator()(vari_value*& x) const { x->chain(); } + inline void operator()(vari_value*& x) const { + x->chain(); + } }; class vari_printer : public boost::static_visitor<> { From 78b73b99f921fb7405d13b2e005a6b7e904921f3 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 11 Jun 2020 13:46:59 -0400 Subject: [PATCH 056/167] add const correct stuff for vari value accessors in var --- stan/math/rev/core/var.hpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 23353733b0a..bcd3cb6f720 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -130,21 +130,31 @@ class var_value> { var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT /** - * Return the value of this variable. + * Return a constant reference to the value of this variable. * * @return The value of this variable. */ - inline auto val() const { return vi_->val_; } + inline const auto& val() const { return vi_->val_; } /** - * Return the derivative of the root expression with + * Return a const reference of the derivative of the root expression with * respect to this expression. This method only works * after one of the `grad()` methods has been * called. * * @return Adjoint for this variable. */ - inline auto adj() const { return vi_->adj_; } + inline const auto& adj() const { return vi_->adj_; } + + /** + * Return a reference to the derivative of the root expression with + * respect to this expression. This method only works + * after one of the `grad()` methods has been + * called. + * + * @return Adjoint for this variable. + */ + inline auto& adj() { return vi_->adj_; } /** * Compute the gradient of this (dependent) variable with respect to From 5c69ed02ef8dee14ebe2c5a6efc08e8ee6843e09 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 12 Jun 2020 12:39:29 -0400 Subject: [PATCH 057/167] remove constructors for mixed floating point types --- nohup.out | 8 +++++ stan/math/rev/core/var.hpp | 22 -------------- test/unit/math/rev/core/var_test.cpp | 44 +++++++++++----------------- 3 files changed, 25 insertions(+), 49 deletions(-) create mode 100644 nohup.out diff --git a/nohup.out b/nohup.out new file mode 100644 index 00000000000..c0b074fb927 --- /dev/null +++ b/nohup.out @@ -0,0 +1,8 @@ +../../3rdparty/chromium/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc:**CRASHING**:seccomp-bpf failure in syscall 0230 +The X11 connection broke (error 1). Did the X11 server die? +Gdk-Message: 14:16:34.225: rstudio: Fatal IO error 11 (Resource temporarily unavailable) on X server :1. + +../../3rdparty/chromium/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc:**CRASHING**:seccomp-bpf failure in syscall 0230 +The X11 connection broke (error 1). Did the X11 server die? +Gdk-Message: 14:16:54.082: rstudio: Fatal IO error 11 (Resource temporarily unavailable) on X server :1. + diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index bcd3cb6f720..e7de6b0a820 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -98,28 +98,6 @@ class var_value> { var_value(vari_value* vi) // NOLINT : vi_(vi) {} - /** - * Construct a variable from a pointer to a variable implementation when. - * the `value_type`s differ. This forces a copy of the vari - * @tparam S the type in the vari_value* that does not have the same type - * as the `value_type` of this `var_value`. - * @param vi A `vari_value`. - */ - template * = nullptr> - var_value(vari_value* vi) : vi_(new vari_type(*vi)) {} // NOLINT - - /** - * Construct from a var_value whose underlying value_type differs in type - * to this class's value_type. This is for cases such as - * `var_value a(5.0); var_value b(a);` - * @tparam S A type that is not the same as the underlying value type. - * same as the underlying value_type. - * param x a `var_value` whose underlying vari_type can be dynamically cast - * to `this::vari_value``. - */ - template * = nullptr> - var_value(const var_value& x) : vi_(new vari_type(*x.vi_)) {} // NOLINT - /** * Constructor from `var_value` whose value_type is the same as this class's * `value_type`. This is used in cases such as diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 74567e4aa73..2b792ac41cd 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -14,8 +14,8 @@ struct AgradRev : public testing::Test { } }; -template -void ctor_overloads_impl_impl() { +template +void ctor_overloads_impl() { using stan::math::var_value; using stan::math::vari_value; using stan::math::test::type_name; @@ -23,13 +23,11 @@ void ctor_overloads_impl_impl() { EXPECT_FLOAT_EQ(3.7, var_value(3.7).val()) << "Failed For T: " << type_name() << "\n"; // make sure copy ctor is used rather than casting vari* to unsigned int - EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) - << "Failed For T: " << type_name() << " and K: " << type_name() - << "\n"; + EXPECT_FLOAT_EQ(12.3, var_value(new vari_value(12.3)).val()) + << "Failed For T: " << type_name() << std::endl; // make sure rvalue var_value can be accepted - EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) - << "Failed For T: " << type_name() << " and K: " << type_name() - << "\n"; + EXPECT_FLOAT_EQ(12.3, var_value(var_value(12.3)).val()) + << "Failed For T: " << type_name() << std::endl; // S type is preserved EXPECT_FLOAT_EQ(static_cast(3.7), var_value(static_cast(3.7)).val()) << "Failed For T: " << type_name() << " and S: " << type_name() @@ -40,32 +38,24 @@ void ctor_overloads_impl_impl() { << "\n"; } -template -void ctor_overloads_impl() { - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); - ctor_overloads_impl_impl(); -} template void ctor_overloads() { - ctor_overloads_impl(); ctor_overloads_impl(); ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); + ctor_overloads_impl(); } TEST_F(AgradRev, ctorOverloads) { - using stan::math::var; - using stan::math::vari; ctor_overloads(); ctor_overloads(); ctor_overloads(); From 917800ab6d081b51d36b418d9a7e6c065ca4050b Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 12 Jun 2020 12:39:45 -0400 Subject: [PATCH 058/167] remove nohup --- nohup.out | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 nohup.out diff --git a/nohup.out b/nohup.out deleted file mode 100644 index c0b074fb927..00000000000 --- a/nohup.out +++ /dev/null @@ -1,8 +0,0 @@ -../../3rdparty/chromium/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc:**CRASHING**:seccomp-bpf failure in syscall 0230 -The X11 connection broke (error 1). Did the X11 server die? -Gdk-Message: 14:16:34.225: rstudio: Fatal IO error 11 (Resource temporarily unavailable) on X server :1. - -../../3rdparty/chromium/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc:**CRASHING**:seccomp-bpf failure in syscall 0230 -The X11 connection broke (error 1). Did the X11 server die? -Gdk-Message: 14:16:54.082: rstudio: Fatal IO error 11 (Resource temporarily unavailable) on X server :1. - From ac7076cf0b4cbe7080dc7f4ed78a2d5751ebb74f Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 12 Jun 2020 19:51:31 -0400 Subject: [PATCH 059/167] switch order of members in vari_printer for order warning on windows --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 1198b6247cd..0866099f9d9 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -190,8 +190,8 @@ class vari_chainer : public boost::static_visitor<> { class vari_printer : public boost::static_visitor<> { public: - int i_{0}; std::ostream& o_; + int i_{0}; vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} template void operator()(T*& x) const { From 1cd05a8730d08384279061c343d7de070dc8d244 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 12 Jun 2020 19:52:13 -0400 Subject: [PATCH 060/167] switch order of members in vari_printer for order warning on windows --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 0866099f9d9..6fc4b62767b 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -194,7 +194,7 @@ class vari_printer : public boost::static_visitor<> { int i_{0}; vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} template - void operator()(T*& x) const { + inline void operator()(T*& x) const { o_ << i_ << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; } }; From 276352461c244054b772f662376382d33e261cf4 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 13 Jun 2020 19:15:04 -0400 Subject: [PATCH 061/167] call set_zero_adjoint_nested directly --- stan/math/rev/core/nested_rev_autodiff.hpp | 2 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 2 +- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 10 +++++----- stan/math/rev/core/vari.hpp | 6 +++--- stan/math/rev/functor/coupled_ode_system.hpp | 2 +- test/unit/math/rev/core/var_test.cpp | 1 + 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/stan/math/rev/core/nested_rev_autodiff.hpp b/stan/math/rev/core/nested_rev_autodiff.hpp index a8f71d21c4a..2e44ca29850 100644 --- a/stan/math/rev/core/nested_rev_autodiff.hpp +++ b/stan/math/rev/core/nested_rev_autodiff.hpp @@ -39,7 +39,7 @@ class nested_rev_autodiff { * Reset all adjoint values in this nested stack * to zero. **/ - void set_zero_all_adjoints() { set_zero_all_adjoints_nested(); } + inline void set_zero_all_adjoints() { set_zero_all_adjoints_nested(); } }; } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 55ea7f7ed45..f5664b7f80b 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -11,7 +11,7 @@ namespace math { /** * Reset all adjoint values in the stack to zero. */ -static void set_zero_all_adjoints() { +inline void set_zero_all_adjoints() { for (auto &x : ChainableStack::instance_->var_stack_) { boost::apply_visitor(vari_zero_adj(), x); } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index f94fa618722..290786ab1a0 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -17,24 +17,24 @@ namespace math { * It is preferred to use the nested_rev_autodiff class for * nested autodiff class as it handles recovery of memory automatically. */ -static void set_zero_all_adjoints_nested() { +inline void set_zero_all_adjoints_nested() { if (empty_nested()) { throw std::logic_error( "empty_nested() must be false before calling" " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); + const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 - for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); - i < ChainableStack::instance_->var_stack_.size(); ++i) { + for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_stack_[i]); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); - for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); - i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { + const auto nochain_stack_size = ChainableStack::instance_->var_nochain_stack_.size(); + for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_nochain_stack_[i]); } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 6fc4b62767b..154b1daab26 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -172,7 +172,7 @@ class vari_value::value>> { // For backwards compatability the default is double using vari = vari_value; -class vari_zero_adj : public boost::static_visitor<> { +class vari_zero_adj final : public boost::static_visitor<> { public: template * = nullptr> inline void operator()(vari_value*& x) const { @@ -180,7 +180,7 @@ class vari_zero_adj : public boost::static_visitor<> { } }; -class vari_chainer : public boost::static_visitor<> { +class vari_chainer final : public boost::static_visitor<> { public: template inline void operator()(vari_value*& x) const { @@ -188,7 +188,7 @@ class vari_chainer : public boost::static_visitor<> { } }; -class vari_printer : public boost::static_visitor<> { +class vari_printer final : public boost::static_visitor<> { public: std::ostream& o_; int i_{0}; diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 0ec42273374..12f47c5729c 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -478,7 +478,7 @@ struct coupled_ode_system { dz_dt[offset + i] = temp_deriv; } - nested.set_zero_all_adjoints(); + set_zero_all_adjoints_nested(); // Parameters stored on the outer (non-nested) nochain stack are not // reset to zero by the last call. This is done as a separate step here. // See efficiency note above on template specialization for more details diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 2b792ac41cd..303d00a3ec6 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include From ed1d828ab4efced3b48f76a1120ec55a108aacc2 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 13 Jun 2020 19:17:40 -0400 Subject: [PATCH 062/167] call set_zero_adjoint_nested directly --- stan/math/rev/functor/coupled_ode_system.hpp | 4 ++-- stan/math/rev/functor/jacobian.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 12f47c5729c..773aebe625e 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -143,7 +143,7 @@ struct coupled_ode_system { dz_dt[offset + i] = temp_deriv; } - nested.set_zero_all_adjoints(); + set_zero_all_adjoints_nested(); // Parameters stored on the outer (non-nested) nochain stack are not // reset to zero by the last call. This is done as a separate step here. // See efficiency note above on template specialization for more details @@ -296,7 +296,7 @@ struct coupled_ode_system { dz_dt[offset + i] = temp_deriv; } - nested.set_zero_all_adjoints(); + set_zero_all_adjoints_nested(); } } diff --git a/stan/math/rev/functor/jacobian.hpp b/stan/math/rev/functor/jacobian.hpp index 75692b06911..c388a8bcccb 100644 --- a/stan/math/rev/functor/jacobian.hpp +++ b/stan/math/rev/functor/jacobian.hpp @@ -27,7 +27,7 @@ void jacobian(const F& f, const Eigen::Matrix& x, grad(fx_var(0).vi_); J.col(0) = x_var.adj(); for (int i = 1; i < fx_var.size(); ++i) { - nested.set_zero_all_adjoints(); + set_zero_all_adjoints_nested(); grad(fx_var(i).vi_); J.col(i) = x_var.adj(); } From 22f90a17d9b25c3b9569ec0efe6da598386cb53e Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sat, 13 Jun 2020 19:18:34 -0400 Subject: [PATCH 063/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 290786ab1a0..342bb562c15 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -33,8 +33,10 @@ inline void set_zero_all_adjoints_nested() { size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); - const auto nochain_stack_size = ChainableStack::instance_->var_nochain_stack_.size(); - for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { + const auto nochain_stack_size + = ChainableStack::instance_->var_nochain_stack_.size(); + for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; + ++i) { boost::apply_visitor(vari_zero_adj(), ChainableStack::instance_->var_nochain_stack_[i]); } From d15494d81e9e9e1fbd6dff8df5dae3bfea9818ad Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 13 Jun 2020 20:55:04 -0400 Subject: [PATCH 064/167] try variant2 --- stan/math/rev/core/chainablestack.hpp | 4 +-- stan/math/rev/core/grad.hpp | 2 +- stan/math/rev/core/print_stack.hpp | 2 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 4 +-- .../rev/core/set_zero_all_adjoints_nested.hpp | 4 +-- stan/math/rev/core/vari.hpp | 27 ++++++++++++------- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index e77bfd51e1d..bc35c3179b9 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_REV_CORE_CHAINABLESTACK_HPP #include -#include +#include namespace stan { namespace math { @@ -11,7 +11,7 @@ class vari_value; class vari_base; class chainable_alloc; -using vari_variant = boost::variant*, vari_value*, +using vari_variant = boost::variant2::variant*, vari_value*, vari_value*>; using ChainableStack = AutodiffStackSingleton; diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 2201794c91c..8c06e6351d2 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -33,7 +33,7 @@ static void grad(Vari* vi) { size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::apply_visitor(vari_chainer(), + boost::variant2::visit([&](auto& x) {x->chain();}, ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index d7dc55d185e..3af1193a1c9 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -22,7 +22,7 @@ inline void print_stack(std::ostream& o) { << std::endl; // TODO(carpenter): this shouldn't need to be cast any more for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::apply_visitor(vari_printer(o, i), + boost::variant2::visit(vari_printer(o, i), ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index f5664b7f80b..a9a952157f4 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,10 +13,10 @@ namespace math { */ inline void set_zero_all_adjoints() { for (auto &x : ChainableStack::instance_->var_stack_) { - boost::apply_visitor(vari_zero_adj(), x); + boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, x); } for (auto &x : ChainableStack::instance_->var_nochain_stack_) { - boost::apply_visitor(vari_zero_adj(), x); + boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 290786ab1a0..6695fb7a2ba 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,7 +27,7 @@ inline void set_zero_all_adjoints_nested() { const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - boost::apply_visitor(vari_zero_adj(), + boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, ChainableStack::instance_->var_stack_[i]); } @@ -35,7 +35,7 @@ inline void set_zero_all_adjoints_nested() { = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); const auto nochain_stack_size = ChainableStack::instance_->var_nochain_stack_.size(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { - boost::apply_visitor(vari_zero_adj(), + boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 154b1daab26..d2c77c53bec 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -106,7 +106,17 @@ class vari_value::value>> { */ template ::value>* = nullptr> - vari_value(const vari_value& x) : val_(x.val_), adj_(x.adj_) { + vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { + ChainableStack::instance_->var_stack_.push_back(this); + } + /** + * Constructor from vari_value + * @tparam S An arithmetic type + * @param x A vari_value + */ + template ::value>* = nullptr> + vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { ChainableStack::instance_->var_stack_.push_back(this); } @@ -172,24 +182,21 @@ class vari_value::value>> { // For backwards compatability the default is double using vari = vari_value; -class vari_zero_adj final : public boost::static_visitor<> { - public: - template * = nullptr> - inline void operator()(vari_value*& x) const { +struct vari_zero_adj { + template + inline void operator()(T&& x) const { x->adj_ = 0.0; } }; -class vari_chainer final : public boost::static_visitor<> { - public: +struct vari_chainer { template - inline void operator()(vari_value*& x) const { + inline void operator()(T&& x) const { x->chain(); } }; -class vari_printer final : public boost::static_visitor<> { - public: +struct vari_printer { std::ostream& o_; int i_{0}; vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} From 5c6836bcba648c0d28cd4d8bd07fcc50fa17be75 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 13 Jun 2020 21:30:53 -0400 Subject: [PATCH 065/167] add noexcept to member functions that will not throw --- stan/math/rev/core/vari.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index d2c77c53bec..49adc896fbc 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -68,8 +68,8 @@ class vari_value::value>> { */ template ::value>* = nullptr> - vari_value(S x) : val_(x), adj_(0.0) { // NOLINT - ChainableStack::instance_->var_stack_.push_back(this); + vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT + ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -91,11 +91,11 @@ class vari_value::value>> { */ template ::value>* = nullptr> - vari_value(S x, bool stacked) : val_(x), adj_(0.0) { + vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { if (stacked) { - ChainableStack::instance_->var_stack_.push_back(this); + ChainableStack::instance_->var_stack_.emplace_back(this); } else { - ChainableStack::instance_->var_nochain_stack_.push_back(this); + ChainableStack::instance_->var_nochain_stack_.emplace_back(this); } } @@ -107,7 +107,7 @@ class vari_value::value>> { template ::value>* = nullptr> vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.push_back(this); + ChainableStack::instance_->var_stack_.emplace_back(this); } /** * Constructor from vari_value @@ -117,7 +117,7 @@ class vari_value::value>> { template ::value>* = nullptr> vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.push_back(this); + ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -126,14 +126,14 @@ class vari_value::value>> { * propagating derivatives, setting the derivative of the * result with respect to itself to be 1. */ - inline void init_dependent() { adj_ = 1.0; } + inline void init_dependent() noexcept { adj_ = 1.0; } /** * Set the adjoint value of this variable to 0. This is used to * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - inline void set_zero_adjoint() { adj_ = 0.0; } + inline void set_zero_adjoint() noexcept { adj_ = 0.0; } virtual void chain() {} @@ -160,7 +160,7 @@ class vari_value::value>> { * @param nbytes Number of bytes to allocate. * @return Pointer to allocated bytes. */ - static inline void* operator new(size_t nbytes) { + static inline void* operator new(size_t nbytes) noexcept { return ChainableStack::instance_->memalloc_.alloc(nbytes); } @@ -175,7 +175,7 @@ class vari_value::value>> { * See the discussion of "plugging the memory leak" in: * http://www.parashift.com/c++-faq/memory-pools.html */ - static inline void operator delete(void* /* ignore arg */) { /* no op */ + static inline void operator delete(void* /* ignore arg */) noexcept { /* no op */ } }; From d8bb2ac5353a93ceb73e682b4623498d2a44ac17 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 13 Jun 2020 21:41:16 -0400 Subject: [PATCH 066/167] use lambda for print_stack --- stan/math/rev/core/print_stack.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 3af1193a1c9..97f09da10cc 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -22,8 +22,9 @@ inline void print_stack(std::ostream& o) { << std::endl; // TODO(carpenter): this shouldn't need to be cast any more for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::variant2::visit(vari_printer(o, i), - ChainableStack::instance_->var_stack_[i]); + boost::variant2::visit([&o, &i](auto x) { + o << i << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; + }, ChainableStack::instance_->var_stack_[i]); } } From cd1b3c4f9b7df45d5b5dca39881853c219424e47 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 14 Jun 2020 01:46:34 +0000 Subject: [PATCH 067/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/chainablestack.hpp | 5 +++-- stan/math/rev/core/grad.hpp | 4 ++-- stan/math/rev/core/print_stack.hpp | 9 ++++++--- stan/math/rev/core/set_zero_all_adjoints.hpp | 8 ++++---- .../math/rev/core/set_zero_all_adjoints_nested.hpp | 14 ++++++++------ stan/math/rev/core/vari.hpp | 3 ++- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index bc35c3179b9..6dd314a84c6 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -11,8 +11,9 @@ class vari_value; class vari_base; class chainable_alloc; -using vari_variant = boost::variant2::variant*, vari_value*, - vari_value*>; +using vari_variant + = boost::variant2::variant*, vari_value*, + vari_value*>; using ChainableStack = AutodiffStackSingleton; diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 8c06e6351d2..4aa2d6fc66f 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -33,8 +33,8 @@ static void grad(Vari* vi) { size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::variant2::visit([&](auto& x) {x->chain();}, - ChainableStack::instance_->var_stack_[i]); + boost::variant2::visit([&](auto& x) { x->chain(); }, + ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 97f09da10cc..7718dbefa26 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -22,9 +22,12 @@ inline void print_stack(std::ostream& o) { << std::endl; // TODO(carpenter): this shouldn't need to be cast any more for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::variant2::visit([&o, &i](auto x) { - o << i << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; - }, ChainableStack::instance_->var_stack_[i]); + boost::variant2::visit( + [&o, &i](auto x) { + o << i << " " << x << " " << x->val_ << " : " << x->adj_ + << std::endl; + }, + ChainableStack::instance_->var_stack_[i]); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index a9a952157f4..7b5714ede0a 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -12,11 +12,11 @@ namespace math { * Reset all adjoint values in the stack to zero. */ inline void set_zero_all_adjoints() { - for (auto &x : ChainableStack::instance_->var_stack_) { - boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, x); + for (auto& x : ChainableStack::instance_->var_stack_) { + boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, x); } - for (auto &x : ChainableStack::instance_->var_nochain_stack_) { - boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, x); + for (auto& x : ChainableStack::instance_->var_nochain_stack_) { + boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 6695fb7a2ba..d128bf88bc1 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,16 +27,18 @@ inline void set_zero_all_adjoints_nested() { const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, - ChainableStack::instance_->var_stack_[i]); + boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, + ChainableStack::instance_->var_stack_[i]); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); - const auto nochain_stack_size = ChainableStack::instance_->var_nochain_stack_.size(); - for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { - boost::variant2::visit([&](auto& x) {x->adj_ = 0.0;}, - ChainableStack::instance_->var_nochain_stack_[i]); + const auto nochain_stack_size + = ChainableStack::instance_->var_nochain_stack_.size(); + for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; + ++i) { + boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, + ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 49adc896fbc..dca9da48a19 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -175,7 +175,8 @@ class vari_value::value>> { * See the discussion of "plugging the memory leak" in: * http://www.parashift.com/c++-faq/memory-pools.html */ - static inline void operator delete(void* /* ignore arg */) noexcept { /* no op */ + static inline void operator delete( + void* /* ignore arg */) noexcept { /* no op */ } }; From 5630a65d73c9e4f511a4dcf8439a9d71f4909f8d Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 14 Jun 2020 16:10:20 -0400 Subject: [PATCH 068/167] use eigen strong inline and static on set_zero_adj() --- stan/math/rev/core/chainablestack.hpp | 3 +-- stan/math/rev/core/set_zero_all_adjoints.hpp | 6 +++--- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 6 +++--- test/unit/math/rev/core/var_test.cpp | 1 - test/unit/math/rev/core/vari_test.cpp | 2 +- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 6dd314a84c6..0eb6aada190 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -12,8 +12,7 @@ class vari_base; class chainable_alloc; using vari_variant - = boost::variant2::variant*, vari_value*, - vari_value*>; + = boost::variant2::variant*, vari_value*>; using ChainableStack = AutodiffStackSingleton; diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 7b5714ede0a..5cb59f39c26 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -11,12 +11,12 @@ namespace math { /** * Reset all adjoint values in the stack to zero. */ -inline void set_zero_all_adjoints() { + static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { for (auto& x : ChainableStack::instance_->var_stack_) { - boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, x); + boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); } for (auto& x : ChainableStack::instance_->var_nochain_stack_) { - boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, x); + boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index d128bf88bc1..0b7fc446a62 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -17,7 +17,7 @@ namespace math { * It is preferred to use the nested_rev_autodiff class for * nested autodiff class as it handles recovery of memory automatically. */ -inline void set_zero_all_adjoints_nested() { +static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { if (empty_nested()) { throw std::logic_error( "empty_nested() must be false before calling" @@ -27,7 +27,7 @@ inline void set_zero_all_adjoints_nested() { const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, + boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, ChainableStack::instance_->var_stack_[i]); } @@ -37,7 +37,7 @@ inline void set_zero_all_adjoints_nested() { = ChainableStack::instance_->var_nochain_stack_.size(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { - boost::variant2::visit([&](auto& x) { x->adj_ = 0.0; }, + boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 303d00a3ec6..e9c744d6e22 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -59,7 +59,6 @@ void ctor_overloads() { TEST_F(AgradRev, ctorOverloads) { ctor_overloads(); ctor_overloads(); - ctor_overloads(); } TEST_F(AgradRev, a_eq_x) { diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index fc5a6e34ab5..355d74a6769 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -10,7 +10,7 @@ TEST(AgradRev, insertion_operator) { } TEST(AgradRev, long_double_test) { - stan::math::vari_value v(5); + stan::math::vari_value v(5); std::stringstream ss; ss << &v; EXPECT_EQ("5:0", ss.str()); From 5285fdd7899db5faf9d5d8a36c098c2c09915167 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 14 Jun 2020 16:11:13 -0400 Subject: [PATCH 069/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/set_zero_all_adjoints.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 5cb59f39c26..cfd1edb83ec 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -11,7 +11,7 @@ namespace math { /** * Reset all adjoint values in the stack to zero. */ - static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { +static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { for (auto& x : ChainableStack::instance_->var_stack_) { boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); } From 006fbf60bfd9a3ec9e2030408a7b81c9479c197c Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 14 Jun 2020 16:20:18 -0400 Subject: [PATCH 070/167] testing if vari long slows us down --- stan/math/rev/core/chainablestack.hpp | 2 +- stan/math/rev/core/vari.hpp | 17 +---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 0eb6aada190..730dbb40b98 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -12,7 +12,7 @@ class vari_base; class chainable_alloc; using vari_variant - = boost::variant2::variant*, vari_value*>; + = boost::variant2::variant*, vari_value*, vari_value*>; using ChainableStack = AutodiffStackSingleton; diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index dca9da48a19..50782d730b0 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -175,28 +175,13 @@ class vari_value::value>> { * See the discussion of "plugging the memory leak" in: * http://www.parashift.com/c++-faq/memory-pools.html */ - static inline void operator delete( - void* /* ignore arg */) noexcept { /* no op */ + static inline void operator delete(void* /* ignore arg */) noexcept { /* no op */ } }; // For backwards compatability the default is double using vari = vari_value; -struct vari_zero_adj { - template - inline void operator()(T&& x) const { - x->adj_ = 0.0; - } -}; - -struct vari_chainer { - template - inline void operator()(T&& x) const { - x->chain(); - } -}; - struct vari_printer { std::ostream& o_; int i_{0}; From 538b0118d2c05378cac09a1866b29077387045a8 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 14 Jun 2020 16:27:12 -0400 Subject: [PATCH 071/167] add back tests for vari long double --- test/unit/math/rev/core/var_test.cpp | 1 + test/unit/math/rev/core/vari_test.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index e9c744d6e22..303d00a3ec6 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -59,6 +59,7 @@ void ctor_overloads() { TEST_F(AgradRev, ctorOverloads) { ctor_overloads(); ctor_overloads(); + ctor_overloads(); } TEST_F(AgradRev, a_eq_x) { diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index 355d74a6769..fc5a6e34ab5 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -10,7 +10,7 @@ TEST(AgradRev, insertion_operator) { } TEST(AgradRev, long_double_test) { - stan::math::vari_value v(5); + stan::math::vari_value v(5); std::stringstream ss; ss << &v; EXPECT_EQ("5:0", ss.str()); From ad2d15f2a80a264fc3fcb3cf13e4b4e266da8db2 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 14 Jun 2020 16:28:38 -0400 Subject: [PATCH 072/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/chainablestack.hpp | 3 ++- stan/math/rev/core/vari.hpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 730dbb40b98..6dd314a84c6 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -12,7 +12,8 @@ class vari_base; class chainable_alloc; using vari_variant - = boost::variant2::variant*, vari_value*, vari_value*>; + = boost::variant2::variant*, vari_value*, + vari_value*>; using ChainableStack = AutodiffStackSingleton; diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 50782d730b0..d5749507323 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -175,7 +175,8 @@ class vari_value::value>> { * See the discussion of "plugging the memory leak" in: * http://www.parashift.com/c++-faq/memory-pools.html */ - static inline void operator delete(void* /* ignore arg */) noexcept { /* no op */ + static inline void operator delete( + void* /* ignore arg */) noexcept { /* no op */ } }; From 28f3373d6eeaeababf2e6e6d11d8d2d1e9568b68 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 15 Jun 2020 17:50:43 -0400 Subject: [PATCH 073/167] remove templates from operators --- stan/math/rev/core/operator_divide_equal.hpp | 7 ++-- stan/math/rev/core/operator_minus_equal.hpp | 7 ++-- .../math/rev/core/operator_multiply_equal.hpp | 7 ++-- stan/math/rev/core/operator_plus_equal.hpp | 7 ++-- stan/math/rev/core/var.hpp | 34 +++++++------------ stan/math/rev/core/vari.hpp | 22 +++++------- 6 files changed, 29 insertions(+), 55 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 3dae0cc79cb..d3a432b9ed3 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -8,17 +8,14 @@ namespace stan { namespace math { template -template *> inline var_value& var_value>::operator/=( - const var_value& b) { + const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template -template *> -inline var_value& var_value>::operator/=( - Arith b) { +inline var_value& var_value>::operator/=(T b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index e70a03bf69b..3a9513a024f 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -9,17 +9,14 @@ namespace stan { namespace math { template -template *> inline var_value& var_value>::operator-=( - const var_value& b) { + const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template -template *> -inline var_value& var_value>::operator-=( - Arith b) { +inline var_value& var_value>::operator-=(T b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index c995b6fa905..69b09cdc4fa 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -9,17 +9,14 @@ namespace stan { namespace math { template -template *> inline var_value& var_value>::operator*=( - const var_value& b) { + const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template -template *> -inline var_value& var_value>::operator*=( - Arith b) { +inline var_value& var_value>::operator*=(T b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index 0da8b734b22..864d5f45d19 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -9,17 +9,14 @@ namespace stan { namespace math { template -template *> inline var_value& var_value>::operator+=( - const var_value& b) { + const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template -template *> -inline var_value& var_value>::operator+=( - Arith b) { +inline var_value& var_value>::operator+=(T b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index e7de6b0a820..eed077e0fe5 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -84,8 +84,8 @@ class var_value> { * @tparam S A type that is convertible to `value_type`. * @param x Value of the variable. */ - template * = nullptr> - var_value(S x) : vi_(new vari_type(x, false)) {} // NOLINT + template * = nullptr> + var_value(const S& x) : vi_(new vari_type(x, false)) {} // NOLINT /** * Construct a variable from a pointer to a variable implementation. @@ -94,8 +94,7 @@ class var_value> { * same as `vari_value` so can be `reinterpret_cast` without a copy. * @param vi A vari_value pointer. */ - template * = nullptr> - var_value(vari_value* vi) // NOLINT + var_value(vari_value* vi) // NOLINT : vi_(vi) {} /** @@ -104,8 +103,7 @@ class var_value> { * `var_value a(4.0); var_value b(a)` since the `value_type` for * a `var_value` with an integral type is a double. */ - template * = nullptr> - var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT + var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT /** * Return a constant reference to the value of this variable. @@ -203,8 +201,7 @@ class var_value> { * @param b The variable to add to this variable. * @return The result of adding the specified variable to this variable. */ - template * = nullptr> - inline var_value& operator+=(const var_value& b); + inline var_value& operator+=(const var_value& b); /** * The compound add/assignment operator for scalars (C++). @@ -217,8 +214,7 @@ class var_value> { * @param b The scalar to add to this variable. * @return The result of adding the specified variable to this variable. */ - template * = nullptr> - inline var_value& operator+=(Arith b); + inline var_value& operator+=(T b); /** * The compound subtract/assignment operator for variables (C++). @@ -232,8 +228,7 @@ class var_value> { * @return The result of subtracting the specified variable from * this variable. */ - template * = nullptr> - inline var_value& operator-=(const var_value& b); + inline var_value& operator-=(const var_value& b); /** * The compound subtract/assignment operator for scalars (C++). @@ -247,8 +242,7 @@ class var_value> { * @return The result of subtracting the specified variable from this * variable. */ - template * = nullptr> - inline var_value& operator-=(Arith b); + inline var_value& operator-=(T b); /** * The compound multiply/assignment operator for variables (C++). @@ -262,8 +256,7 @@ class var_value> { * @return The result of multiplying this variable by the * specified variable. */ - template * = nullptr> - inline var_value& operator*=(const var_value& b); + inline var_value& operator*=(const var_value& b); /** * The compound multiply/assignment operator for scalars (C++). @@ -277,8 +270,7 @@ class var_value> { * @return The result of multiplying this variable by the specified * variable. */ - template * = nullptr> - inline var_value& operator*=(Arith b); + inline var_value& operator*=(T b); /** * The compound divide/assignment operator for variables (C++). If this @@ -291,8 +283,7 @@ class var_value> { * @return The result of dividing this variable by the * specified variable. */ - template * = nullptr> - inline var_value& operator/=(const var_value& b); + inline var_value& operator/=(const var_value& b); /** * The compound divide/assignment operator for scalars (C++). @@ -306,8 +297,7 @@ class var_value> { * @return The result of dividing this variable by the specified * variable. */ - template * = nullptr> - inline var_value& operator/=(Arith b); + inline var_value& operator/=(T b); /** * Write the value of this autodiff variable and its adjoint to diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index d5749507323..f105e7f8c51 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -66,11 +66,11 @@ class vari_value::value>> { * @tparam S an Arithmetic type. * @param x Value of the constructed variable. */ - template ::value>* = nullptr> - vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT - ChainableStack::instance_->var_stack_.emplace_back(this); - } + template ::value>* = nullptr> + vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT + ChainableStack::instance_->var_stack_.emplace_back(this); + } /** * Construct a variable implementation from a value. The @@ -89,8 +89,8 @@ class vari_value::value>> { * @param stacked If false will put this this vari on the nochain stack so * that its `chain()` method is not called. */ - template ::value>* = nullptr> + template ::value>* = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); @@ -104,9 +104,7 @@ class vari_value::value>> { * @tparam S An arithmetic type * @param x A vari_value */ - template ::value>* = nullptr> - vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { + vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -114,9 +112,7 @@ class vari_value::value>> { * @tparam S An arithmetic type * @param x A vari_value */ - template ::value>* = nullptr> - vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { + vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { ChainableStack::instance_->var_stack_.emplace_back(this); } From 0324c82f4b9cd7095c4bbe93c6681cfc48e8d846 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 15 Jun 2020 17:51:37 -0400 Subject: [PATCH 074/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/operator_divide_equal.hpp | 3 ++- stan/math/rev/core/operator_minus_equal.hpp | 3 ++- stan/math/rev/core/operator_multiply_equal.hpp | 3 ++- stan/math/rev/core/operator_plus_equal.hpp | 3 ++- stan/math/rev/core/var.hpp | 4 ++-- stan/math/rev/core/vari.hpp | 14 +++++++------- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index d3a432b9ed3..23687c7011e 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -15,7 +15,8 @@ inline var_value& var_value>::operator/=( } template -inline var_value& var_value>::operator/=(T b) { +inline var_value& var_value>::operator/=( + T b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 3a9513a024f..f72f7d7412d 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -16,7 +16,8 @@ inline var_value& var_value>::operator-=( } template -inline var_value& var_value>::operator-=(T b) { +inline var_value& var_value>::operator-=( + T b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 69b09cdc4fa..61c13263c19 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -16,7 +16,8 @@ inline var_value& var_value>::operator*=( } template -inline var_value& var_value>::operator*=(T b) { +inline var_value& var_value>::operator*=( + T b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index 864d5f45d19..32d41b30772 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -16,7 +16,8 @@ inline var_value& var_value>::operator+=( } template -inline var_value& var_value>::operator+=(T b) { +inline var_value& var_value>::operator+=( + T b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index eed077e0fe5..c8001d28963 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -84,8 +84,8 @@ class var_value> { * @tparam S A type that is convertible to `value_type`. * @param x Value of the variable. */ - template * = nullptr> - var_value(const S& x) : vi_(new vari_type(x, false)) {} // NOLINT + template * = nullptr> + var_value(const S& x) : vi_(new vari_type(x, false)) {} // NOLINT /** * Construct a variable from a pointer to a variable implementation. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index f105e7f8c51..e6ba6ce1579 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -66,11 +66,11 @@ class vari_value::value>> { * @tparam S an Arithmetic type. * @param x Value of the constructed variable. */ - template ::value>* = nullptr> - vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT - ChainableStack::instance_->var_stack_.emplace_back(this); - } + template ::value>* = nullptr> + vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT + ChainableStack::instance_->var_stack_.emplace_back(this); + } /** * Construct a variable implementation from a value. The @@ -89,8 +89,8 @@ class vari_value::value>> { * @param stacked If false will put this this vari on the nochain stack so * that its `chain()` method is not called. */ - template ::value>* = nullptr> + template ::value>* = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); From e30e11121efd520cc9dace588bca52c3399b5491 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 14:54:49 -0400 Subject: [PATCH 075/167] add dense eigen vari type --- stan/math/prim/meta.hpp | 2 + stan/math/prim/meta/is_eigen_dense_base.hpp | 32 ++++ stan/math/prim/meta/is_eigen_sparse_base.hpp | 32 ++++ stan/math/rev/core/chainablestack.hpp | 10 +- stan/math/rev/core/make_vari_variadic.hpp | 107 ++++++++++++++ stan/math/rev/core/set_zero_all_adjoints.hpp | 4 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 4 +- stan/math/rev/core/vari.hpp | 139 ++++++++++++++++++ 8 files changed, 317 insertions(+), 13 deletions(-) create mode 100644 stan/math/prim/meta/is_eigen_dense_base.hpp create mode 100644 stan/math/prim/meta/is_eigen_sparse_base.hpp create mode 100644 stan/math/rev/core/make_vari_variadic.hpp diff --git a/stan/math/prim/meta.hpp b/stan/math/prim/meta.hpp index 0de5dc8f17a..5ef0611c798 100644 --- a/stan/math/prim/meta.hpp +++ b/stan/math/prim/meta.hpp @@ -189,8 +189,10 @@ #include #include #include +#include #include #include +#include #include #include #include diff --git a/stan/math/prim/meta/is_eigen_dense_base.hpp b/stan/math/prim/meta/is_eigen_dense_base.hpp new file mode 100644 index 00000000000..d838d83af90 --- /dev/null +++ b/stan/math/prim/meta/is_eigen_dense_base.hpp @@ -0,0 +1,32 @@ +#ifndef STAN_MATH_PRIM_META_IS_EIGEN_DENSE_BASE_HPP +#define STAN_MATH_PRIM_META_IS_EIGEN_DENSE_BASE_HPP + +#include +#include +#include +#include +#include + +namespace stan { + +/** + * Checks whether type T is derived from Eigen::MatrixBase. + * If true this will have a static member function named value with a type + * of true, else value is false. + * @tparam T Type to check if it is derived from `EigenBase` + * @tparam Enable used for SFINAE deduction. + * @ingroup type_trait + */ +template +struct is_eigen_dense_base + : bool_constant::value> { +}; + +STAN_ADD_REQUIRE_UNARY(eigen_dense_base, is_eigen_dense_base, + require_eigens_types); +STAN_ADD_REQUIRE_CONTAINER(eigen_dense_base, is_eigen_dense_base, + require_eigens_types); + +} // namespace stan + +#endif diff --git a/stan/math/prim/meta/is_eigen_sparse_base.hpp b/stan/math/prim/meta/is_eigen_sparse_base.hpp new file mode 100644 index 00000000000..0628b3dce66 --- /dev/null +++ b/stan/math/prim/meta/is_eigen_sparse_base.hpp @@ -0,0 +1,32 @@ +#ifndef STAN_MATH_PRIM_META_IS_EIGEN_SPARSE_BASE_HPP +#define STAN_MATH_PRIM_META_IS_EIGEN_SPARSE_BASE_HPP + +#include +#include +#include +#include +#include + +namespace stan { + +/** + * Checks whether type T is derived from Eigen::MatrixBase. + * If true this will have a static member function named value with a type + * of true, else value is false. + * @tparam T Type to check if it is derived from `EigenBase` + * @tparam Enable used for SFINAE deduction. + * @ingroup type_trait + */ +template +struct is_eigen_sparse_base + : bool_constant::value> { +}; + +STAN_ADD_REQUIRE_UNARY(eigen_sparse_base, is_eigen_sparse_base, + require_eigens_types); +STAN_ADD_REQUIRE_CONTAINER(eigen_sparse_base, is_eigen_sparse_base, + require_eigens_types); + +} // namespace stan + +#endif diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 6dd314a84c6..9855405f13f 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -2,19 +2,11 @@ #define STAN_MATH_REV_CORE_CHAINABLESTACK_HPP #include +#include #include namespace stan { namespace math { - -template -class vari_value; -class vari_base; class chainable_alloc; - -using vari_variant - = boost::variant2::variant*, vari_value*, - vari_value*>; - using ChainableStack = AutodiffStackSingleton; } // namespace math diff --git a/stan/math/rev/core/make_vari_variadic.hpp b/stan/math/rev/core/make_vari_variadic.hpp new file mode 100644 index 00000000000..27cb8d94397 --- /dev/null +++ b/stan/math/rev/core/make_vari_variadic.hpp @@ -0,0 +1,107 @@ +#ifndef STAN_MATH_REV_CORE_MAKE_VARI_VARIANT_HPP +#define STAN_MATH_REV_CORE_MAKE_VARI_VARIANT_HPP + +#include +#include +#include + +namespace stan { +namespace math { +template +class vari_value; + +namespace internal { +// Make one tuple out of N tuples +template +struct concat_tuple { + using type = decltype(std::tuple_cat(std::declval()...)); +}; +// helper +template +using concat_tuple_t = typename concat_tuple::type; + +// All the vari_value Matrix types +template +struct vari_eigen_mat_tuple { + using type = std::tuple< + vari_value>, + vari_value>, + vari_value>, + vari_value>>; +}; + +template +using vari_eigen_mat_tuple_t = typename vari_eigen_mat_tuple::type; + +template +struct vari_eigen_arr_tuple { + using type = std::tuple< + vari_value>, + vari_value>, + vari_value>, + vari_value>>; +}; + +template +using vari_eigen_arr_tuple_t = typename vari_eigen_arr_tuple::type; + +template +struct vari_eigen_sparsemat_tuple { + using type = std::tuple< + vari_value>, + vari_value>>; +}; + +template +using vari_eigen_sparsemat_tuple_t = typename vari_eigen_sparsemat_tuple::type; + +// All of the vari_value mixes +template +struct vari_combinations { + using type = concat_tuple_t< + vari_eigen_mat_tuple_t, + vari_eigen_arr_tuple_t, +// vari_eigen_sparsemat_tuple_t, + std::tuple>>; +}; + +template +using vari_combinations_t = typename vari_combinations::type; + + +template +struct make_vari_types; + + // break condition on default type which ends the recursion +template <> +struct make_vari_types { + using type = std::tuple<>; +}; + +// alias template +template +using make_vari_types_t = typename make_vari_types::type; + +// Take all the base types T and make their vari_value equivalents +template +struct make_vari_types { + using type = concat_tuple_t, make_vari_types_t>; +}; + +template +struct make_vari_variadic {}; + +// Take the tuple of all the types and place them in boost::variant +template +struct make_vari_variadic> { + using type = boost::variant2::variant; +}; +} +// Create boost variant holding pointers to all the vari_value types +template +using make_vari_variadic_t = typename internal::make_vari_variadic>::type; + +using vari_variant = make_vari_variadic_t; +} +} +#endif diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index cfd1edb83ec..1d706b096bb 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,10 +13,10 @@ namespace math { */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { for (auto& x : ChainableStack::instance_->var_stack_) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); + boost::variant2::visit([](auto& x) { x->set_zero_adjoint(); }, x); } for (auto& x : ChainableStack::instance_->var_nochain_stack_) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); + boost::variant2::visit([](auto& x) { x->set_zero_adjoint(); }, x); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 0b7fc446a62..09aa3cd6559 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,7 +27,7 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, + boost::variant2::visit([](auto& x) { x->set_zero_adjoint(); }, ChainableStack::instance_->var_stack_[i]); } @@ -37,7 +37,7 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { = ChainableStack::instance_->var_nochain_stack_.size(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, + boost::variant2::visit([](auto& x) { x->set_zero_adjoint(); }, ChainableStack::instance_->var_nochain_stack_[i]); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index e6ba6ce1579..f0e6ffc6bb2 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -179,6 +179,145 @@ class vari_value::value>> { // For backwards compatability the default is double using vari = vari_value; + + +template +class vari_value::value>> { + private: + template + friend class var_value; + using eigen_scalar = value_type_t; + value_type_t* val_mem_; + value_type_t* adj_mem_; + + public: + using Scalar = T; + using value_type = T; + using PlainObject = typename T::PlainObject; + using eigen_map = Eigen::Map; + const Eigen::Index rows_; + const Eigen::Index cols_; + const Eigen::Index size_; + const Eigen::Index RowsAtCompileTime = T::RowsAtCompileTime; + const Eigen::Index ColsAtCompileTime = T::ColsAtCompileTime; + /** + * The value of this variable. + */ + eigen_map val_; + + /** + * The adjoint of this variable, which is the partial derivative + * of this variable with respect to the root variable. + */ + eigen_map adj_; + + /** + * Construct a variable implementation from a value. The + * adjoint is initialized to zero. + * + * All constructed variables are added to the stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @param x Value of the constructed variable. + */ + explicit vari_value(const T& x) + : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.size())), + adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.size())), + rows_(x.rows()), + cols_(x.cols()), + size_(x.size()), + val_(val_mem_, x.rows(), x.cols()), + adj_(adj_mem_, x.rows(), x.cols()) { + val_ = x; + adj_.setZero(); + ChainableStack::instance_->var_stack_.push_back(this); + } + + vari_value(const T& x, bool stacked) + : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.size())), + adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.size())), + rows_(x.rows()), + cols_(x.cols()), + size_(x.size()), + val_(val_mem_, x.rows(), x.cols()), + adj_(adj_mem_, x.rows(), x.cols()) { + val_ = x; + adj_.setZero(); + if (stacked) { + ChainableStack::instance_->var_stack_.push_back(this); + } else { + ChainableStack::instance_->var_nochain_stack_.push_back(this); + } + } + + const Eigen::Index rows() const { return rows_; } + const Eigen::Index cols() const { return cols_; } + const Eigen::Index size() const { return size_; } + virtual void chain() {} + /** + * Initialize the adjoint for this (dependent) variable to 1. + * This operation is applied to the dependent variable before + * propagating derivatives, setting the derivative of the + * result with respect to itself to be 1. + */ + void init_dependent() { adj_.setOnes(); } + + /** + * Set the adjoint value of this variable to 0. This is used to + * reset adjoints before propagating derivatives again (for + * example in a Jacobian calculation). + */ + void set_zero_adjoint() { adj_.setZero(); } + + /** + * Insertion operator for vari. Prints the current value and + * the adjoint value. + * + * @param os [in, out] ostream to modify + * @param v [in] vari object to print. + * + * @return The modified ostream. + */ + friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { + return os << v->val_ << ":" << v->adj_; + } + + /** + * Allocate memory from the underlying memory pool. This memory is + * is managed as a whole externally. + * + * Warning: Classes should not be allocated with this operator + * if they have non-trivial destructors. + * + * @param nbytes Number of bytes to allocate. + * @return Pointer to allocated bytes. + */ + static inline void* operator new(size_t nbytes) { + return ChainableStack::instance_->memalloc_.alloc(nbytes); + } + + /** + * Delete a pointer from the underlying memory pool. + * + * This no-op implementation enables a subclass to throw + * exceptions in its constructor. An exception thrown in the + * constructor of a subclass will result in an error being + * raised, which is in turn caught and calls delete(). + * + * See the discussion of "plugging the memory leak" in: + * http://www.parashift.com/c++-faq/memory-pools.html + */ + static inline void operator delete(void* /* ignore arg */) { /* no op */ + } +}; + struct vari_printer { std::ostream& o_; int i_{0}; From 3f8833589f94c17eab19711da8df8f9b05ac33ba Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 16:24:34 -0400 Subject: [PATCH 076/167] try custom visitor --- stan/math/rev/core/grad.hpp | 4 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 11 ++--- stan/math/rev/core/vari_visitor.hpp | 47 +++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 stan/math/rev/core/vari_visitor.hpp diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 4aa2d6fc66f..4c4eaabaf04 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace stan { @@ -33,8 +34,7 @@ static void grad(Vari* vi) { size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::variant2::visit([&](auto& x) { x->chain(); }, - ChainableStack::instance_->var_stack_[i]); + vari_visitor(ChainableStack::instance_->var_stack_[i], [&](auto& x) { x->chain(); }); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 09aa3cd6559..9339682a8e5 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace stan { @@ -27,18 +28,16 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - boost::variant2::visit([](auto& x) { x->set_zero_adjoint(); }, - ChainableStack::instance_->var_stack_[i]); + vari_visitor(ChainableStack::instance_->var_stack_[i], [](auto& x) { x->set_zero_adjoint(); }); } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); const auto nochain_stack_size = ChainableStack::instance_->var_nochain_stack_.size(); - for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; - ++i) { - boost::variant2::visit([](auto& x) { x->set_zero_adjoint(); }, - ChainableStack::instance_->var_nochain_stack_[i]); + for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { + vari_visitor(ChainableStack::instance_->var_nochain_stack_[i], + [](auto& x) { x->set_zero_adjoint(); }); } } diff --git a/stan/math/rev/core/vari_visitor.hpp b/stan/math/rev/core/vari_visitor.hpp new file mode 100644 index 00000000000..f8ae7a65804 --- /dev/null +++ b/stan/math/rev/core/vari_visitor.hpp @@ -0,0 +1,47 @@ +#ifndef STAN_MATH_REV_CORE_VARI_VISITOR_HPP +#define STAN_MATH_REV_CORE_VARI_VISITOR_HPP + +#include +#include + +namespace stan { +namespace math { + +template +struct visit_nt_impl; + +template +struct visit_nt_impl>> { + template + inline static constexpr auto apply(Variant &&var, Visitor &&vis) noexcept { + if (N == var.index()) { + // If this check isnt there the compiler will generate + // exception code, this stops that + return std::forward(vis)( + boost::variant2::get(std::forward(var))); + } + } +}; +template +struct visit_nt_impl 0)>>> { + template + inline static constexpr auto apply(Variant &&var, Visitor &&vis) noexcept { + if (likely(var.index() == N)) { + return std::forward(vis)( + *boost::variant2::get_if*>(&std::forward(var))); + } else { + return visit_nt_impl::apply(std::forward(var), + std::forward(vis)); + } + } +}; + +template +constexpr auto vari_visitor(Variant&& var, Visitor&& vis) noexcept { + constexpr auto sizer = boost::variant2::variant_size::value; + return visit_nt_impl::apply(std::forward(var), std::forward(vis)); +} + +} +} +#endif From 4104696e50b46e9eed88ddad9a9ed0df57422c82 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 16:46:40 -0400 Subject: [PATCH 077/167] switching back to virtual --- stan/math/rev/core/autodiffstackstorage.hpp | 4 ++-- stan/math/rev/core/chainable_alloc.hpp | 1 - stan/math/rev/core/chainablestack.hpp | 7 +------ stan/math/rev/core/grad.hpp | 3 +-- stan/math/rev/core/print_stack.hpp | 9 --------- stan/math/rev/core/set_zero_all_adjoints.hpp | 4 ++-- .../rev/core/set_zero_all_adjoints_nested.hpp | 6 ++---- stan/math/rev/core/vari.hpp | 20 +++++++------------ 8 files changed, 15 insertions(+), 39 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 9b2dc106fdf..e0b993081c5 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -101,8 +101,8 @@ struct AutodiffStackSingleton { struct AutodiffStackStorage { AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; - std::vector var_stack_; - std::vector var_nochain_stack_; + std::vector var_stack_; + std::vector var_nochain_stack_; std::vector var_alloc_stack_; stack_alloc memalloc_; diff --git a/stan/math/rev/core/chainable_alloc.hpp b/stan/math/rev/core/chainable_alloc.hpp index 3541e88aee6..6ff43fd0e43 100644 --- a/stan/math/rev/core/chainable_alloc.hpp +++ b/stan/math/rev/core/chainable_alloc.hpp @@ -18,7 +18,6 @@ class chainable_alloc { chainable_alloc() { ChainableStack::instance_->var_alloc_stack_.push_back(this); } - virtual ~chainable_alloc() {} }; } // namespace math diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 6dd314a84c6..ecbf61a7c43 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -10,12 +10,7 @@ template class vari_value; class vari_base; class chainable_alloc; - -using vari_variant - = boost::variant2::variant*, vari_value*, - vari_value*>; - -using ChainableStack = AutodiffStackSingleton; +using ChainableStack = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 4aa2d6fc66f..efe8fee1e87 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -33,8 +33,7 @@ static void grad(Vari* vi) { size_t end = ChainableStack::instance_->var_stack_.size(); size_t beginning = empty_nested() ? 0 : end - nested_size(); for (size_t i = end; i-- > beginning;) { - boost::variant2::visit([&](auto& x) { x->chain(); }, - ChainableStack::instance_->var_stack_[i]); + ChainableStack::instance_->var_stack_[i]->chain(); } } diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 7718dbefa26..a81b03cc51e 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -20,15 +20,6 @@ namespace math { inline void print_stack(std::ostream& o) { o << "STACK, size=" << ChainableStack::instance_->var_stack_.size() << std::endl; - // TODO(carpenter): this shouldn't need to be cast any more - for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { - boost::variant2::visit( - [&o, &i](auto x) { - o << i << " " << x << " " << x->val_ << " : " << x->adj_ - << std::endl; - }, - ChainableStack::instance_->var_stack_[i]); - } } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index cfd1edb83ec..27a730f8d3c 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,10 +13,10 @@ namespace math { */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { for (auto& x : ChainableStack::instance_->var_stack_) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); + x->set_zero_adjoint(); } for (auto& x : ChainableStack::instance_->var_nochain_stack_) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, x); + x->set_zero_adjoint(); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 0b7fc446a62..1fc4c88eaef 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,8 +27,7 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { const auto stack_size = ChainableStack::instance_->var_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, - ChainableStack::instance_->var_stack_[i]); + ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); } size_t start2 @@ -37,8 +36,7 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { = ChainableStack::instance_->var_nochain_stack_.size(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; ++i) { - boost::variant2::visit([](auto& x) { x->adj_ = 0.0; }, - ChainableStack::instance_->var_nochain_stack_[i]); + ChainableStack::instance_->var_nochain_stack_[i]->set_zero_adjoint(); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index e6ba6ce1579..81066ffbeaf 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -14,6 +14,11 @@ namespace math { template class var_value; +class vari_base { +public: + virtual void chain() {} + virtual void set_zero_adjoint() noexcept {} +}; /** * The variable implementation base class. * @@ -34,7 +39,7 @@ template class vari_value; template -class vari_value::value>> { +class vari_value::value>> : public vari_base { private: template friend class var_value; @@ -129,9 +134,7 @@ class vari_value::value>> { * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - inline void set_zero_adjoint() noexcept { adj_ = 0.0; } - - virtual void chain() {} + void set_zero_adjoint() noexcept final { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and @@ -179,15 +182,6 @@ class vari_value::value>> { // For backwards compatability the default is double using vari = vari_value; -struct vari_printer { - std::ostream& o_; - int i_{0}; - vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} - template - inline void operator()(T*& x) const { - o_ << i_ << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; - } -}; } // namespace math } // namespace stan #endif From ce718a88afba8743bba568ad583f6d10e555c092 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 17:53:50 -0400 Subject: [PATCH 078/167] Trying multi stack method --- stan/math/rev/core/autodiffstackstorage.hpp | 4 ++++ stan/math/rev/core/chainablestack.hpp | 2 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 5 +---- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 13 ++----------- stan/math/rev/core/vari.hpp | 3 +-- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index e0b993081c5..847aab7d1ac 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -24,6 +24,9 @@ namespace math { #define STAN_THREADS_DEF #endif +template +class vari_value; + /** * This struct always provides access to the autodiff stack using * the singleton pattern. Read warnings below! @@ -103,6 +106,7 @@ struct AutodiffStackSingleton { std::vector var_stack_; std::vector var_nochain_stack_; + std::vector*> var_dbl_stack_; std::vector var_alloc_stack_; stack_alloc memalloc_; diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index ecbf61a7c43..47d94983ca6 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -6,7 +6,7 @@ namespace stan { namespace math { -template +template class vari_value; class vari_base; class chainable_alloc; diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 27a730f8d3c..abd4f87d2e0 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -12,10 +12,7 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { - for (auto& x : ChainableStack::instance_->var_stack_) { - x->set_zero_adjoint(); - } - for (auto& x : ChainableStack::instance_->var_nochain_stack_) { + for (auto& x : ChainableStack::instance_->var_dbl_stack_) { x->set_zero_adjoint(); } } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 1fc4c88eaef..26c0afc3e60 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -24,19 +24,10 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); - const auto stack_size = ChainableStack::instance_->var_stack_.size(); + const auto stack_size = ChainableStack::instance_->var_dbl_stack_.size(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); - } - - size_t start2 - = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); - const auto nochain_stack_size - = ChainableStack::instance_->var_nochain_stack_.size(); - for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < nochain_stack_size; - ++i) { - ChainableStack::instance_->var_nochain_stack_[i]->set_zero_adjoint(); + ChainableStack::instance_->var_dbl_stack_[i]->set_zero_adjoint(); } } diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 81066ffbeaf..7cae2c77f06 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -17,7 +17,6 @@ class var_value; class vari_base { public: virtual void chain() {} - virtual void set_zero_adjoint() noexcept {} }; /** * The variable implementation base class. @@ -134,7 +133,7 @@ class vari_value::value>> : public * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() noexcept final { adj_ = 0.0; } + void set_zero_adjoint() noexcept { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and From 85a44b78090974e164369fd2159a83fd661edc82 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 18:04:37 -0400 Subject: [PATCH 079/167] add var_dbl_stack to vari --- stan/math/rev/core/vari.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 7cae2c77f06..b1be4f555b7 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -73,6 +73,7 @@ class vari_value::value>> : public template ::value>* = nullptr> vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT + ChainableStack::instance_->var_dbl_stack_.emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -96,6 +97,7 @@ class vari_value::value>> : public template ::value>* = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { + ChainableStack::instance_->var_dbl_stack_.emplace_back(this); if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); } else { @@ -109,6 +111,7 @@ class vari_value::value>> : public * @param x A vari_value */ vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { + ChainableStack::instance_->var_dbl_stack_.emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -117,6 +120,7 @@ class vari_value::value>> : public * @param x A vari_value */ vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { + ChainableStack::instance_->var_dbl_stack_.emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } From ac6e99a4f43d3d9c7dc551e687f208f1228f2cc5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 18:05:23 -0400 Subject: [PATCH 080/167] add var_dbl_stack to vari --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index b1be4f555b7..f740247d0b3 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -137,7 +137,7 @@ class vari_value::value>> : public * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() noexcept { adj_ = 0.0; } + inline void set_zero_adjoint() noexcept { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and From f9af05505bc70f3cde1b936b39a535f0fcd1af9e Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 18:28:03 -0400 Subject: [PATCH 081/167] add var_dbl_stack to vari --- stan/math/rev/core/vari.hpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index f740247d0b3..f797346e033 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -100,9 +100,7 @@ class vari_value::value>> : public ChainableStack::instance_->var_dbl_stack_.emplace_back(this); if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); - } else { - ChainableStack::instance_->var_nochain_stack_.emplace_back(this); - } + } } /** From 82c03323d262b87c6d2283164f9b5c13056d19b3 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 16 Jun 2020 21:51:17 -0400 Subject: [PATCH 082/167] remove nochain stack and use dbl stack --- stan/math/rev/core/autodiffstackstorage.hpp | 3 +-- stan/math/rev/core/recover_memory.hpp | 2 +- stan/math/rev/core/recover_memory_nested.hpp | 6 +++--- stan/math/rev/core/start_nested.hpp | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 847aab7d1ac..4734e09cdb7 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -105,14 +105,13 @@ struct AutodiffStackSingleton { AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; std::vector var_stack_; - std::vector var_nochain_stack_; std::vector*> var_dbl_stack_; std::vector var_alloc_stack_; stack_alloc memalloc_; // nested positions std::vector nested_var_stack_sizes_; - std::vector nested_var_nochain_stack_sizes_; + std::vector nested_var_dbl_stack_sizes_; std::vector nested_var_alloc_stack_starts_; }; diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index c8ec7e92b66..116d9befcc2 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -22,7 +22,7 @@ static inline void recover_memory() { " before calling recover_memory()"); } ChainableStack::instance_->var_stack_.clear(); - ChainableStack::instance_->var_nochain_stack_.clear(); + ChainableStack::instance_->var_dbl_stack_.clear(); for (auto &x : ChainableStack::instance_->var_alloc_stack_) { delete x; } diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 1cd493a1a11..0e34aaee8ed 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -31,9 +31,9 @@ static inline void recover_memory_nested() { ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); - ChainableStack::instance_->var_nochain_stack_.resize( - ChainableStack::instance_->nested_var_nochain_stack_sizes_.back()); - ChainableStack::instance_->nested_var_nochain_stack_sizes_.pop_back(); + ChainableStack::instance_->var_dbl_stack_.resize( + ChainableStack::instance_->nested_var_dbl_stack_sizes_.back()); + ChainableStack::instance_->nested_var_dbl_stack_sizes_.pop_back(); for (size_t i = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index d17ff7e153b..bb0f0a7cec8 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -16,8 +16,8 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - ChainableStack::instance_->nested_var_nochain_stack_sizes_.push_back( - ChainableStack::instance_->var_nochain_stack_.size()); + ChainableStack::instance_->nested_var_dbl_stack_sizes_.push_back( + ChainableStack::instance_->var_dbl_stack_.size()); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From 346e6cab07b46f0d560be437182f06ab04bfdac7 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 01:13:33 -0400 Subject: [PATCH 083/167] add tuple of stacks to zero out --- make/compiler_flags | 2 +- stan/math/prim/functor.hpp | 1 + stan/math/prim/functor/for_each_tuple.hpp | 37 +++++++++++++++++++ stan/math/rev/core/autodiffstackstorage.hpp | 14 +++---- stan/math/rev/core/chainablestack.hpp | 8 +++- stan/math/rev/core/recover_memory.hpp | 6 ++- stan/math/rev/core/recover_memory_nested.hpp | 12 +++--- stan/math/rev/core/set_zero_all_adjoints.hpp | 10 +++-- .../rev/core/set_zero_all_adjoints_nested.hpp | 14 +++++-- stan/math/rev/core/start_nested.hpp | 7 +++- stan/math/rev/core/vari.hpp | 11 +++--- 11 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 stan/math/prim/functor/for_each_tuple.hpp diff --git a/make/compiler_flags b/make/compiler_flags index 20a490c7fda..0649b6bea2a 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -256,7 +256,7 @@ ifdef STAN_MPI CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor endif -CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) +CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) -g -O0 CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) diff --git a/stan/math/prim/functor.hpp b/stan/math/prim/functor.hpp index f33209dcac3..a11d7dacbb4 100644 --- a/stan/math/prim/functor.hpp +++ b/stan/math/prim/functor.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/prim/functor/for_each_tuple.hpp b/stan/math/prim/functor/for_each_tuple.hpp new file mode 100644 index 00000000000..e6884e5083e --- /dev/null +++ b/stan/math/prim/functor/for_each_tuple.hpp @@ -0,0 +1,37 @@ +#ifndef STAN_MATH_PRIM_FUNCTOR_FOR_EACH_TUPLE_HPP +#define STAN_MATH_PRIM_FUNCTOR_FOR_EACH_TUPLE_HPP + +#include +#include +#include +#include + +namespace stan { +namespace math { +namespace internal { +template +inline auto for_each(F&& f, T&& t, std::index_sequence) { + using tuple_ret = std::tuple(std::declval())))>...>; + return tuple_ret{ std::forward(f)(std::get(t))... }; +} +template +inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { + using tuple_ret = std::tuple(std::declval())), + decltype(std::get(std::declval())))>...>; + return tuple_ret{ std::forward(f)(std::get(t1), std::get(t2))... }; +} +} +template +inline auto for_each_tuple(F&& f, T&& t) { + return internal::for_each(std::forward(f), std::forward(t), + std::make_index_sequence>::value>()); +} +template +inline auto for_each_tuple(F&& f, T1&& t1, T2&& t2) { + return internal::for_each(std::forward(f), std::forward(t1), std::forward(t2), + std::make_index_sequence>::value>()); +} + +} +} +#endif diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 4734e09cdb7..79dc4c52f57 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -88,10 +88,10 @@ class vari_value; * [3] * http://discourse.mc-stan.org/t/potentially-dropping-support-for-older-versions-of-apples-version-of-clang/3780/ */ -template +template struct AutodiffStackSingleton { using AutodiffStackSingleton_t - = AutodiffStackSingleton; + = AutodiffStackSingleton; AutodiffStackSingleton() : own_instance_(init()) {} ~AutodiffStackSingleton() { @@ -105,13 +105,13 @@ struct AutodiffStackSingleton { AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; std::vector var_stack_; - std::vector*> var_dbl_stack_; + ZeroingT var_zeroing_stacks_; std::vector var_alloc_stack_; stack_alloc memalloc_; // nested positions std::vector nested_var_stack_sizes_; - std::vector nested_var_dbl_stack_sizes_; + NumZeroingT nested_var_zeroing_stack_sizes_; std::vector nested_var_alloc_stack_starts_; }; @@ -139,11 +139,11 @@ struct AutodiffStackSingleton { bool own_instance_; }; -template +template STAN_THREADS_DEF typename AutodiffStackSingleton::AutodiffStackStorage - *AutodiffStackSingleton::instance_; + ChainableAllocT, ZeroingT, NumZeroingT>::AutodiffStackStorage + *AutodiffStackSingleton::instance_; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index 47d94983ca6..eeaf0334d3f 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_REV_CORE_CHAINABLESTACK_HPP #include -#include +#include namespace stan { namespace math { @@ -10,7 +10,11 @@ template class vari_value; class vari_base; class chainable_alloc; -using ChainableStack = AutodiffStackSingleton; +using zeroing_stacks = std::tuple*>, + std::vector*>, + std::vector*>>; +using zeroing_stacks_sizes = std::tuple, std::vector, std::vector>; +using ChainableStack = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index 116d9befcc2..e589455ba58 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_REV_CORE_RECOVER_MEMORY_HPP #define STAN_MATH_REV_CORE_RECOVER_MEMORY_HPP +#include #include #include #include @@ -22,7 +23,10 @@ static inline void recover_memory() { " before calling recover_memory()"); } ChainableStack::instance_->var_stack_.clear(); - ChainableStack::instance_->var_dbl_stack_.clear(); + for_each_tuple([](auto& x) { + x.clear(); + return 0; + }, ChainableStack::instance_->var_zeroing_stacks_); for (auto &x : ChainableStack::instance_->var_alloc_stack_) { delete x; } diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 0e34aaee8ed..c9ba7a2bf3e 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_REV_CORE_RECOVER_MEMORY_NESTED_HPP #define STAN_MATH_REV_CORE_RECOVER_MEMORY_NESTED_HPP +#include #include #include #include @@ -30,11 +31,12 @@ static inline void recover_memory_nested() { ChainableStack::instance_->var_stack_.resize( ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); - - ChainableStack::instance_->var_dbl_stack_.resize( - ChainableStack::instance_->nested_var_dbl_stack_sizes_.back()); - ChainableStack::instance_->nested_var_dbl_stack_sizes_.pop_back(); - + for_each_tuple([](auto& x, auto& y) { + x.resize(y.back()); + y.pop_back(); + return 0; + }, ChainableStack::instance_->var_zeroing_stacks_, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_); for (size_t i = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); i < ChainableStack::instance_->var_alloc_stack_.size(); ++i) { diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index abd4f87d2e0..b68268a255d 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_REV_CORE_SET_ZERO_ALL_ADJOINTS_HPP #define STAN_MATH_REV_CORE_SET_ZERO_ALL_ADJOINTS_HPP +#include #include #include #include @@ -12,9 +13,12 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { - for (auto& x : ChainableStack::instance_->var_dbl_stack_) { - x->set_zero_adjoint(); - } + for_each_tuple([](auto& x) { + for (auto& xx : x) { + xx->set_zero_adjoint(); + } + return 0; + }, ChainableStack::instance_->var_zeroing_stacks_); } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 26c0afc3e60..8779e825cc3 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_REV_CORE_SET_ZERO_ALL_ADJOINTS_NESTED_HPP #define STAN_MATH_REV_CORE_SET_ZERO_ALL_ADJOINTS_NESTED_HPP +#include #include #include #include @@ -24,11 +25,16 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); - const auto stack_size = ChainableStack::instance_->var_dbl_stack_.size(); + for_each_tuple([&start1](auto& x, auto& x_size) { + const auto stack_size = x.size(); + if (stack_size > 0) { + for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { + x[i]->set_zero_adjoint(); + } + } + return 0; + }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); // avoid wrap with unsigned when start1 == 0 - for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - ChainableStack::instance_->var_dbl_stack_[i]->set_zero_adjoint(); - } } } // namespace math diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index bb0f0a7cec8..dc1aa6b924d 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -16,8 +16,11 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - ChainableStack::instance_->nested_var_dbl_stack_sizes_.push_back( - ChainableStack::instance_->var_dbl_stack_.size()); + for_each_tuple([](auto& x, auto& y) { + x.push_back(y.size()); + return 0; + }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index f797346e033..d2b339e7fcf 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -50,7 +50,6 @@ class vari_value::value>> : public * The value of this variable. */ const Scalar val_; - /** * The adjoint of this variable, which is the partial derivative * of this variable with respect to the root variable. @@ -73,7 +72,7 @@ class vari_value::value>> : public template ::value>* = nullptr> vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT - ChainableStack::instance_->var_dbl_stack_.emplace_back(this); + std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -97,10 +96,10 @@ class vari_value::value>> : public template ::value>* = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { - ChainableStack::instance_->var_dbl_stack_.emplace_back(this); + std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); - } + } } /** @@ -109,7 +108,7 @@ class vari_value::value>> : public * @param x A vari_value */ vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_dbl_stack_.emplace_back(this); + std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -118,7 +117,7 @@ class vari_value::value>> : public * @param x A vari_value */ vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_dbl_stack_.emplace_back(this); + std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } From 2f7d37c506c6cffc1cb733bb24a1992fd96e1663 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 02:03:00 -0400 Subject: [PATCH 084/167] debugs for nested sizes --- stan/math/prim/functor/for_each_tuple.hpp | 7 ++----- stan/math/rev/core/recover_memory.hpp | 3 +-- stan/math/rev/core/recover_memory_nested.hpp | 12 +++++++++--- stan/math/rev/core/set_zero_all_adjoints.hpp | 7 ++++--- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 1 - stan/math/rev/core/start_nested.hpp | 9 +++++++-- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/stan/math/prim/functor/for_each_tuple.hpp b/stan/math/prim/functor/for_each_tuple.hpp index e6884e5083e..f0a515cc7a2 100644 --- a/stan/math/prim/functor/for_each_tuple.hpp +++ b/stan/math/prim/functor/for_each_tuple.hpp @@ -11,14 +11,11 @@ namespace math { namespace internal { template inline auto for_each(F&& f, T&& t, std::index_sequence) { - using tuple_ret = std::tuple(std::declval())))>...>; - return tuple_ret{ std::forward(f)(std::get(t))... }; + auto l = { (std::forward(f)(std::get(t)), 0)...}; } template inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { - using tuple_ret = std::tuple(std::declval())), - decltype(std::get(std::declval())))>...>; - return tuple_ret{ std::forward(f)(std::get(t1), std::get(t2))... }; + auto l = { (std::forward(f)(std::get(t1), std::get(t2)), 0)...}; } } template diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index e589455ba58..0d5ef1c0eab 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -24,8 +24,7 @@ static inline void recover_memory() { } ChainableStack::instance_->var_stack_.clear(); for_each_tuple([](auto& x) { - x.clear(); - return 0; + x.clear(); }, ChainableStack::instance_->var_zeroing_stacks_); for (auto &x : ChainableStack::instance_->var_alloc_stack_) { delete x; diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index c9ba7a2bf3e..17caae7fddc 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -32,11 +32,17 @@ static inline void recover_memory_nested() { ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); for_each_tuple([](auto& x, auto& y) { - x.resize(y.back()); - y.pop_back(); - return 0; + if (std::is_same, vari_value*>::value) { + std::cout << "zero_stack size : " << x.size() << " nested_zero_stack size: " << y.size() << std::endl; + if(y.size() > 0) { + std::cout << "nested_zero_end : " << *(y.end()-1) << std::endl; + x.resize(y.back()); + y.pop_back(); + } + } }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); + for (size_t i = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); i < ChainableStack::instance_->var_alloc_stack_.size(); ++i) { diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index b68268a255d..c38475da99d 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -14,10 +14,11 @@ namespace math { */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { for_each_tuple([](auto& x) { - for (auto& xx : x) { - xx->set_zero_adjoint(); + if (x.size() > 0) { + for (auto& xx : x) { + xx->set_zero_adjoint(); + } } - return 0; }, ChainableStack::instance_->var_zeroing_stacks_); } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 8779e825cc3..2daee965418 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -32,7 +32,6 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { x[i]->set_zero_adjoint(); } } - return 0; }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); // avoid wrap with unsigned when start1 == 0 } diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index dc1aa6b924d..7cdb17f1d31 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,8 +17,13 @@ static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); for_each_tuple([](auto& x, auto& y) { - x.push_back(y.size()); - return 0; + if (std::is_same, vari_value*>::value) { + std::cout << "1 size : " << x.size() << " 2 size: " << y.size() << std::endl; + } + if (y.size() > 0) { + x.push_back(y.size()); + } + }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( From e3e2d7e40167f7b57646b0b00b337bbd19537e33 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 02:43:57 -0400 Subject: [PATCH 085/167] multi-stack nested --- make/compiler_flags | 2 +- stan/math/prim/functor/for_each_tuple.hpp | 8 ++++---- stan/math/rev/core/recover_memory_nested.hpp | 8 ++------ stan/math/rev/core/set_zero_all_adjoints.hpp | 2 -- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 2 -- stan/math/rev/core/start_nested.hpp | 6 ------ 6 files changed, 7 insertions(+), 21 deletions(-) diff --git a/make/compiler_flags b/make/compiler_flags index 0649b6bea2a..20a490c7fda 100644 --- a/make/compiler_flags +++ b/make/compiler_flags @@ -256,7 +256,7 @@ ifdef STAN_MPI CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor endif -CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) -g -O0 +CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) -O$(O) $(INC) CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB) diff --git a/stan/math/prim/functor/for_each_tuple.hpp b/stan/math/prim/functor/for_each_tuple.hpp index f0a515cc7a2..8245d69a1b4 100644 --- a/stan/math/prim/functor/for_each_tuple.hpp +++ b/stan/math/prim/functor/for_each_tuple.hpp @@ -10,21 +10,21 @@ namespace stan { namespace math { namespace internal { template -inline auto for_each(F&& f, T&& t, std::index_sequence) { +static inline auto for_each(F&& f, T&& t, std::index_sequence) { auto l = { (std::forward(f)(std::get(t)), 0)...}; } template -inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { +static inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { auto l = { (std::forward(f)(std::get(t1), std::get(t2)), 0)...}; } } template -inline auto for_each_tuple(F&& f, T&& t) { +static inline auto for_each_tuple(F&& f, T&& t) { return internal::for_each(std::forward(f), std::forward(t), std::make_index_sequence>::value>()); } template -inline auto for_each_tuple(F&& f, T1&& t1, T2&& t2) { +static inline auto for_each_tuple(F&& f, T1&& t1, T2&& t2) { return internal::for_each(std::forward(f), std::forward(t1), std::forward(t2), std::make_index_sequence>::value>()); } diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 17caae7fddc..9d7ff79125a 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -32,14 +32,10 @@ static inline void recover_memory_nested() { ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); for_each_tuple([](auto& x, auto& y) { - if (std::is_same, vari_value*>::value) { - std::cout << "zero_stack size : " << x.size() << " nested_zero_stack size: " << y.size() << std::endl; - if(y.size() > 0) { - std::cout << "nested_zero_end : " << *(y.end()-1) << std::endl; + if (y.size() > 0) { x.resize(y.back()); - y.pop_back(); + y.pop_back(); } - } }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index c38475da99d..2049c95192a 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -14,11 +14,9 @@ namespace math { */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { for_each_tuple([](auto& x) { - if (x.size() > 0) { for (auto& xx : x) { xx->set_zero_adjoint(); } - } }, ChainableStack::instance_->var_zeroing_stacks_); } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 2daee965418..28e5f1200e1 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -27,11 +27,9 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); for_each_tuple([&start1](auto& x, auto& x_size) { const auto stack_size = x.size(); - if (stack_size > 0) { for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { x[i]->set_zero_adjoint(); } - } }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); // avoid wrap with unsigned when start1 == 0 } diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 7cdb17f1d31..697bb5405f0 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,13 +17,7 @@ static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); for_each_tuple([](auto& x, auto& y) { - if (std::is_same, vari_value*>::value) { - std::cout << "1 size : " << x.size() << " 2 size: " << y.size() << std::endl; - } - if (y.size() > 0) { x.push_back(y.size()); - } - }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( From e742ec0e236a626ab1b98edf4c5532d63336f195 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 11:49:59 -0400 Subject: [PATCH 086/167] add size check on start_nested --- stan/math/rev/core/start_nested.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 697bb5405f0..3caf86313da 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,7 +17,9 @@ static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); for_each_tuple([](auto& x, auto& y) { - x.push_back(y.size()); + if (y.size() > 0) { + x.push_back(y.size()); + } }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( From 5f4278fa4bccd78ff3ba3aeb7ef47342c7e006ec Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 11:51:19 -0400 Subject: [PATCH 087/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/functor/for_each_tuple.hpp | 31 ++++++++++--------- stan/math/rev/core/autodiffstackstorage.hpp | 18 ++++++----- stan/math/rev/core/chainablestack.hpp | 11 ++++--- stan/math/rev/core/recover_memory.hpp | 7 ++--- stan/math/rev/core/recover_memory_nested.hpp | 16 +++++----- stan/math/rev/core/set_zero_all_adjoints.hpp | 12 ++++--- .../rev/core/set_zero_all_adjoints_nested.hpp | 16 ++++++---- stan/math/rev/core/start_nested.hpp | 14 +++++---- stan/math/rev/core/vari.hpp | 21 +++++++++---- 9 files changed, 87 insertions(+), 59 deletions(-) diff --git a/stan/math/prim/functor/for_each_tuple.hpp b/stan/math/prim/functor/for_each_tuple.hpp index 8245d69a1b4..c520e02df91 100644 --- a/stan/math/prim/functor/for_each_tuple.hpp +++ b/stan/math/prim/functor/for_each_tuple.hpp @@ -9,26 +9,29 @@ namespace stan { namespace math { namespace internal { -template +template static inline auto for_each(F&& f, T&& t, std::index_sequence) { - auto l = { (std::forward(f)(std::get(t)), 0)...}; + auto l = {(std::forward(f)(std::get(t)), 0)...}; } -template -static inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { - auto l = { (std::forward(f)(std::get(t1), std::get(t2)), 0)...}; +template +static inline auto for_each(F&& f, T1&& t1, T2&& t2, + std::index_sequence) { + auto l = {(std::forward(f)(std::get(t1), std::get(t2)), 0)...}; } -} -template +} // namespace internal +template static inline auto for_each_tuple(F&& f, T&& t) { - return internal::for_each(std::forward(f), std::forward(t), - std::make_index_sequence>::value>()); + return internal::for_each( + std::forward(f), std::forward(t), + std::make_index_sequence>::value>()); } -template +template static inline auto for_each_tuple(F&& f, T1&& t1, T2&& t2) { - return internal::for_each(std::forward(f), std::forward(t1), std::forward(t2), - std::make_index_sequence>::value>()); + return internal::for_each( + std::forward(f), std::forward(t1), std::forward(t2), + std::make_index_sequence>::value>()); } -} -} +} // namespace math +} // namespace stan #endif diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 79dc4c52f57..e66ac4bf63b 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -88,10 +88,12 @@ class vari_value; * [3] * http://discourse.mc-stan.org/t/potentially-dropping-support-for-older-versions-of-apples-version-of-clang/3780/ */ -template +template struct AutodiffStackSingleton { using AutodiffStackSingleton_t - = AutodiffStackSingleton; + = AutodiffStackSingleton; AutodiffStackSingleton() : own_instance_(init()) {} ~AutodiffStackSingleton() { @@ -104,7 +106,7 @@ struct AutodiffStackSingleton { struct AutodiffStackStorage { AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; - std::vector var_stack_; + std::vector var_stack_; ZeroingT var_zeroing_stacks_; std::vector var_alloc_stack_; stack_alloc memalloc_; @@ -139,11 +141,13 @@ struct AutodiffStackSingleton { bool own_instance_; }; -template +template STAN_THREADS_DEF - typename AutodiffStackSingleton::AutodiffStackStorage - *AutodiffStackSingleton::instance_; + typename AutodiffStackSingleton::AutodiffStackStorage + *AutodiffStackSingleton::instance_; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index eeaf0334d3f..35402d4ab68 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -11,10 +11,13 @@ class vari_value; class vari_base; class chainable_alloc; using zeroing_stacks = std::tuple*>, - std::vector*>, - std::vector*>>; -using zeroing_stacks_sizes = std::tuple, std::vector, std::vector>; -using ChainableStack = AutodiffStackSingleton; + std::vector*>, + std::vector*>>; +using zeroing_stacks_sizes + = std::tuple, std::vector, std::vector>; +using ChainableStack + = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index 0d5ef1c0eab..fe96f0133bb 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -23,10 +23,9 @@ static inline void recover_memory() { " before calling recover_memory()"); } ChainableStack::instance_->var_stack_.clear(); - for_each_tuple([](auto& x) { - x.clear(); - }, ChainableStack::instance_->var_zeroing_stacks_); - for (auto &x : ChainableStack::instance_->var_alloc_stack_) { + for_each_tuple([](auto& x) { x.clear(); }, + ChainableStack::instance_->var_zeroing_stacks_); + for (auto& x : ChainableStack::instance_->var_alloc_stack_) { delete x; } ChainableStack::instance_->var_alloc_stack_.clear(); diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 9d7ff79125a..6bb26555729 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -31,13 +31,15 @@ static inline void recover_memory_nested() { ChainableStack::instance_->var_stack_.resize( ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); - for_each_tuple([](auto& x, auto& y) { - if (y.size() > 0) { - x.resize(y.back()); - y.pop_back(); - } - }, ChainableStack::instance_->var_zeroing_stacks_, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_); + for_each_tuple( + [](auto& x, auto& y) { + if (y.size() > 0) { + x.resize(y.back()); + y.pop_back(); + } + }, + ChainableStack::instance_->var_zeroing_stacks_, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_); for (size_t i = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 2049c95192a..8fd9863c6fc 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,11 +13,13 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { - for_each_tuple([](auto& x) { - for (auto& xx : x) { - xx->set_zero_adjoint(); - } - }, ChainableStack::instance_->var_zeroing_stacks_); + for_each_tuple( + [](auto& x) { + for (auto& xx : x) { + xx->set_zero_adjoint(); + } + }, + ChainableStack::instance_->var_zeroing_stacks_); } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 28e5f1200e1..f710119994e 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -25,12 +25,16 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); - for_each_tuple([&start1](auto& x, auto& x_size) { - const auto stack_size = x.size(); - for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - x[i]->set_zero_adjoint(); - } - }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); + for_each_tuple( + [&start1](auto& x, auto& x_size) { + const auto stack_size = x.size(); + for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; + ++i) { + x[i]->set_zero_adjoint(); + } + }, + ChainableStack::instance_->var_zeroing_stacks_, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_); // avoid wrap with unsigned when start1 == 0 } diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 3caf86313da..d9ac28787de 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -16,12 +16,14 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each_tuple([](auto& x, auto& y) { - if (y.size() > 0) { - x.push_back(y.size()); - } - }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each_tuple( + [](auto& x, auto& y) { + if (y.size() > 0) { + x.push_back(y.size()); + } + }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index d2b339e7fcf..8858a2527e8 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -15,7 +15,7 @@ template class var_value; class vari_base { -public: + public: virtual void chain() {} }; /** @@ -38,7 +38,8 @@ template class vari_value; template -class vari_value::value>> : public vari_base { +class vari_value::value>> + : public vari_base { private: template friend class var_value; @@ -72,7 +73,9 @@ class vari_value::value>> : public template ::value>* = nullptr> vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT - std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); + std::get*>>( + ChainableStack::instance_->var_zeroing_stacks_) + .emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -96,7 +99,9 @@ class vari_value::value>> : public template ::value>* = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { - std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); + std::get*>>( + ChainableStack::instance_->var_zeroing_stacks_) + .emplace_back(this); if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -108,7 +113,9 @@ class vari_value::value>> : public * @param x A vari_value */ vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { - std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); + std::get*>>( + ChainableStack::instance_->var_zeroing_stacks_) + .emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } /** @@ -117,7 +124,9 @@ class vari_value::value>> : public * @param x A vari_value */ vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { - std::get*>>(ChainableStack::instance_->var_zeroing_stacks_).emplace_back(this); + std::get*>>( + ChainableStack::instance_->var_zeroing_stacks_) + .emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } From 9a8ba4c5169276aadebd9fe170d3a4fcbc2147c5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 13:31:15 -0400 Subject: [PATCH 088/167] add docs to for_each --- stan/math/prim/functor.hpp | 2 +- stan/math/prim/functor/for_each.hpp | 50 +++++++++++++++++++ stan/math/prim/functor/for_each_tuple.hpp | 34 ------------- stan/math/rev/core/autodiffstackstorage.hpp | 13 +++-- stan/math/rev/core/chainablestack.hpp | 3 +- stan/math/rev/core/recover_memory.hpp | 2 +- stan/math/rev/core/recover_memory_nested.hpp | 4 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 2 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 2 +- stan/math/rev/core/start_nested.hpp | 5 +- 10 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 stan/math/prim/functor/for_each.hpp delete mode 100644 stan/math/prim/functor/for_each_tuple.hpp diff --git a/stan/math/prim/functor.hpp b/stan/math/prim/functor.hpp index a11d7dacbb4..03f49008c17 100644 --- a/stan/math/prim/functor.hpp +++ b/stan/math/prim/functor.hpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/stan/math/prim/functor/for_each.hpp b/stan/math/prim/functor/for_each.hpp new file mode 100644 index 00000000000..7e3c36ab315 --- /dev/null +++ b/stan/math/prim/functor/for_each.hpp @@ -0,0 +1,50 @@ +#ifndef STAN_MATH_PRIM_FUNCTOR_for_each_HPP +#define STAN_MATH_PRIM_FUNCTOR_for_each_HPP + +#include +#include +#include +#include + +namespace stan { +namespace math { +namespace internal { +template +constexpr inline auto for_each(F&& f, T&& t, std::index_sequence) { + std::initializer_list{( + static_cast(std::forward(f)(std::get(std::forward(t)))), 0)...}; +} +template +constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { + std::initializer_list{(static_cast(std::forward(f)(std::get(std::forward(t1)), std::get(std::forward(t2)))), 0)...}; +} +} +/** + * Apply a function to each element of a tuple + * @tparam F type with a valid `operator()` + * @tparam T tuple + * @param f A functor to apply over each element of the tuple. + * @param t A tuple. + */ +template +constexpr inline auto for_each(F&& f, T&& t) { + return internal::for_each(std::forward(f), std::forward(t), + std::make_index_sequence>::value>()); +} +/** + * Apply a function to each element of a tuple and other container + * @tparam F type with a valid `operator()` + * @tparam T1 tuple + * @tparam Any container with an `operator[]` + * @param f A functor to apply over each element of the tuple. + * @param t A tuple. + */ +template +constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2) { + return internal::for_each(std::forward(f), std::forward(t1), std::forward(t2), + std::make_index_sequence>::value>()); +} + +} +} +#endif diff --git a/stan/math/prim/functor/for_each_tuple.hpp b/stan/math/prim/functor/for_each_tuple.hpp deleted file mode 100644 index 8245d69a1b4..00000000000 --- a/stan/math/prim/functor/for_each_tuple.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef STAN_MATH_PRIM_FUNCTOR_FOR_EACH_TUPLE_HPP -#define STAN_MATH_PRIM_FUNCTOR_FOR_EACH_TUPLE_HPP - -#include -#include -#include -#include - -namespace stan { -namespace math { -namespace internal { -template -static inline auto for_each(F&& f, T&& t, std::index_sequence) { - auto l = { (std::forward(f)(std::get(t)), 0)...}; -} -template -static inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { - auto l = { (std::forward(f)(std::get(t1), std::get(t2)), 0)...}; -} -} -template -static inline auto for_each_tuple(F&& f, T&& t) { - return internal::for_each(std::forward(f), std::forward(t), - std::make_index_sequence>::value>()); -} -template -static inline auto for_each_tuple(F&& f, T1&& t1, T2&& t2) { - return internal::for_each(std::forward(f), std::forward(t1), std::forward(t2), - std::make_index_sequence>::value>()); -} - -} -} -#endif diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 79dc4c52f57..0bdfccd4ba5 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -88,10 +88,10 @@ class vari_value; * [3] * http://discourse.mc-stan.org/t/potentially-dropping-support-for-older-versions-of-apples-version-of-clang/3780/ */ -template +template struct AutodiffStackSingleton { using AutodiffStackSingleton_t - = AutodiffStackSingleton; + = AutodiffStackSingleton; AutodiffStackSingleton() : own_instance_(init()) {} ~AutodiffStackSingleton() { @@ -111,7 +111,7 @@ struct AutodiffStackSingleton { // nested positions std::vector nested_var_stack_sizes_; - NumZeroingT nested_var_zeroing_stack_sizes_; + std::array, std::tuple_size::value> nested_var_zeroing_stack_sizes_; std::vector nested_var_alloc_stack_starts_; }; @@ -139,11 +139,10 @@ struct AutodiffStackSingleton { bool own_instance_; }; -template +template STAN_THREADS_DEF - typename AutodiffStackSingleton::AutodiffStackStorage - *AutodiffStackSingleton::instance_; + typename AutodiffStackSingleton::AutodiffStackStorage + *AutodiffStackSingleton::instance_; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index eeaf0334d3f..ac3d14246b5 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -13,8 +13,7 @@ class chainable_alloc; using zeroing_stacks = std::tuple*>, std::vector*>, std::vector*>>; -using zeroing_stacks_sizes = std::tuple, std::vector, std::vector>; -using ChainableStack = AutodiffStackSingleton; +using ChainableStack = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index 0d5ef1c0eab..b27206e6584 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -23,7 +23,7 @@ static inline void recover_memory() { " before calling recover_memory()"); } ChainableStack::instance_->var_stack_.clear(); - for_each_tuple([](auto& x) { + for_each([](auto& x) { x.clear(); }, ChainableStack::instance_->var_zeroing_stacks_); for (auto &x : ChainableStack::instance_->var_alloc_stack_) { diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 9d7ff79125a..9d67972dc06 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -31,10 +31,10 @@ static inline void recover_memory_nested() { ChainableStack::instance_->var_stack_.resize( ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); - for_each_tuple([](auto& x, auto& y) { + for_each([](auto& x, auto& y) { if (y.size() > 0) { x.resize(y.back()); - y.pop_back(); + y.pop_back(); } }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 2049c95192a..da4c4884418 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,7 +13,7 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { - for_each_tuple([](auto& x) { + for_each([](auto& x) { for (auto& xx : x) { xx->set_zero_adjoint(); } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 28e5f1200e1..7ced3afc76a 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -25,7 +25,7 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); - for_each_tuple([&start1](auto& x, auto& x_size) { + for_each([&start1](auto& x, auto& x_size) { const auto stack_size = x.size(); for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { x[i]->set_zero_adjoint(); diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 3caf86313da..bae6387540e 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_REV_CORE_START_NESTED_HPP #define STAN_MATH_REV_CORE_START_NESTED_HPP +#include #include namespace stan { @@ -16,9 +17,9 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each_tuple([](auto& x, auto& y) { + for_each([](auto& x, auto& y) { if (y.size() > 0) { - x.push_back(y.size()); + x.push_back(y.size()); } }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); From 5c6faab9aedd5bfb5cff94dd4fbedb0033c5100b Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 13:45:54 -0400 Subject: [PATCH 089/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/functor/for_each.hpp | 37 +++++++++++-------- stan/math/rev/core/autodiffstackstorage.hpp | 9 +++-- stan/math/rev/core/chainablestack.hpp | 7 ++-- stan/math/rev/core/recover_memory.hpp | 7 ++-- stan/math/rev/core/recover_memory_nested.hpp | 16 ++++---- stan/math/rev/core/set_zero_all_adjoints.hpp | 13 ++++--- .../rev/core/set_zero_all_adjoints_nested.hpp | 16 +++++--- stan/math/rev/core/start_nested.hpp | 14 ++++--- 8 files changed, 69 insertions(+), 50 deletions(-) diff --git a/stan/math/prim/functor/for_each.hpp b/stan/math/prim/functor/for_each.hpp index 7e3c36ab315..216117828e5 100644 --- a/stan/math/prim/functor/for_each.hpp +++ b/stan/math/prim/functor/for_each.hpp @@ -9,16 +9,21 @@ namespace stan { namespace math { namespace internal { -template +template constexpr inline auto for_each(F&& f, T&& t, std::index_sequence) { - std::initializer_list{( - static_cast(std::forward(f)(std::get(std::forward(t)))), 0)...}; -} -template -constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { - std::initializer_list{(static_cast(std::forward(f)(std::get(std::forward(t1)), std::get(std::forward(t2)))), 0)...}; + std::initializer_list{ + (static_cast(std::forward(f)(std::get(std::forward(t)))), + 0)...}; } +template +constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, + std::index_sequence) { + std::initializer_list{( + static_cast(std::forward(f)(std::get(std::forward(t1)), + std::get(std::forward(t2)))), + 0)...}; } +} // namespace internal /** * Apply a function to each element of a tuple * @tparam F type with a valid `operator()` @@ -26,10 +31,11 @@ constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence +template constexpr inline auto for_each(F&& f, T&& t) { - return internal::for_each(std::forward(f), std::forward(t), - std::make_index_sequence>::value>()); + return internal::for_each( + std::forward(f), std::forward(t), + std::make_index_sequence>::value>()); } /** * Apply a function to each element of a tuple and other container @@ -39,12 +45,13 @@ constexpr inline auto for_each(F&& f, T&& t) { * @param f A functor to apply over each element of the tuple. * @param t A tuple. */ -template +template constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2) { - return internal::for_each(std::forward(f), std::forward(t1), std::forward(t2), - std::make_index_sequence>::value>()); + return internal::for_each( + std::forward(f), std::forward(t1), std::forward(t2), + std::make_index_sequence>::value>()); } -} -} +} // namespace math +} // namespace stan #endif diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 017ccfb4a45..42c74ea92dc 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -111,7 +111,8 @@ struct AutodiffStackSingleton { // nested positions std::vector nested_var_stack_sizes_; - std::array, std::tuple_size::value> nested_var_zeroing_stack_sizes_; + std::array, std::tuple_size::value> + nested_var_zeroing_stack_sizes_; std::vector nested_var_alloc_stack_starts_; }; @@ -140,9 +141,9 @@ struct AutodiffStackSingleton { }; template -STAN_THREADS_DEF - typename AutodiffStackSingleton::AutodiffStackStorage - *AutodiffStackSingleton::instance_; +STAN_THREADS_DEF typename AutodiffStackSingleton::AutodiffStackStorage + *AutodiffStackSingleton::instance_; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index ac3d14246b5..21f0101c23a 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -11,9 +11,10 @@ class vari_value; class vari_base; class chainable_alloc; using zeroing_stacks = std::tuple*>, - std::vector*>, - std::vector*>>; -using ChainableStack = AutodiffStackSingleton; + std::vector*>, + std::vector*>>; +using ChainableStack + = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index b27206e6584..0ceec6d5f35 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -23,10 +23,9 @@ static inline void recover_memory() { " before calling recover_memory()"); } ChainableStack::instance_->var_stack_.clear(); - for_each([](auto& x) { - x.clear(); - }, ChainableStack::instance_->var_zeroing_stacks_); - for (auto &x : ChainableStack::instance_->var_alloc_stack_) { + for_each([](auto& x) { x.clear(); }, + ChainableStack::instance_->var_zeroing_stacks_); + for (auto& x : ChainableStack::instance_->var_alloc_stack_) { delete x; } ChainableStack::instance_->var_alloc_stack_.clear(); diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 9d67972dc06..ba3f33d5b0e 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -31,13 +31,15 @@ static inline void recover_memory_nested() { ChainableStack::instance_->var_stack_.resize( ChainableStack::instance_->nested_var_stack_sizes_.back()); ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); - for_each([](auto& x, auto& y) { - if (y.size() > 0) { - x.resize(y.back()); - y.pop_back(); - } - }, ChainableStack::instance_->var_zeroing_stacks_, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_); + for_each( + [](auto& x, auto& y) { + if (y.size() > 0) { + x.resize(y.back()); + y.pop_back(); + } + }, + ChainableStack::instance_->var_zeroing_stacks_, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_); for (size_t i = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index e5eb54c86b7..41e9f411947 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,11 +13,14 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { - for_each([](auto& x) { - for (auto& xx : x) { - xx->set_zero_adjoint(); - } - }, ChainableStack::instance_->var_zeroing_stacks_);} + for_each( + [](auto& x) { + for (auto& xx : x) { + xx->set_zero_adjoint(); + } + }, + ChainableStack::instance_->var_zeroing_stacks_); +} } // namespace math } // namespace stan diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 7ced3afc76a..dbe1ce44b1f 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -25,12 +25,16 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); - for_each([&start1](auto& x, auto& x_size) { - const auto stack_size = x.size(); - for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; ++i) { - x[i]->set_zero_adjoint(); - } - }, ChainableStack::instance_->var_zeroing_stacks_, ChainableStack::instance_->nested_var_zeroing_stack_sizes_); + for_each( + [&start1](auto& x, auto& x_size) { + const auto stack_size = x.size(); + for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; + ++i) { + x[i]->set_zero_adjoint(); + } + }, + ChainableStack::instance_->var_zeroing_stacks_, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_); // avoid wrap with unsigned when start1 == 0 } diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index bae6387540e..a667f695136 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,12 +17,14 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each([](auto& x, auto& y) { - if (y.size() > 0) { - x.push_back(y.size()); - } - }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each( + [](auto& x, auto& y) { + if (y.size() > 0) { + x.push_back(y.size()); + } + }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From 388b075918991a26e97676b3efbf60ae10d09046 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 13:53:50 -0400 Subject: [PATCH 090/167] update docs --- stan/math/opencl/kernel_generator/type_str.hpp | 1 - stan/math/prim/functor/for_each.hpp | 3 ++- stan/math/rev/core/vari.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stan/math/opencl/kernel_generator/type_str.hpp b/stan/math/opencl/kernel_generator/type_str.hpp index 465a5ba0258..43f4aba0333 100644 --- a/stan/math/opencl/kernel_generator/type_str.hpp +++ b/stan/math/opencl/kernel_generator/type_str.hpp @@ -26,7 +26,6 @@ ADD_TYPE_TO_TYPE_STR(int) ADD_TYPE_TO_TYPE_STR(char) ADD_TYPE_TO_TYPE_STR(bool) #undef ADD_TYPE_TO_TYPE_STR -/** @}*/ } // namespace math } // namespace stan #endif diff --git a/stan/math/prim/functor/for_each.hpp b/stan/math/prim/functor/for_each.hpp index 7e3c36ab315..230fce1df01 100644 --- a/stan/math/prim/functor/for_each.hpp +++ b/stan/math/prim/functor/for_each.hpp @@ -37,7 +37,8 @@ constexpr inline auto for_each(F&& f, T&& t) { * @tparam T1 tuple * @tparam Any container with an `operator[]` * @param f A functor to apply over each element of the tuple. - * @param t A tuple. + * @param t1 A tuple. + * @param t2 Any container with `operator[]`. */ template constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2) { diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 8858a2527e8..f33277780f0 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -16,7 +16,7 @@ class var_value; class vari_base { public: - virtual void chain() {} + virtual void chain() = 0 }; /** * The variable implementation base class. From caa55f489bc811b2d943b7e0edefdce936484763 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 15:05:47 -0400 Subject: [PATCH 091/167] Use trick from boost::hana on for_each to avoid unused-value warning --- stan/math/prim/functor/for_each.hpp | 21 +++++++++++++++++---- stan/math/rev/core/vari.hpp | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/stan/math/prim/functor/for_each.hpp b/stan/math/prim/functor/for_each.hpp index 3eda091ec87..7ee1d5e55a5 100644 --- a/stan/math/prim/functor/for_each.hpp +++ b/stan/math/prim/functor/for_each.hpp @@ -9,19 +9,32 @@ namespace stan { namespace math { namespace internal { +/** + * Implimentation of for_each. + * @note The static cast to void is used in boost::hana's for_each impl + * and is used to suppress unused value warnings from the compiler. + */ template constexpr inline auto for_each(F&& f, T&& t, std::index_sequence) { - std::initializer_list{ + using Swallow = int[]; + static_cast(Swallow{ (static_cast(std::forward(f)(std::get(std::forward(t)))), - 0)...}; + 0)...}); } + +/** + * Implimentation of Binary for_each. + * @note The static cast to void is used in boost::hana's for_each impl + * and is used to suppress unused value warnings from the compiler. + */ template constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, std::index_sequence) { - std::initializer_list{( + using Swallow = int[]; + static_cast(Swallow{( static_cast(std::forward(f)(std::get(std::forward(t1)), std::get(std::forward(t2)))), - 0)...}; + 0)...}); } } // namespace internal /** diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index f33277780f0..7cbca31440d 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -16,7 +16,7 @@ class var_value; class vari_base { public: - virtual void chain() = 0 + virtual void chain() {}; }; /** * The variable implementation base class. From 2d3648bd31fff2afaa59a73d5b4f74123f85ffb6 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 15:06:38 -0400 Subject: [PATCH 092/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 7cbca31440d..b5928ec35e7 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -16,7 +16,7 @@ class var_value; class vari_base { public: - virtual void chain() {}; + virtual void chain(){}; }; /** * The variable implementation base class. From 52ddce42e87c8bdbdad6acd7f1360c30bb12b4d5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 15:12:08 -0400 Subject: [PATCH 093/167] cpplint --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 7cbca31440d..8858a2527e8 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -16,7 +16,7 @@ class var_value; class vari_base { public: - virtual void chain() {}; + virtual void chain() {} }; /** * The variable implementation base class. From b952d58c87d356a17ed154e48a58e9ff90861a53 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 19:13:23 +0000 Subject: [PATCH 094/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 64d492e98d3..8858a2527e8 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -16,7 +16,7 @@ class var_value; class vari_base { public: - virtual void chain(){} + virtual void chain() {} }; /** * The variable implementation base class. From abf0a5eb80c9eac55130dae5d97315903449e242 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 15:59:23 -0400 Subject: [PATCH 095/167] update tests to use new zeroing vari stack instead of nochain --- stan/math/rev/core/var.hpp | 36 +++---------------- stan/math/rev/core/vari.hpp | 8 ++++- .../rev/core/thread_stack_instance_test.cpp | 4 +-- test/unit/math/rev/err/check_bounded_test.cpp | 8 ++--- .../rev/err/check_consistent_size_test.cpp | 4 +-- .../rev/err/check_consistent_sizes_test.cpp | 4 +-- test/unit/math/rev/err/check_finite_test.cpp | 12 +++---- .../rev/err/check_greater_or_equal_test.cpp | 12 +++---- test/unit/math/rev/err/check_greater_test.cpp | 12 +++---- .../math/rev/err/check_less_or_equal_test.cpp | 14 ++++---- test/unit/math/rev/err/check_less_test.cpp | 12 +++---- .../math/rev/err/check_nonnegative_test.cpp | 20 +++++------ test/unit/math/rev/err/check_not_nan_test.cpp | 16 ++++----- .../rev/err/check_pos_semidefinite_test.cpp | 4 +-- .../rev/err/check_positive_finite_test.cpp | 10 +++--- .../unit/math/rev/err/check_positive_test.cpp | 4 +-- 16 files changed, 80 insertions(+), 100 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index c8001d28963..5688ef2833a 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -28,7 +28,7 @@ static void grad(Vari* vi); * and subtraction, as well as a range of mathematical functions * like exponentiation and powers are overridden to operate on * var values objects. - * @tparam T An Arithmetic type. + * @tparam T An Floating point type. */ template class var_value {}; @@ -36,16 +36,9 @@ class var_value {}; template class var_value> { public: - // The internal value_type is always a floating point type - using value_type = std::decay_t; - using vari_type = vari_value; + using value_type = std::decay_t; // Numeric type in vari_value + using vari_type = vari_value; // using vari_pointer = vari_type*; - template - using require_vari_convertible_t - = require_t*, vari_pointer>>; - template - using require_not_vari_convertible_t = require_t< - bool_constant*, vari_pointer>::value>>; /** * Pointer to the implementation of this variable. @@ -65,7 +58,7 @@ class var_value> { * @return true if this variable does not yet have * a defined variable. */ - bool is_uninitialized() { return (vi_ == static_cast(nullptr)); } + bool is_uninitialized() { return (vi_ == nullptr); } /** * Construct a variable for later assignment. @@ -74,7 +67,7 @@ class var_value> { * dangling. Before an assignment, the behavior is thus undefined just * as for a basic double. */ - var_value() : vi_(static_cast(nullptr)) {} + var_value() : vi_(nullptr) {} /** * Construct a variable from the specified arithmetic type argument @@ -89,22 +82,11 @@ class var_value> { /** * Construct a variable from a pointer to a variable implementation. - * @tparam S The type in the vari_value pointer that has the same `value_type` - * as this `var_value`. For integral types a `vari_value` is the - * same as `vari_value` so can be `reinterpret_cast` without a copy. * @param vi A vari_value pointer. */ var_value(vari_value* vi) // NOLINT : vi_(vi) {} - /** - * Constructor from `var_value` whose value_type is the same as this class's - * `value_type`. This is used in cases such as - * `var_value a(4.0); var_value b(a)` since the `value_type` for - * a `var_value` with an integral type is a double. - */ - var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT - /** * Return a constant reference to the value of this variable. * @@ -197,7 +179,6 @@ class var_value> { * then (a += b) behaves exactly the same way as (a = a + b), * creating an intermediate variable representing (a + b). * - * @tparam S a type that is convertible to `value_type` * @param b The variable to add to this variable. * @return The result of adding the specified variable to this variable. */ @@ -210,7 +191,6 @@ class var_value> { * (a += b) behaves exactly the same way as (a = a + b). Note * that the result is an assignable lvalue. * - * @tparam Arith An arithmetic type * @param b The scalar to add to this variable. * @return The result of adding the specified variable to this variable. */ @@ -223,7 +203,6 @@ class var_value> { * then (a -= b) behaves exactly the same way as (a = a - b). * Note that the result is an assignable lvalue. * - * @tparam S a type that is convertible to `value_type` * @param b The variable to subtract from this variable. * @return The result of subtracting the specified variable from * this variable. @@ -237,7 +216,6 @@ class var_value> { * (a -= b) behaves exactly the same way as (a = a - b). Note * that the result is an assignable lvalue. * - * @tparam Arith An arithmetic type * @param b The scalar to subtract from this variable. * @return The result of subtracting the specified variable from this * variable. @@ -251,7 +229,6 @@ class var_value> { * then (a *= b) behaves exactly the same way as (a = a * b). * Note that the result is an assignable lvalue. * - * @tparam S a type that is convertible to `value_type` * @param b The variable to multiply this variable by. * @return The result of multiplying this variable by the * specified variable. @@ -265,7 +242,6 @@ class var_value> { * (a *= b) behaves exactly the same way as (a = a * b). Note * that the result is an assignable lvalue. * - * @tparam Arith An arithmetic type * @param b The scalar to multiply this variable by. * @return The result of multiplying this variable by the specified * variable. @@ -278,7 +254,6 @@ class var_value> { * behaves exactly the same way as (a = a / b). Note that the * result is an assignable lvalue. * - * @tparam S a type that is convertible to `value_type` * @param b The variable to divide this variable by. * @return The result of dividing this variable by the * specified variable. @@ -292,7 +267,6 @@ class var_value> { * (a /= b) behaves exactly the same way as (a = a / b). Note * that the result is an assignable lvalue. * - * @tparam Arith An arithmetic type * @param b The scalar to divide this variable by. * @return The result of dividing this variable by the specified * variable. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 64d492e98d3..4015a7fbfce 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -13,9 +13,15 @@ namespace math { // forward declaration of var template class var_value; - +/** + * Abstract base class that all `vari_value` and it's derived classes inherit. + */ class vari_base { public: + /** + * Apply the chain rule to this variable based on the variables + * on which it depends. + */ virtual void chain(){} }; /** diff --git a/test/unit/math/rev/core/thread_stack_instance_test.cpp b/test/unit/math/rev/core/thread_stack_instance_test.cpp index c47d4f0d2d6..17e20682246 100644 --- a/test/unit/math/rev/core/thread_stack_instance_test.cpp +++ b/test/unit/math/rev/core/thread_stack_instance_test.cpp @@ -93,14 +93,14 @@ TEST(thread_stack_instance, child_instances) { ChainableStack thread_instance; EXPECT_TRUE( main_ad_stack->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size() + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size() #ifdef STAN_THREADS > #else == #endif ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size()); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size()); }; std::thread other_work(thread_tester); diff --git a/test/unit/math/rev/err/check_bounded_test.cpp b/test/unit/math/rev/err/check_bounded_test.cpp index 3f3e00455ce..d3fc2fb6bf6 100644 --- a/test/unit/math/rev/err/check_bounded_test.cpp +++ b/test/unit/math/rev/err/check_bounded_test.cpp @@ -17,14 +17,14 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, -1.0, 6.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -150,14 +150,14 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, 4.0, 6.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_consistent_size_test.cpp b/test/unit/math/rev/err/check_consistent_size_test.cpp index dabf63fdb3b..aaa6c70c74f 100644 --- a/test/unit/math/rev/err/check_consistent_size_test.cpp +++ b/test/unit/math/rev/err/check_consistent_size_test.cpp @@ -17,14 +17,14 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_consistent_size(function, "a", a, 5U)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_consistent_sizes_test.cpp b/test/unit/math/rev/err/check_consistent_sizes_test.cpp index 26c87d413fa..fceb142762e 100644 --- a/test/unit/math/rev/err/check_consistent_sizes_test.cpp +++ b/test/unit/math/rev/err/check_consistent_sizes_test.cpp @@ -19,14 +19,14 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizesVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(10U, stack_size); EXPECT_NO_THROW(check_consistent_sizes(function, "a", a, "b", b)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(10U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_finite_test.cpp b/test/unit/math/rev/err/check_finite_test.cpp index 99a5ad41bd7..4d65b3f8132 100644 --- a/test/unit/math/rev/err/check_finite_test.cpp +++ b/test/unit/math/rev/err/check_finite_test.cpp @@ -17,21 +17,21 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); @@ -71,21 +71,21 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(2U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_greater_or_equal_test.cpp b/test/unit/math/rev/err/check_greater_or_equal_test.cpp index 24252646030..1f4f746b527 100644 --- a/test/unit/math/rev/err/check_greater_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_greater_or_equal_test.cpp @@ -17,21 +17,21 @@ TEST(AgradRevErrorHandlingScalar, CheckGreateOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, -1.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -198,21 +198,21 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, 2.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_greater_test.cpp b/test/unit/math/rev/err/check_greater_test.cpp index 1da58eeb7ae..933cef600b4 100644 --- a/test/unit/math/rev/err/check_greater_test.cpp +++ b/test/unit/math/rev/err/check_greater_test.cpp @@ -16,20 +16,20 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, -1.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -188,20 +188,20 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, 2.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_less_or_equal_test.cpp b/test/unit/math/rev/err/check_less_or_equal_test.cpp index bf63756ff02..fe96da18b5c 100644 --- a/test/unit/math/rev/err/check_less_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_less_or_equal_test.cpp @@ -16,20 +16,20 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -161,27 +161,27 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 5.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_less_test.cpp b/test/unit/math/rev/err/check_less_test.cpp index 241d328b619..c30a694534c 100644 --- a/test/unit/math/rev/err/check_less_test.cpp +++ b/test/unit/math/rev/err/check_less_test.cpp @@ -16,20 +16,20 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -158,20 +158,20 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_nonnegative_test.cpp b/test/unit/math/rev/err/check_nonnegative_test.cpp index 93113b8e186..6d2bafb4991 100644 --- a/test/unit/math/rev/err/check_nonnegative_test.cpp +++ b/test/unit/math/rev/err/check_nonnegative_test.cpp @@ -48,35 +48,35 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(6U, stack_size_after_call); a[1] = -1.0; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(7U, stack_size_after_call); a[1] = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(8U, stack_size_after_call); stan::math::recover_memory(); @@ -116,35 +116,35 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(2U, stack_size_after_call); a = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(3U, stack_size_after_call); a = -1.1; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(4U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_not_nan_test.cpp b/test/unit/math/rev/err/check_not_nan_test.cpp index 820cf12b540..9d29b698126 100644 --- a/test/unit/math/rev/err/check_not_nan_test.cpp +++ b/test/unit/math/rev/err/check_not_nan_test.cpp @@ -16,14 +16,14 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -41,14 +41,14 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -98,14 +98,14 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); @@ -120,14 +120,14 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp index b384b023483..b4666582fcd 100644 --- a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp @@ -50,13 +50,13 @@ TEST(AgradRevErrorHandlingMatrix, checkPosSemiDefiniteMatrixVarCheck) { size_t stack_before_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(9U, stack_before_call); EXPECT_NO_THROW(check_pos_semidefinite("checkPosDefiniteMatrix", "y", y)); size_t stack_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(9U, stack_after_call); } diff --git a/test/unit/math/rev/err/check_positive_finite_test.cpp b/test/unit/math/rev/err/check_positive_finite_test.cpp index e8fb8af1a92..0e63eb4ca49 100644 --- a/test/unit/math/rev/err/check_positive_finite_test.cpp +++ b/test/unit/math/rev/err/check_positive_finite_test.cpp @@ -67,7 +67,7 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); @@ -75,14 +75,14 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(5U, stack_size_after_call); a[2] = std::numeric_limits::infinity(); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); } @@ -163,14 +163,14 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_positive_test.cpp b/test/unit/math/rev/err/check_positive_test.cpp index dee6535363f..2b4ed43f15f 100644 --- a/test/unit/math/rev/err/check_positive_test.cpp +++ b/test/unit/math/rev/err/check_positive_test.cpp @@ -60,14 +60,14 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); + + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); From 4674b1de315adc68ce0ff4915e1da97845713c89 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 16:20:09 -0400 Subject: [PATCH 096/167] update docs for var and vari --- stan/math/rev/core/var.hpp | 8 ++++---- stan/math/rev/core/vari.hpp | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 5688ef2833a..629ca0ca7b7 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -24,7 +24,7 @@ static void grad(Vari* vi); * calculation. * * A var is constructed with a type `T` and used like any - * other scalar. Arithmetical functions like negation, addition, + * other scalar. Arithmetical functions like negation, addition, * and subtraction, as well as a range of mathematical functions * like exponentiation and powers are overridden to operate on * var values objects. @@ -36,9 +36,9 @@ class var_value {}; template class var_value> { public: - using value_type = std::decay_t; // Numeric type in vari_value - using vari_type = vari_value; // - using vari_pointer = vari_type*; + using value_type = std::decay_t; // Numeric type in vari_value. + using vari_type = vari_value; // Type of underlying vari impl. + using vari_pointer = vari_type*; // pointer type for underlying vari. /** * Pointer to the implementation of this variable. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 7f66cd39c99..f555175fa95 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -15,6 +15,12 @@ template class var_value; /** * Abstract base class that all `vari_value` and it's derived classes inherit. + * + * The chain() method applies the chain rule. Concrete extensions + * of this class will represent base variables or the result + * of operations such as addition or subtraction. These extended + * classes will store operand variables and propagate derivative + * information via an implementation of chain(). */ class vari_base { public: @@ -23,6 +29,7 @@ class vari_base { * on which it depends. */ virtual void chain(){} + virtual ~vari_base {} }; /** * The variable implementation base class. @@ -34,11 +41,6 @@ class vari_base { * value. It also stores the adjoint for storing the partial * derivative with respect to the root of the derivative tree. * - * The chain() method applies the chain rule. Concrete extensions - * of this class will represent base variables or the result - * of operations such as addition or subtraction. These extended - * classes will store operand variables and propagate derivative - * information via an implementation of chain(). */ template class vari_value; @@ -73,7 +75,7 @@ class vari_value::value>> * derivative propagation, the chain() method of each variable * will be called in the reverse order of construction. * - * @tparam S an Arithmetic type. + * @tparam S a floating point type. * @param x Value of the constructed variable. */ template ::value>> /** * Construct a variable implementation from a value. The * adjoint is initialized to zero and if `stacked` is `false` this vari - * will be moved to the nochain stack s.t. it's chain method will not be - * called when calling `grad()`. + * will be not be put on the var_stack. Instead it will only be put on + * a stack to keep track of whether the adjoint needs to be set to zero. * - * All constructed variables are added to the stack. Variables + * All constructed variables are added to a stack. Variables * should be constructed before variables on which they depend * to insure proper partial derivative propagation. During * derivative propagation, the chain() method of each variable * will be called in the reverse order of construction. * - * @tparam S an Arithmetic type. + * @tparam S n floating point type. * @param x Value of the constructed variable. * @param stacked If false will put this this vari on the nochain stack so * that its `chain()` method is not called. @@ -115,7 +117,7 @@ class vari_value::value>> /** * Constructor from vari_value - * @tparam S An arithmetic type + * @tparam S A floating point type * @param x A vari_value */ vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { From 64de92c694109d0d25566c9021d4cbea2186aac5 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 20:26:00 +0000 Subject: [PATCH 097/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/var.hpp | 6 +-- stan/math/rev/core/vari.hpp | 11 +++-- .../rev/core/thread_stack_instance_test.cpp | 8 +++- test/unit/math/rev/err/check_bounded_test.cpp | 16 ++++++-- .../rev/err/check_consistent_size_test.cpp | 8 +++- .../rev/err/check_consistent_sizes_test.cpp | 8 +++- test/unit/math/rev/err/check_finite_test.cpp | 24 ++++++++--- .../rev/err/check_greater_or_equal_test.cpp | 24 ++++++++--- test/unit/math/rev/err/check_greater_test.cpp | 24 ++++++++--- .../math/rev/err/check_less_or_equal_test.cpp | 28 +++++++++---- test/unit/math/rev/err/check_less_test.cpp | 24 ++++++++--- .../math/rev/err/check_nonnegative_test.cpp | 40 ++++++++++++++----- test/unit/math/rev/err/check_not_nan_test.cpp | 32 +++++++++++---- .../rev/err/check_pos_semidefinite_test.cpp | 8 +++- .../rev/err/check_positive_finite_test.cpp | 20 +++++++--- .../unit/math/rev/err/check_positive_test.cpp | 8 +++- 16 files changed, 212 insertions(+), 77 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 629ca0ca7b7..f0a18a78fb3 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -36,9 +36,9 @@ class var_value {}; template class var_value> { public: - using value_type = std::decay_t; // Numeric type in vari_value. - using vari_type = vari_value; // Type of underlying vari impl. - using vari_pointer = vari_type*; // pointer type for underlying vari. + using value_type = std::decay_t; // Numeric type in vari_value. + using vari_type = vari_value; // Type of underlying vari impl. + using vari_pointer = vari_type*; // pointer type for underlying vari. /** * Pointer to the implementation of this variable. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index f555175fa95..961d55f98b0 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -24,11 +24,11 @@ class var_value; */ class vari_base { public: - /** - * Apply the chain rule to this variable based on the variables - * on which it depends. - */ - virtual void chain(){} + /** + * Apply the chain rule to this variable based on the variables + * on which it depends. + */ + virtual void chain() {} virtual ~vari_base {} }; /** @@ -127,7 +127,6 @@ class vari_value::value>> ChainableStack::instance_->var_stack_.emplace_back(this); } - /** * Initialize the adjoint for this (dependent) variable to 1. * This operation is applied to the dependent variable before diff --git a/test/unit/math/rev/core/thread_stack_instance_test.cpp b/test/unit/math/rev/core/thread_stack_instance_test.cpp index 17e20682246..ce610781d15 100644 --- a/test/unit/math/rev/core/thread_stack_instance_test.cpp +++ b/test/unit/math/rev/core/thread_stack_instance_test.cpp @@ -93,14 +93,18 @@ TEST(thread_stack_instance, child_instances) { ChainableStack thread_instance; EXPECT_TRUE( main_ad_stack->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size() + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size() #ifdef STAN_THREADS > #else == #endif ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size()); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size()); }; std::thread other_work(thread_tester); diff --git a/test/unit/math/rev/err/check_bounded_test.cpp b/test/unit/math/rev/err/check_bounded_test.cpp index d3fc2fb6bf6..c9fcb02b82a 100644 --- a/test/unit/math/rev/err/check_bounded_test.cpp +++ b/test/unit/math/rev/err/check_bounded_test.cpp @@ -17,14 +17,18 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, -1.0, 6.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -150,14 +154,18 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, 4.0, 6.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_consistent_size_test.cpp b/test/unit/math/rev/err/check_consistent_size_test.cpp index aaa6c70c74f..4f613901d48 100644 --- a/test/unit/math/rev/err/check_consistent_size_test.cpp +++ b/test/unit/math/rev/err/check_consistent_size_test.cpp @@ -17,14 +17,18 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_consistent_size(function, "a", a, 5U)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_consistent_sizes_test.cpp b/test/unit/math/rev/err/check_consistent_sizes_test.cpp index fceb142762e..e2e6be8656a 100644 --- a/test/unit/math/rev/err/check_consistent_sizes_test.cpp +++ b/test/unit/math/rev/err/check_consistent_sizes_test.cpp @@ -19,14 +19,18 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizesVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(10U, stack_size); EXPECT_NO_THROW(check_consistent_sizes(function, "a", a, "b", b)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(10U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_finite_test.cpp b/test/unit/math/rev/err/check_finite_test.cpp index 4d65b3f8132..1e2acbca5b4 100644 --- a/test/unit/math/rev/err/check_finite_test.cpp +++ b/test/unit/math/rev/err/check_finite_test.cpp @@ -17,21 +17,27 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); @@ -71,21 +77,27 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(2U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_greater_or_equal_test.cpp b/test/unit/math/rev/err/check_greater_or_equal_test.cpp index 1f4f746b527..475901b8582 100644 --- a/test/unit/math/rev/err/check_greater_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_greater_or_equal_test.cpp @@ -17,21 +17,27 @@ TEST(AgradRevErrorHandlingScalar, CheckGreateOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, -1.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -198,21 +204,27 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, 2.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_greater_test.cpp b/test/unit/math/rev/err/check_greater_test.cpp index 933cef600b4..cda6141c8bc 100644 --- a/test/unit/math/rev/err/check_greater_test.cpp +++ b/test/unit/math/rev/err/check_greater_test.cpp @@ -16,20 +16,26 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, -1.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -188,20 +194,26 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, 2.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_less_or_equal_test.cpp b/test/unit/math/rev/err/check_less_or_equal_test.cpp index fe96da18b5c..1c0cd417aae 100644 --- a/test/unit/math/rev/err/check_less_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_less_or_equal_test.cpp @@ -16,20 +16,26 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -161,27 +167,35 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 5.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_less_test.cpp b/test/unit/math/rev/err/check_less_test.cpp index c30a694534c..a671b4368b1 100644 --- a/test/unit/math/rev/err/check_less_test.cpp +++ b/test/unit/math/rev/err/check_less_test.cpp @@ -16,20 +16,26 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -158,20 +164,26 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_nonnegative_test.cpp b/test/unit/math/rev/err/check_nonnegative_test.cpp index 6d2bafb4991..b7451c0c0aa 100644 --- a/test/unit/math/rev/err/check_nonnegative_test.cpp +++ b/test/unit/math/rev/err/check_nonnegative_test.cpp @@ -48,35 +48,45 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(6U, stack_size_after_call); a[1] = -1.0; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(7U, stack_size_after_call); a[1] = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(8U, stack_size_after_call); stan::math::recover_memory(); @@ -116,35 +126,45 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(2U, stack_size_after_call); a = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(3U, stack_size_after_call); a = -1.1; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(4U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_not_nan_test.cpp b/test/unit/math/rev/err/check_not_nan_test.cpp index 9d29b698126..260e6908ba3 100644 --- a/test/unit/math/rev/err/check_not_nan_test.cpp +++ b/test/unit/math/rev/err/check_not_nan_test.cpp @@ -16,14 +16,18 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -41,14 +45,18 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -98,14 +106,18 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); @@ -120,14 +132,18 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp index b4666582fcd..f07cfc2311a 100644 --- a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp @@ -50,13 +50,17 @@ TEST(AgradRevErrorHandlingMatrix, checkPosSemiDefiniteMatrixVarCheck) { size_t stack_before_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(9U, stack_before_call); EXPECT_NO_THROW(check_pos_semidefinite("checkPosDefiniteMatrix", "y", y)); size_t stack_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(9U, stack_after_call); } diff --git a/test/unit/math/rev/err/check_positive_finite_test.cpp b/test/unit/math/rev/err/check_positive_finite_test.cpp index 0e63eb4ca49..0c359b38963 100644 --- a/test/unit/math/rev/err/check_positive_finite_test.cpp +++ b/test/unit/math/rev/err/check_positive_finite_test.cpp @@ -67,7 +67,9 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); @@ -75,14 +77,18 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(5U, stack_size_after_call); a[2] = std::numeric_limits::infinity(); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); } @@ -163,14 +169,18 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_positive_test.cpp b/test/unit/math/rev/err/check_positive_test.cpp index 2b4ed43f15f..6a272d03e52 100644 --- a/test/unit/math/rev/err/check_positive_test.cpp +++ b/test/unit/math/rev/err/check_positive_test.cpp @@ -60,14 +60,18 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>(stan::math::ChainableStack::instance_->var_zeroing_stacks_).size(); + + std::get>( + stan::math::ChainableStack::instance_->var_zeroing_stacks_) + .size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); From c8cd17a4711373a846bc9e65c351cb051b1fb6c9 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 16:37:59 -0400 Subject: [PATCH 098/167] forgot () in virtual vari_base destructor --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index f555175fa95..fd1854c0c10 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -29,7 +29,7 @@ class vari_base { * on which it depends. */ virtual void chain(){} - virtual ~vari_base {} + virtual ~vari_base() {}; }; /** * The variable implementation base class. From 9dd3eafe9f6dcdc39874e8779e3cbd8f2098fbf2 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 16:41:39 -0400 Subject: [PATCH 099/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/var.hpp | 10 +++++----- stan/math/rev/core/vari.hpp | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 8071b2da784..66723a584df 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -36,9 +36,9 @@ class var_value {}; template class var_value> { public: - using value_type = std::decay_t; // Numeric type in vari_value. - using vari_type = vari_value; // Type of underlying vari impl. - using vari_pointer = vari_type*; // pointer type for underlying vari. + using value_type = std::decay_t; // Numeric type in vari_value. + using vari_type = vari_value; // Type of underlying vari impl. + using vari_pointer = vari_type*; // pointer type for underlying vari. /** * Pointer to the implementation of this variable. @@ -71,8 +71,8 @@ class var_value> { /** * Construct a variable from the specified floating point argument - * by constructing a new `vari_value`. This constructor is only valid - * when `S` is convertible to this `vari_value`'s `value_type`. + * by constructing a new `vari_value`. This constructor is only + * valid when `S` is convertible to this `vari_value`'s `value_type`. * @tparam S A type that is convertible to `value_type`. * @param x Value of the variable. */ diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 21c914e6af5..65e29775a36 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -24,12 +24,12 @@ class var_value; */ class vari_base { public: - /** - * Apply the chain rule to this variable based on the variables - * on which it depends. - */ - virtual void chain(){} - virtual ~vari_base() {}; + /** + * Apply the chain rule to this variable based on the variables + * on which it depends. + */ + virtual void chain() {} + virtual ~vari_base(){}; }; /** * The variable implementation base class. From 1d187bcbaa728859424fcd6672a467b872a65c49 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 17:05:19 -0400 Subject: [PATCH 100/167] fix libstdc++ bug with array --- stan/math/rev/core/autodiffstackstorage.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 42c74ea92dc..a3e9e42a619 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_REV_CORE_AUTODIFFSTACKSTORAGE_HPP #include +#include #include namespace stan { @@ -112,7 +113,7 @@ struct AutodiffStackSingleton { // nested positions std::vector nested_var_stack_sizes_; std::array, std::tuple_size::value> - nested_var_zeroing_stack_sizes_; + nested_var_zeroing_stack_sizes_{}; std::vector nested_var_alloc_stack_starts_; }; From eccd3cb41850f23c1ce8c01a98e5b980e2707633 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 17:06:10 -0400 Subject: [PATCH 101/167] cpplint --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 65e29775a36..8486051b0ee 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -29,7 +29,7 @@ class vari_base { * on which it depends. */ virtual void chain() {} - virtual ~vari_base(){}; + virtual ~vari_base(){} }; /** * The variable implementation base class. From 6ad67140b394dcbf866e5c9dcfcb9c9a771f4ad9 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 17:06:58 -0400 Subject: [PATCH 102/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 8486051b0ee..37ec7ae49f9 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -29,7 +29,7 @@ class vari_base { * on which it depends. */ virtual void chain() {} - virtual ~vari_base(){} + virtual ~vari_base() {} }; /** * The variable implementation base class. From 165a88338cec8a71260972396b4d2ecbe5935e51 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 17 Jun 2020 18:30:07 -0400 Subject: [PATCH 103/167] fix seg fault by not always giving var_zeroing stacks to nested_var_zero stack sizes --- stan/math/rev/core/start_nested.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index a667f695136..fcd9b8170ea 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -19,9 +19,7 @@ static inline void start_nested() { ChainableStack::instance_->var_stack_.size()); for_each( [](auto& x, auto& y) { - if (y.size() > 0) { x.push_back(y.size()); - } }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); From fa5de840dd16f7e1b566dd162e24eb42e16a8cf6 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 17 Jun 2020 18:30:56 -0400 Subject: [PATCH 104/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/start_nested.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index fcd9b8170ea..241b0aa9a2e 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,12 +17,9 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each( - [](auto& x, auto& y) { - x.push_back(y.size()); - }, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each([](auto& x, auto& y) { x.push_back(y.size()); }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From efaa936d28906ea164a76587f7b73429b2d56a3d Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 18 Jun 2020 17:46:22 -0400 Subject: [PATCH 105/167] only push sizes if they are nonzero for the nested zeroing stack --- stan/math/rev/core/autodiffstackstorage.hpp | 2 +- stan/math/rev/core/start_nested.hpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index a3e9e42a619..2a31d146eb0 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -113,7 +113,7 @@ struct AutodiffStackSingleton { // nested positions std::vector nested_var_stack_sizes_; std::array, std::tuple_size::value> - nested_var_zeroing_stack_sizes_{}; + nested_var_zeroing_stack_sizes_{std::vector{1, 0}}; std::vector nested_var_alloc_stack_starts_; }; diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 241b0aa9a2e..125798a7907 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,7 +17,11 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each([](auto& x, auto& y) { x.push_back(y.size()); }, + for_each([](auto& x, auto& y) { + if (y.size() > 0) { + x.push_back(y.size()); + } + }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( From 908ff3c7c65bce0dadbbed912c82eba2b1e39692 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 18 Jun 2020 17:47:37 -0400 Subject: [PATCH 106/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/start_nested.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 125798a7907..a667f695136 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,13 +17,14 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each([](auto& x, auto& y) { - if (y.size() > 0) { - x.push_back(y.size()); - } - }, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each( + [](auto& x, auto& y) { + if (y.size() > 0) { + x.push_back(y.size()); + } + }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From 3b85b96eb40b2937f917ee502f0cff82a2d054fa Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 19 Jun 2020 14:40:49 -0400 Subject: [PATCH 107/167] move around the stacks to avoid double push_backs in vari --- stan/math/rev/core/set_zero_all_adjoints.hpp | 3 +++ .../rev/core/set_zero_all_adjoints_nested.hpp | 10 ++++++++-- stan/math/rev/core/start_nested.hpp | 2 -- stan/math/rev/core/vari.hpp | 16 ++++++---------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index 41e9f411947..61b5f917247 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -13,6 +13,9 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static EIGEN_STRONG_INLINE void set_zero_all_adjoints() { + for (auto& xx : ChainableStack::instance_->var_stack_) { + xx->set_zero_adjoint(); + } for_each( [](auto& x) { for (auto& xx : x) { diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index dbe1ce44b1f..3dd7577ae93 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -25,10 +25,16 @@ static EIGEN_STRONG_INLINE void set_zero_all_adjoints_nested() { " set_zero_all_adjoints_nested()"); } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); + // avoid wrap with unsigned when start1 == 0 + for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); + i < ChainableStack::instance_->var_stack_.size(); ++i) { + ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); + } for_each( - [&start1](auto& x, auto& x_size) { + [](auto& x, auto& x_size) { + size_t start2 = x_size.size(); const auto stack_size = x.size(); - for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < stack_size; + for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); i < stack_size; ++i) { x[i]->set_zero_adjoint(); } diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index a667f695136..fcd9b8170ea 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -19,9 +19,7 @@ static inline void start_nested() { ChainableStack::instance_->var_stack_.size()); for_each( [](auto& x, auto& y) { - if (y.size() > 0) { x.push_back(y.size()); - } }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 37ec7ae49f9..18572df3db5 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -29,6 +29,7 @@ class vari_base { * on which it depends. */ virtual void chain() {} + virtual void set_zero_adjoint() {} virtual ~vari_base() {} }; /** @@ -81,9 +82,6 @@ class vari_value::value>> template ::value>* = nullptr> vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT - std::get*>>( - ChainableStack::instance_->var_zeroing_stacks_) - .emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -107,11 +105,12 @@ class vari_value::value>> template ::value>* = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { - std::get*>>( - ChainableStack::instance_->var_zeroing_stacks_) - .emplace_back(this); if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); + } else { + std::get*>>( + ChainableStack::instance_->var_zeroing_stacks_) + .emplace_back(this); } } @@ -121,9 +120,6 @@ class vari_value::value>> * @param x A vari_value */ vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { - std::get*>>( - ChainableStack::instance_->var_zeroing_stacks_) - .emplace_back(this); ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -140,7 +136,7 @@ class vari_value::value>> * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - inline void set_zero_adjoint() noexcept { adj_ = 0.0; } + inline void set_zero_adjoint() noexcept final { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and From 8b2a934aec3f739a3d26c3f73edca25eb71f2404 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 19 Jun 2020 15:42:24 -0400 Subject: [PATCH 108/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/start_nested.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index fcd9b8170ea..241b0aa9a2e 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,12 +17,9 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each( - [](auto& x, auto& y) { - x.push_back(y.size()); - }, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each([](auto& x, auto& y) { x.push_back(y.size()); }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From 72fefc4df68265998654eabba5b2c972cf25238e Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 20 Jun 2020 00:21:31 -0400 Subject: [PATCH 109/167] Fix bad stack sizes --- stan/math/rev/core/autodiffstackstorage.hpp | 2 +- stan/math/rev/core/start_nested.hpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 2a31d146eb0..7a2ddde45ad 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -113,7 +113,7 @@ struct AutodiffStackSingleton { // nested positions std::vector nested_var_stack_sizes_; std::array, std::tuple_size::value> - nested_var_zeroing_stack_sizes_{std::vector{1, 0}}; + nested_var_zeroing_stack_sizes_; std::vector nested_var_alloc_stack_starts_; }; diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 241b0aa9a2e..9c37d76195b 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,7 +17,11 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each([](auto& x, auto& y) { x.push_back(y.size()); }, + for_each([](auto& x, auto& y) { + if (y.size() > 0) { + x.push_back(y.size()); + } + }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( From 16919852efc0f2fdde16bb3bd30c13dc3bc22af4 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sat, 20 Jun 2020 00:32:31 -0400 Subject: [PATCH 110/167] use make_unique in ad_tape_observer --- stan/math/rev/core/init_chainablestack.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/init_chainablestack.hpp b/stan/math/rev/core/init_chainablestack.hpp index 4104e388081..5df2f797012 100644 --- a/stan/math/rev/core/init_chainablestack.hpp +++ b/stan/math/rev/core/init_chainablestack.hpp @@ -23,7 +23,7 @@ namespace math { * Refer to https://software.intel.com/en-us/node/506314 for details * on the observer concept. */ -class ad_tape_observer : public tbb::task_scheduler_observer { +class ad_tape_observer final : public tbb::task_scheduler_observer { using stack_ptr = std::unique_ptr; using ad_map = std::unordered_map; @@ -43,7 +43,7 @@ class ad_tape_observer : public tbb::task_scheduler_observer { bool status = false; std::tie(insert_elem, status) = thread_tape_map_.emplace(ad_map::value_type{thread_id, nullptr}); - insert_elem->second = stack_ptr(new ChainableStack()); + insert_elem->second = std::move(std::make_unique()); } } From c1e3d0e5f4fe703567e10ff1d6adbfd3bc203383 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sat, 20 Jun 2020 01:18:10 -0400 Subject: [PATCH 111/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/start_nested.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 9c37d76195b..a667f695136 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,13 +17,14 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each([](auto& x, auto& y) { - if (y.size() > 0) { - x.push_back(y.size()); - } - }, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each( + [](auto& x, auto& y) { + if (y.size() > 0) { + x.push_back(y.size()); + } + }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From 4409bfd92a95003f7b192d0b338dbd659016b417 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 23 Jun 2020 14:49:26 -0400 Subject: [PATCH 112/167] update to fix mem bug in mpi tests --- stan/math/rev/core/start_nested.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index a667f695136..fcd9b8170ea 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -19,9 +19,7 @@ static inline void start_nested() { ChainableStack::instance_->var_stack_.size()); for_each( [](auto& x, auto& y) { - if (y.size() > 0) { x.push_back(y.size()); - } }, ChainableStack::instance_->nested_var_zeroing_stack_sizes_, ChainableStack::instance_->var_zeroing_stacks_); From 725555483cc4d90a27db0aa32832a2e1324466cf Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 23 Jun 2020 18:50:03 +0000 Subject: [PATCH 113/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/start_nested.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index fcd9b8170ea..241b0aa9a2e 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -17,12 +17,9 @@ namespace math { static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( ChainableStack::instance_->var_stack_.size()); - for_each( - [](auto& x, auto& y) { - x.push_back(y.size()); - }, - ChainableStack::instance_->nested_var_zeroing_stack_sizes_, - ChainableStack::instance_->var_zeroing_stacks_); + for_each([](auto& x, auto& y) { x.push_back(y.size()); }, + ChainableStack::instance_->nested_var_zeroing_stack_sizes_, + ChainableStack::instance_->var_zeroing_stacks_); ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( ChainableStack::instance_->var_alloc_stack_.size()); ChainableStack::instance_->memalloc_.start_nested(); From 5e9f707f88975ef6704b3875fe839126df074a15 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 24 Jun 2020 14:52:40 -0400 Subject: [PATCH 114/167] move back to virtual methods --- stan/math/rev/core/var.hpp | 14 -------------- stan/math/rev/core/vari.hpp | 20 ++------------------ 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index c8001d28963..e3705fdbdd9 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -40,12 +40,6 @@ class var_value> { using value_type = std::decay_t; using vari_type = vari_value; using vari_pointer = vari_type*; - template - using require_vari_convertible_t - = require_t*, vari_pointer>>; - template - using require_not_vari_convertible_t = require_t< - bool_constant*, vari_pointer>::value>>; /** * Pointer to the implementation of this variable. @@ -97,14 +91,6 @@ class var_value> { var_value(vari_value* vi) // NOLINT : vi_(vi) {} - /** - * Constructor from `var_value` whose value_type is the same as this class's - * `value_type`. This is used in cases such as - * `var_value a(4.0); var_value b(a)` since the `value_type` for - * a `var_value` with an integral type is a double. - */ - var_value(const var_value& x) : vi_(x.vi_) {} // NOLINT - /** * Return a constant reference to the value of this variable. * diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 81066ffbeaf..a5fd3df62ec 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -17,7 +17,7 @@ class var_value; class vari_base { public: virtual void chain() {} - virtual void set_zero_adjoint() noexcept {} + inline virtual void set_zero_adjoint() noexcept {} }; /** * The variable implementation base class. @@ -104,22 +104,6 @@ class vari_value::value>> : public } } - /** - * Constructor from vari_value - * @tparam S An arithmetic type - * @param x A vari_value - */ - vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.emplace_back(this); - } - /** - * Constructor from vari_value - * @tparam S An arithmetic type - * @param x A vari_value - */ - vari_value(vari_value&& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.emplace_back(this); - } /** * Initialize the adjoint for this (dependent) variable to 1. @@ -134,7 +118,7 @@ class vari_value::value>> : public * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() noexcept final { adj_ = 0.0; } + inline void set_zero_adjoint() noexcept final { adj_ = 0.0; } /** * Insertion operator for vari. Prints the current value and From 330af390fe62cad22930601d9e02f3f7f062e795 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 24 Jun 2020 15:39:27 -0400 Subject: [PATCH 115/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/autodiffstackstorage.hpp | 84 +++++++++---------- stan/math/rev/core/chainablestack.hpp | 3 +- stan/math/rev/core/recover_memory.hpp | 24 +++--- stan/math/rev/core/recover_memory_nested.hpp | 48 +++++------ stan/math/rev/core/set_zero_all_adjoints.hpp | 12 +-- .../rev/core/set_zero_all_adjoints_nested.hpp | 34 ++++---- stan/math/rev/core/start_nested.hpp | 14 ++-- stan/math/rev/core/vari.hpp | 4 +- 8 files changed, 111 insertions(+), 112 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index a349d3968fd..93578cbbc63 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -91,60 +91,60 @@ class vari_value; */ template struct AutodiffStackSingleton { - using AutodiffStackSingleton_t - = AutodiffStackSingleton; + using AutodiffStackSingleton_t + = AutodiffStackSingleton; - AutodiffStackSingleton() : own_instance_(init()) {} - ~AutodiffStackSingleton() { - if (own_instance_) { - delete instance_; - instance_ = nullptr; - } - } + AutodiffStackSingleton() : own_instance_(init()) {} + ~AutodiffStackSingleton() { + if (own_instance_) { + delete instance_; + instance_ = nullptr; + } + } - struct AutodiffStackStorage { - AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; + struct AutodiffStackStorage { + AutodiffStackStorage &operator=(const AutodiffStackStorage &) = delete; - std::vector var_stack_; - std::vector var_nochain_stack_; - std::vector var_alloc_stack_; - stack_alloc memalloc_; + std::vector var_stack_; + std::vector var_nochain_stack_; + std::vector var_alloc_stack_; + stack_alloc memalloc_; - // nested positions - std::vector nested_var_stack_sizes_; - std::vector nested_var_nochain_stack_sizes_; - std::vector nested_var_alloc_stack_starts_; - }; + // nested positions + std::vector nested_var_stack_sizes_; + std::vector nested_var_nochain_stack_sizes_; + std::vector nested_var_alloc_stack_starts_; + }; - explicit AutodiffStackSingleton(AutodiffStackSingleton_t const &) = delete; - AutodiffStackSingleton &operator=(const AutodiffStackSingleton_t &) = delete; + explicit AutodiffStackSingleton(AutodiffStackSingleton_t const &) = delete; + AutodiffStackSingleton &operator=(const AutodiffStackSingleton_t &) = delete; - static STAN_THREADS_DEF AutodiffStackStorage *instance_; + static STAN_THREADS_DEF AutodiffStackStorage *instance_; -private: - static bool init() { - static STAN_THREADS_DEF bool is_initialized = false; - if (!is_initialized) { - is_initialized = true; - instance_ = new AutodiffStackStorage(); - return true; - } - if (!instance_) { - is_initialized = true; - instance_ = new AutodiffStackStorage(); - return true; - } - return false; - } + private: + static bool init() { + static STAN_THREADS_DEF bool is_initialized = false; + if (!is_initialized) { + is_initialized = true; + instance_ = new AutodiffStackStorage(); + return true; + } + if (!instance_) { + is_initialized = true; + instance_ = new AutodiffStackStorage(); + return true; + } + return false; + } - bool own_instance_; + bool own_instance_; }; template STAN_THREADS_DEF - typename AutodiffStackSingleton::AutodiffStackStorage - *AutodiffStackSingleton::instance_; + typename AutodiffStackSingleton::AutodiffStackStorage + *AutodiffStackSingleton::instance_; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index bbf82f47a4d..d383fabdcf9 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -10,8 +10,7 @@ template class vari_value; class vari_base; class chainable_alloc; -using ChainableStack - = AutodiffStackSingleton; +using ChainableStack = AutodiffStackSingleton; } // namespace math } // namespace stan diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index 205d63946b1..c8ec7e92b66 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -16,18 +16,18 @@ namespace math { * false */ static inline void recover_memory() { - if (!empty_nested()) { - throw std::logic_error( - "empty_nested() must be true" - " before calling recover_memory()"); - } - ChainableStack::instance_->var_stack_.clear(); - ChainableStack::instance_->var_nochain_stack_.clear(); - for (auto &x : ChainableStack::instance_->var_alloc_stack_) { - delete x; - } - ChainableStack::instance_->var_alloc_stack_.clear(); - ChainableStack::instance_->memalloc_.recover_all(); + if (!empty_nested()) { + throw std::logic_error( + "empty_nested() must be true" + " before calling recover_memory()"); + } + ChainableStack::instance_->var_stack_.clear(); + ChainableStack::instance_->var_nochain_stack_.clear(); + for (auto &x : ChainableStack::instance_->var_alloc_stack_) { + delete x; + } + ChainableStack::instance_->var_alloc_stack_.clear(); + ChainableStack::instance_->memalloc_.recover_all(); } } // namespace math diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index d1654819953..1cd493a1a11 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -21,30 +21,30 @@ namespace math { * true */ static inline void recover_memory_nested() { - if (empty_nested()) { - throw std::logic_error( - "empty_nested() must be false" - " before calling recover_memory_nested()"); - } - - ChainableStack::instance_->var_stack_.resize( - ChainableStack::instance_->nested_var_stack_sizes_.back()); - ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); - - ChainableStack::instance_->var_nochain_stack_.resize( - ChainableStack::instance_->nested_var_nochain_stack_sizes_.back()); - ChainableStack::instance_->nested_var_nochain_stack_sizes_.pop_back(); - - for (size_t i - = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); - i < ChainableStack::instance_->var_alloc_stack_.size(); ++i) { - delete ChainableStack::instance_->var_alloc_stack_[i]; - } - ChainableStack::instance_->var_alloc_stack_.resize( - ChainableStack::instance_->nested_var_alloc_stack_starts_.back()); - ChainableStack::instance_->nested_var_alloc_stack_starts_.pop_back(); - - ChainableStack::instance_->memalloc_.recover_nested(); + if (empty_nested()) { + throw std::logic_error( + "empty_nested() must be false" + " before calling recover_memory_nested()"); + } + + ChainableStack::instance_->var_stack_.resize( + ChainableStack::instance_->nested_var_stack_sizes_.back()); + ChainableStack::instance_->nested_var_stack_sizes_.pop_back(); + + ChainableStack::instance_->var_nochain_stack_.resize( + ChainableStack::instance_->nested_var_nochain_stack_sizes_.back()); + ChainableStack::instance_->nested_var_nochain_stack_sizes_.pop_back(); + + for (size_t i + = ChainableStack::instance_->nested_var_alloc_stack_starts_.back(); + i < ChainableStack::instance_->var_alloc_stack_.size(); ++i) { + delete ChainableStack::instance_->var_alloc_stack_[i]; + } + ChainableStack::instance_->var_alloc_stack_.resize( + ChainableStack::instance_->nested_var_alloc_stack_starts_.back()); + ChainableStack::instance_->nested_var_alloc_stack_starts_.pop_back(); + + ChainableStack::instance_->memalloc_.recover_nested(); } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index f690ef96410..841ef6d5c60 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -12,12 +12,12 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static void set_zero_all_adjoints() { - for (auto &x : ChainableStack::instance_->var_stack_) { - x->set_zero_adjoint(); - } - for (auto &x : ChainableStack::instance_->var_nochain_stack_) { - x->set_zero_adjoint(); - } + for (auto &x : ChainableStack::instance_->var_stack_) { + x->set_zero_adjoint(); + } + for (auto &x : ChainableStack::instance_->var_nochain_stack_) { + x->set_zero_adjoint(); + } } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 5e8df6404d8..34dc367556c 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -18,24 +18,24 @@ namespace math { * nested autodiff class as it handles recovery of memory automatically. */ static void set_zero_all_adjoints_nested() { - if (empty_nested()) { - throw std::logic_error( - "empty_nested() must be false before calling" - " set_zero_all_adjoints_nested()"); - } - size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); - // avoid wrap with unsigned when start1 == 0 - for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); - i < ChainableStack::instance_->var_stack_.size(); ++i) { - ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); - } + if (empty_nested()) { + throw std::logic_error( + "empty_nested() must be false before calling" + " set_zero_all_adjoints_nested()"); + } + size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); + // avoid wrap with unsigned when start1 == 0 + for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); + i < ChainableStack::instance_->var_stack_.size(); ++i) { + ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); + } - size_t start2 - = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); - for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); - i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { - ChainableStack::instance_->var_nochain_stack_[i]->set_zero_adjoint(); - } + size_t start2 + = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); + for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); + i < ChainableStack::instance_->var_nochain_stack_.size(); ++i) { + ChainableStack::instance_->var_nochain_stack_[i]->set_zero_adjoint(); + } } } // namespace math diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 9b9ef395772..d17ff7e153b 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -14,13 +14,13 @@ namespace math { * nested autodiff as it handles recovery of memory automatically. */ static inline void start_nested() { - ChainableStack::instance_->nested_var_stack_sizes_.push_back( - ChainableStack::instance_->var_stack_.size()); - ChainableStack::instance_->nested_var_nochain_stack_sizes_.push_back( - ChainableStack::instance_->var_nochain_stack_.size()); - ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( - ChainableStack::instance_->var_alloc_stack_.size()); - ChainableStack::instance_->memalloc_.start_nested(); + ChainableStack::instance_->nested_var_stack_sizes_.push_back( + ChainableStack::instance_->var_stack_.size()); + ChainableStack::instance_->nested_var_nochain_stack_sizes_.push_back( + ChainableStack::instance_->var_nochain_stack_.size()); + ChainableStack::instance_->nested_var_alloc_stack_starts_.push_back( + ChainableStack::instance_->var_alloc_stack_.size()); + ChainableStack::instance_->memalloc_.start_nested(); } } // namespace math diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 4b40fd2a261..294cece21bb 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -108,7 +108,7 @@ class vari_value::value>> if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); } else { - ChainableStack::instance_->var_nochain_stack_.emplace_back(this); + ChainableStack::instance_->var_nochain_stack_.emplace_back(this); } } @@ -123,7 +123,7 @@ class vari_value::value>> ~vari_value() = default; - inline void chain() {} + inline void chain() {} /** * Initialize the adjoint for this (dependent) variable to 1. From 73e6599a1f47b456066e7f25e5c6faf5081f23b1 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 24 Jun 2020 16:33:49 -0400 Subject: [PATCH 116/167] remove references to zero_stack from tests --- .../rev/core/thread_stack_instance_test.cpp | 8 ++-- test/unit/math/rev/err/check_bounded_test.cpp | 16 ++++---- .../rev/err/check_consistent_size_test.cpp | 8 ++-- .../rev/err/check_consistent_sizes_test.cpp | 8 ++-- test/unit/math/rev/err/check_finite_test.cpp | 24 +++++------ .../rev/err/check_greater_or_equal_test.cpp | 24 +++++------ test/unit/math/rev/err/check_greater_test.cpp | 24 +++++------ .../math/rev/err/check_less_or_equal_test.cpp | 28 ++++++------- test/unit/math/rev/err/check_less_test.cpp | 24 +++++------ .../math/rev/err/check_nonnegative_test.cpp | 40 +++++++++---------- test/unit/math/rev/err/check_not_nan_test.cpp | 32 +++++++-------- .../rev/err/check_pos_semidefinite_test.cpp | 8 ++-- .../rev/err/check_positive_finite_test.cpp | 20 +++++----- .../unit/math/rev/err/check_positive_test.cpp | 8 ++-- 14 files changed, 136 insertions(+), 136 deletions(-) diff --git a/test/unit/math/rev/core/thread_stack_instance_test.cpp b/test/unit/math/rev/core/thread_stack_instance_test.cpp index ce610781d15..9fba740b45e 100644 --- a/test/unit/math/rev/core/thread_stack_instance_test.cpp +++ b/test/unit/math/rev/core/thread_stack_instance_test.cpp @@ -93,8 +93,8 @@ TEST(thread_stack_instance, child_instances) { ChainableStack thread_instance; EXPECT_TRUE( main_ad_stack->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size() #ifdef STAN_THREADS > @@ -102,8 +102,8 @@ TEST(thread_stack_instance, child_instances) { == #endif ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size()); }; diff --git a/test/unit/math/rev/err/check_bounded_test.cpp b/test/unit/math/rev/err/check_bounded_test.cpp index c9fcb02b82a..59b593c5714 100644 --- a/test/unit/math/rev/err/check_bounded_test.cpp +++ b/test/unit/math/rev/err/check_bounded_test.cpp @@ -17,8 +17,8 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -26,8 +26,8 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -154,8 +154,8 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -163,8 +163,8 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_consistent_size_test.cpp b/test/unit/math/rev/err/check_consistent_size_test.cpp index 4f613901d48..12850980bb5 100644 --- a/test/unit/math/rev/err/check_consistent_size_test.cpp +++ b/test/unit/math/rev/err/check_consistent_size_test.cpp @@ -17,8 +17,8 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -26,8 +26,8 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_consistent_sizes_test.cpp b/test/unit/math/rev/err/check_consistent_sizes_test.cpp index e2e6be8656a..b9b1644efa8 100644 --- a/test/unit/math/rev/err/check_consistent_sizes_test.cpp +++ b/test/unit/math/rev/err/check_consistent_sizes_test.cpp @@ -19,8 +19,8 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizesVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(10U, stack_size); @@ -28,8 +28,8 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizesVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(10U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_finite_test.cpp b/test/unit/math/rev/err/check_finite_test.cpp index 1e2acbca5b4..db346a90537 100644 --- a/test/unit/math/rev/err/check_finite_test.cpp +++ b/test/unit/math/rev/err/check_finite_test.cpp @@ -17,8 +17,8 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -26,8 +26,8 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -35,8 +35,8 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(6U, stack_size_after_call); @@ -77,8 +77,8 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -86,8 +86,8 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); @@ -95,8 +95,8 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(2U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_greater_or_equal_test.cpp b/test/unit/math/rev/err/check_greater_or_equal_test.cpp index 475901b8582..7fa273468d8 100644 --- a/test/unit/math/rev/err/check_greater_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_greater_or_equal_test.cpp @@ -17,8 +17,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreateOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -26,8 +26,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreateOrEqualVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -35,8 +35,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreateOrEqualVarCheckVectorized) { std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -204,8 +204,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -213,8 +213,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); @@ -222,8 +222,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_greater_test.cpp b/test/unit/math/rev/err/check_greater_test.cpp index cda6141c8bc..fa9051d1488 100644 --- a/test/unit/math/rev/err/check_greater_test.cpp +++ b/test/unit/math/rev/err/check_greater_test.cpp @@ -16,8 +16,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -25,16 +25,16 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -194,8 +194,8 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -203,16 +203,16 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_less_or_equal_test.cpp b/test/unit/math/rev/err/check_less_or_equal_test.cpp index 1c0cd417aae..9e96d55a855 100644 --- a/test/unit/math/rev/err/check_less_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_less_or_equal_test.cpp @@ -16,8 +16,8 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -25,16 +25,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -167,8 +167,8 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -176,8 +176,8 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); @@ -185,16 +185,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_less_test.cpp b/test/unit/math/rev/err/check_less_test.cpp index a671b4368b1..3f501c41c33 100644 --- a/test/unit/math/rev/err/check_less_test.cpp +++ b/test/unit/math/rev/err/check_less_test.cpp @@ -16,8 +16,8 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -25,16 +25,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -164,8 +164,8 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -173,16 +173,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_nonnegative_test.cpp b/test/unit/math/rev/err/check_nonnegative_test.cpp index b7451c0c0aa..2093535999c 100644 --- a/test/unit/math/rev/err/check_nonnegative_test.cpp +++ b/test/unit/math/rev/err/check_nonnegative_test.cpp @@ -48,8 +48,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -57,8 +57,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -66,8 +66,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(6U, stack_size_after_call); @@ -75,8 +75,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(7U, stack_size_after_call); @@ -84,8 +84,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(8U, stack_size_after_call); @@ -126,8 +126,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -135,8 +135,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); @@ -144,8 +144,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(2U, stack_size_after_call); @@ -153,8 +153,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(3U, stack_size_after_call); @@ -162,8 +162,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(4U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_not_nan_test.cpp b/test/unit/math/rev/err/check_not_nan_test.cpp index 260e6908ba3..bc6b83bcbf4 100644 --- a/test/unit/math/rev/err/check_not_nan_test.cpp +++ b/test/unit/math/rev/err/check_not_nan_test.cpp @@ -16,8 +16,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -25,8 +25,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -45,8 +45,8 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -54,8 +54,8 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -106,8 +106,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -115,8 +115,8 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); @@ -132,8 +132,8 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -141,8 +141,8 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp index f07cfc2311a..f33712a0fe0 100644 --- a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp @@ -50,16 +50,16 @@ TEST(AgradRevErrorHandlingMatrix, checkPosSemiDefiniteMatrixVarCheck) { size_t stack_before_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(9U, stack_before_call); EXPECT_NO_THROW(check_pos_semidefinite("checkPosDefiniteMatrix", "y", y)); size_t stack_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(9U, stack_after_call); diff --git a/test/unit/math/rev/err/check_positive_finite_test.cpp b/test/unit/math/rev/err/check_positive_finite_test.cpp index 0c359b38963..662e7b63065 100644 --- a/test/unit/math/rev/err/check_positive_finite_test.cpp +++ b/test/unit/math/rev/err/check_positive_finite_test.cpp @@ -67,8 +67,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size); @@ -77,8 +77,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(5U, stack_size_after_call); @@ -86,8 +86,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); @@ -169,8 +169,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -178,8 +178,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); diff --git a/test/unit/math/rev/err/check_positive_test.cpp b/test/unit/math/rev/err/check_positive_test.cpp index 6a272d03e52..3ce7752cbc8 100644 --- a/test/unit/math/rev/err/check_positive_test.cpp +++ b/test/unit/math/rev/err/check_positive_test.cpp @@ -60,8 +60,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size); @@ -69,8 +69,8 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveVarCheckUnivariate) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + std::get>( - stan::math::ChainableStack::instance_->var_zeroing_stacks_) + + + stan::math::ChainableStack::instance_->var_nochain_stack_ .size(); EXPECT_EQ(1U, stack_size_after_call); From 5d4deb57941303635f6419d0c564099cd17b8580 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 24 Jun 2020 21:10:12 +0000 Subject: [PATCH 117/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- .../rev/core/thread_stack_instance_test.cpp | 8 +--- test/unit/math/rev/err/check_bounded_test.cpp | 16 ++------ .../rev/err/check_consistent_size_test.cpp | 8 +--- .../rev/err/check_consistent_sizes_test.cpp | 8 +--- test/unit/math/rev/err/check_finite_test.cpp | 24 +++-------- .../rev/err/check_greater_or_equal_test.cpp | 24 +++-------- test/unit/math/rev/err/check_greater_test.cpp | 24 +++-------- .../math/rev/err/check_less_or_equal_test.cpp | 28 ++++--------- test/unit/math/rev/err/check_less_test.cpp | 24 +++-------- .../math/rev/err/check_nonnegative_test.cpp | 40 +++++-------------- test/unit/math/rev/err/check_not_nan_test.cpp | 32 ++++----------- .../rev/err/check_pos_semidefinite_test.cpp | 8 +--- .../rev/err/check_positive_finite_test.cpp | 20 +++------- .../unit/math/rev/err/check_positive_test.cpp | 8 +--- 14 files changed, 68 insertions(+), 204 deletions(-) diff --git a/test/unit/math/rev/core/thread_stack_instance_test.cpp b/test/unit/math/rev/core/thread_stack_instance_test.cpp index 9fba740b45e..c47d4f0d2d6 100644 --- a/test/unit/math/rev/core/thread_stack_instance_test.cpp +++ b/test/unit/math/rev/core/thread_stack_instance_test.cpp @@ -93,18 +93,14 @@ TEST(thread_stack_instance, child_instances) { ChainableStack thread_instance; EXPECT_TRUE( main_ad_stack->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size() + + stan::math::ChainableStack::instance_->var_nochain_stack_.size() #ifdef STAN_THREADS > #else == #endif ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size()); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size()); }; std::thread other_work(thread_tester); diff --git a/test/unit/math/rev/err/check_bounded_test.cpp b/test/unit/math/rev/err/check_bounded_test.cpp index 59b593c5714..3f3e00455ce 100644 --- a/test/unit/math/rev/err/check_bounded_test.cpp +++ b/test/unit/math/rev/err/check_bounded_test.cpp @@ -17,18 +17,14 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, -1.0, 6.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -154,18 +150,14 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, 4.0, 6.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_consistent_size_test.cpp b/test/unit/math/rev/err/check_consistent_size_test.cpp index 12850980bb5..dabf63fdb3b 100644 --- a/test/unit/math/rev/err/check_consistent_size_test.cpp +++ b/test/unit/math/rev/err/check_consistent_size_test.cpp @@ -17,18 +17,14 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_consistent_size(function, "a", a, 5U)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_consistent_sizes_test.cpp b/test/unit/math/rev/err/check_consistent_sizes_test.cpp index b9b1644efa8..26c87d413fa 100644 --- a/test/unit/math/rev/err/check_consistent_sizes_test.cpp +++ b/test/unit/math/rev/err/check_consistent_sizes_test.cpp @@ -19,18 +19,14 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizesVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(10U, stack_size); EXPECT_NO_THROW(check_consistent_sizes(function, "a", a, "b", b)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(10U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_finite_test.cpp b/test/unit/math/rev/err/check_finite_test.cpp index db346a90537..99a5ad41bd7 100644 --- a/test/unit/math/rev/err/check_finite_test.cpp +++ b/test/unit/math/rev/err/check_finite_test.cpp @@ -17,27 +17,21 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); @@ -77,27 +71,21 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(2U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_greater_or_equal_test.cpp b/test/unit/math/rev/err/check_greater_or_equal_test.cpp index 7fa273468d8..24252646030 100644 --- a/test/unit/math/rev/err/check_greater_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_greater_or_equal_test.cpp @@ -17,27 +17,21 @@ TEST(AgradRevErrorHandlingScalar, CheckGreateOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, -1.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -204,27 +198,21 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, 2.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_greater_test.cpp b/test/unit/math/rev/err/check_greater_test.cpp index fa9051d1488..1da58eeb7ae 100644 --- a/test/unit/math/rev/err/check_greater_test.cpp +++ b/test/unit/math/rev/err/check_greater_test.cpp @@ -16,26 +16,20 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, -1.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -194,26 +188,20 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, 2.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 10.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_less_or_equal_test.cpp b/test/unit/math/rev/err/check_less_or_equal_test.cpp index 9e96d55a855..bf63756ff02 100644 --- a/test/unit/math/rev/err/check_less_or_equal_test.cpp +++ b/test/unit/math/rev/err/check_less_or_equal_test.cpp @@ -16,26 +16,20 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -167,35 +161,27 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 5.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_less_test.cpp b/test/unit/math/rev/err/check_less_test.cpp index 3f501c41c33..241d328b619 100644 --- a/test/unit/math/rev/err/check_less_test.cpp +++ b/test/unit/math/rev/err/check_less_test.cpp @@ -16,26 +16,20 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); @@ -164,26 +158,20 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_nonnegative_test.cpp b/test/unit/math/rev/err/check_nonnegative_test.cpp index 2093535999c..93113b8e186 100644 --- a/test/unit/math/rev/err/check_nonnegative_test.cpp +++ b/test/unit/math/rev/err/check_nonnegative_test.cpp @@ -48,45 +48,35 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(6U, stack_size_after_call); a[1] = -1.0; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(7U, stack_size_after_call); a[1] = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(8U, stack_size_after_call); stan::math::recover_memory(); @@ -126,45 +116,35 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(2U, stack_size_after_call); a = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(3U, stack_size_after_call); a = -1.1; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(4U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/err/check_not_nan_test.cpp b/test/unit/math/rev/err/check_not_nan_test.cpp index bc6b83bcbf4..820cf12b540 100644 --- a/test/unit/math/rev/err/check_not_nan_test.cpp +++ b/test/unit/math/rev/err/check_not_nan_test.cpp @@ -16,18 +16,14 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -45,18 +41,14 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -106,18 +98,14 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); @@ -132,18 +120,14 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp index f33712a0fe0..b384b023483 100644 --- a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp @@ -50,17 +50,13 @@ TEST(AgradRevErrorHandlingMatrix, checkPosSemiDefiniteMatrixVarCheck) { size_t stack_before_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(9U, stack_before_call); EXPECT_NO_THROW(check_pos_semidefinite("checkPosDefiniteMatrix", "y", y)); size_t stack_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(9U, stack_after_call); } diff --git a/test/unit/math/rev/err/check_positive_finite_test.cpp b/test/unit/math/rev/err/check_positive_finite_test.cpp index 662e7b63065..e8fb8af1a92 100644 --- a/test/unit/math/rev/err/check_positive_finite_test.cpp +++ b/test/unit/math/rev/err/check_positive_finite_test.cpp @@ -67,9 +67,7 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); @@ -77,18 +75,14 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); a[2] = std::numeric_limits::infinity(); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); } @@ -169,18 +163,14 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive_finite(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/err/check_positive_test.cpp b/test/unit/math/rev/err/check_positive_test.cpp index 3ce7752cbc8..dee6535363f 100644 --- a/test/unit/math/rev/err/check_positive_test.cpp +++ b/test/unit/math/rev/err/check_positive_test.cpp @@ -60,18 +60,14 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveVarCheckUnivariate) { size_t stack_size = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive(function, "a", a)); size_t stack_size_after_call = stan::math::ChainableStack::instance_->var_stack_.size() - + - stan::math::ChainableStack::instance_->var_nochain_stack_ - .size(); + + stan::math::ChainableStack::instance_->var_nochain_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); From 05a31665f0a7a6c6c0d4c12fd590da58d52ebe0d Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 24 Jun 2020 19:55:38 -0400 Subject: [PATCH 118/167] small fixes --- stan/math/rev/core/autodiffstackstorage.hpp | 4 ---- stan/math/rev/core/chainable_alloc.hpp | 1 + stan/math/rev/core/chainablestack.hpp | 3 +-- stan/math/rev/core/vari.hpp | 4 +--- stan/math/rev/functor/coupled_ode_system.hpp | 3 ++- test/unit/math/rev/core/var_test.cpp | 1 - 6 files changed, 5 insertions(+), 11 deletions(-) diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 93578cbbc63..fecdab186b5 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_REV_CORE_AUTODIFFSTACKSTORAGE_HPP #include -#include #include namespace stan { @@ -25,9 +24,6 @@ namespace math { #define STAN_THREADS_DEF #endif -template -class vari_value; - /** * This struct always provides access to the autodiff stack using * the singleton pattern. Read warnings below! diff --git a/stan/math/rev/core/chainable_alloc.hpp b/stan/math/rev/core/chainable_alloc.hpp index 6ff43fd0e43..a996173b1d7 100644 --- a/stan/math/rev/core/chainable_alloc.hpp +++ b/stan/math/rev/core/chainable_alloc.hpp @@ -18,6 +18,7 @@ class chainable_alloc { chainable_alloc() { ChainableStack::instance_->var_alloc_stack_.push_back(this); } + virtual ~chainable_alloc() {} }; } // namespace math diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index d383fabdcf9..4785afe1146 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -2,11 +2,10 @@ #define STAN_MATH_REV_CORE_CHAINABLESTACK_HPP #include -#include namespace stan { namespace math { -template +template class vari_value; class vari_base; class chainable_alloc; diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 294cece21bb..6515174ae8b 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -32,6 +32,7 @@ class vari_base { virtual void set_zero_adjoint() = 0; virtual ~vari_base() noexcept {} }; + /** * The variable implementation base class. * @@ -43,9 +44,6 @@ class vari_base { * derivative with respect to the root of the derivative tree. * */ -template -class vari_value; - template class vari_value::value>> : public vari_base { diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 773aebe625e..7ae5f4a1ab6 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -484,7 +485,7 @@ struct coupled_ode_system { // See efficiency note above on template specialization for more details // on this. for (size_t j = 0; j < M_; ++j) { - theta_nochain_[j].vi_->adj_ = 0.0; + fill(theta_nochain_[j].vi_->adj_, 0.0); } } } diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 303d00a3ec6..2b792ac41cd 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include From b25944d54875bdc09a1a978ca86be81ed3cccefb Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 24 Jun 2020 19:56:48 -0400 Subject: [PATCH 119/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/chainable_alloc.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/chainable_alloc.hpp b/stan/math/rev/core/chainable_alloc.hpp index a996173b1d7..3541e88aee6 100644 --- a/stan/math/rev/core/chainable_alloc.hpp +++ b/stan/math/rev/core/chainable_alloc.hpp @@ -18,7 +18,7 @@ class chainable_alloc { chainable_alloc() { ChainableStack::instance_->var_alloc_stack_.push_back(this); } - virtual ~chainable_alloc() {} + virtual ~chainable_alloc() {} }; } // namespace math From ffd30da4949823167fd125db3385cb3b233b9ac0 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 25 Jun 2020 13:38:08 -0400 Subject: [PATCH 120/167] use nested chainable stack class for set zero adjoint --- stan/math/prim/fun/cholesky_decompose.hpp | 8 +++----- stan/math/rev/core/init_chainablestack.hpp | 2 +- stan/math/rev/functor/coupled_ode_system.hpp | 6 +++--- stan/math/rev/functor/jacobian.hpp | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index fe64e168b81..10254c001b6 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -59,8 +59,8 @@ cholesky_decompose(const EigMat& m) { * @throw std::domain_error if m is not a symmetric matrix or * if m is not positive definite (if m has more than 0 elements) */ -template * = nullptr> +template * = nullptr, + require_same_t>* = nullptr> inline Eigen::Matrix, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime> cholesky_decompose(const EigMat& m) { @@ -68,9 +68,7 @@ cholesky_decompose(const EigMat& m) { eval_return_type_t& m_eval = m.eval(); check_not_nan("cholesky_decompose", "m", m_eval); #ifdef STAN_OPENCL - if (std::is_same::value - && m.rows() - >= opencl_context.tuning_opts().cholesky_size_worth_transfer) { + if (m.rows() >= opencl_context.tuning_opts().cholesky_size_worth_transfer) { matrix_cl m_cl(m_eval); return from_matrix_cl(cholesky_decompose(m_cl)); } else { diff --git a/stan/math/rev/core/init_chainablestack.hpp b/stan/math/rev/core/init_chainablestack.hpp index 5df2f797012..2f2de5a4086 100644 --- a/stan/math/rev/core/init_chainablestack.hpp +++ b/stan/math/rev/core/init_chainablestack.hpp @@ -43,7 +43,7 @@ class ad_tape_observer final : public tbb::task_scheduler_observer { bool status = false; std::tie(insert_elem, status) = thread_tape_map_.emplace(ad_map::value_type{thread_id, nullptr}); - insert_elem->second = std::move(std::make_unique()); + insert_elem->second = std::make_unique(); } } diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 7ae5f4a1ab6..b6e0e4dbdd9 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -144,7 +144,7 @@ struct coupled_ode_system { dz_dt[offset + i] = temp_deriv; } - set_zero_all_adjoints_nested(); + nested.set_zero_all_adjoints(); // Parameters stored on the outer (non-nested) nochain stack are not // reset to zero by the last call. This is done as a separate step here. // See efficiency note above on template specialization for more details @@ -297,7 +297,7 @@ struct coupled_ode_system { dz_dt[offset + i] = temp_deriv; } - set_zero_all_adjoints_nested(); + nested.set_zero_all_adjoints(); } } @@ -479,7 +479,7 @@ struct coupled_ode_system { dz_dt[offset + i] = temp_deriv; } - set_zero_all_adjoints_nested(); + nested.set_zero_all_adjoints(); // Parameters stored on the outer (non-nested) nochain stack are not // reset to zero by the last call. This is done as a separate step here. // See efficiency note above on template specialization for more details diff --git a/stan/math/rev/functor/jacobian.hpp b/stan/math/rev/functor/jacobian.hpp index c388a8bcccb..75692b06911 100644 --- a/stan/math/rev/functor/jacobian.hpp +++ b/stan/math/rev/functor/jacobian.hpp @@ -27,7 +27,7 @@ void jacobian(const F& f, const Eigen::Matrix& x, grad(fx_var(0).vi_); J.col(0) = x_var.adj(); for (int i = 1; i < fx_var.size(); ++i) { - set_zero_all_adjoints_nested(); + nested.set_zero_all_adjoints(); grad(fx_var(i).vi_); J.col(i) = x_var.adj(); } From e5428e9d6c50154ed2552cba0809dd1ed93ebb29 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 25 Jun 2020 13:38:59 -0400 Subject: [PATCH 121/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/fun/cholesky_decompose.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index 10254c001b6..d3a6ca627a0 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -60,7 +60,7 @@ cholesky_decompose(const EigMat& m) { * if m is not positive definite (if m has more than 0 elements) */ template * = nullptr, - require_same_t>* = nullptr> + require_same_t>* = nullptr> inline Eigen::Matrix, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime> cholesky_decompose(const EigMat& m) { From 356bb4ebdad0a4a0f012c90148b2c9a32ed32b61 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 26 Jun 2020 19:56:45 -0400 Subject: [PATCH 122/167] merge to vari-base-templates and add sparse matrix var type --- stan/math/prim/meta/is_eigen_sparse_base.hpp | 2 +- stan/math/rev/core/var.hpp | 9 +- stan/math/rev/core/vari.hpp | 207 +++++++++++++++++-- test/unit/math/rev/core/var_test.cpp | 105 ++++++++-- 4 files changed, 279 insertions(+), 44 deletions(-) diff --git a/stan/math/prim/meta/is_eigen_sparse_base.hpp b/stan/math/prim/meta/is_eigen_sparse_base.hpp index 0628b3dce66..67d48396203 100644 --- a/stan/math/prim/meta/is_eigen_sparse_base.hpp +++ b/stan/math/prim/meta/is_eigen_sparse_base.hpp @@ -19,7 +19,7 @@ namespace stan { */ template struct is_eigen_sparse_base - : bool_constant::value> { + : bool_constant::value> { }; STAN_ADD_REQUIRE_UNARY(eigen_sparse_base, is_eigen_sparse_base, diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index da6cb4128ed..e704b796e4b 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -36,7 +36,7 @@ class var_value {}; template class var_value> { public: - using value_type = std::decay_t; // Numeric type in vari_value. + using value_type = std::decay_t>; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. using vari_pointer = vari_type*; // pointer type for underlying vari. @@ -77,14 +77,15 @@ class var_value> { * @param x Value of the variable. */ template * = nullptr> - var_value(const S& x) : vi_(new vari_type(x, false)) {} // NOLINT + var_value(S&& x) : vi_(new vari_type(std::forward(x), false)) {} // NOLINT /** * Construct a variable from a pointer to a variable implementation. * @param vi A vari_value pointer. */ - var_value(vari_value* vi) // NOLINT - : vi_(vi) {} + template * = nullptr> + var_value(vari_value* vi) // NOLINT + : vi_(vi) {} /** * Return a constant reference to the value of this variable. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 7461682934c..6868ab941ab 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -189,24 +189,25 @@ using vari = vari_value; template -class vari_value::value>> { +class vari_value::value>> : public vari_base { private: template friend class var_value; - using eigen_scalar = value_type_t; - value_type_t* val_mem_; - value_type_t* adj_mem_; - + template + friend class vari_value; public: - using Scalar = T; - using value_type = T; - using PlainObject = typename T::PlainObject; + using PlainObject = typename std::decay_t::PlainObject; + using eigen_scalar = value_type_t; + value_type_t* val_mem_; + value_type_t* adj_mem_; + using Scalar = std::decay_t; + using value_type = Scalar; using eigen_map = Eigen::Map; const Eigen::Index rows_; const Eigen::Index cols_; const Eigen::Index size_; - const Eigen::Index RowsAtCompileTime = T::RowsAtCompileTime; - const Eigen::Index ColsAtCompileTime = T::ColsAtCompileTime; + const Eigen::Index RowsAtCompileTime = Scalar::RowsAtCompileTime; + const Eigen::Index ColsAtCompileTime = Scalar::ColsAtCompileTime; /** * The value of this variable. */ @@ -230,7 +231,8 @@ class vari_value::value>> { * * @param x Value of the constructed variable. */ - explicit vari_value(const T& x) + template * = nullptr> + explicit vari_value(S&& x) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( @@ -244,8 +246,8 @@ class vari_value::value>> { adj_.setZero(); ChainableStack::instance_->var_stack_.push_back(this); } - - vari_value(const T& x, bool stacked) + template * = nullptr> + vari_value(S&& x, bool stacked) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( @@ -325,15 +327,180 @@ class vari_value::value>> { } }; -struct vari_printer { - std::ostream& o_; - int i_{0}; - vari_printer(std::ostream& o, int i) : o_(o), i_(i) {} - template - inline void operator()(T*& x) const { - o_ << i_ << " " << x << " " << x->val_ << " : " << x->adj_ << std::endl; +template +class vari_value::value>> : public vari_base { + private: + template + friend class var_value; + public: + using PlainObject = std::decay_t::PlainObject>; + using eigen_scalar = value_type_t; + using eigen_index = typename std::decay_t::StorageIndex; + using Scalar = std::decay_t; + using value_type = Scalar; + using eigen_map = Eigen::Map; + eigen_scalar* val_mem_; + eigen_index* val_outer_index_; + eigen_index* val_inner_index_; + eigen_scalar* adj_mem_; + eigen_index* adj_outer_index_; + eigen_index* adj_inner_index_; + const Eigen::Index rows_; + const Eigen::Index cols_; + const Eigen::Index size_; + const Eigen::Index RowsAtCompileTime = T::RowsAtCompileTime; + const Eigen::Index ColsAtCompileTime = T::ColsAtCompileTime; + /** + * The value of this variable. + */ + eigen_map val_; + + /** + * The adjoint of this variable, which is the partial derivative + * of this variable with respect to the root variable. + */ + eigen_map adj_; + + /** + * Construct a variable implementation from a value. The + * adjoint is initialized to zero. + * + * All constructed variables are added to the stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @param x Value of the constructed variable. + */ + template * = nullptr> + explicit vari_value(S&& x) + : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.nonZeros())), + val_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + val_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.nonZeros())), + adj_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + adj_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + rows_(x.rows()), + cols_(x.cols()), + size_(x.size()), + val_(x.rows(), x.cols(), x.nonZeros(), val_outer_index_, val_inner_index_, val_mem_), + adj_(x.rows(), x.cols(), x.nonZeros(), adj_outer_index_, adj_inner_index_, adj_mem_) { + val_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(),x.valuePtr()); + adj_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(), adj_mem_); + for (int k = 0; k < adj_.outerSize(); ++k) { + for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { + it.valueRef() = 0.0; + } + } + ChainableStack::instance_->var_stack_.push_back(this); + } + template * = nullptr> + vari_value(S&& x, bool stacked) + : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.nonZeros())), + val_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + val_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.nonZeros())), + adj_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + adj_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + rows_(x.rows()), + cols_(x.cols()), + size_(x.size()), + val_(x.rows(), x.cols(), x.nonZeros(), val_outer_index_, val_inner_index_, val_mem_), + adj_(x.rows(), x.cols(), x.nonZeros(), adj_outer_index_, adj_inner_index_, adj_mem_) { + val_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(), x.valuePtr()); + adj_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(), adj_mem_); + for (int k = 0; k < adj_.outerSize(); ++k) { + for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { + it.valueRef() = 0.0; + } + } + if (stacked) { + ChainableStack::instance_->var_stack_.push_back(this); + } else { + ChainableStack::instance_->var_nochain_stack_.push_back(this); + } + } + + const Eigen::Index rows() const { return rows_; } + const Eigen::Index cols() const { return cols_; } + const Eigen::Index size() const { return size_; } + void chain() {} + /** + * Initialize the adjoint for this (dependent) variable to 1. + * This operation is applied to the dependent variable before + * propagating derivatives, setting the derivative of the + * result with respect to itself to be 1. + */ + inline void init_dependent() { adj_.setOnes(); } + + /** + * Set the adjoint value of this variable to 0. This is used to + * reset adjoints before propagating derivatives again (for + * example in a Jacobian calculation). + */ + inline void set_zero_adjoint() noexcept final { + for (int k = 0; k < adj_.outerSize(); ++k) { + for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { + it.valueRef() = 0.0; + } + } + } + + /** + * Insertion operator for vari. Prints the current value and + * the adjoint value. + * + * @param os [in, out] ostream to modify + * @param v [in] vari object to print. + * + * @return The modified ostream. + */ + friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { + return os << v->val_ << ":" << v->adj_; + } + + /** + * Allocate memory from the underlying memory pool. This memory is + * is managed as a whole externally. + * + * Warning: Classes should not be allocated with this operator + * if they have non-trivial destructors. + * + * @param nbytes Number of bytes to allocate. + * @return Pointer to allocated bytes. + */ + static inline void* operator new(size_t nbytes) { + return ChainableStack::instance_->memalloc_.alloc(nbytes); + } + + /** + * Delete a pointer from the underlying memory pool. + * + * This no-op implementation enables a subclass to throw + * exceptions in its constructor. An exception thrown in the + * constructor of a subclass will result in an error being + * raised, which is in turn caught and calls delete(). + * + * See the discussion of "plugging the memory leak" in: + * http://www.parashift.com/c++-faq/memory-pools.html + */ + static inline void operator delete(void* /* ignore arg */) { /* no op */ } }; + } // namespace math } // namespace stan #endif diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 2b792ac41cd..14f22cd2773 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -15,7 +16,7 @@ struct AgradRev : public testing::Test { }; template -void ctor_overloads_impl() { +void ctor_overloads_float_impl() { using stan::math::var_value; using stan::math::vari_value; using stan::math::test::type_name; @@ -39,28 +40,94 @@ void ctor_overloads_impl() { } template -void ctor_overloads() { - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); - ctor_overloads_impl(); +void ctor_overloads_float() { + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); + ctor_overloads_float_impl(); } -TEST_F(AgradRev, ctorOverloads) { - ctor_overloads(); - ctor_overloads(); - ctor_overloads(); +TEST_F(AgradRev, ctorfloatOverloads) { + ctor_overloads_float(); + ctor_overloads_float(); + ctor_overloads_float(); } +template +void ctor_overloads_matrix(EigenMat&& xx) { + using stan::math::var_value; + using stan::math::vari_value; + using stan::math::test::type_name; + using plain_type = std::decay_t>; + // standard constructor + plain_type x = xx; + EXPECT_MATRIX_FLOAT_EQ(x, var_value(x).val()); + // make sure copy ctor is used rather than casting vari* to unsigned int + EXPECT_MATRIX_FLOAT_EQ(x, var_value(new vari_value(x)).val()); + // make sure rvalue var_value can be accepted + EXPECT_MATRIX_FLOAT_EQ(x, var_value(var_value(x)).val()); +} + +template +void ctor_overloads_sparse_matrix(EigenMat&& x) { + using stan::math::var_value; + using stan::math::vari_value; + using stan::math::test::type_name; + // standard constructor + std::decay_t xx = var_value>(x).val(); + for (int k = 0; k < x.outerSize(); ++k) { + for (typename std::decay_t::InnerIterator it(x,k), iz(xx,k); + it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + } + } + std::decay_t xxx = var_value>(var_value>(x)).val(); + for (int k = 0; k < x.outerSize(); ++k) { + for (typename std::decay_t::InnerIterator it(x,k), iz(xxx,k); + it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + std::cout << "iz[" << iz.index() << "]: " << iz.value() << " it[" << it.index() << "]: " << it.value() << std::endl; + } + } + +} + +auto make_sparse_matrix_random(int rows, int cols) { +using eigen_triplet = Eigen::Triplet; +boost::mt19937 gen; +boost::random::uniform_real_distribution dist(0.0,1.0); +std::vector tripletList; + for(int i = 0; i < rows; ++i) { + for(int j = 0; j < cols; ++j) { + auto v_ij = dist(gen); //generate random number + if(v_ij < 0.1) { + tripletList.push_back(eigen_triplet(i,j,v_ij)); //if larger than treshold, insert it + } + } + } + Eigen::SparseMatrix mat(rows, cols); + mat.setFromTriplets(tripletList.begin(), tripletList.end()); + return mat; +} + +TEST_F(AgradRev, ctormatrixOverloads) { + using dense_mat = Eigen::Matrix; + using sparse_mat = Eigen::SparseMatrix; + ctor_overloads_matrix(dense_mat::Random(2, 2)); + sparse_mat sparse_x = make_sparse_matrix_random(2, 2); + ctor_overloads_sparse_matrix(sparse_x); +} + + TEST_F(AgradRev, a_eq_x) { AVAR a = 5.0; EXPECT_FLOAT_EQ(5.0, a.val()); From 2130cfed2b45ba4a2974402b369182b6d704e4f9 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 28 Jun 2020 18:51:19 -0400 Subject: [PATCH 123/167] adds sparse and dense vari_value specializations --- stan/math/prim/fun/MatrixExponential.h | 2 +- stan/math/prim/meta/is_eigen_sparse_base.hpp | 4 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 3 +- stan/math/rev/core/var.hpp | 23 +- stan/math/rev/core/vari.hpp | 376 ++++++++++++------ test/unit/math/rev/core/var_test.cpp | 96 +++-- 6 files changed, 344 insertions(+), 160 deletions(-) diff --git a/stan/math/prim/fun/MatrixExponential.h b/stan/math/prim/fun/MatrixExponential.h index 85e9f8274fa..74ae2a47ee2 100644 --- a/stan/math/prim/fun/MatrixExponential.h +++ b/stan/math/prim/fun/MatrixExponential.h @@ -185,7 +185,7 @@ namespace Eigen { { template static void run(const MatrixType& arg, MatrixType& U, MatrixType& V, int& squarings, - T scalar_type) + T scalar_type) { using std::frexp; using std::pow; diff --git a/stan/math/prim/meta/is_eigen_sparse_base.hpp b/stan/math/prim/meta/is_eigen_sparse_base.hpp index 67d48396203..b6eee02a4a7 100644 --- a/stan/math/prim/meta/is_eigen_sparse_base.hpp +++ b/stan/math/prim/meta/is_eigen_sparse_base.hpp @@ -19,8 +19,8 @@ namespace stan { */ template struct is_eigen_sparse_base - : bool_constant::value> { -}; + : bool_constant< + is_base_pointer_convertible::value> {}; STAN_ADD_REQUIRE_UNARY(eigen_sparse_base, is_eigen_sparse_base, require_eigens_types); diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 070b718e82a..d81f5de557c 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -23,7 +23,8 @@ static void set_zero_all_adjoints_nested() { "empty_nested() must be false before calling" " set_zero_all_adjoints_nested()"); } - const size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); + const size_t start1 = + ChainableStack::instance_->nested_var_stack_sizes_.back(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < ChainableStack::instance_->var_stack_.size(); ++i) { diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index e704b796e4b..32d772293dc 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -36,7 +36,7 @@ class var_value {}; template class var_value> { public: - using value_type = std::decay_t>; // type in vari_value. + using value_type = std::decay_t>; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. using vari_pointer = vari_type*; // pointer type for underlying vari. @@ -76,16 +76,33 @@ class var_value> { * @tparam S A type that is convertible to `value_type`. * @param x Value of the variable. */ - template * = nullptr> + template * = nullptr, + require_not_eigen_sparse_base_t* = nullptr> var_value(S&& x) : vi_(new vari_type(std::forward(x), false)) {} // NOLINT + /** + * Construct a variable from the specified floating point argument + * by constructing a new `vari_value`. This constructor is only + * valid when `S` is convertible to this `vari_value`'s `value_type` and is + * derived from Eigen's sparse matrix base class. This specialization exists + * because Eigen sparse matrix expressions do not have `innerSize()` and + * `outerSize()` members. While dense matrix expressions can be passed along + * to the constructor for `vari_value`, sparse matrix expressions need to + * be evaluated before they are passed to the `vari_value` constructor. + * @tparam S A type that is convertible to `value_type`. + * @param x Value of the variable. + */ + template * = nullptr, + require_eigen_sparse_base_t* = nullptr> + var_value(S&& x) : vi_(new vari_type(ref_type_t(x), false)) {} // NOLINT + /** * Construct a variable from a pointer to a variable implementation. * @param vi A vari_value pointer. */ template * = nullptr> var_value(vari_value* vi) // NOLINT - : vi_(vi) {} + : vi_(vi) {} /** * Return a constant reference to the value of this variable. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 6868ab941ab..c69ce0af278 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -36,7 +36,7 @@ class vari_base { template class vari_value; /** - * The variable implementation base class. + * The variable implementation for floating point types. * * This class is complete (not abstract) and may be used for * constants. @@ -186,32 +186,42 @@ class vari_value::value>> // For backwards compatability the default is double using vari = vari_value; - - +/** + * The variable implementation for Eigen dense matrix types. + * + * This class is complete (not abstract) and may be used for + * constants. + * + * A variable implementation is constructed with a constant + * value. It also stores the adjoint for storing the partial + * derivative with respect to the root of the derivative tree. + * + */ template -class vari_value::value>> : public vari_base { - private: - template - friend class var_value; - template - friend class vari_value; +class vari_value::value>> + : public vari_base { public: - using PlainObject = typename std::decay_t::PlainObject; - using eigen_scalar = value_type_t; - value_type_t* val_mem_; - value_type_t* adj_mem_; - using Scalar = std::decay_t; - using value_type = Scalar; - using eigen_map = Eigen::Map; - const Eigen::Index rows_; - const Eigen::Index cols_; - const Eigen::Index size_; - const Eigen::Index RowsAtCompileTime = Scalar::RowsAtCompileTime; - const Eigen::Index ColsAtCompileTime = Scalar::ColsAtCompileTime; /** - * The value of this variable. + * `PlainObject` represents a user constructible type such as Matrix or Array + */ + using PlainObject = std::decay_t>; + using eigen_scalar = value_type_t; // A floating point type + eigen_scalar* val_mem_; // Pointer to memory allocated on the stack for val_ + eigen_scalar* adj_mem_; // Pointer to memory allocated on the stack for adj_ + using Scalar = PlainObject; // The underlying type for this class + using value_type = Scalar; // The underlying type for this class + using eigen_map = Eigen::Map; // Maps for adj_ and val_ + const Eigen::Index rows_; // Number of rows + const Eigen::Index cols_; // Number of cols + const Eigen::Index size_; // Size of rows * cols + /** + * Number of rows known at compile time */ - eigen_map val_; + static constexpr Eigen::Index RowsAtCompileTime = Scalar::RowsAtCompileTime; + /** + * Number of columns known at compile time + */ + static constexpr Eigen::Index ColsAtCompileTime = Scalar::ColsAtCompileTime; /** * The adjoint of this variable, which is the partial derivative @@ -220,45 +230,65 @@ class vari_value::value>> : public va eigen_map adj_; /** - * Construct a variable implementation from a value. The + * The value of this variable. + */ + const eigen_map val_; + + /** + * Construct a dense Eigen variable implementation from a value. The * adjoint is initialized to zero. * - * All constructed variables are added to the stack. Variables + * All constructed variables are added to the stack. Variables * should be constructed before variables on which they depend * to insure proper partial derivative propagation. During * derivative propagation, the chain() method of each variable * will be called in the reverse order of construction. * + * @tparam S A dense Eigen type that is convertible to `value_type` * @param x Value of the constructed variable. */ - template * = nullptr> + template * = nullptr> explicit vari_value(S&& x) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.size())), + x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), rows_(x.rows()), cols_(x.cols()), size_(x.size()), - val_(val_mem_, x.rows(), x.cols()), - adj_(adj_mem_, x.rows(), x.cols()) { - val_ = x; - adj_.setZero(); + adj_(make_adj(adj_mem_, x)), + val_(make_val(val_mem_, std::forward(x))) { ChainableStack::instance_->var_stack_.push_back(this); } + + /** + * Construct an dense Eigen variable implementation from a value. The + * adjoint is initialized to zero and if `stacked` is `false` this vari + * will be not be put on the var_stack. Instead it will only be put on + * a stack to keep track of whether the adjoint needs to be set to zero. + * + * All constructed variables are added to a stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @tparam S A dense Eigen type that is convertible to `value_type` + * @param x Value of the constructed variable. + * @param stacked If false will put this this vari on the nochain stack so + * that its `chain()` method is not called. + */ template * = nullptr> vari_value(S&& x, bool stacked) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.size())), + x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), rows_(x.rows()), cols_(x.cols()), size_(x.size()), - val_(val_mem_, x.rows(), x.cols()), - adj_(adj_mem_, x.rows(), x.cols()) { - val_ = x; - adj_.setZero(); + adj_(make_adj(adj_mem_, x)), + val_(make_val(val_mem_, x)) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -266,9 +296,19 @@ class vari_value::value>> : public va } } + /** + * Return the number of rows for this class's `val_` member + */ const Eigen::Index rows() const { return rows_; } + /** + * Return the number of columns for this class's `val_` member + */ const Eigen::Index cols() const { return cols_; } + /** + * Return the size of this class's `val_` member + */ const Eigen::Index size() const { return size_; } + virtual void chain() {} /** * Initialize the adjoint for this (dependent) variable to 1. @@ -283,7 +323,7 @@ class vari_value::value>> : public va * reset adjoints before propagating derivatives again (for * example in a Jacobian calculation). */ - void set_zero_adjoint() { adj_.setZero(); } + inline void set_zero_adjoint() final { adj_.setZero(); } /** * Insertion operator for vari. Prints the current value and @@ -325,35 +365,77 @@ class vari_value::value>> : public va */ static inline void operator delete(void* /* ignore arg */) { /* no op */ } -}; -template -class vari_value::value>> : public vari_base { private: template friend class var_value; + /** + * Create the map to the val_ stack allocated memory for an Eigen input. + * @tparam S an Eigen type. + * @param mem A pointer to stack allocated memory. + * @param x The Eigen type whose values will be assigned to `val_` + */ + template + inline eigen_map make_val(eigen_scalar*& mem, S&& x) { + eigen_map(mem, x.rows(), x.cols()) = std::forward(x); + return eigen_map(mem, x.rows(), x.cols()); + } + /** + * Create the map to the adj_ stack allocated memory for an Eigen input. + * @tparam S an Eigen type. + * @param mem A pointer to stack allocated memory. + * @param x The Eigen type whose dimensions are set the `adj_`s dimensions. + */ + template + inline eigen_map make_adj(eigen_scalar*& mem, S&& x) { + eigen_map(mem, x.rows(), x.cols()).setZero(); + return eigen_map(mem, x.rows(), x.cols()); + } +}; + +/** + * The variable implementation for Eigen sparse matrix types. + * + * This class is complete (not abstract) and may be used for + * constants. + * + * A variable implementation is constructed with a constant + * value. It also stores the adjoint for storing the partial + * derivative with respect to the root of the derivative tree. + * + */ +template +class vari_value::value>> + : public vari_base { public: - using PlainObject = std::decay_t::PlainObject>; - using eigen_scalar = value_type_t; - using eigen_index = typename std::decay_t::StorageIndex; - using Scalar = std::decay_t; - using value_type = Scalar; - using eigen_map = Eigen::Map; - eigen_scalar* val_mem_; - eigen_index* val_outer_index_; - eigen_index* val_inner_index_; - eigen_scalar* adj_mem_; - eigen_index* adj_outer_index_; - eigen_index* adj_inner_index_; - const Eigen::Index rows_; - const Eigen::Index cols_; - const Eigen::Index size_; - const Eigen::Index RowsAtCompileTime = T::RowsAtCompileTime; - const Eigen::Index ColsAtCompileTime = T::ColsAtCompileTime; + using PlainObject + = std::decay_t>; // Base type of Eigen class + using eigen_scalar = value_type_t; // Scalar type of Eigen class + using eigen_index = typename PlainObject::StorageIndex; // Index type + using Scalar = PlainObject; // vari's adj_ and val_ member type + using value_type = Scalar; // vari's adj_ and val_ member type + using eigen_map = Eigen::Map; // Map of stack alloc'd mem + eigen_scalar* val_mem_; // The value's nonzero values + eigen_index* val_outer_index_; // Value's Outer index of nonzero members + eigen_index* val_inner_index_; // Value's Inner index of nonzero members + eigen_scalar* adj_mem_; // Adjoint's nonzero values + eigen_index* adj_outer_index_; // Adjoint's outer index of nonzero members + eigen_index* adj_inner_index_; // Adjont's inner index of nonzero members + const Eigen::Index rows_; // Number of rows in val_ + const Eigen::Index cols_; // Number of cols in val_ + const Eigen::Index size_; // Size of val_ + /** + * Rows at compile time + */ + static constexpr Eigen::Index RowsAtCompileTime = T::RowsAtCompileTime; + /** + * Columns at compile time + */ + static constexpr Eigen::Index ColsAtCompileTime = T::ColsAtCompileTime; /** * The value of this variable. */ - eigen_map val_; + const eigen_map val_; /** * The adjoint of this variable, which is the partial derivative @@ -362,71 +444,84 @@ class vari_value::value>> : public v eigen_map adj_; /** - * Construct a variable implementation from a value. The + * Construct a variable implementation from a value. The * adjoint is initialized to zero. * - * All constructed variables are added to the stack. Variables - * should be constructed before variables on which they depend + * All constructed variables are added to the stack. For a sparse eigen matrix + * this includes the nozero values as well the inner and outer indices. + * Variables should be constructed before variables on which they depend * to insure proper partial derivative propagation. During * derivative propagation, the chain() method of each variable * will be called in the reverse order of construction. * + * @tparam S A sparse Eigen type that is convertible to `value_type` * @param x Value of the constructed variable. */ - template * = nullptr> + template * = nullptr> explicit vari_value(S&& x) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.nonZeros())), - val_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - val_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.nonZeros())), - adj_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - adj_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), + x.nonZeros())), + val_outer_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + val_inner_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + adj_outer_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + adj_inner_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), rows_(x.rows()), cols_(x.cols()), size_(x.size()), - val_(x.rows(), x.cols(), x.nonZeros(), val_outer_index_, val_inner_index_, val_mem_), - adj_(x.rows(), x.cols(), x.nonZeros(), adj_outer_index_, adj_inner_index_, adj_mem_) { - val_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(),x.valuePtr()); - adj_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(), adj_mem_); - for (int k = 0; k < adj_.outerSize(); ++k) { - for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { - it.valueRef() = 0.0; - } - } + val_(make_val(val_outer_index_, val_inner_index_, val_mem_, x)), + adj_(make_adj(adj_outer_index_, adj_inner_index_, adj_mem_, x)) { ChainableStack::instance_->var_stack_.push_back(this); } + /** + * Construct an sparse Eigen variable implementation from a value. The + * adjoint is initialized to zero and if `stacked` is `false` this vari + * will be not be put on the var_stack. Instead it will only be put on + * a stack to keep track of whether the adjoint needs to be set to zero. + * + * All constructed variables are added to a stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @tparam S A sparse Eigen type that is convertible to `value_type` + * @param x Value of the constructed variable. + * @param stacked If false will put this this vari on the nochain stack so + * that its `chain()` method is not called. + */ template * = nullptr> vari_value(S&& x, bool stacked) - : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.nonZeros())), - val_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - val_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.nonZeros())), - adj_outer_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - adj_inner_index_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - val_(x.rows(), x.cols(), x.nonZeros(), val_outer_index_, val_inner_index_, val_mem_), - adj_(x.rows(), x.cols(), x.nonZeros(), adj_outer_index_, adj_inner_index_, adj_mem_) { - val_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(), x.valuePtr()); - adj_ = Eigen::Map(x.rows(), x.cols(), x.nonZeros(), x.outerIndexPtr(), x.innerIndexPtr(), adj_mem_); - for (int k = 0; k < adj_.outerSize(); ++k) { - for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { - it.valueRef() = 0.0; - } - } + : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + val_outer_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + val_inner_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + adj_outer_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.outerSize())), + adj_inner_index_( + ChainableStack::instance_->memalloc_.alloc_array( + x.innerSize())), + rows_(x.rows()), + cols_(x.cols()), + size_(x.size()), + val_(make_val(val_outer_index_, val_inner_index_, val_mem_, x)), + adj_(make_adj(adj_outer_index_, adj_inner_index_, adj_mem_, x)) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -434,8 +529,17 @@ class vari_value::value>> : public v } } + /** + * Return the number of rows for this class's `val_` member + */ const Eigen::Index rows() const { return rows_; } + /** + * Return the number of columns for this class's `val_` member + */ const Eigen::Index cols() const { return cols_; } + /** + * Return the size of this class's `val_` member + */ const Eigen::Index size() const { return size_; } void chain() {} /** @@ -452,12 +556,12 @@ class vari_value::value>> : public v * example in a Jacobian calculation). */ inline void set_zero_adjoint() noexcept final { - for (int k = 0; k < adj_.outerSize(); ++k) { - for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { - it.valueRef() = 0.0; - } - } - } + for (int k = 0; k < adj_.outerSize(); ++k) { + for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { + it.valueRef() = 0.0; + } + } + } /** * Insertion operator for vari. Prints the current value and @@ -499,6 +603,54 @@ class vari_value::value>> : public v */ static inline void operator delete(void* /* ignore arg */) { /* no op */ } + + private: + template + friend class var_value; + /** + * Fill the val_ map and it's pointers. + * @tparam S An Eigen sparse matrix type. + * @param outer_mem Stack allocated memory for the outer index. + * @param inner_mem Stack allocated memory for the inner index. + * @param val_mem Stack allocated memory for the matrix values. + * @note This function exists because Eigen's `Map` class for sparse matrices + * has a lot of gotchas. + */ + template + inline eigen_map make_val(eigen_index*& outer_mem, eigen_index*& inner_mem, + eigen_scalar*& val_mem, S&& x) { + for (int k = 0; k < x.innerSize(); ++k) { + val_mem[k] = x.valuePtr()[k]; + inner_mem[k] = x.innerIndexPtr()[k]; + } + for (int k = 0; k < x.outerSize(); ++k) { + outer_mem[k] = x.outerIndexPtr()[k]; + } + return eigen_map(x.rows(), x.cols(), x.nonZeros(), outer_mem, inner_mem, + val_mem); + } + /** + * Fill the val_ map and it's pointers. + * @tparam S An Eigen sparse matrix type. + * @param outer_mem Stack allocated memory for the outer index. + * @param inner_mem Stack allocated memory for the inner index. + * @param val_mem Stack allocated memory for the matrix values. + * @note This function exists because Eigen's `Map` class for sparse matrices + * has a lot of gotchas. + */ + template + inline eigen_map make_adj(eigen_index*& outer_mem, eigen_index*& inner_mem, + eigen_scalar*& val_mem, S&& x) { + for (int k = 0; k < x.innerSize(); ++k) { + val_mem[k] = 0.0; + inner_mem[k] = x.innerIndexPtr()[k]; + } + for (int k = 0; k < x.outerSize(); ++k) { + outer_mem[k] = x.outerIndexPtr()[k]; + } + return eigen_map(x.rows(), x.cols(), x.nonZeros(), outer_mem, inner_mem, + val_mem); + } }; } // namespace math diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 14f22cd2773..ee9064fea4a 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -67,14 +67,16 @@ void ctor_overloads_matrix(EigenMat&& xx) { using stan::math::var_value; using stan::math::vari_value; using stan::math::test::type_name; - using plain_type = std::decay_t>; + using eigen_plain = std::decay_t>; + eigen_plain x = xx; // standard constructor - plain_type x = xx; - EXPECT_MATRIX_FLOAT_EQ(x, var_value(x).val()); + EXPECT_MATRIX_FLOAT_EQ((x * x).eval(), var_value(x * x).val()); // make sure copy ctor is used rather than casting vari* to unsigned int - EXPECT_MATRIX_FLOAT_EQ(x, var_value(new vari_value(x)).val()); + EXPECT_MATRIX_FLOAT_EQ( + x, var_value(new vari_value(x)).val()); // make sure rvalue var_value can be accepted - EXPECT_MATRIX_FLOAT_EQ(x, var_value(var_value(x)).val()); + EXPECT_MATRIX_FLOAT_EQ( + x, var_value(var_value(x)).val()); } template @@ -82,52 +84,64 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { using stan::math::var_value; using stan::math::vari_value; using stan::math::test::type_name; - // standard constructor - std::decay_t xx = var_value>(x).val(); - for (int k = 0; k < x.outerSize(); ++k) { - for (typename std::decay_t::InnerIterator it(x,k), iz(xx,k); - it; ++it, ++iz) { - EXPECT_FLOAT_EQ(iz.value(), it.value()); - } - } - std::decay_t xxx = var_value>(var_value>(x)).val(); - for (int k = 0; k < x.outerSize(); ++k) { - for (typename std::decay_t::InnerIterator it(x,k), iz(xxx,k); - it; ++it, ++iz) { - EXPECT_FLOAT_EQ(iz.value(), it.value()); - std::cout << "iz[" << iz.index() << "]: " << iz.value() << " it[" << it.index() << "]: " << it.value() << std::endl; - } - } + using eigen_plain = std::decay_t>; + using inner_iterator = typename eigen_plain::InnerIterator; + // standard constructor with eigen expression + eigen_plain matmul_x = x * x; + eigen_plain matmul_xx = var_value(x * x).val(); + for (int k = 0; k < matmul_x.outerSize(); ++k) { + for (inner_iterator it(matmul_x, k), iz(matmul_xx, k); it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + } + } + // make sure rvalue var_value can be accepted + eigen_plain x_rv = var_value(var_value(x)).val(); + for (int k = 0; k < x.outerSize(); ++k) { + for (inner_iterator it(x, k), iz(x_rv, k); it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + } + } + /** from a vari_value with sparse + * NOTE: This will fail with an expression since allocating the mem + * for the vari requires knowing the sizes of the inner and outer indices + * which is not computed for expressions. + */ + eigen_plain x_from_vari + = var_value(new vari_value(x)).val(); + for (int k = 0; k < x.outerSize(); ++k) { + for (inner_iterator it(x, k), iz(x_from_vari, k); it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + } + } } auto make_sparse_matrix_random(int rows, int cols) { -using eigen_triplet = Eigen::Triplet; -boost::mt19937 gen; -boost::random::uniform_real_distribution dist(0.0,1.0); -std::vector tripletList; - for(int i = 0; i < rows; ++i) { - for(int j = 0; j < cols; ++j) { - auto v_ij = dist(gen); //generate random number - if(v_ij < 0.1) { - tripletList.push_back(eigen_triplet(i,j,v_ij)); //if larger than treshold, insert it - } + using eigen_triplet = Eigen::Triplet; + boost::mt19937 gen; + boost::random::uniform_real_distribution dist(0.0, 1.0); + std::vector tripletList; + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + auto v_ij = dist(gen); + if (v_ij < 0.1) { + tripletList.push_back(eigen_triplet(i, j, v_ij)); + } } - } - Eigen::SparseMatrix mat(rows, cols); - mat.setFromTriplets(tripletList.begin(), tripletList.end()); - return mat; + } + Eigen::SparseMatrix mat(rows, cols); + mat.setFromTriplets(tripletList.begin(), tripletList.end()); + return mat; } TEST_F(AgradRev, ctormatrixOverloads) { - using dense_mat = Eigen::Matrix; - using sparse_mat = Eigen::SparseMatrix; - ctor_overloads_matrix(dense_mat::Random(2, 2)); - sparse_mat sparse_x = make_sparse_matrix_random(2, 2); - ctor_overloads_sparse_matrix(sparse_x); + using dense_mat = Eigen::Matrix; + using sparse_mat = Eigen::SparseMatrix; + ctor_overloads_matrix(dense_mat::Random(10, 10)); + sparse_mat sparse_x = make_sparse_matrix_random(10, 10); + ctor_overloads_sparse_matrix(sparse_x); } - TEST_F(AgradRev, a_eq_x) { AVAR a = 5.0; EXPECT_FLOAT_EQ(5.0, a.val()); From 6f1ce8a5946905eae9b869878c66c246bfb031c0 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 28 Jun 2020 19:06:20 -0400 Subject: [PATCH 124/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/meta/is_eigen_dense_base.hpp | 3 +-- stan/math/rev/core/set_zero_all_adjoints_nested.hpp | 4 ++-- stan/math/rev/core/vari.hpp | 12 ++++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/stan/math/prim/meta/is_eigen_dense_base.hpp b/stan/math/prim/meta/is_eigen_dense_base.hpp index d838d83af90..225f833b45c 100644 --- a/stan/math/prim/meta/is_eigen_dense_base.hpp +++ b/stan/math/prim/meta/is_eigen_dense_base.hpp @@ -19,8 +19,7 @@ namespace stan { */ template struct is_eigen_dense_base - : bool_constant::value> { -}; + : bool_constant::value> {}; STAN_ADD_REQUIRE_UNARY(eigen_dense_base, is_eigen_dense_base, require_eigens_types); diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index d81f5de557c..22c74b5ccc8 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -23,8 +23,8 @@ static void set_zero_all_adjoints_nested() { "empty_nested() must be false before calling" " set_zero_all_adjoints_nested()"); } - const size_t start1 = - ChainableStack::instance_->nested_var_stack_sizes_.back(); + const size_t start1 + = ChainableStack::instance_->nested_var_stack_sizes_.back(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); i < ChainableStack::instance_->var_stack_.size(); ++i) { diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index c69ce0af278..f886ee514a3 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -250,7 +250,7 @@ class vari_value::value>> template * = nullptr> explicit vari_value(S&& x) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.size())), + x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), rows_(x.rows()), @@ -281,7 +281,7 @@ class vari_value::value>> template * = nullptr> vari_value(S&& x, bool stacked) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.size())), + x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), rows_(x.rows()), @@ -460,7 +460,7 @@ class vari_value::value>> template * = nullptr> explicit vari_value(S&& x) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.nonZeros())), + x.nonZeros())), val_outer_index_( ChainableStack::instance_->memalloc_.alloc_array( x.innerSize())), @@ -502,7 +502,7 @@ class vari_value::value>> template * = nullptr> vari_value(S&& x, bool stacked) : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), + x.innerSize())), val_outer_index_( ChainableStack::instance_->memalloc_.alloc_array( x.outerSize())), @@ -618,7 +618,7 @@ class vari_value::value>> */ template inline eigen_map make_val(eigen_index*& outer_mem, eigen_index*& inner_mem, - eigen_scalar*& val_mem, S&& x) { + eigen_scalar*& val_mem, S&& x) { for (int k = 0; k < x.innerSize(); ++k) { val_mem[k] = x.valuePtr()[k]; inner_mem[k] = x.innerIndexPtr()[k]; @@ -640,7 +640,7 @@ class vari_value::value>> */ template inline eigen_map make_adj(eigen_index*& outer_mem, eigen_index*& inner_mem, - eigen_scalar*& val_mem, S&& x) { + eigen_scalar*& val_mem, S&& x) { for (int k = 0; k < x.innerSize(); ++k) { val_mem[k] = 0.0; inner_mem[k] = x.innerIndexPtr()[k]; From dd900b2d250648863038ee5fb6b4ad3dfab8f9f0 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Sun, 28 Jun 2020 21:46:51 -0400 Subject: [PATCH 125/167] add docs for x param for eigen_map vari_value constructors --- stan/math/rev/core/vari.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index c69ce0af278..45116a988b4 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -613,6 +613,7 @@ class vari_value::value>> * @param outer_mem Stack allocated memory for the outer index. * @param inner_mem Stack allocated memory for the inner index. * @param val_mem Stack allocated memory for the matrix values. + * @param x The Eigen sparse type to fill in the stack memory. * @note This function exists because Eigen's `Map` class for sparse matrices * has a lot of gotchas. */ @@ -635,6 +636,7 @@ class vari_value::value>> * @param outer_mem Stack allocated memory for the outer index. * @param inner_mem Stack allocated memory for the inner index. * @param val_mem Stack allocated memory for the matrix values. + * @param x The Eigen sparse type to fill in the stack memory. * @note This function exists because Eigen's `Map` class for sparse matrices * has a lot of gotchas. */ From 5d8c384e42396d3ef0bc34a06014ef50bfaa4c40 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 29 Jun 2020 17:16:25 -0400 Subject: [PATCH 126/167] use chainable_alloc for sparse matrix vari_value --- stan/math/rev/core/vari.hpp | 107 +++------------------------ test/unit/math/rev/core/var_test.cpp | 54 ++++++++++---- 2 files changed, 51 insertions(+), 110 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 282e33e3144..9aa1abea444 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -406,7 +406,7 @@ class vari_value::value>> */ template class vari_value::value>> - : public vari_base { + : public vari_base, chainable_alloc { public: using PlainObject = std::decay_t>; // Base type of Eigen class @@ -414,13 +414,6 @@ class vari_value::value>> using eigen_index = typename PlainObject::StorageIndex; // Index type using Scalar = PlainObject; // vari's adj_ and val_ member type using value_type = Scalar; // vari's adj_ and val_ member type - using eigen_map = Eigen::Map; // Map of stack alloc'd mem - eigen_scalar* val_mem_; // The value's nonzero values - eigen_index* val_outer_index_; // Value's Outer index of nonzero members - eigen_index* val_inner_index_; // Value's Inner index of nonzero members - eigen_scalar* adj_mem_; // Adjoint's nonzero values - eigen_index* adj_outer_index_; // Adjoint's outer index of nonzero members - eigen_index* adj_inner_index_; // Adjont's inner index of nonzero members const Eigen::Index rows_; // Number of rows in val_ const Eigen::Index cols_; // Number of cols in val_ const Eigen::Index size_; // Size of val_ @@ -435,13 +428,13 @@ class vari_value::value>> /** * The value of this variable. */ - const eigen_map val_; + const PlainObject val_; /** * The adjoint of this variable, which is the partial derivative * of this variable with respect to the root variable. */ - eigen_map adj_; + PlainObject adj_; /** * Construct a variable implementation from a value. The @@ -458,28 +451,13 @@ class vari_value::value>> * @param x Value of the constructed variable. */ template * = nullptr> - explicit vari_value(S&& x) - : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.nonZeros())), - val_outer_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - val_inner_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - adj_outer_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - adj_inner_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), + explicit vari_value(S&& x) : rows_(x.rows()), cols_(x.cols()), size_(x.size()), - val_(make_val(val_outer_index_, val_inner_index_, val_mem_, x)), - adj_(make_adj(adj_outer_index_, adj_inner_index_, adj_mem_, x)) { + val_(x), + adj_(x) { + this->set_zero_adjoint(); ChainableStack::instance_->var_stack_.push_back(this); } /** @@ -500,28 +478,13 @@ class vari_value::value>> * that its `chain()` method is not called. */ template * = nullptr> - vari_value(S&& x, bool stacked) - : val_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - val_outer_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - val_inner_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), - adj_outer_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.outerSize())), - adj_inner_index_( - ChainableStack::instance_->memalloc_.alloc_array( - x.innerSize())), + vari_value(S&& x, bool stacked) : rows_(x.rows()), cols_(x.cols()), size_(x.size()), - val_(make_val(val_outer_index_, val_inner_index_, val_mem_, x)), - adj_(make_adj(adj_outer_index_, adj_inner_index_, adj_mem_, x)) { + val_(x), + adj_(x) { + this->set_zero_adjoint(); if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -557,7 +520,7 @@ class vari_value::value>> */ inline void set_zero_adjoint() noexcept final { for (int k = 0; k < adj_.outerSize(); ++k) { - for (typename eigen_map::InnerIterator it(adj_, k); it; ++it) { + for (typename PlainObject::InnerIterator it(adj_, k); it; ++it) { it.valueRef() = 0.0; } } @@ -607,52 +570,6 @@ class vari_value::value>> private: template friend class var_value; - /** - * Fill the val_ map and it's pointers. - * @tparam S An Eigen sparse matrix type. - * @param outer_mem Stack allocated memory for the outer index. - * @param inner_mem Stack allocated memory for the inner index. - * @param val_mem Stack allocated memory for the matrix values. - * @param x The Eigen sparse type to fill in the stack memory. - * @note This function exists because Eigen's `Map` class for sparse matrices - * has a lot of gotchas. - */ - template - inline eigen_map make_val(eigen_index*& outer_mem, eigen_index*& inner_mem, - eigen_scalar*& val_mem, S&& x) { - for (int k = 0; k < x.innerSize(); ++k) { - val_mem[k] = x.valuePtr()[k]; - inner_mem[k] = x.innerIndexPtr()[k]; - } - for (int k = 0; k < x.outerSize(); ++k) { - outer_mem[k] = x.outerIndexPtr()[k]; - } - return eigen_map(x.rows(), x.cols(), x.nonZeros(), outer_mem, inner_mem, - val_mem); - } - /** - * Fill the val_ map and it's pointers. - * @tparam S An Eigen sparse matrix type. - * @param outer_mem Stack allocated memory for the outer index. - * @param inner_mem Stack allocated memory for the inner index. - * @param val_mem Stack allocated memory for the matrix values. - * @param x The Eigen sparse type to fill in the stack memory. - * @note This function exists because Eigen's `Map` class for sparse matrices - * has a lot of gotchas. - */ - template - inline eigen_map make_adj(eigen_index*& outer_mem, eigen_index*& inner_mem, - eigen_scalar*& val_mem, S&& x) { - for (int k = 0; k < x.innerSize(); ++k) { - val_mem[k] = 0.0; - inner_mem[k] = x.innerIndexPtr()[k]; - } - for (int k = 0; k < x.outerSize(); ++k) { - outer_mem[k] = x.outerIndexPtr()[k]; - } - return eigen_map(x.rows(), x.cols(), x.nonZeros(), outer_mem, inner_mem, - val_mem); - } }; } // namespace math diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index ee9064fea4a..05e7802feb0 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -79,6 +79,24 @@ void ctor_overloads_matrix(EigenMat&& xx) { x, var_value(var_value(x)).val()); } +auto make_sparse_matrix_random(int rows, int cols) { + using eigen_triplet = Eigen::Triplet; + boost::mt19937 gen; + boost::random::uniform_real_distribution dist(0.0, 1.0); + std::vector tripletList; + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + auto v_ij = dist(gen); + if (v_ij < 0.1) { + tripletList.push_back(eigen_triplet(i, j, v_ij)); + } + } + } + Eigen::SparseMatrix mat(rows, cols); + mat.setFromTriplets(tripletList.begin(), tripletList.end()); + return mat; +} + template void ctor_overloads_sparse_matrix(EigenMat&& x) { using stan::math::var_value; @@ -94,6 +112,14 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { EXPECT_FLOAT_EQ(iz.value(), it.value()); } } + const eigen_plain const_matmul_x = x * x; + eigen_plain const_matmul_xx = var_value(const_matmul_x).val(); + for (int k = 0; k < matmul_x.outerSize(); ++k) { + for (inner_iterator it(const_matmul_x, k), iz(const_matmul_xx, k); it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + } + } + // make sure rvalue var_value can be accepted eigen_plain x_rv = var_value(var_value(x)).val(); for (int k = 0; k < x.outerSize(); ++k) { @@ -114,24 +140,22 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { EXPECT_FLOAT_EQ(iz.value(), it.value()); } } -} - -auto make_sparse_matrix_random(int rows, int cols) { - using eigen_triplet = Eigen::Triplet; - boost::mt19937 gen; - boost::random::uniform_real_distribution dist(0.0, 1.0); - std::vector tripletList; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - auto v_ij = dist(gen); - if (v_ij < 0.1) { - tripletList.push_back(eigen_triplet(i, j, v_ij)); + // test inplace addition works + auto inplace_add_var = var_value(new vari_value(x)); + eigen_plain test_y = make_sparse_matrix_random(10, 10); + inplace_add_var.adj() += test_y; + // adjoints sparsity pattern will be pattern of x and test_y for addition + for (int k = 0; k < x.outerSize(); ++k) { + for (inner_iterator it(test_y, k), iz(inplace_add_var.adj(), k); iz; ++iz) { + if (iz.row() == it.row() && iz.col() == it.col()) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + ++it; + } else { + EXPECT_FLOAT_EQ(iz.value(), 0.0); } } } - Eigen::SparseMatrix mat(rows, cols); - mat.setFromTriplets(tripletList.begin(), tripletList.end()); - return mat; + } TEST_F(AgradRev, ctormatrixOverloads) { From 1cbd95bd54ce5f8cde52f389b4eb71d3eb991aee Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 29 Jun 2020 17:17:14 -0400 Subject: [PATCH 127/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 22 +++++++--------------- test/unit/math/rev/core/var_test.cpp | 4 ++-- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 9aa1abea444..a5a3954a7c7 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -414,9 +414,9 @@ class vari_value::value>> using eigen_index = typename PlainObject::StorageIndex; // Index type using Scalar = PlainObject; // vari's adj_ and val_ member type using value_type = Scalar; // vari's adj_ and val_ member type - const Eigen::Index rows_; // Number of rows in val_ - const Eigen::Index cols_; // Number of cols in val_ - const Eigen::Index size_; // Size of val_ + const Eigen::Index rows_; // Number of rows in val_ + const Eigen::Index cols_; // Number of cols in val_ + const Eigen::Index size_; // Size of val_ /** * Rows at compile time */ @@ -451,12 +451,8 @@ class vari_value::value>> * @param x Value of the constructed variable. */ template * = nullptr> - explicit vari_value(S&& x) : - rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - val_(x), - adj_(x) { + explicit vari_value(S&& x) + : rows_(x.rows()), cols_(x.cols()), size_(x.size()), val_(x), adj_(x) { this->set_zero_adjoint(); ChainableStack::instance_->var_stack_.push_back(this); } @@ -478,12 +474,8 @@ class vari_value::value>> * that its `chain()` method is not called. */ template * = nullptr> - vari_value(S&& x, bool stacked) : - rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - val_(x), - adj_(x) { + vari_value(S&& x, bool stacked) + : rows_(x.rows()), cols_(x.cols()), size_(x.size()), val_(x), adj_(x) { this->set_zero_adjoint(); if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 05e7802feb0..4563b58ed2f 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -115,7 +115,8 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { const eigen_plain const_matmul_x = x * x; eigen_plain const_matmul_xx = var_value(const_matmul_x).val(); for (int k = 0; k < matmul_x.outerSize(); ++k) { - for (inner_iterator it(const_matmul_x, k), iz(const_matmul_xx, k); it; ++it, ++iz) { + for (inner_iterator it(const_matmul_x, k), iz(const_matmul_xx, k); it; + ++it, ++iz) { EXPECT_FLOAT_EQ(iz.value(), it.value()); } } @@ -155,7 +156,6 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { } } } - } TEST_F(AgradRev, ctormatrixOverloads) { From cea502a08cb9dc674b1b50cc647f552055ca6130 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 29 Jun 2020 17:36:03 -0400 Subject: [PATCH 128/167] add chainable_alloc() constructor to sparse vari constructor --- stan/math/rev/core/vari.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 9aa1abea444..aade0dc72f5 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -456,7 +456,8 @@ class vari_value::value>> cols_(x.cols()), size_(x.size()), val_(x), - adj_(x) { + adj_(x), + chainable_alloc() { this->set_zero_adjoint(); ChainableStack::instance_->var_stack_.push_back(this); } @@ -483,7 +484,8 @@ class vari_value::value>> cols_(x.cols()), size_(x.size()), val_(x), - adj_(x) { + adj_(x), + chainable_alloc() { this->set_zero_adjoint(); if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); From cc2a49d37a30533c59b0fda0e191546ec557b0e4 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 29 Jun 2020 17:37:25 -0400 Subject: [PATCH 129/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 9df5e5fe74d..e52e8099910 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -451,8 +451,8 @@ class vari_value::value>> * @param x Value of the constructed variable. */ template * = nullptr> - explicit vari_value(S&& x) : - rows_(x.rows()), + explicit vari_value(S&& x) + : rows_(x.rows()), cols_(x.cols()), size_(x.size()), val_(x), @@ -479,8 +479,8 @@ class vari_value::value>> * that its `chain()` method is not called. */ template * = nullptr> - vari_value(S&& x, bool stacked) : - rows_(x.rows()), + vari_value(S&& x, bool stacked) + : rows_(x.rows()), cols_(x.cols()), size_(x.size()), val_(x), From 41d0e7474ddaef7745675733bceb3b8983d41b12 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 30 Jun 2020 14:18:48 -0400 Subject: [PATCH 130/167] using chainable_alloc allows the sparse vari to accept eigen expressions --- stan/math/rev/core/vari.hpp | 8 +++++++- test/unit/math/rev/core/var_test.cpp | 17 +++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 9df5e5fe74d..fa34a32c197 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -513,7 +513,13 @@ class vari_value::value>> * propagating derivatives, setting the derivative of the * result with respect to itself to be 1. */ - inline void init_dependent() { adj_.setOnes(); } + inline void init_dependent() { + for (int k = 0; k < adj_.outerSize(); ++k) { + for (typename PlainObject::InnerIterator it(adj_, k); it; ++it) { + it.valueRef() = 1.0; + } + } + } /** * Set the adjoint value of this variable to 0. This is used to diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 4563b58ed2f..b2e74001a5e 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -129,30 +129,27 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { } } - /** from a vari_value with sparse - * NOTE: This will fail with an expression since allocating the mem - * for the vari requires knowing the sizes of the inner and outer indices - * which is not computed for expressions. - */ + // from a vari_value with sparse eigen expression eigen_plain x_from_vari - = var_value(new vari_value(x)).val(); - for (int k = 0; k < x.outerSize(); ++k) { - for (inner_iterator it(x, k), iz(x_from_vari, k); it; ++it, ++iz) { + = var_value(new vari_value(x * x)).val(); + for (int k = 0; k < matmul_x.outerSize(); ++k) { + for (inner_iterator it(matmul_x, k), iz(x_from_vari, k); it; ++it, ++iz) { EXPECT_FLOAT_EQ(iz.value(), it.value()); } } // test inplace addition works auto inplace_add_var = var_value(new vari_value(x)); eigen_plain test_y = make_sparse_matrix_random(10, 10); + inplace_add_var.vi_->init_dependent(); inplace_add_var.adj() += test_y; // adjoints sparsity pattern will be pattern of x and test_y for addition for (int k = 0; k < x.outerSize(); ++k) { for (inner_iterator it(test_y, k), iz(inplace_add_var.adj(), k); iz; ++iz) { if (iz.row() == it.row() && iz.col() == it.col()) { - EXPECT_FLOAT_EQ(iz.value(), it.value()); + EXPECT_FLOAT_EQ(iz.value() - 1, it.value()); ++it; } else { - EXPECT_FLOAT_EQ(iz.value(), 0.0); + EXPECT_FLOAT_EQ(iz.value(), 1.0); } } } From cdaa2e61f93da11e95aa73ae820187e1da6238ea Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 30 Jun 2020 14:19:43 -0400 Subject: [PATCH 131/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 1c5ae9a820d..60523e34c48 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -519,7 +519,7 @@ class vari_value::value>> it.valueRef() = 1.0; } } - } + } /** * Set the adjoint value of this variable to 0. This is used to From dc44dcc9200e914db1ee666c2e68a1ef65f53b04 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 30 Jun 2020 16:49:50 -0400 Subject: [PATCH 132/167] add static_assert to make sure var inner type is assignable. Add tests for init_dependent --- stan/math/rev/core/var.hpp | 33 ++++++++++------------------ test/unit/math/rev/core/var_test.cpp | 4 ++++ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 32d772293dc..2c6a682c831 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -35,10 +35,18 @@ class var_value {}; template class var_value> { + /** + * Checks whether the template type `T` is an assignable type. + */ + template + using check_plain_type = std::is_same, + std::decay_t>>; + static_assert(check_plain_type::value, "The template for this var is an" + " expression but a var_value's inner type must be assignable such as" + " a double, Eigen::Matrix, or Eigen::Array"); public: using value_type = std::decay_t>; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. - using vari_pointer = vari_type*; // pointer type for underlying vari. /** * Pointer to the implementation of this variable. @@ -76,33 +84,14 @@ class var_value> { * @tparam S A type that is convertible to `value_type`. * @param x Value of the variable. */ - template * = nullptr, - require_not_eigen_sparse_base_t* = nullptr> + template * = nullptr> var_value(S&& x) : vi_(new vari_type(std::forward(x), false)) {} // NOLINT - /** - * Construct a variable from the specified floating point argument - * by constructing a new `vari_value`. This constructor is only - * valid when `S` is convertible to this `vari_value`'s `value_type` and is - * derived from Eigen's sparse matrix base class. This specialization exists - * because Eigen sparse matrix expressions do not have `innerSize()` and - * `outerSize()` members. While dense matrix expressions can be passed along - * to the constructor for `vari_value`, sparse matrix expressions need to - * be evaluated before they are passed to the `vari_value` constructor. - * @tparam S A type that is convertible to `value_type`. - * @param x Value of the variable. - */ - template * = nullptr, - require_eigen_sparse_base_t* = nullptr> - var_value(S&& x) : vi_(new vari_type(ref_type_t(x), false)) {} // NOLINT - /** * Construct a variable from a pointer to a variable implementation. * @param vi A vari_value pointer. */ - template * = nullptr> - var_value(vari_value* vi) // NOLINT - : vi_(vi) {} + var_value(vari_type* vi) : vi_(vi) {} // NOLINT /** * Return a constant reference to the value of this variable. diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index b2e74001a5e..9b2dfcde152 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -77,6 +77,10 @@ void ctor_overloads_matrix(EigenMat&& xx) { // make sure rvalue var_value can be accepted EXPECT_MATRIX_FLOAT_EQ( x, var_value(var_value(x)).val()); + // test init_dependent for adj + auto test_var_x = var_value(var_value(x)); + test_var_x.vi_->init_dependent(); + EXPECT_MATRIX_FLOAT_EQ(eigen_plain::Ones(x.rows(), x.cols()) , test_var_x.adj()); } auto make_sparse_matrix_random(int rows, int cols) { From fa3a6fc24a03ad2457a73372e633919f4b6d1b27 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 30 Jun 2020 20:50:50 +0000 Subject: [PATCH 133/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/var.hpp | 15 +++++++++------ test/unit/math/rev/core/var_test.cpp | 3 ++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 2c6a682c831..4c63d29e28d 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -39,11 +39,14 @@ class var_value> { * Checks whether the template type `T` is an assignable type. */ template - using check_plain_type = std::is_same, - std::decay_t>>; - static_assert(check_plain_type::value, "The template for this var is an" - " expression but a var_value's inner type must be assignable such as" - " a double, Eigen::Matrix, or Eigen::Array"); + using check_plain_type + = std::is_same, std::decay_t>>; + static_assert( + check_plain_type::value, + "The template for this var is an" + " expression but a var_value's inner type must be assignable such as" + " a double, Eigen::Matrix, or Eigen::Array"); + public: using value_type = std::decay_t>; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. @@ -91,7 +94,7 @@ class var_value> { * Construct a variable from a pointer to a variable implementation. * @param vi A vari_value pointer. */ - var_value(vari_type* vi) : vi_(vi) {} // NOLINT + var_value(vari_type* vi) : vi_(vi) {} // NOLINT /** * Return a constant reference to the value of this variable. diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 9b2dfcde152..35914078705 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -80,7 +80,8 @@ void ctor_overloads_matrix(EigenMat&& xx) { // test init_dependent for adj auto test_var_x = var_value(var_value(x)); test_var_x.vi_->init_dependent(); - EXPECT_MATRIX_FLOAT_EQ(eigen_plain::Ones(x.rows(), x.cols()) , test_var_x.adj()); + EXPECT_MATRIX_FLOAT_EQ(eigen_plain::Ones(x.rows(), x.cols()), + test_var_x.adj()); } auto make_sparse_matrix_random(int rows, int cols) { From 8d20a6727542b5c98b7a228af1ef9f88c567b996 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Jul 2020 17:34:34 -0400 Subject: [PATCH 134/167] Add tests for new meta functions, remove forwarding from vari --- stan/math/prim/meta.hpp | 1 + stan/math/prim/meta/is_eigen_dense_base.hpp | 4 +- stan/math/prim/meta/is_eigen_matrix_base.hpp | 2 +- stan/math/prim/meta/is_eigen_sparse_base.hpp | 4 +- stan/math/prim/meta/is_plain_type.hpp | 21 +++++ stan/math/rev/core/var.hpp | 12 +-- stan/math/rev/core/vari.hpp | 86 +++++++++---------- .../mix/meta/is_eigen_sparse_base_test.cpp | 58 +++++++++++++ .../math/prim/meta/is_plain_type_test.cpp | 28 ++++++ test/unit/math/rev/core/vari_test.cpp | 5 ++ 10 files changed, 160 insertions(+), 61 deletions(-) create mode 100644 stan/math/prim/meta/is_plain_type.hpp create mode 100644 test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp create mode 100644 test/unit/math/prim/meta/is_plain_type_test.cpp diff --git a/stan/math/prim/meta.hpp b/stan/math/prim/meta.hpp index 94955d73d99..35945171495 100644 --- a/stan/math/prim/meta.hpp +++ b/stan/math/prim/meta.hpp @@ -192,6 +192,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/prim/meta/is_eigen_dense_base.hpp b/stan/math/prim/meta/is_eigen_dense_base.hpp index 225f833b45c..3ce1dbe8ebc 100644 --- a/stan/math/prim/meta/is_eigen_dense_base.hpp +++ b/stan/math/prim/meta/is_eigen_dense_base.hpp @@ -10,10 +10,10 @@ namespace stan { /** - * Checks whether type T is derived from Eigen::MatrixBase. + * Checks whether type T is derived from Eigen::DenseBase. * If true this will have a static member function named value with a type * of true, else value is false. - * @tparam T Type to check if it is derived from `EigenBase` + * @tparam T Type to check if it is derived from `DenseBase` * @tparam Enable used for SFINAE deduction. * @ingroup type_trait */ diff --git a/stan/math/prim/meta/is_eigen_matrix_base.hpp b/stan/math/prim/meta/is_eigen_matrix_base.hpp index f6d47c0b2e1..9e4e6e9465e 100644 --- a/stan/math/prim/meta/is_eigen_matrix_base.hpp +++ b/stan/math/prim/meta/is_eigen_matrix_base.hpp @@ -13,7 +13,7 @@ namespace stan { * Checks whether type T is derived from Eigen::MatrixBase. * If true this will have a static member function named value with a type * of true, else value is false. - * @tparam T Type to check if it is derived from `EigenBase` + * @tparam T Type to check if it is derived from `MatrixBase` * @tparam Enable used for SFINAE deduction. * @ingroup type_trait */ diff --git a/stan/math/prim/meta/is_eigen_sparse_base.hpp b/stan/math/prim/meta/is_eigen_sparse_base.hpp index b6eee02a4a7..2c1028730bf 100644 --- a/stan/math/prim/meta/is_eigen_sparse_base.hpp +++ b/stan/math/prim/meta/is_eigen_sparse_base.hpp @@ -10,10 +10,10 @@ namespace stan { /** - * Checks whether type T is derived from Eigen::MatrixBase. + * Checks whether type T is derived from Eigen::SparseMatrixBase. * If true this will have a static member function named value with a type * of true, else value is false. - * @tparam T Type to check if it is derived from `EigenBase` + * @tparam T Type to check if it is derived from `SparseMatrixBase` * @tparam Enable used for SFINAE deduction. * @ingroup type_trait */ diff --git a/stan/math/prim/meta/is_plain_type.hpp b/stan/math/prim/meta/is_plain_type.hpp new file mode 100644 index 00000000000..bec202aef5f --- /dev/null +++ b/stan/math/prim/meta/is_plain_type.hpp @@ -0,0 +1,21 @@ +#ifndef STAN_MATH_PRIM_META_IS_PLAIN_TYPE_HPP +#define STAN_MATH_PRIM_META_IS_PLAIN_TYPE_HPP + +#include +#include +#include + +namespace stan { +/** \ingroup type_trait + * Checks whether the template type `T` is an assignable type. This is used + * to detect whether a type is an Eigen matrix expression. + */ + template + using is_plain_type + = std::is_same, std::decay_t>>; + +STAN_ADD_REQUIRE_UNARY(plain_type, is_plain_type, require_eigens_types); +STAN_ADD_REQUIRE_UNARY_INNER(plain_type, is_plain_type, require_eigens_types); + +} // namespace stan +#endif diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 4c63d29e28d..af697d60a0e 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -35,20 +35,14 @@ class var_value {}; template class var_value> { - /** - * Checks whether the template type `T` is an assignable type. - */ - template - using check_plain_type - = std::is_same, std::decay_t>>; static_assert( - check_plain_type::value, + is_plain_type::value, "The template for this var is an" " expression but a var_value's inner type must be assignable such as" " a double, Eigen::Matrix, or Eigen::Array"); public: - using value_type = std::decay_t>; // type in vari_value. + using value_type = std::decay_t; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. /** @@ -88,7 +82,7 @@ class var_value> { * @param x Value of the variable. */ template * = nullptr> - var_value(S&& x) : vi_(new vari_type(std::forward(x), false)) {} // NOLINT + var_value(S&& x) : vi_(new vari_type(x, false)) {} // NOLINT /** * Construct a variable from a pointer to a variable implementation. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 60523e34c48..5300bb8975e 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -49,10 +49,6 @@ class vari_value; template class vari_value::value>> : public vari_base { - private: - template - friend class var_value; - public: using Scalar = T; using value_type = Scalar; @@ -181,6 +177,11 @@ class vari_value::value>> static inline void operator delete( void* /* ignore arg */) noexcept { /* no op */ } + private: + template + friend class var_value; + template * = nullptr> + explicit vari_value(T1&& A, T2&& B) : val_(A), adj_(B) {} }; // For backwards compatability the default is double @@ -201,19 +202,23 @@ template class vari_value::value>> : public vari_base { public: + static_assert( + is_plain_type::value, + "The template for this var is an" + " expression but a var_value's inner type must be assignable such as" + " a double, Eigen::Matrix, or Eigen::Array"); + /** * `PlainObject` represents a user constructible type such as Matrix or Array */ using PlainObject = std::decay_t>; - using eigen_scalar = value_type_t; // A floating point type - eigen_scalar* val_mem_; // Pointer to memory allocated on the stack for val_ - eigen_scalar* adj_mem_; // Pointer to memory allocated on the stack for adj_ using Scalar = PlainObject; // The underlying type for this class using value_type = Scalar; // The underlying type for this class + using eigen_scalar = value_type_t; // A floating point type using eigen_map = Eigen::Map; // Maps for adj_ and val_ - const Eigen::Index rows_; // Number of rows - const Eigen::Index cols_; // Number of cols - const Eigen::Index size_; // Size of rows * cols + using vari_type = vari_value::value>>; + eigen_scalar* val_mem_; // Pointer to memory allocated on the stack for val_ + eigen_scalar* adj_mem_; // Pointer to memory allocated on the stack for adj_ /** * Number of rows known at compile time */ @@ -253,11 +258,8 @@ class vari_value::value>> x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), - rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - adj_(make_adj(adj_mem_, x)), - val_(make_val(val_mem_, std::forward(x))) { + adj_(make_adj(x)), + val_(make_val(x)) { ChainableStack::instance_->var_stack_.push_back(this); } @@ -284,11 +286,8 @@ class vari_value::value>> x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), - rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - adj_(make_adj(adj_mem_, x)), - val_(make_val(val_mem_, x)) { + adj_(make_adj(x)), + val_(make_val(x)) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -299,15 +298,15 @@ class vari_value::value>> /** * Return the number of rows for this class's `val_` member */ - const Eigen::Index rows() const { return rows_; } + const Eigen::Index rows() const { return val_.rows(); } /** * Return the number of columns for this class's `val_` member */ - const Eigen::Index cols() const { return cols_; } + const Eigen::Index cols() const { return val_.rows(); } /** * Return the size of this class's `val_` member */ - const Eigen::Index size() const { return size_; } + const Eigen::Index size() const { return val_.size(); } virtual void chain() {} /** @@ -334,9 +333,9 @@ class vari_value::value>> * * @return The modified ostream. */ - friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { - return os << v->val_ << ":" << v->adj_; - } + friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { + return os << "val: \n" << v->val_ << " \nadj: \n" << v->adj_; + } /** * Allocate memory from the underlying memory pool. This memory is @@ -369,6 +368,8 @@ class vari_value::value>> private: template friend class var_value; + template + friend class vari_value; /** * Create the map to the val_ stack allocated memory for an Eigen input. * @tparam S an Eigen type. @@ -376,9 +377,9 @@ class vari_value::value>> * @param x The Eigen type whose values will be assigned to `val_` */ template - inline eigen_map make_val(eigen_scalar*& mem, S&& x) { - eigen_map(mem, x.rows(), x.cols()) = std::forward(x); - return eigen_map(mem, x.rows(), x.cols()); + inline eigen_map make_val(S&& x) { + eigen_map(val_mem_, x.rows(), x.cols()) = x; + return eigen_map(val_mem_, x.rows(), x.cols()); } /** * Create the map to the adj_ stack allocated memory for an Eigen input. @@ -387,9 +388,9 @@ class vari_value::value>> * @param x The Eigen type whose dimensions are set the `adj_`s dimensions. */ template - inline eigen_map make_adj(eigen_scalar*& mem, S&& x) { - eigen_map(mem, x.rows(), x.cols()).setZero(); - return eigen_map(mem, x.rows(), x.cols()); + inline eigen_map make_adj(S&& x) { + eigen_map(adj_mem_, x.rows(), x.cols()).setZero(); + return eigen_map(adj_mem_, x.rows(), x.cols()); } }; @@ -414,9 +415,6 @@ class vari_value::value>> using eigen_index = typename PlainObject::StorageIndex; // Index type using Scalar = PlainObject; // vari's adj_ and val_ member type using value_type = Scalar; // vari's adj_ and val_ member type - const Eigen::Index rows_; // Number of rows in val_ - const Eigen::Index cols_; // Number of cols in val_ - const Eigen::Index size_; // Size of val_ /** * Rows at compile time */ @@ -452,10 +450,7 @@ class vari_value::value>> */ template * = nullptr> explicit vari_value(S&& x) - : rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - val_(x), + : val_(x), adj_(x), chainable_alloc() { this->set_zero_adjoint(); @@ -480,10 +475,7 @@ class vari_value::value>> */ template * = nullptr> vari_value(S&& x, bool stacked) - : rows_(x.rows()), - cols_(x.cols()), - size_(x.size()), - val_(x), + : val_(x), adj_(x), chainable_alloc() { this->set_zero_adjoint(); @@ -497,15 +489,15 @@ class vari_value::value>> /** * Return the number of rows for this class's `val_` member */ - const Eigen::Index rows() const { return rows_; } + const Eigen::Index rows() const { return val_.rows(); } /** * Return the number of columns for this class's `val_` member */ - const Eigen::Index cols() const { return cols_; } + const Eigen::Index cols() const { return val_.cols(); } /** * Return the size of this class's `val_` member */ - const Eigen::Index size() const { return size_; } + const Eigen::Index size() const { return val_.size(); } void chain() {} /** * Initialize the adjoint for this (dependent) variable to 1. @@ -544,7 +536,7 @@ class vari_value::value>> * @return The modified ostream. */ friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { - return os << v->val_ << ":" << v->adj_; + return os << "val: \n" << v->val_ << " \nadj: \n" << v->adj_; } /** diff --git a/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp b/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp new file mode 100644 index 00000000000..c55adfc4364 --- /dev/null +++ b/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +TEST(MathMetaPrim, is_eigen_sparse_base_hierarchy_tests) { + using Eigen::Array; + using Eigen::Matrix; + using stan::is_eigen_sparse_base; + using stan::math::test::all_eigen_dense; + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); +} + +TEST(MathMetaPrim, is_eigen_sparse_base_sparse_tests) { + using Eigen::Array; + using Eigen::Matrix; + using stan::is_eigen_sparse_base; + using stan::math::test::all_eigen_sparse; + all_eigen_sparse(); +} + +TEST(MathMetaPrim, is_eigen_sparse_base_decomp_tests) { + using Eigen::Array; + using Eigen::Matrix; + using stan::is_eigen_sparse_base; + using stan::math::test::all_eigen_dense_decomp; + all_eigen_dense_decomp(); +} + +TEST(MathMetaPrim, is_eigen_sparse_base_expr_tests) { + using Eigen::Array; + using Eigen::Matrix; + using stan::is_eigen_sparse_base; + using stan::math::test::all_eigen_dense_exprs; + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); +} diff --git a/test/unit/math/prim/meta/is_plain_type_test.cpp b/test/unit/math/prim/meta/is_plain_type_test.cpp new file mode 100644 index 00000000000..a845657ede0 --- /dev/null +++ b/test/unit/math/prim/meta/is_plain_type_test.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +TEST(MathMetaPrim, is_plain_type) { + using stan::is_plain_type; + EXPECT_TRUE((is_plain_type::value)); + EXPECT_TRUE((is_plain_type::value)); + EXPECT_TRUE((is_plain_type::value)); + + EXPECT_TRUE((is_plain_type>::value)); + EXPECT_TRUE((is_plain_type>::value)); + EXPECT_TRUE((is_plain_type&>::value)); + + EXPECT_TRUE((is_plain_type>::value)); + EXPECT_TRUE((is_plain_type>::value)); + + EXPECT_TRUE((is_plain_type>::value)); + EXPECT_TRUE((is_plain_type&>::value)); + Eigen::Matrix a; + Eigen::Matrix b; + + EXPECT_FALSE((is_plain_type::value)); + EXPECT_FALSE((is_plain_type::value)); + EXPECT_FALSE((is_plain_type< + Eigen::MatrixBase>&&>::value)); + EXPECT_FALSE((is_plain_type>::value)); +} diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index fc5a6e34ab5..c8961eb62ae 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -15,3 +15,8 @@ TEST(AgradRev, long_double_test) { ss << &v; EXPECT_EQ("5:0", ss.str()); } + +TEST(AgradRev, dense_matrix_vari) { + using stan::math::vari_value; + stan::math::vari_value A(Eigen::MatrixXd::Random(3, 3)); +} From b83f5bf3234a77c360463af1a1f672f50a27d5e3 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 1 Jul 2020 17:45:09 -0400 Subject: [PATCH 135/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/prim/meta/is_plain_type.hpp | 6 +-- stan/math/rev/core/var.hpp | 2 +- stan/math/rev/core/vari.hpp | 37 +++++++++---------- .../mix/meta/is_eigen_sparse_base_test.cpp | 36 +++++++++--------- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/stan/math/prim/meta/is_plain_type.hpp b/stan/math/prim/meta/is_plain_type.hpp index bec202aef5f..9bb0cdc1a20 100644 --- a/stan/math/prim/meta/is_plain_type.hpp +++ b/stan/math/prim/meta/is_plain_type.hpp @@ -10,9 +10,9 @@ namespace stan { * Checks whether the template type `T` is an assignable type. This is used * to detect whether a type is an Eigen matrix expression. */ - template - using is_plain_type - = std::is_same, std::decay_t>>; +template +using is_plain_type + = std::is_same, std::decay_t>>; STAN_ADD_REQUIRE_UNARY(plain_type, is_plain_type, require_eigens_types); STAN_ADD_REQUIRE_UNARY_INNER(plain_type, is_plain_type, require_eigens_types); diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index af697d60a0e..8dbed9e7171 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -42,7 +42,7 @@ class var_value> { " a double, Eigen::Matrix, or Eigen::Array"); public: - using value_type = std::decay_t; // type in vari_value. + using value_type = std::decay_t; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. /** diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 5300bb8975e..81d2f4874d6 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -177,10 +177,12 @@ class vari_value::value>> static inline void operator delete( void* /* ignore arg */) noexcept { /* no op */ } - private: + + private: template friend class var_value; - template * = nullptr> + template * = nullptr> explicit vari_value(T1&& A, T2&& B) : val_(A), adj_(B) {} }; @@ -202,11 +204,11 @@ template class vari_value::value>> : public vari_base { public: - static_assert( - is_plain_type::value, - "The template for this var is an" - " expression but a var_value's inner type must be assignable such as" - " a double, Eigen::Matrix, or Eigen::Array"); + static_assert( + is_plain_type::value, + "The template for this var is an" + " expression but a var_value's inner type must be assignable such as" + " a double, Eigen::Matrix, or Eigen::Array"); /** * `PlainObject` represents a user constructible type such as Matrix or Array @@ -215,8 +217,9 @@ class vari_value::value>> using Scalar = PlainObject; // The underlying type for this class using value_type = Scalar; // The underlying type for this class using eigen_scalar = value_type_t; // A floating point type - using eigen_map = Eigen::Map; // Maps for adj_ and val_ - using vari_type = vari_value::value>>; + using eigen_map = Eigen::Map; // Maps for adj_ and val_ + using vari_type + = vari_value::value>>; eigen_scalar* val_mem_; // Pointer to memory allocated on the stack for val_ eigen_scalar* adj_mem_; // Pointer to memory allocated on the stack for adj_ /** @@ -333,9 +336,9 @@ class vari_value::value>> * * @return The modified ostream. */ - friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { - return os << "val: \n" << v->val_ << " \nadj: \n" << v->adj_; - } + friend std::ostream& operator<<(std::ostream& os, const vari_value* v) { + return os << "val: \n" << v->val_ << " \nadj: \n" << v->adj_; + } /** * Allocate memory from the underlying memory pool. This memory is @@ -449,10 +452,7 @@ class vari_value::value>> * @param x Value of the constructed variable. */ template * = nullptr> - explicit vari_value(S&& x) - : val_(x), - adj_(x), - chainable_alloc() { + explicit vari_value(S&& x) : val_(x), adj_(x), chainable_alloc() { this->set_zero_adjoint(); ChainableStack::instance_->var_stack_.push_back(this); } @@ -474,10 +474,7 @@ class vari_value::value>> * that its `chain()` method is not called. */ template * = nullptr> - vari_value(S&& x, bool stacked) - : val_(x), - adj_(x), - chainable_alloc() { + vari_value(S&& x, bool stacked) : val_(x), adj_(x), chainable_alloc() { this->set_zero_adjoint(); if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); diff --git a/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp b/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp index c55adfc4364..cc513e83160 100644 --- a/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp +++ b/test/unit/math/mix/meta/is_eigen_sparse_base_test.cpp @@ -8,18 +8,18 @@ TEST(MathMetaPrim, is_eigen_sparse_base_hierarchy_tests) { using Eigen::Matrix; using stan::is_eigen_sparse_base; using stan::math::test::all_eigen_dense; - all_eigen_dense(); - all_eigen_dense(); - all_eigen_dense(); - all_eigen_dense(); - all_eigen_dense(); - all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); + all_eigen_dense(); } TEST(MathMetaPrim, is_eigen_sparse_base_sparse_tests) { @@ -43,12 +43,12 @@ TEST(MathMetaPrim, is_eigen_sparse_base_expr_tests) { using Eigen::Matrix; using stan::is_eigen_sparse_base; using stan::math::test::all_eigen_dense_exprs; - all_eigen_dense_exprs(); - all_eigen_dense_exprs(); - all_eigen_dense_exprs(); + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); + all_eigen_dense_exprs(); all_eigen_dense_exprs(); all_eigen_dense_exprs Date: Wed, 1 Jul 2020 20:02:28 -0400 Subject: [PATCH 136/167] remove docs for removed mem param in make_* functions in vari_value --- stan/math/rev/core/vari.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 81d2f4874d6..577ff3c419d 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -376,7 +376,6 @@ class vari_value::value>> /** * Create the map to the val_ stack allocated memory for an Eigen input. * @tparam S an Eigen type. - * @param mem A pointer to stack allocated memory. * @param x The Eigen type whose values will be assigned to `val_` */ template @@ -387,7 +386,6 @@ class vari_value::value>> /** * Create the map to the adj_ stack allocated memory for an Eigen input. * @tparam S an Eigen type. - * @param mem A pointer to stack allocated memory. * @param x The Eigen type whose dimensions are set the `adj_`s dimensions. */ template From fba6166e41e33eb6b1e4b19d47512d0d5d590aee Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Jul 2020 23:04:07 -0400 Subject: [PATCH 137/167] remove private constructor for vari_value --- stan/math/rev/core/vari.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 577ff3c419d..1570dd817fb 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -181,9 +181,6 @@ class vari_value::value>> private: template friend class var_value; - template * = nullptr> - explicit vari_value(T1&& A, T2&& B) : val_(A), adj_(B) {} }; // For backwards compatability the default is double From 48801a05d3a4a0ed18fe8d6bb2c8bd3db4dc43e4 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 6 Jul 2020 15:42:47 -0400 Subject: [PATCH 138/167] remove make functions from vari_value matrix constructor. Add additional tests for matrix and sparse matrix vari_value construction --- stan/math/rev/core/var.hpp | 13 +++-- stan/math/rev/core/vari.hpp | 74 ++++++++------------------- test/unit/math/rev/core/var_test.cpp | 40 +++++---------- test/unit/math/rev/core/vari_test.cpp | 39 +++++++++++++- test/unit/util.hpp | 21 ++++++++ 5 files changed, 102 insertions(+), 85 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 8dbed9e7171..a356aa7100a 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -30,16 +30,19 @@ static void grad(Vari* vi); * var values objects. * @tparam T An Floating point type. */ -template -class var_value {}; - template -class var_value> { +class var_value { static_assert( is_plain_type::value, "The template for this var is an" " expression but a var_value's inner type must be assignable such as" " a double, Eigen::Matrix, or Eigen::Array"); + static_assert( + std::is_floating_point>::value, + "The template for must be a floating point or a container holding" + " floating point types"); + static_assert(std::is_reference::value, + "The template for a var_value must not be a reference!"); public: using value_type = std::decay_t; // type in vari_value. @@ -63,7 +66,7 @@ class var_value> { * @return true if this variable does not yet have * a defined variable. */ - bool is_uninitialized() { return (vi_ == nullptr); } + using bool is_uninitialized() { return (vi_ == nullptr); } /** * Construct a variable for later assignment. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 1570dd817fb..69e5ba2f802 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -47,8 +47,7 @@ class vari_value; * */ template -class vari_value::value>> - : public vari_base { +class vari_value> : public vari_base { public: using Scalar = T; using value_type = Scalar; @@ -75,8 +74,7 @@ class vari_value::value>> * @tparam S a floating point type. * @param x Value of the constructed variable. */ - template ::value>* = nullptr> + template * = nullptr> vari_value(S x) noexcept : val_(x), adj_(0.0) { // NOLINT ChainableStack::instance_->var_stack_.emplace_back(this); } @@ -98,8 +96,7 @@ class vari_value::value>> * @param stacked If false will put this this vari on the nochain stack so * that its `chain()` method is not called. */ - template ::value>* = nullptr> + template * = nullptr> vari_value(S x, bool stacked) noexcept : val_(x), adj_(0.0) { if (stacked) { ChainableStack::instance_->var_stack_.emplace_back(this); @@ -108,15 +105,6 @@ class vari_value::value>> } } - /** - * Constructor from vari_value - * @tparam S A floating point type - * @param x A vari_value - */ - vari_value(const vari_value& x) noexcept : val_(x.val_), adj_(x.adj_) { - ChainableStack::instance_->var_stack_.emplace_back(this); - } - ~vari_value() = default; inline void chain() {} @@ -198,8 +186,7 @@ using vari = vari_value; * */ template -class vari_value::value>> - : public vari_base { +class vari_value> : public vari_base { public: static_assert( is_plain_type::value, @@ -214,9 +201,11 @@ class vari_value::value>> using Scalar = PlainObject; // The underlying type for this class using value_type = Scalar; // The underlying type for this class using eigen_scalar = value_type_t; // A floating point type - using eigen_map = Eigen::Map; // Maps for adj_ and val_ - using vari_type - = vari_value::value>>; + /** + * Maps for adj_ and val_ + */ + using eigen_map = Eigen::Map; + using vari_type = vari_value>; eigen_scalar* val_mem_; // Pointer to memory allocated on the stack for val_ eigen_scalar* adj_mem_; // Pointer to memory allocated on the stack for adj_ /** @@ -229,15 +218,15 @@ class vari_value::value>> static constexpr Eigen::Index ColsAtCompileTime = Scalar::ColsAtCompileTime; /** - * The adjoint of this variable, which is the partial derivative - * of this variable with respect to the root variable. + * The value of this variable. */ - eigen_map adj_; + const eigen_map val_; /** - * The value of this variable. + * The adjoint of this variable, which is the partial derivative + * of this variable with respect to the root variable. */ - const eigen_map val_; + eigen_map adj_; /** * Construct a dense Eigen variable implementation from a value. The @@ -258,8 +247,8 @@ class vari_value::value>> x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), - adj_(make_adj(x)), - val_(make_val(x)) { + val_(eigen_map(val_mem_, x.rows(), x.cols()) = x), + adj_(eigen_map(adj_mem_, x.rows(), x.cols()).setZero()) { ChainableStack::instance_->var_stack_.push_back(this); } @@ -286,8 +275,8 @@ class vari_value::value>> x.size())), adj_mem_(ChainableStack::instance_->memalloc_.alloc_array( x.size())), - adj_(make_adj(x)), - val_(make_val(x)) { + val_(eigen_map(val_mem_, x.rows(), x.cols()) = x), + adj_(eigen_map(adj_mem_, x.rows(), x.cols()).setZero()) { if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); } else { @@ -315,7 +304,7 @@ class vari_value::value>> * propagating derivatives, setting the derivative of the * result with respect to itself to be 1. */ - void init_dependent() { adj_.setOnes(); } + inline void init_dependent() { adj_.setOnes(); } /** * Set the adjoint value of this variable to 0. This is used to @@ -370,26 +359,6 @@ class vari_value::value>> friend class var_value; template friend class vari_value; - /** - * Create the map to the val_ stack allocated memory for an Eigen input. - * @tparam S an Eigen type. - * @param x The Eigen type whose values will be assigned to `val_` - */ - template - inline eigen_map make_val(S&& x) { - eigen_map(val_mem_, x.rows(), x.cols()) = x; - return eigen_map(val_mem_, x.rows(), x.cols()); - } - /** - * Create the map to the adj_ stack allocated memory for an Eigen input. - * @tparam S an Eigen type. - * @param x The Eigen type whose dimensions are set the `adj_`s dimensions. - */ - template - inline eigen_map make_adj(S&& x) { - eigen_map(adj_mem_, x.rows(), x.cols()).setZero(); - return eigen_map(adj_mem_, x.rows(), x.cols()); - } }; /** @@ -409,8 +378,6 @@ class vari_value::value>> public: using PlainObject = std::decay_t>; // Base type of Eigen class - using eigen_scalar = value_type_t; // Scalar type of Eigen class - using eigen_index = typename PlainObject::StorageIndex; // Index type using Scalar = PlainObject; // vari's adj_ and val_ member type using value_type = Scalar; // vari's adj_ and val_ member type /** @@ -490,6 +457,7 @@ class vari_value::value>> * Return the size of this class's `val_` member */ const Eigen::Index size() const { return val_.size(); } + void chain() {} /** * Initialize the adjoint for this (dependent) variable to 1. @@ -562,6 +530,8 @@ class vari_value::value>> private: template friend class var_value; + template + friend class vari_value; }; } // namespace math diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 35914078705..0c24ad72cb2 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -15,6 +15,8 @@ struct AgradRev : public testing::Test { } }; +namespace stan { + namespace test { template void ctor_overloads_float_impl() { using stan::math::var_value; @@ -56,12 +58,6 @@ void ctor_overloads_float() { ctor_overloads_float_impl(); } -TEST_F(AgradRev, ctorfloatOverloads) { - ctor_overloads_float(); - ctor_overloads_float(); - ctor_overloads_float(); -} - template void ctor_overloads_matrix(EigenMat&& xx) { using stan::math::var_value; @@ -84,24 +80,6 @@ void ctor_overloads_matrix(EigenMat&& xx) { test_var_x.adj()); } -auto make_sparse_matrix_random(int rows, int cols) { - using eigen_triplet = Eigen::Triplet; - boost::mt19937 gen; - boost::random::uniform_real_distribution dist(0.0, 1.0); - std::vector tripletList; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - auto v_ij = dist(gen); - if (v_ij < 0.1) { - tripletList.push_back(eigen_triplet(i, j, v_ij)); - } - } - } - Eigen::SparseMatrix mat(rows, cols); - mat.setFromTriplets(tripletList.begin(), tripletList.end()); - return mat; -} - template void ctor_overloads_sparse_matrix(EigenMat&& x) { using stan::math::var_value; @@ -160,12 +138,20 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { } } +} +} +TEST_F(AgradRev, ctorfloatOverloads) { + stan::test::ctor_overloads_float(); + stan::test::ctor_overloads_float(); + stan::test::ctor_overloads_float(); +} + TEST_F(AgradRev, ctormatrixOverloads) { using dense_mat = Eigen::Matrix; using sparse_mat = Eigen::SparseMatrix; - ctor_overloads_matrix(dense_mat::Random(10, 10)); - sparse_mat sparse_x = make_sparse_matrix_random(10, 10); - ctor_overloads_sparse_matrix(sparse_x); + stan::test::ctor_overloads_matrix(dense_mat::Random(10, 10)); + sparse_mat sparse_x = stan::test::make_sparse_matrix_random(10, 10); + stan::test::ctor_overloads_sparse_matrix(sparse_x); } TEST_F(AgradRev, a_eq_x) { diff --git a/test/unit/math/rev/core/vari_test.cpp b/test/unit/math/rev/core/vari_test.cpp index c8961eb62ae..4ad4497c010 100644 --- a/test/unit/math/rev/core/vari_test.cpp +++ b/test/unit/math/rev/core/vari_test.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -18,5 +19,41 @@ TEST(AgradRev, long_double_test) { TEST(AgradRev, dense_matrix_vari) { using stan::math::vari_value; - stan::math::vari_value A(Eigen::MatrixXd::Random(3, 3)); + using eig_mat = Eigen::MatrixXd; + vari_value A_vari(eig_mat::Random(3, 3)); + eig_mat B(eig_mat::Random(3, 3)); + vari_value B_vari(B); + EXPECT_MATRIX_FLOAT_EQ(B, B_vari.val_); +} +TEST(AgradRev, dense_vector_vari) { + using stan::math::vari_value; + using eig_vec = Eigen::Matrix; + vari_value A_vari(eig_vec::Random(3)); + eig_vec B(eig_vec::Random(3)); + vari_value B_vari(B); + EXPECT_MATRIX_FLOAT_EQ(B, B_vari.val_); +} + +TEST(AgradRev, dense_row_vector_vari) { + using stan::math::vari_value; + using eig_row_vec = Eigen::Matrix; + vari_value A_vari(eig_row_vec::Random(3)); + eig_row_vec B(eig_row_vec::Random(3)); + vari_value B_vari(B); + EXPECT_MATRIX_FLOAT_EQ(B, B_vari.val_); +} + +TEST(AgradRev, sparse_matrix_vari) { + using stan::math::vari_value; + using eig_mat = Eigen::SparseMatrix; + using inner_iterator = typename eig_mat::InnerIterator; + using stan::test::make_sparse_matrix_random; + vari_value A_vari(make_sparse_matrix_random(10, 10)); + eig_mat B = make_sparse_matrix_random(10, 10); + vari_value B_vari(B); + for (int k = 0; k < B.outerSize(); ++k) { + for (inner_iterator it(B, k), iz(B_vari.val_, k); it; ++it, ++iz) { + EXPECT_FLOAT_EQ(iz.value(), it.value()); + } + } } diff --git a/test/unit/util.hpp b/test/unit/util.hpp index 4f0bf854254..b8746686e56 100644 --- a/test/unit/util.hpp +++ b/test/unit/util.hpp @@ -2,6 +2,8 @@ #define TEST_UNIT_UTIL_HPP #include +#include +#include #include #include #include @@ -195,6 +197,25 @@ void expect_type_matrix(const Eigen::Matrix& x) { EXPECT_EQ(Eigen::Dynamic, C); EXPECT_EQ(Eigen::Dynamic, R); } + +auto make_sparse_matrix_random(int rows, int cols) { + using eigen_triplet = Eigen::Triplet; + boost::mt19937 gen; + boost::random::uniform_real_distribution dist(0.0, 1.0); + std::vector tripletList; + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + auto v_ij = dist(gen); + if (v_ij < 0.1) { + tripletList.push_back(eigen_triplet(i, j, v_ij)); + } + } + } + Eigen::SparseMatrix mat(rows, cols); + mat.setFromTriplets(tripletList.begin(), tripletList.end()); + return mat; +} + } // namespace test } // namespace stan From 896b267b75d12100845ca5246cd893fc078c1f47 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 6 Jul 2020 15:43:37 -0400 Subject: [PATCH 139/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/var.hpp | 2 +- stan/math/rev/core/vari.hpp | 6 +++--- test/unit/math/rev/core/var_test.cpp | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index a356aa7100a..75535f0c899 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -42,7 +42,7 @@ class var_value { "The template for must be a floating point or a container holding" " floating point types"); static_assert(std::is_reference::value, - "The template for a var_value must not be a reference!"); + "The template for a var_value must not be a reference!"); public: using value_type = std::decay_t; // type in vari_value. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 69e5ba2f802..c0d1bc69dca 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -377,9 +377,9 @@ class vari_value::value>> : public vari_base, chainable_alloc { public: using PlainObject - = std::decay_t>; // Base type of Eigen class - using Scalar = PlainObject; // vari's adj_ and val_ member type - using value_type = Scalar; // vari's adj_ and val_ member type + = std::decay_t>; // Base type of Eigen class + using Scalar = PlainObject; // vari's adj_ and val_ member type + using value_type = Scalar; // vari's adj_ and val_ member type /** * Rows at compile time */ diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 0c24ad72cb2..d6731d59ffd 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -16,7 +16,7 @@ struct AgradRev : public testing::Test { }; namespace stan { - namespace test { +namespace test { template void ctor_overloads_float_impl() { using stan::math::var_value; @@ -138,8 +138,8 @@ void ctor_overloads_sparse_matrix(EigenMat&& x) { } } -} -} +} // namespace test +} // namespace stan TEST_F(AgradRev, ctorfloatOverloads) { stan::test::ctor_overloads_float(); stan::test::ctor_overloads_float(); From 950498b5705481d7b6b3fe2a740a448c23779302 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 6 Jul 2020 17:42:28 -0400 Subject: [PATCH 140/167] update var templates --- stan/math/rev/core/operator_divide_equal.hpp | 4 ++-- stan/math/rev/core/operator_minus_equal.hpp | 4 ++-- stan/math/rev/core/operator_multiply_equal.hpp | 4 ++-- stan/math/rev/core/operator_plus_equal.hpp | 4 ++-- stan/math/rev/core/var.hpp | 5 +---- stan/math/rev/core/vari.hpp | 8 ++++---- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 306dbf4d2b0..71772987fcd 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -8,14 +8,14 @@ namespace stan { namespace math { template -inline var_value& var_value>::operator/=( +inline var_value& var_value::operator/=( const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value>::operator/=( +inline var_value& var_value::operator/=( T b) { if (b == 1.0) { return *this; diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 6be93a516e2..5ad0e377c9a 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -9,14 +9,14 @@ namespace stan { namespace math { template -inline var_value& var_value>::operator-=( +inline var_value& var_value::operator-=( const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value>::operator-=( +inline var_value& var_value::operator-=( T b) { if (b == 0.0) { return *this; diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 91e2cc06e38..41c4dfde6f2 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -9,14 +9,14 @@ namespace stan { namespace math { template -inline var_value& var_value>::operator*=( +inline var_value& var_value::operator*=( const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value>::operator*=( +inline var_value& var_value::operator*=( T b) { if (b == 1.0) { return *this; diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index 2c58bf9fa2f..f50ac886045 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -9,14 +9,14 @@ namespace stan { namespace math { template -inline var_value& var_value>::operator+=( +inline var_value& var_value::operator+=( const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value>::operator+=( +inline var_value& var_value::operator+=( T b) { if (b == 0.0) { return *this; diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 75535f0c899..4472b6df5ed 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -41,9 +41,6 @@ class var_value { std::is_floating_point>::value, "The template for must be a floating point or a container holding" " floating point types"); - static_assert(std::is_reference::value, - "The template for a var_value must not be a reference!"); - public: using value_type = std::decay_t; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. @@ -66,7 +63,7 @@ class var_value { * @return true if this variable does not yet have * a defined variable. */ - using bool is_uninitialized() { return (vi_ == nullptr); } + inline bool is_uninitialized() { return (vi_ == nullptr); } /** * Construct a variable for later assignment. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index c0d1bc69dca..fe253f8293a 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -11,7 +11,7 @@ namespace stan { namespace math { // forward declaration of var -template +template class var_value; /** * Abstract base class that all `vari_value` and it's derived classes inherit. @@ -167,7 +167,7 @@ class vari_value> : public vari_base { } private: - template + template friend class var_value; }; @@ -355,7 +355,7 @@ class vari_value> : public vari_base { } private: - template + template friend class var_value; template friend class vari_value; @@ -528,7 +528,7 @@ class vari_value::value>> } private: - template + template friend class var_value; template friend class vari_value; From 9cfb8a928519e8ccfa0c8478c9f2807f2fb9ee8e Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 6 Jul 2020 17:58:39 -0400 Subject: [PATCH 141/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/core/operator_divide_equal.hpp | 6 ++---- stan/math/rev/core/operator_minus_equal.hpp | 6 ++---- stan/math/rev/core/operator_multiply_equal.hpp | 6 ++---- stan/math/rev/core/operator_plus_equal.hpp | 6 ++---- stan/math/rev/core/var.hpp | 1 + 5 files changed, 9 insertions(+), 16 deletions(-) diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index 71772987fcd..5374ad3467b 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -8,15 +8,13 @@ namespace stan { namespace math { template -inline var_value& var_value::operator/=( - const var_value& b) { +inline var_value& var_value::operator/=(const var_value& b) { vi_ = new internal::divide_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value::operator/=( - T b) { +inline var_value& var_value::operator/=(T b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 5ad0e377c9a..2a9d33dea2b 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -9,15 +9,13 @@ namespace stan { namespace math { template -inline var_value& var_value::operator-=( - const var_value& b) { +inline var_value& var_value::operator-=(const var_value& b) { vi_ = new internal::subtract_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value::operator-=( - T b) { +inline var_value& var_value::operator-=(T b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index 41c4dfde6f2..45035a6acfe 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -9,15 +9,13 @@ namespace stan { namespace math { template -inline var_value& var_value::operator*=( - const var_value& b) { +inline var_value& var_value::operator*=(const var_value& b) { vi_ = new internal::multiply_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value::operator*=( - T b) { +inline var_value& var_value::operator*=(T b) { if (b == 1.0) { return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index f50ac886045..769a96d0db1 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -9,15 +9,13 @@ namespace stan { namespace math { template -inline var_value& var_value::operator+=( - const var_value& b) { +inline var_value& var_value::operator+=(const var_value& b) { vi_ = new internal::add_vv_vari(vi_, b.vi_); return *this; } template -inline var_value& var_value::operator+=( - T b) { +inline var_value& var_value::operator+=(T b) { if (b == 0.0) { return *this; } diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 4472b6df5ed..9fb4aeee2db 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -41,6 +41,7 @@ class var_value { std::is_floating_point>::value, "The template for must be a floating point or a container holding" " floating point types"); + public: using value_type = std::decay_t; // type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. From f02d232a9064ec9e472f8a3d1d43f943ac4a7bb5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 7 Jul 2020 16:08:09 -0400 Subject: [PATCH 142/167] kickoff jenkins --- test/unit/math/rev/core/var_test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index d6731d59ffd..6ff5698a352 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -64,6 +64,7 @@ void ctor_overloads_matrix(EigenMat&& xx) { using stan::math::vari_value; using stan::math::test::type_name; using eigen_plain = std::decay_t>; + eigen_plain x = xx; // standard constructor EXPECT_MATRIX_FLOAT_EQ((x * x).eval(), var_value(x * x).val()); From 2d1928acfe13f1680a570077165085b8ee788f17 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 7 Jul 2020 16:28:41 -0400 Subject: [PATCH 143/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- test/unit/math/rev/core/var_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 6ff5698a352..2d83471b324 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -64,7 +64,7 @@ void ctor_overloads_matrix(EigenMat&& xx) { using stan::math::vari_value; using stan::math::test::type_name; using eigen_plain = std::decay_t>; - + eigen_plain x = xx; // standard constructor EXPECT_MATRIX_FLOAT_EQ((x * x).eval(), var_value(x * x).val()); From c8fd050d022743c54739f95dbad2a254991fac76 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 8 Jul 2020 19:42:59 -0400 Subject: [PATCH 144/167] move Eigen numtraits and read_var to core to help with include order errors --- stan/math/rev/core.hpp | 3 +++ stan/math/rev/{fun => core}/Eigen_NumTraits.hpp | 6 +++--- stan/math/rev/core/gevv_vvv_vari.hpp | 4 ++-- stan/math/rev/core/matrix_vari.hpp | 2 +- stan/math/rev/{fun => core}/read_var.hpp | 0 stan/math/rev/core/var.hpp | 4 +--- stan/math/rev/core/vari.hpp | 4 +--- stan/math/rev/fun.hpp | 4 +--- stan/math/rev/fun/grad.hpp | 2 +- stan/math/rev/fun/tcrossprod.hpp | 2 +- stan/math/rev/meta/is_var.hpp | 5 ++++- stan/math/rev/meta/is_vari.hpp | 5 ++++- stan/math/rev/meta/partials_type.hpp | 2 +- 13 files changed, 23 insertions(+), 20 deletions(-) rename stan/math/rev/{fun => core}/Eigen_NumTraits.hpp (99%) rename stan/math/rev/{fun => core}/read_var.hpp (100%) diff --git a/stan/math/rev/core.hpp b/stan/math/rev/core.hpp index 33eb934c08e..88ca7b2f2c5 100644 --- a/stan/math/rev/core.hpp +++ b/stan/math/rev/core.hpp @@ -1,6 +1,8 @@ #ifndef STAN_MATH_REV_CORE_HPP #define STAN_MATH_REV_CORE_HPP +#include + #include #include #include @@ -48,6 +50,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/rev/fun/Eigen_NumTraits.hpp b/stan/math/rev/core/Eigen_NumTraits.hpp similarity index 99% rename from stan/math/rev/fun/Eigen_NumTraits.hpp rename to stan/math/rev/core/Eigen_NumTraits.hpp index 1e6b3140427..68cd6ef1442 100644 --- a/stan/math/rev/fun/Eigen_NumTraits.hpp +++ b/stan/math/rev/core/Eigen_NumTraits.hpp @@ -2,10 +2,10 @@ #define STAN_MATH_REV_FUN_EIGEN_NUMTRAITS_HPP #include -#include -#include #include -#include +#include +#include +#include #include #include diff --git a/stan/math/rev/core/gevv_vvv_vari.hpp b/stan/math/rev/core/gevv_vvv_vari.hpp index 0505788ad3d..5fad3458a01 100644 --- a/stan/math/rev/core/gevv_vvv_vari.hpp +++ b/stan/math/rev/core/gevv_vvv_vari.hpp @@ -1,9 +1,9 @@ #ifndef STAN_MATH_REV_CORE_GEVV_VVV_VARI_HPP #define STAN_MATH_REV_CORE_GEVV_VVV_VARI_HPP -#include -#include #include +#include +#include namespace stan { namespace math { diff --git a/stan/math/rev/core/matrix_vari.hpp b/stan/math/rev/core/matrix_vari.hpp index 3aca20ff174..09c2cc93128 100644 --- a/stan/math/rev/core/matrix_vari.hpp +++ b/stan/math/rev/core/matrix_vari.hpp @@ -1,7 +1,7 @@ #ifndef STAN_MATH_REV_CORE_MATRIX_VARI_HPP #define STAN_MATH_REV_CORE_MATRIX_VARI_HPP -#include +#include #include #include #include diff --git a/stan/math/rev/fun/read_var.hpp b/stan/math/rev/core/read_var.hpp similarity index 100% rename from stan/math/rev/fun/read_var.hpp rename to stan/math/rev/core/read_var.hpp diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 9fb4aeee2db..98660c12519 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -1,11 +1,9 @@ #ifndef STAN_MATH_REV_CORE_VAR_HPP #define STAN_MATH_REV_CORE_VAR_HPP +#include #include -#include #include -#include -#include #include #include diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index fe253f8293a..0af28d897e4 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -1,9 +1,9 @@ #ifndef STAN_MATH_REV_CORE_VARI_HPP #define STAN_MATH_REV_CORE_VARI_HPP +#include #include #include -#include #include #include @@ -33,8 +33,6 @@ class vari_base { virtual ~vari_base() noexcept {} }; -template -class vari_value; /** * The variable implementation for floating point types. * diff --git a/stan/math/rev/fun.hpp b/stan/math/rev/fun.hpp index 7aeaa486f67..8b5b8620b9f 100644 --- a/stan/math/rev/fun.hpp +++ b/stan/math/rev/fun.hpp @@ -2,8 +2,7 @@ #define STAN_MATH_REV_FUN_HPP #include -#include - +#include #include #include @@ -118,7 +117,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/rev/fun/grad.hpp b/stan/math/rev/fun/grad.hpp index 099e641337a..31853a673f2 100644 --- a/stan/math/rev/fun/grad.hpp +++ b/stan/math/rev/fun/grad.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_REV_FUN_GRAD_HPP #include -#include +#include #include #include diff --git a/stan/math/rev/fun/tcrossprod.hpp b/stan/math/rev/fun/tcrossprod.hpp index 02b57159a9f..08961cabe73 100644 --- a/stan/math/rev/fun/tcrossprod.hpp +++ b/stan/math/rev/fun/tcrossprod.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/stan/math/rev/meta/is_var.hpp b/stan/math/rev/meta/is_var.hpp index e4d9fa54fed..5bb004524bb 100644 --- a/stan/math/rev/meta/is_var.hpp +++ b/stan/math/rev/meta/is_var.hpp @@ -1,12 +1,15 @@ #ifndef STAN_MATH_REV_META_IS_VAR_HPP #define STAN_MATH_REV_META_IS_VAR_HPP -#include #include #include namespace stan { +namespace math { + template + class var_value; +} namespace internal { template struct is_var_impl : std::false_type {}; diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index 39fb79cb767..4e7ddcc8315 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -1,12 +1,15 @@ #ifndef STAN_MATH_REV_META_IS_VARI_HPP #define STAN_MATH_REV_META_IS_VARI_HPP -#include #include #include namespace stan { +namespace math { + template + class vari_value; +} namespace internal { template struct is_vari_impl : std::false_type {}; diff --git a/stan/math/rev/meta/partials_type.hpp b/stan/math/rev/meta/partials_type.hpp index 823117fe062..1975c331ef1 100644 --- a/stan/math/rev/meta/partials_type.hpp +++ b/stan/math/rev/meta/partials_type.hpp @@ -1,9 +1,9 @@ #ifndef STAN_MATH_REV_META_PARTIALS_TYPE_HPP #define STAN_MATH_REV_META_PARTIALS_TYPE_HPP -#include #include #include +#include #include namespace stan { From 9660a5a18f620b1249f2478f4e8a4358011b8b83 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 8 Jul 2020 19:44:33 -0400 Subject: [PATCH 145/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/rev/meta/is_var.hpp | 4 ++-- stan/math/rev/meta/is_vari.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stan/math/rev/meta/is_var.hpp b/stan/math/rev/meta/is_var.hpp index 5bb004524bb..f0f3855bc7f 100644 --- a/stan/math/rev/meta/is_var.hpp +++ b/stan/math/rev/meta/is_var.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { - template - class var_value; +template +class var_value; } namespace internal { template diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index 4e7ddcc8315..db786456202 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { - template - class vari_value; +template +class vari_value; } namespace internal { template From 826d1d545eae2ec53a456bb110f599a5119b18ab Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 9 Jul 2020 00:10:06 -0400 Subject: [PATCH 146/167] move Eigen Numtraits and typedefs to core for fwd and rev --- stan/math/fwd/core.hpp | 3 +++ stan/math/fwd/{fun => core}/Eigen_NumTraits.hpp | 4 ++-- stan/math/fwd/{fun => core}/read_fvar.hpp | 0 stan/math/fwd/core/std_iterator_traits.hpp | 2 +- stan/math/fwd/{fun => core}/typedefs.hpp | 8 ++++---- stan/math/fwd/fun.hpp | 4 +--- stan/math/fwd/fun/mdivide_left_tri_low.hpp | 3 +-- stan/math/fwd/fun/multiply.hpp | 3 +-- stan/math/mix/fun/typedefs.hpp | 6 +++--- stan/math/rev/core.hpp | 1 + test/unit/math/rev/{fun => core}/read_var_test.cpp | 0 test/unit/math/rev/meta/VectorBuilderHelper_test.cpp | 2 +- test/unit/math/rev/meta/VectorBuilder_test.cpp | 2 +- test/unit/math/rev/meta/ad_promotable_test.cpp | 2 +- test/unit/math/rev/meta/child_type_test.cpp | 2 +- test/unit/math/rev/meta/include_summand_test.cpp | 2 +- test/unit/math/rev/meta/is_constant_all_test.cpp | 3 ++- test/unit/math/rev/meta/partials_return_type_test.cpp | 2 +- test/unit/math/rev/meta/partials_type_test.cpp | 2 +- test/unit/math/rev/meta/require_generics_test.cpp | 3 +-- 20 files changed, 27 insertions(+), 27 deletions(-) rename stan/math/fwd/{fun => core}/Eigen_NumTraits.hpp (98%) rename stan/math/fwd/{fun => core}/read_fvar.hpp (100%) rename stan/math/fwd/{fun => core}/typedefs.hpp (79%) rename test/unit/math/rev/{fun => core}/read_var_test.cpp (100%) diff --git a/stan/math/fwd/core.hpp b/stan/math/fwd/core.hpp index 543a8206bf4..9ae0d7ac1fe 100644 --- a/stan/math/fwd/core.hpp +++ b/stan/math/fwd/core.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_FWD_CORE_HPP #include +#include #include #include #include @@ -17,8 +18,10 @@ #include #include #include +#include #include #include #include +#include #endif diff --git a/stan/math/fwd/fun/Eigen_NumTraits.hpp b/stan/math/fwd/core/Eigen_NumTraits.hpp similarity index 98% rename from stan/math/fwd/fun/Eigen_NumTraits.hpp rename to stan/math/fwd/core/Eigen_NumTraits.hpp index 8612324d68a..9bc6f09275e 100644 --- a/stan/math/fwd/fun/Eigen_NumTraits.hpp +++ b/stan/math/fwd/core/Eigen_NumTraits.hpp @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include #include diff --git a/stan/math/fwd/fun/read_fvar.hpp b/stan/math/fwd/core/read_fvar.hpp similarity index 100% rename from stan/math/fwd/fun/read_fvar.hpp rename to stan/math/fwd/core/read_fvar.hpp diff --git a/stan/math/fwd/core/std_iterator_traits.hpp b/stan/math/fwd/core/std_iterator_traits.hpp index 0caffd3e034..e3220e81108 100644 --- a/stan/math/fwd/core/std_iterator_traits.hpp +++ b/stan/math/fwd/core/std_iterator_traits.hpp @@ -1,8 +1,8 @@ #ifndef STAN_MATH_FWD_CORE_STD_ITERATOR_TRAITS_HPP #define STAN_MATH_FWD_CORE_STD_ITERATOR_TRAITS_HPP -#include #include +#include #include namespace std { diff --git a/stan/math/fwd/fun/typedefs.hpp b/stan/math/fwd/core/typedefs.hpp similarity index 79% rename from stan/math/fwd/fun/typedefs.hpp rename to stan/math/fwd/core/typedefs.hpp index bd8ab2d84e4..8e5ebd27320 100644 --- a/stan/math/fwd/fun/typedefs.hpp +++ b/stan/math/fwd/core/typedefs.hpp @@ -1,9 +1,9 @@ -#ifndef STAN_MATH_FWD_FUN_TYPEDEFS_HPP -#define STAN_MATH_FWD_FUN_TYPEDEFS_HPP +#ifndef STAN_MATH_FWD_CORE_TYPEDEFS_HPP +#define STAN_MATH_FWD_CORE_TYPEDEFS_HPP #include -#include -#include +#include +#include namespace stan { namespace math { diff --git a/stan/math/fwd/fun.hpp b/stan/math/fwd/fun.hpp index e10e9da72f1..91aafac3272 100644 --- a/stan/math/fwd/fun.hpp +++ b/stan/math/fwd/fun.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_FWD_FUN_HPP #include -#include +#include #include #include @@ -94,7 +94,6 @@ #include #include #include -#include #include #include #include @@ -111,7 +110,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/fwd/fun/mdivide_left_tri_low.hpp b/stan/math/fwd/fun/mdivide_left_tri_low.hpp index c2c19d8ee67..a172d19818c 100644 --- a/stan/math/fwd/fun/mdivide_left_tri_low.hpp +++ b/stan/math/fwd/fun/mdivide_left_tri_low.hpp @@ -4,10 +4,9 @@ #include #include #include -#include +#include #include #include -#include namespace stan { namespace math { diff --git a/stan/math/fwd/fun/multiply.hpp b/stan/math/fwd/fun/multiply.hpp index ebb03db7b07..71bda19187b 100644 --- a/stan/math/fwd/fun/multiply.hpp +++ b/stan/math/fwd/fun/multiply.hpp @@ -1,12 +1,11 @@ #ifndef STAN_MATH_FWD_FUN_MULTIPLY_HPP #define STAN_MATH_FWD_FUN_MULTIPLY_HPP +#include #include #include -#include #include #include -#include namespace stan { namespace math { diff --git a/stan/math/mix/fun/typedefs.hpp b/stan/math/mix/fun/typedefs.hpp index ddf7ea5d793..88370cfb82f 100644 --- a/stan/math/mix/fun/typedefs.hpp +++ b/stan/math/mix/fun/typedefs.hpp @@ -1,10 +1,10 @@ -#ifndef STAN_MATH_MIX_MAT_FUN_TYPEDEFS_HPP -#define STAN_MATH_MIX_MAT_FUN_TYPEDEFS_HPP +#ifndef STAN_MATH_MIX_MAT_CORE_TYPEDEFS_HPP +#define STAN_MATH_MIX_MAT_CORE_TYPEDEFS_HPP #include #include +#include #include -#include namespace stan { namespace math { diff --git a/stan/math/rev/core.hpp b/stan/math/rev/core.hpp index 88ca7b2f2c5..36f0b4f2349 100644 --- a/stan/math/rev/core.hpp +++ b/stan/math/rev/core.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_REV_CORE_HPP #include +#include #include #include diff --git a/test/unit/math/rev/fun/read_var_test.cpp b/test/unit/math/rev/core/read_var_test.cpp similarity index 100% rename from test/unit/math/rev/fun/read_var_test.cpp rename to test/unit/math/rev/core/read_var_test.cpp diff --git a/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp b/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp index 70d8c464102..46fa4649963 100644 --- a/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp +++ b/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/VectorBuilder_test.cpp b/test/unit/math/rev/meta/VectorBuilder_test.cpp index 557c21b1de6..c7c470d815c 100644 --- a/test/unit/math/rev/meta/VectorBuilder_test.cpp +++ b/test/unit/math/rev/meta/VectorBuilder_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/ad_promotable_test.cpp b/test/unit/math/rev/meta/ad_promotable_test.cpp index 3f21820fe1b..4ffcddb7222 100644 --- a/test/unit/math/rev/meta/ad_promotable_test.cpp +++ b/test/unit/math/rev/meta/ad_promotable_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/child_type_test.cpp b/test/unit/math/rev/meta/child_type_test.cpp index 8e6112fc0ba..afb585c4386 100644 --- a/test/unit/math/rev/meta/child_type_test.cpp +++ b/test/unit/math/rev/meta/child_type_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/include_summand_test.cpp b/test/unit/math/rev/meta/include_summand_test.cpp index 69ba28a7081..0d9dba66322 100644 --- a/test/unit/math/rev/meta/include_summand_test.cpp +++ b/test/unit/math/rev/meta/include_summand_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/test/unit/math/rev/meta/is_constant_all_test.cpp b/test/unit/math/rev/meta/is_constant_all_test.cpp index 771eea6d8c9..ef703907916 100644 --- a/test/unit/math/rev/meta/is_constant_all_test.cpp +++ b/test/unit/math/rev/meta/is_constant_all_test.cpp @@ -1,4 +1,5 @@ -#include +#include +#include #include #include diff --git a/test/unit/math/rev/meta/partials_return_type_test.cpp b/test/unit/math/rev/meta/partials_return_type_test.cpp index 8eed06a2e8f..43e265137fb 100644 --- a/test/unit/math/rev/meta/partials_return_type_test.cpp +++ b/test/unit/math/rev/meta/partials_return_type_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/test/unit/math/rev/meta/partials_type_test.cpp b/test/unit/math/rev/meta/partials_type_test.cpp index d3187a0ec47..aaeb2a7e81f 100644 --- a/test/unit/math/rev/meta/partials_type_test.cpp +++ b/test/unit/math/rev/meta/partials_type_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include TEST(MetaTraitsRevScal, partials_type) { diff --git a/test/unit/math/rev/meta/require_generics_test.cpp b/test/unit/math/rev/meta/require_generics_test.cpp index ee93fe9b5a7..c4304af0abc 100644 --- a/test/unit/math/rev/meta/require_generics_test.cpp +++ b/test/unit/math/rev/meta/require_generics_test.cpp @@ -1,7 +1,6 @@ -#include // just this is bad #include -#include +#include #include #include #include From fb317b7bf22dd348b6a4b824e7cdc8da7f5d24cb Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 9 Jul 2020 18:09:22 -0400 Subject: [PATCH 147/167] move around fvar Eigen_Numtraits --- stan/math/fwd/core/Eigen_NumTraits.hpp | 1 - stan/math/fwd/meta/is_fvar.hpp | 5 ++++- stan/math/fwd/meta/partials_type.hpp | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/stan/math/fwd/core/Eigen_NumTraits.hpp b/stan/math/fwd/core/Eigen_NumTraits.hpp index 9bc6f09275e..12d4d04b72e 100644 --- a/stan/math/fwd/core/Eigen_NumTraits.hpp +++ b/stan/math/fwd/core/Eigen_NumTraits.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_FWD_FUN_EIGEN_NUMTRAITS_HPP #include -#include #include #include #include diff --git a/stan/math/fwd/meta/is_fvar.hpp b/stan/math/fwd/meta/is_fvar.hpp index c767583db46..6e62d94227e 100644 --- a/stan/math/fwd/meta/is_fvar.hpp +++ b/stan/math/fwd/meta/is_fvar.hpp @@ -1,12 +1,15 @@ #ifndef STAN_MATH_FWD_META_IS_FVAR_HPP #define STAN_MATH_FWD_META_IS_FVAR_HPP -#include #include #include namespace stan { +namespace math { + template + struct fvar; +} namespace internal { template struct is_fvar_impl : std::false_type {}; diff --git a/stan/math/fwd/meta/partials_type.hpp b/stan/math/fwd/meta/partials_type.hpp index 66ea6c5d32a..96fb08921e5 100644 --- a/stan/math/fwd/meta/partials_type.hpp +++ b/stan/math/fwd/meta/partials_type.hpp @@ -1,12 +1,15 @@ #ifndef STAN_MATH_FWD_META_PARTIALS_TYPE_HPP #define STAN_MATH_FWD_META_PARTIALS_TYPE_HPP -#include #include #include #include namespace stan { +namespace math { + template + struct fvar; +} template struct partials_type> { From eeeb4ee80603957e5c80f8a8e8b51aec97ed9f8c Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 9 Jul 2020 18:10:11 -0400 Subject: [PATCH 148/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) --- stan/math/fwd/meta/is_fvar.hpp | 4 ++-- stan/math/fwd/meta/partials_type.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stan/math/fwd/meta/is_fvar.hpp b/stan/math/fwd/meta/is_fvar.hpp index 6e62d94227e..e2f4e51e088 100644 --- a/stan/math/fwd/meta/is_fvar.hpp +++ b/stan/math/fwd/meta/is_fvar.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { - template - struct fvar; +template +struct fvar; } namespace internal { template diff --git a/stan/math/fwd/meta/partials_type.hpp b/stan/math/fwd/meta/partials_type.hpp index 96fb08921e5..4d80c547f57 100644 --- a/stan/math/fwd/meta/partials_type.hpp +++ b/stan/math/fwd/meta/partials_type.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { - template - struct fvar; +template +struct fvar; } template From 58ca7e234373b0b0223f58001c2254e01d526790 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 13 Jul 2020 10:58:47 -0400 Subject: [PATCH 149/167] Revert "[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)" This reverts commit eeeb4ee80603957e5c80f8a8e8b51aec97ed9f8c. --- stan/math/fwd/meta/is_fvar.hpp | 4 ++-- stan/math/fwd/meta/partials_type.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stan/math/fwd/meta/is_fvar.hpp b/stan/math/fwd/meta/is_fvar.hpp index e2f4e51e088..6e62d94227e 100644 --- a/stan/math/fwd/meta/is_fvar.hpp +++ b/stan/math/fwd/meta/is_fvar.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { -template -struct fvar; + template + struct fvar; } namespace internal { template diff --git a/stan/math/fwd/meta/partials_type.hpp b/stan/math/fwd/meta/partials_type.hpp index 4d80c547f57..96fb08921e5 100644 --- a/stan/math/fwd/meta/partials_type.hpp +++ b/stan/math/fwd/meta/partials_type.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { -template -struct fvar; + template + struct fvar; } template From 60f54abaf2cb8405a74f01ae12dabea47e0e54cc Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 13 Jul 2020 10:58:49 -0400 Subject: [PATCH 150/167] Revert "move around fvar Eigen_Numtraits" This reverts commit fb317b7bf22dd348b6a4b824e7cdc8da7f5d24cb. --- stan/math/fwd/core/Eigen_NumTraits.hpp | 1 + stan/math/fwd/meta/is_fvar.hpp | 5 +---- stan/math/fwd/meta/partials_type.hpp | 5 +---- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/stan/math/fwd/core/Eigen_NumTraits.hpp b/stan/math/fwd/core/Eigen_NumTraits.hpp index 12d4d04b72e..9bc6f09275e 100644 --- a/stan/math/fwd/core/Eigen_NumTraits.hpp +++ b/stan/math/fwd/core/Eigen_NumTraits.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_FWD_FUN_EIGEN_NUMTRAITS_HPP #include +#include #include #include #include diff --git a/stan/math/fwd/meta/is_fvar.hpp b/stan/math/fwd/meta/is_fvar.hpp index 6e62d94227e..c767583db46 100644 --- a/stan/math/fwd/meta/is_fvar.hpp +++ b/stan/math/fwd/meta/is_fvar.hpp @@ -1,15 +1,12 @@ #ifndef STAN_MATH_FWD_META_IS_FVAR_HPP #define STAN_MATH_FWD_META_IS_FVAR_HPP +#include #include #include namespace stan { -namespace math { - template - struct fvar; -} namespace internal { template struct is_fvar_impl : std::false_type {}; diff --git a/stan/math/fwd/meta/partials_type.hpp b/stan/math/fwd/meta/partials_type.hpp index 96fb08921e5..66ea6c5d32a 100644 --- a/stan/math/fwd/meta/partials_type.hpp +++ b/stan/math/fwd/meta/partials_type.hpp @@ -1,15 +1,12 @@ #ifndef STAN_MATH_FWD_META_PARTIALS_TYPE_HPP #define STAN_MATH_FWD_META_PARTIALS_TYPE_HPP +#include #include #include #include namespace stan { -namespace math { - template - struct fvar; -} template struct partials_type> { From d7bd3f582cd14bde58ebf658d66bb16191e54e4f Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 13 Jul 2020 10:58:50 -0400 Subject: [PATCH 151/167] Revert "move Eigen Numtraits and typedefs to core for fwd and rev" This reverts commit 826d1d545eae2ec53a456bb110f599a5119b18ab. --- stan/math/fwd/core.hpp | 3 --- stan/math/fwd/core/std_iterator_traits.hpp | 2 +- stan/math/fwd/fun.hpp | 4 +++- stan/math/fwd/{core => fun}/Eigen_NumTraits.hpp | 4 ++-- stan/math/fwd/fun/mdivide_left_tri_low.hpp | 3 ++- stan/math/fwd/fun/multiply.hpp | 3 ++- stan/math/fwd/{core => fun}/read_fvar.hpp | 0 stan/math/fwd/{core => fun}/typedefs.hpp | 8 ++++---- stan/math/mix/fun/typedefs.hpp | 6 +++--- stan/math/rev/core.hpp | 1 - test/unit/math/rev/{core => fun}/read_var_test.cpp | 0 test/unit/math/rev/meta/VectorBuilderHelper_test.cpp | 2 +- test/unit/math/rev/meta/VectorBuilder_test.cpp | 2 +- test/unit/math/rev/meta/ad_promotable_test.cpp | 2 +- test/unit/math/rev/meta/child_type_test.cpp | 2 +- test/unit/math/rev/meta/include_summand_test.cpp | 2 +- test/unit/math/rev/meta/is_constant_all_test.cpp | 3 +-- test/unit/math/rev/meta/partials_return_type_test.cpp | 2 +- test/unit/math/rev/meta/partials_type_test.cpp | 2 +- test/unit/math/rev/meta/require_generics_test.cpp | 3 ++- 20 files changed, 27 insertions(+), 27 deletions(-) rename stan/math/fwd/{core => fun}/Eigen_NumTraits.hpp (98%) rename stan/math/fwd/{core => fun}/read_fvar.hpp (100%) rename stan/math/fwd/{core => fun}/typedefs.hpp (79%) rename test/unit/math/rev/{core => fun}/read_var_test.cpp (100%) diff --git a/stan/math/fwd/core.hpp b/stan/math/fwd/core.hpp index 9ae0d7ac1fe..543a8206bf4 100644 --- a/stan/math/fwd/core.hpp +++ b/stan/math/fwd/core.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_FWD_CORE_HPP #include -#include #include #include #include @@ -18,10 +17,8 @@ #include #include #include -#include #include #include #include -#include #endif diff --git a/stan/math/fwd/core/std_iterator_traits.hpp b/stan/math/fwd/core/std_iterator_traits.hpp index e3220e81108..0caffd3e034 100644 --- a/stan/math/fwd/core/std_iterator_traits.hpp +++ b/stan/math/fwd/core/std_iterator_traits.hpp @@ -1,8 +1,8 @@ #ifndef STAN_MATH_FWD_CORE_STD_ITERATOR_TRAITS_HPP #define STAN_MATH_FWD_CORE_STD_ITERATOR_TRAITS_HPP -#include #include +#include #include namespace std { diff --git a/stan/math/fwd/fun.hpp b/stan/math/fwd/fun.hpp index 91aafac3272..e10e9da72f1 100644 --- a/stan/math/fwd/fun.hpp +++ b/stan/math/fwd/fun.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_FWD_FUN_HPP #include -#include +#include #include #include @@ -94,6 +94,7 @@ #include #include #include +#include #include #include #include @@ -110,6 +111,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/fwd/core/Eigen_NumTraits.hpp b/stan/math/fwd/fun/Eigen_NumTraits.hpp similarity index 98% rename from stan/math/fwd/core/Eigen_NumTraits.hpp rename to stan/math/fwd/fun/Eigen_NumTraits.hpp index 9bc6f09275e..8612324d68a 100644 --- a/stan/math/fwd/core/Eigen_NumTraits.hpp +++ b/stan/math/fwd/fun/Eigen_NumTraits.hpp @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include #include diff --git a/stan/math/fwd/fun/mdivide_left_tri_low.hpp b/stan/math/fwd/fun/mdivide_left_tri_low.hpp index a172d19818c..c2c19d8ee67 100644 --- a/stan/math/fwd/fun/mdivide_left_tri_low.hpp +++ b/stan/math/fwd/fun/mdivide_left_tri_low.hpp @@ -4,9 +4,10 @@ #include #include #include -#include +#include #include #include +#include namespace stan { namespace math { diff --git a/stan/math/fwd/fun/multiply.hpp b/stan/math/fwd/fun/multiply.hpp index 71bda19187b..ebb03db7b07 100644 --- a/stan/math/fwd/fun/multiply.hpp +++ b/stan/math/fwd/fun/multiply.hpp @@ -1,11 +1,12 @@ #ifndef STAN_MATH_FWD_FUN_MULTIPLY_HPP #define STAN_MATH_FWD_FUN_MULTIPLY_HPP -#include #include #include +#include #include #include +#include namespace stan { namespace math { diff --git a/stan/math/fwd/core/read_fvar.hpp b/stan/math/fwd/fun/read_fvar.hpp similarity index 100% rename from stan/math/fwd/core/read_fvar.hpp rename to stan/math/fwd/fun/read_fvar.hpp diff --git a/stan/math/fwd/core/typedefs.hpp b/stan/math/fwd/fun/typedefs.hpp similarity index 79% rename from stan/math/fwd/core/typedefs.hpp rename to stan/math/fwd/fun/typedefs.hpp index 8e5ebd27320..bd8ab2d84e4 100644 --- a/stan/math/fwd/core/typedefs.hpp +++ b/stan/math/fwd/fun/typedefs.hpp @@ -1,9 +1,9 @@ -#ifndef STAN_MATH_FWD_CORE_TYPEDEFS_HPP -#define STAN_MATH_FWD_CORE_TYPEDEFS_HPP +#ifndef STAN_MATH_FWD_FUN_TYPEDEFS_HPP +#define STAN_MATH_FWD_FUN_TYPEDEFS_HPP #include -#include -#include +#include +#include namespace stan { namespace math { diff --git a/stan/math/mix/fun/typedefs.hpp b/stan/math/mix/fun/typedefs.hpp index 88370cfb82f..ddf7ea5d793 100644 --- a/stan/math/mix/fun/typedefs.hpp +++ b/stan/math/mix/fun/typedefs.hpp @@ -1,10 +1,10 @@ -#ifndef STAN_MATH_MIX_MAT_CORE_TYPEDEFS_HPP -#define STAN_MATH_MIX_MAT_CORE_TYPEDEFS_HPP +#ifndef STAN_MATH_MIX_MAT_FUN_TYPEDEFS_HPP +#define STAN_MATH_MIX_MAT_FUN_TYPEDEFS_HPP #include #include -#include #include +#include namespace stan { namespace math { diff --git a/stan/math/rev/core.hpp b/stan/math/rev/core.hpp index 36f0b4f2349..88ca7b2f2c5 100644 --- a/stan/math/rev/core.hpp +++ b/stan/math/rev/core.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_REV_CORE_HPP #include -#include #include #include diff --git a/test/unit/math/rev/core/read_var_test.cpp b/test/unit/math/rev/fun/read_var_test.cpp similarity index 100% rename from test/unit/math/rev/core/read_var_test.cpp rename to test/unit/math/rev/fun/read_var_test.cpp diff --git a/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp b/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp index 46fa4649963..70d8c464102 100644 --- a/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp +++ b/test/unit/math/rev/meta/VectorBuilderHelper_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/VectorBuilder_test.cpp b/test/unit/math/rev/meta/VectorBuilder_test.cpp index c7c470d815c..557c21b1de6 100644 --- a/test/unit/math/rev/meta/VectorBuilder_test.cpp +++ b/test/unit/math/rev/meta/VectorBuilder_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/ad_promotable_test.cpp b/test/unit/math/rev/meta/ad_promotable_test.cpp index 4ffcddb7222..3f21820fe1b 100644 --- a/test/unit/math/rev/meta/ad_promotable_test.cpp +++ b/test/unit/math/rev/meta/ad_promotable_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/child_type_test.cpp b/test/unit/math/rev/meta/child_type_test.cpp index afb585c4386..8e6112fc0ba 100644 --- a/test/unit/math/rev/meta/child_type_test.cpp +++ b/test/unit/math/rev/meta/child_type_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/unit/math/rev/meta/include_summand_test.cpp b/test/unit/math/rev/meta/include_summand_test.cpp index 0d9dba66322..69ba28a7081 100644 --- a/test/unit/math/rev/meta/include_summand_test.cpp +++ b/test/unit/math/rev/meta/include_summand_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/test/unit/math/rev/meta/is_constant_all_test.cpp b/test/unit/math/rev/meta/is_constant_all_test.cpp index ef703907916..771eea6d8c9 100644 --- a/test/unit/math/rev/meta/is_constant_all_test.cpp +++ b/test/unit/math/rev/meta/is_constant_all_test.cpp @@ -1,5 +1,4 @@ -#include -#include +#include #include #include diff --git a/test/unit/math/rev/meta/partials_return_type_test.cpp b/test/unit/math/rev/meta/partials_return_type_test.cpp index 43e265137fb..8eed06a2e8f 100644 --- a/test/unit/math/rev/meta/partials_return_type_test.cpp +++ b/test/unit/math/rev/meta/partials_return_type_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/test/unit/math/rev/meta/partials_type_test.cpp b/test/unit/math/rev/meta/partials_type_test.cpp index aaeb2a7e81f..d3187a0ec47 100644 --- a/test/unit/math/rev/meta/partials_type_test.cpp +++ b/test/unit/math/rev/meta/partials_type_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include TEST(MetaTraitsRevScal, partials_type) { diff --git a/test/unit/math/rev/meta/require_generics_test.cpp b/test/unit/math/rev/meta/require_generics_test.cpp index c4304af0abc..ee93fe9b5a7 100644 --- a/test/unit/math/rev/meta/require_generics_test.cpp +++ b/test/unit/math/rev/meta/require_generics_test.cpp @@ -1,6 +1,7 @@ +#include // just this is bad #include -#include +#include #include #include #include From 370c55ba179dce87a3508fe227e159e90c921ac1 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 13 Jul 2020 10:58:51 -0400 Subject: [PATCH 152/167] Revert "[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)" This reverts commit 9660a5a18f620b1249f2478f4e8a4358011b8b83. --- stan/math/rev/meta/is_var.hpp | 4 ++-- stan/math/rev/meta/is_vari.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stan/math/rev/meta/is_var.hpp b/stan/math/rev/meta/is_var.hpp index f0f3855bc7f..5bb004524bb 100644 --- a/stan/math/rev/meta/is_var.hpp +++ b/stan/math/rev/meta/is_var.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { -template -class var_value; + template + class var_value; } namespace internal { template diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index db786456202..4e7ddcc8315 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -7,8 +7,8 @@ namespace stan { namespace math { -template -class vari_value; + template + class vari_value; } namespace internal { template From 0f12bbf67d02c873932e2861b2fd9e8faac0ff61 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 13 Jul 2020 10:58:51 -0400 Subject: [PATCH 153/167] Revert "move Eigen numtraits and read_var to core to help with include order errors" This reverts commit c8fd050d022743c54739f95dbad2a254991fac76. --- stan/math/rev/core.hpp | 3 --- stan/math/rev/core/gevv_vvv_vari.hpp | 4 ++-- stan/math/rev/core/matrix_vari.hpp | 2 +- stan/math/rev/core/var.hpp | 4 +++- stan/math/rev/core/vari.hpp | 4 +++- stan/math/rev/fun.hpp | 4 +++- stan/math/rev/{core => fun}/Eigen_NumTraits.hpp | 6 +++--- stan/math/rev/fun/grad.hpp | 2 +- stan/math/rev/{core => fun}/read_var.hpp | 0 stan/math/rev/fun/tcrossprod.hpp | 2 +- stan/math/rev/meta/is_var.hpp | 5 +---- stan/math/rev/meta/is_vari.hpp | 5 +---- stan/math/rev/meta/partials_type.hpp | 2 +- 13 files changed, 20 insertions(+), 23 deletions(-) rename stan/math/rev/{core => fun}/Eigen_NumTraits.hpp (99%) rename stan/math/rev/{core => fun}/read_var.hpp (100%) diff --git a/stan/math/rev/core.hpp b/stan/math/rev/core.hpp index 88ca7b2f2c5..33eb934c08e 100644 --- a/stan/math/rev/core.hpp +++ b/stan/math/rev/core.hpp @@ -1,8 +1,6 @@ #ifndef STAN_MATH_REV_CORE_HPP #define STAN_MATH_REV_CORE_HPP -#include - #include #include #include @@ -50,7 +48,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/rev/core/gevv_vvv_vari.hpp b/stan/math/rev/core/gevv_vvv_vari.hpp index 5fad3458a01..0505788ad3d 100644 --- a/stan/math/rev/core/gevv_vvv_vari.hpp +++ b/stan/math/rev/core/gevv_vvv_vari.hpp @@ -1,9 +1,9 @@ #ifndef STAN_MATH_REV_CORE_GEVV_VVV_VARI_HPP #define STAN_MATH_REV_CORE_GEVV_VVV_VARI_HPP -#include -#include #include +#include +#include namespace stan { namespace math { diff --git a/stan/math/rev/core/matrix_vari.hpp b/stan/math/rev/core/matrix_vari.hpp index 09c2cc93128..3aca20ff174 100644 --- a/stan/math/rev/core/matrix_vari.hpp +++ b/stan/math/rev/core/matrix_vari.hpp @@ -1,7 +1,7 @@ #ifndef STAN_MATH_REV_CORE_MATRIX_VARI_HPP #define STAN_MATH_REV_CORE_MATRIX_VARI_HPP -#include +#include #include #include #include diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 98660c12519..9fb4aeee2db 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -1,9 +1,11 @@ #ifndef STAN_MATH_REV_CORE_VAR_HPP #define STAN_MATH_REV_CORE_VAR_HPP -#include #include +#include #include +#include +#include #include #include diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 0af28d897e4..fe253f8293a 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -1,9 +1,9 @@ #ifndef STAN_MATH_REV_CORE_VARI_HPP #define STAN_MATH_REV_CORE_VARI_HPP -#include #include #include +#include #include #include @@ -33,6 +33,8 @@ class vari_base { virtual ~vari_base() noexcept {} }; +template +class vari_value; /** * The variable implementation for floating point types. * diff --git a/stan/math/rev/fun.hpp b/stan/math/rev/fun.hpp index 8b5b8620b9f..7aeaa486f67 100644 --- a/stan/math/rev/fun.hpp +++ b/stan/math/rev/fun.hpp @@ -2,7 +2,8 @@ #define STAN_MATH_REV_FUN_HPP #include -#include +#include + #include #include @@ -117,6 +118,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/rev/core/Eigen_NumTraits.hpp b/stan/math/rev/fun/Eigen_NumTraits.hpp similarity index 99% rename from stan/math/rev/core/Eigen_NumTraits.hpp rename to stan/math/rev/fun/Eigen_NumTraits.hpp index 68cd6ef1442..1e6b3140427 100644 --- a/stan/math/rev/core/Eigen_NumTraits.hpp +++ b/stan/math/rev/fun/Eigen_NumTraits.hpp @@ -2,10 +2,10 @@ #define STAN_MATH_REV_FUN_EIGEN_NUMTRAITS_HPP #include +#include +#include #include -#include -#include -#include +#include #include #include diff --git a/stan/math/rev/fun/grad.hpp b/stan/math/rev/fun/grad.hpp index 31853a673f2..099e641337a 100644 --- a/stan/math/rev/fun/grad.hpp +++ b/stan/math/rev/fun/grad.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_REV_FUN_GRAD_HPP #include -#include +#include #include #include diff --git a/stan/math/rev/core/read_var.hpp b/stan/math/rev/fun/read_var.hpp similarity index 100% rename from stan/math/rev/core/read_var.hpp rename to stan/math/rev/fun/read_var.hpp diff --git a/stan/math/rev/fun/tcrossprod.hpp b/stan/math/rev/fun/tcrossprod.hpp index 08961cabe73..02b57159a9f 100644 --- a/stan/math/rev/fun/tcrossprod.hpp +++ b/stan/math/rev/fun/tcrossprod.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/stan/math/rev/meta/is_var.hpp b/stan/math/rev/meta/is_var.hpp index 5bb004524bb..e4d9fa54fed 100644 --- a/stan/math/rev/meta/is_var.hpp +++ b/stan/math/rev/meta/is_var.hpp @@ -1,15 +1,12 @@ #ifndef STAN_MATH_REV_META_IS_VAR_HPP #define STAN_MATH_REV_META_IS_VAR_HPP +#include #include #include namespace stan { -namespace math { - template - class var_value; -} namespace internal { template struct is_var_impl : std::false_type {}; diff --git a/stan/math/rev/meta/is_vari.hpp b/stan/math/rev/meta/is_vari.hpp index 4e7ddcc8315..39fb79cb767 100644 --- a/stan/math/rev/meta/is_vari.hpp +++ b/stan/math/rev/meta/is_vari.hpp @@ -1,15 +1,12 @@ #ifndef STAN_MATH_REV_META_IS_VARI_HPP #define STAN_MATH_REV_META_IS_VARI_HPP +#include #include #include namespace stan { -namespace math { - template - class vari_value; -} namespace internal { template struct is_vari_impl : std::false_type {}; diff --git a/stan/math/rev/meta/partials_type.hpp b/stan/math/rev/meta/partials_type.hpp index 1975c331ef1..823117fe062 100644 --- a/stan/math/rev/meta/partials_type.hpp +++ b/stan/math/rev/meta/partials_type.hpp @@ -1,9 +1,9 @@ #ifndef STAN_MATH_REV_META_PARTIALS_TYPE_HPP #define STAN_MATH_REV_META_PARTIALS_TYPE_HPP +#include #include #include -#include #include namespace stan { From 2a14395174551f4e8f65ad401946d71dbeb6006a Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 22 Jul 2020 13:41:29 -0400 Subject: [PATCH 154/167] adds uint16_t to var test --- test/unit/math/rev/core/var_test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 2b792ac41cd..c9bb2dfb48b 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -51,6 +51,7 @@ void ctor_overloads() { ctor_overloads_impl(); ctor_overloads_impl(); ctor_overloads_impl(); + ctor_overloads_impl(); ctor_overloads_impl(); ctor_overloads_impl(); } From 9df61cad06820380333375a05d4fdbf231d0011b Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 22 Jul 2020 18:34:36 +0000 Subject: [PATCH 155/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 3384575923f..089edc383eb 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -196,8 +196,9 @@ class vari_value> : public vari_base { * `PlainObject` represents a user constructible type such as Matrix or Array */ using PlainObject = plain_type_t; - using Scalar = typename PlainObject::Scalar; // The underlying type for this class - using value_type = PlainObject; // The underlying type for this class + using Scalar = + typename PlainObject::Scalar; // The underlying type for this class + using value_type = PlainObject; // The underlying type for this class using eigen_scalar = value_type_t; // A floating point type /** * Maps for adj_ and val_ @@ -209,11 +210,13 @@ class vari_value> : public vari_base { /** * Number of rows known at compile time */ - static constexpr Eigen::Index RowsAtCompileTime = PlainObject::RowsAtCompileTime; + static constexpr Eigen::Index RowsAtCompileTime + = PlainObject::RowsAtCompileTime; /** * Number of columns known at compile time */ - static constexpr Eigen::Index ColsAtCompileTime = PlainObject::ColsAtCompileTime; + static constexpr Eigen::Index ColsAtCompileTime + = PlainObject::ColsAtCompileTime; /** * The value of this variable. @@ -369,12 +372,13 @@ class vari_value> : public vari_base { * */ template -class vari_value> - : public vari_base, chainable_alloc { +class vari_value> : public vari_base, + chainable_alloc { public: using PlainObject = plain_type_t; // Base type of Eigen class - using Scalar = typename PlainObject::Scalar; // vari's adj_ and val_ member type - using value_type = PlainObject; // vari's adj_ and val_ member type + using Scalar = + typename PlainObject::Scalar; // vari's adj_ and val_ member type + using value_type = PlainObject; // vari's adj_ and val_ member type /** * Rows at compile time */ From 6e36185aacc4ceb0aad7a4e4ffc1e2a5c0146756 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 3 Aug 2020 13:04:37 -0400 Subject: [PATCH 156/167] remove Scalar type trait from vari_value --- stan/math/rev/core/vari.hpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 089edc383eb..032fca6d776 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -49,17 +49,16 @@ class vari_value; template class vari_value> : public vari_base { public: - using Scalar = T; - using value_type = Scalar; + using value_type = std::decay_t; /** * The value of this variable. */ - const Scalar val_; + const value_type val_; /** * The adjoint of this variable, which is the partial derivative * of this variable with respect to the root variable. */ - Scalar adj_; + value_type adj_; /** * Construct a variable implementation from a value. The @@ -196,8 +195,6 @@ class vari_value> : public vari_base { * `PlainObject` represents a user constructible type such as Matrix or Array */ using PlainObject = plain_type_t; - using Scalar = - typename PlainObject::Scalar; // The underlying type for this class using value_type = PlainObject; // The underlying type for this class using eigen_scalar = value_type_t; // A floating point type /** @@ -376,8 +373,6 @@ class vari_value> : public vari_base, chainable_alloc { public: using PlainObject = plain_type_t; // Base type of Eigen class - using Scalar = - typename PlainObject::Scalar; // vari's adj_ and val_ member type using value_type = PlainObject; // vari's adj_ and val_ member type /** * Rows at compile time From 547909347f79b30758f1d9cbe461b1f135f11cbd Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 3 Aug 2020 17:25:46 +0000 Subject: [PATCH 157/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 032fca6d776..b52b5b375e8 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -195,7 +195,7 @@ class vari_value> : public vari_base { * `PlainObject` represents a user constructible type such as Matrix or Array */ using PlainObject = plain_type_t; - using value_type = PlainObject; // The underlying type for this class + using value_type = PlainObject; // The underlying type for this class using eigen_scalar = value_type_t; // A floating point type /** * Maps for adj_ and val_ @@ -373,7 +373,7 @@ class vari_value> : public vari_base, chainable_alloc { public: using PlainObject = plain_type_t; // Base type of Eigen class - using value_type = PlainObject; // vari's adj_ and val_ member type + using value_type = PlainObject; // vari's adj_ and val_ member type /** * Rows at compile time */ From ec592d593143e5a4492ea098dfc599fb1dfec04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Tue, 4 Aug 2020 00:50:06 +0200 Subject: [PATCH 158/167] Add vari_base (#1967) Adds matrix_cl template specializations for vari_value * tmp * added vari * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) * fix doxygen * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) * fixed copying * fixed doxygen * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) * fixed cpplint * adds support for copying matrix to device * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) * bugfixes * fixed a test * bugfixed segfault in constant * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) * fixed circular include * wake up github * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) * added include * more includes * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) * added Eigen constructor and fixed docs * views are not put on stack anymore * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) * bugfix * added const * [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) Co-authored-by: Stan Jenkins Co-authored-by: Jenkins --- stan/math/opencl/copy.hpp | 18 ++ stan/math/opencl/kernel_generator.hpp | 1 + .../math/opencl/kernel_generator/constant.hpp | 132 ++++++++++++ .../kernel_generator/is_kernel_expression.hpp | 27 ++- .../opencl/kernel_generator/operation_cl.hpp | 7 + .../kernel_generator/operation_cl_lhs.hpp | 3 +- stan/math/opencl/kernel_generator/scalar.hpp | 2 +- stan/math/opencl/rev/copy.hpp | 94 ++++++-- stan/math/opencl/rev/vari.hpp | 204 ++++++++++++++++++ stan/math/opencl/value_type.hpp | 4 +- stan/math/prim/meta/ref_type.hpp | 5 +- stan/math/rev/core/var.hpp | 3 + stan/math/rev/fun/lambert_w.hpp | 1 + .../opencl/kernel_generator/constant_test.cpp | 17 ++ test/unit/math/opencl/rev/copy_test.cpp | 51 ++++- test/unit/math/opencl/rev/sub_block_test.cpp | 39 ++-- test/unit/math/opencl/rev/vari_test.cpp | 25 +++ test/unit/math/rev/meta/is_fvar_test.cpp | 1 + .../rev/meta/is_var_or_arithmetic_test.cpp | 1 + test/unit/math/rev/meta/is_var_test.cpp | 1 + test/unit/math/rev/meta/is_vari_test.cpp | 1 + 21 files changed, 596 insertions(+), 41 deletions(-) create mode 100644 stan/math/opencl/kernel_generator/constant.hpp create mode 100644 stan/math/opencl/rev/vari.hpp create mode 100644 test/unit/math/opencl/kernel_generator/constant_test.cpp create mode 100644 test/unit/math/opencl/rev/vari_test.cpp diff --git a/stan/math/opencl/copy.hpp b/stan/math/opencl/copy.hpp index 6a4b8657c77..e795f7c1b04 100644 --- a/stan/math/opencl/copy.hpp +++ b/stan/math/opencl/copy.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -101,6 +102,23 @@ inline Eigen::Matrix from_matrix_cl(const matrix_cl& src) { return dst; } +/** \ingroup opencl + * Copies result of a kernel generator expression to the + * destination Eigen matrix. + * + * @tparam R rows type of the destination + * @tparam C cols type of the destination + * @tparam T type of expression + * @param src source expression + * @return Eigen matrix with a copy of the data in the source matrix + */ +template * = nullptr, + require_not_matrix_cl_t* = nullptr> +inline Eigen::Matrix, R, C> from_matrix_cl(const T& src) { + return from_matrix_cl(src.eval()); +} + /** \ingroup opencl * Packs the flat triangular matrix on the OpenCL device and * copies it to the std::vector. diff --git a/stan/math/opencl/kernel_generator.hpp b/stan/math/opencl/kernel_generator.hpp index 2d66030ff3a..aba3e0abe43 100644 --- a/stan/math/opencl/kernel_generator.hpp +++ b/stan/math/opencl/kernel_generator.hpp @@ -114,6 +114,7 @@ #include #include +#include #include #include #include diff --git a/stan/math/opencl/kernel_generator/constant.hpp b/stan/math/opencl/kernel_generator/constant.hpp new file mode 100644 index 00000000000..ff9e48f07e7 --- /dev/null +++ b/stan/math/opencl/kernel_generator/constant.hpp @@ -0,0 +1,132 @@ +#ifndef STAN_MATH_OPENCL_KERNEL_GENERATOR_CONSTANT_HPP +#define STAN_MATH_OPENCL_KERNEL_GENERATOR_CONSTANT_HPP +#ifdef STAN_OPENCL + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace stan { +namespace math { + +/** \addtogroup opencl_kernel_generator + * @{ + */ + +/** + * Represents a matrix of single repeated value in kernel generator expressions. + * @tparam T type of the scalar + */ +template +class constant_ : public operation_cl, T> { + T a_; + int rows_; + int cols_; + + public: + static_assert(std::is_arithmetic::value, + "class scalar_: std::is_arithmetic must be true!"); + using Scalar = T; + using base = operation_cl, T>; + using base::var_name_; + + /** + * Constructor + * @param a scalar value + * @param rows number of rows of the matrix + * @param cols number of columns of the matrix + */ + explicit constant_(const T a, int rows, int cols) + : a_(a), rows_(rows), cols_(cols) {} + + /** + * Creates a deep copy of this expression. + * @return copy of \c *this + */ + inline constant_ deep_copy() const { + return constant_(a_, rows_, cols_); + } + + /** + * Generates kernel code for this expression. + * @param row_index_name row index variable name + * @param col_index_name column index variable name + * @param view_handled whether whether caller already handled matrix view + * @return part of kernel with code for this expression + */ + inline kernel_parts generate(const std::string& row_index_name, + const std::string& col_index_name, + const bool view_handled) const { + kernel_parts res{}; + res.args = type_str() + " " + var_name_ + ", "; + return res; + } + + /** + * Sets kernel arguments for this and nested expressions. + * @param[in,out] generated set of expressions that already set their kernel + * arguments + * @param kernel kernel to set arguments on + * @param[in,out] arg_num consecutive number of the first argument to set. + * This is incremented for each argument set by this function. + */ + inline void set_args(std::set& generated, + cl::Kernel& kernel, int& arg_num) const { + kernel.setArg(arg_num++, a_); + } + + /** + * Number of rows of a matrix that would be the result of evaluating this + * expression. + * @return number of rows + */ + inline int rows() const { return rows_; } + + /** + * Number of columns of a matrix that would be the result of evaluating this + * expression. + * @return number of columns + */ + inline int cols() const { return cols_; } + + /** + * Determine indices of extreme sub- and superdiagonals written. + * @return pair of indices - bottom and top diagonal + */ + inline std::pair extreme_diagonals() const { + return {std::numeric_limits::min(), std::numeric_limits::max()}; + } +}; + +/** + * Matrix of repeated values in kernel generator expressions. + * + * In most cases scalars should be directly used instead of this. This is, + * however, useful for initializing some expression to specific value if that + * expresssion could also be plain `matrix_cl`. + * + * @tparam T type of argument + * @param a input argument + * @param rows number of rows + * @param cols number of columns + * @return Block of given expression + */ +template > +inline auto constant(const T a, int rows, int cols) { + return constant_(a, rows, cols); +} + +/** @}*/ +} // namespace math +} // namespace stan + +#endif +#endif diff --git a/stan/math/opencl/kernel_generator/is_kernel_expression.hpp b/stan/math/opencl/kernel_generator/is_kernel_expression.hpp index 5bb54c818d2..186f3f84ba1 100644 --- a/stan/math/opencl/kernel_generator/is_kernel_expression.hpp +++ b/stan/math/opencl/kernel_generator/is_kernel_expression.hpp @@ -7,16 +7,21 @@ #include namespace stan { -namespace math { /** \addtogroup opencl_kernel_generator * @{ */ + /** - * Non-templated base of \c operation is needed for easy checking if something - * is a subclass of \c operation. + * Non-templated base of `operation_cl` is needed for easy checking if something + * is a subclass of `operation_cl`. */ class operation_cl_base {}; +/** + * Non-templated base of `operation_cl_lhs` is needed for easy checking if + * something is a subclass of `operation_cl_lhs`. + */ +class operation_cl_lhs_base {}; /** * Determines whether a type is non-scalar type that is a valid kernel generator @@ -26,7 +31,6 @@ template struct is_kernel_expression_and_not_scalar : bool_constant>::value> {}; - template struct is_kernel_expression_and_not_scalar> : std::true_type {}; @@ -57,8 +61,21 @@ using require_all_kernel_expressions_and_none_scalar_t template using require_all_kernel_expressions_t = require_all_t...>; + +/** + * Determines whether a type is an assignable kernel generator + * expression. + */ +template +struct is_kernel_expression_lhs + : bool_constant>::value> {}; +template +struct is_kernel_expression_lhs> : std::true_type {}; + /** @}*/ -} // namespace math +STAN_ADD_REQUIRE_UNARY(kernel_expression_lhs, is_kernel_expression_lhs, + opencl_kernel_generator); } // namespace stan #endif diff --git a/stan/math/opencl/kernel_generator/operation_cl.hpp b/stan/math/opencl/kernel_generator/operation_cl.hpp index 846e4403606..a20478a793b 100644 --- a/stan/math/opencl/kernel_generator/operation_cl.hpp +++ b/stan/math/opencl/kernel_generator/operation_cl.hpp @@ -314,6 +314,8 @@ class operation_cl : public operation_cl_base { * @return number of rows */ inline int rows() const { + static_assert( + N > 0, "default rows does not work on expressions with no arguments!"); return index_apply([&](auto... Is) { // assuming all non-dynamic sizes match return std::max({this->get_arg().rows()...}); @@ -326,6 +328,8 @@ class operation_cl : public operation_cl_base { * @return number of columns */ inline int cols() const { + static_assert( + N > 0, "default cols does not work on expressions with no arguments!"); return index_apply([&](auto... Is) { // assuming all non-dynamic sizes match return std::max({this->get_arg().cols()...}); @@ -352,6 +356,9 @@ class operation_cl : public operation_cl_base { * @return pair of indices - bottom and top diagonal */ inline std::pair extreme_diagonals() const { + static_assert(N > 0, + "default extreme_diagonals does not work on expressions with " + "no arguments!"); return index_apply([&](auto... Is) { auto arg_diags = std::make_tuple(this->get_arg().extreme_diagonals()...); diff --git a/stan/math/opencl/kernel_generator/operation_cl_lhs.hpp b/stan/math/opencl/kernel_generator/operation_cl_lhs.hpp index ef2c73b812d..c7b47d8bb98 100644 --- a/stan/math/opencl/kernel_generator/operation_cl_lhs.hpp +++ b/stan/math/opencl/kernel_generator/operation_cl_lhs.hpp @@ -24,7 +24,8 @@ namespace math { * @tparam Args types of arguments to this operation */ template -class operation_cl_lhs : public operation_cl { +class operation_cl_lhs : public operation_cl, + public operation_cl_lhs_base { protected: using base = operation_cl; static constexpr int N = sizeof...(Args); diff --git a/stan/math/opencl/kernel_generator/scalar.hpp b/stan/math/opencl/kernel_generator/scalar.hpp index 2ddd13b5d01..5282b1f3c4f 100644 --- a/stan/math/opencl/kernel_generator/scalar.hpp +++ b/stan/math/opencl/kernel_generator/scalar.hpp @@ -27,7 +27,7 @@ namespace math { */ template class scalar_ : public operation_cl, T> { - private: + protected: T a_; public: diff --git a/stan/math/opencl/rev/copy.hpp b/stan/math/opencl/rev/copy.hpp index 2dda8fe80d5..b343a4a06f9 100644 --- a/stan/math/opencl/rev/copy.hpp +++ b/stan/math/opencl/rev/copy.hpp @@ -16,6 +16,76 @@ namespace stan { namespace math { +namespace internal { +template * = nullptr> +class op_copy_to_cl_vari final + : public vari_value>> { + T_arg_adj arg_adj_; + + public: + template * = nullptr, + require_vt_same* = nullptr> + explicit op_copy_to_cl_vari(const T_arg_val& val, T_arg_adj adj) + : vari_value>>(to_matrix_cl(val)), + arg_adj_(adj) {} + + virtual void chain() { + arg_adj_ += from_matrix_cl::RowsAtCompileTime, + std::decay_t::ColsAtCompileTime>( + this->adj_); + } +}; +} // namespace internal + +/** \ingroup opencl + * Copies the source var containing Eigen matrices to destination var that has + * data stored on the OpenCL device. + * + * @tparam T type of the Eigen matrix + * @param a source Eigen matrix + * @return var with a copy of the data on the OpenCL device + */ +template +inline var_value>> to_matrix_cl( + const var_value& a) { + return new internal::op_copy_to_cl_variadj_)>(a.val(), + a.vi_->adj_); +} + +namespace internal { +template * = nullptr> +class op_copy_from_cl_vari final + : public vari_value, Rows, Cols>> { + vari_value& a_; + + public: + explicit op_copy_from_cl_vari(vari_value& a) + : vari_value, Rows, Cols>>( + from_matrix_cl(a.val_)), + a_(a) {} + + virtual void chain() { a_.adj_ = a_.adj_ + to_matrix_cl(this->adj_); } +}; +} // namespace internal + +/** \ingroup opencl + * Copies the source var that has data stored on the OpenCL device to + * destination var containing Eigen matrices. + * + * @tparam Rows number of compile time rows of the destination matrix + * @tparam Rows number of compile time columns of the destination matrix + * @tparam T type of the matrix or expression on the OpenCL device + * @param a source matrix_cl or expression + * @return var with a copy of the data on the host + */ +template * = nullptr> +inline var_value, Rows, Cols>> from_matrix_cl( + const var_value& a) { + return new internal::op_copy_from_cl_vari(*a.vi_); +} + /** \ingroup opencl * Copies the source Eigen matrix of vars to * the destination matrix that is stored @@ -26,19 +96,17 @@ namespace math { * @param src source Eigen matrix * @return matrix_cl with a copy of the data in the source matrix */ -template * = nullptr> -inline matrix_cl to_matrix_cl(const Eigen::Matrix& src) try { - matrix_cl dst(src.rows(), src.cols()); - if (src.size() == 0) { - return dst; - } - dst.val() = to_matrix_cl(src.val().eval()); - dst.adj() = to_matrix_cl(src.adj().eval()); - return dst; -} catch (const cl::Error& e) { - check_opencl_error("copy var Eigen->(OpenCL)", e); - matrix_cl dst(src.rows(), src.cols()); - return dst; +template * = nullptr> +inline var_value>>> to_matrix_cl( + const T& src) { + // the matrix can go out of scope before chain() is called. So we store a map + // to the data + var* src_array + = ChainableStack::instance_->memalloc_.alloc_array(src.size()); + Eigen::Map> src_stacked(src_array, src.rows(), src.cols()); + src_stacked = src; + return new internal::op_copy_to_cl_vari( + src_stacked.val(), src_stacked.adj()); } /** \ingroup opencl diff --git a/stan/math/opencl/rev/vari.hpp b/stan/math/opencl/rev/vari.hpp new file mode 100644 index 00000000000..0d63bbe9ac0 --- /dev/null +++ b/stan/math/opencl/rev/vari.hpp @@ -0,0 +1,204 @@ +#ifndef STAN_MATH_OPENCL_REV_VARI_HPP +#define STAN_MATH_OPENCL_REV_VARI_HPP +#ifdef STAN_OPENCL + +#include +#include +#include + +namespace stan { +namespace math { +/** + * The variable implementation for `matrix_cl`. + * + * This class is complete (not abstract) and may be used for + * constants. + * + * A variable implementation is constructed with a constant + * value. It also stores the adjoint for storing the partial + * derivative with respect to the root of the derivative tree. + * + */ +template +class vari_value> + : public vari_base, public chainable_alloc { + public: + /** + * The adjoint of this variable, which is the partial derivative + * of this variable with respect to the root variable. + */ + T adj_; + + /** + * The value of this variable. + */ + T val_; + + /** + * Construct a matrix_cl variable implementation from a value. The + * adjoint is initialized to zero. + * + * All constructed variables are added to the stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @tparam S A `matrix_cl` or kernel generator expression type that is + * convertible to `value_type` + * @param x Value of the constructed variable. + */ + template * = nullptr> + explicit vari_value(S&& x) + : chainable_alloc(), + adj_(constant(0, x.rows(), x.cols())), + val_(std::forward(x)) { + ChainableStack::instance_->var_stack_.push_back(this); + } + + /** + * Construct a matrix_cl variable implementation from an Eigen value. The + * adjoint is initialized to zero. + * + * All constructed variables are added to the stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @tparam S A dense Eigen value or expression that has same scalar type as T + * @param x Value of the constructed variable. + */ + template * = nullptr, + require_vt_same* = nullptr> + explicit vari_value(const S& x) + : chainable_alloc(), adj_(constant(0, x.rows(), x.cols())), val_(x) { + ChainableStack::instance_->var_stack_.push_back(this); + } + + /** + * Construct an matrix_cl variable implementation from a value. The + * adjoint is initialized to zero and if `stacked` is `false` this vari + * will be not be put on the var_stack. Instead it will only be put on + * a stack to keep track of whether the adjoint needs to be set to zero. + * + * All constructed variables are added to a stack. Variables + * should be constructed before variables on which they depend + * to insure proper partial derivative propagation. During + * derivative propagation, the chain() method of each variable + * will be called in the reverse order of construction. + * + * @tparam S A `matrix_cl` or kernel generator expression type that is + * convertible to `value_type` + * @param x Value of the constructed variable. + * @param stacked If false will put this this vari on the nochain stack so + * that its `chain()` method is not called. + */ + template * = nullptr> + vari_value(S&& x, bool stacked) + : chainable_alloc(), + adj_(constant(0, x.rows(), x.cols())), + val_(std::forward(x)) { + if (stacked) { + ChainableStack::instance_->var_stack_.push_back(this); + } else { + ChainableStack::instance_->var_nochain_stack_.push_back(this); + } + } + + /** + * Returns a view into a block of matrix. + * @param row starting row of the block + * @param col starting column of the block + * @param rows number of rows in the block + * @param cols number of columns in the block + * @return block + */ + auto block(int row, int col, int rows, int cols) { + auto&& val_block = stan::math::block(val_, row, col, rows, cols); + auto&& adj_block = stan::math::block(adj_, row, col, rows, cols); + return vari_value>(std::move(val_block), + std::move(adj_block)); + } + + /** + * Return the number of rows for this class's `val_` member + */ + const Eigen::Index rows() const { return val_.rows(); } + /** + * Return the number of columns for this class's `val_` member + */ + const Eigen::Index cols() const { return val_.cols(); } + /** + * Return the size of this class's `val_` member + */ + const Eigen::Index size() const { return val_.size(); } + + virtual void chain() {} + + /** + * Set the adjoint value of this variable to 0. This is used to + * reset adjoints before propagating derivatives again (for + * example in a Jacobian calculation). + */ + inline void set_zero_adjoint() final { adj_ = constant(0, rows(), cols()); } + + /** + * Allocate memory from the underlying memory pool. This memory is + * is managed as a whole externally. + * + * Warning: Classes should not be allocated with this operator + * if they have non-trivial destructors. + * + * @param nbytes Number of bytes to allocate. + * @return Pointer to allocated bytes. + */ + static inline void* operator new(size_t nbytes) { + return ChainableStack::instance_->memalloc_.alloc(nbytes); + } + + /** + * Delete a pointer from the underlying memory pool. + * + * This no-op implementation enables a subclass to throw + * exceptions in its constructor. An exception thrown in the + * constructor of a subclass will result in an error being + * raised, which is in turn caught and calls delete(). + * + * See the discussion of "plugging the memory leak" in: + * http://www.parashift.com/c++-faq/memory-pools.html + */ + static inline void operator delete(void* /* ignore arg */) { /* no op */ + } + + protected: + // to allow access to this constructor from instantinations with different + // template parameters + template + friend class vari_value; + + /** + * Construct a matrix_cl variable implementation from a value and + * adjoint. + * + * This constructor does not add the matrix to any stack! It is intended only + * for views into matrices already on stack. + * + * @param val Value of the constructed variable. + * @param adj Adjoint of the constructed variable. + */ + vari_value(T&& val, T&& adj) + : chainable_alloc(), + adj_(std::forward(adj)), + val_(std::forward(val)) {} + + private: + template + friend class var_value; +}; + +} // namespace math +} // namespace stan + +#endif +#endif diff --git a/stan/math/opencl/value_type.hpp b/stan/math/opencl/value_type.hpp index 0392008483e..2effcf27184 100644 --- a/stan/math/opencl/value_type.hpp +++ b/stan/math/opencl/value_type.hpp @@ -3,7 +3,7 @@ #ifdef STAN_OPENCL #include -#include +#include #include namespace stan { @@ -12,7 +12,7 @@ namespace stan { * Return the value type of an OpenCL matrix. */ template -struct value_type> { +struct value_type> { using type = typename std::decay_t::Scalar; }; } // namespace stan diff --git a/stan/math/prim/meta/ref_type.hpp b/stan/math/prim/meta/ref_type.hpp index 1660f9eb183..03a83a76607 100644 --- a/stan/math/prim/meta/ref_type.hpp +++ b/stan/math/prim/meta/ref_type.hpp @@ -73,12 +73,13 @@ struct ref_type_for_opencl { = std::conditional_t::value, T_val, const T&>; // Setting Outer stride of Ref to 0 (default) won't actually check that // expression has contiguous outer stride. Instead we need to check that - // evaluator flags contain LinearAccessBit. + // evaluator flags contain LinearAccessBit and PacketAccessBit. using type = std::conditional_t< Eigen::internal::traits>>:: template match::MatchAtCompileTime + && (Eigen::internal::evaluator::Flags & Eigen::LinearAccessBit) && (Eigen::internal::evaluator::Flags - & Eigen::LinearAccessBit), + & Eigen::PacketAccessBit), T_optionally_ref, T_plain_col_major>; }; diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 9fb4aeee2db..da5af408c83 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -8,6 +8,9 @@ #include #include #include +#ifdef STAN_OPENCL +#include +#endif namespace stan { namespace math { diff --git a/stan/math/rev/fun/lambert_w.hpp b/stan/math/rev/fun/lambert_w.hpp index 0f2455f3cc6..b1c845585cf 100644 --- a/stan/math/rev/fun/lambert_w.hpp +++ b/stan/math/rev/fun/lambert_w.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace stan { diff --git a/test/unit/math/opencl/kernel_generator/constant_test.cpp b/test/unit/math/opencl/kernel_generator/constant_test.cpp new file mode 100644 index 00000000000..a14dbabff5b --- /dev/null +++ b/test/unit/math/opencl/kernel_generator/constant_test.cpp @@ -0,0 +1,17 @@ +#ifdef STAN_OPENCL + +#include +#include +#include +#include +#include +#include + +TEST(KernelGenerator, constant_test) { + stan::math::matrix_cl m1_cl(stan::math::constant(1.2, 3, 4)); + + Eigen::MatrixXd res = stan::math::from_matrix_cl(m1_cl); + EXPECT_MATRIX_EQ(res, Eigen::MatrixXd::Constant(3, 4, 1.2)); +} + +#endif diff --git a/test/unit/math/opencl/rev/copy_test.cpp b/test/unit/math/opencl/rev/copy_test.cpp index c5e43d49db3..b55432ad6bc 100644 --- a/test/unit/math/opencl/rev/copy_test.cpp +++ b/test/unit/math/opencl/rev/copy_test.cpp @@ -1,5 +1,6 @@ #ifdef STAN_OPENCL #include +#include #include #include #include @@ -14,7 +15,8 @@ TEST(MathMatrixRevCL, matrix_cl_vector_copy) { // vector stan::math::matrix_cl d11_cl(3, 1); stan::math::matrix_cl d111_cl(3, 1); - EXPECT_NO_THROW(d11_cl = stan::math::to_matrix_cl(d1_cpu)); + d11_cl.val() = stan::math::to_matrix_cl(d1_cpu.val()); + d11_cl.adj() = stan::math::to_matrix_cl(d1_cpu.adj()); EXPECT_NO_THROW(d11_cl.adj() = stan::math::copy_cl(d11_cl.val())); EXPECT_NO_THROW(d111_cl = stan::math::copy_cl(d11_cl)); EXPECT_NO_THROW(d1_a_cpu = stan::math::from_matrix_cl(d11_cl)); @@ -39,7 +41,8 @@ TEST(MathMatrixRevCL, matrix_cl_matrix_copy) { stan::math::matrix_cl d000_cl; stan::math::matrix_cl d22_cl(2, 3); stan::math::matrix_cl d222_cl(2, 3); - EXPECT_NO_THROW(d22_cl = stan::math::to_matrix_cl(d2_cpu)); + d22_cl.val() = stan::math::to_matrix_cl(d2_cpu.val()); + d22_cl.adj() = stan::math::to_matrix_cl(d2_cpu.adj()); EXPECT_NO_THROW(d222_cl = stan::math::copy_cl(d22_cl)); EXPECT_NO_THROW(d2_a_cpu = stan::math::from_matrix_cl(d22_cl.val())); EXPECT_NO_THROW(d2_b_cpu = stan::math::from_matrix_cl(d222_cl.val())); @@ -55,8 +58,8 @@ TEST(MathMatrixRevCL, matrix_cl_matrix_copy) { EXPECT_EQ(4, d2_b_cpu(1, 0)); EXPECT_EQ(5, d2_b_cpu(1, 1)); EXPECT_EQ(6, d2_b_cpu(1, 2)); - d00_cl = stan::math::to_matrix_cl(d0_cpu); - EXPECT_NO_THROW(d00_cl = stan::math::to_matrix_cl(d0_cpu)); + d00_cl.val() = stan::math::to_matrix_cl(d0_cpu.val()); + d00_cl.adj() = stan::math::to_matrix_cl(d0_cpu.adj()); EXPECT_NO_THROW(d0_cpu = stan::math::from_matrix_cl(d00_cl.val())); EXPECT_NO_THROW(d000_cl = stan::math::copy_cl(d00_cl)); } @@ -150,4 +153,44 @@ TEST(MathMatrixRevCL, matrix_cl_pack_unpack_copy_exception) { std::invalid_argument); */ } + +TEST(VariCL, var_matrix_to_matrix_cl) { + using stan::math::var_value; + Eigen::MatrixXd vals(2, 3); + vals << 1, 2, 3, 4, 5, 6; + var_value a(vals); + var_value> a_cl = stan::math::to_matrix_cl(a); + EXPECT_MATRIX_EQ(from_matrix_cl(a_cl.val()), vals); + a_cl.adj() = stan::math::constant(1, 2, 3); + a_cl.vi_->chain(); + EXPECT_MATRIX_EQ(a.adj(), Eigen::MatrixXd::Constant(2, 3, 1)); +} + +TEST(VariCL, matrix_var_to_matrix_cl) { + using stan::math::var_value; + Eigen::MatrixXd vals(2, 3); + vals << 1, 2, 3, 4, 5, 6; + stan::math::matrix_v vars = vals; + var_value> a_cl + = stan::math::to_matrix_cl(vars); + EXPECT_MATRIX_EQ(from_matrix_cl(a_cl.val()), vals); + a_cl.adj() = stan::math::constant(1, 2, 3); + a_cl.vi_->chain(); + EXPECT_MATRIX_EQ(vars.adj(), Eigen::MatrixXd::Constant(2, 3, 1)); +} + +TEST(VariCL, from_matrix_cl) { + using stan::math::var_value; + Eigen::MatrixXd vals(2, 3); + vals << 1, 2, 3, 4, 5, 6; + stan::math::matrix_cl vals_cl(vals); + var_value> a_cl(vals_cl); + var_value a = stan::math::from_matrix_cl(a_cl); + EXPECT_MATRIX_EQ(a.val(), vals); + a.adj() = Eigen::MatrixXd::Constant(2, 3, 1); + a.vi_->chain(); + EXPECT_MATRIX_EQ(from_matrix_cl(a_cl.adj()), + Eigen::MatrixXd::Constant(2, 3, 1)); +} + #endif diff --git a/test/unit/math/opencl/rev/sub_block_test.cpp b/test/unit/math/opencl/rev/sub_block_test.cpp index f492b26de63..32ccc28cded 100644 --- a/test/unit/math/opencl/rev/sub_block_test.cpp +++ b/test/unit/math/opencl/rev/sub_block_test.cpp @@ -64,7 +64,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { stan::math::matrix_cl b_cl(b); Eigen::Matrix c; - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Lower); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 0, 1, 0, 1, 2, 2); @@ -80,7 +81,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Lower); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 1, 0, 1, 1, 2, 2); @@ -96,7 +98,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 1); EXPECT_EQ(c(2, 2), 1); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Lower); b_cl.view(stan::math::matrix_cl_view::Upper); a_cl.sub_block(b_cl, 0, 0, 1, 0, 2, 2); @@ -112,7 +115,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 1); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Lower); b_cl.view(stan::math::matrix_cl_view::Upper); a_cl.sub_block(b_cl, 0, 0, 0, 0, 2, 2); @@ -128,7 +132,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Lower); b_cl.view(stan::math::matrix_cl_view::Upper); a_cl.sub_block(b_cl, 1, 0, 1, 0, 2, 2); @@ -144,7 +149,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Lower); b_cl.view(stan::math::matrix_cl_view::Upper); a_cl.sub_block(b_cl, 1, 0, 1, 0, 2, 3); @@ -160,7 +166,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 1); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Upper); a_cl.sub_block(b_cl, 1, 0, 1, 0, 2, 2); @@ -176,7 +183,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Upper); a_cl.sub_block(b_cl, 0, 1, 1, 1, 2, 2); @@ -192,7 +200,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 1); EXPECT_EQ(c(2, 2), 1); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 0, 0, 0, 1, 2, 2); @@ -208,7 +217,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 0, 0, 0, 0, 2, 2); @@ -224,7 +234,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 0, 1, 0, 1, 2, 2); @@ -240,7 +251,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 0); EXPECT_EQ(c(2, 2), 0); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 0, 1, 0, 1, 3, 2); @@ -256,7 +268,8 @@ TEST(MathMatrixRevCL, sub_block_triangular) { EXPECT_EQ(c(2, 1), 1); EXPECT_EQ(c(2, 2), 1); - a_cl = stan::math::to_matrix_cl(a); + a_cl.val() = stan::math::to_matrix_cl(a.val()); + a_cl.adj() = stan::math::to_matrix_cl(a.adj()); a_cl.view(stan::math::matrix_cl_view::Upper); b_cl.view(stan::math::matrix_cl_view::Lower); a_cl.sub_block(b_cl, 0, 0, 0, 0, 3, 3); diff --git a/test/unit/math/opencl/rev/vari_test.cpp b/test/unit/math/opencl/rev/vari_test.cpp new file mode 100644 index 00000000000..4960f887324 --- /dev/null +++ b/test/unit/math/opencl/rev/vari_test.cpp @@ -0,0 +1,25 @@ +#ifdef STAN_OPENCL +#include +#include +#include +#include + +TEST(AgradRev, matrix_cl_vari_block) { + using stan::math::vari_value; + Eigen::MatrixXd a = Eigen::MatrixXd::Random(3, 3); + Eigen::MatrixXd b = Eigen::MatrixXd::Random(3, 3); + stan::math::matrix_cl a_cl(a); + stan::math::matrix_cl b_cl(b); + + vari_value> A(a); + EXPECT_MATRIX_EQ(a.block(0, 1, 2, 2), + stan::math::from_matrix_cl(A.block(0, 1, 2, 2).val_)); + vari_value> B(a_cl); + B.adj_ = b_cl; + EXPECT_MATRIX_EQ(a.block(0, 1, 2, 2), + stan::math::from_matrix_cl(B.block(0, 1, 2, 2).val_)); + EXPECT_MATRIX_EQ(b.block(0, 1, 2, 2), + stan::math::from_matrix_cl(B.block(0, 1, 2, 2).adj_)); +} + +#endif diff --git a/test/unit/math/rev/meta/is_fvar_test.cpp b/test/unit/math/rev/meta/is_fvar_test.cpp index 4a159531f99..46e38837ca7 100644 --- a/test/unit/math/rev/meta/is_fvar_test.cpp +++ b/test/unit/math/rev/meta/is_fvar_test.cpp @@ -1,4 +1,5 @@ #include +#include #include TEST(MetaTraitsRevScal, is_fvar) { diff --git a/test/unit/math/rev/meta/is_var_or_arithmetic_test.cpp b/test/unit/math/rev/meta/is_var_or_arithmetic_test.cpp index ff1188df1bc..4778049da7a 100644 --- a/test/unit/math/rev/meta/is_var_or_arithmetic_test.cpp +++ b/test/unit/math/rev/meta/is_var_or_arithmetic_test.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/test/unit/math/rev/meta/is_var_test.cpp b/test/unit/math/rev/meta/is_var_test.cpp index cb225ff7d1c..ec276e7ab7a 100644 --- a/test/unit/math/rev/meta/is_var_test.cpp +++ b/test/unit/math/rev/meta/is_var_test.cpp @@ -1,4 +1,5 @@ #include +#include #include TEST(MetaTraitsRevScal, is_var) { diff --git a/test/unit/math/rev/meta/is_vari_test.cpp b/test/unit/math/rev/meta/is_vari_test.cpp index 818d6c3b4d7..cf132d8eb15 100644 --- a/test/unit/math/rev/meta/is_vari_test.cpp +++ b/test/unit/math/rev/meta/is_vari_test.cpp @@ -1,4 +1,5 @@ #include +#include #include TEST(MetaTraitsRevScal, is_vari) { From 33e266d9d2933aab6b7c67266e9067ece651fd47 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 4 Aug 2020 12:34:17 -0400 Subject: [PATCH 159/167] remove changes to cholesky and vari_pointer type trait in var --- stan/math/prim/fun/cholesky_decompose.hpp | 8 +++++--- stan/math/rev/core/var.hpp | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/fun/cholesky_decompose.hpp b/stan/math/prim/fun/cholesky_decompose.hpp index 5b15002b77d..c382bd84d09 100644 --- a/stan/math/prim/fun/cholesky_decompose.hpp +++ b/stan/math/prim/fun/cholesky_decompose.hpp @@ -29,7 +29,9 @@ namespace math { * @throw std::domain_error if m is not a symmetric matrix or * if m is not positive definite (if m has more than 0 elements) */ -template * = nullptr> +template * = nullptr, + require_not_vt_same* = nullptr, + require_not_eigen_vt* = nullptr> inline Eigen::Matrix, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime> cholesky_decompose(const EigMat& m) { @@ -60,8 +62,8 @@ cholesky_decompose(const EigMat& m) { * if m is not positive definite (if m has more than 0 elements) */ template * = nullptr, - require_same_t>* = nullptr> -inline Eigen::Matrix, EigMat::RowsAtCompileTime, + require_vt_same* = nullptr> +inline Eigen::Matrix cholesky_decompose(const EigMat& m) { const eval_return_type_t& m_eval = m.eval(); diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 66723a584df..a7dbaf7b149 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -38,7 +38,6 @@ class var_value> { public: using value_type = std::decay_t; // Numeric type in vari_value. using vari_type = vari_value; // Type of underlying vari impl. - using vari_pointer = vari_type*; // pointer type for underlying vari. /** * Pointer to the implementation of this variable. From bf096b92d1d986265d41f3b0a78c9cc4d4559246 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 5 Aug 2020 18:34:35 -0400 Subject: [PATCH 160/167] Add grad() method without an input vari and add template paramter to var_value's grad so that it is only available when the value_type of the var_value is not a container --- stan/math/rev/core/grad.hpp | 20 ++++++++++++++++++++ stan/math/rev/core/var.hpp | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index efe8fee1e87..02b1ae824de 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -37,6 +37,26 @@ static void grad(Vari* vi) { } } +/** + * Compute the gradient for all variables starting from the end of the AD tape. + * This function does not recover memory. The chain + * rule is applied working down the stack from the last vari created on the + * AD tape and then calling each vari's `chain()` method in turn. + * + *

This function computes a nested gradient only going back as far + * as the last nesting. + * + *

This function does not recover any memory from the computation. + * + */ +static void grad() { + size_t end = ChainableStack::instance_->var_stack_.size(); + size_t beginning = empty_nested() ? 0 : end - nested_size(); + for (size_t i = end; i-- > beginning;) { + ChainableStack::instance_->var_stack_[i]->chain(); + } +} + } // namespace math } // namespace stan diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index da5af408c83..c6e77960123 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -129,10 +129,14 @@ class var_value { * The grad() function does not recover memory. In Stan * 2.4 and earlier, this function did recover memory. * + * @tparam CheckContainer Not set by user. The default value of value_type + * is used to require that grad is only available for scalar `var_value` + * types. * @param x Vector of independent variables. * @param g Gradient vector of partial derivatives of this * variable with respect to x. */ + template * = nullptr> inline void grad(std::vector>& x, std::vector& g) { stan::math::grad(vi_); g.resize(x.size()); @@ -145,8 +149,12 @@ class var_value { * Compute the gradient of this (dependent) variable with respect * to all (independent) variables. * + * @tparam CheckContainer Not set by user. The default value of value_type + * is used to require that grad is only available for scalar `var_value` + * types. * The grad() function does not recover memory. */ + template * = nullptr> void grad() { stan::math::grad(vi_); } // POINTER OVERRIDES From d2edb26b74e7c6335bf1c01178e7f07c656019f3 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 5 Aug 2020 22:35:14 +0000 Subject: [PATCH 161/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/var.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index c6e77960123..fef1d79588f 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -136,7 +136,8 @@ class var_value { * @param g Gradient vector of partial derivatives of this * variable with respect to x. */ - template * = nullptr> + template * = nullptr> inline void grad(std::vector>& x, std::vector& g) { stan::math::grad(vi_); g.resize(x.size()); @@ -154,8 +155,11 @@ class var_value { * types. * The grad() function does not recover memory. */ - template * = nullptr> - void grad() { stan::math::grad(vi_); } + template * = nullptr> + void grad() { + stan::math::grad(vi_); + } // POINTER OVERRIDES From 614ae976fb56adbb68c05bfc687b667c11cf2f1f Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 5 Aug 2020 18:37:40 -0400 Subject: [PATCH 162/167] fix docs in vari for matrix_cl --- stan/math/opencl/rev/vari.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/opencl/rev/vari.hpp b/stan/math/opencl/rev/vari.hpp index 0d63bbe9ac0..b02edc27d19 100644 --- a/stan/math/opencl/rev/vari.hpp +++ b/stan/math/opencl/rev/vari.hpp @@ -45,7 +45,7 @@ class vari_value> * will be called in the reverse order of construction. * * @tparam S A `matrix_cl` or kernel generator expression type that is - * convertible to `value_type` + * convertible to `T` * @param x Value of the constructed variable. */ template * = nullptr> @@ -89,7 +89,7 @@ class vari_value> * will be called in the reverse order of construction. * * @tparam S A `matrix_cl` or kernel generator expression type that is - * convertible to `value_type` + * convertible to `T` * @param x Value of the constructed variable. * @param stacked If false will put this this vari on the nochain stack so * that its `chain()` method is not called. From bf38a76f9b37969978ff78a4adeed35b4e053bae Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 5 Aug 2020 22:00:15 -0400 Subject: [PATCH 163/167] use grad() in grad(Vari*) --- stan/math/rev/core/grad.hpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 02b1ae824de..d6d43a18f0d 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -30,11 +30,7 @@ namespace math { template static void grad(Vari* vi) { vi->init_dependent(); - size_t end = ChainableStack::instance_->var_stack_.size(); - size_t beginning = empty_nested() ? 0 : end - nested_size(); - for (size_t i = end; i-- > beginning;) { - ChainableStack::instance_->var_stack_[i]->chain(); - } + grad(); } /** From c6af0b07130712d0b5f91dd27c4c7619a43f13be Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 5 Aug 2020 22:09:39 -0400 Subject: [PATCH 164/167] Move grad() in front of grad(Vari*) --- stan/math/rev/core/grad.hpp | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index d6d43a18f0d..570b86d18d6 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -11,6 +11,26 @@ namespace stan { namespace math { +/** + * Compute the gradient for all variables starting from the end of the AD tape. + * This function does not recover memory. The chain + * rule is applied working down the stack from the last vari created on the + * AD tape and then calling each vari's `chain()` method in turn. + * + *

This function computes a nested gradient only going back as far + * as the last nesting. + * + *

This function does not recover any memory from the computation. + * + */ +static void grad() { + size_t end = ChainableStack::instance_->var_stack_.size(); + size_t beginning = empty_nested() ? 0 : end - nested_size(); + for (size_t i = end; i-- > beginning;) { + ChainableStack::instance_->var_stack_[i]->chain(); + } +} + /** * Compute the gradient for all variables starting from the * specified root variable implementation. Does not recover @@ -33,26 +53,6 @@ static void grad(Vari* vi) { grad(); } -/** - * Compute the gradient for all variables starting from the end of the AD tape. - * This function does not recover memory. The chain - * rule is applied working down the stack from the last vari created on the - * AD tape and then calling each vari's `chain()` method in turn. - * - *

This function computes a nested gradient only going back as far - * as the last nesting. - * - *

This function does not recover any memory from the computation. - * - */ -static void grad() { - size_t end = ChainableStack::instance_->var_stack_.size(); - size_t beginning = empty_nested() ? 0 : end - nested_size(); - for (size_t i = end; i-- > beginning;) { - ChainableStack::instance_->var_stack_[i]->chain(); - } -} - } // namespace math } // namespace stan From 4e70ba6b3165a0b68885861eab8d705a939929e9 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 6 Aug 2020 17:55:21 +0000 Subject: [PATCH 165/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index b0ae98487c3..51925ac349d 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -207,13 +207,11 @@ class vari_value> : public vari_base { /** * Number of rows known at compile time */ - static constexpr int RowsAtCompileTime - = PlainObject::RowsAtCompileTime; + static constexpr int RowsAtCompileTime = PlainObject::RowsAtCompileTime; /** * Number of columns known at compile time */ - static constexpr int ColsAtCompileTime - = PlainObject::ColsAtCompileTime; + static constexpr int ColsAtCompileTime = PlainObject::ColsAtCompileTime; /** * The value of this variable. From 8cec8bb9b99614237f7fc7acfbda91083b6610cd Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 6 Aug 2020 16:38:17 -0400 Subject: [PATCH 166/167] Use pf in var_value for the sparse matrix constructor --- stan/math/rev/core/var.hpp | 2 +- stan/math/rev/core/vari.hpp | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index be9c43cda5e..b7b453d7a28 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -86,7 +86,7 @@ class var_value { * @param x Value of the variable. */ template * = nullptr> - var_value(const S& x) : vi_(new vari_type(x, false)) {} // NOLINT + var_value(S&& x) : vi_(new vari_type(std::forward(x), false)) {} // NOLINT /** * Construct a variable from a pointer to a variable implementation. diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 51925ac349d..4aec90125b5 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -380,10 +380,6 @@ class vari_value> : public vari_base, * Columns at compile time */ static constexpr int ColsAtCompileTime = T::ColsAtCompileTime; - /** - * The value of this variable. - */ - const PlainObject val_; /** * The adjoint of this variable, which is the partial derivative @@ -391,6 +387,11 @@ class vari_value> : public vari_base, */ PlainObject adj_; + /** + * The value of this variable. + */ + const PlainObject val_; + /** * Construct a variable implementation from a value. The * adjoint is initialized to zero. @@ -406,7 +407,7 @@ class vari_value> : public vari_base, * @param x Value of the constructed variable. */ template * = nullptr> - explicit vari_value(const S& x) : val_(x), adj_(x), chainable_alloc() { + explicit vari_value(S&& x) : adj_(x), val_(std::forward(x)), chainable_alloc() { this->set_zero_adjoint(); ChainableStack::instance_->var_stack_.push_back(this); } @@ -428,7 +429,7 @@ class vari_value> : public vari_base, * that its `chain()` method is not called. */ template * = nullptr> - vari_value(const S& x, bool stacked) : val_(x), adj_(x), chainable_alloc() { + vari_value(S&& x, bool stacked) : adj_(x), val_(std::forward(x)), chainable_alloc() { this->set_zero_adjoint(); if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); From 32045cca0cd25023b9606c66a8f5f0a7976281f0 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 6 Aug 2020 20:38:56 +0000 Subject: [PATCH 167/167] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/rev/core/vari.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index 4aec90125b5..b17e532870c 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -407,7 +407,8 @@ class vari_value> : public vari_base, * @param x Value of the constructed variable. */ template * = nullptr> - explicit vari_value(S&& x) : adj_(x), val_(std::forward(x)), chainable_alloc() { + explicit vari_value(S&& x) + : adj_(x), val_(std::forward(x)), chainable_alloc() { this->set_zero_adjoint(); ChainableStack::instance_->var_stack_.push_back(this); } @@ -429,7 +430,8 @@ class vari_value> : public vari_base, * that its `chain()` method is not called. */ template * = nullptr> - vari_value(S&& x, bool stacked) : adj_(x), val_(std::forward(x)), chainable_alloc() { + vari_value(S&& x, bool stacked) + : adj_(x), val_(std::forward(x)), chainable_alloc() { this->set_zero_adjoint(); if (stacked) { ChainableStack::instance_->var_stack_.push_back(this);