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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
// 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 [`MatPolynomialRingZq`] matrix.
use super::MatPolynomialRingZq;
use crate::{
integer::{MatPolyOverZ, PolyOverZ},
integer_mod_q::{ModulusPolynomialRingZq, PolynomialRingZq},
traits::{MatrixDimensions, MatrixGetEntry, MatrixGetSubmatrix},
};
use flint_sys::{fmpz_poly::fmpz_poly_struct, fmpz_poly_mat::fmpz_poly_mat_entry};
impl MatPolynomialRingZq {
/// Returns the modulus of the matrix as a [`ModulusPolynomialRingZq`].
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use qfall_math::integer::MatPolyOverZ;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let modulus = poly_ring_mat.get_mod();
/// ```
pub fn get_mod(&self) -> ModulusPolynomialRingZq {
self.modulus.clone()
}
}
impl MatPolynomialRingZq {
/// Creates a [`MatPolyOverZ`] where each entry is a representative of the
/// equivalence class of each entry from a [`MatPolynomialRingZq`].
///
/// The representation of the coefficients is in the range `[0, modulus)` and
/// the representation of the polynomials is in the range `[0, modulus_polynomial)`.
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use qfall_math::integer::MatPolyOverZ;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let matrix = poly_ring_mat.get_representative_least_nonnegative_residue();
///
/// let cmp_poly_mat = MatPolyOverZ::from_str("[[3 15 0 1, 1 8],[0, 2 1 2]]").unwrap();
/// assert_eq!(cmp_poly_mat, matrix);
/// ```
pub fn get_representative_least_nonnegative_residue(&self) -> MatPolyOverZ {
self.matrix.clone()
}
}
impl MatrixDimensions for MatPolynomialRingZq {
/// Returns the number of rows of the matrix as an [`i64`].
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use qfall_math::integer::MatPolyOverZ;
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let rows = poly_ring_mat.get_num_rows();
/// ```
fn get_num_rows(&self) -> i64 {
self.matrix.get_num_rows()
}
/// Returns the number of columns of the matrix as an [`i64`].
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use qfall_math::integer::MatPolyOverZ;
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let rows = poly_ring_mat.get_num_columns();
/// ```
fn get_num_columns(&self) -> i64 {
self.matrix.get_num_columns()
}
}
impl MatrixGetEntry<PolyOverZ> for MatPolynomialRingZq {
/// Outputs the [`PolyOverZ`] 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 [`PolyOverZ`] 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::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use qfall_math::integer::{MatPolyOverZ, PolyOverZ};
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 50").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let entry_1: PolyOverZ = unsafe { poly_ring_mat.get_entry_unchecked(1, 0) };
/// let entry_2: PolyOverZ = unsafe { poly_ring_mat.get_entry_unchecked(0, 1) };
///
///
/// assert_eq!(entry_1, PolyOverZ::from(0));
/// assert_eq!(entry_2, PolyOverZ::from(42));
/// ```
unsafe fn get_entry_unchecked(&self, row: i64, column: i64) -> PolyOverZ {
unsafe { self.matrix.get_entry_unchecked(row, column) }
}
}
impl MatrixGetEntry<PolynomialRingZq> for MatPolynomialRingZq {
/// Outputs the [`PolynomialRingZq`] 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 [`PolynomialRingZq`] 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::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq, PolynomialRingZq};
/// use qfall_math::integer::{MatPolyOverZ, PolyOverZ};
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 50").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let entry_1: PolynomialRingZq = unsafe { poly_ring_mat.get_entry_unchecked(0, 1) };
/// let entry_2: PolynomialRingZq = unsafe { poly_ring_mat.get_entry_unchecked(0, 1) };
///
/// let value_cmp = PolynomialRingZq::from((&PolyOverZ::from(42), &modulus));
/// assert_eq!(entry_1, value_cmp);
/// assert_eq!(entry_1, entry_2);
/// ```
unsafe fn get_entry_unchecked(&self, row: i64, column: i64) -> PolynomialRingZq {
PolynomialRingZq {
poly: unsafe { self.matrix.get_entry_unchecked(row, column) },
modulus: self.get_mod(),
}
}
}
impl MatrixGetSubmatrix for MatPolynomialRingZq {
/// 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::{integer::MatPolyOverZ, traits::MatrixGetSubmatrix};
/// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();();
/// let mat = MatPolyOverZ::identity(3, 3);
/// let poly_ring_mat = MatPolynomialRingZq::from((&mat, &modulus));
///
/// let sub_mat_1 = poly_ring_mat.get_submatrix(0, 2, 1, 1).unwrap();
/// let sub_mat_2 = poly_ring_mat.get_submatrix(0, -1, 1, -2).unwrap();
/// let sub_mat_3 = unsafe{poly_ring_mat.get_submatrix_unchecked(0, 3, 1, 2)};
///
/// let e_2 = MatPolyOverZ::from_str("[[0],[1 1],[0]]").unwrap();
/// let e_2 = MatPolynomialRingZq::from((&e_2, &modulus));
/// 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 {
MatPolynomialRingZq {
matrix: unsafe {
self.matrix
.get_submatrix_unchecked(row_1, row_2, col_1, col_2)
},
modulus: self.get_mod(),
}
}
}
impl MatPolynomialRingZq {
/// Efficiently collects all [`fmpz_poly_struct`]s in a [`MatPolynomialRingZq`] without cloning them.
///
/// Hence, the values on the returned [`Vec`] are intended for short-term use
/// as the access to [`fmpz_poly_struct`] values could lead to memory leaks or modified values
/// once the [`MatPolynomialRingZq`] instance was modified or dropped.
///
/// # Examples
/// ```compile_fail
/// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
/// use qfall_math::integer::MatPolyOverZ;
/// use std::str::FromStr;
///
/// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
/// let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[2 1 2, 3 1 1 1]]").unwrap();
/// let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
///
/// let fmpz_entries = poly_ring_mat.collect_entries();
/// ```
#[allow(dead_code)]
pub(crate) fn collect_entries(&self) -> Vec<fmpz_poly_struct> {
let mut entries: Vec<fmpz_poly_struct> =
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 { *fmpz_poly_mat_entry(&self.matrix.matrix, row, col) };
entries.push(entry);
}
}
entries
}
}
#[cfg(test)]
mod test_get_entry {
use crate::integer::{MatPolyOverZ, PolyOverZ};
use crate::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq, PolynomialRingZq};
use crate::traits::MatrixGetEntry;
use std::str::FromStr;
const LARGE_PRIME: u64 = u64::MAX - 58;
/// Ensure that getting entries works on the edge.
#[test]
fn get_edges() {
let modulus = ModulusPolynomialRingZq::from_str("2 42 1 mod 89").unwrap();
let matrix = MatPolynomialRingZq::new(5, 10, &modulus);
let entry_1: PolyOverZ = matrix.get_entry(0, 0).unwrap();
let entry_2: PolyOverZ = matrix.get_entry(4, 9).unwrap();
assert_eq!(PolyOverZ::default(), entry_1);
assert_eq!(PolyOverZ::default(), entry_2);
}
/// Ensure that getting entries works with large numbers.
#[test]
fn large_positive() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("5 42 17 1 2 1 mod {LARGE_PRIME}"))
.unwrap();
let poly_mat =
MatPolyOverZ::from_str(&format!("[[4 1 0 {} 1, 1 42],[0, 2 1 2]]", i64::MAX))
.unwrap();
let matrix = MatPolynomialRingZq::from((&poly_mat, &modulus));
let entry: PolyOverZ = matrix.get_entry(0, 0).unwrap();
assert_eq!(
PolyOverZ::from_str(&format!("4 1 0 {} 1", i64::MAX)).unwrap(),
entry
);
}
/// Ensure that a wrong number of rows yields an Error.
#[test]
fn error_wrong_row() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("5 42 17 1 2 1 mod {LARGE_PRIME}"))
.unwrap();
let matrix = MatPolynomialRingZq::new(5, 10, &modulus);
assert!(MatrixGetEntry::<PolynomialRingZq>::get_entry(&matrix, 5, 1).is_err());
assert!(MatrixGetEntry::<PolynomialRingZq>::get_entry(&matrix, -6, 1).is_err());
assert!(MatrixGetEntry::<PolyOverZ>::get_entry(&matrix, 5, 1).is_err());
assert!(MatrixGetEntry::<PolyOverZ>::get_entry(&matrix, -6, 1).is_err());
}
/// Ensure that a wrong number of columns yields an Error.
#[test]
fn error_wrong_column() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("5 42 17 1 2 1 mod {LARGE_PRIME}"))
.unwrap();
let matrix = MatPolynomialRingZq::new(5, 10, &modulus);
assert!(MatrixGetEntry::<PolynomialRingZq>::get_entry(&matrix, 1, 10).is_err());
assert!(MatrixGetEntry::<PolynomialRingZq>::get_entry(&matrix, 1, -11).is_err());
assert!(MatrixGetEntry::<PolyOverZ>::get_entry(&matrix, 1, 10).is_err());
assert!(MatrixGetEntry::<PolyOverZ>::get_entry(&matrix, 1, -11).is_err());
}
/// Ensure that getting entries works with different types.
#[test]
fn diff_types() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("5 42 17 1 2 1 mod {LARGE_PRIME}"))
.unwrap();
let matrix = MatPolynomialRingZq::new(5, 10, &modulus);
let _: PolyOverZ = matrix.get_entry(0, 0).unwrap();
let _: PolynomialRingZq = matrix.get_entry(0, 0).unwrap();
}
}
#[cfg(test)]
mod test_get_num {
use crate::{
integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq},
traits::MatrixDimensions,
};
use std::str::FromStr;
/// Ensure that the getter for number of rows works correctly.
#[test]
fn num_rows() {
let modulus = ModulusPolynomialRingZq::from_str("2 42 1 mod 89").unwrap();
let matrix = MatPolynomialRingZq::new(5, 10, &modulus);
assert_eq!(matrix.get_num_rows(), 5);
}
/// Ensure that the getter for number of columns works correctly.
#[test]
fn num_columns() {
let modulus = ModulusPolynomialRingZq::from_str("2 42 1 mod 89").unwrap();
let matrix = MatPolynomialRingZq::new(5, 10, &modulus);
assert_eq!(matrix.get_num_columns(), 10);
}
}
#[cfg(test)]
mod test_mod {
use crate::{
integer::MatPolyOverZ,
integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq},
};
use std::str::FromStr;
const LARGE_PRIME: u64 = u64::MAX - 58;
/// Ensure that the getter for modulus works correctly.
#[test]
fn get_mod() {
let modulus = ModulusPolynomialRingZq::from_str("2 42 1 mod 89").unwrap();
let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
let matrix = MatPolynomialRingZq::from((&poly_mat, &modulus));
assert_eq!(
matrix.get_mod(),
ModulusPolynomialRingZq::from_str("2 42 1 mod 89").unwrap()
);
}
/// Ensure that the getter for modulus works with large numbers.
#[test]
fn get_mod_large() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("2 42 1 mod {LARGE_PRIME}")).unwrap();
let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
let matrix = MatPolynomialRingZq::from((&poly_mat, &modulus));
assert_eq!(
matrix.get_mod(),
ModulusPolynomialRingZq::from_str(&format!("2 42 1 mod {LARGE_PRIME}")).unwrap()
);
}
/// Ensure that no memory leak occurs in get_mod.
#[test]
fn get_mod_memory() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("2 42 1 mod {LARGE_PRIME}")).unwrap();
let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[0, 2 1 2]]").unwrap();
let matrix = MatPolynomialRingZq::from((&poly_mat, &modulus));
let _ = matrix.get_mod();
let _ = ModulusPolynomialRingZq::from_str(&format!("2 42 1 mod {LARGE_PRIME}")).unwrap();
let modulus = matrix.get_mod();
assert_eq!(
modulus,
ModulusPolynomialRingZq::from_str(&format!("2 42 1 mod {LARGE_PRIME}")).unwrap()
);
}
}
#[cfg(test)]
mod test_get_representative_least_nonnegative_residue {
use crate::{
integer::MatPolyOverZ,
integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq},
};
use std::str::FromStr;
const LARGE_PRIME: u64 = u64::MAX - 58;
/// Ensure that the getter for a large modulus and large entries works.
#[test]
fn get_representative_least_nonnegative_residue_large_entry_and_modulus() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("5 42 0 0 0 1 mod {LARGE_PRIME}")).unwrap();
let poly_mat = MatPolyOverZ::from_str("[[4 1 0 0 1, 1 42],[0, 1 -1]]").unwrap();
let matrix = MatPolynomialRingZq::from((&poly_mat, &modulus));
assert_eq!(
MatPolyOverZ::from_str(&format!(
"[[4 1 0 0 1, 1 42],[0, 1 {}]]",
LARGE_PRIME - 1
))
.unwrap(),
matrix.get_representative_least_nonnegative_residue()
)
}
}
#[cfg(test)]
mod test_get_vec {
use crate::{
integer::MatPolyOverZ,
integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq},
traits::MatrixGetSubmatrix,
};
use std::str::FromStr;
/// Ensure that getting a row works.
#[test]
fn get_row_works() {
let matrix = MatPolyOverZ::from_str(&format!(
"[[0, 0, 0],[1 42, 1 {}, 1 {}]]",
i64::MAX,
i64::MIN
))
.unwrap();
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let matrix = MatPolynomialRingZq::from((&matrix, &modulus));
let row_1 = matrix.get_row(0).unwrap();
let row_2 = matrix.get_row(1).unwrap();
let cmp_1 = MatPolyOverZ::from_str("[[0, 0, 0]]").unwrap();
let cmp_2 = MatPolyOverZ::from_str(&format!("[[1 42, 1 {}, 1 {}]]", i64::MAX, i64::MIN))
.unwrap();
let cmp_1 = MatPolynomialRingZq::from((&cmp_1, &modulus));
let cmp_2 = MatPolynomialRingZq::from((&cmp_2, &modulus));
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 = MatPolyOverZ::from_str(&format!(
"[[0, 0, 0],[1 42, 1 {}, 1 {}]]",
i64::MAX,
u64::MAX - 1
))
.unwrap();
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let matrix = MatPolynomialRingZq::from((&matrix, &modulus));
let row_1 = matrix.get_row(-2).unwrap();
let row_2 = matrix.get_row(-1).unwrap();
let cmp_1 = MatPolyOverZ::from_str("[[0, 0, 0]]").unwrap();
let cmp_2 =
MatPolyOverZ::from_str(&format!("[[1 42, 1 {}, 1 {}]]", i64::MAX, u64::MAX - 1))
.unwrap();
assert_eq!(cmp_1, row_1.matrix);
assert_eq!(cmp_2, row_2.matrix);
}
/// Ensure that getting a column works.
#[test]
fn get_column_works() {
let matrix = MatPolyOverZ::from_str(&format!(
"[[1 42, 0, 2 17 42],[1 {}, 0, 2 17 42],[1 {}, 0, 2 17 42]]",
i64::MAX,
i64::MIN
))
.unwrap();
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let matrix = MatPolynomialRingZq::from((&matrix, &modulus));
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 =
MatPolyOverZ::from_str(&format!("[[1 42],[1 {}],[1 {}]]", i64::MAX, i64::MIN))
.unwrap();
let cmp_2 = MatPolyOverZ::from_str("[[0],[0],[0]]").unwrap();
let cmp_3 = MatPolyOverZ::from_str("[[2 17 42],[2 17 42],[2 17 42]]").unwrap();
let cmp_1 = MatPolynomialRingZq::from((&cmp_1, &modulus));
let cmp_2 = MatPolynomialRingZq::from((&cmp_2, &modulus));
let cmp_3 = MatPolynomialRingZq::from((&cmp_3, &modulus));
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 = MatPolyOverZ::from_str(&format!(
"[[1 42, 0, 2 17 42],[1 {}, 0, 2 17 42],[1 {}, 0, 2 17 42]]",
i64::MAX,
u64::MAX - 1
))
.unwrap();
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let matrix = MatPolynomialRingZq::from((&matrix, &modulus));
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 = MatPolyOverZ::from_str(&format!(
"[[1 42],[1 {}],[1 {}]]",
i64::MAX,
u64::MAX - 1
))
.unwrap();
let cmp_2 = MatPolyOverZ::from_str("[[0],[0],[0]]").unwrap();
let cmp_3 = MatPolyOverZ::from_str("[[2 17 42],[2 17 42],[2 17 42]]").unwrap();
assert_eq!(cmp_1, column_1.matrix);
assert_eq!(cmp_2, column_2.matrix);
assert_eq!(cmp_3, column_3.matrix);
}
/// Ensure that wrong row and column dimensions yields an error.
#[test]
fn wrong_dim_error() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let matrix = MatPolyOverZ::from_str(&format!(
"[[1 17, 2 17 42, 3 1 1 1],[1 {}, 1 1, 2 2 3],[1 {}, 1 142, 1 1]]",
i64::MAX,
i64::MIN
))
.unwrap();
let matrix = MatPolynomialRingZq::from((&matrix, &modulus));
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::{MatPolyOverZ, Z},
integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq},
traits::{MatrixDimensions, MatrixGetSubmatrix},
};
use std::str::FromStr;
/// Ensures that getting the entire matrix as a submatrix works.
#[test]
fn entire_matrix() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(5, 5);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
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 modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(5, 5);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
let sub_mat = mat.get_submatrix(0, 0, 0, 0).unwrap();
let cmp_mat = MatPolyOverZ::identity(1, 1);
let cmp_mat = MatPolynomialRingZq::from((&cmp_mat, &modulus));
assert_eq!(cmp_mat, sub_mat);
}
/// Ensures that the dimensions of the submatrix are correct.
#[test]
fn correct_dimensions() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(100, 100);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
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 modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::from_str(&format!(
"[[2 -1 {}, 1 2, 1 3],[1 1, 1 {}, 1 3]]",
i64::MAX,
i64::MIN
))
.unwrap();
let mat = MatPolynomialRingZq::from((&mat, &modulus));
let sub_mat = mat.get_submatrix(0, 1, 0, 1).unwrap();
let cmp_mat = MatPolyOverZ::from_str(&format!(
"[[2 -1 {}, 1 2],[1 1, 1 {}]]",
i64::MAX,
i64::MIN
))
.unwrap();
let cmp_mat = MatPolynomialRingZq::from((&cmp_mat, &modulus));
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 modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(10, 10);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
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 modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(3, 3);
let matrix = MatPolynomialRingZq::from((&mat, &modulus));
assert_eq!(matrix, matrix.get_submatrix(0, -1, 0, -1).unwrap());
assert_eq!(matrix, matrix.get_submatrix(-3, -1, -3, -1).unwrap());
}
/// Ensures that the function panics if no columns of the matrix are addressed.
#[test]
#[should_panic]
fn no_columns() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(10, 10);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
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 modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(10, 10);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
let _ = mat.get_submatrix(5, 4, 0, 0);
}
/// Ensure that the submatrix function can be called with several types.
#[test]
fn availability() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
let mat = MatPolyOverZ::identity(10, 10);
let mat = MatPolynomialRingZq::from((&mat, &modulus));
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 crate::integer::{MatPolyOverZ, PolyOverZ};
use crate::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
use flint_sys::fmpz_poly::fmpz_poly_set;
use std::str::FromStr;
const LARGE_PRIME: u64 = u64::MAX - 58;
/// Ensures that all entries of the polynomial are actually collected in the vector.
#[test]
fn all_entries_collected() {
let modulus =
ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {LARGE_PRIME}")).unwrap();
let poly_mat_1 = MatPolyOverZ::from_str(&format!(
"[[4 -1 0 3 1, 1 {}],[2 1 2, 3 {} 1 1]]",
i64::MAX,
i64::MIN + 58,
))
.unwrap();
let poly_ring_mat_1 = MatPolynomialRingZq::from((&poly_mat_1, &modulus));
let poly_mat_2 = MatPolyOverZ::from_str("[[1 42, 2 1 17]]").unwrap();
let poly_ring_mat_2 = MatPolynomialRingZq::from((&poly_mat_2, &modulus));
let entries_1 = poly_ring_mat_1.collect_entries();
let entries_2 = poly_ring_mat_2.collect_entries();
let mut entry_1 = PolyOverZ::default();
let mut entry_2 = entry_1.clone();
let mut entry_3 = entry_1.clone();
unsafe { fmpz_poly_set(&mut entry_1.poly, &entries_1[1]) }
unsafe { fmpz_poly_set(&mut entry_2.poly, &entries_1[3]) }
unsafe { fmpz_poly_set(&mut entry_3.poly, &entries_2[0]) }
assert_eq!(entries_1.len(), 4);
assert_eq!(
PolyOverZ::from_str(&format!("1 {}", i64::MAX)).unwrap(),
entry_1
);
assert_eq!(
PolyOverZ::from_str(&format!("3 {} 1 1", i64::MAX)).unwrap(),
entry_2
);
assert_eq!(entries_2.len(), 2);
assert_eq!(PolyOverZ::from(42), entry_3);
}
/// Ensure that the doc-test compiles and works correctly.
#[test]
fn doc_test() {
let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
let poly_mat = MatPolyOverZ::from_str("[[4 -1 0 1 1, 1 42],[2 1 2, 3 1 1 1]]").unwrap();
let poly_ring_mat = MatPolynomialRingZq::from((&poly_mat, &modulus));
let _ = poly_ring_mat.collect_entries();
}
}