Skip to content

Commit 91ab9de

Browse files
authored
fix the return of the solve_matrix_system method (#7)
* fix the return of the solve_matrix_system method * update readme file * format
1 parent 22240c4 commit 91ab9de

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lin_algebra"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
authors = ["LucaBonamino"]
66
description = "A linear algebra package to compute image, kernel and rank of linear applications."

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Add the dependency to your `Cargo.toml`:
1616

1717
```toml
1818
[dependencies]
19-
lin_algebra = "0.2.0"
19+
lin_algebra = "0.3.1"
2020
```
2121

2222
## Usage

src/gf2_matrix.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,29 @@ impl GF2Matrix {
189189
/// # Returns
190190
/// Matrix X such that self * X = Y.
191191
pub fn solve_matrix_system(&self, y: &GF2Matrix) -> GF2Matrix {
192-
let mut solution: Vec<Vec<u8>> = Vec::new();
193192
if self.rank() < self.ncols() {
194193
panic!("Matrix must have full rank");
195194
}
195+
196196
let (ech, operations) = self.echelon_form();
197197
let to_remove = ech.nrows() - ech.rank();
198-
for i in 0..y.ncols() {
199-
let mut solved_b = Self::apply_operations(&operations, &y.column(i));
198+
199+
let n_rows = self.ncols();
200+
let n_cols = y.ncols();
201+
202+
let mut elements = vec![vec![0u8; n_cols]; n_rows];
203+
for j in 0..y.ncols() {
204+
let mut solved_b = Self::apply_operations(&operations, &y.column(j));
200205
for _ in 0..to_remove {
201206
solved_b.remove(solved_b.len() - 1);
202207
}
203-
solution.push(solved_b);
208+
209+
for i in 0..n_rows {
210+
elements[i][j] = solved_b[i];
211+
}
204212
}
205213

206-
GF2Matrix::new(solution)
214+
GF2Matrix::new(elements)
207215
}
208216

209217
/// Solves for X such that equation A*x = b where A is a GF2Matrix and b a Vec<u8>.

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ mod tests {
150150
let r = m.solve_matrix_system(&rm);
151151
assert_eq!(
152152
r.elements,
153-
vec![vec![0, 1, 1], vec![0, 0, 1], vec![1, 1, 0]]
153+
vec![vec![0, 0, 1], vec![1, 0, 1], vec![1, 1, 0]]
154154
);
155155
}
156156

@@ -165,7 +165,7 @@ mod tests {
165165
let r = m.solve_matrix_system(&rm);
166166
assert_eq!(
167167
r.elements,
168-
vec![vec![0, 1, 1], vec![0, 0, 1], vec![1, 1, 0]]
168+
vec![vec![0, 0, 1], vec![1, 0, 1], vec![1, 1, 0]]
169169
);
170170
}
171171

0 commit comments

Comments
 (0)