Skip to content

Commit b8ea4b5

Browse files
committed
templates
1 parent a2ce196 commit b8ea4b5

File tree

6 files changed

+235
-175
lines changed

6 files changed

+235
-175
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
name: Bug report
3+
about: Report a bug or unexpected behavior
4+
labels: bug
5+
---
6+
7+
## Summary
8+
9+
Briefly describe the problem.
10+
11+
## Reproduction steps
12+
13+
How can the issue be reproduced?
14+
15+
```rust
16+
// minimal example, if applicable
17+
```
18+
19+
## Expected vs actual behavior
20+
21+
What did you expect to happen, and what happened instead?
22+
23+
## Environment
24+
25+
- OS:
26+
- Rust version (```rustc --version```):
27+
- lin_algebra version:
28+
29+
## Additional context
30+
31+
Any other information that might help (logs, error messages, ideas).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
name: Feature request
3+
about: Suggest a new feature or improvement
4+
labels: enhancement
5+
---
6+
7+
## Summary
8+
9+
Briefly describe the feature or improvement you are proposing.
10+
11+
## Motivation
12+
13+
Why is this feature useful?
14+
What problem does it solve?
15+
16+
## Proposed usage (optional)
17+
18+
If you have an idea of how this feature could look in Python, show a short example.
19+
20+
```rust
21+
// example usage
22+
```
23+
24+
## Additional context
25+
26+
Any other information, references, or links that might be helpful.

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ Please keep pull requests focused and well-described.
2626
```bash
2727
cargo build
2828
cargo test
29+
cargo fmt
2930
```
3031

3132

