Source

z3 / src / util / inf_rational.h

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    inf_rational.h

Abstract:

    Rational numbers with infenitesimals

Author:

    Leonardo de Moura (leonardo) 2006-09-18.
    Nikolaj Bjorner (nbjorner) 2006-10-24.

Revision History:

--*/
#ifndef _INF_RATIONAL_H_
#define _INF_RATIONAL_H_
#include<stdlib.h>
#include<string>
#include"debug.h"
#include"vector.h"
#include"rational.h"


class inf_rational {
    static inf_rational m_zero;
    static inf_rational m_one;
    static inf_rational m_minus_one;
    rational m_first;
    rational m_second;
 public:

    unsigned hash() const { 
        return m_first.hash() ^ (m_second.hash()+1);
    }

    struct hash_proc {  unsigned operator()(inf_rational const& r) const { return r.hash(); }  };

	struct eq_proc { bool operator()(inf_rational const& r1, inf_rational const& r2) const { return r1 == r2; } };

    void swap(inf_rational & n) { 
        m_first.swap(n.m_first);
        m_second.swap(n.m_second);
    }
    std::string to_string() const {
        if (m_second.is_zero()) {
            return m_first.to_string();
        }
        std::string s = "(";
        s += m_first.to_string();
        if (m_second.is_neg()) {
            s += " -e*";
        }
        else {
            s += " +e*";
        }
        s += abs(m_second).to_string();
        s += ")";
        return s;
    }

    inf_rational():
        m_first(rational()),
        m_second(rational())
     {}

    inf_rational(const inf_rational & r): 
        m_first(r.m_first),
        m_second(r.m_second)
     {}

    explicit inf_rational(int n):
        m_first(rational(n)),
        m_second(rational())
    {}

    explicit inf_rational(int n, int d):
        m_first(rational(n,d)),
        m_second(rational())
    {}

    explicit inf_rational(rational const& r, bool pos_inf):
        m_first(r),
        m_second(pos_inf?rational(1):rational(-1))
    {}

    explicit inf_rational(rational const& r):
        m_first(r)
    {
        m_second.reset();
    }

    inf_rational(rational const& r, rational const& i):
        m_first(r),
        m_second(i) {
    }

    ~inf_rational() {}

    /**
       \brief Set inf_rational to 0.
    */
    void reset() {
        m_first.reset();
        m_second.reset();
    }

    bool is_int() const { 
        return m_first.is_int() && m_second.is_zero();
    }

    bool is_int64() const { 
        return m_first.is_int64() && m_second.is_zero();
    }

    bool is_uint64() const { 
        return m_first.is_uint64() && m_second.is_zero();
    }

    bool is_rational() const { return m_second.is_zero(); }

    int64 get_int64() const {
	SASSERT(is_int64());
        return m_first.get_int64();
    }

    uint64 get_uint64() const {
	SASSERT(is_uint64());
        return m_first.get_uint64();
    }

    rational const& get_rational() const {
        return m_first;
    }

    rational const& get_infinitesimal() const {
        return m_second;
    }

    rational const & get_first() const { return m_first; }

    inf_rational & operator=(const inf_rational & r) {
        m_first = r.m_first;
        m_second = r.m_second;
	return *this;
    }

    inf_rational & operator=(const rational & r) {
        m_first = r;
        m_second.reset();
        return *this;
    }

    friend inline inf_rational numerator(const inf_rational & r) {
        SASSERT(r.m_second.is_zero());
        return inf_rational(numerator(r.m_first));
    }

    friend inline inf_rational denominator(const inf_rational & r) {
        SASSERT(r.m_second.is_zero());
        return inf_rational(denominator(r.m_first));
    }

    inf_rational & operator+=(const inf_rational & r) { 
        m_first  += r.m_first;
        m_second += r.m_second;
	return *this; 
    }

    inf_rational & operator-=(const inf_rational & r) { 
        m_first  -= r.m_first;
        m_second -= r.m_second;
	return *this; 
    }

    inf_rational & operator+=(const rational & r) { 
        m_first  += r;
	return *this; 
    }

    inf_rational & operator-=(const rational & r) { 
        m_first  -= r;
	return *this; 
    }

