Vcsn  2.3
Be Rational
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
constant-term.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/project.hh>
5 #include <vcsn/ctx/fwd.hh>
6 #include <vcsn/ctx/traits.hh>
7 #include <vcsn/dyn/value.hh>
8 
9 namespace vcsn
10 {
11 
13  template <typename ExpSet>
14  weight_t_of<ExpSet>
15  constant_term(const ExpSet& rs, const typename ExpSet::value_t& e);
16 
17  namespace rat
18  {
19 
20  /*-----------------------------.
21  | constant_term(expression). |
22  `-----------------------------*/
23 
27  template <typename ExpSet>
29  : public ExpSet::const_visitor
30  {
31  public:
32  using expressionset_t = ExpSet;
33  using super_t = typename expressionset_t::const_visitor;
35 
37  using expression_t = typename expressionset_t::value_t;
40 
42  constexpr static const char* me() { return "constant_term"; }
43 
45  : rs_{rs}
46  , ws_{*rs_.weightset()}
47  {}
48 
50  {
51  try
52  {
53  return constant_term(v);
54  }
55  catch (const std::runtime_error& e)
56  {
57  raise(e, " while computing constant-term of: ", to_string(rs_, v));
58  }
59 
60  }
61 
62  private:
65  {
66  v->accept(*this);
67  return std::move(res_);
68  }
69 
71  {
72  res_ = ws_.zero();
73  }
74 
76  {
77  res_ = ws_.one();
78  }
79 
81  {
82  res_ = ws_.zero();
83  }
84 
86  {
87  weight_t res = ws_.zero();
88  for (auto c: v)
89  res = ws_.add(res, constant_term(c));
90  res_ = std::move(res);
91  }
92 
95  template <typename Node>
96  void visit_product(const Node& v)
97  {
98  weight_t res = ws_.one();
99  for (auto c: v)
100  res = ws_.mul(res, constant_term(c));
101  res_ = std::move(res);
102  }
103 
108 
113 
118 
120  {
121  res_ = ws_.transpose(constant_term(v.sub()));
122  }
123 
125  {
126  res_ = ws_.star(constant_term(v.sub()));
127  }
128 
130  {
131  res_ = ws_.mul(v.weight(), constant_term(v.sub()));
132  }
133 
135  {
136  res_ = ws_.mul(constant_term(v.sub()), v.weight());
137  }
138 
140  {
141  res_
142  = ws_.is_zero(constant_term(v.sub()))
143  ? ws_.one()
144  : ws_.zero();
145  }
146 
147  /*---------.
148  | tuple. |
149  `---------*/
150 
151  using tuple_t = typename super_t::tuple_t;
152  template <bool = context_t::is_lat,
153  typename Dummy = void>
154  struct visit_tuple
155  {
157  template <size_t I>
159  {
160  return ::vcsn::constant_term(visitor_.rs_.template project<I>(),
161  std::get<I>(v.sub()));
162  }
163 
165  template <size_t... I>
167  {
168  return visitor_.ws_.mul(work_<I>(v)...);
169  }
170 
173  {
175  }
176  const self_t& visitor_;
177  };
178 
179  template <typename Dummy>
180  struct visit_tuple<false, Dummy>
181  {
183  {
185  }
186  const self_t& visitor_;
187  };
188 
189  void visit(const tuple_t& v, std::true_type) override
190  {
191  res_ = visit_tuple<>{*this}(v);
192  }
193 
194  private:
198  };
199  } // rat::
200 
201  template <typename ExpSet>
203  constant_term(const ExpSet& rs, const typename ExpSet::value_t& e)
204  {
206  return c(e);
207  }
208 
209  namespace dyn
210  {
211  namespace detail
212  {
214  template <typename ExpSet>
215  weight
217  {
218  const auto& e = exp->as<ExpSet>();
219  return {*e.valueset().weightset(),
220  constant_term<ExpSet>(e.valueset(), e.value())};
221  }
222  }
223  }
224 } // vcsn::
return v
Definition: multiply.hh:361
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
static constexpr const char * me()
Name of this algorithm, for error messages.
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
void visit(const tuple_t &v, std::true_type) override
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename super_t::tuple_t tuple_t
An inner node implementing a weight.
Definition: expression.hh:264
VCSN_RAT_UNSUPPORTED(ldivide)
y, yet both operands have a null constant-term.
return res
Definition: multiply.hh:398
typename expressionset_t::value_t expression_t
weight_t constant_term(const expression_t &v)
Easy recursion.
context_t_of< expressionset_t > context_t
weight_t_of< expressionset_t > weight_t
weight constant_term(const expression &exp)
Bridge.
auto rs
Definition: lift.hh:152
Definition: a-star.hh:8
typename expressionset_t::const_visitor super_t
weight_t operator()(const tuple_t &v)
Entry point.
void visit_product(const Node &v)
Visit a variadic node whose constant-term is the product of the constant-terms of its children...
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:41
weightset_t_of< expressionset_t > weightset_t
weight_t operator()(const expression_t &v)
auto & as()
Extract wrapped typed value.
Definition: value.hh:53
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
A functor to compute the constant term of an expression.
An inner node with multiple children.
Definition: expression.hh:118
weight_t work_(const tuple_t &v, detail::index_sequence< I... >)
Product of the constant-terms of all tapes.
weight_t_of< ExpSet > constant_term(const ExpSet &rs, const typename ExpSet::value_t &e)
The constant term of e.
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
weight_t work_(const tuple_t &v)
Constant term for one tape.
constant_term_visitor(const expressionset_t &rs)