qfall-math 0.1.1

Mathematical foundations for rapid prototyping of lattice-based cryptography
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
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
// Copyright © 2023 Marcel Luca Schmidt
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementations to get information about a [`MatQ`] matrix.

use super::MatQ;
use crate::rational::Q;
use crate::traits::{MatrixDimensions, MatrixGetEntry, MatrixGetSubmatrix};
use flint_sys::fmpq_mat::{fmpq_mat_init_set, fmpq_mat_window_clear, fmpq_mat_window_init};
use flint_sys::{
    fmpq::{fmpq, fmpq_set},
    fmpq_mat::fmpq_mat_entry,
};
use std::mem::MaybeUninit;

impl MatrixDimensions for MatQ {
    /// Returns the number of rows of the matrix as a [`i64`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::MatQ;
    /// use qfall_math::traits::*;
    ///
    /// let matrix = MatQ::new(5, 6);
    /// let rows = matrix.get_num_rows();
    /// ```
    fn get_num_rows(&self) -> i64 {
        self.matrix.r
    }

    /// Returns the number of columns of the matrix as a [`i64`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::MatQ;
    /// use qfall_math::traits::*;
    ///
    /// let matrix = MatQ::new(5, 6);
    /// let columns = matrix.get_num_columns();
    /// ```
    fn get_num_columns(&self) -> i64 {
        self.matrix.c
    }
}

impl MatrixGetEntry<Q> for MatQ {
    /// Outputs the [`Q`] value of a specific matrix entry
    /// without checking whether it's part of the matrix.
    ///
    /// Parameters:
    /// - `row`: specifies the row in which the entry is located
    /// - `column`: specifies the column in which the entry is located
    ///
    /// Returns the [`Q`] value of the matrix at the position of the given
    /// row and column.
    ///
    /// # Safety
    /// To use this function safely, make sure that the selected entry is part
    /// of the matrix. If it is not, memory leaks, unexpected panics, etc. might
    /// occur.
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::{MatQ, Q};
    /// use qfall_math::traits::MatrixGetEntry;
    /// use std::str::FromStr;
    ///
    /// let matrix = MatQ::from_str("[[1, 2, 3/4],[4, 5, 6],[7, 8, 9]]").unwrap();
    ///
    /// assert_eq!(unsafe { matrix.get_entry_unchecked(0, 2) }, Q::from((3, 4)));
    /// assert_eq!(unsafe { matrix.get_entry_unchecked(2, 1) }, Q::from(8));
    /// assert_eq!(unsafe { matrix.get_entry_unchecked(2, 1) }, Q::from(8));
    /// ```
    unsafe fn get_entry_unchecked(&self, row: i64, column: i64) -> Q {
        let mut copy = Q::default();
        let entry = unsafe { fmpq_mat_entry(&self.matrix, row, column) };
        unsafe { fmpq_set(&mut copy.value, entry) };

        copy
    }
}

impl MatrixGetSubmatrix for MatQ {
    /// Returns a deep copy of the submatrix defined by the given parameters
    /// and does not check the provided dimensions.
    /// There is also a safe version of this function that checks the input.
    ///
    /// Parameters:
    /// `row_1`: the starting row of the submatrix
    /// `row_2`: the ending row of the submatrix
    /// `col_1`: the starting column of the submatrix
    /// `col_2`: the ending column of the submatrix
    ///
    /// Returns the submatrix from `(row_1, col_1)` to `(row_2, col_2)`(exclusively).
    ///
    /// # Examples
    /// ```
    /// use qfall_math::{rational::MatQ, traits::MatrixGetSubmatrix};
    /// use std::str::FromStr;
    ///
    /// let mat = MatQ::identity(3, 3);
    ///
    /// let sub_mat_1 = mat.get_submatrix(0, 2, 1, 1).unwrap();
    /// let sub_mat_2 = mat.get_submatrix(0, -1, 1, -2).unwrap();
    /// let sub_mat_3 = unsafe{mat.get_submatrix_unchecked(0, 3, 1, 2)};
    ///
    /// let e_2 = MatQ::from_str("[[0],[1],[0]]").unwrap();
    /// assert_eq!(e_2, sub_mat_1);
    /// assert_eq!(e_2, sub_mat_2);
    /// assert_eq!(e_2, sub_mat_3);
    /// ```
    ///
    /// # Safety
    /// To use this function safely, make sure that the selected submatrix is part
    /// of the matrix. If it is not, memory leaks, unexpected panics, etc. might
    /// occur.
    unsafe fn get_submatrix_unchecked(
        &self,
        row_1: i64,
        row_2: i64,
        col_1: i64,
        col_2: i64,
    ) -> Self {
        let mut window = MaybeUninit::uninit();
        // The memory for the elements of window is shared with self.
        unsafe {
            fmpq_mat_window_init(
                window.as_mut_ptr(),
                &self.matrix,
                row_1,
                col_1,
                row_2,
                col_2,
            )
        };
        let mut window_copy = MaybeUninit::uninit();
        unsafe {
            // Deep clone of the content of the window
            fmpq_mat_init_set(window_copy.as_mut_ptr(), window.as_ptr());
            // Clears the matrix window and releases any memory that it uses. Note that
            // the memory to the underlying matrix that window points to is not freed
            fmpq_mat_window_clear(window.as_mut_ptr());
        }
        MatQ {
            matrix: unsafe { window_copy.assume_init() },
        }
    }
}

