mpir 0.4.5

partial Rust porting of mpir multiple precision library based on gmp mpfr
Documentation
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! mpq
//!

use std::fmt;
use std::error::Error;
use std::mem::MaybeUninit;

use crate::prim::{*, typ::*, mpz::*, mpf::*, gmp::*}; // randstate::*

/// __mpq_struct
// not use #[derive(Clone)]
#[repr(C)]
pub struct __mpq_struct {
  /// _mp_num
  pub _mp_num: __mpz_struct,
  /// _mp_den
  pub _mp_den: __mpz_struct
}

/// impl SNew
impl SNew for __mpq_struct {
  /// new
  /// This is acrobatic way, new() MUST be called with mpq_s::init*()
  #[inline]
  fn new() -> Self {
/*
    __mpq_struct {
      _mp_num: mpz_s::new(), // init_set_ui(0),
      _mp_den: mpz_s::new() // init_set_ui(1)
    }
*/
unsafe {
    let num = MaybeUninit::<mpz_s>::uninit();
    let den = MaybeUninit::<mpz_s>::uninit();
    __mpq_struct {
      _mp_num: num.assume_init(),
      _mp_den: den.assume_init()
    }
}
  }
}

/// impl Drop
impl Drop for __mpq_struct {
  fn drop(&mut self) {
    // needless to call self.clear() when auto called clear for mpz_s members
/*
    self._mp_num = mpz_s::init(); // must set _mp_num before call self.clear()
    self._mp_den = mpz_s::init(); // must set _mp_den before call self.clear()
    self.clear() // duplex (already called clear for mpz_s members)
*/
  }
}

/// impl AsPtr
impl AsPtr for __mpq_struct {}

/// impl AsPtrMut
impl AsPtrMut for __mpq_struct {}

/// impl mpq_s
impl __mpq_struct {
  /// clear
  #[inline]
  pub fn clear(&mut self) -> () {
    mpq_clear(self)
  }

  /// init create new instance
  #[inline]
  pub fn init() -> Self {
    let mut t = mpq_s::new();
    mpq_init(&mut t);
    t
  }

  /// set self = q
  #[inline]
  pub fn set(&mut self, q: mpq_r) -> &mut Self {
    mpq_set(self, q);
    self
  }

  /// set_ui self = u/f
  #[inline]
  pub fn set_ui(&mut self, (u, f): (ui_t, ui_t)) -> &mut Self {
    mpq_set_ui(self, u, f);
    self
  }

  /// set_si self = s/f
  #[inline]
  pub fn set_si(&mut self, (s, f): (si_t, ui_t)) -> &mut Self {
    mpq_set_si(self, s, f);
    self
  }

  /// set_d self = d
  #[inline]
  pub fn set_d(&mut self, d: double_t) -> &mut Self {
    mpq_set_d(self, d);
    self
  }

  /// set_z self = a
  #[inline]
  pub fn set_z(&mut self, a: mpz_r) -> &mut Self {
    mpq_set_z(self, a);
    self
  }

  /// set_f self = f
  #[inline]
  pub fn set_f(&mut self, f: mpf_r) -> &mut Self {
    mpq_set_f(self, f);
    self
  }

  /// set_num
  #[inline]
  pub fn set_num(&mut self, num: mpz_r) -> &mut Self {
    mpq_set_num(self, num);
    self
  }

  /// set_den
  #[inline]
  pub fn set_den(&mut self, den: mpz_r) -> &mut Self {
    mpq_set_den(self, den);
    self
  }

  /// set_str self from str
  #[inline]
  pub fn set_str(&mut self, s: &str, b: int_t) -> &mut Self {
    mpq_set_str(self, s, b);
    self
  }

  /// fmtstr
  #[inline]
  pub fn fmtstr(&self, b: int_t) -> String {
    mpq_get_str(None, b, self).expect("mpq fmtstr")
  }

  /// get_d (loss of digits)
  #[inline]
  pub fn get_d(&self) -> double_t {
    mpq_get_d(self)
  }

  /// get_num create new instance mpz_s (direct use of numref is recommended)
  #[inline]
  pub fn get_num(&self) -> mpz_s {
/*
    let mut t = mpz_s::init();
    mpq_get_num(&mut t, self);
    t
*/
    mpz_s::init_set(self.numref())
  }

