From ca621ebea5dcf4bba4d7d63dc036d84f972b461b Mon Sep 17 00:00:00 2001 From: axect Date: Thu, 11 Apr 2024 23:14:34 +0900 Subject: [PATCH 1/2] DEP: Add anyhow --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 567d134d..7f301446 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ lapack = { version = "0.19", optional = true } serde = { version = "1.0", features = ["derive"], optional = true } json = { version = "0.12", optional = true } arrow2 = { version = "0.18", features = ["io_parquet", "io_parquet_compression"], optional = true } +anyhow = "1.0.82" [package.metadata.docs.rs] rustdoc-args = [ "--html-in-header", "katex-header.html", "--cfg", "docsrs"] From 5c0657b7737684ac325d975846a766821b5d76e9 Mon Sep 17 00:00:00 2001 From: axect Date: Thu, 11 Apr 2024 23:17:46 +0900 Subject: [PATCH 2/2] CHGE: Change all RootFindingError to anyhow --- src/numerical/root.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/numerical/root.rs b/src/numerical/root.rs index 1462c81a..22db61c3 100644 --- a/src/numerical/root.rs +++ b/src/numerical/root.rs @@ -1,17 +1,7 @@ -use thiserror::Error; - -#[derive(Error, Debug, Clone)] -pub enum RootFindingError { - #[error("No available root")] - NoAvailableRoot, - #[error("Zero derivative at {0}")] - ZeroDerivative(f64), - #[error("Constrain violated at {0}")] - ConstraintViolation(f64), -} +use anyhow::{Result, bail}; pub trait RootFindingProblem { - fn function(&self, x: f64) -> Result; + fn function(&self, x: f64) -> Result; fn initial_guess(&self) -> T; fn derivative(&self, x: f64) -> f64 { unimplemented!() @@ -22,11 +12,11 @@ pub trait RootFindingProblem { } pub trait RootFindingMethod { - fn step>(&self, problem: &P, state: T) -> Result; + fn step>(&self, problem: &P, state: T) -> Result; } pub trait RootSolver { - fn solve(&self, problem: &P, finder: &F) -> Result + fn solve(&self, problem: &P, finder: &F) -> Result where P: RootFindingProblem, F: RootFindingMethod; @@ -45,7 +35,7 @@ impl RootFindingMethod<(f64, f64)> for BisectionMethod { &self, problem: &P, state: (f64, f64), - ) -> Result<(f64, f64), RootFindingError> { + ) -> Result<(f64, f64)> { let (a, b) = state; let c = (a + b) / 2.0; @@ -60,7 +50,7 @@ impl RootFindingMethod<(f64, f64)> for BisectionMethod { } else if fc == 0.0 { Ok((c, c)) } else { - Err(RootFindingError::NoAvailableRoot) + bail!("There is no root in the interval [{}, {}]", a, b); } } } @@ -78,12 +68,12 @@ impl RootFindingMethod for NewtonMethod { &self, problem: &P, state: f64, - ) -> Result { + ) -> Result { let f = problem.function(state)?; let df = problem.derivative(state); if df == 0.0 { - return Err(RootFindingError::ZeroDerivative(state)); + bail!("Zero derivative at x = {}", state); } Ok(state - f / df) @@ -103,15 +93,15 @@ impl RootFindingMethod<(f64, f64)> for SecantMethod { &self, problem: &P, state: (f64, f64), - ) -> Result<(f64, f64), RootFindingError> { + ) -> Result<(f64, f64)> { let (x0, x1) = state; let f0 = problem.function(x0)?; let f1 = problem.function(x1)?; if f0 == f1 { - return Err(RootFindingError::ZeroDerivative(x0)); + bail!("Zero secant at ({}, {})", x0, x1); } - unimplemented!() + Ok((x1, x1 - f1 * (x1 - x0) / (f1 - f0))) } }