inertia-core 0.4.0

Core functionality of the Inertia crate with high level wrappers for FLINT, Arb, and Antic.
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
/*
 *  Copyright (C) 2021 William Youmans
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use crate::*;

use flint_sys::{fmpq, fmpz};
use inertia_algebra::ops::*;
use libc::{c_long, c_ulong};

use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::mem::MaybeUninit;


impl_assign_unsafe! {
    None
    Integer, Integer
    fmpz::fmpz_set
}

impl_assign_unsafe! {
    None
    Integer, u64 {u64 u32 u16 u8}
    fmpz::fmpz_set_ui
}

impl_assign_unsafe! {
    None
    Integer, i64 {i64 i32 i16 i8}
    fmpz::fmpz_set_si
}

impl_cmp_unsafe! {
    eq
    Integer
    fmpz::fmpz_equal
}

impl_cmp_unsafe! {
    ord
    Integer
    fmpz::fmpz_cmp
}

impl_cmp_unsafe! {
    partial_eq
    Integer, u64 {u64 u32 u16 u8}
    fmpz::fmpz_equal_ui
}

impl_cmp_unsafe! {
    partial_ord
    Integer, u64 {u64 u32 u16 u8}
    fmpz::fmpz_cmp_ui
}

impl_cmp_unsafe! {
    partial_eq
    Integer, i64 {i64 i32 i16 i8}
    fmpz::fmpz_equal_si
}

impl_cmp_unsafe! {
    partial_ord
    Integer, i64 {i64 i32 i16 i8}
    fmpz::fmpz_cmp_si
}

impl_unop_unsafe! {
    None
    Integer
    Neg {neg}
    NegAssign {neg_assign}
    fmpz::fmpz_neg
}

impl_unop_unsafe! {
    None
    Integer
    Not {not}
    NotAssign {not_assign}
    fmpz::fmpz_complement
}

impl_unop_unsafe! {
    None
    Integer, Rational
    Inv {inv}
    fmpq_inv_fmpz
}

impl_binop_unsafe! {
    None
    Integer, Integer, Integer

    BitAnd {bitand}
    BitAndAssign {bitand_assign}
    BitAndFrom {bitand_from}
    AssignBitAnd {assign_bitand}
    fmpz::fmpz_and;

    BitOr {bitor}
    BitOrAssign {bitor_assign}
    BitOrFrom {bitor_from}
    AssignBitOr {assign_bitor}
    fmpz::fmpz_or;

    BitXor {bitxor}
    BitXorAssign {bitxor_assign}
    BitXorFrom {bitxor_from}
    AssignBitXor {assign_bitxor}
    fmpz::fmpz_xor;

    Add {add}
    AddAssign {add_assign}
    AddFrom {add_from}
    AssignAdd {assign_add}
    fmpz::fmpz_add;

    Sub {sub}
    SubAssign {sub_assign}
    SubFrom {sub_from}
    AssignSub {assign_sub}
    fmpz::fmpz_sub;

    Mul {mul}
    MulAssign {mul_assign}
    MulFrom {mul_from}
    AssignMul {assign_mul}
    fmpz::fmpz_mul;

    Rem {rem}
    RemAssign {rem_assign}
    RemFrom {rem_from}
    AssignRem {assign_rem}
    fmpz::fmpz_tdiv_r;

}

impl_binop_unsafe! {
    None
    Integer, Integer, Rational

    Pow {pow}
    AssignPow {assign_pow}
    fmpz_pow_fmpz;

    Div {div}
    AssignDiv {assign_div}
    fmpq::fmpq_set_fmpz_frac;
}

impl_binop_unsafe! {
    None
    op_assign
    Integer, u64 {u64 u32 u16 u8}, Integer

    BitAnd {bitand}
    BitAndAssign {bitand_assign}
    AssignBitAnd {assign_bitand}
    fmpz_and_ui;

    BitOr {bitor}
    BitOrAssign {bitor_assign}
    AssignBitOr {assign_bitor}
    fmpz_or_ui;

    BitXor {bitxor}
    BitXorAssign {bitxor_assign}
    AssignBitXor {assign_bitxor}
    fmpz_xor_ui;

    Add {add}
    AddAssign {add_assign}
    AssignAdd {assign_add}
    fmpz::fmpz_add_ui;

    Sub {sub}
    SubAssign {sub_assign}
    AssignSub {assign_sub}
    fmpz::fmpz_sub_ui;

    Mul {mul}
    MulAssign {mul_assign}
    AssignMul {assign_mul}
    fmpz::fmpz_mul_ui;

    Rem {rem}
    RemAssign {rem_assign}
    AssignRem {assign_rem}
    fmpz_tdiv_r_ui;

    Pow {pow}
    PowAssign {pow_assign}
    AssignPow {assign_pow}
    fmpz::fmpz_pow_ui;
}

impl_binop_unsafe! {
    None
    Integer, u64 {u64 u32 u16 u8}, Rational

    Div {div}
    AssignDiv {assign_div}
    fmpz_div_ui;
}

impl_binop_unsafe! {
    None
    op_assign
    Integer, i64 {i64 i32 i16 i8}, Integer

    BitAnd {bitand}
    BitAndAssign {bitand_assign}
    AssignBitAnd {assign_bitand}
    fmpz_and_si;

    BitOr {bitor}
    BitOrAssign {bitor_assign}
    AssignBitOr {assign_bitor}
    fmpz_or_si;

    BitXor {bitxor}
    BitXorAssign {bitxor_assign}
    AssignBitXor {assign_bitxor}
    fmpz_xor_si;

    Add {add}
    AddAssign {add_assign}
    AssignAdd {assign_add}
    fmpz::fmpz_add_si;

    Sub {sub}
    SubAssign {sub_assign}
    AssignSub {assign_sub}
    fmpz::fmpz_sub_si;

    Mul {mul}
    MulAssign {mul_assign}
    AssignMul {assign_mul}
    fmpz::fmpz_mul_si;

    Rem {rem}
    RemAssign {rem_assign}
    AssignRem {assign_rem}
    fmpz_tdiv_r_si;
}

impl_binop_unsafe! {
    None
    Integer, i64 {i64 i32 i16 i8}, Rational

    Pow {pow}
    AssignPow {assign_pow}
    fmpz_pow_si;

    Div {div}
    AssignDiv {assign_div}
    fmpz_div_si;
}

impl_binop_unsafe! {
    None
    op_from
    u64 {u64 u32 u16 u8}, Integer, Integer

    BitAnd {bitand}
    BitAndFrom {bitand_from}
    AssignBitAnd {assign_bitand}
    fmpz_ui_and;

    BitOr {bitor}
    BitOrFrom {bitor_from}
    AssignBitOr {assign_bitor}
    fmpz_ui_or;

    BitXor {bitxor}
    BitXorFrom {bitxor_from}
    AssignBitXor {assign_bitxor}
    fmpz_ui_xor;

    Add {add}
    AddFrom {add_from}
    AssignAdd {assign_add}
    fmpz_ui_add;

    Sub {sub}
    SubFrom {sub_from}
    AssignSub {assign_sub}
    fmpz_ui_sub;

    Mul {mul}
    MulFrom {mul_from}
    AssignMul {assign_mul}
    fmpz_ui_mul;

    Rem {rem}
    RemFrom {rem_from}
    AssignRem {assign_rem}
    fmpz_ui_tdiv_r;
}

impl_binop_unsafe! {
    None
    u64 {u64 u32 u16 u8}, Integer, Rational

    Pow {pow}
    AssignPow {assign_pow}
    fmpz_ui_pow;

    Div {div}
    AssignDiv {assign_div}
    fmpz_ui_div;
}

impl_binop_unsafe! {
    None
    op_from
    i64 {i64 i32 i16 i8}, Integer, Integer

    BitAnd {bitand}
    BitAndFrom {bitand_from}
    AssignBitAnd {assign_bitand}
    fmpz_si_and;

    BitOr {bitor}
    BitOrFrom {bitor_from}
    AssignBitOr {assign_bitor}
    fmpz_si_or;

    BitXor {bitxor}
    BitXorFrom {bitxor_from}
    AssignBitXor {assign_bitxor}
    fmpz_si_xor;

    Add {add}
    AddFrom {add_from}
    AssignAdd {assign_add}
    fmpz_si_add;

    Sub {sub}
    SubFrom {sub_from}
    AssignSub {assign_sub}
    fmpz_si_sub;

    Mul {mul}
    MulFrom {mul_from}
    AssignMul {assign_mul}
    fmpz_si_mul;

    Rem {rem}
    RemFrom {rem_from}
    AssignRem {assign_rem}
    fmpz_si_tdiv_r;
}

impl_binop_unsafe! {
    None
    i64 {i64 i32 i16 i8}, Integer, Rational

    Pow {pow}
    AssignPow {assign_pow}
    fmpz_si_pow;

    Div {div}
    AssignDiv {assign_div}
    fmpz_si_div;
}

#[inline]
unsafe fn fmpz_and_ui(res: *mut fmpz::fmpz, f: *const fmpz::fmpz, x: c_ulong) {
    fmpz::fmpz_init_set_ui(res, x);
    fmpz::fmpz_and(res, f, res);
}

#[inline]
unsafe fn fmpz_and_si(res: *mut fmpz::fmpz, f: *const fmpz::fmpz, x: c_long) {
    fmpz::fmpz_init_set_si(res, x);
    fmpz::fmpz_and(res, f, res);
}

#[inline]
unsafe fn fmpz_ui_and(res: *mut fmpz::fmpz, x: c_ulong, f: *const fmpz::fmpz) {
    fmpz_and_ui(res, f, x);
}

#[inline]
unsafe fn fmpz_si_and(res: *mut fmpz::fmpz, x: c_long, f: *const fmpz::fmpz) {
    fmpz_and_si(res, f, x);
}

#[inline]
unsafe fn fmpz_or_ui(res: *mut fmpz::fmpz, f: *const fmpz::fmpz, x: c_ulong) {
    fmpz::fmpz_init_set_ui(res, x);
    fmpz::fmpz_or(res, f, res);
}

#[inline]
unsafe fn fmpz_or_si(res: *mut fmpz::fmpz, f: *const fmpz::fmpz, x: c_long) {
    fmpz::fmpz_init_set_si(res, x);
    fmpz::fmpz_or(res, f, res);
}

#[inline]
unsafe fn fmpz_ui_or(res: *mut fmpz::fmpz, x: c_ulong, f: *const fmpz::fmpz) {
    fmpz_or_ui(res, f, x);
}

#[inline]
unsafe fn fmpz_si_or(res: *mut fmpz::fmpz, x: c_long, f: *const fmpz::fmpz) {
    fmpz_or_si(res, f, x);
}

#[inline]
unsafe fn fmpz_xor_ui(res: *mut fmpz::fmpz, f: *const fmpz::fmpz, x: c_ulong) {
    fmpz::fmpz_init_set_ui(res, x);
    fmpz::fmpz_xor(res, f, res);
}

#[inline]
unsafe fn fmpz_xor_si(res: *mut fmpz::fmpz, f: *const fmpz::fmpz, x: c_long) {
    fmpz::fmpz_init_set_si(res, x);
    fmpz::fmpz_xor(res, f, res);
}

#[inline]
unsafe fn fmpz_ui_xor(res: *mut fmpz::fmpz, x: c_ulong, f: *const fmpz::fmpz) {
    fmpz_xor_ui(res, f, x);
}

#[inline]
unsafe fn fmpz_si_xor(res: *mut fmpz::fmpz, x: c_long, f: *const fmpz::fmpz) {
    fmpz_xor_si(res, f, x);
}

#[inline]
unsafe fn fmpz_ui_add(res: *mut fmpz::fmpz, x: c_ulong, f: *const fmpz::fmpz) {
    fmpz::fmpz_add_ui(res, f, x);
}

#[inline]
unsafe fn fmpz_si_add(res: *mut fmpz::fmpz, x: c_long, f: *const fmpz::fmpz) {
    fmpz::fmpz_add_si(res, f, x);
}

#[inline]
unsafe fn fmpz_ui_sub(res: *mut fmpz::fmpz, x: c_ulong, f: *const fmpz::fmpz) {
    fmpz::fmpz_sub_ui(res, f, x);
    fmpz::fmpz_neg(res, res);
}

#[inline]
unsafe fn fmpz_si_sub(res: *mut fmpz::fmpz, x: c_long, f: *const fmpz::fmpz) {
    fmpz::fmpz_sub_si(res, f, x);
    fmpz::fmpz_neg(res, res);
}

#[inline]
unsafe fn fmpz_ui_mul(res: *mut fmpz::fmpz, f: c_ulong, g: *const fmpz::fmpz) {
    fmpz::fmpz_mul_ui(res, g, f);
}

#[inline]
unsafe fn fmpz_si_mul(res: *mut fmpz::fmpz, f: c_long, g: *const fmpz::fmpz) {
    fmpz::fmpz_mul_si(res, g, f);
}

#[inline]
unsafe fn fmpz_tdiv_r_ui(f: *mut fmpz::fmpz, g: *const fmpz::fmpz, h: c_ulong) {
    let r = fmpz::fmpz_tdiv_ui(g, h);
    fmpz::fmpz_set_ui(f, r);
}

#[inline]
unsafe fn fmpz_tdiv_r_si(f: *mut fmpz::fmpz, g: *const fmpz::fmpz, h: c_long) {
    let r = fmpz::fmpz_tdiv_ui(g, h as u64);
    fmpz::fmpz_set_ui(f, r);
}

#[inline]
unsafe fn fmpz_ui_tdiv_r(res: *mut fmpz::fmpz, x: c_ulong, h: *const fmpz::fmpz) {
    fmpz::fmpz_init_set_ui(res, x);
    fmpz::fmpz_tdiv_r(res, res, h);
}

#[inline]
unsafe fn fmpz_si_tdiv_r(res: *mut fmpz::fmpz, x: c_long, h: *const fmpz::fmpz) {
    fmpz::fmpz_init_set_si(res, x);
    fmpz::fmpz_tdiv_r(res, res, h);
}

#[inline]
unsafe fn fmpq_inv_fmpz(res: *mut fmpq::fmpq, f: *const fmpz::fmpz) {
    fmpq::fmpq_set_fmpz_den1(res, f);
    fmpq::fmpq_inv(res, res);
}

#[inline]
unsafe fn fmpz_pow_fmpz(res: *mut fmpq::fmpq, f: *const fmpz::fmpz, g: *const fmpz::fmpz) {
    fmpq::fmpq_set_fmpz_den1(res, f);
    fmpq::fmpq_pow_fmpz(res, res, g);
}

#[inline]
unsafe fn fmpz_pow_si(res: *mut fmpq::fmpq, f: *const fmpz::fmpz, g: c_long) {
    fmpq::fmpq_set_fmpz_den1(res, f);
    fmpq::fmpq_pow_si(res, res, g);
}

#[inline]
unsafe fn fmpz_ui_pow(res: *mut fmpq::fmpq, f: c_ulong, g: *const fmpz::fmpz) {
    fmpq::fmpq_set_ui_den1(res, f);
    fmpq::fmpq_pow_fmpz(res, res, g);
}

#[inline]
unsafe fn fmpz_si_pow(res: *mut fmpq::fmpq, f: c_long, g: *const fmpz::fmpz) {
    fmpq::fmpq_set_si_den1(res, f);
    fmpq::fmpq_pow_fmpz(res, res, g);
}

#[inline]
unsafe fn fmpz_div_ui(res: *mut fmpq::fmpq, f: *const fmpz::fmpz, g: c_ulong) {
    let mut z = MaybeUninit::uninit();
    fmpz::fmpz_init_set_ui(z.as_mut_ptr(), g);
    fmpq::fmpq_set_fmpz_frac(res, f, z.as_ptr());
    fmpz::fmpz_clear(z.as_mut_ptr());
}

#[inline]
unsafe fn fmpz_div_si(res: *mut fmpq::fmpq, f: *const fmpz::fmpz, g: c_long) {
    let mut z = MaybeUninit::uninit();
    fmpz::fmpz_init_set_si(z.as_mut_ptr(), g);
    fmpq::fmpq_set_fmpz_frac(res, f, z.as_ptr());
    fmpz::fmpz_clear(z.as_mut_ptr());
}

#[inline]
unsafe fn fmpz_ui_div(res: *mut fmpq::fmpq, f: c_ulong, g: *const fmpz::fmpz) {
    let mut z = MaybeUninit::uninit();
    fmpz::fmpz_init_set_ui(z.as_mut_ptr(), f);
    fmpq::fmpq_set_fmpz_frac(res, z.as_ptr(), g);
    fmpz::fmpz_clear(z.as_mut_ptr());
}

#[inline]
unsafe fn fmpz_si_div(res: *mut fmpq::fmpq, f: c_long, g: *const fmpz::fmpz) {
    let mut z = MaybeUninit::uninit();
    fmpz::fmpz_init_set_si(z.as_mut_ptr(), f);
    fmpq::fmpq_set_fmpz_frac(res, z.as_ptr(), g);
    fmpz::fmpz_clear(z.as_mut_ptr());
}