Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
transpose.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_RAT_TRANSPOSE_HH
2 # define VCSN_CORE_RAT_TRANSPOSE_HH
3 
4 # include <vcsn/core/rat/fwd.hh>
5 
6 namespace vcsn
7 {
8 
9  namespace detail
10  {
11 
12  /*--------------------.
13  | transpose(ratexp). |
14  `--------------------*/
15 
17  template <class RatExpSet>
18  class transposer
19  : public RatExpSet::const_visitor
20  {
21  public:
22  using ratexpset_t = RatExpSet;
23  using ratexp_t = typename ratexpset_t::value_t;
24  using super_t = typename ratexpset_t::const_visitor;
25 
27  : rs_{rs}
28  , res_{}
29  {}
30 
31  ratexp_t
32  operator()(const ratexp_t& e)
33  {
34  e->accept(*this);
35  return std::move(res_);
36  }
37 
38  ratexp_t
39  transpose(const ratexp_t& e)
40  {
41  ratexp_t res;
42  std::swap(res_, res);
43  e->accept(*this);
44  std::swap(res_, res);
45  return res;
46  }
47 
49  {
50  res_ = rs_.zero();
51  }
52 
54  {
55  res_ = rs_.one();
56  }
57 
58  VCSN_RAT_VISIT(atom, e)
59  {
60  res_ = rs_.atom(rs_.labelset()->transpose(e.value()));
61  }
62 
64  {
65  res_ = rs_.zero();
66  for (auto v: e)
67  res_ = rs_.add(res_, transpose(v));
68  }
69 
70  VCSN_RAT_VISIT(conjunction, e)
71  {
72  res_ = transpose(e.head());
73  for (auto v: e.tail())
74  res_ = rs_.conjunction(res_, transpose(v));
75  }
76 
77  VCSN_RAT_VISIT(shuffle, e)
78  {
79  // FIXME: that should be easy to factor.
80  res_= transpose(e.head());
81  for (auto v: e.tail())
82  res_ = rs_.shuffle(res_, transpose(v));
83  }
84 
85  VCSN_RAT_VISIT(prod, e)
86  {
87  res_ = rs_.one();
88  for (auto v: e)
89  res_ = rs_.mul(transpose(v), res_);
90  }
91 
92  VCSN_RAT_VISIT(star, e)
93  {
94  res_ = rs_.star(transpose(e.sub()));
95  }
96 
97  VCSN_RAT_VISIT(complement, e)
98  {
99  res_ = rs_.complement(transpose(e.sub()));
100  }
101 
102  VCSN_RAT_VISIT(transposition, e)
103  {
104  // Don't stack indefinitly transpositions on top of
105  // transitions. Not only is this useless, it would also break
106  // the involution as r.transpose().transpose() would not be r,
107  // but "r{T}{T}". On the other hand, if "(abc){T}".tranpose()
108  // return "abc", we also lose the involution.
109  //
110  // So rather, don't stack more that two transpositions:
111  //
112  // (abc){T}.transpose() => (abc){T}{T}
113  // (abc){T}{T}.transpose() => (abc){T}
114  //
115  // Do the same with ldiv, for the same reasons: involution.
116  //
117  // (E\F).transpose() => (E\F){T}
118  // (E\F){T}.transpose() => (E\F)
119  if (e.sub()->type() == rat::type_t::transposition
120  || e.sub()->type() == rat::type_t::ldiv)
121  res_ = e.sub();
122  else
123  res_ = rs_.transposition(e.shared_from_this());
124  }
125 
126  VCSN_RAT_VISIT(ldiv, e)
127  {
128  // There is nothing we can do here but leaving an explicit
129  // transposition.
130  res_ = rs_.transposition(e.shared_from_this());
131  }
132 
133  VCSN_RAT_VISIT(lweight, e)
134  {
135  res_ = rs_.rmul(transpose(e.sub()),
136  rs_.weightset()->transpose(e.weight()));
137  }
138 
139  VCSN_RAT_VISIT(rweight, e)
140  {
141  res_ = rs_.lmul(rs_.weightset()->transpose(e.weight()),
142  transpose(e.sub()));
143  }
144 
145 
146  private:
149  };
150  }
151 
152  template <class RatExpSet>
153  typename RatExpSet::value_t
154  transpose(const RatExpSet& rs, const typename RatExpSet::value_t& v)
155  {
156  return rs.transpose(v);
157  }
158 
159 }
160 
161 #endif // !VCSN_CORE_RAT_TRANSPOSE_HH
VCSN_RAT_VISIT(rweight, e)
Definition: transpose.hh:139
VCSN_RAT_VISIT(complement, e)
Definition: transpose.hh:97
transposer(const ratexpset_t &rs)
Definition: transpose.hh:26
typename ratexpset_t::const_visitor super_t
Definition: transpose.hh:24
typename ratexpset_t::value_t ratexp_t
Definition: transpose.hh:23
VCSN_RAT_VISIT(conjunction, e)
Definition: transpose.hh:70
VCSN_RAT_VISIT(shuffle, e)
Definition: transpose.hh:77
ratexp_t operator()(const ratexp_t &e)
Definition: transpose.hh:32
VCSN_RAT_VISIT(lweight, e)
Definition: transpose.hh:133
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:230
ratexp_t transpose(const ratexp_t &e)
Definition: transpose.hh:39
VCSN_RAT_VISIT(transposition, e)
Definition: transpose.hh:102