From 2503e3da918d30bfc45b08ca5a68fa29a6d98a24 Mon Sep 17 00:00:00 2001 From: Srinath Kailasa Date: Mon, 26 Sep 2022 13:21:41 +0100 Subject: [PATCH 1/6] Lint --- README.md | 4 +- src/addition.rs | 3 +- src/base_matrix.rs | 12 ++--- src/data_container.rs | 9 ++-- src/examples.rs | 22 +++++----- src/global.rs | 4 +- src/layouts.rs | 29 ++++++------ src/layouts/arbitrary_stride_column_major.rs | 2 +- src/layouts/arbitrary_stride_column_vector.rs | 2 +- src/layouts/arbitrary_stride_row_major.rs | 4 +- src/layouts/arbitrary_stride_row_vector.rs | 2 +- src/layouts/column_major.rs | 2 +- src/layouts/column_vector.rs | 2 +- src/layouts/row_major.rs | 2 +- src/layouts/row_vector.rs | 2 +- src/layouts/upper_triangular.rs | 9 +++- src/lib.rs | 31 +++++++------ src/macros.rs | 2 - src/matrix.rs | 12 ++--- src/matrix/base_methods.rs | 11 +++-- src/matrix/random.rs | 3 +- src/matrix_multiply.rs | 3 +- src/matrix_ref.rs | 6 +-- src/scalar_mult.rs | 2 +- src/traits.rs | 14 +++--- src/traits/layout.rs | 26 +++++------ src/traits/matrix.rs | 26 +++-------- src/traits/random_access.rs | 44 +++++++++---------- src/traits/size.rs | 9 ++-- src/types.rs | 2 - 30 files changed, 134 insertions(+), 167 deletions(-) diff --git a/README.md b/README.md index 279d35d..201a2eb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# householder -A native Rust library for advanced Linear Algebra routines. +# Householder +A Rust native library for advanced Linear Algebra routines. The library is in a very early development stage and not recommended yet for production use. diff --git a/src/addition.rs b/src/addition.rs index ab55449..ebd0087 100644 --- a/src/addition.rs +++ b/src/addition.rs @@ -1,5 +1,5 @@ //! Addition of two matrices. -//! +//! //! This module defines a type [AdditionMat] that represents the addition of two //! matrices. Two matrices can be added together if they have the same dimension and //! same index layout, meaning a 1d indexing traverses both matrices in the same order. @@ -234,7 +234,6 @@ mod test { let mut mat1 = MatrixD::::zeros_from_dim(2, 3); let mut mat2 = MatrixD::::zeros_from_dim(2, 3); - *mat1.get_mut(1, 2) = 5.0; *mat2.get_mut(1, 2) = 6.0; diff --git a/src/base_matrix.rs b/src/base_matrix.rs index 83c9bec..67b0428 100644 --- a/src/base_matrix.rs +++ b/src/base_matrix.rs @@ -1,13 +1,13 @@ //! Definition of a base matrix. -//! +//! //! A base matrix is an implementation of a matrix that //! is directly associated with a [DataContainer] (typically a memory region). -//! -//! The user should never interact with [BaseMatrix] directly. Rather, the +//! +//! The user should never interact with [BaseMatrix] directly. Rather, the //! relevant user type is a [GenericBaseMatrix](crate::matrix::GenericBaseMatrix), -//! which is a [Matrix](crate::matrix::Matrix) that forwards call to the +//! which is a [Matrix](crate::matrix::Matrix) that forwards call to the //! [BaseMatrix] implementation. -//! +//! use crate::data_container::{DataContainer, DataContainerMut}; use crate::layouts::*; use crate::traits::*; @@ -178,4 +178,4 @@ impl< self.data .get_unchecked_mut(self.layout.convert_1d_raw(index)) } -} \ No newline at end of file +} diff --git a/src/data_container.rs b/src/data_container.rs index f5d8ef7..33673c0 100644 --- a/src/data_container.rs +++ b/src/data_container.rs @@ -1,11 +1,11 @@ //! Data containers are simple structures that hold data and allow access to it. -//! +//! //! A data container in `householder` is defined through the [DataContainer] trait. -//! It is a very simple interface that provides low-level access methods and +//! It is a very simple interface that provides low-level access methods and //! knows about how many elements are in the data container. Some data containers //! are pre-defined, namely -//! -//! +//! +//! use crate::types::IndexType; use crate::types::Scalar; @@ -67,7 +67,6 @@ pub trait DataContainerMut: DataContainer { self.number_of_elements() ); - assert!( last <= self.number_of_elements(), "Value of 'last' {} must be smaller or equal to the number of elements {}.", diff --git a/src/examples.rs b/src/examples.rs index 6a51164..061ac01 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -1,17 +1,17 @@ //! Common Examples -//! +//! //! # Defining matrices and vectors -//! +//! //! We can define a simple 3 x 5 matrix with `f64` entries //! as follows. -//! +//! //! ``` //! # use householder::*; //! let mut mat = mat![f64, (3, 5)]; //! ``` //! The result is a row-major matrix. To define a column-major //! matrix we use -//! +//! //! ``` //! # use householder::*; //! let mut mat = mat![f64, (3, 5), ColumnMajor]; @@ -21,13 +21,13 @@ //! # use householder::*; //! let mut vec = vector![f64, 5]; //! ``` -//! +//! //! For a row vector use //! ``` //! # use householder::*; //! let mut vec = vector![f64, 5, RowVector]; //! ``` -//! +//! //! A normally distributed random matrix is obtained as //! ``` //! # use householder::*; @@ -41,7 +41,7 @@ //! mat.fill_from_rand_standard_normal(&mut rng); //! ``` //! # Accessing entries -//! +//! //! We can access and modify entries of a matrix //! with bounds checks in the following way. //! ``` @@ -59,7 +59,7 @@ //! let mut mat = mat![f64, (3, 5)]; //! *mat.get_mut(3, 5) = 4.0; //! ``` -//! +//! //! ```should_panic //! # use householder::*; //! let mut mat = mat![f64, (3, 5)]; @@ -76,7 +76,7 @@ //! } //! ``` //! # Operations on matrices and vectors. -//! +//! //! Matrix/vector sums or products with a scalar are written as //! ``` //! # use householder::*; @@ -92,7 +92,7 @@ //! The `eval` statement instantiates a new matrix and then iterates //! through the new matrix to componentwise fill up the matrix with //! the result of the right-hand side componentwise operation. -//! +//! //! Matrix/vector and matrix/matrix products are implemented via //! the [matrixmultiply] crate. //! ``` @@ -104,7 +104,7 @@ //! let res2 = row_vec.dot(&mat); //! ``` //! # Access to submatrices. -//! +//! //! We can access a single subblock of a matrix as follows. //! ``` //! # use householder::*; diff --git a/src/global.rs b/src/global.rs index d602ab4..8f312ee 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,8 +1,8 @@ //! Re-exports for definitions to be made available on crate level. +pub use crate::data_container::*; +pub use crate::layouts::*; pub use crate::matrix::*; pub use crate::matrix_multiply::{Dot, MatMul}; pub use crate::tools::*; pub use crate::traits::*; -pub use crate::layouts::*; -pub use crate::data_container::*; diff --git a/src/layouts.rs b/src/layouts.rs index 8d380ef..754dc47 100644 --- a/src/layouts.rs +++ b/src/layouts.rs @@ -1,28 +1,25 @@ //! This module combines different concrete memory layout types. -//! +//! //! Memory layouts implement the [LayoutType](crate::traits::LayoutType) trait //! and describe iteration order of matrix elements and their map to memory //! locations. -pub mod row_major; +pub mod arbitrary_stride_column_major; +pub mod arbitrary_stride_column_vector; +pub mod arbitrary_stride_row_major; +pub mod arbitrary_stride_row_vector; pub mod column_major; -pub mod upper_triangular; pub mod column_vector; +pub mod row_major; pub mod row_vector; -pub mod arbitrary_stride_row_major; -pub mod arbitrary_stride_column_major; -pub mod arbitrary_stride_row_vector; -pub mod arbitrary_stride_column_vector; - - +pub mod upper_triangular; -pub use row_major::*; +pub use arbitrary_stride_column_major::*; +pub use arbitrary_stride_column_vector::*; +pub use arbitrary_stride_row_major::*; +pub use arbitrary_stride_row_vector::*; pub use column_major::*; -pub use upper_triangular::*; pub use column_vector::*; +pub use row_major::*; pub use row_vector::*; -pub use arbitrary_stride_row_major::*; -pub use arbitrary_stride_column_major::*; -pub use arbitrary_stride_row_vector::*; -pub use arbitrary_stride_column_vector::*; - +pub use upper_triangular::*; diff --git a/src/layouts/arbitrary_stride_column_major.rs b/src/layouts/arbitrary_stride_column_major.rs index bdba4f3..3f062a4 100644 --- a/src/layouts/arbitrary_stride_column_major.rs +++ b/src/layouts/arbitrary_stride_column_major.rs @@ -1,5 +1,5 @@ //! Column major layout with arbitrary stride. -//! +//! //! This layout uses an arbitrary stride in memory with 1d indexing //! in column major order. For further information on memory layouts //! see [crate::traits::layout]. diff --git a/src/layouts/arbitrary_stride_column_vector.rs b/src/layouts/arbitrary_stride_column_vector.rs index 11b8bac..ab1251f 100644 --- a/src/layouts/arbitrary_stride_column_vector.rs +++ b/src/layouts/arbitrary_stride_column_vector.rs @@ -1,5 +1,5 @@ //! Column vector with arbitrary stride. -//! +//! //! This layout describes a column vector with arbitrary //! stride. The stride vector is by definition of the form //! `(r, 1)` for this type, where `r` is the distance of two diff --git a/src/layouts/arbitrary_stride_row_major.rs b/src/layouts/arbitrary_stride_row_major.rs index 11d3395..e611a2d 100644 --- a/src/layouts/arbitrary_stride_row_major.rs +++ b/src/layouts/arbitrary_stride_row_major.rs @@ -1,9 +1,9 @@ //! Row major layout with arbitrary stride. -//! +//! //! This layout uses an arbitrary stride in memory with 1d indexing //! in row major order. For further information on memory layouts //! see [crate::traits::layout]. - + use crate::traits::*; use crate::types::IndexType; diff --git a/src/layouts/arbitrary_stride_row_vector.rs b/src/layouts/arbitrary_stride_row_vector.rs index bf2c50b..86ccf21 100644 --- a/src/layouts/arbitrary_stride_row_vector.rs +++ b/src/layouts/arbitrary_stride_row_vector.rs @@ -1,5 +1,5 @@ //! Row vector with arbitrary stride. -//! +//! //! This layout describes a row vector with arbitrary //! stride. The stride vector is by definition of the form //! `(1, c)` for this type, where `c` is the distance of two diff --git a/src/layouts/column_major.rs b/src/layouts/column_major.rs index 88e8e6d..6c25ada 100644 --- a/src/layouts/column_major.rs +++ b/src/layouts/column_major.rs @@ -1,5 +1,5 @@ //! Column major layout. -//! +//! //! This layout describes a column major memory order //! with column major 1d indexing. For further information on memory layouts //! see [crate::traits::layout]. diff --git a/src/layouts/column_vector.rs b/src/layouts/column_vector.rs index 2086e75..6b18f6d 100644 --- a/src/layouts/column_vector.rs +++ b/src/layouts/column_vector.rs @@ -1,5 +1,5 @@ //! Column vector layout. -//! +//! //! This layout describes a column vector whose elements //! are in consecutive order in memory. diff --git a/src/layouts/row_major.rs b/src/layouts/row_major.rs index e0e86c0..e9a6289 100644 --- a/src/layouts/row_major.rs +++ b/src/layouts/row_major.rs @@ -1,5 +1,5 @@ //! Row major layout. -//! +//! //! This layout describes a row major memory order //! with row major 1d indexing. For further information on memory layouts //! see [crate::traits::layout]. diff --git a/src/layouts/row_vector.rs b/src/layouts/row_vector.rs index a7221c7..cc4cfc3 100644 --- a/src/layouts/row_vector.rs +++ b/src/layouts/row_vector.rs @@ -1,5 +1,5 @@ //! Row vector layout. -//! +//! //! This layout describes a row vector whose elements //! are in consecutive order in memory. diff --git a/src/layouts/upper_triangular.rs b/src/layouts/upper_triangular.rs index dc0e3b3..8850079 100644 --- a/src/layouts/upper_triangular.rs +++ b/src/layouts/upper_triangular.rs @@ -1,5 +1,5 @@ //! Upper triangular layout. -//! +//! //! This is a special layout for upper triangular matrices //! whose elemenst are consecutively ordered in memory without //! storing the zero lower triangular elements. @@ -38,7 +38,12 @@ impl LayoutType for UpperTriangular { #[inline] fn convert_2d_1d(&self, row: IndexType, col: IndexType) -> IndexType { - assert!(col >= row, "For upper triangular require 'col' >= 'row': row={}, col={}", row, col); + assert!( + col >= row, + "For upper triangular require 'col' >= 'row': row={}, col={}", + row, + col + ); ((self.dim - row) * (self.dim - row - 1)) / 2 + col - row } diff --git a/src/lib.rs b/src/lib.rs index b753067..257627c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,53 +1,52 @@ //! A Rust native linear algebra library -//! +//! //! The goal of `householder` is the development of a Rust native //! linear algebra library that is performant and does not require //! external BLAS/Lapack linkage or dependencies. -//! +//! //! The library is early stage. The core data structures are implemented //! and functionality is continuously being added. -//! +//! //! The core of `householder` is the [Matrix](crate::matrix::Matrix) type, //! which supports fixed size implementations, dynamic size implementations, //! and specialises also to vectors. It is agnostic to underlying storage //! layouts (i.e. row or column major) and also supports layouts with //! arbitrary strides. -//! +//! //! Algebraic operations on matrices are implemented using a system that is akin //! to expression templates. Adding matrices or multiplying them with a scalar //! is not immediately executed but creates a new type that stores the information //! about the operation. Only when the user asks for the evaluation, all operations //! are executed in a single pass without creating temporaries. -//! +//! //! Matrix-matrix products are implemented through the [matrixmultiply](matrixmultiply) //! crate. We are in the process of implementing more advanced linear algebra routines //! (e.g. LU, QR, etc.). But these are not yet available. The focus is on implementing //! modern blocked multi-threaded routines whose performance is competitive with Lapack. -//! +//! //! To learn more about `householder` we recommend the user to read the following bits //! of information. -//! +//! //! - [Basic trait definitions](crate::traits) //! - [Matrix storage layouts](crate::layouts) //! - [The Matrix type](crate::matrix) //! - [Examples](crate::examples) pub mod data_container; -pub mod types; -pub mod traits; +pub mod examples; pub mod layouts; -pub mod tools; -pub mod matrix; pub mod macros; -pub mod examples; - +pub mod matrix; +pub mod tools; +pub mod traits; +pub mod types; +pub mod addition; pub mod base_matrix; +pub mod global; +pub mod matrix_multiply; pub mod matrix_ref; pub mod scalar_mult; -pub mod matrix_multiply; -pub mod addition; -pub mod global; pub use cauchy::Scalar; pub use global::*; diff --git a/src/macros.rs b/src/macros.rs index fb4c975..86568dc 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -65,7 +65,6 @@ macro_rules! rand_vector { }}; } - #[cfg(test)] mod test { @@ -108,5 +107,4 @@ mod test { assert_eq!(vec.dim(), (5, 1)); } - } diff --git a/src/matrix.rs b/src/matrix.rs index 3dd9b21..a55495e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -9,24 +9,26 @@ //! A matrix is generic over the following parameters: //! - `Item`. Implements the [Scalar] trait and represents the underlying scalar type //! of the matrix. -//! - `MatImpl`. he actual implementation of the matrix. It must itself implement the -//! trait [MatrixTrait] or [MatrixTraitMut] depending on wheter mutable access +//! - `MatImpl`. The actual implementation of the matrix. It must itself implement the +//! trait [MatrixTrait] or [MatrixTraitMut] depending on whether mutable access //! is required. //! - `L`. A given type that implements the [LayoutType] trait and specifies the memory layout //! of the matrix. //! - `RS`. A type that implements [SizeType] and specifies whether the row dimension is known //! at compile time or dynamically at runtime. //! - `CS`. A type that implements [SizeType] and specifies whether the column dimension is -//! known at compile time or dynamically at runtime. +//! known at compile time or dynamically at runtime. +pub mod base_methods; pub mod common_impl; pub mod constructors; -pub mod base_methods; pub mod matrix_slices; pub mod random; use crate::base_matrix::BaseMatrix; -use crate::data_container::{ArrayContainer, DataContainer, VectorContainer, SliceContainer, SliceContainerMut}; +use crate::data_container::{ + ArrayContainer, DataContainer, SliceContainer, SliceContainerMut, VectorContainer, +}; use crate::layouts::*; use crate::matrix_ref::MatrixRef; use crate::traits::*; diff --git a/src/matrix/base_methods.rs b/src/matrix/base_methods.rs index 650bb31..bf00867 100644 --- a/src/matrix/base_methods.rs +++ b/src/matrix/base_methods.rs @@ -6,7 +6,7 @@ use crate::data_container::{DataContainer, DataContainerMut}; use crate::traits::*; use crate::types::*; -use super::{GenericBaseMatrixMut, GenericBaseMatrix}; +use super::{GenericBaseMatrix, GenericBaseMatrixMut}; impl< Item: Scalar, @@ -17,7 +17,7 @@ impl< > GenericBaseMatrixMut { /// Apply a callable to each element of a matrix. - /// + /// /// The callable `f` takes a mutable reference to a matrix /// element. pub fn for_each(&mut self, mut f: F) { @@ -34,8 +34,7 @@ impl< RS: SizeIdentifier, CS: SizeIdentifier, > GenericBaseMatrix - { - +{ /// Return a pointer to the start of the underlying memory region. #[inline] pub fn get_pointer(&self) -> *const Item { @@ -43,7 +42,7 @@ impl< } /// Return a region of the matrix as memory slice. - /// + /// /// The parameters `first` and `last` are with respect to raw /// indexing in the memory. So for nontrivial strides this will /// be different to the 1d indices in standard row or column major @@ -71,7 +70,7 @@ impl< } /// Return a region of the matrix as mutable memory slice. - /// + /// /// The parameters `first` and `last` are with respect to raw /// indexing in the memory. So for nontrivial strides this will /// be different to the 1d indices in standard row or column major diff --git a/src/matrix/random.rs b/src/matrix/random.rs index 17bde8e..22d41ae 100644 --- a/src/matrix/random.rs +++ b/src/matrix/random.rs @@ -1,11 +1,11 @@ //! Methods for the creation of random matrices. use crate::data_container::DataContainerMut; +use crate::tools::*; use crate::traits::*; use crate::types::*; use rand::prelude::*; use rand_distr::StandardNormal; -use crate::tools::*; use super::GenericBaseMatrixMut; @@ -31,4 +31,3 @@ rand_impl!(f32); rand_impl!(f64); rand_impl!(c32); rand_impl!(c64); - diff --git a/src/matrix_multiply.rs b/src/matrix_multiply.rs index 8073b3c..510e1ef 100644 --- a/src/matrix_multiply.rs +++ b/src/matrix_multiply.rs @@ -59,7 +59,6 @@ pub trait MatMul< macro_rules! dot_impl { ($Scalar:ty) => { - // Matrix x Matrix = Matrix impl< L1: StridedLayoutType, @@ -349,10 +348,10 @@ dot_impl!(f64); mod test { use super::*; + use crate::matrix::*; use crate::tools::RandScalar; use approx::assert_ulps_eq; use rand_distr::StandardNormal; - use crate::matrix::*; use rand::prelude::*; diff --git a/src/matrix_ref.rs b/src/matrix_ref.rs index b2b01d9..f2f0e71 100644 --- a/src/matrix_ref.rs +++ b/src/matrix_ref.rs @@ -1,5 +1,5 @@ //! A matrix that holds a reference to another matrix. -//! +//! //! The type defined in this module translates a reference to a matrix into an owned matrix. //! Why is this necessary? Consider the code `let sum = mat1 + mat2` with `mat1` and `mat2` //! matrices. The result `sum` is an addition type that now owns `mat1` and `mat2`. But @@ -11,7 +11,7 @@ //! solution is to create a type that turns a reference to a matrix into an owned matrix. //! This is what [MatrixRef] is doing. It simply takes a reference to a matrix and forwards //! all matrix operations to the reference. Hence, an expression of the form `&mat1 + mat2` will -//! first be converted into an expression similar to `MatrixRef(&mat1) + mat2`, and then +//! first be converted into an expression similar to `MatrixRef(&mat1) + mat2`, and then //! both terms passed onto the addition type, which takes ownership of both terms. use crate::matrix::Matrix; @@ -47,7 +47,6 @@ impl< pub fn new(mat: &'a Matrix) -> Self { Self(mat, PhantomData, PhantomData, PhantomData, PhantomData) } - } pub struct MatrixRefMut<'a, Item, MatImpl, L, RS, CS>( @@ -110,7 +109,6 @@ macro_rules! matrix_ref_traits { type C = CS; } - impl< 'a, Item: Scalar, diff --git a/src/scalar_mult.rs b/src/scalar_mult.rs index b11291e..52628bc 100644 --- a/src/scalar_mult.rs +++ b/src/scalar_mult.rs @@ -1,5 +1,5 @@ //! Multiplication of a matrix with a scalar -//! +//! //! This module implements the multiplication of a matrix with a scalar. Instead //! of immediately executing the product a new matrix is created whose implementation //! is a struct that holds the original operator and the scalar. On element access the diff --git a/src/traits.rs b/src/traits.rs index fce9591..9d62026 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,20 +1,20 @@ //! Trait definitions -//! +//! //! `householder` relies heavily on traits. This module //! collects the core trait definitions to define a matrix type. //! The following functionality is available through traits. -//! +//! //! - [Definition of random access operations.](random_access) //! - [Size type descriptions (e.g. fixed given dimenion or dynamically allocated).](size) //! - [Matrix storage layout.](layout) //! - [Summary trait defining a matrix.](matrix) - -pub mod random_access; -pub mod size; + pub mod layout; pub mod matrix; +pub mod random_access; +pub mod size; -pub use random_access::*; -pub use size::*; pub use layout::*; pub use matrix::*; +pub use random_access::*; +pub use size::*; diff --git a/src/traits/layout.rs b/src/traits/layout.rs index 745e7c3..a59d192 100644 --- a/src/traits/layout.rs +++ b/src/traits/layout.rs @@ -1,5 +1,5 @@ //! Layout Definitions -//! +//! //! The traits in this module determine the memory layout of a matrix. //! Consider a simple matrix of the form //! \\[ @@ -7,7 +7,7 @@ //! 3 & 4 //! \end{bmatrix} //! \\] -//! +//! //! In **row-major** form this matrix is stored in memory in the order //! $\begin{bmatrix}1 & 2 & 3 & 4\end{bmatrix}$ In **column-major** form the matrix is stored in memory //! as $\begin{bmatrix}1 & 3 & 2 & 4\end{bmatrix}$. These are the two most commonly used @@ -16,7 +16,7 @@ //! to walk `r` positions to go from one row to the next, and `c` positions to walk from one //! column to the next. For the matrix $A$ the stride tuple is `(2, 1)` in **row-major** //! and `(1, 2)` in **column-major** form. -//! +//! //! Strides arise naturally in the description of submatrices. //! Imagine that the matrix $A$ is a submatrix of the bigger matrix. //! \\[ @@ -34,18 +34,18 @@ //! $\begin{bmatrix}1 & 2 & x & 3 & 4\end{bmatrix}$. The stride tuple associated with this //! layout is `(3, 1)`. The distance of elements within each row is `1` and the distance within //! each column is `3`. -//! +//! //! However, more complicated layouts are possible (e.g. a storage layout for upper triangular //! matrices). The trait here ensures as much generality as possible in defining memory //! layouts. -//! +//! //! Each matrix is assigned with a logical indexing that is either **row-major** or **column-major** //! and a physical layout. The logial indexing just determines whether a one-dimensional iterator //! iterates through the matrix elements by row or by column. Consider again that $A$ is submatrix //! of a larger $3\times 3$ matrix stored in **row-major** form. A logical **row-major** traversal //! of $A$ will always return *\begin{bmatrix}1 & 2 & 3 & 4\end{bmatrix}$ independent of the //! underlying physical layout. -//! +//! //! The logical indexing is determined by the two methods [convert_1d_2d](crate::traits::LayoutType::convert_1d_2d) //! and [convert_2d_1d](crate::traits::LayoutType::convert_2d_1d). These methods map between //! logical `(row, col)` index tuples and one dimensional indices. The translation to the @@ -56,13 +56,13 @@ //! For base **row-major** and **column-major** storage types the physical and logical layout //! are typical identical. But for more complex types (e.g. with arbitrary stride vectors) they //! are typically different from each other. -//! +//! //! The main trait in this module is the [LayoutType](crate::traits::LayoutType) trait. //! If this is implemented for a matrix the [Layout](crate::traits::Layout) is auto-implemented. //! This latter trait only provides a method to return the [LayoutType] implementation. //! This crate also provides a number of other traits. -//! - [BaseLayoutType]: Derives from [LayoutType] -//! and marks simple base traits that are suitable +//! - [BaseLayoutType]: Derives from [LayoutType] +//! and marks simple base traits that are suitable //! for logical indexing. Instantiations only depend on the matrix //! dimension and not e.g. non-standard strides. //! - [VectorBaseLayoutType]: Derives from [BaseLayoutType] @@ -72,19 +72,16 @@ //! and marks base layouts for matrices. //! - [StridedLayoutType]: Derives from [LayoutType] and //! marks layouts with non-trivial strides. -//! +//! //! `householder` provides a number of concrete layouts. These are defined //! in the [Layouts module](crate::layouts). - - use crate::types::IndexType; /// This trait defines base layout traits. These are layouts /// that can be instantiated purely from information about the /// matrix dimension. pub trait BaseLayoutType: LayoutType { - /// Create a new layout from providing the matrix dimension as `(rows, cols)` tuple. fn from_dimension(dim: (IndexType, IndexType)) -> Self; } @@ -93,7 +90,6 @@ pub trait BaseLayoutType: LayoutType { /// layouts which can be instantiated by only providing the /// length of the vector (e.g. a simple row or column vector). pub trait VectorBaseLayoutType: BaseLayoutType { - /// Create a new layout from providing the length of the vector. fn from_length(length: IndexType) -> Self; } @@ -107,7 +103,6 @@ pub trait StridedLayoutType: LayoutType {} /// The main trait defining a layout. For detailed information see the /// [module description](crate::traits::layout). pub trait LayoutType { - /// The associated logical layout for indexing /// matrix elements. type IndexLayout: BaseLayoutType; @@ -138,7 +133,6 @@ pub trait LayoutType { fn index_layout(&self) -> Self::IndexLayout; } - /// This layout provides a method to return layout information. /// It is auto-implemented for all objects that implement [LayoutType]. pub trait Layout { diff --git a/src/traits/matrix.rs b/src/traits/matrix.rs index 7f2445b..c209768 100644 --- a/src/traits/matrix.rs +++ b/src/traits/matrix.rs @@ -7,34 +7,22 @@ //! - [Layout]. Provides an interface to obtain layout information for the matrix. //! - [SizeType]. Specifies whether the row/column dimension is known at compile time //! or specified at runtime. -//! +//! //! [MatrixTraitMut] additionally depends on the trait [RandomAccessMut] to provide //! mutable access to matrix elements. -use crate::traits::{ - LayoutType, Layout, RandomAccess, RandomAccessMut, SizeIdentifier, - SizeType, -}; +use crate::traits::{Layout, LayoutType, RandomAccess, RandomAccessMut, SizeIdentifier, SizeType}; use crate::types::Scalar; /// Combined trait for basic matrix properties. See [crate::traits::matrix] /// for details. -pub trait MatrixTrait< - Item: Scalar, - L: LayoutType, - RS: SizeIdentifier, - CS: SizeIdentifier, ->: - RandomAccess + Layout + SizeType +pub trait MatrixTrait: + RandomAccess + Layout + SizeType { } /// Combined trait for mutable matrices. See [crate::traits::matrix] for details. -pub trait MatrixTraitMut< - Item: Scalar, - L: LayoutType, - RS: SizeIdentifier, - CS: SizeIdentifier, ->: RandomAccessMut + MatrixTrait +pub trait MatrixTraitMut: + RandomAccessMut + MatrixTrait { } @@ -43,7 +31,7 @@ impl< L: LayoutType, RS: SizeIdentifier, CS: SizeIdentifier, - Mat: RandomAccess + Layout + SizeType, + Mat: RandomAccess + Layout + SizeType, > MatrixTrait for Mat { } diff --git a/src/traits/random_access.rs b/src/traits/random_access.rs index 4098896..7c2b21d 100644 --- a/src/traits/random_access.rs +++ b/src/traits/random_access.rs @@ -1,30 +1,29 @@ //! Traits for random access of matrices -//! +//! //! The traits in this module define safe and unsafe random access //! methods for a matrix. The user needs to implement `UnsafeRandomAccess` -//! and `UnsafeRandomAccessMut` (for mutable access). -//! +//! and `UnsafeRandomAccessMut` (for mutable access). +//! //! If additionally the [Layout](crate::traits::Layout) is implemented (aut-implemented //! if the [LayoutType](crate::traits::LayoutType) trait is implemented), then -//! he corresponding safe traits `SafeRandomAccess` and +//! he corresponding safe traits `SafeRandomAccess` and //! `SafeRandomAccessMut` are auto-implemented. -//! +//! //! Each trait provides a two-dimensional and a one-dimensional access method, //! namely `get` and `get1d` (together with their mutable and unsafe variants). //! The two-dimensional access takes a row and a column and returns the corresponding -//! matrix element. The one-dimensional access takes a single -//! +//! matrix element. The one-dimensional access takes a single +//! //! The one-dimensional access //! takes a single `index` parameter that iterates through the matrix elements. //! It is recommended to use the [convert_1d_raw](crate::traits::LayoutType::convert_1d_raw) //! functions from that trait to implement `get1d` to ensure compatibility with //! the memory layout defined in that trait. -use crate::types::{IndexType, Scalar}; use crate::traits::{Layout, LayoutType}; +use crate::types::{IndexType, Scalar}; - -/// This trait provides unsafe access to the underlying data. See +/// This trait provides unsafe access to the underlying data. See /// [Random Access](crate::traits::random_access) for a description. pub trait UnsafeRandomAccess { type Item: Scalar; @@ -36,7 +35,7 @@ pub trait UnsafeRandomAccess { unsafe fn get1d_unchecked(&self, index: IndexType) -> Self::Item; } -/// This trait provides unsafe mutable access to the underlying data. See +/// This trait provides unsafe mutable access to the underlying data. See /// [Random Access](crate::traits::random_access) for a description. pub trait UnsafeRandomAccessMut { type Item: Scalar; @@ -48,10 +47,9 @@ pub trait UnsafeRandomAccessMut { unsafe fn get1d_unchecked_mut(&mut self, index: IndexType) -> &mut Self::Item; } -/// This trait provides bounds checked access to the underlying data. See +/// This trait provides bounds checked access to the underlying data. See /// [Random Access](crate::traits::random_access) for a description. pub trait RandomAccess: UnsafeRandomAccess { - /// Return the element at position (`row`, `col`). fn get(&self, row: usize, col: usize) -> Self::Item; @@ -59,10 +57,9 @@ pub trait RandomAccess: UnsafeRandomAccess { fn get1d(&self, elem: usize) -> Self::Item; } -/// This trait provides bounds checked mutable access to the underlying data. See +/// This trait provides bounds checked mutable access to the underlying data. See /// [Random Access](crate::traits::random_access) for a description. pub trait RandomAccessMut: UnsafeRandomAccessMut { - /// Return a mutable reference to the element at position (`row`, `col`). fn get_mut(&mut self, row: usize, col: usize) -> &mut Self::Item; @@ -70,7 +67,6 @@ pub trait RandomAccessMut: UnsafeRandomAccessMut { fn get1d_mut(&mut self, elem: usize) -> &mut Self::Item; } - /// Check that a given pair of `row` and `col` is not out of bounds for given dimension `dim`. #[inline] fn assert_dimension(row: IndexType, col: IndexType, dim: (IndexType, IndexType)) { @@ -101,26 +97,26 @@ fn assert_dimension1d(elem: IndexType, nelems: IndexType) { ); } -impl + Layout> RandomAccess for Mat { - fn get(&self, row: IndexType, col: IndexType) -> Self::Item { +impl + Layout> RandomAccess for Mat { + fn get(&self, row: IndexType, col: IndexType) -> Self::Item { assert_dimension(row, col, self.layout().dim()); - unsafe {self.get_unchecked(row, col) } + unsafe { self.get_unchecked(row, col) } } fn get1d(&self, elem: IndexType) -> Self::Item { assert_dimension1d(elem, self.layout().number_of_elements()); - unsafe {self.get1d_unchecked(elem)} + unsafe { self.get1d_unchecked(elem) } } } -impl + Layout> RandomAccessMut for Mat { - fn get_mut(&mut self, row: IndexType, col: IndexType) -> &mut Self::Item { +impl + Layout> RandomAccessMut for Mat { + fn get_mut(&mut self, row: IndexType, col: IndexType) -> &mut Self::Item { assert_dimension(row, col, self.layout().dim()); - unsafe {self.get_unchecked_mut(row, col) } + unsafe { self.get_unchecked_mut(row, col) } } fn get1d_mut(&mut self, elem: IndexType) -> &mut Self::Item { assert_dimension1d(elem, self.layout().number_of_elements()); - unsafe {self.get1d_unchecked_mut(elem)} + unsafe { self.get1d_unchecked_mut(elem) } } } diff --git a/src/traits/size.rs b/src/traits/size.rs index 3107875..dcc17c6 100644 --- a/src/traits/size.rs +++ b/src/traits/size.rs @@ -8,14 +8,13 @@ //! be types that implement the [SizeIdentifier] trait. A [SizeIdentifier] has an associated //! [usize] constant [SizeIdentifier::N] that gives compile time information on the actual //! size. The following data types are implemented that have size types -//! +//! //! - [Fixed1]. This type specifies a row/column of fixed dimension 1. //! - [Fixed2]. This type specifies a row/column of fixed dimension 2. //! - [Fixed3]. This type specifies a row/column of fixed dimension 3. //! - [Dynamic]. This type specifies a row/column dimension defined at runtime. //! The corresponding constant [SizeIdentifier::N] is set to 0. -//! - +//! /// Fixed Dimension 1. pub struct Fixed1; @@ -39,14 +38,13 @@ pub trait SizeIdentifier { impl SizeIdentifier for Fixed1 { const N: usize = 1; - } impl SizeIdentifier for Fixed2 { const N: usize = 2; } impl SizeIdentifier for Fixed3 { - const N: usize= 3; + const N: usize = 3; } impl SizeIdentifier for Dynamic { const N: usize = 0; @@ -59,4 +57,3 @@ pub trait SizeType { type R: SizeIdentifier; type C: SizeIdentifier; } - diff --git a/src/types.rs b/src/types.rs index 0ea5cf1..64257df 100644 --- a/src/types.rs +++ b/src/types.rs @@ -13,5 +13,3 @@ pub use cauchy::c64; /// The index type used throughout this crate. By default /// it is set to `usize`. pub type IndexType = usize; - - From fef3dd551b347df04e3fb443ae43dc6b1b23e5ab Mon Sep 17 00:00:00 2001 From: Srinath Kailasa Date: Mon, 26 Sep 2022 13:26:15 +0100 Subject: [PATCH 2/6] Add linting to gh workflow --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b853900..bdc9635 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,6 +40,9 @@ jobs: cargo test $CARGO_OPTIONS -- -Z unstable-options --format json | cargo2junit > results.xml; zip -0 ccov.zip `find . \( -name "$PROJECT_NAME_UNDERSCORE*.gc*" \) -print`; grcov ccov.zip -s . -t lcov --llvm --ignore-not-existing --ignore "/*" --ignore "tests/*" -o lcov.info; + + - name: Run linter + run: cargo fmt --all -- --check - name: Upload test results uses: EnricoMi/publish-unit-test-result-action@v1 with: From 2388d4170c4819026bc6592b1ecf111ed15319db Mon Sep 17 00:00:00 2001 From: Srinath Kailasa Date: Mon, 26 Sep 2022 13:32:20 +0100 Subject: [PATCH 3/6] Add rustfmt to build --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bdc9635..7633460 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,9 +18,9 @@ jobs: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: - profile: minimal toolchain: nightly override: true + components: rustfmt - name: Cache dependencies uses: actions/cache@v2 env: From c562edda15a588e5d44c528e9bc2a2a12fc1d6ab Mon Sep 17 00:00:00 2001 From: Srinath Kailasa Date: Mon, 26 Sep 2022 14:01:03 +0100 Subject: [PATCH 4/6] Some niceties --- src/data_container.rs | 10 ++++++++-- src/matrix/matrix_slices.rs | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/data_container.rs b/src/data_container.rs index 33673c0..a92a98a 100644 --- a/src/data_container.rs +++ b/src/data_container.rs @@ -36,7 +36,7 @@ pub trait DataContainer { ); unsafe { - std::slice::from_raw_parts(self.get_pointer().offset(first as isize), last - first) + std::slice::from_raw_parts(self.get_pointer().add(first), last - first) } } @@ -76,7 +76,7 @@ pub trait DataContainerMut: DataContainer { unsafe { std::slice::from_raw_parts_mut( - self.get_pointer_mut().offset(first as isize), + self.get_pointer_mut().add(first), last - first, ) } @@ -121,6 +121,12 @@ impl ArrayContainer { } } +impl Default for ArrayContainer { + fn default() -> Self { + Self::new() + } +} + impl<'a, Item: Scalar> SliceContainer<'a, Item> { /// New slice container from a reference. pub fn new(slice: &'a [Item]) -> SliceContainer { diff --git a/src/matrix/matrix_slices.rs b/src/matrix/matrix_slices.rs index 69271a6..5bf8b5a 100644 --- a/src/matrix/matrix_slices.rs +++ b/src/matrix/matrix_slices.rs @@ -31,7 +31,7 @@ macro_rules! block_matrix { let start_index = self.layout().convert_2d_raw(top_left.0, top_left.1); unsafe { SliceMatrix::<'a, Item, $StrideLayout, $RS, $CS>::from_pointer( - self.get_pointer().offset(start_index as isize), + self.get_pointer().add(start_index), dim, self.layout().stride(), ) @@ -61,7 +61,7 @@ macro_rules! block_matrix { unsafe { SliceMatrixMut::<'a, Item, $StrideLayout, $RS, $CS>::from_pointer( - self.get_pointer_mut().offset(start_index as isize), + self.get_pointer_mut().add(start_index), dim, self.layout().stride(), ) From 54c26098359941160947f4cd0599d3d662b011e6 Mon Sep 17 00:00:00 2001 From: Srinath Kailasa Date: Mon, 26 Sep 2022 14:01:17 +0100 Subject: [PATCH 5/6] Lint --- src/data_container.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/data_container.rs b/src/data_container.rs index a92a98a..c8aa50a 100644 --- a/src/data_container.rs +++ b/src/data_container.rs @@ -35,9 +35,7 @@ pub trait DataContainer { self.number_of_elements() ); - unsafe { - std::slice::from_raw_parts(self.get_pointer().add(first), last - first) - } + unsafe { std::slice::from_raw_parts(self.get_pointer().add(first), last - first) } } /// Return the number of elements in the container. @@ -74,12 +72,7 @@ pub trait DataContainerMut: DataContainer { self.number_of_elements() ); - unsafe { - std::slice::from_raw_parts_mut( - self.get_pointer_mut().add(first), - last - first, - ) - } + unsafe { std::slice::from_raw_parts_mut(self.get_pointer_mut().add(first), last - first) } } } From 7a6e216e1b1c40022eddfad18bc8789d0bc20709 Mon Sep 17 00:00:00 2001 From: Srinath Kailasa Date: Mon, 26 Sep 2022 14:11:34 +0100 Subject: [PATCH 6/6] Resolve conflicts --- src/data_container.rs | 4 ++-- src/traits/matrix.rs | 12 ------------ src/traits/random_access.rs | 12 ------------ 3 files changed, 2 insertions(+), 26 deletions(-) diff --git a/src/data_container.rs b/src/data_container.rs index 55adbef..f798a13 100644 --- a/src/data_container.rs +++ b/src/data_container.rs @@ -114,13 +114,13 @@ impl ArrayContainer { } } -impl Default for ArrayContainer { +impl Default for ArrayContainer { fn default() -> Self { Self::new() } } -impl<'a, Item: Scalar> SliceContainer<'a, Item> { +impl<'a, Item: HScalar> SliceContainer<'a, Item> { /// New slice container from a reference. pub fn new(slice: &'a [Item]) -> SliceContainer { SliceContainer:: { data: slice } diff --git a/src/traits/matrix.rs b/src/traits/matrix.rs index dfdef77..f496742 100644 --- a/src/traits/matrix.rs +++ b/src/traits/matrix.rs @@ -11,29 +11,17 @@ //! [MatrixTraitMut] additionally depends on the trait [RandomAccessMut] to provide //! mutable access to matrix elements. use crate::traits::{Layout, LayoutType, RandomAccess, RandomAccessMut, SizeIdentifier, SizeType}; -<<<<<<< HEAD -use crate::types::Scalar; - -/// Combined trait for basic matrix properties. See [crate::traits::matrix] -/// for details. -pub trait MatrixTrait: -======= use crate::types::HScalar; /// Combined trait for basic matrix properties. See [crate::traits::matrix] /// for details. pub trait MatrixTrait: ->>>>>>> main RandomAccess + Layout + SizeType { } /// Combined trait for mutable matrices. See [crate::traits::matrix] for details. -<<<<<<< HEAD -pub trait MatrixTraitMut: -======= pub trait MatrixTraitMut: ->>>>>>> main RandomAccessMut + MatrixTrait { } diff --git a/src/traits/random_access.rs b/src/traits/random_access.rs index c6da91a..a77a611 100644 --- a/src/traits/random_access.rs +++ b/src/traits/random_access.rs @@ -21,11 +21,7 @@ //! the memory layout defined in that trait. use crate::traits::{Layout, LayoutType}; -<<<<<<< HEAD -use crate::types::{IndexType, Scalar}; -======= use crate::types::{HScalar, IndexType}; ->>>>>>> main /// This trait provides unsafe access to the underlying data. See /// [Random Access](crate::traits::random_access) for a description. @@ -101,11 +97,7 @@ fn assert_dimension1d(elem: IndexType, nelems: IndexType) { ); } -<<<<<<< HEAD -impl + Layout> RandomAccess for Mat { -======= impl + Layout> RandomAccess for Mat { ->>>>>>> main fn get(&self, row: IndexType, col: IndexType) -> Self::Item { assert_dimension(row, col, self.layout().dim()); unsafe { self.get_unchecked(row, col) } @@ -117,11 +109,7 @@ impl + Layout> RandomAccess } } -<<<<<<< HEAD -impl + Layout> RandomAccessMut for Mat { -======= impl + Layout> RandomAccessMut for Mat { ->>>>>>> main fn get_mut(&mut self, row: IndexType, col: IndexType) -> &mut Self::Item { assert_dimension(row, col, self.layout().dim()); unsafe { self.get_unchecked_mut(row, col) }