diff --git a/.gitignore b/.gitignore index 37f8b79b3..c5cc3d310 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,4 @@ cmake-build-debug/* build.ninja .ninja* a.out -include/boost/math/differentiation/build/ -include/boost/math/differentiation/build/* diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index a1f6011d9..000000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright 2020 Peter Dimov -# Copyright 2021 Matt Borland -# Distributed under the Boost Software License, Version 1.0. -# https://www.boost.org/LICENSE_1_0.txt - -cmake_minimum_required(VERSION 3.5...3.16) - -project(boost_math VERSION 1.89.0 LANGUAGES CXX) - -add_library(boost_math INTERFACE) - -add_library(Boost::math ALIAS boost_math) - -target_include_directories(boost_math INTERFACE include) -if(NOT CMAKE_VERSION VERSION_LESS "3.19") - file(GLOB_RECURSE headers include/*.hpp) - target_sources(boost_math PRIVATE ${headers}) -endif() - -include(CMakeDependentOption) - -cmake_dependent_option(BOOST_MATH_STANDALONE "Use Boost.Math in standalone mode" ON "NOT BOOST_SUPERPROJECT_VERSION" OFF) - -message(STATUS "Boost.Math: standalone mode ${BOOST_MATH_STANDALONE}") - -if(BOOST_MATH_STANDALONE) - - target_compile_definitions(boost_math INTERFACE BOOST_MATH_STANDALONE=1) - -else() - - target_link_libraries(boost_math - INTERFACE - Boost::assert - Boost::concept_check - Boost::config - Boost::core - Boost::integer - Boost::lexical_cast - Boost::predef - Boost::random - Boost::static_assert - Boost::throw_exception - ) - -endif() - -if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") - - add_subdirectory(test) - -# Only enable tests when we're the root project -elseif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - - include(CTest) - add_subdirectory(test) - - include(GNUInstallDirs) - install(DIRECTORY "include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - -endif() diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user deleted file mode 100644 index d7a3547ae..000000000 --- a/CMakeLists.txt.user +++ /dev/null @@ -1,455 +0,0 @@ - - - - - - EnvironmentId - {ffc14226-ab71-4806-b0f4-12ccb1245f00} - - - ProjectExplorer.Project.ActiveTarget - 0 - - - ProjectExplorer.Project.EditorSettings - - true - false - true - - Cpp - - CppGlobal - - - - QmlJS - - QmlJSGlobal - - - 2 - UTF-8 - false - 4 - false - 0 - 80 - true - true - 1 - 0 - false - true - false - 2 - true - true - 0 - 8 - true - false - 1 - true - true - true - *.md, *.MD, Makefile - false - true - true - - - - ProjectExplorer.Project.PluginSettings - - - true - false - true - true - true - true - - false - - - 0 - true - - true - true - Builtin.DefaultTidyAndClazy - 4 - true - - - - true - - - - - ProjectExplorer.Project.Target.0 - - Desktop - Qt 6.8.1 for macOS - Qt 6.8.1 for macOS - qt.qt6.681.clang_64_kit - 0 - 0 - 0 - - Debug - 2 - false - - -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake --DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} --DCMAKE_GENERATOR:STRING=Ninja --DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} --DCMAKE_BUILD_TYPE:STRING=Debug --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} - 0 - /Users/mzhelyez/Documents/code/boost/libs/math/build/Qt_6_8_1_for_macOS-Debug - - - - - all - - false - - true - Build - CMakeProjectManager.MakeStep - - 1 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - - - clean - - false - - true - Build - CMakeProjectManager.MakeStep - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Debug - CMakeProjectManager.CMakeBuildConfiguration - - - Release - 2 - false - - -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake --DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} --DCMAKE_GENERATOR:STRING=Ninja --DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} --DCMAKE_BUILD_TYPE:STRING=Release --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} - /Users/mzhelyez/Documents/code/boost/libs/math/build/Qt_6_8_1_for_macOS-Release - - - - - all - - false - - true - CMakeProjectManager.MakeStep - - 1 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - - - clean - - false - - true - CMakeProjectManager.MakeStep - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Release - CMakeProjectManager.CMakeBuildConfiguration - - - RelWithDebInfo - 2 - false - - -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake --DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} --DCMAKE_GENERATOR:STRING=Ninja --DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} --DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} - /Users/mzhelyez/Documents/code/boost/libs/math/build/Qt_6_8_1_for_macOS-RelWithDebInfo - - - - - all - - false - - true - CMakeProjectManager.MakeStep - - 1 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - - - clean - - false - - true - CMakeProjectManager.MakeStep - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Release with Debug Information - CMakeProjectManager.CMakeBuildConfiguration - - - RelWithDebInfo - 2 - false - - -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake --DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} --DCMAKE_GENERATOR:STRING=Ninja --DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} --DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} - 0 - /Users/mzhelyez/Documents/code/boost/libs/math/build/Qt_6_8_1_for_macOS-Profile - - - - - all - - false - - true - CMakeProjectManager.MakeStep - - 1 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - - - clean - - false - - true - CMakeProjectManager.MakeStep - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Profile - CMakeProjectManager.CMakeBuildConfiguration - - - MinSizeRel - 2 - false - - -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake --DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} --DCMAKE_GENERATOR:STRING=Ninja --DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} --DCMAKE_BUILD_TYPE:STRING=MinSizeRel --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} - /Users/mzhelyez/Documents/code/boost/libs/math/build/Qt_6_8_1_for_macOS-MinSizeRel - - - - - all - - false - - true - CMakeProjectManager.MakeStep - - 1 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - - - clean - - false - - true - CMakeProjectManager.MakeStep - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Minimum Size Release - CMakeProjectManager.CMakeBuildConfiguration - - 5 - - - 0 - Deploy - Deploy - ProjectExplorer.BuildSteps.Deploy - - 1 - - false - ProjectExplorer.DefaultDeployConfiguration - - - - - - - - - false - - true - ApplicationManagerPlugin.Deploy.CMakePackageStep - - - install-package --acknowledge - true - Install Application Manager package - ApplicationManagerPlugin.Deploy.InstallPackageStep - - - - - - - - 2 - Deploy - Deploy - ProjectExplorer.BuildSteps.Deploy - - 1 - - false - ApplicationManagerPlugin.Deploy.Configuration - - 2 - - true - true - 0 - true - - 2 - - false - -e cpu-cycles --call-graph dwarf,4096 -F 250 - - ProjectExplorer.CustomExecutableRunConfiguration - - false - true - true - - 1 - - - - ProjectExplorer.Project.TargetCount - 1 - - - ProjectExplorer.Project.Updater.FileVersion - 22 - - - Version - 22 - - diff --git a/example/autodiff_reverse_black_scholes.cpp b/example/autodiff_reverse_black_scholes.cpp index c52fec317..c808bc54d 100644 --- a/example/autodiff_reverse_black_scholes.cpp +++ b/example/autodiff_reverse_black_scholes.cpp @@ -7,16 +7,16 @@ using namespace boost::math::differentiation::reverse_mode; using namespace boost::math::constants; -template -X phi(X const& x) +template +Real phi(Real const& x) { - return one_div_root_two_pi() * exp(-0.5 * x * x); + return one_div_root_two_pi() * exp(-0.5 * x * x); } -template -X Phi(X const& x) +template +Real Phi(Real const& x) { - return 0.5 * erfc(-one_div_root_two() * x); + return 0.5 * erfc(-one_div_root_two() * x); } enum class CP { call, put }; @@ -33,7 +33,7 @@ T black_scholes_option_price(CP cp, double K, T const& S, T const& sigma, T cons case CP::put: return exp(-r * tau) * K * Phi(-d2) - S * Phi(-d1); default: - throw std::runtime_error("Invalid CP value."); + throw runtime_error("Invalid CP value."); } } @@ -105,7 +105,7 @@ int main() << "formula call delta = " << formula_call_delta << "\n" << "autodiff call vega = " << call_greeks[1].item() << '\n' << " formula call vega = " << formula_vega << '\n' - << "autodiff call theta = " << call_greeks[2].item() << '\n' + << "autodiff call theta = " << -call_greeks[2].item() << '\n' << " formula call theta = " << formula_call_theta << '\n' << "autodiff call rho = " << call_greeks[3].item() << 'n' << " formula call rho = " << formula_call_rho << '\n' diff --git a/include/boost/math/differentiation/autodiff_reverse.hpp b/include/boost/math/differentiation/autodiff_reverse.hpp index 920b153a7..6e37532d4 100644 --- a/include/boost/math/differentiation/autodiff_reverse.hpp +++ b/include/boost/math/differentiation/autodiff_reverse.hpp @@ -5,7 +5,6 @@ #ifndef BOOST_MATH_DIFFERENTIATION_AUTODIFF_HPP #define BOOST_MATH_DIFFERENTIATION_AUTODIFF_HPP -#include #include #include #include @@ -24,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -689,7 +687,7 @@ auto grad(rvar &f, std::vector *> &x) static_assert(order_1 <= order_2, "variable differentiating w.r.t. must have order >= function order"); std::vector *> xx; - for (auto xi : x) + for (auto &xi : x) xx.push_back(&(xi->template get_value_at())); return detail::grad_op_impl{}(f, xx); } diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_overloads.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_overloads.hpp index 8b551d7d1..07b5482cf 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_overloads.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_overloads.hpp @@ -19,9 +19,6 @@ struct add_expr /* @brief addition * rvar+rvar * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; // Explicitly define constructor to forward to base class explicit add_expr(const expression &left_hand_expr, @@ -39,7 +36,7 @@ struct add_expr { return inner_t(1.0); } -}; // namespace reverse_modetemplatestructadd_expr:publicabstract_binary_expression > +}; template struct add_const_expr : public abstract_unary_expression> @@ -47,8 +44,6 @@ struct add_const_expr /* @brief * rvar+float or float+rvar * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit add_const_expr(const expression &arg_expr, const T v) : abstract_unary_expression>(arg_expr, v){}; @@ -66,9 +61,6 @@ struct mult_expr /* @brief multiplication * rvar * rvar * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; explicit mult_expr(const expression &left_hand_expr, const expression &right_hand_expr) @@ -93,8 +85,6 @@ struct mult_const_expr /* @brief * rvar+float or float+rvar * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit mult_const_expr(const expression &arg_expr, const T v) @@ -114,9 +104,6 @@ struct sub_expr /* @brief addition * rvar-rvar * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; // Explicitly define constructor to forward to base class explicit sub_expr(const expression &left_hand_expr, @@ -144,9 +131,6 @@ struct div_expr /* @brief multiplication * rvar / rvar * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; // Explicitly define constructor to forward to base class explicit div_expr(const expression &left_hand_expr, @@ -158,7 +142,7 @@ struct div_expr inner_t evaluate() const { return this->lhs.evaluate() / this->rhs.evaluate(); }; static const inner_t left_derivative(const inner_t &l, const inner_t &r, const inner_t &v) { - return 1.0 / r; + return static_cast(1.0) / r; }; static const inner_t right_derivative(const inner_t &l, const inner_t &r, const inner_t &v) { @@ -172,8 +156,6 @@ struct div_by_const_expr /* @brief * rvar/float * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit div_by_const_expr(const expression &arg_expr, const T v) @@ -193,8 +175,6 @@ struct const_div_by_expr /** @brief * float/rvar * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit const_div_by_expr(const expression &arg_expr, const T v) @@ -270,7 +250,7 @@ add_const_expr operator+(const U &v, const expression mult_const_expr operator-(const expression &arg) { - return mult_const_expr(arg, -1.0); + return mult_const_expr(arg, static_cast(-1.0)); } /** @brief @@ -292,7 +272,7 @@ template operator-(const expression &arg, const U &v) { /* rvar - float = rvar + (-float) */ - return add_const_expr(arg, static_cast(-1.0 * v)); + return add_const_expr(arg, static_cast(-v)); } /** @brief diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_erf_overloads.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_erf_overloads.hpp index b2404e92c..c8c36c758 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_erf_overloads.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_erf_overloads.hpp @@ -60,9 +60,7 @@ struct erf_expr : public abstract_unary_expression; + using inner_t = rvar_t; explicit erf_expr(const expression &arg_expr, const T v) : abstract_unary_expression>(arg_expr, v){}; @@ -76,10 +74,8 @@ struct erf_expr : public abstract_unary_expression()); + BOOST_MATH_STD_USING + return static_cast(2.0) * exp(-argv * argv) / sqrt(constants::pi()); } }; @@ -91,9 +87,8 @@ struct erfc_expr : public abstract_unary_expression; + + using inner_t = rvar_t; explicit erfc_expr(const expression &arg_expr, const T v) : abstract_unary_expression>(arg_expr, v){}; @@ -107,10 +102,8 @@ struct erfc_expr : public abstract_unary_expression()); + BOOST_MATH_STD_USING + return static_cast(-2.0) * exp(-argv * argv) / sqrt(constants::pi()); } }; @@ -122,9 +115,8 @@ struct erf_inv_expr : public abstract_unary_expression; + + using inner_t = rvar_t; explicit erf_inv_expr(const expression &arg_expr, const T v) : abstract_unary_expression>(arg_expr, v){}; @@ -138,19 +130,16 @@ struct erf_inv_expr : public abstract_unary_expression 1)>( [](auto &&x) { - return 0.5 * sqrt(constants::pi()) - * reverse_mode::exp(reverse_mode::pow(reverse_mode::erf_inv(x), 2.0)); + return static_cast(0.5) * sqrt(constants::pi()) + * reverse_mode::exp( + reverse_mode::pow(reverse_mode::erf_inv(x), static_cast(2.0))); }, [](auto &&x) { - return 0.5 * sqrt(constants::pi()) * exp(pow(boost::math::erf_inv(x), 2)); + return static_cast(0.5) * sqrt(constants::pi()) + * exp(pow(boost::math::erf_inv(x), static_cast(2.0))); }, argv); } @@ -164,9 +153,8 @@ struct erfc_inv_expr : public abstract_unary_expression; + + using inner_t = rvar_t; explicit erfc_inv_expr(const expression &arg_expr, const T v) : abstract_unary_expression>(arg_expr, v){}; @@ -180,19 +168,16 @@ struct erfc_inv_expr : public abstract_unary_expression 1)>( [](auto &&x) { - return -0.5 * sqrt(constants::pi()) - * reverse_mode::exp(reverse_mode::pow(reverse_mode::erfc_inv(x), 2.0)); + return static_cast(-0.5) * sqrt(constants::pi()) + * reverse_mode::exp( + reverse_mode::pow(reverse_mode::erfc_inv(x), static_cast(2.0))); }, [](auto &&x) { - return -0.5 * sqrt(constants::pi()) * exp(pow(boost::math::erfc_inv(x), 2)); + return static_cast(-0.5) * sqrt(constants::pi()) + * exp(pow(boost::math::erfc_inv(x), static_cast(2.0))); }, argv); } diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_expression_template_base.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_expression_template_base.hpp index f24806d0e..9f1c020da 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_expression_template_base.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_expression_template_base.hpp @@ -5,7 +5,7 @@ #ifndef REVERSE_MODE_AUTODIFF_EXPRESSION_TEMPLATE_BASE_HPP #define REVERSE_MODE_AUTODIFF_EXPRESSION_TEMPLATE_BASE_HPP -#include +#include #include namespace boost { namespace math { @@ -215,10 +215,6 @@ struct abstract_unary_expression explicit abstract_unary_expression(const expression &arg_expr, const T &constant) : arg(static_cast(arg_expr)) , constant(constant){}; - // explicit abstract_unary_expression(const ARG& arg_expr, const T& constant) - // : arg(arg_expr) - // , constant(constant) - //{} inner_t evaluate() const { return static_cast(this)->evaluate(); diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_helper_functions.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_helper_functions.hpp index 04ebd3740..fd08e8e88 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_helper_functions.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_helper_functions.hpp @@ -5,7 +5,7 @@ #ifndef BOOST_REVERSE_MODE_AUTODIFF_HELPER_FUNCTIONS_HPP #define BOOST_REVERSE_MODE_AUTODIFF_HELPER_FUNCTIONS_HPP -#include +#include #include namespace boost { namespace math { diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_memory_management.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_memory_management.hpp index 7db6a39d9..ea62bbffb 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_memory_management.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_memory_management.hpp @@ -8,14 +8,13 @@ #include #include +#include #include +#include #include #include -#include #include #include -#include - namespace boost { namespace math { namespace differentiation { @@ -82,25 +81,25 @@ public: {} reference operator*() { - BOOST_ASSERT(index_ >= begin_ && index_ < end_); + BOOST_MATH_ASSERT(index_ >= begin_ && index_ < end_); return (*storage_->data_[index_ / buffer_size])[index_ % buffer_size]; } const_reference_type operator*() const { - BOOST_ASSERT(index_ >= begin_ && index_ < end_); + BOOST_MATH_ASSERT(index_ >= begin_ && index_ < end_); return (*storage_->data_[index_ / buffer_size])[index_ % buffer_size]; } pointer operator->() { - BOOST_ASSERT(index_ >= begin_ && index_ < end_); + BOOST_MATH_ASSERT(index_ >= begin_ && index_ < end_); return &operator*(); } const_ptr_type operator->() const { - BOOST_ASSERT(index_ >= begin_ && index_ < end_); + BOOST_MATH_ASSERT(index_ >= begin_ && index_ < end_); return &operator*(); } flat_linear_allocator_iterator &operator++() @@ -311,8 +310,8 @@ public: if (item_id() == 0 && total_size_ != 0) { allocate_buffer(); } - BOOST_ASSERT(buffer_id() < data_.size()); - BOOST_ASSERT(item_id() < buffer_size); + BOOST_MATH_ASSERT(buffer_id() < data_.size()); + BOOST_MATH_ASSERT(item_id() < buffer_size); T *ptr = &(*data_[buffer_id()])[item_id()]; new (ptr) T(std::forward(args)...); ++total_size_; @@ -415,12 +414,12 @@ public: */ T &operator[](std::size_t i) { - BOOST_ASSERT(i < total_size_); + BOOST_MATH_ASSERT(i < total_size_); return (*data_[i / buffer_size])[i % buffer_size]; } const T &operator[](std::size_t i) const { - BOOST_ASSERT(i < total_size_); + BOOST_MATH_ASSERT(i < total_size_); return (*data_[i / buffer_size])[i % buffer_size]; } }; diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_overloads.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_overloads.hpp index bb434d124..bbea936e0 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_overloads.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_overloads.hpp @@ -27,8 +27,6 @@ struct fabs_expr : public abstract_unary_expression; explicit fabs_expr(const expression &arg_expr, const T v) @@ -36,7 +34,7 @@ struct fabs_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -56,8 +54,6 @@ struct ceil_expr : public abstract_unary_expression; explicit ceil_expr(const expression &arg_expr, const T v) @@ -65,7 +61,7 @@ struct ceil_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -85,8 +81,6 @@ struct floor_expr : public abstract_unary_expression; explicit floor_expr(const expression &arg_expr, const T v) @@ -94,7 +88,7 @@ struct floor_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -114,8 +108,6 @@ struct trunc_expr : public abstract_unary_expression; explicit trunc_expr(const expression &arg_expr, const T v) @@ -123,8 +115,7 @@ struct trunc_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -141,8 +132,6 @@ struct exp_expr : public abstract_unary_expression; explicit exp_expr(const expression &arg_expr, const T v) @@ -150,12 +139,12 @@ struct exp_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::exp; + BOOST_MATH_STD_USING return exp(argv); } }; @@ -168,9 +157,6 @@ struct pow_expr * d/dx pow(x,y) = y pow (x, y-1) * d/dy pow(x,y) = pow(x,y) log(x) * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; // Explicitly define constructor to forward to base class explicit pow_expr(const expression &left_hand_expr, @@ -181,18 +167,17 @@ struct pow_expr inner_t evaluate() const { - using std::pow; + BOOST_MATH_STD_USING return pow(this->lhs.evaluate(), this->rhs.evaluate()); }; static const inner_t left_derivative(const inner_t &l, const inner_t &r, const inner_t &v) { - using std::pow; - return r * pow(l, r - 1); + BOOST_MATH_STD_USING + return r * pow(l, r - static_cast(1.0)); }; static const inner_t right_derivative(const inner_t &l, const inner_t &r, const inner_t &v) { - using std::log; - using std::pow; + BOOST_MATH_STD_USING return pow(l, r) * log(l); }; }; @@ -203,8 +188,6 @@ struct expr_pow_float_expr { /** @brief pow(rvar,float) */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit expr_pow_float_expr(const expression &arg_expr, const T v) @@ -213,12 +196,12 @@ struct expr_pow_float_expr inner_t evaluate() const { - using std::pow; + BOOST_MATH_STD_USING return pow(this->arg.evaluate(), this->constant); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::pow; + BOOST_MATH_STD_USING return inner_t{constant} * pow(argv, inner_t{constant - 1}); } }; @@ -229,8 +212,6 @@ struct float_pow_expr_expr { /** @brief pow(float, rvar) * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit float_pow_expr_expr(const expression &arg_expr, const T v) @@ -239,12 +220,12 @@ struct float_pow_expr_expr inner_t evaluate() const { - using std::pow; + BOOST_MATH_STD_USING return pow(this->constant, this->arg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::pow; + BOOST_MATH_STD_USING return pow(constant, argv) * log(constant); } }; @@ -255,8 +236,6 @@ struct sqrt_expr : public abstract_unary_expression; explicit sqrt_expr(const expression &arg_expr, const T v) @@ -264,13 +243,13 @@ struct sqrt_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sqrt; - return 1.0 / (2 * sqrt(argv)); + BOOST_MATH_STD_USING + return static_cast(1.0) / (static_cast(2.0) * sqrt(argv)); } }; @@ -280,8 +259,6 @@ struct log_expr : public abstract_unary_expression; explicit log_expr(const expression &arg_expr, const T v) @@ -289,12 +266,12 @@ struct log_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - return 1.0 / argv; + return static_cast(1.0) / argv; } }; @@ -304,8 +281,6 @@ struct cos_expr : public abstract_unary_expression; explicit cos_expr(const expression &arg_expr, const T v) @@ -313,12 +288,12 @@ struct cos_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sin; + BOOST_MATH_STD_USING return -sin(argv); } }; @@ -338,12 +313,12 @@ struct sin_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::cos; + BOOST_MATH_STD_USING return cos(argv); } }; @@ -354,8 +329,6 @@ struct tan_expr : public abstract_unary_expression; explicit tan_expr(const expression &arg_expr, const T v) @@ -363,13 +336,13 @@ struct tan_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::cos; - return 1.0 / (cos(argv) * cos(argv)); + BOOST_MATH_STD_USING + return static_cast(1.0) / (cos(argv) * cos(argv)); } }; @@ -379,8 +352,6 @@ struct acos_expr : public abstract_unary_expression; explicit acos_expr(const expression &arg_expr, const T v) @@ -388,13 +359,13 @@ struct acos_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sqrt; - return -1.0 / sqrt(1 - argv * argv); + BOOST_MATH_STD_USING + return static_cast(-1.0) / sqrt(static_cast(1.0) - argv * argv); } }; @@ -413,13 +384,13 @@ struct asin_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sqrt; - return 1.0 / sqrt(1 - argv * argv); + BOOST_MATH_STD_USING + return static_cast(1.0) / sqrt(static_cast(1.0) - argv * argv); } }; @@ -429,8 +400,6 @@ struct atan_expr : public abstract_unary_expression; explicit atan_expr(const expression &arg_expr, const T v) @@ -438,12 +407,13 @@ struct atan_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - return 1.0 / (1 + argv * argv); + BOOST_MATH_STD_USING + return static_cast(1.0) / (static_cast(1.0) + argv * argv); } }; template @@ -452,9 +422,6 @@ struct atan2_expr { /** @brief atan2(x,y) * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; // Explicitly define constructor to forward to base class explicit atan2_expr(const expression &left_hand_expr, @@ -465,7 +432,7 @@ struct atan2_expr inner_t evaluate() const { - using std::atan2; + BOOST_MATH_STD_USING return atan2(this->lhs.evaluate(), this->rhs.evaluate()); }; static const inner_t left_derivative(const inner_t &l, const inner_t &r, const inner_t &v) @@ -484,8 +451,6 @@ struct atan2_left_float_expr { /** @brief atan2(float,rvar) * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit atan2_left_float_expr(const expression &arg_expr, const T v) @@ -494,7 +459,7 @@ struct atan2_left_float_expr inner_t evaluate() const { - using std::atan2; + BOOST_MATH_STD_USING return atan2(this->constant, this->arg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -509,8 +474,6 @@ struct atan2_right_float_expr { /** @brief atan2(rvar,float) * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit atan2_right_float_expr(const expression &arg_expr, const T v) @@ -519,7 +482,7 @@ struct atan2_right_float_expr inner_t evaluate() const { - using std::atan2; + BOOST_MATH_STD_USING return atan2(this->arg.evaluate(), this->constant); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -534,8 +497,6 @@ struct round_expr : public abstract_unary_expression; explicit round_expr(const expression &arg_expr, const T v) @@ -543,7 +504,7 @@ struct round_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -558,8 +519,6 @@ struct sinh_expr : public abstract_unary_expression; explicit sinh_expr(const expression &arg_expr, const T v) @@ -567,12 +526,12 @@ struct sinh_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::cosh; + BOOST_MATH_STD_USING return cosh(argv); } }; @@ -583,8 +542,6 @@ struct cosh_expr : public abstract_unary_expression; explicit cosh_expr(const expression &arg_expr, const T v) @@ -592,12 +549,12 @@ struct cosh_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sinh; + BOOST_MATH_STD_USING return sinh(argv); } }; @@ -607,8 +564,6 @@ struct tanh_expr : public abstract_unary_expression; explicit tanh_expr(const expression &arg_expr, const T v) @@ -616,13 +571,13 @@ struct tanh_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::cosh; - return 1.0 / (cosh(argv) * cosh(argv)); + BOOST_MATH_STD_USING + return static_cast(1.0) / (cosh(argv) * cosh(argv)); } }; @@ -632,8 +587,6 @@ struct log10_expr : public abstract_unary_expression; explicit log10_expr(const expression &arg_expr, const T v) @@ -641,13 +594,13 @@ struct log10_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::log; - return 1.0 / (argv * log(10.0)); + BOOST_MATH_STD_USING + return static_cast(1.0) / (argv * log(static_cast(10.0))); } }; @@ -657,8 +610,6 @@ struct acosh_expr : public abstract_unary_expression; explicit acosh_expr(const expression &arg_expr, const T v) @@ -666,13 +617,14 @@ struct acosh_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sqrt; - return 1.0 / (sqrt(argv - 1) * sqrt(argv + 1)); + BOOST_MATH_STD_USING + return static_cast(1.0) + / (sqrt(argv - static_cast(1.0)) * sqrt(argv + static_cast(1.0))); } }; @@ -682,8 +634,6 @@ struct asinh_expr : public abstract_unary_expression; explicit asinh_expr(const expression &arg_expr, const T v) @@ -691,13 +641,13 @@ struct asinh_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sqrt; - return 1.0 / (sqrt(1 + argv * argv)); + BOOST_MATH_STD_USING + return static_cast(1.0) / (sqrt(static_cast(1.0) + argv * argv)); } }; @@ -707,8 +657,6 @@ struct atanh_expr : public abstract_unary_expression; explicit atanh_expr(const expression &arg_expr, const T v) @@ -716,13 +664,13 @@ struct atanh_expr : public abstract_unary_expressionarg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - using std::sqrt; - return 1.0 / (1 - argv * argv); + BOOST_MATH_STD_USING + return static_cast(1.0) / (static_cast(1.0) - argv * argv); } }; template @@ -731,9 +679,6 @@ struct fmod_expr { /** @brief * */ - using lhs_type = LHS; - using rhs_type = RHS; - using value_type = T; using inner_t = rvar_t; // Explicitly define constructor to forward to base class explicit fmod_expr(const expression &left_hand_expr, @@ -744,7 +689,7 @@ struct fmod_expr inner_t evaluate() const { - using std::fmod; + BOOST_MATH_STD_USING return fmod(this->lhs.evaluate(), this->rhs.evaluate()); }; static const inner_t left_derivative(const inner_t &l, const inner_t &r, const inner_t &v) @@ -753,8 +698,8 @@ struct fmod_expr }; static const inner_t right_derivative(const inner_t &l, const inner_t &r, const inner_t &v) { - using std::trunc; - return -1.0 * trunc(l / r); + BOOST_MATH_STD_USING + return static_cast(-1.0) * trunc(l / r); }; }; @@ -764,8 +709,6 @@ struct fmod_left_float_expr { /** @brief * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit fmod_left_float_expr(const expression &arg_expr, const T v) @@ -774,12 +717,12 @@ struct fmod_left_float_expr inner_t evaluate() const { - using std::fmod; + BOOST_MATH_STD_USING return fmod(this->constant, this->arg.evaluate()); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) { - return -1.0 * trunc(constant / argv); + return static_cast(-1.0) * trunc(constant / argv); } }; @@ -789,8 +732,6 @@ struct fmod_right_float_expr { /** @brief * */ - using arg_type = ARG; - using value_type = T; using inner_t = rvar_t; explicit fmod_right_float_expr(const expression &arg_expr, const T v) @@ -799,7 +740,7 @@ struct fmod_right_float_expr inner_t evaluate() const { - using std::fmod; + BOOST_MATH_STD_USING return fmod(this->arg.evaluate(), this->constant); } static const inner_t derivative(const inner_t &argv, const inner_t &v, const T &constant) @@ -811,7 +752,7 @@ struct fmod_right_float_expr template fabs_expr fabs(const expression &arg) { - return fabs_expr(arg, 0.0); + return fabs_expr(arg, static_cast(0.0)); } template auto abs(const expression &arg) @@ -821,18 +762,18 @@ auto abs(const expression &arg) template ceil_expr ceil(const expression &arg) { - return ceil_expr(arg, 0.0); + return ceil_expr(arg, static_cast(0.0)); } template floor_expr floor(const expression &arg) { - return floor_expr(arg, 0.0); + return floor_expr(arg, static_cast(0.0)); } template exp_expr exp(const expression &arg) { - return exp_expr(arg, 0.0); + return exp_expr(arg, static_cast(0.0)); } template @@ -861,66 +802,64 @@ float_pow_expr_expr pow(const T &v, const expression log_expr log(const expression &arg) { - return log_expr(arg, 0.0); + return log_expr(arg, static_cast(0.0)); }; template sqrt_expr sqrt(const expression &arg) { - return sqrt_expr(arg, 0.0); + return sqrt_expr(arg, static_cast(0.0)); }; template auto frexp(const expression &arg, int *i) { - using std::frexp; - using std::pow; + BOOST_MATH_STD_USING frexp(arg.evaluate(), i); - return arg / pow(2.0, *i); + return arg / pow(static_cast(2.0), *i); } template auto ldexp(const expression &arg, const int &i) { - using std::ldexp; - using std::pow; - return arg * pow(2.0, i); + BOOST_MATH_STD_USING + return arg * pow(static_cast(2.0), i); } template cos_expr cos(const expression &arg) { - return cos_expr(arg, 0.0); + return cos_expr(arg, static_cast(0.0)); }; template sin_expr sin(const expression &arg) { - return sin_expr(arg, 0.0); + return sin_expr(arg, static_cast(0.0)); }; template tan_expr tan(const expression &arg) { - return tan_expr(arg, 0.0); + return tan_expr(arg, static_cast(0.0)); }; template acos_expr acos(const expression &arg) { - return acos_expr(arg, 0.0); + return acos_expr(arg, static_cast(0.0)); }; template asin_expr asin(const expression &arg) { - return asin_expr(arg, 0.0); + return asin_expr(arg, static_cast(0.0)); }; template atan_expr atan(const expression &arg) { - return atan_expr(arg, 0.0); + return atan_expr(arg, static_cast(0.0)); }; template @@ -945,48 +884,43 @@ atan2_left_float_expr atan2(const T &v, const expression trunc_expr trunc(const expression &arg) { - return trunc_expr(arg, 0.0); + return trunc_expr(arg, static_cast(0.0)); } template auto fmod(const expression &lhs, const expression &rhs) { - //return lhs - trunc(lhs / rhs) * rhs; return fmod_expr(lhs, rhs); } template auto fmod(const expression &lhs, const T rhs) { - //return lhs - trunc(lhs / rhs) * rhs; return fmod_right_float_expr(lhs, rhs); } template auto fmod(const T lhs, const expression &rhs) { - //return lhs - trunc(lhs / rhs) * rhs; return fmod_left_float_expr(rhs, lhs); } template round_expr round(const expression &arg) { - return round_expr(arg, 0.0); + return round_expr(arg, static_cast(0.0)); } template int iround(const expression &arg) { - using boost::math::iround; rvar tmp = arg.evaluate(); return iround(tmp.item()); } template long lround(const expression &arg) { - //using boost::math::lround; - using std::lround; + BOOST_MATH_STD_USING rvar tmp = arg.evaluate(); return lround(tmp.item()); } @@ -994,7 +928,6 @@ long lround(const expression &arg) template long long llround(const expression &arg) { - using boost::math::llround; rvar tmp = arg.evaluate(); return llround(tmp.item()); } @@ -1002,7 +935,6 @@ long long llround(const expression &arg) template int itrunc(const expression &arg) { - using boost::math::itrunc; rvar tmp = arg.evaluate(); return itrunc(tmp.item()); } @@ -1010,7 +942,6 @@ int itrunc(const expression &arg) template long ltrunc(const expression &arg) { - using boost::math::ltrunc; rvar tmp = arg.evaluate(); return ltrunc(tmp.item()); } @@ -1018,7 +949,6 @@ long ltrunc(const expression &arg) template long long lltrunc(const expression &arg) { - using boost::math::lltrunc; rvar tmp = arg.evaluate(); return lltrunc(tmp.item()); } @@ -1026,40 +956,40 @@ long long lltrunc(const expression &arg) template sinh_expr sinh(const expression &arg) { - return sinh_expr(arg, 0.0); + return sinh_expr(arg, static_cast(0.0)); } template cosh_expr cosh(const expression &arg) { - return cosh_expr(arg, 0.0); + return cosh_expr(arg, static_cast(0.0)); } template tanh_expr tanh(const expression &arg) { - return tanh_expr(arg, 0.0); + return tanh_expr(arg, static_cast(0.0)); } template log10_expr log10(const expression &arg) { - return log10_expr(arg, 0.0); + return log10_expr(arg, static_cast(0.0)); } template asinh_expr asinh(const expression &arg) { - return asinh_expr(arg, 0.0); + return asinh_expr(arg, static_cast(0.0)); } template acosh_expr acosh(const expression &arg) { - return acosh_expr(arg, 0.0); + return acosh_expr(arg, static_cast(0.0)); } template atanh_expr atanh(const expression &arg) { - return atanh_expr(arg, 0.0); + return atanh_expr(arg, static_cast(0.0)); } } // namespace reverse_mode } // namespace differentiation diff --git a/test/test_reverse_mode_autodiff_stl_support.cpp b/test/test_reverse_mode_autodiff_stl_support.cpp index ee27617c0..889bb7c84 100644 --- a/test/test_reverse_mode_autodiff_stl_support.cpp +++ b/test/test_reverse_mode_autodiff_stl_support.cpp @@ -709,7 +709,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_log10, T, all_float_types) gradient_tape& tape = get_active_tape(); tape.zero_grad(); rvar test_func_2 = log10(x_rvar * x_rvar); - T expected_deriv = 2 / (x_v * log(10)); + T expected_deriv = T{2.0} / (x_v * log(T{10.0})); test_func_2.backward(); BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol()); tape.clear();