autoppl  v0.8
A C++ template library for probabilistic programming
op_overloads.hpp
Go to the documentation of this file.
1 #pragma once
10 
11 namespace ppl {
12 
13 // operator- (unary)
14 PPL_UNARY_FUNC(operator-, UnaryMinus)
15 
16 // operator+,-,*,/ (binary)
17 template <class LHSType, class RHSType
18  , class = std::enable_if_t<
19  util::is_not_both_arithmetic_v<LHSType, RHSType> &&
20  util::is_valid_op_param_v<LHSType> &&
21  util::is_valid_op_param_v<RHSType>
22  > >
23 inline constexpr auto operator+(const LHSType& lhs,
24  const RHSType& rhs)
25 {
26  return expr::var::details::operator_helper<ad::core::Add>(lhs, rhs);
27 }
28 
29 template <class LHSType, class RHSType
30  , class = std::enable_if_t<
31  util::is_not_both_arithmetic_v<LHSType, RHSType> &&
32  util::is_valid_op_param_v<LHSType> &&
33  util::is_valid_op_param_v<RHSType>
34  > >
35 inline constexpr auto operator-(const LHSType& lhs,
36  const RHSType& rhs)
37 {
38  return expr::var::details::operator_helper<ad::core::Sub>(lhs, rhs);
39 }
40 
41 template <class LHSType, class RHSType
42  , class = std::enable_if_t<
43  util::is_not_both_arithmetic_v<LHSType, RHSType> &&
44  util::is_valid_op_param_v<LHSType> &&
45  util::is_valid_op_param_v<RHSType>
46  > >
47 inline constexpr auto operator*(const LHSType& lhs,
48  const RHSType& rhs)
49 {
50  return expr::var::details::operator_helper<ad::core::Mul>(lhs, rhs);
51 }
52 
53 template <class LHSType, class RHSType
54  , class = std::enable_if_t<
55  util::is_not_both_arithmetic_v<LHSType, RHSType> &&
56  util::is_valid_op_param_v<LHSType> &&
57  util::is_valid_op_param_v<RHSType>
58  > >
59 inline constexpr auto operator/(const LHSType& lhs,
60  const RHSType& rhs)
61 {
62  return expr::var::details::operator_helper<ad::core::Div>(lhs, rhs);
63 }
64 
65 // operator+=, -=, *=, /=
66 template <class TParamViewType
67  , class VarExprType
68  , class = std::enable_if_t<
69  util::is_tparam_v<TParamViewType> &&
70  util::is_valid_op_param_v<VarExprType>
71  > >
72 constexpr inline auto operator+=(const TParamViewType& tp_view,
73  const VarExprType& expr)
74 {
75  return expr::var::details::opeq_helper<expr::var::AddEq>(tp_view, expr);
76 }
77 
78 template <class TParamViewType
79  , class VarExprType
80  , class = std::enable_if_t<
81  util::is_tparam_v<TParamViewType> &&
82  util::is_valid_op_param_v<VarExprType>
83  > >
84 constexpr inline auto operator-=(const TParamViewType& tp_view,
85  const VarExprType& expr)
86 {
87  return expr::var::details::opeq_helper<expr::var::SubEq>(tp_view, expr);
88 }
89 
90 template <class TParamViewType
91  , class VarExprType
92  , class = std::enable_if_t<
93  util::is_tparam_v<TParamViewType> &&
94  util::is_valid_op_param_v<VarExprType>
95  > >
96 constexpr inline auto operator*=(const TParamViewType& tp_view,
97  const VarExprType& expr)
98 {
99  return expr::var::details::opeq_helper<expr::var::MulEq>(tp_view, expr);
100 }
101 
102 template <class TParamViewType
103  , class VarExprType
104  , class = std::enable_if_t<
105  util::is_tparam_v<TParamViewType> &&
106  util::is_valid_op_param_v<VarExprType>
107  > >
108 constexpr inline auto operator/=(const TParamViewType& tp_view,
109  const VarExprType& expr)
110 {
111  return expr::var::details::opeq_helper<expr::var::DivEq>(tp_view, expr);
112 }
113 
114 // operator,
115 template <class LHSExprType
116  , class RHSExprType
117  , class = std::enable_if_t<
118  (util::is_var_expr_v<LHSExprType> ||
119  util::is_model_expr_v<LHSExprType>) &&
120  (util::is_var_expr_v<RHSExprType> ||
121  util::is_model_expr_v<RHSExprType>)
122  > >
123 constexpr inline auto operator,(const LHSExprType& lhs,
124  const RHSExprType& rhs)
125 {
126  if constexpr (util::is_var_expr_v<LHSExprType> &&
127  util::is_var_expr_v<RHSExprType>) {
130  lhs_t wrap_lhs = lhs;
131  rhs_t wrap_rhs = rhs;
132  return expr::var::GlueNode<lhs_t, rhs_t>(wrap_lhs, wrap_rhs);
133 
134  } else if constexpr (util::is_model_expr_v<LHSExprType> &&
135  util::is_model_expr_v<RHSExprType>) {
136  return expr::model::GlueNode(lhs.self(), rhs.self());
137  } else {
138 
139  static_assert(util::is_var_expr_v<LHSExprType> &&
140  util::is_var_expr_v<RHSExprType>,
141  "Both expressions must be either variable expression or model expressions.");
142  }
143 }
144 
150 template <class VarType
151  , class DistType
152  , class = std::enable_if_t<
153  util::is_var_v<VarType> &&
154  util::is_dist_assignable_v<VarType> &&
155  util::is_dist_expr_v<DistType>
156  > >
157 inline constexpr auto operator|=(const VarType& var,
158  const DistType& dist)
159 {
160  using view_t = util::convert_to_param_t<VarType>;
161  view_t var_view = var;
162  return expr::model::BarEqNode(var_view, dist.self());
163 }
164 
165 template <class TPExpr
166  , class ModelExpr
167  , class = std::enable_if_t<
168  util::is_var_expr_v<TPExpr> &&
169  util::is_model_expr_v<ModelExpr>
170  > >
171 constexpr inline auto operator|(const TPExpr& tp,
172  const ModelExpr& model)
173 {
174  using tp_t = TPExpr;
175  using model_t = ModelExpr;
177 }
178 
179 } // namespace ppl
ppl::expr::var::GlueNode
Definition: glue.hpp:12
ppl::operator*=
constexpr auto operator*=(const TParamViewType &tp_view, const VarExprType &expr)
Definition: op_overloads.hpp:96
PPL_UNARY_FUNC
#define PPL_UNARY_FUNC(name, strct)
Definition: unary.hpp:88
ppl::expr::prog::ProgramNode
Definition: program.hpp:39
unary.hpp
ppl::operator/
constexpr auto operator/(const LHSType &lhs, const RHSType &rhs)
Definition: op_overloads.hpp:59
glue.hpp
program.hpp
ppl::operator|
constexpr auto operator|(const TPExpr &tp, const ModelExpr &model)
Definition: op_overloads.hpp:171
ppl::operator/=
constexpr auto operator/=(const TParamViewType &tp_view, const VarExprType &expr)
Definition: op_overloads.hpp:108
ppl::operator*
constexpr auto operator*(const LHSType &lhs, const RHSType &rhs)
Definition: op_overloads.hpp:47
ppl::operator+
constexpr auto operator+(const LHSType &lhs, const RHSType &rhs)
Definition: op_overloads.hpp:23
ppl::operator|=
constexpr auto operator|=(const VarType &var, const DistType &dist)
Definition: op_overloads.hpp:157
ppl::operator-=
constexpr auto operator-=(const TParamViewType &tp_view, const VarExprType &expr)
Definition: op_overloads.hpp:84
ppl::expr::model::GlueNode
Definition: glue.hpp:17
ppl::expr::model::BarEqNode
Definition: bar_eq.hpp:20
op_eq.hpp
glue.hpp
ppl::operator-
constexpr auto operator-(const LHSType &lhs, const RHSType &rhs)
Definition: op_overloads.hpp:35
ppl
Definition: bounded.hpp:11
ppl::util::convert_to_param_t
typename details::convert_to_param< T >::type convert_to_param_t
Definition: traits.hpp:148
bar_eq.hpp
binary.hpp
traits.hpp
ppl::operator+=
constexpr auto operator+=(const TParamViewType &tp_view, const VarExprType &expr)
Definition: op_overloads.hpp:72
ppl::operator,
constexpr auto operator,(const LHSExprType &lhs, const RHSExprType &rhs)
Definition: op_overloads.hpp:123