Vcsn  2.2a
Be Rational
constant-term.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/ctx/fwd.hh>
4 #include <vcsn/ctx/traits.hh>
6 #include <vcsn/dyn/expression.hh>
7 #include <vcsn/dyn/weight.hh>
8 
9 namespace vcsn
10 {
11 
12  namespace rat
13  {
14 
15  /*-----------------------------.
16  | constant_term(expression). |
17  `-----------------------------*/
18 
22  template <typename ExpSet>
24  : public ExpSet::const_visitor
25  {
26  public:
27  using expressionset_t = ExpSet;
28  using super_t = typename expressionset_t::const_visitor;
30 
32  using expression_t = typename expressionset_t::value_t;
35 
37  constexpr static const char* me() { return "constant_term"; }
38 
40  : ws_(ws)
41  {}
42 
44  : constant_term_visitor(*rs.weightset())
45  {}
46 
47  weight_t
49  {
50  v->accept(*this);
51  return std::move(res_);
52  }
53 
54  private:
57  {
58  v->accept(*this);
59  return std::move(res_);
60  }
61 
63  {
64  res_ = ws_.zero();
65  }
66 
68  {
69  res_ = ws_.one();
70  }
71 
73  {
74  res_ = ws_.zero();
75  }
76 
78  {
79  weight_t res = ws_.zero();
80  for (auto c: v)
81  res = ws_.add(res, constant_term(c));
82  res_ = std::move(res);
83  }
84 
87  template <typename Node>
88  void visit_product(const Node& v)
89  {
90  weight_t res = ws_.one();
91  for (auto c: v)
92  res = ws_.mul(res, constant_term(c));
93  res_ = std::move(res);
94  }
95 
100 
104 
106  {
107  res_ = ws_.transpose(constant_term(v.sub()));
108  }
109 
111  {
112  res_ = ws_.star(constant_term(v.sub()));
113  }
114 
116  {
117  res_ = ws_.mul(v.weight(), constant_term(v.sub()));
118  }
119 
121  {
122  res_ = ws_.mul(constant_term(v.sub()), v.weight());
123  }
124 
126  {
127  res_
128  = ws_.is_zero(constant_term(v.sub()))
129  ? ws_.one()
130  : ws_.zero();
131  }
132 
133  /*---------.
134  | tuple. |
135  `---------*/
136 
137  using tuple_t = typename super_t::tuple_t;
138  template <bool = context_t::is_lat,
139  typename Dummy = void>
140  struct visit_tuple
141  {
143  template <size_t I>
145  {
146  using rs_t = typename expressionset_t::template project_t<I>;
148  return constant_term(std::get<I>(v.sub()));
149  }
150 
152  template <size_t... I>
154  {
155  return visitor_.ws_.mul(work_<I>(v)...);
156  }
157 
160  {
162  }
163  const self_t& visitor_;
164  };
165 
166  template <typename Dummy>
167  struct visit_tuple<false, Dummy>
168  {
170  {
172  }
173  const self_t& visitor_;
174  };
175 
176  void visit(const tuple_t& v, std::true_type) override
177  {
178  res_ = visit_tuple<>{*this}(v);
179  }
180 
181  private:
184  };
185 
186  } // rat::
187 
189  template <typename ExpSet>
191  constant_term(const ExpSet& rs, const typename ExpSet::value_t& e)
192  {
194  return constant_term(e);
195  }
196 
197  namespace dyn
198  {
199  namespace detail
200  {
202  template <typename ExpSet>
203  weight
205  {
206  const auto& e = exp->as<ExpSet>();
207  return make_weight(*e.expressionset().weightset(),
208  constant_term<ExpSet>(e.expressionset(),
209  e.expression()));
210  }
211  }
212  }
213 
214 } // vcsn::
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
weight_t constant_term(const expression_t &v)
Easy recursion.
An inner node implementing a weight.
Definition: expression.hh:264
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
A functor to compute the constant term of an expression.
typename expressionset_t::value_t expression_t
weight_t_of< expressionset_t > weight_t
constant_term_visitor(const weightset_t &ws)
weight_t operator()(const expression_t &v)
weightset_t_of< expressionset_t > weightset_t
void visit_product(const Node &v)
Visit a variadic node whose constant-term is the product of the constant-terms of its children...
weight_t work_(const tuple_t &v, detail::index_sequence< I... >)
Product of the constant-terms of all tapes.
auto rs
Definition: lift.hh:151
context_t_of< expressionset_t > context_t
weight_t operator()(const tuple_t &v)
Entry point.
static constexpr const char * me()
Name of this algorithm, for error messages.
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:71
VCSN_RAT_UNSUPPORTED(ldiv)
y, yet both operands have a null constant-term.
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
Definition: weight.hh:79
typename super_t::tuple_t tuple_t
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
weight_t_of< ExpSet > constant_term(const ExpSet &rs, const typename ExpSet::value_t &e)
The constant term of e.
An inner node with multiple children.
Definition: expression.hh:118
weight constant_term(const expression &exp)
Bridge.
weight_t work_(const tuple_t &v)
Constant term for one tape.
typename expressionset_t::const_visitor super_t
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:55
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:92
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:58
constant_term_visitor(const expressionset_t &rs)
Definition: a-star.hh:8