17 #ifndef AWALI_WEIGHTSET_C_HH
18 #define AWALI_WEIGHTSET_C_HH
53 std::string
vname(
bool =
true)
const
109 if (std::norm(v) < 1)
113 raise(
sname(),
": star: invalid value: ",
format(*
this, v));
123 return v.real() == 0 && v.imag() == 0;
129 return v.real() == 1 && v.imag() == 0;
140 if(std::norm(lhs) == std::norm(rhs))
141 return std::arg(lhs) < std::arg(rhs);
142 return std::norm(lhs) < std::norm(rhs);
185 return {double(v.
num) / double(v.
den)};
207 std::ostringstream os;
209 if (i.peek()==
'+' || i.peek()==
'-')
211 while((maybe = i.peek()) ==
'.' || (maybe >=
'0' && maybe <=
'9')) {
215 if(os.str().empty()) {
220 throw std::runtime_error(
sname() +
" : invalid real");
223 std::istringstream is(os.str());
235 if ((maybe = i.peek()) ==
'i') {
240 if ((maybe = i.peek()) ==
'i') {
244 if ((maybe = i.peek()) ==
'+' || maybe ==
'-'){
275 parse(
const std::string & s,
size_t& p) {
282 const std::string&
format =
"text")
286 if(v.real()!=0 || v.imag()==0) {
287 o <<
"\"re\":" << v.real();
289 o<<
", \"im\":" << v.imag();
292 o<<
"\"im\":" << v.imag();
302 double im = v.imag();
310 if(v.real()==0 && v.imag()==0)
320 else if (
format ==
"text")
323 raise(
"invalid format: ",
format);
327 template<
unsigned version = version::fsm_json>
331 version::check_fsmjson<version>();
341 template<
unsigned version = version::fsm_json>
346 version::check_fsmjson<version>();
354 else if (v.real() == 0.)
364 template<
unsigned version = version::fsm_json>
369 version::check_fsmjson<version>();
378 std::vector<json::node_t*>
const& v = p->
array()->
values;
383 a[i]=v[i]->to_double();
403 size_t pos = str.length();
413 +
" node to a complex number.",
420 inline c join(
const c&,
const c&) {
return {}; }
422 inline c join(
const r&,
const c&) {
return {}; }
423 inline c join(
const c&,
const r&) {
return {}; }
425 inline c join(
const q&,
const c&) {
return {}; }
426 inline c join(
const c&,
const q&) {
return {}; }
428 inline c join(
const z&,
const c&) {
return {}; }
429 inline c join(
const c&,
const z&) {
return {}; }
431 inline c join(
const n&,
const c&) {
return {}; }
432 inline c join(
const c&,
const n&) {
return {}; }
434 inline c join(
const b&,
const c&) {
return {}; }
435 inline c join(
const c&,
const b&) {
return {}; }
std::vector< node_t * > const & values
Definition: node.hh:436
Exception used when trying to coerce a node to a given type.
Definition: node.hh:143
virtual node_t * at(std::string const &key)
virtual double to_double() const
Coerces this node_t to a double
Definition: node.hh:343
virtual string_t const * string() const
Casts this node to string_t.
Definition: node.hh:215
virtual bool has_child(std::string const &) const
Definition: node.hh:284
node_kind_t const kind
Definition: node.hh:196
virtual unsigned arity() const
Definition: node.hh:355
virtual array_t const * array() const
Casts this node to array_t.
Definition: node.hh:207
object_t * push_back(std::string key, node_t *node)
std::string value
Definition: node.hh:531
Definition: qfraction.hh:26
den_t den
Definition: qfraction.hh:32
num_t num
Definition: qfraction.hh:31
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of complex numbers.
Definition: c.hh:44
static value_t zero()
Definition: c.hh:68
static c make(std::istream &is)
Build from the description in is.
Definition: c.hh:59
static bool is_special(const value_t)
Definition: c.hh:116
static bool equals(const value_t l, const value_t r)
Definition: c.hh:132
static value_t conv(q, q::value_t v)
Definition: c.hh:183
std::string vname(bool=true) const
Definition: c.hh:53
json::node_t * to_json() const
Definition: c.hh:329
static double greedy(std::istream &i)
Definition: c.hh:206
value_t value_from_json(json::node_t const *p) const
Definition: c.hh:366
static value_t abs(const value_t v)
Definition: c.hh:151
std::complex< double > value_t
Definition: c.hh:65
json::node_t * value_to_json(value_t v) const
Definition: c.hh:343
static value_t conv(b, b::value_t v)
Definition: c.hh:201
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: c.hh:138
static value_t conv(self_type, value_t v)
Definition: c.hh:171
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: c.hh:281
static value_t transpose(const value_t v)
Definition: c.hh:157
static constexpr bool show_one()
Definition: c.hh:147
static value_t conv(std::istream &i)
Definition: c.hh:231
static value_t parse(const std::string &s, size_t &p)
Definition: c.hh:275
static value_t one()
Definition: c.hh:73
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: c.hh:316
static size_t hash(value_t v)
Definition: c.hh:162
static bool is_one(const value_t v)
Definition: c.hh:126
value_t star(const value_t v) const
Definition: c.hh:106
static std::string sname()
Definition: c.hh:48
static value_t ldiv(const value_t l, const value_t r)
Definition: c.hh:101
static bool is_zero(const value_t v)
Definition: c.hh:121
static constexpr bool is_commutative_semiring()
Definition: c.hh:145
static value_t sub(const value_t l, const value_t r)
Definition: c.hh:83
static value_t mul(const value_t l, const value_t r)
Definition: c.hh:88
static value_t conv(n, n::value_t v)
Definition: c.hh:195
static value_t rdiv(const value_t l, const value_t r)
Definition: c.hh:94
static value_t conv(r, r::value_t v)
Definition: c.hh:177
static value_t add(const value_t l, const value_t r)
Definition: c.hh:78
static constexpr star_status_t star_status()
Definition: c.hh:148
static value_t conv(z, z::value_t v)
Definition: c.hh:189
The semiring of Natural numbers.
Definition: n.hh:34
unsigned int value_t
Definition: n.hh:55
The semiring of rational numbers.
Definition: q.hh:42
The semiring of floating Numbers.
Definition: r.hh:35
double value_t
Definition: r.hh:56
The semiring of Integers.
Definition: z.hh:35
int value_t
Definition: z.hh:56
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ ABSVAL
Definition: enums.hh:168
std::string const & string_of(node_kind_t kind)
@ OBJECT
Definition: node.hh:93
@ ARRAY
Definition: node.hh:94
@ STRING
Definition: node.hh:97
std::complex< double > lr_parse_complex(std::string const &s, size_t &p, bool allow_empty=false, std::complex< double > value_if_empty={0, 0})
Reads a complex number left of position p in string, by using as many characters as possible.
Definition: lr_parse_number.hh:458
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:445
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
std::size_t hash_value(const T &v)
Definition: hash.hh:76
Main namespace of Awali.
Definition: ato.hh:22