  /// get_den create new instance mpz_s (direct use of denref is recommended)
  #[inline]
  pub fn get_den(&self) -> mpz_s {
/*
    let mut t = mpz_s::init();
    mpq_get_den(&mut t, self);
    t
*/
    mpz_s::init_set(self.denref())
  }

  /// numref
  #[inline]
  pub fn numref(&self) -> mpz_r {
    mpq_numref(self)
  }

  /// denref
  #[inline]
  pub fn denref(&self) -> mpz_r {
    mpq_denref(self)
  }

  /// swap
  #[inline]
  pub fn swap(&mut self, r: mpq_t) -> &mut Self {
    mpq_swap(self, r);
    self
  }

  /// cmp
  #[inline]
  pub fn cmp(&self, r: mpq_r) -> int_t {
    mpq_cmp(self, r)
  }

  /// cmp_z
  #[inline]
  pub fn cmp_z(&self, a: mpz_r) -> int_t {
    mpq_cmp_z(self, a)
  }

  /// cmp_ui
  #[inline]
  pub fn cmp_ui(&self, (u, f): (ui_t, ui_t)) -> int_t {
    mpq_cmp_ui(self, u, f)
  }

  /// cmp_si
  #[inline]
  pub fn cmp_si(&self, (s, f): (si_t, ui_t)) -> int_t {
    mpq_cmp_si(self, s, f)
  }

  /// equal ***should use cmp == 0***
  #[inline]
  pub fn equal(&self, r: mpq_r) -> bool {
    mpq_equal(self, r)
  }

  /// sgn
  #[inline]
  pub fn sgn(&self) -> int_t {
    mpq_sgn(self)
  }

  /// inv q**-1 create new instance
  #[inline]
  pub fn inv(&self) -> Self {
    let mut t = mpq_s::init();
    mpq_inv(&mut t, self);
    t
  }

  /// abs create new instance
  #[inline]
  pub fn abs(&self) -> Self {
    let mut t = mpq_s::init();
    mpq_abs(&mut t, self);
    t
  }

  /// neg create new instance
  #[inline]
  pub fn neg(&self) -> Self {
    let mut t = mpq_s::init();
    mpq_neg(&mut t, self);
    t
  }

  /// sub self -= r
  #[inline]
  pub fn sub(&mut self, r: mpq_r) -> &mut Self {
    mpq_sub(self, &mpq_s::from(&*self), r); // cast &mut self to &self
    self
  }

  /// add self += r
  #[inline]
  pub fn add(&mut self, r: mpq_r) -> &mut Self {
    mpq_add(self, &mpq_s::from(&*self), r); // cast &mut self to &self
    self
  }

  /// mul self *= r
  #[inline]
  pub fn mul(&mut self, r: mpq_r) -> &mut Self {
    mpq_mul(self, &mpq_s::from(&*self), r); // cast &mut self to &self
    self
  }

  /// mul_2exp self *= 2**n
  #[inline]
  pub fn mul_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
    mpq_mul_2exp(self, &mpq_s::from(&*self), n); // cast &mut self to &self
    self
  }

  /// div self /= r
  #[inline]
  pub fn div(&mut self, r: mpq_r) -> &mut Self {
    mpq_div(self, &mpq_s::from(&*self), r); // cast &mut self to &self
    self
  }

  /// div_2exp self /= 2**n
  #[inline]
  pub fn div_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
    mpq_div_2exp(self, &mpq_s::from(&*self), n); // cast &mut self to &self
    self
  }

  /// frac create new instance
  #[inline]
  pub fn frac(a: mpz_r, b: mpz_r) -> Self {
    let mut t = mpq_s::init();
    t.set_num(a).set_den(b);
    t
  }

  /// reduction of fraction self = (num / gcd) / (den / gcd)
  #[inline]
  pub fn reduce(&mut self) -> &mut Self {
    let gcd = &self.numref().gcd(self.denref());
    let mut num = mpz_s::init();
    let mut den = mpz_s::init();
    mpz_divexact(&mut num, self.numref(), gcd);
    mpz_divexact(&mut den, self.denref(), gcd);
    self.set_num(&num);
    self.set_den(&den);
    self
  }

  /// cmp_u
  #[inline]
  pub fn cmp_u(&self, u: ui_t) -> int_t {
    self.cmp_ui((u, 1))
  }

  /// cmp_s
  #[inline]
  pub fn cmp_s(&self, s: si_t) -> int_t {
    self.cmp_si((s, 1))
  }
}

