### Install lin_algebra Crate Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Add the lin_algebra crate to your project's Cargo.toml file to include it as a dependency. ```toml [dependencies] lin_algebra = "0.4.1" ``` -------------------------------- ### Build, Test, and Format Project Source: https://github.com/lucabonamino/lin_algebra/blob/main/CONTRIBUTING.md Standard commands for building the project, running tests, and formatting the code using Cargo. ```bash cargo build ``` ```bash cargo test ``` ```bash cargo fmt ``` -------------------------------- ### GF2Matrix Operations in Rust Source: https://github.com/lucabonamino/lin_algebra/blob/main/README.md Demonstrates various operations on GF2Matrix, including computing the reduced echelon form, kernel, image, rank, and solving systems of linear equations. ```Rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { // Reduces echelon form let gf2_mat = gf2_matrix::GF2Matrix::new( vec![vec![1,0,0,0], vec![0,1,0,1], vec![0,1,0,1]] ) let (educed_echelon_form, row_operations) = gf2_mat.echelon_form(); println!("Reduced echelon form {:?}", educed_echelon_form); println!( "Row operation applied to reach the reduces echelon form {:?}", row_operations ); // Kernel println!("Kernel {:?}", gf2_mat.kernel()); // Image println!("Image {:?}", gf2_mat.image()); // Rank println!("Rank {}", gf2_mat.rank()); // Solve M x = b let b = vec![1,0,0,1] println!("x: {:?}", gf2_mat.solve(&b)); // Solve A X = Y let y_matrix = gf2_matrix::GF2Matrix::new( vec![vec![1,0,0,1], vec![1,1,0,1], vec![0,1,0,1]] ) println!("x: {:?}", gf2_mat.solve_matrix_system(&y_matrix)); } ``` -------------------------------- ### Solve Linear System A·x = b over GF(2) Source: https://context7.com/lucabonamino/lin_algebra/llms.txt The `solve` method solves a linear system `A·x = b` over GF(2). It requires the matrix `A` to have full column rank; otherwise, it panics. The input `b` must be a `Vec` column vector. ```rust use lin_algebra::gf2_matrix::GF2Matrix; fn main() { let a = GF2Matrix::new(vec![ vec![1, 0, 0], vec![0, 1, 1], vec![1, 0, 1], ]); let b = vec![0u8, 0, 1]; let x = a.solve(&b); println!("Solution x: {:?}", x); // Output: Solution x: [0, 1, 1] // Verify: A·x = b → [0,0,1] ✓ } ``` -------------------------------- ### PackedGF2Matrix Operations in Rust Source: https://github.com/lucabonamino/lin_algebra/blob/main/README.md Illustrates operations on PackedGF2Matrix, including echelon form computation, conversion to GF2Matrix, and matrix-vector multiplication. ```Rust use lin_algebra::packed_gf2_matrix::{BitOrder, PackedGF2Matrix}; fn main() { // Each integer encodes one row of the matrix in bit-packed form. let packed = PackedGF2Matrix::::new( vec![ 0b1000u8, 0b0101u8, 0b0101u8, ], 4, ); let (echelon, ops) = packed.echelon_form(); println!("Packed echelon form: {:?}", echelon); println!("Row operations: {:?}", ops); // Convert to an explicit GF(2) matrix let explicit = packed.from_int_matrix_to_gf2_matrix(BitOrder::LSB); println!("Explicit matrix: {:?}", explicit); // Example of packed matrix-vector multiplication let v = 0b0011u8; let result = PackedGF2Matrix::matrix_by_vector(&packed, &v); println!("Packed matrix-vector product: {:?}", result); } ``` -------------------------------- ### Solve Matrix System A·X = Y over GF(2) Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Use `solve_matrix_system` to solve `A·X = Y` for matrix `X` over GF(2). `A` must be full column rank. Linearly dependent rows in `Y` are handled automatically. ```rust use lin_algebra::gf2_matrix::GF2Matrix; fn main() { let a = GF2Matrix::new(vec![ vec![1, 0, 0], vec![0, 1, 1], vec![1, 0, 1], ]); let y = GF2Matrix::new(vec![ vec![0, 0, 1], vec![0, 1, 1], vec![1, 1, 1], ]); let x = a.solve_matrix_system(&y); println!("Solution X:\n{:?}", x.elements); // Output: [[0, 0, 1], [1, 0, 1], [1, 1, 0]] } ``` -------------------------------- ### From / Into Trait Conversions Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Provides bidirectional conversion between GF2Matrix and PackedGF2Matrix using Rust's From/Into traits. ```APIDOC ## From / Into Trait Conversions The `convert` module implements Rust's standard `From` trait for lossless bidirectional conversion between `GF2Matrix` and `PackedGF2Matrix`. All conversions use MSB bit ordering. ### Conversions - **`PackedGF2Matrix` to `GF2Matrix`**: Implemented via `GF2Matrix::from(&packed_matrix)`. - **`GF2Matrix` to `PackedGF2Matrix`**: Implemented via `PackedGF2Matrix::from(&gf2_matrix)`. - **`Vec` to `PackedGF2Matrix`**: Implemented via `PackedGF2Matrix::from(vector)` or `vector.into()`. ### Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; // PackedGF2Matrix → GF2Matrix let packed = PackedGF2Matrix::::new(vec![0b0001u8, 0b0010u8, 0b0100u8, 0b1000u8], 4); let explicit: GF2Matrix = GF2Matrix::from(&packed); // explicit.elements will be [[0,0,0,1], [0,0,1,0], [0,1,0,0], [1,0,0,0]] // GF2Matrix → PackedGF2Matrix let mat = GF2Matrix::new(vec![vec![1, 0, 1, 0], vec![0, 1, 1, 0]]); let repacked: PackedGF2Matrix = PackedGF2Matrix::from(&mat); // repacked.row(0) will be 0b10100000 (after shifting) // repacked.row(1) will be 0b01100000 (after shifting) // Vec → PackedGF2Matrix (via Into) let from_vec: PackedGF2Matrix = vec![0b11u8, 0b10u8].into(); // from_vec.ncols() will be 2 ``` ``` -------------------------------- ### Add lin_algebra dependency to Cargo.toml Source: https://github.com/lucabonamino/lin_algebra/blob/main/README.md Add this line to your Cargo.toml file to include the lin_algebra library as a dependency. ```toml [dependencies] lin_algebra = "0.4.1" ``` -------------------------------- ### Compute Kernel (Null Space) Basis Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Obtain a basis for the kernel (null space) of the linear application represented by a GF2Matrix. The result is a vector of basis vectors, computed via RREF. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], ]); let ker = mat.kernel(); println!("Kernel basis: {:?}", ker); // Output: [[0, 0, 1, 0], [0, 1, 0, 1]] // Two free columns → two-dimensional kernel } ``` -------------------------------- ### GF2Matrix::new Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Constructs a new GF2Matrix with the specified dimensions and elements. The matrix stores each entry explicitly as a u8 value of 0 or 1. ```APIDOC ## GF2Matrix::new ### Description Constructs a new `GF2Matrix` over GF(2) from a vector of vectors representing the matrix elements. ### Method `GF2Matrix::new(elements: Vec>)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], vec![0, 1, 0, 1], ]); ``` ### Response #### Success Response (200) Returns a `GF2Matrix` instance. #### Response Example None ``` -------------------------------- ### Compute Image (Column Space) Basis Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Generate a basis for the image (column space) of the linear application represented by a GF2Matrix. The basis consists of the non-zero rows from the matrix's RREF. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { // Third row is linearly dependent on second let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], vec![0, 1, 0, 1], ]); let img = mat.image(); println!("Image basis: {:?}", img); // Output: [[1, 0, 0, 0], [0, 1, 0, 1]] // The zero row from RREF is excluded } ``` -------------------------------- ### GF2Matrix / PackedGF2Matrix: From/Into Trait Conversions Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Implements standard Rust `From` and `Into` traits for bidirectional conversion between GF2Matrix and PackedGF2Matrix. All conversions use MSB bit ordering. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; fn main() { // PackedGF2Matrix → GF2Matrix (by value and by reference) let packed = PackedGF2Matrix::::new(vec![0b0001u8, 0b0010u8, 0b0100u8, 0b1000u8], 4); let explicit: GF2Matrix = GF2Matrix::from(&packed); println!("Explicit rows: {:?}", explicit.elements); // Output: [[0,0,0,1], [0,0,1,0], [0,1,0,0], [1,0,0,0]] // GF2Matrix → PackedGF2Matrix (by value and by reference) let mat = GF2Matrix::new(vec![ vec![1, 0, 1, 0], vec![0, 1, 1, 0], ]); let repacked: PackedGF2Matrix = PackedGF2Matrix::from(&mat); println!("Row 0 packed: {:08b}", repacked.row(0)); // 0b10100000 >> 4 = 0b1010 println!("Row 1 packed: {:08b}", repacked.row(1)); // 0b01100000 >> 4 = 0b0110 // Vec → PackedGF2Matrix (via Into) let from_vec: PackedGF2Matrix = vec![0b11u8, 0b10u8].into(); println!("ncols: {}", from_vec.ncols()); // Output: ncols: 2 } ``` -------------------------------- ### PackedGF2Matrix: Construct from Vector Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Convenience constructors to build a PackedGF2Matrix from a vector of row values. `from_vec` consumes the vector, while `from_vec_referenced` clones from a reference. The number of columns is inferred from the vector length. ```rust use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; fn main() { // from_vec — consumes the vector let rows = vec![0b1010u8, 0b0110u8, 0b1100u8]; let m1 = PackedGF2Matrix::from_vec(rows); println!("ncols (from_vec): {}", m1.ncols()); // Output: ncols (from_vec): 3 // from_vec_referenced — clones from reference let rows2 = vec![0b1010u8, 0b0110u8]; let m2 = PackedGF2Matrix::from_vec_referenced(&rows2); println!("ncols (from_vec_referenced): {}", m2.ncols()); // Output: ncols (from_vec_referenced): 2 } ``` -------------------------------- ### Construct GF2Matrix Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Create a new GF2Matrix with explicit element-by-element entries. This matrix type is suitable for legible construction and inspection. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { // Construct a 3×4 matrix over GF(2) let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], vec![0, 1, 0, 1], ]); println!("Rows: {}, Cols: {}", mat.nrows(), mat.ncols()); // Output: Rows: 3, Cols: 4 } ``` -------------------------------- ### GF2Matrix::solve Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Solves a linear system of equations of the form A·x = b over GF(2). Panics if the matrix A does not have full column rank. ```APIDOC ## GF2Matrix::solve — Solve a Linear System A·x = b ### Description Solves the system `A·x = b` over GF(2) where `A` is the matrix and `b` is a `Vec` column vector. Panics with `"Matrix must have full rank"` if the matrix does not have full column rank. ### Method `solve(b: &Vec) -> Vec` ### Parameters - **b** (`&Vec`): The right-hand side column vector. ### Response - `Vec`: The solution vector `x`. ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let a = GF2Matrix::new(vec![vec![1, 0, 0], vec![0, 1, 1], vec![1, 0, 1]]); let b = vec![0u8, 0, 1]; let x = a.solve(&b); println!("Solution x: {:?}", x); ``` ### Response Example ``` Solution x: [0, 1, 1] ``` ``` -------------------------------- ### PackedGF2Matrix::from_int_matrix_to_gf2_matrix Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Expands a bit-packed matrix into an explicit GF2Matrix. The BitOrder parameter determines the mapping of bits to columns. ```APIDOC ## PackedGF2Matrix::from_int_matrix_to_gf2_matrix — Packed to Explicit Conversion Expands a bit-packed matrix into an explicit `GF2Matrix` by extracting individual bits from each row integer. The `BitOrder` parameter controls whether the least-significant bit (`LSB`) maps to column 0, or the most-significant bit (`MSB`) maps to column 0. ### Parameters - **bit_order**: `BitOrder` - Specifies the bit ordering (LSB or MSB) for mapping to columns. ### Returns - `GF2Matrix` - The expanded explicit GF2Matrix. ### Example ```rust use lin_algebra::packed_gf2_matrix::{BitOrder, PackedGF2Matrix}; let packed = PackedGF2Matrix::::new(vec![0b0010u8, 0b1001u8], 4); // LSB: bit 0 → column 0 let gf2_lsb = packed.from_int_matrix_to_gf2_matrix(BitOrder::LSB); // gf2_lsb.elements will be [[0, 1, 0, 0], [1, 0, 0, 1]] // MSB: bit (n-1) → column 0 let gf2_msb = packed.from_int_matrix_to_gf2_matrix(BitOrder::MSB); // gf2_msb.elements will be [[0, 0, 1, 0], [1, 0, 0, 1]] ``` ``` -------------------------------- ### GF2Matrix::image Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Returns a basis for the image (column space) of the linear application represented by the matrix over GF(2). ```APIDOC ## GF2Matrix::image ### Description Computes and returns a basis for the image (column space) of the linear transformation defined by the matrix. The basis vectors are derived from the non-zero rows of the matrix's RREF. ### Method `image() -> Vec>` ### Parameters None ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], vec![0, 1, 0, 1], ]); let img = mat.image(); ``` ### Response #### Success Response (200) - **image basis** (Vec>) - A vector where each inner vector is a basis vector for the image. #### Response Example ```rust // Assuming mat is defined as in the example let img = mat.image(); println!("Image basis: {:?}", img); // Output: [[1, 0, 0, 0], [0, 1, 0, 1]] ``` ``` -------------------------------- ### Find Pivot Column in a Row Source: https://context7.com/lucabonamino/lin_algebra/llms.txt The static utility `GF2Matrix::get_pivot` finds the column index of the first non-zero entry (pivot) in a row over GF(2). It returns `None` if the row consists entirely of zeros. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixCommon; fn main() { assert_eq!(GF2Matrix::get_pivot(&[1, 0, 0, 1]), Some(0)); assert_eq!(GF2Matrix::get_pivot(&[0, 1, 1, 0]), Some(1)); assert_eq!(GF2Matrix::get_pivot(&[0, 0, 0, 1]), Some(3)); assert_eq!(GF2Matrix::get_pivot(&[0, 0, 0, 0]), None); println!("Pivot of [0,0,1,0]: {:?}", GF2Matrix::get_pivot(&[0, 0, 1, 0])); // Output: Pivot of [0,0,1,0]: Some(2) } ``` -------------------------------- ### GF2Matrix::kernel Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Computes a basis for the kernel (null space) of the linear application represented by the matrix over GF(2). ```APIDOC ## GF2Matrix::kernel ### Description Computes a basis for the kernel (null space) of the linear transformation defined by the matrix. The basis vectors are returned as a vector of vectors. ### Method `kernel() -> Vec>` ### Parameters None ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], ]); let ker = mat.kernel(); ``` ### Response #### Success Response (200) - **kernel basis** (Vec>) - A vector where each inner vector is a basis vector for the kernel. #### Response Example ```rust // Assuming mat is defined as in the example let ker = mat.kernel(); println!("Kernel basis: {:?}", ker); // Output: [[0, 0, 1, 0], [0, 1, 0, 1]] ``` ``` -------------------------------- ### PackedGF2Matrix: Convert Packed to Explicit GF2Matrix Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Expands a bit-packed matrix into an explicit GF2Matrix. Use BitOrder::LSB or BitOrder::MSB to control bit-to-column mapping. ```rust use lin_algebra::packed_gf2_matrix::{BitOrder, PackedGF2Matrix}; fn main() { let packed = PackedGF2Matrix::::new(vec![0b0010u8, 0b1001u8], 4); // LSB: bit 0 → column 0 let gf2_lsb = packed.from_int_matrix_to_gf2_matrix(BitOrder::LSB); println!("LSB rows: {:?}", gf2_lsb.elements); // Output: LSB rows: [[0, 1, 0, 0], [1, 0, 0, 1]] // MSB: bit (n-1) → column 0 let gf2_msb = packed.from_int_matrix_to_gf2_matrix(BitOrder::MSB); println!("MSB rows: {:?}", gf2_msb.elements); // Output: MSB rows: [[0, 0, 1, 0], [1, 0, 0, 1]] } ``` -------------------------------- ### GF2Matrix::solve_matrix_system Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Solves the matrix equation A·X = Y over GF(2) for the matrix X, given a full-rank matrix A and a right-hand-side matrix Y. Handles linearly dependent rows in Y. ```APIDOC ## GF2Matrix::solve_matrix_system — Solve A·X = Y ### Description Solves the matrix equation `A·X = Y` over GF(2) for a matrix `X`, given a full-rank matrix `A` and a right-hand-side matrix `Y`. Panics with `"Matrix must have full rank"` if `A` does not have full column rank. Linearly dependent rows in `Y` are handled automatically. ### Method `solve_matrix_system(y: &GF2Matrix) -> GF2Matrix` ### Parameters - **y** (`&GF2Matrix`): The right-hand side matrix `Y`. ### Response - `GF2Matrix`: The solution matrix `X`. ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let a = GF2Matrix::new(vec![vec![1, 0, 0], vec![0, 1, 1], vec![1, 0, 1]]); let y = GF2Matrix::new(vec![vec![0, 0, 1], vec![0, 1, 1], vec![1, 1, 1]]); let x = a.solve_matrix_system(&y); println!("Solution X:\n{:?}", x.elements); ``` ### Response Example ``` Solution X: [[0, 0, 1], [1, 0, 1], [1, 1, 0]] ``` ``` -------------------------------- ### PackedGF2Matrix::from_vec / from_vec_referenced Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Constructs a PackedGF2Matrix from a vector of packed row values. The number of columns is inferred from the vector length. ```APIDOC ## PackedGF2Matrix::from_vec / from_vec_referenced — Construct from Vector Two convenience constructors that build a `PackedGF2Matrix` from a plain vector of packed row values. `from_vec` moves the vector; `from_vec_referenced` clones from a reference. In both cases the number of columns (`n`) is inferred as the length of the vector. ### Methods - **`from_vec(rows: Vec) -> PackedGF2Matrix`**: Consumes the provided vector to create the matrix. - **`from_vec_referenced(rows: &[T]) -> PackedGF2Matrix`**: Clones data from a reference to create the matrix. ### Returns - `PackedGF2Matrix` - The constructed packed matrix. ### Example ```rust use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; // from_vec — consumes the vector let rows = vec![0b1010u8, 0b0110u8, 0b1100u8]; let m1 = PackedGF2Matrix::from_vec(rows); // m1.ncols() will be 3 // from_vec_referenced — clones from reference let rows2 = vec![0b1010u8, 0b0110u8]; let m2 = PackedGF2Matrix::from_vec_referenced(&rows2); // m2.ncols() will be 2 ``` ``` -------------------------------- ### Compute Reduced Row Echelon Form (RREF) Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Calculate the RREF of a GF2Matrix and retrieve the history of row operations applied. Each operation is encoded as a tuple (i, j) representing row_i ← row_i + row_j (mod 2). ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], vec![0, 1, 0, 1], ]); let (rref, ops) = mat.echelon_form(); println!("RREF: {:?}", rref.elements); // Output: [[1, 0, 0, 0], [0, 1, 0, 1], [0, 0, 0, 0]] println!("Row operations: {:?}", ops); // Output: [(2, 1)] — row 2 was XOR'd with row 1 } ``` -------------------------------- ### Compute Reduced Row Echelon Form on Packed Matrix Source: https://context7.com/lucabonamino/lin_algebra/llms.txt The `echelon_form` method computes the RREF of a `PackedGF2Matrix` using efficient XOR-based row operations. It returns the transformed matrix and a history of row operations. ```rust use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; fn main() { // Rows (MSB-first, 3 bits): 0b110=[1,1,0], 0b101=[1,0,1], 0b011=[0,1,1] let packed = PackedGF2Matrix::::new( vec![0b110u8, 0b101u8, 0b011u8], 3, ); let (echelon, ops) = packed.echelon_form(); println!("Echelon rows: {:?}", (0..echelon.nrows()).map(|i| echelon.row(i)).collect::>()); println!("Operations: {:?}", ops); // The matrix is reduced to RREF over GF(2) using XOR row additions } ``` -------------------------------- ### Bit-Packed Matrix Representation Source: https://context7.com/lucabonamino/lin_algebra/llms.txt `PackedGF2Matrix` uses unsigned integers to store matrix rows, enabling memory efficiency and XOR-based operations. Each bit in the integer corresponds to a GF(2) entry. ```rust use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; fn main() { // 3×4 matrix: each u8 encodes one row (4 significant bits) // Row 0: 0b1000 = [1,0,0,0] (MSB first) // Row 1: 0b0101 = [0,1,0,1] // Row 2: 0b0101 = [0,1,0,1] let packed = PackedGF2Matrix::::new( vec![0b1000u8, 0b0101u8, 0b0101u8], 4, ); println!("Rows: {}, Cols: {}", packed.nrows(), packed.ncols()); // Output: Rows: 3, Cols: 4 println!("Row 1 (packed): {:08b}", packed.row(1)); // Output: Row 1 (packed): 00000101 } ``` -------------------------------- ### PackedGF2Matrix::echelon_form Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Computes the reduced row echelon form (RREF) of a bit-packed GF(2) matrix using efficient XOR-based row operations. ```APIDOC ## PackedGF2Matrix::echelon_form — RREF on Packed Matrix ### Description Computes the reduced row echelon form of a bit-packed GF(2) matrix using XOR-based row operations directly on the packed integer rows. Returns the transformed matrix and the row-operation history using the same `(i, j)` convention as `GF2Matrix::echelon_form`. ### Method `echelon_form() -> (PackedGF2Matrix, Vec<(usize, usize)>)` ### Parameters None ### Response - `PackedGF2Matrix`: The matrix in reduced row echelon form. - `Vec<(usize, usize)>`: A history of row operations performed. ### Request Example ```rust use lin_algebra::packed_gf2_matrix::PackedGF2Matrix; let packed = PackedGF2Matrix::::new(vec![0b110u8, 0b101u8, 0b011u8], 3); let (echelon, ops) = packed.echelon_form(); println!("Echelon rows: {:?}", (0..echelon.nrows()).map(|i| echelon.row(i)).collect::>()); println!("Operations: {:?}", ops); ``` ### Response Example ``` Echelon rows: [1, 0, 0] Operations: [(0, 1), (0, 2)] ``` ``` -------------------------------- ### GF2Matrix::rank Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Returns the rank of the linear application represented by the matrix, which is the number of linearly independent rows. ```APIDOC ## GF2Matrix::rank ### Description Calculates and returns the rank of the matrix, which corresponds to the dimension of the image (column space) or the number of linearly independent rows. ### Method `rank() -> usize` ### Parameters None ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let full_rank = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], ]); println!("Rank: {}", full_rank.rank()); ``` ### Response #### Success Response (200) - **rank** (usize) - The rank of the matrix. #### Response Example ```rust // Assuming full_rank is defined as in the example println!("Rank: {}", full_rank.rank()); // Output: Rank: 2 ``` ``` -------------------------------- ### Calculate Matrix Rank Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Determine the rank of the linear map represented by a GF2Matrix. The rank is the number of linearly independent rows. RREF is computed if the matrix is not already in that form. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { let full_rank = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], ]); println!("Rank: {}", full_rank.rank()); // Output: Rank: 2 let rank_one = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![1, 0, 0, 0], // duplicate row → rank drops ]); println!("Rank: {}", rank_one.rank()); // Output: Rank: 1 } ``` -------------------------------- ### GF2Matrix::get_pivot Source: https://context7.com/lucabonamino/lin_algebra/llms.txt A static utility function to find the pivot column index for a given row (represented as a slice). Returns None if the row consists entirely of zeros. ```APIDOC ## MatrixCommon::get_pivot — Find Pivot Column ### Description A static utility on `GF2Matrix` that returns the column index of the first non-zero entry in a row (the pivot position), or `None` if the row is all zeros. ### Method `GF2Matrix::get_pivot(row: &[u8]) -> Option` ### Parameters - **row** (`&[u8]`): A slice representing a row of the matrix. ### Response - `Option`: The column index of the pivot, or `None` if the row is all zeros. ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixCommon; assert_eq!(GF2Matrix::get_pivot(&[1, 0, 0, 1]), Some(0)); assert_eq!(GF2Matrix::get_pivot(&[0, 0, 0, 0]), None); println!("Pivot of [0,0,1,0]: {:?}", GF2Matrix::get_pivot(&[0, 0, 1, 0])); ``` ### Response Example ``` Pivot of [0,0,1,0]: Some(2) ``` ``` -------------------------------- ### GF2Matrix::echelon_form Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Computes the reduced row echelon form (RREF) of the matrix over GF(2) and returns the transformed matrix along with the history of row operations applied. ```APIDOC ## GF2Matrix::echelon_form ### Description Computes the reduced row echelon form (RREF) of the matrix over GF(2). It returns the RREF matrix and a vector detailing the row operations performed. ### Method `echelon_form() -> (GF2Matrix, Vec<(usize, usize)>)` ### Parameters None ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; let mat = GF2Matrix::new(vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 1], vec![0, 1, 0, 1], ]); let (rref, ops) = mat.echelon_form(); ``` ### Response #### Success Response (200) - **rref** (GF2Matrix) - The reduced row echelon form of the matrix. - **ops** (Vec<(usize, usize)>) - A vector of tuples representing row operations. Each tuple `(i, j)` signifies `row_i ← row_i + row_j` (mod 2). #### Response Example ```rust // Assuming mat is defined as in the example let (rref, ops) = mat.echelon_form(); println!("RREF: {:?}", rref.elements); // Output: [[1, 0, 0, 0], [0, 1, 0, 1], [0, 0, 0, 0]] println!("Row operations: {:?}", ops); // Output: [(2, 1)] ``` ``` -------------------------------- ### Check if GF2Matrix is in Reduced Row Echelon Form Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Use `is_reduced_echelon` to determine if a GF2Matrix is in RREF. This is useful for optimizing rank, kernel, and image computations by avoiding redundant RREF calculations. ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; fn main() { let rref = GF2Matrix::new(vec![ vec![1, 0, 0, 1], vec![0, 1, 0, 1], ]); println!("Is RREF: {}", rref.is_reduced_echelon()); // Output: Is RREF: true let not_rref = GF2Matrix::new(vec![ vec![1, 1, 0, 1], vec![0, 1, 0, 1], ]); println!("Is RREF: {}", not_rref.is_reduced_echelon()); // Output: Is RREF: false } ``` -------------------------------- ### GF2Matrix::is_reduced_echelon Source: https://context7.com/lucabonamino/lin_algebra/llms.txt Checks if a GF2Matrix is in reduced row echelon form (RREF). This is useful for optimizing subsequent computations like rank or kernel. ```APIDOC ## GF2Matrix::is_reduced_echelon — Check RREF ### Description Returns `true` if the matrix is already in reduced row echelon form. This is used internally before computing rank, kernel, and image to avoid redundant RREF computations. ### Method `is_reduced_echelon()` ### Parameters None ### Response - `bool`: `true` if the matrix is in RREF, `false` otherwise. ### Request Example ```rust use lin_algebra::gf2_matrix::GF2Matrix; use lin_algebra::matrix::MatrixTrait; let rref = GF2Matrix::new(vec![vec![1, 0, 0, 1], vec![0, 1, 0, 1]]); println!("Is RREF: {}", rref.is_reduced_echelon()); ``` ### Response Example ``` Is RREF: true ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.