From 26f19e1429312e5e0d6fcbc3db325f9923d76d54 Mon Sep 17 00:00:00 2001 From: Tomasz Sobczyk Date: Sun, 29 Nov 2020 22:50:49 +0100 Subject: [PATCH] Make automatic differentiation node types constexpr. --- src/learn/autograd.h | 48 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/learn/autograd.h b/src/learn/autograd.h index 714f741a..4edf0e4c 100644 --- a/src/learn/autograd.h +++ b/src/learn/autograd.h @@ -79,6 +79,8 @@ namespace Learner::Autograd::UnivariateStatic template struct Evaluable { + constexpr Evaluable() = default; + template auto eval(const std::tuple& args) const { @@ -121,7 +123,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - VariableParameter() + constexpr VariableParameter() { } @@ -143,7 +145,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - ConstantParameter() + constexpr ConstantParameter() { } @@ -165,7 +167,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - Constant(T x) : + constexpr Constant(T x) : m_x(std::move(x)) { } @@ -191,7 +193,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - Sum(LhsT&& lhs, RhsT&& rhs) : + constexpr Sum(LhsT&& lhs, RhsT&& rhs) : m_lhs(std::forward(lhs)), m_rhs(std::forward(rhs)) { @@ -215,19 +217,19 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto operator+(LhsT&& lhs, RhsT&& rhs) + constexpr auto operator+(LhsT&& lhs, RhsT&& rhs) { return Sum(std::forward(lhs), std::forward(rhs)); } template ::ValueType> - auto operator+(LhsT&& lhs, Id rhs) + constexpr auto operator+(LhsT&& lhs, Id rhs) { return Sum&&>(std::forward(lhs), Constant(rhs)); } template ::ValueType> - auto operator+(Id lhs, RhsT&& rhs) + constexpr auto operator+(Id lhs, RhsT&& rhs) { return Sum&&, RhsT&&>(Constant(lhs), std::forward(rhs)); } @@ -237,7 +239,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - Difference(LhsT&& lhs, RhsT&& rhs) : + constexpr Difference(LhsT&& lhs, RhsT&& rhs) : m_lhs(std::forward(lhs)), m_rhs(std::forward(rhs)) { @@ -261,19 +263,19 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto operator-(LhsT&& lhs, RhsT&& rhs) + constexpr auto operator-(LhsT&& lhs, RhsT&& rhs) { return Difference(std::forward(lhs), std::forward(rhs)); } template ::ValueType> - auto operator-(LhsT&& lhs, Id rhs) + constexpr auto operator-(LhsT&& lhs, Id rhs) { return Difference&&>(std::forward(lhs), Constant(rhs)); } template ::ValueType> - auto operator-(Id lhs, RhsT&& rhs) + constexpr auto operator-(Id lhs, RhsT&& rhs) { return Difference&&, RhsT&&>(Constant(lhs), std::forward(rhs)); } @@ -283,7 +285,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - Product(LhsT&& lhs, RhsT&& rhs) : + constexpr Product(LhsT&& lhs, RhsT&& rhs) : m_lhs(std::forward(lhs)), m_rhs(std::forward(rhs)) { @@ -307,19 +309,19 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto operator*(LhsT&& lhs, RhsT&& rhs) + constexpr auto operator*(LhsT&& lhs, RhsT&& rhs) { return Product(std::forward(lhs), std::forward(rhs)); } template ::ValueType> - auto operator*(LhsT&& lhs, Id rhs) + constexpr auto operator*(LhsT&& lhs, Id rhs) { return Product&&>(std::forward(lhs), Constant(rhs)); } template ::ValueType> - auto operator*(Id lhs, RhsT&& rhs) + constexpr auto operator*(Id lhs, RhsT&& rhs) { return Product&&, RhsT&&>(Constant(lhs), std::forward(rhs)); } @@ -329,7 +331,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - explicit Negation(ArgT&& x) : + constexpr explicit Negation(ArgT&& x) : m_x(std::forward(x)) { } @@ -351,7 +353,7 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto operator-(ArgT&& x) + constexpr auto operator-(ArgT&& x) { return Negation(std::forward(x)); } @@ -361,7 +363,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - explicit Sigmoid(ArgT&& x) : + constexpr explicit Sigmoid(ArgT&& x) : m_x(std::forward(x)) { } @@ -393,7 +395,7 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto sigmoid(ArgT&& x) + constexpr auto sigmoid(ArgT&& x) { return Sigmoid(std::forward(x)); } @@ -403,7 +405,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - explicit Pow(ArgT&& x, Id exponent) : + constexpr explicit Pow(ArgT&& x, Id exponent) : m_x(std::forward(x)), m_exponent(std::move(exponent)) { @@ -427,7 +429,7 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto pow(ArgT&& x, Id exp) + constexpr auto pow(ArgT&& x, Id exp) { return Pow(std::forward(x), std::move(exp)); } @@ -437,7 +439,7 @@ namespace Learner::Autograd::UnivariateStatic { using ValueType = T; - explicit Log(ArgT&& x) : + constexpr explicit Log(ArgT&& x) : m_x(std::forward(x)) { } @@ -469,7 +471,7 @@ namespace Learner::Autograd::UnivariateStatic }; template ::ValueType> - auto log(ArgT&& x) + constexpr auto log(ArgT&& x) { return Log(std::forward(x)); }