Vcsn  2.1
Be Rational
left-mult.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/copy.hh>
4 #include <vcsn/algos/standard.hh>
6 #include <vcsn/ctx/traits.hh>
7 #include <vcsn/dyn/automaton.hh> // dyn::make_automaton
8 #include <vcsn/dyn/expression.hh>
9 #include <vcsn/dyn/weight.hh>
10 #include <vcsn/misc/raise.hh>
11 
12 namespace vcsn
13 {
14 
15  namespace detail
16  {
17  template <typename Aut>
19  {
20  using automaton_t = Aut;
25 
27  static automaton_t&
29  {
30  weightset_t ws(*res->context().weightset());
31  if (ws.is_zero(w))
32  zero_here(res);
33  else if (ws.is_one(w))
34  {}
35  else if (is_standard(res))
36  {
37  state_t initial = res->dst_of(res->initial_transitions().front());
38  for (auto t: res->all_out(initial))
39  res->lmul_weight(t, w);
40  }
41  else
42  for (auto t: res->initial_transitions())
43  res->lmul_weight(t, w);
44  return res;
45  }
46 
48  static automaton_t&
50  {
51  weightset_t ws(*res->context().weightset());
52  if (ws.is_zero(w))
53  zero_here(res);
54  else if (ws.is_one(w))
55  {}
56  else
57  for (auto t: res->final_transitions())
58  res->rmul_weight(t, w);
59  return res;
60  }
61 
63  static automaton_t&
65  {
67  a->set_initial(a->new_state());
68  res = std::move(a);
69  return res;
70  }
71  };
72  }
73 
74  /*-----------------------.
75  | left-mult(automaton). |
76  `-----------------------*/
77 
78  template <typename Aut>
79  inline
80  Aut&
81  left_mult_here(const weight_t_of<Aut>& w, Aut& res)
82  {
84  }
85 
86  template <typename AutIn,
87  typename AutOut = fresh_automaton_t_of<AutIn>>
88  inline
89  AutOut
90  left_mult(const weight_t_of<AutOut>& w, const AutIn& aut)
91  {
92  auto res = copy<AutIn, AutOut>(aut);
93  left_mult_here(w, res);
94  return res;
95  }
96 
97  namespace dyn
98  {
99  namespace detail
100  {
102  template <typename WeightSet, typename Aut>
103  automaton
104  left_mult(const weight& weight, const automaton& aut)
105  {
106  const auto& a1 = aut->as<Aut>();
107  const auto& w1 = weight->as<WeightSet>();
108  // FIXME: this is hairy because there is no elegant means (so
109  // far) to copy an automaton to a supertype, because the
110  // incoming context is not automatically converted to the
111  // supertype by vcsn::copy.
112  auto ctx = make_context(*a1->labelset(),
113  join(w1.weightset(), *a1->weightset()));
114  auto a2 = make_mutable_automaton(ctx);
115  copy_into(a1, a2);
116  auto w2 = ctx.weightset()->conv(w1.weightset(), w1.weight());
117  return make_automaton(::vcsn::left_mult_here(w2, a2));
118  }
119 
120  }
121  }
122 
123 
124  /*-------------------------.
125  | left-mult(expression). |
126  `-------------------------*/
127 
128  template <typename ExpSet>
129  inline
130  typename ExpSet::value_t
131  left_mult(const ExpSet& rs,
132  const weight_t_of<ExpSet>& w,
133  const typename ExpSet::value_t& r)
134  {
135  return rs.lmul(w, r);
136  }
137 
158  template <typename WeightSet, typename ExpSet>
159  expressionset<context<labelset_t_of<ExpSet>,
160  join_t<WeightSet, weightset_t_of<ExpSet>>>>
162  const ExpSet& rs)
163  {
164  auto ctx = make_context(*rs.labelset(), join(ws, *rs.weightset()));
165  return make_expressionset(ctx, rs.identities());
166  }
167 
168  namespace dyn
169  {
170  namespace detail
171  {
173  template <typename WeightSet, typename ExpSet>
174  expression
176  {
177  const auto& w1 = weight->as<WeightSet>();
178  const auto& r1 = exp->as<ExpSet>();
179  auto rs = join_weightset_expressionset(w1.weightset(), r1.expressionset());
180  auto w2 = rs.weightset()->conv(w1.weightset(), w1.weight());
181  auto r2 = rs.conv(r1.expressionset(), r1.expression());
182  return make_expression(rs,
183  ::vcsn::left_mult(rs, w2, r2));
184  }
185 
186  }
187  }
188 
189  /*------------------------.
190  | right-mult(automaton). |
191  `------------------------*/
192 
193  template <typename Aut>
194  inline
195  Aut&
196  right_mult_here(Aut& res, const weight_t_of<Aut>& w)
197  {
199  }
200 
201  template <typename Aut>
202  inline
204  right_mult(const Aut& aut, const weight_t_of<Aut>& w)
205  {
206  auto res = copy(aut);
207  right_mult_here(res, w);
208  return res;
209  }
210 
211  namespace dyn
212  {
213  namespace detail
214  {
216  template <typename Aut, typename WeightSet>
217  automaton
218  right_mult(const automaton& aut, const weight& weight)
219  {
220  const auto& a1 = aut->as<Aut>();
221  const auto& w1 = weight->as<WeightSet>();
222  // FIXME: see comment for left_mult.
223  auto ctx = make_context(*a1->labelset(),
224  join(*a1->weightset(), w1.weightset()));
225  auto a2 = make_mutable_automaton(ctx);
226  copy_into(a1, a2);
227  auto w2 = ctx.weightset()->conv(w1.weightset(), w1.weight());
228  return make_automaton(::vcsn::right_mult_here(a2, w2));
229  }
230  }
231  }
232 
233  /*--------------------------.
234  | right-mult(expression). |
235  `--------------------------*/
236 
237  template <typename ExpSet>
238  inline
239  typename ExpSet::value_t
240  right_mult(const ExpSet& rs,
241  const typename ExpSet::value_t& r,
242  const weight_t_of<ExpSet>& w)
243  {
244  return rs.rmul(r, w);
245  }
246 
247  namespace dyn
248  {
249  namespace detail
250  {
252  template <typename ExpSet, typename WeightSet>
253  expression
255  {
256  const auto& w1 = weight->as<WeightSet>();
257  const auto& r1 = exp->as<ExpSet>();
258  auto rs = join_weightset_expressionset(w1.weightset(), r1.expressionset());
259  auto w2 = rs.weightset()->conv(w1.weightset(), w1.weight());
260  auto r2 = rs.conv(r1.expressionset(), r1.expression());
261  return make_expression(rs,
262  ::vcsn::right_mult(rs, r2, w2));
263  }
264  }
265  }
266 }
automaton left_mult(const weight &weight, const automaton &aut)
Bridge.
Definition: left-mult.hh:104
Aut & left_mult_here(const weight_t_of< Aut > &w, Aut &res)
Definition: left-mult.hh:81
weightset_t_of< context_t > weightset_t
Definition: left-mult.hh:23
context make_context(const std::string &name)
Bridge.
Definition: make-context.hh:44
AutOut make_fresh_automaton(const AutIn &model)
Create an empty, mutable, automaton, based on another one.
Definition: copy.hh:239
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Definition: join.hh:44
static automaton_t & left_mult_here(const weight_t &w, automaton_t &res)
Left-multiplication by a weight.
Definition: left-mult.hh:28
Aut & right_mult_here(Aut &res, const weight_t_of< Aut > &w)
Definition: left-mult.hh:196
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:48
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:45
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:51
context_t_of< automaton_t > context_t
Definition: left-mult.hh:21
weight_t_of< context_t > weight_t
Definition: left-mult.hh:22
static automaton_t & zero_here(automaton_t &res)
Transform res into the (standard) empty automaton.
Definition: left-mult.hh:64
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
AutOut copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:254
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:80
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:86
bool is_standard(const Aut &a)
Whether a is standard.
Definition: standard.hh:27
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
void copy_into(const AutIn &in, AutOut &out, KeepState keep_state, KeepTrans keep_trans)
Copy selected states and transitions of an automaton.
Definition: copy.hh:126
expressionset< Context > make_expressionset(const Context &ctx, rat::identities identities={})
Shorthand to expressionset constructor.
auto rs
Definition: lift.hh:151
expression make_expression(const ExpSet &rs, const typename ExpSet::value_t &r)
Definition: expression.hh:83
static automaton_t & right_mult_here(automaton_t &res, const weight_t &w)
Right-multiplication by a weight.
Definition: left-mult.hh:49
Ctx make_context(const std::string &name)
Definition: make-context.hh:20
expression left_mult_expression(const weight &weight, const expression &exp)
Bridge (left_mult).
Definition: left-mult.hh:175
automaton right_mult(const automaton &aut, const weight &weight)
Bridge.
Definition: left-mult.hh:218
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:78
fresh_automaton_t_of< Aut > right_mult(const Aut &aut, const weight_t_of< Aut > &w)
Definition: left-mult.hh:204
state_t_of< automaton_t > state_t
Definition: left-mult.hh:24
context join(const context &c1, const context &c2)
Bridge.
Definition: make-context.hh:92
AutOut left_mult(const weight_t_of< AutOut > &w, const AutIn &aut)
Definition: left-mult.hh:90
expressionset< context< labelset_t_of< ExpSet >, join_t< WeightSet, weightset_t_of< ExpSet > > > > join_weightset_expressionset(const WeightSet &ws, const ExpSet &rs)
Join between an expressionset and a weightset.
Definition: left-mult.hh:161
expression right_mult_expression(const expression &exp, const weight &weight)
Bridge (right_mult).
Definition: left-mult.hh:254
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:50