impl MatQ {
    /// Efficiently collects all [`fmpq`]s in a [`MatQ`] without cloning them.
    ///
    /// Hence, the values on the returned [`Vec`] are intended for short-term use
    /// as the access to [`fmpq`] values could lead to memory leaks or modified values
    /// once the [`MatQ`] instance was modified or dropped.
    ///
    /// # Examples
    /// ```compile_fail
    /// use qfall_math::rational::MatQ;
    /// use std::str::FromStr;
    ///
    /// let mat = MatQ::from_str("[[1/1, 2],[3/1, 4],[5/1, 6]]").unwrap();
    ///
    /// let fmpq_entries = mat.collect_entries();
    /// ```
    ///
    /// # Safety
    /// The user has to ensure that all entries are within the matrix dimensions.
    /// Otherwise, memory leaks can occur and no guarantees are given.
    pub(crate) fn collect_entries(&self) -> Vec<fmpq> {
        let mut entries: Vec<fmpq> =
            Vec::with_capacity((self.get_num_rows() * self.get_num_columns()) as usize);

        for row in 0..self.get_num_rows() {
            for col in 0..self.get_num_columns() {
                // efficiently get entry without cloning the entry itself
                let entry = unsafe { *fmpq_mat_entry(&self.matrix, row, col) };
                entries.push(entry);
            }
        }

        entries
    }

    /// Returns a copy of all entries of `self` as [`f64`] values in [`Vec`]tors s.t.
    /// the resulting vector can be used as `entries_f64[i][j]` to
    /// access the entry in row `i` and column `j`.
    ///
    /// **WARNING:** The return is system dependent if any entry of the matrix is
    /// is too large or too small to fit in an [`f64`], i.e. the value should be within
    /// [`f64::MIN`] and [`f64::MAX`]. It the entry can't be represented exactly, it will
    /// be rounded towards zero.
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::MatQ;
    /// use std::str::FromStr;
    ///
    /// let mat = MatQ::from_str("[[1/1, 2],[3/1, 4],[5/1, 6]]").unwrap();
    ///
    /// let entries_f64 = mat.collect_entries_f64();
    ///
    /// assert_eq!(entries_f64[0][1], 2.0);
    /// ```
    pub fn collect_entries_f64(&self) -> Vec<Vec<f64>> {
        let num_rows = self.get_num_rows() as usize;
        let num_cols = self.get_num_columns() as usize;

        let mut entries: Vec<Vec<f64>> = vec![Vec::with_capacity(num_cols); num_rows];

        for (i, row) in entries.iter_mut().enumerate() {
            for j in 0..num_cols {
                // efficiently get entry without cloning the entry itself
                let entry = unsafe {
                    flint_sys::fmpq::fmpq_get_d(fmpq_mat_entry(&self.matrix, i as i64, j as i64))
                };
                row.push(entry);
            }
        }

        entries
    }
}

#[cfg(test)]
mod test_get_entry {
    use super::Q;
    use crate::{
        rational::MatQ,
        traits::{MatrixGetEntry, MatrixSetEntry},
    };
    use std::str::FromStr;