3233
## Code Style
3334
- Prefer clear names over clever code
34-
- Add docstrings for public methods
35+
- Add documentation comments (///) for public items
3536
- Add tests for new functionality when possible
3637

3738
## Question or ideas?

src/gf2_matrix.rs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::matrix::{MatrixTrait, Matrix};
1+
use crate::matrix::{Matrix, MatrixTrait};
22

33
/// GF(2) matrix.
4-
///
4+
///
55
/// Implements the trait MatrixTrait: needs to implement
66
/// - rank
77
/// - kernel
@@ -11,16 +11,15 @@ use crate::matrix::{MatrixTrait, Matrix};
1111
pub type GF2Matrix = Matrix<u8>;
1212

1313
impl MatrixTrait<u8> for GF2Matrix {
14-
1514
/// Checks if a GF(2) matrix is in reduced row echelon form (RREF).
1615
///
1716
/// # Returns
1817
/// `true` if the matrix is in reduced row echelon form; otherwise, `false`.
19-
fn is_reduced_echelon(&self) -> bool{
18+
fn is_reduced_echelon(&self) -> bool {
2019
let nrows = self.nrows();
2120
let mut old_piv = 0;
2221
let mut found_zero_row = false;
23-
for i in 0..nrows{
22+
for i in 0..nrows {
2423
let piv = GF2Matrix::get_pivot(&self.elements[i]);
2524
match piv {
2625
None => {
@@ -37,30 +36,27 @@ impl MatrixTrait<u8> for GF2Matrix {
3736
}
3837
}
3938
old_piv = piv;
40-
4139
}
4240
}
4341
}
4442
return true;
4543
}
4644

4745
/// Computes the rank of the linear applocation represented by a GF(2) matrix.
48-
///
46+
///
4947
/// If the matrix is not in echelon form (RREF),
5048
/// it first converts the matrix to its RREF before computing the rank.
51-
///
49+
///
5250
/// # Returns
5351
/// An integer representing the rank of the matrix.
5452
///
55-
fn rank(&self) -> usize{
56-
if self.is_reduced_echelon(){
57-
return self.rank_echelon_form()
58-
}
59-
else {
53+
fn rank(&self) -> usize {
54+
if self.is_reduced_echelon() {
55+
return self.rank_echelon_form();
56+
} else {
6057
let (ech_form, _) = self.echelon_form();
61-
return ech_form.rank_echelon_form();
58+
return ech_form.rank_echelon_form();
6259
}
63-
6460
}
6561

6662
/// Computes the base of the kernel of the linear applocation represented by a GF(2) matrix.
@@ -70,27 +66,26 @@ impl MatrixTrait<u8> for GF2Matrix {
7066
///
7167
/// # Returns
7268
/// A vector of row vectors, each representing a basis vector of the kernel.
73-
fn kernel(&self)-> Vec<Vec<u8>>{
74-
if self.is_reduced_echelon(){
69+
fn kernel(&self) -> Vec<Vec<u8>> {
70+
if self.is_reduced_echelon() {
7571
println!("{:?}", self.elements);
7672
return self.kernel_echelon_form();
77-
}
78-
else {
73+
} else {
7974
let (ech_form, _) = self.echelon_form();
8075
println!("{:?}", ech_form);
8176
return ech_form.kernel_echelon_form();
8277
}
8378
}
8479

8580
/// Computes the reduced echelon form (RREF) of a GF(2) matrix along with the history of all row operations applied.
86-
///
81+
///
8782
/// # Returns
88-
/// A tuple where the first element is the RREF form of the matrix and
83+
/// A tuple where the first element is the RREF form of the matrix and
8984
/// the second is a Vec containing the history of the row operations applied to the matrix to compute the RREF.
9085
/// Each element of the row operations vector, is a tuple heving the modified row as first element and the row to which it has been summed as second element:
9186
/// R1 -> R1 + R2 is represented the entry (R1, R2)
9287
/// The swap of two rows is represented as 3 entries:
93-
/// swap(R1, R2) is represented as (R1, R2), (R2,R1), (R1,R2)
88+
/// swap(R1, R2) is represented as (R1, R2), (R2,R1), (R1,R2)
9489
fn echelon_form(&self) -> (Self, Vec<(usize, usize)>) {
9590
let mut m_copy = self.clone();
9691
let rows = m_copy.nrows();
@@ -134,13 +129,13 @@ impl MatrixTrait<u8> for GF2Matrix {
134129
}
135130

136131
/// Computes the image of the linear application corresponding to a GF(2) matrix.
137-
///
132+
///
138133
/// If the matrix is not in reduced row echelon form (RREF),
139134
/// it first converts the matrix to its RREF before computing the image.
140-
///
135+
///
141136
/// # Returns
142137
/// A vector of row vectors, each representing a basis vector of the image.
143-
fn image(&self) -> Vec<Vec<u8>>{
138+
fn image(&self) -> Vec<Vec<u8>> {
144139
let mat = if !self.is_reduced_echelon() {
145140
let (m, _) = self.echelon_form();
146141
m
@@ -169,11 +164,9 @@ impl MatrixTrait<u8> for GF2Matrix {
169164
fn get_pivot(row: &Vec<u8>) -> Option<usize> {
170165
row.iter().position(|&x| x == 1)
171166
}
172-
173167
}
174168

175-
impl GF2Matrix {
176-
169+
impl GF2Matrix {
177170
fn apply_operations(operations: &Vec<(usize, usize)>, v: &Vec<u8>) -> Vec<u8> {
178171
let mut result = v.clone();
179172
for &(op1, op2) in operations.iter() {
@@ -184,63 +177,57 @@ impl GF2Matrix {
184177

185178
/// Returns the column of the matrix by column index.
186179
fn column(&self, idx: usize) -> Vec<u8> {
187-
self.elements
188-
.iter()
189-
.map(|row| row[idx])
190-
.collect()
180+
self.elements.iter().map(|row| row[idx]).collect()
191181
}
192182

193183
/// Solves for X such that equation A*X = B where A and B are GF2Matrix natrices.
194-
///
195-
/// # Arguments
196-
///
184+
///
185+
/// # Arguments
186+
///
197187
/// * `y` - right hand side matrix: GF2Matrix such that self * X = Y
198-
///
188+
///
199189
/// # Returns
200190
/// Matrix X such that self * X = Y.
201-
pub fn solve_matrix_system(&self, y: &GF2Matrix) -> GF2Matrix{
191+
pub fn solve_matrix_system(&self, y: &GF2Matrix) -> GF2Matrix {
202192
let mut solution: Vec<Vec<u8>> = Vec::new();
203-
if self.rank() < self.ncols(){
193+
if self.rank() < self.ncols() {
204194
panic!("Matrix must have full rank");
205195
}
206196
let (ech, operations) = self.echelon_form();
207197
let to_remove = ech.nrows() - ech.rank();
208-
for i in 0..y.ncols(){
198+
for i in 0..y.ncols() {
209199
let mut solved_b = Self::apply_operations(&operations, &y.column(i));
210-
for _ in 0..to_remove{
211-
solved_b.remove(solved_b.len()-1);
200+
for _ in 0..to_remove {
201+
solved_b.remove(solved_b.len() - 1);
212202
}
213203
solution.push(solved_b);
214204
}
215205

216206
GF2Matrix::new(solution)
217-
218207
}
219208

220209
/// Solves for X such that equation A*x = b where A is a GF2Matrix and b a Vec<u8>.
221-
///
210+
///
222211
/// # Arguments
223-
///
212+
///
224213
/// * `b`- right hand side vector: such that self*x = b
225-
///
214+
///
226215
/// # Return
227216
/// x such that self*x = b
228-
pub fn solve(&self, b: &Vec<u8>) -> Vec<u8>{
217+
pub fn solve(&self, b: &Vec<u8>) -> Vec<u8> {
229218
// et mut y_copy = y.clone();
230-
if self.rank() < self.ncols(){
219+
if self.rank() < self.ncols() {
231220
panic!("Matrix must have full rank");
232221
}
233222
let (ech, operations) = self.echelon_form();
234223
let to_remove = ech.nrows() - ech.rank();
235-
let mut solved_b= Self::apply_operations(&operations, b);
236-
for _ in 0..to_remove{
237-
solved_b.remove(solved_b.len()-1);
224+
let mut solved_b = Self::apply_operations(&operations, b);
225+
for _ in 0..to_remove {
226+
solved_b.remove(solved_b.len() - 1);
238227
}
239228
solved_b
240-
241229
}
242230

243-
244231
fn swap_rows(&mut self, row1: usize, row2: usize) {
245232
self.elements.swap(row1, row2);
246233
}
@@ -260,7 +247,6 @@ impl GF2Matrix {
260247
count
261248
}
262249

263-
264250
fn kernel_echelon_form(&self) -> Vec<Vec<u8>> {
265251
let rows = self.nrows();
266252
let cols = self.ncols();
@@ -300,5 +286,4 @@ impl GF2Matrix {
300286

301287
kernel_base
302288
}
303-
304-
}
289+
}

0 commit comments

Comments
 (0)