    inf_rational & operator*=(const rational & r1) {
        m_first  *= r1;
        m_second *= r1;
        return *this;
    }

    //
    // These operations get us out of the realm of inf_rational:
    // (r1 + e*k1)*(r2 + e*k2) = (r1*r2 + (r1*k2 + r2*k1)*e)
    //
    // inf_rational & operator*=(const inf_rational & r)
    // inf_rational & operator/=(const inf_rational & r)
    // inf_rational & operator%=(const inf_rational & r)
    // friend inline inf_rational div(const inf_rational & r1, const inf_rational & r2)
    // inf_rational expt(int n)
    // instead, we define operators that approximate some of these operations from above and below.
    
    friend inf_rational inf_mult(inf_rational const& r1, inf_rational const& r2);
    friend inf_rational sup_mult(inf_rational const& r1, inf_rational const& r2);

    friend inf_rational inf_div(inf_rational const& r1, inf_rational const& r2);
    friend inf_rational sup_div(inf_rational const& r1, inf_rational const& r2);

    friend inf_rational inf_power(inf_rational const& r1, unsigned n);
    friend inf_rational sup_power(inf_rational const& r1, unsigned n);

    friend inf_rational inf_root(inf_rational const& r1, unsigned n);
    friend inf_rational sup_root(inf_rational const& r1, unsigned n);

    inf_rational & operator/=(const rational & r) {
        m_first /= r;
        m_second /= r;
        return *this;
    }

    friend inline inf_rational operator*(const rational & r1, const inf_rational & r2);
    friend inline inf_rational operator/(const inf_rational & r1, const rational & r2);

    inf_rational & operator++() {
        ++m_first;
        return *this;
    }

    const inf_rational operator++(int) { inf_rational tmp(*this); ++(*this); return tmp; }
  
    inf_rational & operator--() {
        --m_first;
        return *this;
    }

    const inf_rational operator--(int) { inf_rational tmp(*this); --(*this); return tmp; }

    friend inline bool operator==(const inf_rational & r1, const inf_rational & r2) {
        return r1.m_first == r2.m_first && r1.m_second == r2.m_second;
    }

    friend inline bool operator==(const rational & r1, const inf_rational & r2) {
        return r1 == r2.m_first && r2.m_second.is_zero();
    }

    friend inline bool operator==(const inf_rational & r1, const rational & r2) {
        return r1.m_first == r2 && r1.m_second.is_zero();
    }

    friend inline bool operator<(const inf_rational & r1, const inf_rational & r2) { 
        return 
            (r1.m_first < r2.m_first) ||
            (r1.m_first == r2.m_first && r1.m_second < r2.m_second);
    }

    friend inline bool operator<(const rational & r1, const inf_rational & r2) { 
        return 
            (r1 < r2.m_first) ||
            (r1 == r2.m_first && r2.m_second.is_pos());
    }

    friend inline bool operator<(const inf_rational & r1, const rational & r2) { 
        return 
            (r1.m_first < r2) ||
            (r1.m_first == r2 && r1.m_second.is_neg());
    }

    void neg() {
        m_first.neg();
        m_second.neg();
    }

    bool is_zero() const {
        return m_first.is_zero() && m_second.is_zero();
    }

    bool is_one() const {
        return m_first.is_one() && m_second.is_zero();
    }

    bool is_minus_one() const {
        return m_first.is_minus_one() && m_second.is_zero();
    }

    bool is_neg() const {
        return 
            m_first.is_neg() || 
            (m_first.is_zero() && m_second.is_neg());
    }
    
    bool is_pos() const {
        return 
            m_first.is_pos() || 
            (m_first.is_zero() && m_second.is_pos());
    }

    bool is_nonneg() const {
        return 
            m_first.is_pos() ||
            (m_first.is_zero() && m_second.is_nonneg());
    }

    bool is_nonpos() const {
        return 
            m_first.is_neg() ||
            (m_first.is_zero() && m_second.is_nonpos());
    }

    friend inline rational floor(const inf_rational & r) {
        if (r.m_first.is_int()) {
            if (r.m_second.is_nonneg()) {
                return r.m_first;
            }
            return r.m_first - rational(1);
        }
        
        return floor(r.m_first);
    }

