Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
left-mult.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_LEFT_MULT_HH
2 # define VCSN_ALGOS_LEFT_MULT_HH
3 
4 # include <vcsn/algos/copy.hh>
5 # include <vcsn/algos/standard.hh>
7 # include <vcsn/ctx/traits.hh>
8 # include <vcsn/dyn/automaton.hh> // dyn::make_automaton
9 # include <vcsn/dyn/ratexp.hh>
10 # include <vcsn/dyn/weight.hh>
11 # include <vcsn/misc/raise.hh>
12 
13 namespace vcsn
14 {
15 
16  namespace detail
17  {
18  template <typename Aut>
20  {
21  using automaton_t = Aut;
26 
28  static automaton_t&
30  {
31  weightset_t ws(*res->context().weightset());
32  if (ws.is_zero(w))
33  zero_here(res);
34  else if (ws.is_one(w))
35  {}
36  else if (is_standard(res))
37  {
38  state_t initial = res->dst_of(res->initial_transitions().front());
39  for (auto t: res->all_out(initial))
40  res->lmul_weight(t, w);
41  }
42  else
43  for (auto t: res->initial_transitions())
44  res->lmul_weight(t, w);
45  return res;
46  }
47 
49  static automaton_t&
51  {
52  weightset_t ws(*res->context().weightset());
53  if (ws.is_zero(w))
54  zero_here(res);
55  else if (ws.is_one(w))
56  {}
57  else
58  for (auto t: res->final_transitions())
59  res->rmul_weight(t, w);
60  return res;
61  }
62 
64  static automaton_t&
66  {
67  automaton_t a = make_shared_ptr<automaton_t>(res->context());
68  a->set_initial(a->new_state());
69  res = std::move(a);
70  return res;
71  }
72  };
73  }
74 
75  /*-----------------------.
76  | left-mult(automaton). |
77  `-----------------------*/
78 
79  template <typename Aut>
80  inline
81  Aut&
82  left_mult_here(const weight_t_of<Aut>& w, Aut& res)
83  {
85  }
86 
87  template <typename AutIn,
88  typename AutOut = typename AutIn::element_type::automaton_nocv_t>
89  inline
90  AutOut
91  left_mult(const weight_t_of<AutOut>& w, const AutIn& aut)
92  {
93  auto res = copy<AutIn, AutOut>(aut);
94  left_mult_here(w, res);
95  return res;
96  }
97 
98  namespace dyn
99  {
100  namespace detail
101  {
103  template <typename WeightSet, typename Aut>
104  automaton
105  left_mult(const weight& weight, const automaton& aut)
106  {
107  const auto& a1 = aut->as<Aut>();
108  const auto& w1 = weight->as<WeightSet>();
109  // FIXME: this is hairy because there is no elegant means (so
110  // far) to copy an automaton to a supertype, because the
111  // incoming context is not automatically converted to the
112  // supertype by vcsn::copy.
113  auto ctx = make_context(*a1->labelset(),
114  join(w1.weightset(), *a1->weightset()));
115  auto a2 = make_mutable_automaton(ctx);
116  copy_into(a1, a2);
117  auto w2 = ctx.weightset()->conv(w1.weightset(), w1.weight());
118  return make_automaton(::vcsn::left_mult_here(w2, a2));
119  }
120 
122  (const weight&, const automaton&) -> automaton);
123 
124  }
125  }
126 
127 
128  /*--------------------.
129  | left-mult(ratexp). |
130  `--------------------*/
131 
132  template <typename RatExpSet>
133  inline
134  typename RatExpSet::value_t
135  left_mult(const RatExpSet& rs,
136  const weight_t_of<RatExpSet>& w,
137  const typename RatExpSet::value_t& r)
138  {
139  return rs.lmul(w, r);
140  }
141 
162  template <typename WeightSet, typename RatExpSet>
163  ratexpset<context<labelset_t_of<RatExpSet>,
164  join_t<WeightSet, weightset_t_of<RatExpSet>>>>
166  const RatExpSet& rs)
167  {
168  auto ctx = make_context(*rs.labelset(), join(ws, *rs.weightset()));
169  return make_ratexpset(ctx, rs.identities());
170  }
171 
172  namespace dyn
173  {
174  namespace detail
175  {
177  template <typename WeightSet, typename RatExpSet>
178  ratexp
179  left_mult_ratexp(const weight& weight, const ratexp& exp)
180  {
181  const auto& w1 = weight->as<WeightSet>();
182  const auto& r1 = exp->as<RatExpSet>();
183  auto rs = join_weightset_ratexpset(w1.weightset(), r1.ratexpset());
184  auto w2 = rs.weightset()->conv(w1.weightset(), w1.weight());
185  auto r2 = rs.conv(r1.ratexpset(), r1.ratexp());
186  return make_ratexp(rs,
187  ::vcsn::left_mult(rs, w2, r2));
188  }
189 
191  (const weight&, const ratexp&) -> ratexp);
192 
193  }
194  }
195 
196  /*------------------------.
197  | right-mult(automaton). |
198  `------------------------*/
199 
200  template <typename Aut>
201  inline
202  Aut&
203  right_mult_here(Aut& res, const weight_t_of<Aut>& w)
204  {
206  }
207 
208  template <typename Aut>
209  inline
210  typename Aut::element_type::automaton_nocv_t
211  right_mult(const Aut& aut, const weight_t_of<Aut>& w)
212  {
213  auto res = copy(aut);
214  right_mult_here(res, w);
215  return res;
216  }
217 
218  namespace dyn
219  {
220  namespace detail
221  {
223  template <typename Aut, typename WeightSet>
224  automaton
225  right_mult(const automaton& aut, const weight& weight)
226  {
227  const auto& a1 = aut->as<Aut>();
228  const auto& w1 = weight->as<WeightSet>();
229  // FIXME: see comment for left_mult.
230  auto ctx = make_context(*a1->labelset(),
231  join(*a1->weightset(), w1.weightset()));
232  auto a2 = make_mutable_automaton(ctx);
233  copy_into(a1, a2);
234  auto w2 = ctx.weightset()->conv(w1.weightset(), w1.weight());
235  return make_automaton(::vcsn::right_mult_here(a2, w2));
236  }
237 
239  (const automaton&, const weight&) -> automaton);
240  }
241  }
242 
243  /*---------------------.
244  | right-mult(ratexp). |
245  `---------------------*/
246 
247  template <typename RatExpSet>
248  inline
249  typename RatExpSet::value_t
250  right_mult(const RatExpSet& rs,
251  const typename RatExpSet::value_t& r,
252  const weight_t_of<RatExpSet>& w)
253  {
254  return rs.rmul(r, w);
255  }
256 
257  namespace dyn
258  {
259  namespace detail
260  {
262  template <typename RatExpSet, typename WeightSet>
263  ratexp
264  right_mult_ratexp(const ratexp& exp, const weight& weight)
265  {
266  const auto& w1 = weight->as<WeightSet>();
267  const auto& r1 = exp->as<RatExpSet>();
268  auto rs = join_weightset_ratexpset(w1.weightset(), r1.ratexpset());
269  auto w2 = rs.weightset()->conv(w1.weightset(), w1.weight());
270  auto r2 = rs.conv(r1.ratexpset(), r1.ratexp());
271  return make_ratexp(rs,
272  ::vcsn::right_mult(rs, r2, w2));
273  }
274 
276  (const ratexp&, const weight&) -> ratexp);
277 
278  }
279  }
280 }
281 
282 #endif // !VCSN_ALGOS_LEFT_MULT_HH
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
ratexpset< context< labelset_t_of< RatExpSet >, join_t< WeightSet, weightset_t_of< RatExpSet > > > > join_weightset_ratexpset(const WeightSet &ws, const RatExpSet &rs)
Join between a ratexpset and a weightset.
Definition: left-mult.hh:165
static automaton_t & left_mult_here(const weight_t &w, automaton_t &res)
Left-multiplication by a weight.
Definition: left-mult.hh:29
ratexpset< Context > make_ratexpset(const Context &ctx, rat::identities identities)
Shorthand to ratexpset constructor.
Definition: ratexpset.hh:471
bool is_standard(const Aut &a)
Whether a is standard.
Definition: standard.hh:27
context make_context(const std::string &name)
Bridge.
Definition: make-context.hh:46
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:82
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
automaton left_mult(const weight &weight, const automaton &aut)
Bridge.
Definition: left-mult.hh:105
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:171
Aut & left_mult_here(const weight_t_of< Aut > &w, Aut &res)
Definition: left-mult.hh:82
automaton right_mult(const automaton &aut, const weight &weight)
Bridge.
Definition: left-mult.hh:225
static automaton_t & zero_here(automaton_t &res)
Transform res into the (standard) empty automaton.
Definition: left-mult.hh:65
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
state_t_of< automaton_t > state_t
Definition: left-mult.hh:25
Aut::element_type::automaton_nocv_t right_mult(const Aut &aut, const weight_t_of< Aut > &w)
Definition: left-mult.hh:211
static automaton_t & right_mult_here(automaton_t &res, const weight_t &w)
Right-multiplication by a weight.
Definition: left-mult.hh:50
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
Aut & right_mult_here(Aut &res, const weight_t_of< Aut > &w)
Definition: left-mult.hh:203
weightset_t_of< context_t > weightset_t
Definition: left-mult.hh:24
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
context_t_of< automaton_t > context_t
Definition: left-mult.hh:22
void copy_into(const AutIn &in, AutOut &out, Pred keep_state)
Copy an automaton.
Definition: copy.hh:101
weight_t_of< context_t > weight_t
Definition: left-mult.hh:23
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Definition: join.hh:44
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
ratexp right_mult_ratexp(const ratexp &exp, const weight &weight)
Bridge.
Definition: left-mult.hh:264
Ctx make_context(const std::string &name)
Definition: make-context.hh:22
AutOut left_mult(const weight_t_of< AutOut > &w, const AutIn &aut)
Definition: left-mult.hh:91
ratexp left_mult_ratexp(const weight &weight, const ratexp &exp)
Bridge.
Definition: left-mult.hh:179