3 #include <unordered_map>
32 template <Automaton Aut1, Automaton Aut2>
37 "multiply_here: requires free labelset");
46 template <Automaton Aut1, Automaton Aut2>
50 const auto& ls = *res->labelset();
51 const auto& ws = *res->weightset();
52 const auto& bws = *b->weightset();
66 const auto& map =
copy.state_map();
71 auto s1 = res->src_of(t1);
72 auto w1 = res->weight_of(t1);
73 res->del_transition(t1);
74 for (
auto t2: init_ts)
75 res->new_transition(s1,
76 map.at(b->dst_of(t2)),
79 ws.conv(bws, b->weight_of(t2))));
87 template <Automaton Aut1, Automaton Aut2>
91 const auto& ls = *res->labelset();
92 const auto& bls = *b->labelset();
93 const auto& ws = *res->weightset();
94 const auto& bws = *b->weightset();
107 [b](state_t_of<Aut2> s)
109 return !b->is_initial(s) || !
in(b, s).empty();
113 [
b] (transition_t_of<Aut2> t) {
return b->src_of(t) != b->pre(); });
114 const auto&
map =
copy.state_map();
118 for (
auto t1: final_ts)
130 auto s1 = res->src_of(t1);
131 auto w1 = res->weight_of(t1);
132 res->del_transition(t1);
133 for (
auto t2: init_ts)
135 auto w2 = b->weight_of(t2);
136 for (
auto t3:
all_out(b, b->dst_of(t2)))
137 res->set_transition(s1,
138 map.at(b->dst_of(t3)),
139 ls.conv(bls, b->label_of(t3)),
142 ws.conv(bws, b->weight_of(t3))));
149 template <Automaton Aut1, Automaton Aut2,
typename Tag = general_tag>
151 multiply(
const Aut1& lhs,
const Aut2& rhs, Tag tag = {})
152 -> decltype(lhs->null_state(),
166 template <Automaton Lhs, Automaton Rhs,
typename String>
169 const std::string& algo)
171 const auto& l = lhs->as<Lhs>();
172 const auto&
r = rhs->as<Rhs>();
174 (algo ==
"auto" ?
"standard" : algo,
201 template <Automaton Aut,
typename Tag = general_tag>
203 multiply(
const Aut& aut,
int min,
int max, Tag tag = {})
204 -> decltype(aut->null_state(),
212 res =
star(aut, tag);
219 "multiply: invalid exponents: ", min,
", ", max);
223 auto s = res->new_state();
232 for (
int n = 1; n < min; ++n)
240 auto s =
sum->new_state();
244 for (
int n = 1; n <= max - min; ++n)
257 template <Automaton Aut,
typename Int1,
typename Int2,
typename String>
260 const std::string& algo)
262 const auto& aut = a->as<Aut>();
264 [aut, min, max](
auto tag)
279 template <
typename ValueSet>
280 typename ValueSet::value_t
282 const typename ValueSet::value_t& lhs,
283 const typename ValueSet::value_t& rhs)
285 return vs.mul(lhs, rhs);
293 template <
typename ExpSetLhs,
typename ExpSetRhs>
297 auto join_elts = join<ExpSetLhs, ExpSetRhs>(lhs, rhs);
300 std::get<1>(join_elts),
301 std::get<2>(join_elts)));
311 template <
typename ExpSetLhs,
typename ExpSetRhs>
315 auto join_elts = join<ExpSetLhs, ExpSetRhs>(lhs, rhs);
316 auto res = std::get<0>(join_elts).concat(std::get<1>(join_elts),
317 std::get<2>(join_elts));
328 template <
typename ExpSet>
329 typename ExpSet::value_t
333 typename ExpSet::value_t res;
340 res = rs.mul(rs.power(r, min), res);
345 "multiply: invalid exponents: ", min,
", ", max);
346 res = rs.power(r, min);
349 typename ExpSet::value_t
sum = rs.one();
350 for (
int n = 1; n <= max - min; ++n)
351 sum = rs.add(sum, rs.power(r, n));
352 res = rs.mul(res, sum);
363 template <
typename ExpSet,
typename Int1,
typename Int2>
367 const auto&
r = re->as<ExpSet>();
386 template <
typename LabelSetLhs,
typename LabelSetRhs>
390 const auto& l = lhs->as<LabelSetLhs>();
391 const auto&
r = rhs->as<LabelSetRhs>();
392 auto rs =
join(l.labelset(), r.labelset());
393 auto lr = rs.conv(l.labelset(), l.label());
394 auto rr = rs.conv(r.labelset(), r.label());
410 template <
typename PolynomialSetLhs,
typename PolynomialSetRhs>
414 auto join_elts = join<PolynomialSetLhs, PolynomialSetRhs>(lhs, rhs);
417 std::get<1>(join_elts),
418 std::get<2>(join_elts)));
432 template <
typename WeightSetLhs,
typename WeightSetRhs>
436 const auto& l = lhs->as<WeightSetLhs>();
437 const auto&
r = rhs->as<WeightSetRhs>();
438 auto ws =
join(l.weightset(), r.weightset());
439 auto lw = ws.conv(l.weightset(), l.weight());
440 auto rw = ws.conv(r.weightset(), r.weight());
445 template <
typename WeightSet,
typename Int1,
typename Int2>
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Tag for operations on all automata.
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
std::shared_ptr< const detail::polynomial_base > polynomial
expression multiply_expression_repeated(const expression &re, int min, int max)
Bridge (multiply).
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
std::shared_ptr< const detail::weight_base > weight
ExpSet::value_t multiply(const ExpSet &rs, const typename ExpSet::value_t &r, int min, int max)
auto sum(const Aut1 &lhs, const Aut2 &rhs, Tag tag={}) -> decltype(join_automata(lhs, rhs))
The sum of two automata.
polynomial make_polynomial(const PolynomialSet &ps, const typename PolynomialSet::value_t &p)
void copy_into(const AutIn &in, AutOut &out, KeepState keep_state, KeepTrans keep_trans)
Copy selected states and transitions of an automaton.
polynomial multiply_polynomial(const polynomial &lhs, const polynomial &rhs)
Bridge (multiply).
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
label make_label(const LabelSet &ls, const typename LabelSet::value_t &l)
weightset_mixin< detail::r_impl > r
Provide a variadic mul on top of a binary mul(), and one().
weight multiply_weight(const weight &lhs, const weight &rhs)
Bridge (multiply).
expression multiply_expression(const expression &lhs, const expression &rhs)
Bridge (multiply).
auto determinize(const Aut &a, Tag={}, bool_constant< Lazy >={})
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
std::shared_ptr< const detail::label_base > label
auto make_join_automaton(deterministic_tag, Auts &&...auts)
Make an empty automaton which is a supertype of others.
automaton multiply_repeated(const automaton &a, int min, int max, const std::string &algo)
Bridge (multiply).
expression concatenate_expression(const expression &lhs, const expression &rhs)
Bridge (concatenate).
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
std::shared_ptr< detail::automaton_base > automaton
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
auto star(const Aut &aut, Tag tag={}) -> decltype(detail::make_join_automaton(tag, aut))
Star of an automaton.
Tag for operations on deterministic automata.
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
detail::copier< AutIn, AutOut > make_copier(const AutIn &in, AutOut &out, bool safe=true)
Build an automaton copier.
weight multiply_weight_repeated(const weight &wgt, int min, int max)
Bridge (multiply).
std::shared_ptr< detail::expression_base > expression
context join(const context &c1, const context &c2)
Bridge.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Tag for operations on standard automata.
expression make_expression(const ExpSet &rs, const typename ExpSet::value_t &r)
weightset_mixin< detail::b_impl > b
Aut1 & multiply_here(Aut1 &res, const Aut2 &b, deterministic_tag)
Append automaton b to res.
auto dispatch_tags(std::string algo, Operation op, Aut &&...auts)
Dispatch an operation between automata depending on their nature.
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
label multiply_label(const label &lhs, const label &rhs)
Bridge (multiply).
auto multiply(const Aut1 &lhs, const Aut2 &rhs, Tag tag={}) -> decltype(lhs->null_state(), detail::make_join_automaton(tag, lhs, rhs))
Concatenate two automata, general case.
automaton multiply(const automaton &lhs, const automaton &rhs, const std::string &algo)
Bridge.