    friend inline rational ceil(const inf_rational & r) {
        if (r.m_first.is_int()) {
            if (r.m_second.is_nonpos()) {
                return r.m_first;
            }
            return r.m_first + rational(1);
        }
        
        return ceil(r.m_first);
    }

    static const inf_rational & zero() {
        return m_zero;
    }

    static const inf_rational & one() {
        return m_one;
    }

    static const inf_rational & minus_one() {
        return m_minus_one;
    }

    // Perform: this += c * k
    void addmul(const rational & c, const inf_rational & k) {
        m_first.addmul(c, k.m_first);
        m_second.addmul(c, k.m_second);
    }

    // Perform: this += c * k
    void submul(const rational & c, const inf_rational & k) {
        m_first.submul(c, k.m_first);
        m_second.submul(c, k.m_second);
    }
};

inline bool operator!=(const inf_rational & r1, const inf_rational & r2) { 
    return !operator==(r1, r2); 
}

inline bool operator!=(const rational & r1, const inf_rational & r2) { 
    return !operator==(r1, r2); 
}

inline bool operator!=(const inf_rational & r1, const rational & r2) { 
    return !operator==(r1, r2); 
}

inline bool operator>(const inf_rational & r1, const inf_rational & r2) { 
    return operator<(r2, r1); 
}

inline bool operator>(const inf_rational & r1, const rational & r2) { 
    return operator<(r2, r1); 
}

inline bool operator>(const rational & r1, const inf_rational & r2) { 
    return operator<(r2, r1); 
}

inline bool operator<=(const inf_rational & r1, const inf_rational & r2) { 
    return !operator>(r1, r2); 
}

inline bool operator<=(const rational & r1, const inf_rational & r2) { 
    return !operator>(r1, r2); 
}

inline bool operator<=(const inf_rational & r1, const rational & r2) { 
    return !operator>(r1, r2); 
}

inline bool operator>=(const inf_rational & r1, const inf_rational & r2) { 
    return !operator<(r1, r2); 
}

inline bool operator>=(const rational & r1, const inf_rational & r2) { 
    return !operator<(r1, r2); 
}

inline bool operator>=(const inf_rational & r1, const rational & r2) { 
    return !operator<(r1, r2); 
}

inline inf_rational operator+(const inf_rational & r1, const inf_rational & r2) { 
    return inf_rational(r1) += r2; 
}

inline inf_rational operator-(const inf_rational & r1, const inf_rational & r2) { 
    return inf_rational(r1) -= r2; 
}

inline inf_rational operator-(const inf_rational & r) { 
    inf_rational result(r); 
    result.neg(); 
    return result; 
}

inline inf_rational operator*(const rational & r1, const inf_rational & r2) {
    inf_rational result(r2);
    result.m_first  *= r1;
    result.m_second *= r1;
    return result;
}

inline inf_rational operator/(const inf_rational & r1, const rational & r2) {
    inf_rational result(r1);
    result.m_first  /= r2;
    result.m_second /= r2;
    return result;
}

#if 0
inf_rational inf_mult(inf_rational const& r1, inf_rational const& r2);
inf_rational sup_mult(inf_rational const& r1, inf_rational const& r2);

inf_rational inf_div(inf_rational const& r1, inf_rational const& r2);
inf_rational sup_div(inf_rational const& r1, inf_rational const& r2);

inf_rational inf_power(inf_rational const& r1, unsigned n);
inf_rational sup_power(inf_rational const& r1, unsigned n);

inf_rational inf_root(inf_rational const& r1, unsigned n);
inf_rational sup_root(inf_rational const& r1, unsigned n);
#endif
//
// inline inf_rational operator/(const inf_rational & r1, const inf_rational & r2)
// inline inf_rational operator%(const inf_rational & r1, const inf_rational & r2)
// inf_rational gcd(const inf_rational & r1, const inf_rational & r2);
// inf_rational lcm(const inf_rational & r1, const inf_rational & r2);

inline std::ostream & operator<<(std::ostream & target, const inf_rational & r)
{
    target << r.to_string();
    return target;
}


inline inf_rational abs(const inf_rational & r) {
    inf_rational result(r);
    if (result.is_neg()) {
        result.neg();
    }
    return result;
}

#endif /* _INF_RATIONAL_H_ */