Skip to content

Commit

Permalink
docs in operator
Browse files Browse the repository at this point in the history
  • Loading branch information
mscroggs committed Mar 21, 2024
1 parent cd1c03d commit c192989
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 24 deletions.
1 change: 1 addition & 0 deletions io/src/matrix_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum DataType {

/// Identifier trait to associate a matrix market identifier with a Rust type.
pub trait MmIdentifier {
/// Matrix market type
const MMTYPE: &'static str;
}

Expand Down
4 changes: 4 additions & 0 deletions operator/src/interface/array_vector_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ use rlst_dense::{
rlst_dynamic_array1,
};

/// Array vector space
pub struct ArrayVectorSpace<Item: RlstScalar> {
dimension: usize,
_marker: PhantomData<Item>,
}

/// Element of an array vector space
pub struct ArrayVectorSpaceElement<Item: RlstScalar> {
elem: DynamicArray<Item, 1>,
}

impl<Item: RlstScalar> ArrayVectorSpaceElement<Item> {
/// Create a new element
pub fn new(space: &ArrayVectorSpace<Item>) -> Self {
Self {
elem: rlst_dynamic_array1!(Item, [space.dimension()]),
Expand All @@ -32,6 +35,7 @@ impl<Item: RlstScalar> ArrayVectorSpaceElement<Item> {
}

impl<Item: RlstScalar> ArrayVectorSpace<Item> {
/// Create a new vector space
pub fn new(dimension: usize) -> Self {
Self {
dimension,
Expand Down
3 changes: 3 additions & 0 deletions operator/src/interface/dense_matrix_operator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Dense matrix operator
use crate::{space::*, AsApply, OperatorBase};
use rlst_dense::types::RlstScalar;
use rlst_dense::{
Expand All @@ -7,6 +8,7 @@ use rlst_dense::{

use super::array_vector_space::ArrayVectorSpace;

/// Dense matrix operator
pub struct DenseMatrixOperator<
'a,
Item: RlstScalar,
Expand Down Expand Up @@ -40,6 +42,7 @@ impl<
+ 'a,
> DenseMatrixOperator<'a, Item, ArrayImpl>
{
/// Create a new dense matrix operator
pub fn new(
arr: Array<Item, ArrayImpl, 2>,
domain: &'a ArrayVectorSpace<Item>,
Expand Down
5 changes: 5 additions & 0 deletions operator/src/interface/sparse_operator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Sparse operator
use crate::{space::*, AsApply, OperatorBase};
use rlst_dense::traits::{RawAccess, RawAccessMut, Shape};
use rlst_dense::types::RlstScalar;
Expand All @@ -6,6 +7,7 @@ use rlst_sparse::sparse::csr_mat::CsrMatrix;

use super::array_vector_space::ArrayVectorSpace;

/// CSR matrix operator
pub struct CsrMatrixOperator<'a, Item: RlstScalar> {
csr_mat: &'a CsrMatrix<Item>,
domain: &'a ArrayVectorSpace<Item>,
Expand All @@ -22,6 +24,7 @@ impl<Item: RlstScalar> std::fmt::Debug for CsrMatrixOperator<'_, Item> {
}

impl<'a, Item: RlstScalar> CsrMatrixOperator<'a, Item> {
/// Create a new CSR matrix operator
pub fn new(
csr_mat: &'a CsrMatrix<Item>,
domain: &'a ArrayVectorSpace<Item>,
Expand Down Expand Up @@ -66,6 +69,7 @@ impl<Item: RlstScalar> AsApply for CsrMatrixOperator<'_, Item> {
}
}

/// CSC matrix operator
pub struct CscMatrixOperator<'a, Item: RlstScalar> {
csc_mat: &'a CscMatrix<Item>,
domain: &'a ArrayVectorSpace<Item>,
Expand All @@ -82,6 +86,7 @@ impl<Item: RlstScalar> std::fmt::Debug for CscMatrixOperator<'_, Item> {
}

impl<'a, Item: RlstScalar> CscMatrixOperator<'a, Item> {
/// Create a new CSC matrix operator
pub fn new(
csc_mat: &'a CscMatrix<Item>,
domain: &'a ArrayVectorSpace<Item>,
Expand Down
2 changes: 1 addition & 1 deletion operator/src/linalg.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// Linear algebra operations on function spaces
//! Linear algebra operations on function spaces
1 change: 1 addition & 0 deletions operator/src/operations.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//! Operations
pub mod conjugate_gradients;
pub mod modified_gram_schmidt;
8 changes: 8 additions & 0 deletions operator/src/operations/conjugate_gradients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{AsApply, Element, InnerProductSpace, LinearSpace, NormedSpace};
use num::One;
use rlst_dense::types::RlstScalar;

/// Iteration for CG
pub struct CgIteration<'a, Space: InnerProductSpace, Op: AsApply<Domain = Space, Range = Space>> {
operator: &'a Op,
space: &'a Space,
Expand All @@ -18,6 +19,7 @@ pub struct CgIteration<'a, Space: InnerProductSpace, Op: AsApply<Domain = Space,
impl<'a, Space: InnerProductSpace, Op: AsApply<Domain = Space, Range = Space>>
CgIteration<'a, Space, Op>
{
/// Create a new CG iteration
pub fn new(op: &'a Op, rhs: &'a Space::E) -> Self {
Self {
operator: op,
Expand All @@ -31,31 +33,37 @@ impl<'a, Space: InnerProductSpace, Op: AsApply<Domain = Space, Range = Space>>
}
}

/// Set x
pub fn set_x(mut self, x: &Space::E) -> Self {
self.x.fill_inplace(x);
self
}

/// Set the tolerance
pub fn set_tol(mut self, tol: <Space::F as RlstScalar>::Real) -> Self {
self.tol = tol;
self
}

/// Set maximum number of iterations
pub fn set_max_iter(mut self, max_iter: usize) -> Self {
self.max_iter = max_iter;
self
}

/// Set the cammable
pub fn set_callable(mut self, callable: impl FnMut(&Space::E, &Space::E) + 'a) -> Self {
self.callable = Some(Box::new(callable));
self
}

/// Enable debug printing
pub fn print_debug(mut self) -> Self {
self.print_debug = true;
self
}

/// Run CG
pub fn run(mut self) -> (Space::E, <Space::F as RlstScalar>::Real) {
fn print_success<T: RlstScalar>(it_count: usize, rel_res: T) {
println!(
Expand Down
2 changes: 2 additions & 0 deletions operator/src/operations/modified_gram_schmidt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use rlst_dense::{
array::DynamicArray,
traits::{RandomAccessMut, Shape},
};
/// Gram Schmidt orthogonalization
pub struct ModifiedGramSchmidt;

impl ModifiedGramSchmidt {
/// Orthogonalize a matrix
pub fn orthogonalize<
Item: RlstScalar,
Elem: Element<F = Item>,
Expand Down
18 changes: 13 additions & 5 deletions operator/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@ use num::{One, Zero};
use rlst_dense::types::*;
use std::fmt::Debug;

// A base operator trait.
/// A base operator trait.
pub trait OperatorBase: Debug {
/// Domain space type
type Domain: LinearSpace;
/// Range space type
type Range: LinearSpace;

/// Get the domain
fn domain(&self) -> &Self::Domain;

/// Get the range
fn range(&self) -> &Self::Range;

/// Convert to RLST reference
fn as_ref_obj(&self) -> RlstOperatorReference<'_, Self>
where
Self: Sized,
{
RlstOperatorReference(self)
}

/// Form a new operator alpha * self.
fn scale(self, alpha: <Self::Range as LinearSpace>::F) -> ScalarTimesOperator<Self>
where
Self: Sized,
{
ScalarTimesOperator(self, alpha)
}

/// Form a new operator self + other.
fn sum<Op: OperatorBase<Domain = Self::Domain, Range = Self::Range> + Sized>(
self,
other: Op,
Expand Down Expand Up @@ -55,6 +58,7 @@ pub trait OperatorBase: Debug {

/// Apply an operator as y -> alpha * Ax + beta y
pub trait AsApply: OperatorBase {
/// Apply an operator as y -> alpha * Ax + beta y
fn apply_extended(
&self,
alpha: <Self::Range as LinearSpace>::F,
Expand All @@ -63,6 +67,7 @@ pub trait AsApply: OperatorBase {
y: &mut <Self::Range as LinearSpace>::E,
) -> RlstResult<()>;

/// Apply
fn apply(&self, x: &<Self::Domain as LinearSpace>::E) -> <Self::Range as LinearSpace>::E {
let mut out = self.range().zero();

Expand All @@ -77,6 +82,7 @@ pub trait AsApply: OperatorBase {
}
}

/// Operator reference
pub struct RlstOperatorReference<'a, Op: OperatorBase>(&'a Op);

impl<Op: OperatorBase> std::fmt::Debug for RlstOperatorReference<'_, Op> {
Expand Down Expand Up @@ -111,6 +117,7 @@ impl<Op: AsApply> AsApply for RlstOperatorReference<'_, Op> {
}
}

/// Operator sum
pub struct OperatorSum<
Domain: LinearSpace,
Range: LinearSpace,
Expand Down Expand Up @@ -174,6 +181,7 @@ impl<
}
}

/// Operator muiltiplied by a scalar
pub struct ScalarTimesOperator<Op: OperatorBase>(Op, <Op::Range as LinearSpace>::F);

impl<Op: OperatorBase> std::fmt::Debug for ScalarTimesOperator<Op> {
Expand Down
4 changes: 4 additions & 0 deletions operator/src/space/dual_space.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Dual space
use super::{ElementView, LinearSpace};
use rlst_dense::types::RlstResult;

/// A dual space
pub trait DualSpace: LinearSpace {
/// Space type
type Space: LinearSpace<F = Self::F>;

/// Dual pairing
fn dual_pairing(
&self,
x: ElementView<Self>,
Expand Down
16 changes: 9 additions & 7 deletions operator/src/space/element.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//! Elements of linear spaces
use num::One;
use rlst_dense::types::RlstScalar;

use super::LinearSpace;

/// Elements of linear spaces.
/// An Element of a linear spaces.
pub trait Element {
/// Item type of the vector.
/// Space type
type Space: LinearSpace<F = Self::F, E = Self>;

/// Scalar Type
type F: RlstScalar;

/// View
type View<'b>
where
Self: 'b;
/// Mutable view
type ViewMut<'b>
where
Self: 'b;
Expand Down Expand Up @@ -52,6 +53,7 @@ pub trait Element {
self
}

/// self += other
fn sum(mut self, other: &Self) -> Self
where
Self: Sized,
Expand Down Expand Up @@ -88,8 +90,8 @@ pub trait Element {
}
}

// The view type associated with elements of linear spaces.
/// The view type associated with elements of linear spaces.
pub type ElementView<'view, Space> = <<Space as LinearSpace>::E as Element>::View<'view>;

// The mutable view type associated with elements of linear spaces.
/// The mutable view type associated with elements of linear spaces.
pub type ElementViewMut<'view, Space> = <<Space as LinearSpace>::E as Element>::ViewMut<'view>;
26 changes: 16 additions & 10 deletions operator/src/space/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@
use crate::Element;

/// A frame
///
/// A frame is a collection of elements of a space.
pub trait Frame {
/// Element type
type E: Element;

/// Iterator
type Iter<'iter>: std::iter::Iterator<Item = &'iter Self::E>
where
Self: 'iter;

/// Mutable iterator
type IterMut<'iter>: std::iter::Iterator<Item = &'iter mut Self::E>
where
Self: 'iter;

/// Get an element
fn get(&self, index: usize) -> Option<&Self::E>;

/// Get a mutable element
fn get_mut(&mut self, index: usize) -> Option<&mut Self::E>;

/// Number of elements
fn len(&self) -> usize;

/// Is empty
fn is_empty(&self) -> bool {
self.len() == 0
}

/// Get iterator
fn iter(&self) -> Self::Iter<'_>;

/// Get mutable iterator
fn iter_mut(&mut self) -> Self::IterMut<'_>;

/// Add an element
fn push(&mut self, elem: Self::E);

/// Evaluate
fn evaluate(&self, coeffs: &[<Self::E as Element>::F], result: &mut Self::E) {
assert_eq!(coeffs.len(), self.len());
for (elem, coeff) in self.iter().zip(coeffs.iter().copied()) {
Expand All @@ -37,11 +41,13 @@ pub trait Frame {
}
}

/// A vector frame
pub struct VectorFrame<Elem: Element> {
data: Vec<Elem>,
}

impl<Elem: Element> VectorFrame<Elem> {
/// Create a new vector frame
pub fn new() -> Self {
Self { data: Vec::new() }
}
Expand Down
3 changes: 3 additions & 0 deletions operator/src/space/indexable_space.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Indexable space
use super::LinearSpace;

/// Indexable space
pub trait IndexableSpace: LinearSpace {
/// Dimension
fn dimension(&self) -> usize;
}
4 changes: 4 additions & 0 deletions operator/src/space/inner_product_space.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Inner product space
use super::LinearSpace;

/// Inner product space
pub trait InnerProductSpace: LinearSpace {
/// Inner product
fn inner(&self, x: &Self::E, other: &Self::E) -> Self::F;
}
3 changes: 2 additions & 1 deletion operator/src/space/linear_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub trait LinearSpace {
cloned
}
}

/// Element type
pub type ElementType<Space> = <Space as LinearSpace>::E;
/// Field type
pub type FieldType<Space> = <Space as LinearSpace>::F;
Loading

0 comments on commit c192989

Please sign in to comment.