Awali
Another Weighted Automata library
left_mult.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_LEFT_MULT_HH
18 # define AWALI_ALGOS_LEFT_MULT_HH
19 
20 #include <awali/sttc/algos/copy.hh>
22 #include <awali/sttc/ctx/traits.hh>
23 #include <awali/sttc/misc/raise.hh>
24 
25 namespace awali { namespace sttc {
26 
27 
28  namespace internal
29  {
30  template <typename Aut>
32  {
33  using automaton_t = Aut;
37 
39  static automaton_t&
41  {
42  weightset_t ws(*res->context().weightset());
43  if (ws.is_one(w))
44  {}
45  else if(standard)
46  {
47  require(is_standard(res), "left_mult : the automaton must be standard");
48  state_t initial = res->dst_of(*(res->initial_transitions().begin()));
49  if (ws.is_zero(w)) {
50  for(auto s : res->states())
51  if(s != initial)
52  res->del_state(s);
53  }
54  else
55  for (auto t: res->all_out(initial))
56  res->lmul_weight(t, w);
57  }
58  else if (ws.is_zero(w))
59  zero_here(res);
60  else
61  for (auto t: res->initial_transitions())
62  res->lmul_weight(t, w);
63  return res;
64  }
65 
67  static automaton_t&
69  {
70  weightset_t ws(*res->context().weightset());
71  if (ws.is_zero(w))
72  zero_here(res);
73  else if (ws.is_one(w))
74  {}
75  else
76  for (auto t: res->final_transitions())
77  res->rmul_weight(t, w);
78  return res;
79  }
80 
82  static automaton_t&
84  {
85  automaton_t a = make_shared_ptr<automaton_t>(res->context());
86  a->set_initial(a->add_state());
87  res = std::move(a);
88  return res;
89  }
90  };
91  }
92 
93  /*-----------------------.
94  | left-mult(automaton). |
95  `-----------------------*/
96 
114  template <typename Aut>
115  inline
116  Aut&
117  left_mult_here(Aut& res, const weight_t_of<Aut>& w, bool standard=false)
118  {
120  }
121 
122  template <typename Aut>
123  inline
124  typename Aut::element_type::automaton_nocv_t
125  left_mult(const Aut& aut, const weight_t_of<Aut>& w, bool standard=false)
126  {
127  auto res = copy(aut);
128  left_mult_here(res, w, standard);
129  return res;
130  }
131 
132 
133  /*--------------------.
134  | left-mult(ratexp). |
135  `--------------------*/
136 
137  template <typename RatExpSet>
138  inline
139  typename RatExpSet::ratexp_t
140  left_mult(const RatExpSet& rs,
141  const weight_t_of<RatExpSet>& w,
142  const typename RatExpSet::value_t& r)
143  {
144  return rs.lmul(w, r);
145  }
146 
147  /*------------------------.
148  | right-mult(automaton). |
149  `------------------------*/
150 
151  template <typename Aut>
152  inline
153  Aut&
154  right_mult_here(Aut& res, const weight_t_of<Aut>& w)
155  {
157  }
158 
159  template <typename Aut>
160  inline
161  typename Aut::element_type::automaton_nocv_t
162  right_mult(const Aut& aut, const weight_t_of<Aut>& w)
163  {
164  auto res = copy(aut);
165  right_mult_here(res, w);
166  return res;
167  }
168 
169  /*---------------------.
170  | right-mult(ratexp). |
171  `---------------------*/
172 
173  template <typename RatExpSet>
174  inline
175  typename RatExpSet::ratexp_t
176  right_mult(const RatExpSet& rs,
177  const typename RatExpSet::value_t& r,
178  const weight_t_of<RatExpSet>& w)
179  {
180  return rs.rmul(r, w);
181  }
182 
183 }}//end of ns awali::stc
184 
185 #endif // !AWALI_ALGOS_LEFT_MULT_HH
The semiring of floating Numbers.
Definition: r.hh:35
Aut & right_mult_here(Aut &res, const weight_t_of< Aut > &w)
Definition: left_mult.hh:154
bool is_standard(const Aut &a)
Definition: standard.hh:40
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
Aut standard(Aut &aut, bool keep_history=true)
Definition: standard.hh:92
AutOut copy(const AutIn &input, Pred keep_state, bool keep_history=true, bool transpose=false)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:189
Aut & left_mult_here(Aut &res, const weight_t_of< Aut > &w, bool standard=false)
multiplies the initial states by a weight
Definition: left_mult.hh:117
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
Aut::element_type::automaton_nocv_t left_mult(const Aut &aut, const weight_t_of< Aut > &w, bool standard=false)
Definition: left_mult.hh:125
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
Aut::element_type::automaton_nocv_t right_mult(const Aut &aut, const weight_t_of< Aut > &w)
Definition: left_mult.hh:162
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
Aut automaton_t
Definition: left_mult.hh:33
static automaton_t & left_mult_here(automaton_t &res, const weight_t &w, bool standard)
Left-multiplication by a weight.
Definition: left_mult.hh:40
context_t_of< automaton_t > context_t
Definition: left_mult.hh:34
weightset_t_of< context_t > weightset_t
Definition: left_mult.hh:36
weight_t_of< context_t > weight_t
Definition: left_mult.hh:35
static automaton_t & right_mult_here(automaton_t &res, const weight_t &w)
Right-multiplication by a weight.
Definition: left_mult.hh:68
static automaton_t & zero_here(automaton_t &res)
Transform res into the (standard) empty automaton.
Definition: left_mult.hh:83