Vcsn  2.8
Be Rational
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  res_ = constant_term(v.sub());
88  }
89 
91  {
92  weight_t res = ws_.zero();
93  for (auto c: v)
94  res = ws_.add(res, constant_term(c));
95  res_ = std::move(res);
96  }
97 
100  template <typename Node>
101  void visit_product(const Node& v)
102  {
103  weight_t res = ws_.one();
104  for (auto c: v)
105  res = ws_.mul(res, constant_term(c));
106  res_ = std::move(res);
107  }
108 
113 
118 
123 
125  {
126  res_ = ws_.transpose(constant_term(v.sub()));
127  }
128 
130  {
131  res_ = ws_.star(constant_term(v.sub()));
132  }
133 
135  {
136  res_ = ws_.mul(v.weight(), constant_term(v.sub()));
137  }
138 
140  {
141  res_ = ws_.mul(constant_term(v.sub()), v.weight());
142  }
143 
145  {
146  res_
147  = ws_.is_zero(constant_term(v.sub()))
148  ? ws_.one()
149  : ws_.zero();
150  }
151 
152  /*---------.
153  | tuple. |
154  `---------*/
155 
156  using tuple_t = typename super_t::tuple_t;
157  template <bool = context_t::is_lat,
158  typename Dummy = void>
159  struct visit_tuple
160  {
162  template <size_t I>
164  {
165  return ::vcsn::constant_term(visitor_.rs_.template project<I>(),
166  std::get<I>(v.sub()));
167  }
168 
170  template <size_t... I>
172  {
173  return visitor_.ws_.mul(work_<I>(v)...);
174  }
175 
178  {
180  }
181  const self_t& visitor_;
182  };
183 
184  template <typename Dummy>
185  struct visit_tuple<false, Dummy>
186  {
188  {
190  }
191  const self_t& visitor_;
192  };
193 
194  void visit(const tuple_t& v, std::true_type) override
195  {
196  res_ = visit_tuple<>{*this}(v);
197  }
198 
199  private:
203  };
204  } // rat::
205 
206  template <typename ExpSet>
208  constant_term(const ExpSet& rs, const typename ExpSet::value_t& e)
209  {
211  return c(e);
212  }
213 
214  namespace dyn
215  {
216  namespace detail
217  {
219  template <typename ExpSet>
220  weight
222  {
223  const auto& e = exp->as<ExpSet>();
224  return {*e.valueset().weightset(),
225  constant_term<ExpSet>(e.valueset(), e.value())};
226  }
227  }
228  }
229 } // vcsn::
weight_t_of< expressionset_t > weight_t
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
Definition: traits.hh:67
weight_t constant_term(const expression_t &v)
Easy recursion.
weight_t operator()(const tuple_t &v)
Entry point.
typename expressionset_t::const_visitor super_t
An inner node with multiple children.
Definition: expression.hh:119
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.
VCSN_RAT_UNSUPPORTED(ldivide)
y`, yet both operands have a null constant-term.
An inner node to name the subexpression.
Definition: expression.hh:290
context_t_of< expressionset_t > context_t
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
constant_term_visitor(const expressionset_t &rs)
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
Definition: a-star.hh:8
typename expressionset_t::value_t expression_t
A functor to compute the constant term of an expression.
void visit(const tuple_t &v, std::true_type) override
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:38
An inner node implementing a weight.
Definition: expression.hh:256
A static list of size_t.
Definition: tuple.hh:32
weight_t operator()(const expression_t &v)
weightset_t_of< expressionset_t > weightset_t
typename super_t::tuple_t tuple_t
return v
Definition: multiply.hh:362
value_impl< detail::expression_tag > expression
Definition: fwd.hh:31
weight_t_of< ExpSet > constant_term(const ExpSet &rs, const typename ExpSet::value_t &e)
The constant term of e.
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
Definition: traits.hh:66
weight constant_term(const expression &exp)
Bridge.
weight_t work_(const tuple_t &v)
Constant term for one tape.
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
value_impl< detail::weight_tag > weight
Definition: fwd.hh:34
static constexpr const char * me()
Name of this algorithm, for error messages.
return res
Definition: multiply.hh:399