    /// Ensure that getting entries works with large large numerators and denominators.
    #[test]
    fn max_int_positive() {
        let mut matrix = MatQ::new(5, 10);
        let value_1 = Q::from(i64::MAX);
        let value_2 = Q::from((1, i64::MAX));
        matrix.set_entry(0, 0, value_1).unwrap();
        matrix.set_entry(1, 1, value_2).unwrap();

        let entry_1 = matrix.get_entry(0, 0).unwrap();
        let entry_2 = matrix.get_entry(1, 1).unwrap();

        assert_eq!(Q::from(i64::MAX), entry_1);
        assert_eq!(Q::from((1, i64::MAX)), entry_2);
    }

    /// Ensure that getting entries works with large numerators and denominators (larger than [`i64`]).
    #[test]
    fn large_positive() {
        let mut matrix = MatQ::new(5, 10);
        let value_1 = Q::from(u64::MAX);
        let value_2 = Q::from((1, u64::MAX));
        matrix.set_entry(0, 0, value_1).unwrap();
        matrix.set_entry(1, 1, value_2).unwrap();

        let entry_1 = matrix.get_entry(0, 0).unwrap();
        let entry_2 = matrix.get_entry(1, 1).unwrap();

        assert_eq!(Q::from(u64::MAX), entry_1);
        assert_eq!(Q::from((1, u64::MAX)), entry_2);
    }

    /// Ensure that getting entries works with large negative numerators and denominators.
    #[test]
    fn max_int_negative() {
        let mut matrix = MatQ::new(5, 10);
        let value_1 = Q::from(i64::MIN);
        let value_2 = Q::from((1, i64::MIN));
        matrix.set_entry(0, 0, value_1).unwrap();
        matrix.set_entry(1, 1, value_2).unwrap();

        let entry_1 = matrix.get_entry(0, 0).unwrap();
        let entry_2 = matrix.get_entry(1, 1).unwrap();

        assert_eq!(Q::from(i64::MIN), entry_1);
        assert_eq!(Q::from((1, i64::MIN)), entry_2);
    }

    /// Ensure that getting entries works with large negative numerators and denominators (larger than [`i64`]).
    #[test]
    fn large_negative() {
        let mut matrix = MatQ::new(5, 10);
        let value_1 = format!("-{}", u64::MAX);
        let value_2 = format!("1/-{}", u64::MAX);
        matrix
            .set_entry(0, 0, Q::from_str(&value_1).unwrap())
            .unwrap();
        matrix
            .set_entry(1, 1, Q::from_str(&value_2).unwrap())
            .unwrap();

        let entry_1 = matrix.get_entry(0, 0).unwrap();
        let entry_2 = matrix.get_entry(1, 1).unwrap();

        assert_eq!(Q::from_str(&format!("-{}", u64::MAX)).unwrap(), entry_1);
        assert_eq!(Q::from_str(&format!("1/-{}", u64::MAX)).unwrap(), entry_2);
    }

    /// Ensure that getting entries at (0, 0) works.
    #[test]
    fn getting_at_zero() {
        let mut matrix = MatQ::new(5, 10);
        let value = Q::from(i64::MIN);
        matrix.set_entry(0, 0, value).unwrap();

        let entry = matrix.get_entry(0, 0).unwrap();

        assert_eq!(entry, Q::from(i64::MIN));
    }

    /// Ensure that a wrong number of rows yields an Error.
    #[test]
    fn error_wrong_row() {
        let matrix = MatQ::new(5, 10);

        assert!(matrix.get_entry(5, 1).is_err());
        assert!(matrix.get_entry(-6, 1).is_err());
    }

    /// Ensure that a wrong number of columns yields an Error.
    #[test]
    fn error_wrong_column() {
        let matrix = MatQ::new(5, 10);

        assert!(matrix.get_entry(1, 100).is_err());
        assert!(matrix.get_entry(1, -11).is_err());
    }

    /// Ensure that negative indices return the correct values.
    #[test]
    fn negative_indexing() {
        let matrix = MatQ::from_str("[[1, 2, 3],[4, 5, 6],[7, 8, 9]]").unwrap();

        assert_eq!(matrix.get_entry(-1, -1).unwrap(), Q::from(9));
        assert_eq!(matrix.get_entry(-1, -2).unwrap(), Q::from(8));
        assert_eq!(matrix.get_entry(-3, -3).unwrap(), Q::from(1));
    }

