Skip to content

Commit 05403cf

Browse files
committed
documented all methods
1 parent db196fa commit 05403cf

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

src/lib.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//! Simple & extensible type-checked matrixes
2+
//!
3+
//! This was made as a learning project and thrives to provide matrices generic over any type.
4+
//! Some basic matrix manipulation operations are implemented for the matrix assuming the concrete type implements the required traits.
5+
//! The main selling point is that most operations fail to compile if the operation is impossible. This is done through the use of `min_const_generic` (rustc v1.51) and allows operations such as matrix product to always work if the code compiles.
6+
//!
7+
//! Although most operations implemented are done so with mathematical matrices in mind the type itself can be used for any use-case
8+
//!
9+
//! For how to use this crate, refer to [`Matrix`]
10+
111
#![feature(maybe_uninit_extra)]
212
#![feature(array_methods)]
313
use num::traits::{One, Zero};
@@ -9,8 +19,9 @@ use std::slice::{Iter, IterMut};
919
use thiserror::Error;
1020

1121
#[derive(Debug, PartialEq, Eq, Clone)]
12-
///C is the type of the coefficients of the Matrix
13-
///ROWS is the number of lines & COLS the number of columns
22+
/// Matrix type generic over its coefficient and dimensions
23+
///
24+
/// This struct contains most of the crate's features.
1425
pub struct Matrix<C, const ROWS: usize, const COLS: usize> {
1526
data: [[C; COLS]; ROWS],
1627
}
@@ -226,9 +237,19 @@ impl<'a, C: 'a, const SIZE: usize> Matrix<C, SIZE, SIZE>
226237
where
227238
C: Clone + MulAssign<&'a C> + AddAssign<&'a C>,
228239
{
229-
///Permutation operation between `source` and `target` rows
240+
/// Permutes two rows.
241+
///
242+
/// Permutation is an operation that can be understood as swapping a line for another.
243+
/// Returns an Error if either `source` of `target` is out of bounds, that is greater than `SIZE`.
244+
///
245+
/// # Example
230246
///
231-
///This can be understood as swapping the `source` row with the `target` one
247+
/// ```
248+
///# use matrix::Matrix;
249+
/// let mut mat = Matrix::from([[1, 2, 1], [3, 4, 1], [5, 6, 1]]);
250+
/// mat.permute(0, 1);
251+
/// assert_eq!(mat, Matrix::from([[3, 4, 1], [1, 2, 1], [5, 6, 1]])); //look at the order of the lines
252+
///```
232253
pub fn permute(&mut self, source: usize, target: usize) -> Result<(), Error> {
233254
if (target >= SIZE) | (source >= SIZE) {
234255
return Err(Error::OutOfBounds);
@@ -238,9 +259,19 @@ where
238259
Ok(())
239260
}
240261

241-
///Dilation operation on row `row`.
262+
/// Dilates a row
263+
///
264+
/// To dilate a row is to multiply all the coefficients of the row by a factor.
265+
/// Returns an Error if `row` is out of bounds, that is greater than `SIZE`.
266+
///
267+
/// # Example
242268
///
243-
///Line dilation is to multiply all coefficients of a row by a factor
269+
/// ```
270+
///# use matrix::Matrix;
271+
/// let mut mat = Matrix::from([[1, 2, 1], [3, 4, 1], [5, 6, 1]]);
272+
/// mat.dilate(0, &2);
273+
/// assert_eq!(mat, Matrix::from([[2, 4, 2], [3, 4, 1], [5, 6, 1]]));
274+
///```
244275
pub fn dilate(&mut self, row: usize, factor: &'a C) -> Result<(), Error> {
245276
match self.data.get_mut(row) {
246277
None => Err(Error::OutOfBounds),
@@ -254,9 +285,25 @@ impl<C, const SIZE: usize> Matrix<C, SIZE, SIZE>
254285
where
255286
for<'a> C: Clone + MulAssign<&'a C> + AddAssign<&'a C>,
256287
{
257-
///Transvection operation on row `source` with row `other` and `factor`
288+
/// Applies a transvection from a row to another
289+
///
290+
/// Tto transvect is to add a row to another, coefficient by coefficient.
291+
/// Returns OutOfBounds if either `source` of `other` is out of bounds, that is greater than `SIZE`.
292+
///
293+
/// In debug it will also return WrongOperation `source` and `other` are the same row.
294+
/// If you encounter this issue, use [`dilate`] instead.
295+
/// Because the operation can also with `transvect` although [`dilate`] is better, `WrongOperation` never occurs in release.
296+
///
297+
/// [`dilate`]: #method.dilate
258298
///
259-
///Line transvection is to add a row to a source row for each coefficient
299+
/// # Examples
300+
///
301+
/// ```
302+
///# use matrix::Matrix;
303+
/// let mut mat = Matrix::from([[1, 2, 1], [3, 4, 1], [5, 6, 1]]);
304+
/// mat.transvect(0, 2);
305+
/// assert_eq!(mat, Matrix::from([[6, 8, 2], [3, 4, 1], [5, 6, 1]]));
306+
///```
260307
pub fn transvect(&mut self, source: usize, other: usize) -> Result<(), Error> {
261308
if (other >= SIZE) | (source >= SIZE) {
262309
return Err(Error::OutOfBounds);

0 commit comments

Comments
 (0)