Skip to main content

ark_relations/utils/
matrix.rs

1use ark_ff::Field;
2use ark_std::vec::Vec;
3/// A sparse representation of constraint matrices.
4pub type Matrix<F> = Vec<Vec<(F, usize)>>;
5
6/// Transpose a matrix of field elements.
7#[must_use]
8pub fn transpose<F: Field>(matrix: &Matrix<F>, num_col: usize) -> Matrix<F> {
9    // Initialize the transposed matrix with empty vectors
10    let mut transposed: Matrix<F> = vec![Vec::new(); num_col];
11
12    // Iterate through each row and each element in the row
13    for (row_index, row) in matrix.iter().enumerate() {
14        for &(value, col_index) in row {
15            // Add the element to the new row (which is originally a column) in the
16            // transposed matrix
17            transposed[col_index].push((value, row_index));
18        }
19    }
20
21    // Return the transposed matrix
22    transposed
23}
24
25/// Multiply a matrix by a vector.
26pub fn mat_vec_mul<F: Field>(matrix: &Matrix<F>, vector: &[F]) -> Vec<F> {
27    let mut output: Vec<F> = Vec::new();
28    for row in matrix {
29        let mut sum: F = F::zero();
30        for (value, col) in row {
31            sum += vector[*col] * value;
32        }
33        output.push(sum);
34    }
35    output
36}