    /// Ensure that the entry is a deep copy and not just a clone of the reference.
    #[test]
    fn memory_test() {
        let mut matrix = MatQ::new(5, 10);
        let value = Q::from(u64::MAX);
        matrix.set_entry(1, 1, value).unwrap();
        let entry = matrix.get_entry(1, 1).unwrap();
        matrix.set_entry(1, 1, Q::ZERO).unwrap();

        assert_eq!(Q::from(u64::MAX), entry);
    }
}

#[cfg(test)]
mod test_get_num {
    use crate::{rational::MatQ, traits::MatrixDimensions};

    /// Ensure that the getter for number of rows works correctly.
    #[test]
    fn num_rows() {
        let matrix = MatQ::new(5, 10);

        assert_eq!(matrix.get_num_rows(), 5);
    }

    /// Ensure that the getter for number of columns works correctly.
    #[test]
    fn num_columns() {
        let matrix = MatQ::new(5, 10);

        assert_eq!(matrix.get_num_columns(), 10);
    }
}

#[cfg(test)]
mod test_get_vec {
    use crate::{rational::MatQ, traits::MatrixGetSubmatrix};
    use std::str::FromStr;

    /// Ensure that getting a row works
    #[test]
    fn get_row_works() {
        let matrix = MatQ::from_str(&format!(
            "[[0, 0, 0, 0, 0],[4/3, {}, {}, 1/{}, 1/{}]]",
            i64::MAX,
            i64::MIN,
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let row_1 = matrix.get_row(0).unwrap();
        let row_2 = matrix.get_row(1).unwrap();

        let cmp_1 = MatQ::from_str("[[0, 0, 0, 0, 0]]").unwrap();
        let cmp_2 = MatQ::from_str(&format!(
            "[[4/3, {}, {}, 1/{}, 1/{}]]",
            i64::MAX,
            i64::MIN,
            i64::MAX,
            i64::MIN
        ))
        .unwrap();

        assert_eq!(cmp_1, row_1);
        assert_eq!(cmp_2, row_2);
    }

    /// Ensure that getting a row with a negative index works
    #[test]
    fn get_row_negative_indexing_works() {
        let matrix =
            MatQ::from_str(&format!("[[0, 0, 0],[42, {}, {}]]", i64::MAX, i64::MIN)).unwrap();
        let row_1 = matrix.get_row(-2).unwrap();
        let row_2 = matrix.get_row(-1).unwrap();

        let cmp_1 = MatQ::from_str("[[0, 0, 0]]").unwrap();
        let cmp_2 = MatQ::from_str(&format!("[[42, {}, {}]]", i64::MAX, i64::MIN)).unwrap();

        assert_eq!(cmp_1, row_1);
        assert_eq!(cmp_2, row_2);
    }

    /// Ensure that getting a column works
    #[test]
    fn get_column_works() {
        let matrix = MatQ::from_str(&format!(
            "[[1, 0, 3],[{}, 0, 5],[{}, 0, 7],[1/{}, 0, 9],[1/{}, 0, 11]]",
            i64::MAX,
            i64::MIN,
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let column_1 = matrix.get_column(0).unwrap();
        let column_2 = matrix.get_column(1).unwrap();
        let column_3 = matrix.get_column(2).unwrap();

        let cmp_1 = MatQ::from_str(&format!(
            "[[1],[{}],[{}],[1/{}],[1/{}]]",
            i64::MAX,
            i64::MIN,
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let cmp_2 = MatQ::from_str("[[0],[0],[0],[0],[0]]").unwrap();
        let cmp_3 = MatQ::from_str("[[3],[5],[7],[9],[11]]").unwrap();

        assert_eq!(cmp_1, column_1);
        assert_eq!(cmp_2, column_2);
        assert_eq!(cmp_3, column_3);
    }

    /// Ensure that getting a column with a negative index works
    #[test]
    fn get_column_negative_indexing_works() {
        let matrix = MatQ::from_str(&format!(
            "[[42, 0, 42],[{}, 0, 17],[{}, 0, 42]]",
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let column_1 = matrix.get_column(-3).unwrap();
        let column_2 = matrix.get_column(-2).unwrap();
        let column_3 = matrix.get_column(-1).unwrap();

        let cmp_1 = MatQ::from_str(&format!("[[42],[{}],[{}]]", i64::MAX, i64::MIN)).unwrap();
        let cmp_2 = MatQ::from_str("[[0],[0],[0]]").unwrap();
        let cmp_3 = MatQ::from_str("[[42],[17],[42]]").unwrap();

        assert_eq!(cmp_1, column_1);
        assert_eq!(cmp_2, column_2);
        assert_eq!(cmp_3, column_3);
    }

    /// Ensure that wrong row and column dimensions yields an error
    #[test]
    fn wrong_dim_error() {
        let matrix = MatQ::from_str(&format!(
            "[[1, 2, 3],[{}, 4, 5],[{}, 6, 7]]",
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let row_1 = matrix.get_row(-4);
        let row_2 = matrix.get_row(4);
        let column_1 = matrix.get_column(-4);
        let column_2 = matrix.get_column(4);

        assert!(row_1.is_err());
        assert!(row_2.is_err());
        assert!(column_1.is_err());
        assert!(column_2.is_err());
    }
}

#[cfg(test)]
mod test_get_submatrix {
    use crate::{
        integer::Z,
        rational::MatQ,
        traits::{MatrixDimensions, MatrixGetSubmatrix},
    };
    use std::str::FromStr;

    /// Ensures that getting the entire matrix as a submatrix works.
    #[test]
    fn entire_matrix() {
        let mat = MatQ::identity(5, 5);

        let sub_mat = mat.get_submatrix(0, 4, 0, 4).unwrap();

        assert_eq!(mat, sub_mat);
    }

    /// Ensures that a single matrix entry can be retrieved.
    #[test]
    fn matrix_single_entry() {
        let mat = MatQ::identity(5, 5);

        let sub_mat = mat.get_submatrix(0, 0, 0, 0).unwrap();

        let cmp_mat = MatQ::identity(1, 1);
        assert_eq!(cmp_mat, sub_mat);
    }

    /// Ensures that the dimensions of the submatrix are correct.
    #[test]
    fn correct_dimensions() {
        let mat = MatQ::identity(100, 100);

        let sub_mat = mat.get_submatrix(1, 37, 0, 29).unwrap();

        assert_eq!(37, sub_mat.get_num_rows());
        assert_eq!(30, sub_mat.get_num_columns());
    }

    /// Ensures that a submatrix can be correctly retrieved for a matrix with large
    /// entries.
    #[test]
    fn large_entries() {
        let mat =
            MatQ::from_str(&format!("[[{}/3, 2, 3],[1, {}, 3]]", u64::MAX, i64::MIN)).unwrap();

        let sub_mat = mat.get_submatrix(0, 1, 0, 1).unwrap();

        let cmp_mat = MatQ::from_str(&format!("[[{}/3, 2],[1, {}]]", u64::MAX, i64::MIN)).unwrap();
        assert_eq!(cmp_mat, sub_mat);
    }

    /// Ensures that an error is returned if coordinates are addressed that are not
    /// within the matrix.
    #[test]
    fn invalid_coordinates() {
        let mat = MatQ::identity(10, 10);

        assert!(mat.get_submatrix(0, 0, 0, 10).is_err());
        assert!(mat.get_submatrix(0, 10, 0, 0).is_err());
        assert!(mat.get_submatrix(0, 0, -11, 0).is_err());
        assert!(mat.get_submatrix(-11, 0, 0, 0).is_err());
    }

    /// Ensure that negative indices return the correct submatrix.
    #[test]
    fn negative_indexing() {
        let matrix = MatQ::identity(3, 3);

        assert_eq!(matrix, matrix.get_submatrix(0, -1, 0, -1).unwrap());
        assert_eq!(matrix, matrix.get_submatrix(-3, -1, -3, -1).unwrap());
        assert_eq!(
            matrix.get_row(0).unwrap(),
            matrix.get_submatrix(0, -3, -3, -1).unwrap()
        );
    }

    /// Ensures that the function panics if no columns of the matrix are addressed.
    #[test]
    #[should_panic]
    fn no_columns() {
        let mat = MatQ::identity(10, 10);

        let _ = mat.get_submatrix(0, 0, 6, 5);
    }

    /// Ensures that the function panics if no rows of the matrix are addressed.
    #[test]
    #[should_panic]
    fn no_rows() {
        let mat = MatQ::identity(10, 10);

        let _ = mat.get_submatrix(5, 4, 0, 0);
    }

    /// Ensure that the submatrix function can be called with several types.
    #[test]
    fn availability() {
        let mat = MatQ::identity(10, 10);

        let _ = mat.get_submatrix(0_i8, 0_i8, 0_i8, 0_i8);
        let _ = mat.get_submatrix(0_i16, 0_i16, 0_i16, 0_i16);
        let _ = mat.get_submatrix(0_i32, 0_i32, 0_i32, 0_i32);
        let _ = mat.get_submatrix(0_i64, 0_i64, 0_i64, 0_i64);
        let _ = mat.get_submatrix(0_u8, 0_u8, 0_u8, 0_u8);
        let _ = mat.get_submatrix(0_u16, 0_i16, 0_u16, 0_u16);
        let _ = mat.get_submatrix(0_u32, 0_i32, 0_u32, 0_u32);
        let _ = mat.get_submatrix(0_u64, 0_i64, 0_u64, 0_u64);
        let _ = mat.get_submatrix(&Z::ZERO, &Z::ZERO, &Z::ZERO, &Z::ZERO);
    }
}

#[cfg(test)]
mod test_collect_entries {
    use super::MatQ;
    use std::str::FromStr;

    /// Ensures that all entries from the matrices are actually collected in the vector.
    #[test]
    fn all_entries_collected() {
        let mat_1 = MatQ::from_str(&format!(
            "[[1/{}, 2],[{}, {}],[-3/-4, 4]]",
            i64::MAX,
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let mat_2 = MatQ::from_str("[[-1/1, 2/-4]]").unwrap();

        let entries_1 = mat_1.collect_entries();
        let entries_2 = mat_2.collect_entries();

        assert_eq!(entries_1.len(), 6);
        assert_eq!(entries_1[0].num.0, 1);
        assert!(entries_1[0].den.0 >= 2_i64.pow(62));
        assert_eq!(entries_1[1].num.0, 2);
        assert_eq!(entries_1[1].den.0, 1);
        assert!(entries_1[2].num.0 >= 2_i64.pow(62));
        assert_eq!(entries_1[2].den.0, 1);
        assert!(entries_1[3].num.0 >= 2_i64.pow(62));
        assert_eq!(entries_1[3].den.0, 1);
        assert_eq!(entries_1[4].num.0, 3);
        assert_eq!(entries_1[4].den.0, 4);
        assert_eq!(entries_1[5].num.0, 4);
        assert_eq!(entries_1[5].den.0, 1);

        assert_eq!(entries_2.len(), 2);
        assert_eq!(entries_2[0].num.0, -1);
        assert_eq!(entries_2[0].den.0, 1);
        assert_eq!(entries_2[1].num.0, -1);
        assert_eq!(entries_2[1].den.0, 2);
    }

    /// Ensures that all entries from the matrices are actually collected
    /// in [`MatQ::collect_entries_f64`].
    #[test]
    fn all_entries_collected_f64() {
        let mat_1 = MatQ::from_str(&format!(
            "[[1/{}, 2],[{}, 4/5],[-3/-4, {}]]",
            i64::MAX,
            i64::MAX,
            i64::MIN
        ))
        .unwrap();
        let mat_2 = MatQ::from_str("[[-1/1, 2/-4]]").unwrap();

        let entries_1 = mat_1.collect_entries_f64();
        let entries_2 = mat_2.collect_entries_f64();

        assert_eq!(entries_1.len(), 3);
        assert_eq!(entries_1[0].len(), 2);
        assert_eq!(entries_1[0][0], 1.0 / i64::MAX as f64);
        assert_eq!(entries_1[0][1], 2.0);
        assert!((entries_1[1][0] - i64::MAX as f64).abs() < 1_025.0);
        assert!((entries_1[1][1] - 0.8).abs() < 0.00001);
        assert_eq!(entries_1[2][0], 0.75);
        assert_eq!(entries_1[2][1], i64::MIN as f64);

        assert_eq!(entries_2.len(), 1);
        assert_eq!(entries_2[0].len(), 2);
        assert_eq!(entries_2[0][0], -1.0);
        assert_eq!(entries_2[0][1], -0.5);
    }
}