/// impl Debug
impl fmt::Debug for __mpq_struct {
  /// fmt
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "({:?}) / ({:?})", self._mp_num, self._mp_den)
  }
}

/// impl Display
impl fmt::Display for __mpq_struct {
  /// fmt
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.fmtstr(10))
  }
}

/// mpq_s
#[allow(non_camel_case_types)]
pub type mpq_s = __mpq_struct; // [__mpq_struct; 1]
/// mpq_t
#[allow(non_camel_case_types)]
pub type mpq_t<'a> = &'a mut mpq_s; // *mut mpq_s
/// mpq_r
#[allow(non_camel_case_types)]
pub type mpq_r<'a> = &'a mpq_s; // *const mpq_s

/// mpq_clears
pub fn mpq_clears(vq: &mut Vec<mpq_t>) -> () {
  vq.iter_mut().for_each(|q| {
    unsafe { __gmpq_clear(*q) } // not use __gmpq_clears
  })
}

/// mpq_clear
#[inline]
pub fn mpq_clear(q: mpq_t) -> () {
  unsafe { __gmpq_clear(q) }
}

/// mpq_inits
pub fn mpq_inits(vq: &mut Vec<mpq_t>) -> () {
  vq.iter_mut().for_each(|q| {
    unsafe { __gmpq_init(*q) } // not use __gmpq_inits
  })
}

/// mpq_init
#[inline]
pub fn mpq_init(q: mpq_t) -> () {
  unsafe { __gmpq_init(q) }
}

/// mpq_set
#[inline]
pub fn mpq_set(q: mpq_t, r: mpq_r) -> () {
  unsafe { __gmpq_set(q, r) }
}

/// mpq_set_ui
#[inline]
pub fn mpq_set_ui(q: mpq_t, u: ui_t, f: ui_t) -> () {
  unsafe { __gmpq_set_ui(q, u, f) }
}

/// mpq_set_si
#[inline]
pub fn mpq_set_si(q: mpq_t, s: si_t, f: ui_t) -> () {
  unsafe { __gmpq_set_si(q, s, f) }
}

/// mpq_set_d
#[inline]
pub fn mpq_set_d(q: mpq_t, d: double_t) -> () {
  unsafe { __gmpq_set_d(q, d) }
}

/// mpq_set_z
#[inline]
pub fn mpq_set_z(q: mpq_t, a: mpz_r) -> () {
  unsafe { __gmpq_set_z(q, a) }
}

/// mpq_set_f
#[inline]
pub fn mpq_set_f(q: mpq_t, f: mpf_r) -> () {
  unsafe { __gmpq_set_f(q, f) }
}

/// mpq_set_num
#[inline]
pub fn mpq_set_num(q: mpq_t, num: mpz_r) -> () {
  unsafe { __gmpq_set_num(q, num) }
}

/// mpq_set_den
#[inline]
pub fn mpq_set_den(q: mpq_t, den: mpz_r) -> () {
  unsafe { __gmpq_set_den(q, den) }
}

/// mpq_set_str
#[inline]
pub fn mpq_set_str(q: mpq_t, s: &str, b: int_t) -> () {
  mpq_set_str_u8z(q, to_u8z!(s), b)
}

/// mpq_set_str_u8z
#[inline]
pub fn mpq_set_str_u8z(q: mpq_t, s: &[u8], b: int_t) -> () {
  unsafe { __gmpq_set_str(q, s as *const [u8] as *const u8, b) }
}

/// mpq_get_u8z
pub fn mpq_get_u8z<'a>(s: Option<&mut [u8]>, b: int_t, q: &'a mpq_s) ->
  Option<Vec<u8>> {
  let ff = s == None;
unsafe {
  let p = __gmpq_get_str(
    match s { None => 0 as *mut u8, Some(s) => s as *mut [u8] as *mut u8 },
    b, q);
  u8zvec(p, ff)
}
}

/// mpq_get_str
/// the length of mut String must larger than mpq_t display length
pub fn mpq_get_str<'a>(s: Option<&mut String>, b: int_t, q: &'a mpq_s) ->
  Result<String, Box<dyn Error>> {
  let r = mpq_get_u8z(
    match s { None => None, Some(s) => Some(unsafe { s.as_bytes_mut() }) },
    b, q);
  match r {
  None => Err("err mpq get str".into()),
  Some(r) => Ok(String::from_utf8(r)?)
  }
}

