Vcsn  2.2
Be Rational
transpose.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/core/rat/fwd.hh>
4 
5 namespace vcsn
6 {
7 
8  namespace detail
9  {
10 
11  /*-------------------------.
12  | transpose(expression). |
13  `-------------------------*/
14 
18  template <typename ExpSet>
19  class transposer
20  : public ExpSet::const_visitor
21  {
22  public:
23  using expressionset_t = ExpSet;
24  using expression_t = typename expressionset_t::value_t;
25  using super_t = typename expressionset_t::const_visitor;
26 
28  constexpr static const char* me() { return "transpose"; }
29 
31  : rs_{rs}
32  , res_{}
33  {}
34 
37  {
38  e->accept(*this);
39  return std::move(res_);
40  }
41 
44  {
45  expression_t res;
46  std::swap(res_, res);
47  e->accept(*this);
48  std::swap(res_, res);
49  return res;
50  }
51 
52  private:
54  {
55  res_ = rs_.zero();
56  }
57 
59  {
60  res_ = rs_.one();
61  }
62 
63  VCSN_RAT_VISIT(atom, e)
64  {
65  res_ = rs_.atom(rs_.labelset()->transpose(e.value()));
66  }
67 
69  {
70  res_ = rs_.zero();
71  for (const auto& v: e)
72  res_ = rs_.add(res_, transpose(v));
73  }
74 
75  VCSN_RAT_VISIT(conjunction, e)
76  {
77  res_ = transpose(e.head());
78  for (const auto& v: e.tail())
79  res_ = rs_.conjunction(res_, transpose(v));
80  }
81 
82  VCSN_RAT_VISIT(infiltration, e)
83  {
84  // FIXME: that should be easy to factor.
85  res_= transpose(e.head());
86  for (const auto& v: e.tail())
87  res_ = rs_.infiltration(res_, transpose(v));
88  }
89 
90  VCSN_RAT_VISIT(shuffle, e)
91  {
92  // FIXME: that should be easy to factor.
93  res_= transpose(e.head());
94  for (const auto& v: e.tail())
95  res_ = rs_.shuffle(res_, transpose(v));
96  }
97 
98  VCSN_RAT_VISIT(prod, e)
99  {
100  res_ = rs_.one();
101  for (const auto& v: e)
102  res_ = rs_.mul(transpose(v), res_);
103  }
104 
105  VCSN_RAT_VISIT(star, e)
106  {
107  res_ = rs_.star(transpose(e.sub()));
108  }
109 
110  VCSN_RAT_VISIT(complement, e)
111  {
112  res_ = rs_.complement(transpose(e.sub()));
113  }
114 
115  VCSN_RAT_VISIT(transposition, e)
116  {
117  // Don't stack indefinitly transpositions on top of
118  // transitions. Not only is this useless, it would also break
119  // the involution as r.transpose().transpose() would not be r,
120  // but "r{T}{T}". On the other hand, if "(abc){T}".transpose()
121  // return "abc", we also lose the involution.
122  //
123  // So rather, don't stack more that two transpositions:
124  //
125  // (abc){T}.transpose() => (abc){T}{T}
126  // (abc){T}{T}.transpose() => (abc){T}
127  //
128  // Do the same with ldiv, for the same reasons: involution.
129  //
130  // (E\F).transpose() => (E\F){T}
131  // (E\F){T}.transpose() => (E\F)
132  if (e.sub()->type() == rat::type_t::transposition
133  || e.sub()->type() == rat::type_t::ldiv)
134  res_ = e.sub();
135  else
136  res_ = rs_.transposition(e.shared_from_this());
137  }
138 
139  VCSN_RAT_VISIT(ldiv, e)
140  {
141  // There is nothing we can do here but leaving an explicit
142  // transposition.
143  res_ = rs_.transposition(e.shared_from_this());
144  }
145 
146  VCSN_RAT_VISIT(lweight, e)
147  {
148  res_ = rs_.rmul(transpose(e.sub()),
149  rs_.weightset()->transpose(e.weight()));
150  }
151 
152  VCSN_RAT_VISIT(rweight, e)
153  {
154  res_ = rs_.lmul(rs_.weightset()->transpose(e.weight()),
155  transpose(e.sub()));
156  }
157 
158  using tuple_t = typename super_t::tuple_t;
159  virtual void visit(const tuple_t&, std::true_type) override
160  {
161  raise(me(), ": tuple is not supported");
162  }
163 
164 
165  private:
168  };
169  }
170 
175  template <typename ExpSet>
176  typename ExpSet::value_t
177  transpose(const ExpSet& rs, const typename ExpSet::value_t& v)
178  {
179  return rs.transpose(v);
180  }
181 
182 }
VCSN_RAT_VISIT(complement, e)
Definition: transpose.hh:110
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: transpose.hh:28
expression_t transpose(const expression_t &e)
Definition: transpose.hh:43
VCSN_RAT_VISIT(transposition, e)
Definition: transpose.hh:115
Definition: a-star.hh:8
VCSN_RAT_VISIT(rweight, e)
Definition: transpose.hh:152
expressionset_t rs_
Definition: transpose.hh:166
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:227
typename expressionset_t::value_t expression_t
Definition: transpose.hh:24
auto rs
Definition: lift.hh:151
VCSN_RAT_VISIT(shuffle, e)
Definition: transpose.hh:90
A visitor to create a transposed expression,.
Definition: transpose.hh:19
VCSN_RAT_VISIT(lweight, e)
Definition: transpose.hh:146
virtual void visit(const tuple_t &, std::true_type) override
Definition: transpose.hh:159
transposer(const expressionset_t &rs)
Definition: transpose.hh:30
VCSN_RAT_VISIT(infiltration, e)
Definition: transpose.hh:82
typename super_t::tuple_t tuple_t
Definition: transpose.hh:158
typename expressionset_t::const_visitor super_t
Definition: transpose.hh:25
VCSN_RAT_VISIT(conjunction, e)
Definition: transpose.hh:75
expression_t operator()(const expression_t &e)
Definition: transpose.hh:36