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
//! some linear algebra fun
//!
//! this module mainly contains an implementation of matrices over a finite
//! field.
use ark_ff::Field;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::rand::{Rng, RngCore};
use crate::error::KomodoError;
/// a matrix defined over a finite field
///
/// internally, a matrix is just a vector of field elements that whose length is
/// exactly the width times the height and where elements are organized row by
/// row.
#[derive(Clone, PartialEq, Default, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Matrix<T: Field> {
pub elements: Vec<T>,
pub height: usize,
pub width: usize,
}
impl<T: Field> Matrix<T> {
/// build a matrix from a diagonal of elements
///
/// # Example
/// building a diagonal matrix from the diagonal $[1, 2, 3, 4]$ will give
/// ```text
/// [
/// [1, 0, 0, 0],
/// [0, 2, 0, 0],
/// [0, 0, 3, 0],
/// [0, 0, 0, 4],
/// ]
/// ```
fn from_diagonal(diagonal: Vec<T>) -> Self {
let size = diagonal.len();
let mut elements = Vec::new();
elements.resize(size * size, T::zero());
for i in 0..size {
elements[i * size + i] = diagonal[i];
}
Self {
elements,
height: size,
width: size,
}
}
/// build the identity matrix of a given size
///
/// # Example
/// the identity of size 3 is
/// ```text
/// [
/// [1, 0, 0],
/// [0, 1, 0],
/// [0, 0, 1],
/// ]
/// ```
fn identity(size: usize) -> Self {
Self::from_diagonal(vec![T::one(); size])
}
/// build a Vandermonde matrix for some seed points
///
/// actually, this is the tranpose of the Vandermonde matrix defined in the
/// [Wikipedia article][article], i.e. there are as many columns as there
/// are seed points and there are as many rows as there are powers of the
/// seed points.
///
/// > **Note**
/// > if you are sure the points are distinct and don't want to perform any
/// > runtime check to ensure that condition, have a look at
/// > [`Self::vandermonde_unchecked`].
///
/// # Example
/// ```rust
/// # use ark_ff::Field;
/// # use komodo::algebra::linalg::Matrix;
/// // helper to convert integers to field elements
/// fn vec_to_elements<T: Field>(elements: Vec<u128>) -> Vec<T>
/// # {
/// # elements.iter().map(|&x| T::from(x)).collect()
/// # }
/// # type T = ark_bls12_381::Fr;
///
/// let seed_points = vec_to_elements(vec![0, 1, 2, 3, 4]);
/// let height = 4;
///
/// let expected = vec_to_elements(vec![
/// 1, 1, 1, 1, 1,
/// 0, 1, 2, 3, 4,
/// 0, 1, 4, 9, 16,
/// 0, 1, 8, 27, 64,
/// ]);
///
/// assert_eq!(
/// Matrix::<T>::vandermonde(&seed_points, height).unwrap(),
/// Matrix { elements: expected, height, width: seed_points.len() }
/// );
/// ```
///
/// [article]: https://en.wikipedia.org/wiki/Vandermonde_matrix
pub fn vandermonde(points: &[T], height: usize) -> Result<Self, KomodoError> {
for i in 0..points.len() {
for j in (i + 1)..points.len() {
if points[i] == points[j] {
return Err(KomodoError::InvalidVandermonde(
i,
j,
format!("{}", points[i]),
));
}
}
}
Ok(Self::vandermonde_unchecked(points, height))
}
/// the unchecked version of [`Self::vandermonde`]
pub fn vandermonde_unchecked(points: &[T], height: usize) -> Self {
let width = points.len();
let mut elements = Vec::new();
elements.resize(height * width, T::zero());
for (j, pj) in points.iter().enumerate() {
let mut el = T::one();
for i in 0..height {
elements[i * width + j] = el;
el *= pj;
}
}
Self {
elements,
height,
width,
}
}
/// build a completely random matrix of shape $n \times m$
pub fn random<R: RngCore>(n: usize, m: usize, rng: &mut R) -> Self {
Self {
elements: (0..(n * m)).map(|_| T::from(rng.gen::<u128>())).collect(),
height: n,
width: m,
}
}
/// build a matrix from a "_matrix_" of elements
///
/// > **Note**
/// > if you are sure each row should have the same length and don't want to
/// > perform any runtime check to ensure that condition, have a look at
/// > [`Self::from_vec_vec_unchecked`].
///
/// # Example
/// ```rust
/// # use komodo::algebra::linalg::Matrix;
/// # use ark_ff::Field;
/// // helper to convert integers to field elements
/// fn vec_to_elements<T: Field>(elements: Vec<u128>) -> Vec<T>
/// # {
/// # elements.iter().map(|&x| T::from(x)).collect()
/// # }
/// // helper to convert integers to field elements, in a "matrix"
/// fn mat_to_elements<T: Field>(mat: Vec<Vec<u128>>) -> Vec<Vec<T>>
/// # {
/// # mat.iter().cloned().map(vec_to_elements).collect()
/// # }
/// # type T = ark_bls12_381::Fr;
///
/// let elements = mat_to_elements(vec![
/// vec![0, 1, 2, 3],
/// vec![4, 5, 6, 7],
/// vec![8, 9, 0, 1],
/// ]);
///
/// let height = elements.len();
/// let width = elements[0].len();
///
/// let expected = vec_to_elements(vec![
/// 0, 1, 2, 3,
/// 4, 5, 6, 7,
/// 8, 9, 0, 1,
/// ]);
///
/// assert_eq!(
/// Matrix::<T>::from_vec_vec(elements).unwrap(),
/// Matrix { elements: expected, height, width }
/// );
/// ```
pub fn from_vec_vec(matrix: Vec<Vec<T>>) -> Result<Self, KomodoError> {
if matrix.is_empty() {
return Ok(Self {
elements: vec![],
height: 0,
width: 0,
});
}
let width = matrix[0].len();
for (i, row) in matrix.iter().enumerate() {
if row.len() != width {
return Err(KomodoError::InvalidMatrixElements(format!(
"expected rows to be of same length {}, found {} at row {}",
width,
row.len(),
i
)));
}
}
Ok(Self::from_vec_vec_unchecked(matrix))
}
/// the unchecked version of [`Self::from_vec_vec`]
pub fn from_vec_vec_unchecked(matrix: Vec<Vec<T>>) -> Self {
let height = matrix.len();
let width = matrix[0].len();
let mut elements = Vec::new();
elements.resize(height * width, T::zero());
for i in 0..height {
for j in 0..width {
elements[i * width + j] = matrix[i][j];
}
}
Self {
elements,
height,
width,
}
}
fn get(&self, i: usize, j: usize) -> T {
self.elements[i * self.width + j]
}
fn set(&mut self, i: usize, j: usize, value: T) {
self.elements[i * self.width + j] = value;
}
/// extract a single column from the matrix
///
/// > **Note**
/// > returns `None` if the provided index is out of bounds
pub(crate) fn get_col(&self, j: usize) -> Option<Vec<T>> {
if j >= self.width {
return None;
}
Some((0..self.height).map(|i| self.get(i, j)).collect())
}
// compute _row / value_
fn divide_row_by(&mut self, row: usize, value: T) {
for j in 0..self.width {
self.set(row, j, self.get(row, j) / value);
}
}
// compute _destination = destination + source * value_
fn multiply_row_by_and_add_to_row(&mut self, source: usize, value: T, destination: usize) {
for j in 0..self.width {
self.set(
destination,
j,
self.get(destination, j) + self.get(source, j) * value,
);
}
}
/// compute the inverse of the matrix
///
/// > **None**
/// > the matrix should be
/// > - square
/// > - invertible
pub fn invert(&self) -> Result<Self, KomodoError> {
if self.height != self.width {
return Err(KomodoError::NonSquareMatrix(self.height, self.width));
}
let mut inverse = Self::identity(self.height);
let mut matrix = self.clone();
for i in 0..matrix.height {
let pivot = matrix.get(i, i);
if pivot.is_zero() {
return Err(KomodoError::NonInvertibleMatrix(i));
}
inverse.divide_row_by(i, pivot);
matrix.divide_row_by(i, pivot);
for k in 0..matrix.height {
if k != i {
let factor = matrix.get(k, i);
inverse.multiply_row_by_and_add_to_row(i, -factor, k);
matrix.multiply_row_by_and_add_to_row(i, -factor, k);
}
}
}
Ok(inverse)
}
/// swap rows `i` and `j`, inplace
///
/// > **Note**
/// > this function assumes both `i` and `j` are in bounds, unexpected
/// > results are expected if `i` or `j` are out of bounds.
fn swap_rows(&mut self, i: usize, j: usize) {
for k in 0..self.width {
self.elements.swap(i * self.width + k, j * self.width + k);
}
}
/// compute the rank of the matrix
///
/// > **None**
/// > see the [_Wikipedia article_](https://en.wikipedia.org/wiki/Rank_(linear_algebra))
/// > for more information
/// >
/// > - the rank is always smaller than the min between the height and the
/// > width of any matrix.
/// > - a square and invertible matrix will have _full rank_, i.e. it will
/// > be equal to its size.
pub fn rank(&self) -> usize {
let mut mat = self.clone();
let mut i = 0;
for j in 0..self.width {
let mut found = false;
// look for the first non-zero pivot in the j-th column
for k in i..self.height {
if !mat.get(k, j).is_zero() {
mat.swap_rows(i, k); // move the non-zero element to the diagonal
found = true;
break;
}
}
if found {
// update the bottom-right part of the matrix
for k in (i + 1)..self.height {
let ratio = mat.get(k, j) / mat.get(i, j);
for l in j..self.width {
let el = mat.get(i, l);
mat.set(k, l, mat.get(k, l) - ratio * el);
}
}
i += 1;
}
}
let nb_non_zero_rows = (0..self.height)
.filter(|i| {
let row = mat.elements[(i * self.width)..((i + 1) * self.width)].to_vec();
row.iter().any(|&x| !x.is_zero())
})
.collect::<Vec<_>>()
.len();
nb_non_zero_rows
}
/// compute the matrix multiplication with another matrix
///
/// if `mat` represents a matrix $A$ and `rhs` is the representation of
/// another matrix $B$, then `mat.mul(rhs)` will compute $A \times B$
///
/// > **Note**
/// > both matrices should have compatible shapes, i.e. if `self` has shape
/// > `(n, m)` and `rhs` has shape `(p, q)`, then `m == p`.
pub fn mul(&self, rhs: &Self) -> Result<Self, KomodoError> {
if self.width != rhs.height {
return Err(KomodoError::IncompatibleMatrixShapes(
self.height,
self.width,
rhs.height,
rhs.width,
));
}
let height = self.height;
let width = rhs.width;
let common = self.width;
let mut elements = Vec::new();
elements.resize(height * width, T::zero());
for i in 0..height {
for j in 0..width {
elements[i * width + j] = (0..common).map(|k| self.get(i, k) * rhs.get(k, j)).sum();
}
}
Ok(Self {
elements,
height,
width,
})
}
/// compute the transpose of the matrix
///
/// > **Note**
/// > see the [_Wikipedia article_](https://en.wikipedia.org/wiki/Transpose)
pub fn transpose(&self) -> Self {
let height = self.width;
let width = self.height;
let mut elements = Vec::new();
elements.resize(height * width, T::zero());
for i in 0..height {
for j in 0..width {
elements[i * width + j] = self.get(j, i);
}
}
Self {
elements,
height,
width,
}
}
/// truncate the matrix to the provided shape, from right and bottom
///
/// # Example
/// if a matrix has shape `(10, 11)` and is truncated to `(5, 7)`, the 5
/// bottom rows and 4 right columns will be removed.
pub(crate) fn truncate(&self, rows: Option<usize>, cols: Option<usize>) -> Self {
let width = if let Some(w) = cols {
self.width - w
} else {
self.width
};
let height = if let Some(h) = rows {
self.height - h
} else {
self.height
};
let mut elements = Vec::new();
elements.resize(height * width, T::zero());
for i in 0..height {
for j in 0..width {
elements[i * width + j] = self.get(i, j);
}
}
Self {
elements,
height,
width,
}
}
}
impl<T: Field> std::fmt::Display for Matrix<T> {
/// an example matrix with the identity of order 3
/// ```text
/// /1 0 0\
/// |0 1 0|
/// \0 0 1/
/// ```
///
/// - zero elements will show as "0" instead of a blank string
/// - elements that are bigger than the format size will be cropped, i.e.
/// - by default, the format size is undefined an thus elements won't be cropped
/// - if the format looks like `{:5}`, any element whose representation is bigger than 5
/// characters will be cropped
/// - the default cropping is done with `...` but adding `#` to the format string will use `*`
/// instead
///
/// a few examples of a matrix with some random elements that are too big to be shown in 5
/// characters
///
/// - when the format is `{:5}`
/// ```text
/// /1 0 20... 0 \
/// |0 1 32... 0 |
/// |0 0 0 0 |
/// |0 0 0 11...|
/// \0 0 0 17.../
/// ```
/// - when the format is `{:#}` or `{:#1}`
/// ```text
/// /1 0 * 0\
/// |0 1 * 0|
/// |0 0 0 0|
/// |0 0 0 *|
/// \0 0 0 */
/// ```
/// - when the format is `{:#5}`
/// ```text
/// /1 0 * 0 \
/// |0 1 * 0 |
/// |0 0 0 0 |
/// |0 0 0 * |
/// \0 0 0 * /
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for i in 0..self.height {
let start = if i == 0 {
"/"
} else if i == self.height - 1 {
"\\"
} else {
"|"
};
write!(f, "{}", start)?;
for j in 0..self.width {
let x = self.get(i, j);
let y = if x.is_zero() {
"0".to_string()
} else {
format!("{}", x)
};
if let Some(w) = f.width() {
if y.len() > w {
if f.alternate() {
write!(f, "{:width$}", "*", width = w)?;
} else {
let t = if w > 3 { w - 3 } else { 0 };
write!(
f,
"{:width$}",
format!("{}{}", y.chars().take(t).collect::<String>(), "..."),
width = w
)?;
}
} else {
write!(f, "{:width$}", format!("{}", y), width = w)?;
}
} else if f.alternate() && y.len() > 1 {
write!(f, "*")?;
} else {
write!(f, "{}", y)?;
}
if j < self.width - 1 {
write!(f, " ")?;
}
}
let end = if i == 0 {
"\\"
} else if i == self.height - 1 {
"/"
} else {
"|"
};
writeln!(f, "{}", end)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use ark_bls12_381::Fr;
use ark_ff::Field;
use super::{KomodoError, Matrix};
// two wrapped functions to make the tests more readable
fn vec_to_elements<T: Field>(elements: Vec<u128>) -> Vec<T> {
elements.iter().map(|&x| T::from(x)).collect()
}
fn mat_to_elements<T: Field>(mat: Vec<Vec<u128>>) -> Vec<Vec<T>> {
mat.iter().cloned().map(vec_to_elements).collect()
}
#[test]
fn from_vec_vec() {
let actual = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![2, 0, 0],
vec![0, 3, 0],
vec![0, 0, 4],
vec![2, 3, 4],
]))
.unwrap();
let expected = Matrix {
elements: vec_to_elements(vec![2, 0, 0, 0, 3, 0, 0, 0, 4, 2, 3, 4]),
height: 4,
width: 3,
};
assert_eq!(actual, expected);
let matrix = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![vec![0], vec![0, 0]]));
assert!(matrix.is_err());
assert!(matches!(
matrix.err().unwrap(),
KomodoError::InvalidMatrixElements(..)
));
}
#[test]
fn diagonal() {
let actual = Matrix::<Fr>::from_diagonal(vec_to_elements(vec![2, 3, 4]));
let expected = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![2, 0, 0],
vec![0, 3, 0],
vec![0, 0, 4],
]))
.unwrap();
assert_eq!(actual, expected);
}
#[test]
fn identity() {
let actual = Matrix::<Fr>::identity(3);
let expected = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 0, 0],
vec![0, 1, 0],
vec![0, 0, 1],
]))
.unwrap();
assert_eq!(actual, expected);
}
#[test]
fn multiplication() {
let a = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![9, 4, 3],
vec![8, 5, 2],
vec![7, 6, 1],
]))
.unwrap();
let b = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 2, 3],
vec![4, 5, 6],
vec![7, 8, 9],
]))
.unwrap();
assert!(matches!(
a.mul(&Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![vec![1, 2]])).unwrap()),
Err(KomodoError::IncompatibleMatrixShapes(3, 3, 1, 2))
));
let product = a.mul(&b).unwrap();
let expected = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![46, 62, 78],
vec![42, 57, 72],
vec![38, 52, 66],
]))
.unwrap();
assert_eq!(product, expected);
}
#[test]
fn random() {
let mut rng = ark_std::test_rng();
for n in 0..10 {
for m in 0..10 {
let mat = Matrix::<Fr>::random(n, m, &mut rng);
assert_eq!(mat.elements.len(), n * m);
assert_eq!(mat.width, m);
assert_eq!(mat.height, n);
}
}
}
#[test]
fn inverse() {
let mut rng = ark_std::test_rng();
let matrix = Matrix::<Fr>::identity(3);
let inverse = matrix.invert().unwrap();
assert_eq!(Matrix::<Fr>::identity(3), inverse);
let matrix = Matrix::<Fr>::from_diagonal(vec_to_elements(vec![2, 3, 4]));
let inverse = matrix.invert().unwrap();
assert_eq!(matrix.mul(&inverse).unwrap(), Matrix::<Fr>::identity(3));
assert_eq!(inverse.mul(&matrix).unwrap(), Matrix::<Fr>::identity(3));
for n in 1..20 {
let matrix = Matrix::random(n, n, &mut rng);
let inverse = matrix.invert().unwrap();
assert_eq!(matrix.mul(&inverse).unwrap(), Matrix::<Fr>::identity(n));
assert_eq!(inverse.mul(&matrix).unwrap(), Matrix::<Fr>::identity(n));
}
let inverse =
Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![vec![1, 0, 0], vec![0, 1, 0]]))
.unwrap()
.invert();
assert!(inverse.is_err());
assert!(matches!(
inverse.err().unwrap(),
KomodoError::NonSquareMatrix(..)
));
let inverse = Matrix::<Fr>::from_diagonal(vec_to_elements(vec![0, 3, 4])).invert();
assert!(inverse.is_err());
assert!(matches!(
inverse.err().unwrap(),
KomodoError::NonInvertibleMatrix(0)
));
let inverse = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 1, 0],
vec![0, 0, 0],
vec![0, 0, 1],
]))
.unwrap()
.invert();
assert!(inverse.is_err());
assert!(matches!(
inverse.err().unwrap(),
KomodoError::NonInvertibleMatrix(1)
));
}
#[test]
fn vandermonde() {
assert!(Matrix::<Fr>::vandermonde(&vec_to_elements(vec![0, 4, 2, 3, 4]), 4).is_err());
assert!(Matrix::<Fr>::vandermonde(&vec_to_elements(vec![0, 1, 2, 3, 4]), 4).is_ok());
let actual =
Matrix::<Fr>::vandermonde_unchecked(&mat_to_elements(vec![vec![0, 1, 2, 3, 4]])[0], 4);
#[rustfmt::skip]
let expected = Matrix::from_vec_vec(mat_to_elements(vec![
vec![1, 1, 1, 1, 1],
vec![0, 1, 2, 3, 4],
vec![0, 1, 4, 9, 16],
vec![0, 1, 8, 27, 64],
]))
.unwrap();
assert_eq!(actual, expected);
}
#[test]
fn transpose() {
let matrix = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 2, 3, 10],
vec![4, 5, 6, 11],
vec![7, 8, 9, 12],
]))
.unwrap();
let transpose = Matrix::from_vec_vec(mat_to_elements(vec![
vec![1, 4, 7],
vec![2, 5, 8],
vec![3, 6, 9],
vec![10, 11, 12],
]))
.unwrap();
assert_eq!(matrix.transpose(), transpose);
}
#[test]
fn truncate() {
let matrix = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 2, 3, 10],
vec![4, 5, 6, 11],
vec![7, 8, 9, 12],
]))
.unwrap();
assert_eq!(matrix.truncate(None, None), matrix);
assert_eq!(matrix.truncate(Some(0), None), matrix);
assert_eq!(matrix.truncate(None, Some(0)), matrix);
assert_eq!(matrix.truncate(Some(0), Some(0)), matrix);
let truncated =
Matrix::from_vec_vec(mat_to_elements(vec![vec![1, 2], vec![4, 5]])).unwrap();
assert_eq!(matrix.truncate(Some(1), Some(2)), truncated);
}
#[test]
fn get_cols() {
let matrix = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 2, 3, 10],
vec![4, 5, 6, 11],
vec![7, 8, 9, 12],
]))
.unwrap();
assert!(matrix.get_col(10).is_none());
assert_eq!(matrix.get_col(0), Some(vec_to_elements(vec![1, 4, 7])));
assert_eq!(matrix.get_col(3), Some(vec_to_elements(vec![10, 11, 12])));
}
#[test]
fn rank() {
let mut rng = ark_std::test_rng();
for n in 1..=20 {
assert_eq!(Matrix::<Fr>::identity(n).rank(), n);
}
for _ in 0..20 {
let m = Matrix::<Fr>::random(7, 13, &mut rng);
assert_eq!(m.rank(), m.transpose().rank());
}
let m = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 0, 0],
vec![0, 2, 0],
vec![0, 0, 3],
]))
.unwrap();
assert_eq!(m.rank(), 3);
let m = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 0, 0],
vec![0, 2, 0],
vec![0, 0, 3],
vec![0, 0, 3],
]))
.unwrap();
assert_eq!(m.rank(), 3);
let m = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![1, 0, 0],
vec![0, 2, 0],
vec![0, 0, 0],
]))
.unwrap();
assert_eq!(m.rank(), 2);
let m = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![0, 0, 0],
vec![0, 0, 0],
vec![0, 0, 0],
]))
.unwrap();
assert_eq!(m.rank(), 0);
let m = Matrix::<Fr>::from_vec_vec(mat_to_elements(vec![
vec![0, 0, 1, 0],
vec![1, 0, 0, 1],
vec![0, 1, 0, 1],
vec![0, 1, 1, 0],
vec![1, 0, 0, 0],
]))
.unwrap();
let rank = m.rank();
assert!(
rank <= m.height.min(m.width),
"rank should be less than {}, got {}",
m.height.min(m.width),
rank
);
}
}