Skip to main content

complex_bessel/
types.rs

1//! Core types for Bessel function computation.
2
3#[cfg(not(feature = "std"))]
4use alloc::vec::Vec;
5use core::fmt;
6
7use num_complex::Complex;
8
9use crate::machine::BesselFloat;
10
11/// Result of a Bessel function sequence computation.
12pub struct BesselResult<T: BesselFloat> {
13    /// Computed function values for orders ν, ν+1, ..., ν+n-1.
14    pub values: Vec<Complex<T>>,
15    /// Number of leading components set to zero due to underflow.
16    pub underflow_count: usize,
17}
18
19/// Scaling option for Bessel function computation.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Scaling {
22    /// No scaling applied.
23    Unscaled,
24    /// Exponential scaling to prevent overflow/underflow.
25    Exponential,
26}
27
28/// Kind of Hankel function.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum HankelKind {
31    /// H^(1), Hankel function of the first kind.
32    First,
33    /// H^(2), Hankel function of the second kind.
34    Second,
35}
36
37/// Selects Airy function value or its derivative.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum AiryDerivative {
40    /// Ai(z) or Bi(z).
41    Value,
42    /// Ai'(z) or Bi'(z).
43    Derivative,
44}
45
46/// Error type for Bessel function computation.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum BesselError {
49    /// Invalid input (e.g., z=0 for Hankel, negative order).
50    InvalidInput,
51    /// Overflow: |z| or ν too large, or |z| too small.
52    Overflow,
53    /// Argument reduction caused loss of more than half of significant digits.
54    PrecisionLoss,
55    /// Argument reduction caused loss of all significant digits.
56    TotalPrecisionLoss,
57    /// Algorithm did not meet termination criteria.
58    ConvergenceFailure,
59}
60
61impl fmt::Display for BesselError {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            BesselError::InvalidInput => {
65                write!(f, "invalid input: check z and nu constraints")
66            }
67            BesselError::Overflow => {
68                write!(f, "overflow: result magnitude exceeds representable range")
69            }
70            BesselError::PrecisionLoss => {
71                write!(
72                    f,
73                    "precision loss: more than half of significant digits lost"
74                )
75            }
76            BesselError::TotalPrecisionLoss => {
77                write!(f, "total precision loss: no significant digits remain")
78            }
79            BesselError::ConvergenceFailure => {
80                write!(
81                    f,
82                    "convergence failure: algorithm did not meet termination criteria"
83                )
84            }
85        }
86    }
87}
88
89#[cfg(feature = "std")]
90impl std::error::Error for BesselError {}