Vcsn  2.1
Be Rational
qmp.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <ostream>
5 
6 #include <cstddef> // https://gcc.gnu.org/gcc-4.9/porting_to.html
7 #include <gmpxx.h>
8 
9 #include <vcsn/core/join.hh>
10 #include <vcsn/misc/format.hh>
11 #include <vcsn/misc/functional.hh>
12 #include <vcsn/misc/raise.hh>
13 #include <vcsn/misc/star-status.hh>
14 #include <vcsn/misc/stream.hh> // eat
15 #include <vcsn/weightset/b.hh>
16 #include <vcsn/weightset/fwd.hh>
17 #include <vcsn/weightset/q.hh>
19 #include <vcsn/weightset/z.hh>
20 
21 namespace vcsn
22 {
23  namespace detail
24  {
25  class qmp_impl
26  {
27  public:
28  using self_t = qmp;
29 
30  static symbol sname()
31  {
32  static symbol res("qmp");
33  return res;
34  }
35 
37  static qmp make(std::istream& is)
38  {
39  eat(is, sname());
40  return {};
41  }
42 
43  using value_t = mpq_class;
44 
45  static value_t zero()
46  {
47  // Not value_t{0, 1} to avoid the (const char* s, int base)
48  // constructor.
49  return value_t{mpz_class(0), 1};
50  }
51 
52  static value_t one()
53  {
54  return value_t{1, 1};
55  }
56 
57  static value_t add(const value_t l, const value_t r)
58  {
59  return l + r;
60  }
61 
62  static value_t sub(const value_t l, const value_t r)
63  {
64  return l - r;
65  }
66 
67  static value_t mul(const value_t l, const value_t r)
68  {
69  return l * r;
70  }
71 
72  static value_t
73  rdiv(const value_t l, const value_t r)
74  {
75  require(!is_zero(r), "div: division by zero");
76  return l / r;
77  }
78 
79  static value_t
80  ldiv(const value_t l, const value_t r)
81  {
82  return rdiv(r, l);
83  }
84 
85  value_t star(const value_t v) const
86  {
87  if (abs(v.get_num()) < v.get_den())
88  // No need to reduce: numerator and denominators are primes.
89  return {v.get_den(), v.get_den() - v.get_num()};
90  else
91  raise(sname(), ": star: invalid value: ", to_string(*this, v));
92  }
93 
94  static bool is_special(const value_t) // C++11: cannot be constexpr.
95  {
96  return false;
97  }
98 
99  static bool is_zero(const value_t v)
100  {
101  return v.get_num() == 0;
102  }
103 
104  static bool is_one(const value_t v)
105  {
106  // All values are normalized.
107  return v.get_num() == 1 && v.get_den() == 1;
108  }
109 
110  static bool equal(const value_t l, const value_t r)
111  {
112  return l == r;
113  }
114 
116  static bool less(value_t l, value_t r)
117  {
118  return l < r;
119  }
120 
121  static constexpr bool is_commutative() { return true; }
122 
123  static constexpr bool show_one() { return false; }
124  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
125 
126  static value_t
127  abs(const value_t v)
128  {
129  return ::abs(v);
130  }
131 
132  static value_t
134  {
135  return v;
136  }
137 
138  static size_t hash(value_t v)
139  {
140  // FIXME: be serious...
141  return hash_value(to_string(qmp_impl(), v));
142  }
143 
144  static value_t
146  {
147  return v;
148  }
149 
150  static value_t
152  {
153  return {v, 1};
154  }
155 
156  static value_t
158  {
159  return {v, 1};
160  }
161 
162  static value_t
163  conv(std::istream& i, bool = true)
164  {
165  value_t res;
166  i >> res;
167  return res;
168  }
169 
170  static std::ostream&
171  print(const value_t v, std::ostream& o,
172  format fmt = {})
173  {
174  if (fmt == format::latex)
175  {
176  if (v.get_den() == 1)
177  o << v.get_num();
178  else
179  o << "\\frac{" << v.get_num() << "}{" << v.get_den() << '}';
180  }
181  else
182  o << v;
183  return o;
184  }
185 
186  std::ostream&
187  print_set(std::ostream& o, format fmt = {}) const
188  {
189  if (fmt == format::latex)
190  o << "\\mathbb{Q}_{\\text{mp}}";
191  else if (fmt == format::text)
192  o << sname();
193  else
194  raise("invalid format: ", fmt);
195  return o;
196  }
197  };
198 
199  /*-------.
200  | join. |
201  `-------*/
202 
207  }
208 }
static bool is_special(const value_t)
Definition: qmp.hh:94
static value_t abs(const value_t v)
Definition: qmp.hh:127
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: qmp.hh:187
static value_t rdiv(const value_t l, const value_t r)
Definition: qmp.hh:73
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static value_t transpose(const value_t v)
Definition: qmp.hh:133
static value_t conv(z, z::value_t v)
Definition: qmp.hh:151
static std::ostream & print(const value_t v, std::ostream &o, format fmt={})
Definition: qmp.hh:171
static value_t ldiv(const value_t l, const value_t r)
Definition: qmp.hh:80
mpq_class value_t
Definition: qmp.hh:43
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static value_t add(const value_t l, const value_t r)
Definition: qmp.hh:57
static value_t one()
Definition: qmp.hh:52
value_t star(const value_t v) const
Definition: qmp.hh:85
static constexpr bool show_one()
Definition: qmp.hh:123
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
static bool is_one(const value_t v)
Definition: qmp.hh:104
An input/output format.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static constexpr bool is_commutative()
Definition: qmp.hh:121
static value_t conv(self_t, value_t v)
Definition: qmp.hh:145
weightset_mixin< detail::q_impl > q
Definition: fwd.hh:52
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
static bool equal(const value_t l, const value_t r)
Definition: qmp.hh:110
static size_t hash(value_t v)
Definition: qmp.hh:138
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
static value_t zero()
Definition: qmp.hh:45
valid iff proper succeeds on the "absolute value" of the automaton
Definition: star-status.hh:9
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static qmp make(std::istream &is)
Build from the description in is.
Definition: qmp.hh:37
star_status_t
Definition: star-status.hh:5
static value_t conv(std::istream &i, bool=true)
Definition: qmp.hh:163
weightset_mixin< detail::qmp_impl > qmp
Definition: fwd.hh:53
static constexpr star_status_t star_status()
Definition: qmp.hh:124
static value_t conv(b, b::value_t v)
Definition: qmp.hh:157
VCSN_JOIN_SIMPLE(b, b)
static value_t mul(const value_t l, const value_t r)
Definition: qmp.hh:67
static symbol sname()
Definition: qmp.hh:30
static value_t sub(const value_t l, const value_t r)
Definition: qmp.hh:62
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56
static bool less(value_t l, value_t r)
Whether < r.
Definition: qmp.hh:116
static bool is_zero(const value_t v)
Definition: qmp.hh:99