Vcsn  2.4
Be Rational
weight-series.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/evaluate.hh>
6 #include <vcsn/algos/proper.hh>
7 #include <vcsn/dyn/fwd.hh>
8 #include <vcsn/dyn/automaton.hh>
9 #include <vcsn/dyn/value.hh>
10 #include <vcsn/weightset/fwd.hh> // nmin
11 
12 namespace vcsn
13 {
14  template <Automaton Aut>
15  auto
16  weight_series(const Aut& a)
17  -> std::enable_if_t<!is_tropical<weightset_t_of<Aut>>::value,
19  {
20  try
21  {
22  auto aut = proper(to_spontaneous(a));
23  return evaluate(aut);
24  }
25  catch (const std::runtime_error& e)
26  {
27  raise(e, " while computing weight series");
28  }
29  }
30 
31  template <Automaton Aut>
32  auto
33  weight_series(const Aut& a)
34  -> std::enable_if_t<is_tropical<weightset_t_of<Aut>>::value,
35  weight_t_of<Aut>>
36  {
37  try
38  {
39  auto res = path_monomial(a, lightest_path(a));
40  return res ? res->second : a->weightset()->zero();
41  }
42  catch (const std::runtime_error& e)
43  {
44  raise(e, " while computing weight series");
45  }
46  }
47 
48  namespace dyn
49  {
50  namespace detail
51  {
53  template <Automaton Aut>
54  weight
56  {
57  const auto& a = aut->as<Aut>();
58  auto res = ::vcsn::weight_series(a);
59  const auto& ctx = a->context();
60  return {*ctx.weightset(), res};
61  }
62  }
63  }
64 } // namespace vcsn
return res
Definition: multiply.hh:398
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
auto proper(const Aut &aut, direction dir=direction::backward, bool prune=true, const std::string &algo="auto") -> fresh_automaton_t_of< Aut, detail::proper_context< context_t_of< Aut >>>
Eliminate spontaneous transitions.
Definition: proper.hh:244
auto to_spontaneous(const Aut &aut) -> std::enable_if_t<!context_t_of< Aut >::is_lao, decltype(make_mutable_automaton(make_context(oneset(), *aut->weightset())))>
Convert to spontaneous automaton: change every transition's label to the empty word.
weight weight_series(const automaton &aut)
Bridge.
Definition: a-star.hh:8
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
A dyn automaton.
Definition: automaton.hh:17
auto weight_series(const Aut &a) -> std::enable_if_t<!is_tropical< weightset_t_of< Aut >>::value, weight_t_of< Aut >>
auto evaluate(const Aut &a, const word_t_of< Aut > &w) -> std::enable_if_t<!context_t_of< Aut >::is_lao, weight_t_of< Aut >>
General case of evaluation.
Definition: evaluate.hh:172
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:37
auto path_monomial(const Aut &aut, const std::vector< transition_t_of< Aut >> &path, state_t_of< Aut > src=Aut::element_type::pre(), state_t_of< Aut > dst=Aut::element_type::post()) -> boost::optional< typename detail::word_polynomialset_t< context_t_of< Aut >>::monomial_t >
Given a path (typically computed by lightest_path), the corresponding monomial (label, weight).
std::vector< transition_t_of< Aut > > lightest_path(const Aut &aut, state_t_of< Aut > source, state_t_of< Aut > dest, a_star_tag)
Definition: a-star.hh:151