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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* 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/>.
*/
mod ops;
//mod conv;
//#[cfg(feature = "serde")]
//mod serde;
use crate::*;
use flint_sys::{fmpq, fmpq_mat};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem::MaybeUninit;
#[derive(Debug)]
pub struct RatMat {
inner: fmpq_mat::fmpq_mat_struct,
}
impl AsRef<RatMat> for RatMat {
fn as_ref(&self) -> &RatMat {
self
}
}
impl Clone for RatMat {
#[inline]
fn clone(&self) -> Self {
let mut z = MaybeUninit::uninit();
unsafe {
fmpq_mat::fmpq_mat_init_set(z.as_mut_ptr(), self.as_ptr());
RatMat::from_raw(z.assume_init())
}
}
}
impl fmt::Display for RatMat {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let r = self.nrows().try_into().expect(
"Cannot convert signed long to usize.");
let c = self.ncols().try_into().expect(
"Cannot convert signed long to usize.");
let mut out = Vec::with_capacity(r);
for i in 0..r {
let mut row = Vec::with_capacity(c + 2);
row.push("[".to_string());
for j in 0..c {
row.push(format!(" {} ", self.get_entry(i, j)));
}
if i == r - 1 {
row.push("]".to_string());
} else {
row.push("]\n".to_string());
}
out.push(row.join(""));
}
write!(f, "{}", out.join(""))
}
}
impl Drop for RatMat {
#[inline]
fn drop(&mut self) {
unsafe { fmpq_mat::fmpq_mat_clear(self.as_mut_ptr()) }
}
}
// TODO: make entries method that borrows so we dont need to copy entries
impl Hash for RatMat {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.get_entries().hash(state);
}
}
impl<const CAP: usize> NewMatrix<[&Rational; CAP]> for RatMat {
fn new(src: [&Rational; CAP], nrows: i64, ncols: i64) -> Self {
let nrows_ui: usize = nrows.try_into().expect(
"Cannot convert signed long to usize.");
let ncols_ui: usize = ncols.try_into().expect(
"Cannot convert signed long to usize.");
assert_eq!(src.len(), nrows_ui * ncols_ui);
let mut res = RatMat::zero(nrows, ncols);
let mut col;
let mut row = 0usize;
for (i, x) in src.into_iter().enumerate() {
col = i % ncols_ui;
if col == 0 && i != 0 {
row += 1;
}
res.set_entry(row, col, x);
}
res
}
}
impl<T, const CAP: usize> NewMatrix<[T; CAP]> for RatMat
where
T: Into<Rational>
{
fn new(src: [T; CAP], nrows: i64, ncols: i64) -> Self {
let nrows_ui: usize = nrows.try_into().expect(
"Cannot convert signed long to usize.");
let ncols_ui: usize = ncols.try_into().expect(
"Cannot convert signed long to usize.");
assert_eq!(src.len(), nrows_ui * ncols_ui);
let mut res = RatMat::zero(nrows, ncols);
let mut col;
let mut row = 0usize;
for (i, x) in src.into_iter().enumerate() {
col = i % ncols_ui;
if col == 0 && i != 0 {
row += 1;
}
res.set_entry(row, col, x.into());
}
res
}
}
impl NewMatrix<&[Rational]> for RatMat {
fn new(src: &[Rational], nrows: i64, ncols: i64) -> Self {
let nrows_ui: usize = nrows.try_into().expect(
"Cannot convert signed long to usize.");
let ncols_ui: usize = ncols.try_into().expect(
"Cannot convert signed long to usize.");
assert_eq!(src.len(), nrows_ui * ncols_ui);
let mut res = RatMat::zero(nrows, ncols);
let mut col;
let mut row = 0usize;
for (i, x) in src.iter().enumerate() {
col = i % ncols_ui;
if col == 0 && i != 0 {
row += 1;
}
res.set_entry(row, col, x);
}
res
}
}
impl<'a, T> NewMatrix<&'a [T]> for RatMat
where
&'a T: Into<Rational>
{
fn new(src: &'a [T], nrows: i64, ncols: i64) -> Self {
let nrows_ui: usize = nrows.try_into().expect(
"Cannot convert signed long to usize.");
let ncols_ui: usize = ncols.try_into().expect(
"Cannot convert signed long to usize.");
assert_eq!(src.len(), nrows_ui * ncols_ui);
let mut res = RatMat::zero(nrows, ncols);
let mut col;
let mut row = 0usize;
for (i, x) in src.iter().enumerate() {
col = i % ncols_ui;
if col == 0 && i != 0 {
row += 1;
}
res.set_entry(row, col, x.into());
}
res
}
}
impl RatMat {
// private helper methods to convert usize indices to i64, emit consistent
// messages on panic, and bounds check
fn check_indices(&self, i: usize, j: usize) -> (i64, i64) {
(self.check_row_index(i), self.check_col_index(j))
}
fn check_row_index(&self, i: usize) -> i64 {
let i = i.try_into().expect("Cannot convert index to a signed long.");
assert!(i < self.nrows_si());
i
}
fn check_col_index(&self, j: usize) -> i64 {
let j = j.try_into().expect("Cannot convert index to a signed long.");
assert!(j < self.ncols_si());
j
}
#[inline]
pub fn new<S>(src: S, nrows: i64, ncols: i64) -> RatMat
where
Self: NewMatrix<S>
{
<RatMat as NewMatrix<S>>::new(src, nrows, ncols)
}
#[inline]
pub const fn as_ptr(&self) -> *const fmpq_mat::fmpq_mat_struct {
&self.inner
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut fmpq_mat::fmpq_mat_struct {
&mut self.inner
}
#[inline]
pub fn from_raw(raw: fmpq_mat::fmpq_mat_struct) -> RatMat {
RatMat { inner: raw }
}
#[inline]
pub fn zero(nrows: i64, ncols: i64) -> RatMat {
let mut z = MaybeUninit::uninit();
unsafe {
fmpq_mat::fmpq_mat_init(z.as_mut_ptr(), nrows, ncols);
RatMat::from_raw(z.assume_init())
}
}
#[inline]
pub fn one(dim: i64) -> RatMat {
let mut res = RatMat::zero(dim, dim);
unsafe {
fmpq_mat::fmpq_mat_one(res.as_mut_ptr());
}
res
}
/// Set `self` to the zero matrix.
#[inline]
pub fn zero_assign(&mut self) {
unsafe {
fmpq_mat::fmpq_mat_zero(self.as_mut_ptr());
}
}
/// Set `self` to the identity matrix. Panics if the matrix is not square.
#[inline]
pub fn one_assign(&mut self) {
assert!(self.is_square());
unsafe {
fmpq_mat::fmpq_mat_one(self.as_mut_ptr());
}
}
/// Return the number of rows.
#[inline]
pub fn nrows(&self) -> usize {
self.nrows_si().try_into().expect("Cannot convert signed long to usize.")
}
/// Return the number of rows.
#[inline]
pub fn nrows_si(&self) -> i64 {
unsafe { fmpq_mat::fmpq_mat_nrows(self.as_ptr())}
}
/// Return the number of columns.
#[inline]
pub fn ncols(&self) -> usize {
self.ncols_si().try_into().expect("Cannot convert signed long to usize.")
}
/// Return the number of columns.
#[inline]
pub fn ncols_si(&self) -> i64 {
unsafe { fmpq_mat::fmpq_mat_ncols(self.as_ptr())}
}
#[inline]
pub fn is_empty(&self) -> bool {
unsafe { fmpq_mat::fmpq_mat_is_empty(self.as_ptr()) != 0 }
}
#[inline]
pub fn is_square(&self) -> bool {
unsafe { fmpq_mat::fmpq_mat_is_square(self.as_ptr()) != 0 }
}
#[inline]
pub fn is_zero(&self) -> bool {
unsafe { fmpq_mat::fmpq_mat_is_zero(self.as_ptr()) != 0 }
}
#[inline]
pub fn is_one(&self) -> bool {
unsafe { fmpq_mat::fmpq_mat_is_one(self.as_ptr()) != 0 }
}
/// Get the `(i, j)`-th entry of the matrix.
#[inline]
pub fn get_entry(&self, i: usize, j: usize) -> Rational {
let mut res = Rational::zero();
self.assign_entry(i, j, &mut res);
res
}
// TODO: need consistent naming convention
// even better: remove, replace with 'entry' returning a borrow which can
// be assigned.
/// Get the `(i, j)`-th entry of an integer matrix and assign it to `out`.
/// Avoids unnecessary allocation.
#[inline]
pub fn assign_entry(&self, i: usize, j: usize, out: &mut Rational) {
let (i, j) = self.check_indices(i, j);
unsafe {
let x = fmpq_mat::fmpq_mat_entry(self.as_ptr(), i, j);
fmpq::fmpq_set(out.as_mut_ptr(), x);
}
}
/// Set the `(i, j)`-th entry of the matrix.
#[inline]
pub fn set_entry<T: AsRef<Rational>>(&mut self, i: usize, j: usize, e: T) {
let (i, j) = self.check_indices(i, j);
unsafe {
let x = fmpq_mat::fmpq_mat_entry(self.as_ptr(), i, j);
fmpq::fmpq_set(x, e.as_ref().as_ptr());
}
}
/// Get a vector with all of the entries of the matrix.
pub fn get_entries(&self) -> Vec<Rational> {
let r = self.nrows();
let c = self.ncols();
let mut out = Vec::with_capacity(r * c);
for i in 0..r {
for j in 0..c {
out.push(self.get_entry(i, j));
}
}
out
}
/*
/// Swap two integer matrices. The dimensions are allowed to be different.
#[inline]
pub fn swap(&mut self, other: &mut RatMat) {
unsafe {
fmpq_mat::fmpq_mat_swap(self.as_mut_ptr(), other.as_mut_ptr());
}
}
/// Swap the rows `r1` and `r2` of an integer matrix.
pub fn swap_rows(&mut self, r1: usize, r2: usize) {
let r1 = self.check_row_index(r1);
let r2 = self.check_row_index(r2);
unsafe {
fmpq_mat::fmpq_mat_swap_rows(
self.as_mut_ptr(),
std::ptr::null(),
r1,
r2
);
}
}
/// Swap the columns `r` and `s` of an integer matrix.
pub fn swap_cols(&mut self, c1: usize, c2: usize) {
let c1 = self.check_col_index(c1);
let c2 = self.check_col_index(c2);
unsafe {
fmpq_mat::fmpq_mat_swap_rows(
self.as_mut_ptr(),
std::ptr::null(),
c1,
c2
);
}
}
/// Swap row `i` and `r - i` for `0 <= i < r/2` where `r` is the number
/// of rows of the input matrix.
#[inline]
pub fn invert_rows(&mut self) {
unsafe {
fmpq_mat::fmpq_mat_invert_rows(
self.as_mut_ptr(),
std::ptr::null()
);
}
}
/// Swap columns `i` and `c - i` for `0 <= i < c/2` where `c` is the number
/// of columns of the input matrix.
#[inline]
pub fn invert_columns(&mut self) {
unsafe {
fmpq_mat::fmpq_mat_invert_cols(
self.as_mut_ptr(),
std::ptr::null()
);
}
}
/* TODO: function missing from bindings
/// Swap two integer matrices by swapping the individual entries rather
/// than swapping the contents of their structs.
#[inline]
pub fn swap_entrywise(&mut self, other: &mut RatMat) {
unsafe {
fmpq_mat::fmpq_mat_swap_entrywise(
self.as_mut_ptr(),
other.as_mut_ptr()
);
}
}
*/
/*
/// Return true if the matrix is invertible.
#[inline]
pub fn is_invertible(&self) -> bool {
self.is_square() && !self.det().is_zero()
}*/
/// Return true if row `i` is all zeros.
pub fn is_zero_row(&self, i: usize) -> bool {
let i = self.check_row_index(i);
unsafe {
fmpq_mat::fmpq_mat_is_zero_row(self.as_ptr(), i) != 0
}
}
/// Return true if column `i` is all zeros.
// TODO: Does an additional allocation compared to `is_zero_row`.
#[inline]
pub fn is_zero_col(&self, i: usize) -> bool {
self.column(i).is_zero()
}
/// Return the transpose.
#[inline]
pub fn transpose(&self) -> RatMat {
let mut res = RatMat::zero(self.nrows_si(), self.ncols_si());
unsafe {
fmpq_mat::fmpq_mat_transpose(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Transpose the matrix in place. Panics if the matrix is not square.
#[inline]
pub fn transpose_assign(&mut self) {
assert!(self.is_square());
unsafe { fmpq_mat::fmpq_mat_transpose(self.as_mut_ptr(), self.as_ptr()); }
}
/// Horizontally concatenate two matrices. Panics if the number of rows of
/// both matrices do not agree.
pub fn hcat<T>(&self, other: T) -> RatMat where
T: AsRef<RatMat>
{
let other = other.as_ref();
let nrows = self.nrows_si();
assert_eq!(nrows, other.nrows_si());
let mut res = RatMat::zero(nrows, self.ncols_si() + other.ncols_si());
unsafe {
fmpq_mat::fmpq_mat_concat_horizontal(
res.as_mut_ptr(),
self.as_ptr(),
other.as_ptr()
);
}
res
}
/// Vertically concatenate two matrices. Panics if the number of columns of
/// both matrices do not agree.
pub fn vcat<T>(&self, other: T) -> RatMat where
T: AsRef<RatMat>
{
let other = other.as_ref();
let ncols = self.ncols_si();
assert_eq!(ncols, other.ncols_si());
let mut res = RatMat::zero(self.nrows_si() + other.nrows_si(), ncols);
unsafe {
fmpq_mat::fmpq_mat_concat_horizontal(
res.as_mut_ptr(),
self.as_ptr(),
other.as_ptr()
);
}
res
}
// TODO: 'window' version to avoid allocation
/// Return a new matrix containing the `r2 - r1` by `c2 - c1` submatrix of
/// an integer matrix whose `(0, 0)` entry is the `(r1, c1)` entry of the input.
pub fn submatrix(&self, r1: usize, c1: usize, r2: usize, c2: usize) -> RatMat {
if r1 == r2 || c1 == c2 {
return RatMat::zero(0, 0)
}
assert!(r1 <= r2);
assert!(c1 <= c2);
let (r1, c1) = self.check_indices(r1, c1);
let (r2, c2) = self.check_indices(r2, c2);
let mut res = RatMat::zero(r2 - r1, c2 - c1);
let mut win = MaybeUninit::uninit();
unsafe {
fmpq_mat::fmpq_mat_window_init(
win.as_mut_ptr(),
self.as_ptr(),
r1,
c1,
r2,
c2
);
fmpq_mat::fmpq_mat_set(res.as_mut_ptr(), win.as_ptr());
fmpq_mat::fmpq_mat_window_clear(win.as_mut_ptr());
}
res
}
/// Return row `i` as an integer matrix.
#[inline]
pub fn row(&self, i: usize) -> RatMat {
self.submatrix(i, 0, i + 1, self.ncols())
}
/// Return column `j` as an integer matrix.
#[inline]
pub fn column(&self, j: usize) -> RatMat {
self.submatrix(0, j, self.nrows(), j + 1)
}
/// Square an integer matrix. The matrix must be square.
#[inline]
pub fn square(&self) -> Self {
assert!(self.is_square());
let mut res = RatMat::zero(self.nrows_si(), self.ncols_si());
unsafe {
fmpq_mat::fmpq_mat_sqr(res.as_mut_ptr(), self.as_ptr())
}
res
}
/// Square an integer matrix in place. The matrix must be square.
#[inline]
pub fn square_assign(&mut self) {
assert!(self.is_square());
unsafe {
fmpq_mat::fmpq_mat_sqr(self.as_mut_ptr(), self.as_ptr());
}
}
/// Return the kronecker product of two integer matrices.
pub fn kronecker_product<T>(&self, other: T) -> RatMat where
T: AsRef<RatMat>
{
let other = other.as_ref();
let mut res = RatMat::zero(
self.nrows_si() * other.nrows_si(),
self.ncols_si() * other.ncols_si()
);
unsafe {
fmpq_mat::fmpq_mat_kronecker_product(
res.as_mut_ptr(),
self.as_ptr(),
other.as_ptr()
);
}
res
}
/// Compute the trace of a square integer matrix.
#[inline]
pub fn trace(&self) -> Integer {
assert!(self.is_square());
let mut res = Integer::zero();
unsafe {
fmpq_mat::fmpq_mat_trace(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Return the content of an integer matrix, that is, the gcd of all its
/// entries. Returns zero if the matrix is empty.
#[inline]
pub fn content(&self) -> Integer {
let mut res = Integer::zero();
unsafe {
fmpq_mat::fmpq_mat_content(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Compute the determinant of the matrix.
#[inline]
pub fn det(&self) -> Integer {
assert!(self.is_square());
let mut res = Integer::zero();
unsafe {
fmpq_mat::fmpq_mat_det(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Return an absolute upper bound on the determinant of a square integer
/// matrix computed from the Hadamard inequality.
#[inline]
pub fn det_bound(&self) -> Integer {
assert!(self.is_square());
let mut res = Integer::zero();
unsafe {
fmpq_mat::fmpq_mat_det_bound(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Return a positive divisor of the determinant of a square integer matrix.
/// If the determinant is zero this will always return zero.
#[inline]
pub fn det_divisor(&self) -> Integer {
assert!(self.is_square());
let mut res = Integer::zero();
unsafe {
fmpq_mat::fmpq_mat_det_divisor(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Applies a similarity transform to an `n` by `n` integer matrix. If `P`
/// is the identity matrix whose zero entries in row `r` have been replaced
/// by `d`, this transform is equivalent to `P^-1 * M * P`.
#[inline]
pub fn similarity<T>(&self, r: usize, d: T) -> RatMat where
T: AsRef<Integer>
{
let mut res = self.clone();
res.similarity_assign(r, d);
res
}
/// Applies a similarity transform to an `n` by `n` integer matrix in place.
pub fn similarity_assign<T>(&mut self, r: usize, d: T) where
T: AsRef<Integer>
{
let r = self.check_row_index(r);
assert!(self.is_square());
unsafe {
fmpq_mat::fmpq_mat_similarity(
self.as_mut_ptr(),
r.into(),
d.as_ref().as_ptr()
);
}
}
/// Return the characteristic polynomial of a square integer matrix.
#[inline]
pub fn charpoly(&self) -> IntPoly {
assert!(self.is_square());
let mut res = IntPoly::zero();
unsafe {
fmpq_mat::fmpq_mat_charpoly(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Return the minimal polynomial of a square integer matrix.
#[inline]
pub fn minpoly(&self) -> IntPoly {
assert!(self.is_square());
let mut res = IntPoly::zero();
unsafe {
fmpq_mat::fmpq_mat_minpoly(res.as_mut_ptr(), self.as_ptr());
}
res
}
/// Return the rank of a matrix, that is, the number of linearly independent
/// columns (equivalently, rows) of an integer matrix. The rank is computed by
/// row reducing a copy of the input matrix.
#[inline]
pub fn rank(&self) -> i64 {
unsafe { fmpq_mat::fmpq_mat_rank(self.as_ptr()) }
}
*/
}