From b8102bbea628dbe88290c12e8fa13a16286f5599 Mon Sep 17 00:00:00 2001 From: Timo Betcke Date: Sun, 12 Jan 2025 09:44:33 +0000 Subject: [PATCH] Small improvements. --- src/operator/abstract_operator.rs | 10 ---------- src/operator/operations/conjugate_gradients.rs | 3 +++ src/operator/space/linear_space.rs | 7 ++++++- tests/operator.rs | 17 ++++++++++------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/operator/abstract_operator.rs b/src/operator/abstract_operator.rs index 5d63e21..7621b00 100644 --- a/src/operator/abstract_operator.rs +++ b/src/operator/abstract_operator.rs @@ -19,16 +19,6 @@ pub trait OperatorBase: Debug { /// Get the range fn range(&self) -> Rc; - /// Get a zero in the domain space. - fn domain_zero(&self) -> ::E { - ::zero(self.domain()) - } - - /// Get a zero in the range space. - fn range_zero(&self) -> ::E { - ::zero(self.range()) - } - /// Convert to RLST reference fn r(&self) -> RlstOperatorReference<'_, Self> where diff --git a/src/operator/operations/conjugate_gradients.rs b/src/operator/operations/conjugate_gradients.rs index af0482f..8444670 100644 --- a/src/operator/operations/conjugate_gradients.rs +++ b/src/operator/operations/conjugate_gradients.rs @@ -82,6 +82,9 @@ impl<'a, Space: InnerProductSpace, Op: AsApply> 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 = ::norm(&self.operator.range(), self.rhs); let mut res_inner = ::inner(&self.operator.range(), &res, &res); let mut res_norm = res_inner.abs().sqrt(); diff --git a/src/operator/space/linear_space.rs b/src/operator/space/linear_space.rs index c87b6fb..7e54957 100644 --- a/src/operator/space/linear_space.rs +++ b/src/operator/space/linear_space.rs @@ -16,10 +16,15 @@ pub trait LinearSpace { /// Type associated with elements of the space. type E: Element; - /// Create a new vector from the space. + /// Create a new zero element from the space. fn zero(space: Rc) -> Self::E; } /// Element type pub type ElementType = ::E; /// Field type pub type FieldType = ::F; + +/// Create a new zero element from a given space. +pub fn zero_element(space: Rc) -> ElementType { + Space::zero(space) +} diff --git a/tests/operator.rs b/tests/operator.rs index 20c2ad6..cf82acf 100644 --- a/tests/operator.rs +++ b/tests/operator.rs @@ -2,7 +2,10 @@ 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() { @@ -10,8 +13,8 @@ fn test_dense_matrix_operator() { 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); @@ -21,9 +24,9 @@ fn test_dense_matrix_operator() { #[test] pub fn test_gram_schmidt() { let space = ArrayVectorSpace::::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); @@ -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))