Awali
Another Weighted Automata library
constant_term.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_CONSTANT_TERM_HH
18 # define AWALI_ALGOS_CONSTANT_TERM_HH
19 
20 # include <set>
21 
22 #include <awali/sttc/ctx/fwd.hh>
23 #include <awali/sttc/ctx/traits.hh>
25 
26 namespace awali { namespace sttc {
27 
28 
29  namespace rat
30  {
31 
32  /*------------------------.
33  | constant_term(ratexp). |
34  `------------------------*/
35 
37  template <typename RatExpSet>
39  : public RatExpSet::const_visitor
40  {
41  public:
42  using ratexpset_t = RatExpSet;
44  using ratexp_t = typename ratexpset_t::ratexp_t;
47 
48  using super_type = typename ratexpset_t::const_visitor;
49 
50  constexpr static const char* me() { return "constant_term"; }
51 
53  : ws_(*rs.weightset())
54  {}
55 
56  weight_t
57  operator()(const ratexp_t& v)
58  {
59  v->accept(*this);
60  return std::move(res_);
61  }
62 
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 
94  {
95  weight_t res = ws_.one();
96  for (auto c: v)
97  res = ws_.mul(res, constant_term(c));
98  res_ = std::move(res);
99  }
100 
103 
105  {
106  // FIXME: Code duplication with prod_t.
107  weight_t res = ws_.one();
108  for (auto c: v)
109  res = ws_.mul(res, constant_term(c));
110  res_ = std::move(res);
111  }
112 
114  {
115  // FIXME: Code duplication with prod_t.
116  weight_t res = ws_.one();
117  for (auto c: v)
118  res = ws_.mul(res, constant_term(c));
119  res_ = std::move(res);
120  }
121 
123  {
124  res_ = ws_.star(constant_term(v.sub()));
125  }
126 
128  {
129  res_ = ws_.mul(v.weight(), constant_term(v.sub()));
130  }
131 
133  {
134  res_ = ws_.mul(constant_term(v.sub()), v.weight());
135  }
136 
138  {
139  res_
140  = ws_.is_zero(constant_term(v.sub()))
141  ? ws_.one()
142  : ws_.zero();
143  }
144 
145  private:
146  //ratexpset_t ws_;
147  weightset_t ws_;
148  weight_t res_;
149  };
150 
151  } // rat::
152 
153  template <typename RatExpSet>
154  weight_t_of<RatExpSet>
155  constant_term(const RatExpSet& rs, const typename RatExpSet::ratexp_t& e)
156  {
158  return constant_term(e);
159  }
160 
161 }}//end of ns awali::stc
162 
163 #endif // !AWALI_ALGOS_CONSTANT_TERM_HH
The semiring of complex numbers.
Definition: c.hh:44
Definition: ratexp.hh:280
Definition: constant_term.hh:40
AWALI_RAT_VISIT(prod, v)
Definition: constant_term.hh:93
AWALI_RAT_VISIT(sum, v)
Definition: constant_term.hh:85
weight_t_of< ratexpset_t > weight_t
Definition: constant_term.hh:45
AWALI_RAT_VISIT(complement, v)
Definition: constant_term.hh:137
weight_t constant_term(const ratexp_t &v)
Easy recursion.
Definition: constant_term.hh:64
AWALI_RAT_VISIT(atom,)
Definition: constant_term.hh:80
AWALI_RAT_VISIT(shuffle, v)
Definition: constant_term.hh:113
AWALI_RAT_VISIT(lweight, v)
Definition: constant_term.hh:127
AWALI_RAT_VISIT(zero,)
Definition: constant_term.hh:70
typename ratexpset_t::ratexp_t ratexp_t
Definition: constant_term.hh:44
weightset_t_of< ratexpset_t > weightset_t
Definition: constant_term.hh:46
RatExpSet ratexpset_t
Definition: constant_term.hh:42
AWALI_RAT_VISIT(one,)
Definition: constant_term.hh:75
AWALI_RAT_VISIT(star, v)
Definition: constant_term.hh:122
context_t_of< ratexpset_t > context_t
Definition: constant_term.hh:43
constant_term_visitor(const ratexpset_t &rs)
Definition: constant_term.hh:52
constexpr static const char * me()
Definition: constant_term.hh:50
typename ratexpset_t::const_visitor super_type
Definition: constant_term.hh:48
weight_t operator()(const ratexp_t &v)
Definition: constant_term.hh:57
AWALI_RAT_VISIT(rweight, v)
Definition: constant_term.hh:132
Definition: ratexp.hh:262
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
weightset_description weightset(const std::string &k)
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
weight_t_of< RatExpSet > constant_term(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e)
Definition: constant_term.hh:155
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73