/// mpq_get_d
#[inline]
pub fn mpq_get_d(q: mpq_r) -> double_t {
  unsafe { __gmpq_get_d(q) }
}

/// mpq_get_num (direct use of numref is recommended) ***may be bug in gmp***
#[inline]
pub fn mpq_get_num(num: mpz_t, q: mpq_r) -> () {
  unsafe { __gmpq_get_num(num, q) }
}

/// mpq_get_den (direct use of denref is recommended) ***may be bug in gmp***
#[inline]
pub fn mpq_get_den(den: mpz_t, q: mpq_r) -> () {
  unsafe { __gmpq_get_den(den, q) }
}

/// mpq_numref
#[inline]
pub fn mpq_numref(q: mpq_r) -> mpz_r {
/*
  unsafe { &mut std::slice::from_raw_parts(__gmpq_numref(q), 1)[0] }
*/
  &q._mp_num
}

/// mpq_denref
#[inline]
pub fn mpq_denref(q: mpq_r) -> mpz_r {
/*
  unsafe { &mut std::slice::from_raw_parts(__gmpq_denref(q), 1)[0] }
*/
  &q._mp_den
}

/// mpq_swap
#[inline]
pub fn mpq_swap(q: mpq_t, r: mpq_t) -> () {
  unsafe { __gmpq_swap(q, r) }
}

/// mpq_cmp
#[inline]
pub fn mpq_cmp(q: mpq_r, r: mpq_r) -> int_t {
  unsafe { __gmpq_cmp(q, r) }
}

/// mpq_cmp_z
#[inline]
pub fn mpq_cmp_z(q: mpq_r, a: mpz_r) -> int_t {
  unsafe { __gmpq_cmp_z(q, a) }
}

/// mpq_cmp_ui
#[inline]
pub fn mpq_cmp_ui(q: mpq_r, u: ui_t, f: ui_t) -> int_t {
  unsafe { __gmpq_cmp_ui(q, u, f) }
}

/// mpq_cmp_si
#[inline]
pub fn mpq_cmp_si(q: mpq_r, s: si_t, f: ui_t) -> int_t {
  unsafe { __gmpq_cmp_si(q, s, f) }
}

/// mpq_equal
#[inline]
pub fn mpq_equal(q: mpq_r, r: mpq_r) -> bool {
  unsafe { __gmpq_equal(q, r) != 0 }
}

/// mpq_sgn
#[inline]
pub fn mpq_sgn(q: mpq_r) -> int_t {
//  unsafe { __gmpq_sgn(q) }
  let t = q._mp_num._mp_size;
  if t < 0 { -1 } else { if t > 0 { 1 } else { 0 } }
}

/// mpq_inv p = q**-1
#[inline]
pub fn mpq_inv(p: mpq_t, q: mpq_r) -> () {
  unsafe { __gmpq_inv(p, q) }
}

/// mpq_abs
#[inline]
pub fn mpq_abs(p: mpq_t, q: mpq_r) -> () {
  unsafe { __gmpq_abs(p, q) }
}

/// mpq_neg
#[inline]
pub fn mpq_neg(p: mpq_t, q: mpq_r) -> () {
  unsafe { __gmpq_neg(p, q) }
}

/// mpq_sub p = q - r
#[inline]
pub fn mpq_sub(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
  unsafe { __gmpq_sub(p, q, r) }
}

/// mpq_add p = q + r
#[inline]
pub fn mpq_add(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
  unsafe { __gmpq_add(p, q, r) }
}

/// mpq_mul p = q * r
#[inline]
pub fn mpq_mul(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
  unsafe { __gmpq_mul(p, q, r) }
}

/// mpq_mul_2exp p = q * 2**n
#[inline]
pub fn mpq_mul_2exp(p: mpq_t, q: mpq_r, n: mp_bitcnt_t) -> () {
  unsafe { __gmpq_mul_2exp(p, q, n) }
}

/// mpq_div p = q / r
#[inline]
pub fn mpq_div(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
  unsafe { __gmpq_div(p, q, r) }
}

/// mpq_div_2exp p = q / 2**n
#[inline]
pub fn mpq_div_2exp(p: mpq_t, q: mpq_r, n: mp_bitcnt_t) -> () {
  unsafe { __gmpq_div_2exp(p, q, n) }
}