Awali
Another Weighted Automata library
exp_copy.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_EXP_COPY_HH
18 # define AWALI_ALGOS_EXP_COPY_HH
19 
20 #include <awali/sttc/ctx/fwd.hh>
21 #include <awali/sttc/ctx/traits.hh>
23 
24 namespace awali { namespace sttc {
25 
26  namespace rat
27  {
30  template <typename RatExpSet,
31  typename Weightset = weightset_t_of<RatExpSet>>
33  : public RatExpSet::const_visitor
34  {
35  public:
36  using in_ratexpset_t = RatExpSet;
40 
41  using out_weightset_t = Weightset;
44  using ratexp_t = typename in_ratexpset_t::ratexp_t;
45  using out_ratexp_t = typename out_ratexpset_t::ratexp_t;
46 
47  using super_type = typename RatExpSet::const_visitor;
48 
49  constexpr static const char* me() { return "exp copy"; }
50 
52  : out_weightset_ (out_weightset),
53  in_ratexpset_ (ratexpset)
54  {}
55 
57  operator()(const ratexp_t& v)
58  {
59  v->accept(*this);
60  return res_;
61  }
62 
64  {
65  res_ = out_ratexpset_.zero();
66  }
67 
69  {
70  res_ = out_ratexpset_.one();
71  }
72 
74  {
75  res_ = out_ratexpset_.atom(e.value());
76  }
77 
79  {
80  auto r = out_ratexpset_.zero();
81  for (auto c: e)
82  {
83  c->accept(*this);
84  r = out_ratexpset_.add(r, res_);
85  }
86  res_ = r;
87  }
88 
94 
96  {
97  auto r = out_ratexpset_.one();
98  for (auto c: e)
99  {
100  c->accept(*this);
101  r = out_ratexpset_.mul(r, res_);
102  }
103  res_ = r;
104  }
105 
107  {
108  e.sub()->accept(*this);
109  res_ = out_ratexpset_.star(res_);
110  }
111 
113  {
114  e.sub()->accept(*this);
115  res_ = out_ratexpset_.lmul(out_weightset_.conv(in_weightset_, e.weight()), res_);
116  }
117 
119  {
120  e.sub()->accept(*this);
121  res_ = out_ratexpset_.rmul(res_, out_weightset_.conv(in_weightset_, e.weight()));
122  }
123 
125  return out_ratexpset_;
126  }
127 
128  private:
129  const out_weightset_t& out_weightset_;
130  const in_ratexpset_t& in_ratexpset_;
131  out_context_t out_context_{*in_ratexpset_.labelset(), out_weightset_};
132  out_ratexpset_t out_ratexpset_{out_context_, in_ratexpset_.identities()};
133  in_weightset_t in_weightset_ = *in_ratexpset_.weightset();
134  typename out_ratexpset_t::ratexp_t res_;
135  };
136 
137  } // rat::
138 
152  template <typename OutWeightSet,
153  typename RatExpSet
154  >
155  auto
156  ratexp_copy(const typename RatExpSet::ratexp_t& exp,
157  const RatExpSet& ratexpset,
158  const OutWeightSet& out_weightset = OutWeightSet() )
160  {
162  return copyer(exp);
163  }
164 
174  template <typename RatExpSet>
175  auto
176  ratexp_copy(const typename RatExpSet::ratexp_t& exp,
177  const RatExpSet& ratexpset)
179  {
181  return copyer(exp);
182  }
183  template <typename OutWeightSet,
192  typename RatExpSet>
193  auto
194  promote_ratexpset(const RatExpSet& ratexpset,
195  const OutWeightSet& out_weightset = OutWeightSet())
197  {
199  return copyer.get_ratexpset();
200  }
201  }
202 }//end of ns awali::stc
203 
204 #endif // !AWALI_ALGOS_EXP_COPY_HH
The semiring of complex numbers.
Definition: c.hh:44
const labelset_ptr & labelset() const
Definition: context.hh:152
The semiring of floating Numbers.
Definition: r.hh:35
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: exp_copy.hh:34
RatExpSet in_ratexpset_t
Definition: exp_copy.hh:36
AWALI_RAT_VISIT(zero,)
Definition: exp_copy.hh:63
typename in_ratexpset_t::ratexp_t ratexp_t
Definition: exp_copy.hh:44
context< labelset_t, out_weightset_t > out_context_t
Definition: exp_copy.hh:42
ratexpset_of< out_context_t > out_ratexpset_t
Definition: exp_copy.hh:43
constexpr static const char * me()
Definition: exp_copy.hh:49
weightset_t_of< in_context_t > in_weightset_t
Definition: exp_copy.hh:38
labelset_t_of< in_context_t > labelset_t
Definition: exp_copy.hh:39
expcopy_visitor(const in_ratexpset_t &ratexpset, const out_weightset_t &out_weightset)
Definition: exp_copy.hh:51
AWALI_RAT_VISIT(sum, e)
Definition: exp_copy.hh:78
typename out_ratexpset_t::ratexp_t out_ratexp_t
Definition: exp_copy.hh:45
context_t_of< in_ratexpset_t > in_context_t
Definition: exp_copy.hh:37
out_ratexp_t operator()(const ratexp_t &v)
Definition: exp_copy.hh:57
AWALI_RAT_VISIT(atom, e)
Definition: exp_copy.hh:73
AWALI_RAT_VISIT(lweight, e)
Definition: exp_copy.hh:112
AWALI_RAT_VISIT(rweight, e)
Definition: exp_copy.hh:118
AWALI_RAT_VISIT(one,)
Definition: exp_copy.hh:68
typename RatExpSet::const_visitor super_type
Definition: exp_copy.hh:47
AWALI_RAT_VISIT(star, e)
Definition: exp_copy.hh:106
Weightset out_weightset_t
Definition: exp_copy.hh:41
out_ratexpset_t get_ratexpset()
Definition: exp_copy.hh:124
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
variadic_mul_mixin< rat::ratexpset_impl< Context > > ratexpset
Definition: fwd.hh:182
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
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
auto promote_ratexpset(const RatExpSet &ratexpset, const OutWeightSet &out_weightset=OutWeightSet()) -> typename rat::expcopy_visitor< RatExpSet, OutWeightSet >::out_ratexpset_t
Compute a derived ratexpset.
Definition: exp_copy.hh:194
auto ratexp_copy(const typename RatExpSet::ratexp_t &exp, const RatExpSet &ratexpset, const OutWeightSet &out_weightset=OutWeightSet()) -> typename rat::expcopy_visitor< RatExpSet, OutWeightSet >::out_ratexp_t
copy a rational expression
Definition: exp_copy.hh:156
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
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38
value_t mul(const Ts &... ts) const
Definition: weightset.hh:49
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73