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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use crate::{matrix::Number, GF2Matrix};
#[derive(Clone, Copy, Debug)]
pub enum BitOrder {
LSB,
MSB,
}
/// An intermediate matrix representation where each row is encoded
/// as an integer value.
///
/// Each element of `elements` represents one row of the matrix,
/// with its bits encoding the entries of that row.
///
/// This type is typically used as a compact or efficient
/// representation before expanding into an explicit GF(2) matrix.
#[derive(Clone)]
pub struct PackedGF2Matrix<T: Number> {
elements: Vec<T>,
n: usize,
}
impl<T: Number> PackedGF2Matrix<T> {
/// Creates a new integer-encoded matrix.
///
/// # Arguments
///
/// * `elements` - A vector where each element encodes one row as bits.
/// * `n` - The number of columns (bits) per row.
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
///
/// let m = PackedGF2Matrix::new(vec![0b1011u8, 0b0101u8], 4);
/// ```
pub fn new(elements: Vec<T>, n: usize) -> Self {
Self {
elements: elements,
n: n,
}
}
fn get_packed_bit(value: T, len: usize, idx: usize) -> u8 {
((value.into_usize() >> (len - 1 - idx)) & 1) as u8
}
fn toggle_packed_bit(value: &mut T, len: usize, idx: usize) {
*value = *value ^ (T::one() << (len - 1 - idx));
}
/// Returns the number of rows in the matrix.
///
/// # Returns
///
/// number of rows: this is equal to the number of elements stored internally.
pub fn nrows(&self) -> usize {
self.elements.len()
}
/// Returns the number of columns in the matrix.
///
/// # Returns
///
/// Number of columns: this corresponds to the number of bits extracted from each row.
pub fn ncols(&self) -> usize {
self.n
}
/// Returns the integer-encoded value of a specific row.
///
/// # Arguments
///
/// * `row_index`- usize, index of the row.
///
/// # Returns
///
/// Row corresponding to index `row_index`
///
/// # Panics
///
/// Panics if `row_index` is out of bounds.
pub fn row(&self, row_index: usize) -> T {
self.elements[row_index]
}
/// Converts the integer-encoded matrix into an explicit GF(2) matrix.
///
/// Each row is expanded into a vector of bits (`0` or `1`),
/// according to the specified bit order.
///
/// # Bit Order
///
/// - `BitOrder::LSB`: the least-significant bit is placed first
/// (column 0 corresponds to bit 0).
/// - `BitOrder::MSB`: the most-significant bit is placed first
/// (column 0 corresponds to bit `n - 1`).
///
/// # Returns
///
/// A `GF2Matrix` whose entries are elements of GF(2),
/// represented as `u8` values (`0` or `1`).
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::{PackedGF2Matrix, BitOrder};
///
/// let m = PackedGF2Matrix::new(vec![0b0010u8], 4);
/// let gf2 = m.from_int_matrix_to_gf2_matrix(BitOrder::LSB);
///
/// // Result: [[0, 1, 0, 0]]
/// ```
pub fn from_int_matrix_to_gf2_matrix(&self, bit_order: BitOrder) -> GF2Matrix {
let mut matrix_elements = Vec::with_capacity(self.nrows());
match bit_order {
BitOrder::LSB => {
for &row in &self.elements {
let mut r = Vec::with_capacity(self.n);
for i in 0..self.n {
r.push((((row >> i) & T::one()) != T::zero()) as u8);
}
matrix_elements.push(r);
}
}
BitOrder::MSB => {
for &row in &self.elements {
let mut r = Vec::with_capacity(self.n);
for i in (0..self.n).rev() {
r.push((((row >> i) & T::one()) != T::zero()) as u8);
}
matrix_elements.push(r);
}
}
};
GF2Matrix::new(matrix_elements)
}
/// Creates a one-row bit-packed matrix from an owned vector of row data.
///
/// Each element of `vect` is interpreted as one packed row of the matrix.
/// The number of columns is inferred from the length of the vector.
///
/// # Parameters
///
/// - `vect`: the vector containing the packed row elements.
///
/// # Returns
///
/// A `PackedGF2Matrix` whose internal storage is initialized from `vect`.
///
/// # Notes
///
/// - The input vector is moved into the matrix without cloning.
/// - The resulting matrix has `vect.len()` columns in its packed representation.
///
pub fn from_vec(vect: Vec<T>) -> Self {
let n = vect.len();
Self {
elements: vect,
n: n,
}
}
/// Creates a one-row bit-packed matrix from a referenced vector of row data.
///
/// Each element of `vect` is interpreted as one packed row of the matrix.
/// The number of columns is inferred from the length of the vector.
///
/// # Parameters
///
/// - `vect`: a reference to the vector containing the packed row elements.
///
/// # Returns
///
/// A `PackedGF2Matrix` whose internal storage is initialized from a clone
/// of `vect`.
///
/// # Notes
///
/// - The input vector is cloned using `to_vec()`.
/// - The resulting matrix owns its internal storage independently of the
/// original vector.
/// - The resulting matrix has `vect.len()` columns in its packed representation.
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let rows = vec![0b1010u8, 0b0110u8];
/// let m = PackedGF2Matrix::from_vec_referenced(&rows);
///
/// assert_eq!(m.ncols(), 2);
/// ```
pub fn from_vec_referenced(vect: &Vec<T>) -> Self {
let n = vect.len();
Self {
elements: vect.to_vec(),
n: n,
}
}
fn get_element(&self, row: usize, col: usize) -> u8 {
((self.elements[row].into_usize() >> (self.ncols() - 1 - col)) & 1) as u8
}
fn swap_rows(&mut self, idx1: usize, idx2: usize) {
self.elements.swap(idx1, idx2);
}
/// Reduces this matrix in place to row-reduced echelon form over GF(2).
///
/// This method modifies the matrix directly and returns the ordered list of row
/// operations used during elimination.
///
/// # Row Operations
///
/// The returned operations are ordered and can be applied to the original matrix
/// to reproduce the final state of `self`.
///
/// The following row operations may be recorded:
///
/// - `(i, j)` with `i != j` represents adding row `j` to row `i`: `row_i <- row_i + row_j`.
/// - A row swap between rows `r` and `i` is encoded as three row additions:
/// `(r, i)`, `(i, r)`, `(r, i)`.
///
/// # Notes
/// - This method does not clone the matrix.
/// - The original contents of the matrix are overwritten.
/// - This method allocates a new `Vec` for the returned operations.
/// - If you want to reuse the operations vector allocation across repeated
/// calls, use [`Self::echelon_form_in_place_with_ops`].
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let mut m = PackedGF2Matrix::new(vec![0b110u8, 0b101u8, 0b011u8], 3);
///
/// let ops = m.echelon_form_in_place();
///
/// // `m` is now in row-reduced echelon form.
/// // `ops` contains the recorded row operations.
/// ```
pub fn echelon_form_in_place(&mut self) -> Vec<(usize, usize)> {
let mut operations = Vec::new();
self.echelon_form_in_place_with_ops(&mut operations);
operations
}
/// Reduces this matrix in place to row-reduced echelon form over GF(2),
/// reusing the provided operations buffer.
/// This method is intended for performance-sensitive code that repeatedly
/// computes row-reduced echelon forms and wants to avoid allocating a new
/// operations vector on every call.
///
/// The provided `operations` vector is cleared before new operations are
/// recorded. Its existing capacity is retained.
///
/// # Row operations
///
/// The recorded operations are ordered and can be applied to the original matrix
/// to reproduce the final state of `self`.
///
/// The following row operations may be recorded:
///
/// - `(i, j)` with `i != j` represents adding row `j` to row `i`: `row_i <- row_i + row_j`.
/// - A row swap between rows `r` and `i` is encoded as three row additions:
/// `(r, i)`, `(i, r)`, `(r, i)`.
///
/// # Notes
///
/// - This method does not clone the matrix.
/// - The original contents of the matrix are overwritten.
/// - `operations.clear()` is called before recording new operations.
/// - Clearing the vector removes old operations but keeps the allocated capacity for reuse.
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let original = PackedGF2Matrix::new(vec![0b110u8, 0b101u8, 0b011u8], 3);
/// let mut work = original.clone();
/// let mut ops = Vec::new();
///
/// work.clone_from(&original);
/// work.echelon_form_in_place_with_ops(&mut ops);
/// // `work` is now in row-reduced echelon form.
/// // `ops` contains the recorded row operations.
/// ```
pub fn echelon_form_in_place_with_ops(&mut self, operations: &mut Vec<(usize, usize)>) {
operations.clear();
let mut lead = 0;
for r in 0..self.nrows() {
if lead >= self.ncols() {
break;
}
let mut i = r;
while self.get_element(i, lead) == 0 {
i += 1;
if i == self.nrows() {
i = r;
lead += 1;
if lead == self.ncols() {
return;
}
}
}
self.swap_rows(r, i);
if r != i {
operations.push((r, i));
operations.push((i, r));
operations.push((r, i));
}
for i in 0..self.nrows() {
if i != r && self.get_element(i, lead) == 1 {
self.elements[i] = self.elements[i] ^ self.elements[r];
operations.push((i, r));
}
}
lead += 1;
}
}
/// Converts this matrix into row-reduced echelon form over GF(2).
///
/// This method consumes the matrix, performs Gauss-Jordan-style elimination
/// in place, and returns the transformed matrix together with the recorded row operations.
///
/// Because this method takes ownership of `self`, it does not clone the matrix.
/// If the original matrix must be preserved, clone it before calling this method or use [`Self::echelon_form`].
///
/// # Returns
///
/// A pair `(reduced_matrix, operations)` where:
///
/// - `reduced_matrix` is the row-reduced echelon form of the matrix.
/// - `operations` is the ordered list of row operations used to obtain it.
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let m = PackedGF2Matrix::new(vec![0b110u8, 0b101u8, 0b011u8], 3);
/// let (rref, ops) = m.into_echelon_form();
///
/// // `m` has been consumed.
/// // `rref` contains the transformed matrix over GF(2).
/// ```
pub fn into_echelon_form(mut self) -> (Self, Vec<(usize, usize)>) {
let operations = self.echelon_form_in_place();
(self, operations)
}
/// Computes a row-reduced echelon form of the bit-packed matrix over GF(2).
///
/// This method does not modify the original matrix. It clones `self`, performs
/// elimination on the clone, and returns the transformed matrix together with
/// the sequence of elementary row operations used during elimination.
///
/// Over GF(2), row addition is implemented as XOR, and every nonzero pivot is
/// equal to `1`.
///
/// # Row Operations
///
/// The returned operations are ordered and can be applied to the original matrix
/// to reproduce the returned echelon matrix.
///
/// The following row operations may be recorded:
///
/// - `(i, j)` with `i != j` represents adding row `j` to row `i`:
/// `row_i <- row_i + row_j`.
/// - A row swap between rows `r` and `i` is encoded as three row additions:
/// `(r, i)`, `(i, r)`, `(r, i)`.
///
/// This swap encoding is valid over GF(2), where row addition is XOR.
///
/// # Returns
///
/// A pair `(echelon_matrix, operations)` where:
///
/// - `echelon_matrix` is the transformed matrix.
/// - `operations` is the list of row operations used to obtain it.
///
/// # Notes
/// - The original matrix is not modified.
/// - This method allocates a cloned matrix.
/// - If you want to avoid cloning, use [`Self::into_echelon_form`] or [`Self::echelon_form_in_place`].
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let m = PackedGF2Matrix::new(vec![0b110u8, 0b101u8, 0b011u8], 3);
///
/// let (echelon, ops) = m.echelon_form();
///
/// // `m` is unchanged.
/// // `echelon` contains the transformed matrix over GF(2).
/// // `ops` contains the recorded row operations.
/// ```
pub fn echelon_form(&self) -> (Self, Vec<(usize, usize)>) {
self.clone().into_echelon_form()
}
fn get_pivot(&self, row: usize) -> Option<usize> {
for col in 0..self.ncols() {
if self.get_element(row, col) == 1 {
return Some(col);
}
}
None
}
/// Checks if a GF(2) matrix is in reduced row echelon form (RREF).
///
/// # Returns
/// `true` if the matrix is in reduced row echelon form; otherwise, `false`.
pub fn is_reduced_echelon(&self) -> bool {
let mut previous_pivot: Option<usize> = None;
let mut seen_zero_row = false;
for row in 0..self.nrows() {
match self.get_pivot(row) {
None => {
seen_zero_row = true;
}
Some(pivot) => {
if seen_zero_row {
return false;
}
if let Some(previous) = previous_pivot {
if pivot <= previous {
return false;
}
}
previous_pivot = Some(pivot);
}
}
}
true
}
/// Computes the rank of the linear applocation represented by a packed GF(2) matrix which is already in echelon form.
///
/// # Returns
/// An integer representing the rank of the matrix.
///
pub fn rank_echelon(&self) -> usize {
self.elements
.iter()
.filter(|&&row| row != T::zero())
.count()
}
/// Computes the rank of the linear applocation represented by a GF(2) Bitpacked matrix.
///
/// It first converts the matrix to its RREF before computing the rank.
///
/// # Returns
/// An integer representing the rank of the matrix.
///
pub fn rank(&self) -> usize {
let echelon = self.echelon_form();
echelon.0.rank_echelon()
}
/// Computes the rank of the linear applocation represented by a packed GF(2) matrix if the matrix is already in echelon form.
/// If the matrix is not in reduces echelon form, it returns None.
/// # Returns
/// An integer representing the rank of the matrix.
///
pub fn rank_if_echelon(&self) -> Option<usize> {
if self.is_reduced_echelon() {
Some(self.rank_echelon())
} else {
None
}
}
fn get_value_element(&self, value: T, col: usize) -> u8 {
Self::get_packed_bit(value, self.ncols(), col)
}
fn toggle_value_bit(&self, value: &mut T, col: usize) {
Self::toggle_packed_bit(value, self.ncols(), col);
}
/// Computes a basis of the kernel of a bit-packed GF(2) matrix already in reduced echelon form.
///
/// The returned vectors are also bit-packed using MSB order.
pub fn kernel_echelon_form(&self) -> Vec<T> {
assert!(
self.is_reduced_echelon(),
"kernel_echelon_form expects the matrix to be in echelon form"
);
let cols = self.ncols();
let mut pivots: Vec<(usize, usize)> = Vec::new(); // (pivot_col, pivot_row)
let mut is_pivot_col = vec![false; cols];
for row in 0..self.nrows() {
if let Some(pivot_col) = self.get_pivot(row) {
pivots.push((pivot_col, row));
is_pivot_col[pivot_col] = true;
}
}
let mut kernel_basis: Vec<T> = Vec::new();
for free_col in 0..cols {
if is_pivot_col[free_col] {
continue;
}
let mut kernel_vector = T::zero();
// Set the free variable to 1.
self.toggle_value_bit(&mut kernel_vector, free_col);
// Solve pivot variables by back-substitution.
for &(pivot_col, pivot_row) in pivots.iter().rev() {
let mut sum = 0u8;
for col in (pivot_col + 1)..cols {
let a = self.get_element(pivot_row, col);
let x = self.get_value_element(kernel_vector, col);
sum ^= a & x;
}
if sum == 1 {
self.toggle_value_bit(&mut kernel_vector, pivot_col);
}
}
kernel_basis.push(kernel_vector);
}
kernel_basis
}
/// Computes a basis of the kernel of any packed GF(2) matrix.
pub fn kernel(&self) -> Vec<T> {
let (echelon, _) = self.echelon_form();
echelon.kernel_echelon_form()
}
/// Computes a basis for the image of a bit-packed GF(2) matrix already in
/// reduced row echelon form.
///
/// The returned basis vectors are the nonzero rows of the matrix, stored in the
/// same bit-packed row representation used internally.
///
/// # Returns
///
/// A vector of bit-packed rows, each representing a basis vector of the image.
///
/// # Panics
///
/// Panics if the matrix is not in reduced row echelon form.
///
/// # Notes
///
/// - This method does not clone the matrix.
/// - This method assumes that nonzero rows of the reduced matrix form the image
/// basis.
/// - Use [`Self::image`] if the matrix may not already be reduced.
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let m = PackedGF2Matrix::new(vec![0b100u8, 0b010u8, 0b000u8], 3);
///
/// let image_basis = m.image_echelon_form();
///
/// assert_eq!(image_basis, vec![0b100u8, 0b010u8]);
/// ```
pub fn image_echelon_form(&self) -> Vec<T> {
assert!(
self.is_reduced_echelon(),
"image_echelon_form expects the matrix to be in reduced echelon form"
);
self.elements
.iter()
.copied()
.filter(|&row| row != T::zero())
.collect()
}
/// Computes a basis for the image of the linear map represented by this
/// bit-packed GF(2) matrix.
///
/// If the matrix is not already in reduced row echelon form, this method first
/// computes its echelon form. Despite the historical name `echelon_form`, that
/// method performs Gauss-Jordan-style elimination and returns a row-reduced
/// echelon form.
///
/// The returned basis vectors are stored in the same bit-packed row
/// representation used internally by the matrix.
///
/// # Returns
///
/// A vector of bit-packed rows, each representing a basis vector of the image.
///
/// # Notes
///
/// - The original matrix is not modified.
/// - If the matrix is already in reduced echelon form, no matrix clone is
/// needed to extract the image basis.
/// - If the matrix is not reduced, this method allocates a transformed matrix
/// by calling [`Self::echelon_form`].
/// - Zero rows are ignored.
/// - Nonzero rows of the reduced matrix form the returned basis.
///
/// # Example
///
/// ```rust
/// # use lin_algebra::packed_gf2_matrix::PackedGF2Matrix;
/// let m = PackedGF2Matrix::new(vec![0b110u8, 0b101u8, 0b011u8], 3);
///
/// let image_basis = m.image();
///
/// // Each entry of `image_basis` is a bit-packed row.
/// ```
pub fn image(&self) -> Vec<T> {
if self.is_reduced_echelon() {
self.image_echelon_form()
} else {
let (echelon, _) = self.echelon_form();
echelon.image_echelon_form()
}
}
/// Applies a sequence of GF(2) row operations to a packed vector.
///
/// The packed vector is interpreted as having length `len`.
///
/// Each operation `(i, j)` represents:
///
/// `v_i <- v_i + v_j`
///
/// Since the entries are in GF(2), addition is XOR.
fn apply_operations_packed(operations: &[(usize, usize)], mut value: T, len: usize) -> T {
for &(i, j) in operations {
if Self::get_packed_bit(value, len, j) == 1 {
Self::toggle_packed_bit(&mut value, len, i);
}
}
value
}
/// Returns `true` if all bits from `start` to `len - 1` are zero.
fn packed_suffix_is_zero(value: T, len: usize, start: usize) -> bool {
for idx in start..len {
if Self::get_packed_bit(value, len, idx) == 1 {
return false;
}
}
true
}
/// Keeps the first `new_len` bits of a packed vector of length `old_len`.
///
/// This is useful after elimination, where the first `self.ncols()` entries
/// contain the solution and the remaining entries correspond to zero rows.
fn truncate_packed_prefix(value: T, old_len: usize, new_len: usize) -> T {
let mut result = T::zero();
for idx in 0..new_len {
if Self::get_packed_bit(value, old_len, idx) == 1 {
Self::toggle_packed_bit(&mut result, new_len, idx);
}
}
result
}
/// Returns the column of the bit-packed matrix at index `idx`, packed as a
/// value of type `T`.
///
/// The returned packed vector has length `self.nrows()`.
pub fn column_packed(&self, idx: usize) -> T {
assert!(
idx < self.ncols(),
"column index out of bounds: index is {}, but matrix has {} columns",
idx,
self.ncols()
);
let mut column = T::zero();
for row in 0..self.nrows() {
if self.get_element(row, idx) == 1 {
Self::toggle_packed_bit(&mut column, self.nrows(), row);
}
}
column
}
/// Solves the linear system `A * x = b` over GF(2), where `A` is this
/// bit-packed matrix and `b` is a packed right-hand side vector.
///
/// The right-hand side vector `b` is interpreted as a packed vector of length
/// `self.nrows()`. The returned solution vector is packed as a value of type
/// `T` with length `self.ncols()`.
///
/// # Returns
///
/// A packed integer representing the solution vector `x`.
///
/// # Panics
///
/// Panics if:
///
/// - the matrix does not have full column rank.
/// - the system is inconsistent.
pub fn solve(&self, b: T) -> T {
let (echelon, operations) = self.echelon_form();
let rank = echelon.rank_echelon();
if rank < self.ncols() {
panic!("Matrix must have full rank");
}
let solved_b = Self::apply_operations_packed(&operations, b, self.nrows());
if !Self::packed_suffix_is_zero(solved_b, self.nrows(), rank) {
panic!("Linear system is inconsistent");
}
Self::truncate_packed_prefix(solved_b, self.nrows(), self.ncols())
}
/// Solves the matrix equation `A * X = Y` over GF(2), where `A` is this
/// bit-packed matrix and `Y` is a bit-packed right-hand side matrix.
///
/// The returned solution matrix `X` is also bit-packed.
///
/// If `A` has shape `m x n` and `Y` has shape `m x k`, then the returned
/// matrix has shape `n x k`.
///
/// # Returns
///
/// A bit-packed matrix `X` such that `self * X = y`.
///
/// # Panics
///
/// Panics if:
///
/// - `self.nrows() != y.nrows()`.
/// - the matrix does not have full column rank.
/// - the system is inconsistent for at least one column of `y`.
pub fn solve_matrix_system(&self, y: &PackedGF2Matrix<T>) -> PackedGF2Matrix<T> {
assert_eq!(
self.nrows(),
y.nrows(),
"left-hand side and right-hand side must have the same number of rows"
);
let (echelon, operations) = self.echelon_form();
let rank = echelon.rank_echelon();
if rank < self.ncols() {
panic!("Matrix must have full rank");
}
let n_rows = self.ncols(); // rows of X
let n_cols = y.ncols(); // columns of X
let mut solution_rows = vec![T::zero(); n_rows];
for col in 0..n_cols {
let rhs_col = y.column_packed(col);
let solved_col = Self::apply_operations_packed(&operations, rhs_col, self.nrows());
if !Self::packed_suffix_is_zero(solved_col, self.nrows(), rank) {
panic!("Linear system is inconsistent");
}
let solution_col = Self::truncate_packed_prefix(solved_col, self.nrows(), self.ncols());
for row in 0..n_rows {
if Self::get_packed_bit(solution_col, n_rows, row) == 1 {
Self::toggle_packed_bit(&mut solution_rows[row], n_cols, col);
}
}
}
PackedGF2Matrix::new(solution_rows, n_cols)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_int_matrix_to_gf2_matrix_u8_lsb() {
let elements = vec![0, 1, 2, 4, 8];
let int_matrix = PackedGF2Matrix::<u8>::new(elements.clone(), 4);
let gf2_matrix = int_matrix.from_int_matrix_to_gf2_matrix(BitOrder::LSB);
let expected = vec![
vec![0, 0, 0, 0],
vec![1, 0, 0, 0],
vec![0, 1, 0, 0],
vec![0, 0, 1, 0],
vec![0, 0, 0, 1],
];
assert_eq!(gf2_matrix.elements, expected);
let elements = vec![0, 1, 2, 3, 5];
let int_matrix = PackedGF2Matrix::<u8>::new(elements.clone(), 4);
let gf2_matrix = int_matrix.from_int_matrix_to_gf2_matrix(BitOrder::LSB);
let expected = vec![
vec![0, 0, 0, 0],
vec![1, 0, 0, 0],
vec![0, 1, 0, 0],
vec![1, 1, 0, 0],
vec![1, 0, 1, 0],
];
assert_eq!(gf2_matrix.elements, expected);
}
#[test]
fn test_from_int_matrix_to_gf2_matrix_u8_msb() {
let elements = vec![0, 1, 2, 4, 8];
let int_matrix = PackedGF2Matrix::<u8>::new(elements.clone(), 4);
let gf2_matrix = int_matrix.from_int_matrix_to_gf2_matrix(BitOrder::MSB);
let expected = vec![
vec![0, 0, 0, 0],
vec![0, 0, 0, 1],
vec![0, 0, 1, 0],
vec![0, 1, 0, 0],
vec![1, 0, 0, 0],
];
assert_eq!(gf2_matrix.elements, expected);
let elements = vec![0, 1, 2, 3, 5];
let int_matrix = PackedGF2Matrix::<u8>::new(elements.clone(), 4);
let gf2_matrix = int_matrix.from_int_matrix_to_gf2_matrix(BitOrder::MSB);
let expected = vec![
vec![0, 0, 0, 0],
vec![0, 0, 0, 1],
vec![0, 0, 1, 0],
vec![0, 0, 1, 1],
vec![0, 1, 0, 1],
];
assert_eq!(gf2_matrix.elements, expected);
}
}