Skip to main content

lll_rs/matrix/
mod.rs

1//! Basic matrix structure for LLL
2
3use crate::vector::Vector;
4
5use std::{
6    fmt::{self, Debug},
7    ops::{Index, IndexMut},
8};
9
10/// A `Matrix` is a collection of `Vector`s
11pub struct Matrix<T: Vector> {
12    /// Internal representation as a list of elements of type `T`
13    columns: Vec<T>,
14
15    /// Dimensions of the matrix
16    dimensions: (usize, usize),
17}
18
19impl<T: Vector> Matrix<T>
20where
21    T: Clone,
22{
23    /// Initialise an empty `Matrix`
24    ///      - `col_num`: number of columns
25    ///      - `col_dim`: number of rows
26    pub fn init(col_num: usize, col_dim: usize) -> Self {
27        Self {
28            columns: vec![T::init(col_dim); col_num],
29            dimensions: (col_num, col_dim),
30        }
31    }
32
33    /// Return the matrix dimensions
34    pub fn dimensions(&self) -> (usize, usize) {
35        self.dimensions
36    }
37
38    /// Swap two columns of the matrix
39    pub fn swap(&mut self, i: usize, j: usize) {
40        self.columns.swap(i, j);
41    }
42}
43
44/// Direct access to a column
45impl<T> Index<usize> for Matrix<T>
46where
47    T: Vector,
48{
49    type Output = T;
50
51    fn index(&self, index: usize) -> &T {
52        &self.columns[index]
53    }
54}
55
56/// Direct access to a column (mutable)
57impl<T> IndexMut<usize> for Matrix<T>
58where
59    T: Vector,
60{
61    fn index_mut(&mut self, index: usize) -> &mut T {
62        &mut self.columns[index]
63    }
64}
65
66impl<T> fmt::Debug for Matrix<T>
67where
68    T: Vector + Debug,
69{
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        writeln!(f, "{:?}\n", self.columns)
72    }
73}