Skip to content

Commit

Permalink
Small improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
tbetcke committed Jan 12, 2025
1 parent 45784e1 commit b8102bb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 0 additions & 10 deletions src/operator/abstract_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ pub trait OperatorBase: Debug {
/// Get the range
fn range(&self) -> Rc<Self::Range>;

/// Get a zero in the domain space.
fn domain_zero(&self) -> <Self::Domain as LinearSpace>::E {
<Self::Domain as LinearSpace>::zero(self.domain())
}

/// Get a zero in the range space.
fn range_zero(&self) -> <Self::Range as LinearSpace>::E {
<Self::Range as LinearSpace>::zero(self.range())
}

/// Convert to RLST reference
fn r(&self) -> RlstOperatorReference<'_, Self>
where
Expand Down
3 changes: 3 additions & 0 deletions src/operator/operations/conjugate_gradients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ impl<'a, Space: InnerProductSpace, Op: AsApply<Domain = Space, Range = Space>>

let mut p = res.clone();

// This syntax is only necessary because the type inference becomes confused for some reason.
// If I write `let rhs_norm = self.rhs.norm()` the compiler thinks that `self.rhs` is a space and
// not an element.
let rhs_norm = <Space as NormedSpace>::norm(&self.operator.range(), self.rhs);
let mut res_inner = <Space as InnerProductSpace>::inner(&self.operator.range(), &res, &res);
let mut res_norm = res_inner.abs().sqrt();
Expand Down
7 changes: 6 additions & 1 deletion src/operator/space/linear_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ pub trait LinearSpace {
/// Type associated with elements of the space.
type E: Element<F = Self::F>;

/// Create a new vector from the space.
/// Create a new zero element from the space.
fn zero(space: Rc<Self>) -> Self::E;
}
/// Element type
pub type ElementType<Space> = <Space as LinearSpace>::E;
/// Field type
pub type FieldType<Space> = <Space as LinearSpace>::F;

/// Create a new zero element from a given space.
pub fn zero_element<Space: LinearSpace>(space: Rc<Space>) -> ElementType<Space> {
Space::zero(space)
}
17 changes: 10 additions & 7 deletions tests/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
use num::traits::{One, Zero};
use rand::Rng;
use rlst::{operator::Operator, prelude::*};
use rlst::{
operator::{zero_element, Operator},
prelude::*,
};

#[test]
fn test_dense_matrix_operator() {
let mut mat = rlst_dynamic_array2!(f64, [3, 4]);
mat.fill_from_seed_equally_distributed(0);

let op = Operator::from(mat);
let mut x = op.domain_zero();
let mut y = op.range_zero();
let mut x = zero_element(op.domain());
let mut y = zero_element(op.range());

x.view_mut().fill_from_seed_equally_distributed(0);

Expand All @@ -21,9 +24,9 @@ fn test_dense_matrix_operator() {
#[test]
pub fn test_gram_schmidt() {
let space = ArrayVectorSpace::<c64>::from_dimension(5);
let mut vec1 = ArrayVectorSpace::zero(space.clone());
let mut vec2 = ArrayVectorSpace::zero(space.clone());
let mut vec3 = ArrayVectorSpace::zero(space.clone());
let mut vec1 = zero_element(space.clone());
let mut vec2 = zero_element(space.clone());
let mut vec3 = zero_element(space.clone());

vec1.view_mut().fill_from_seed_equally_distributed(0);
vec2.view_mut().fill_from_seed_equally_distributed(1);
Expand Down Expand Up @@ -87,7 +90,7 @@ fn test_cg() {

let op = Operator::from(mat.r());

let mut rhs = op.range_zero();
let mut rhs = zero_element(op.range());
rhs.view_mut().fill_from_equally_distributed(&mut rng);

let cg = (CgIteration::new(&op, &rhs))
Expand Down

0 comments on commit b8102bb

Please sign in to comment.