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