differential_equations/traits.rs
1//! Defines Generics for the library. Includes generics for the floating point numbers.
2
3use nalgebra::RealField;
4
5/// Real Number Trait
6///
7/// This trait specifies the acceptable types for real numbers.
8/// Currently implemented for:
9/// * `f32` - 32-bit floating point
10/// * `f64` - 64-bit floating point
11///
12/// Provides additional functionality required for ODE solvers beyond
13/// what's provided by nalgebra's RealField trait.
14///
15pub trait Real: Copy + RealField {
16 fn infinity() -> Self;
17 fn to_f64(self) -> f64;
18}
19
20impl Real for f32 {
21 fn infinity() -> Self {
22 std::f32::INFINITY
23 }
24
25 fn to_f64(self) -> f64 {
26 f64::from(self)
27 }
28}
29
30impl Real for f64 {
31 fn infinity() -> Self {
32 std::f64::INFINITY
33 }
34
35 fn to_f64(self) -> f64 {
36 self
37 }
38}
39
40/// Callback data trait
41///
42/// This trait represents data that can be returned from functions
43/// that are used to control the solver's execution flow. The
44/// Clone and Debug traits are required for internal use but anything
45/// that implements this trait can be used as callback data.
46/// For example, this can be a string, a number, or any other type
47/// that implements the Clone and Debug traits.
48///
49pub trait CallBackData: Clone + std::fmt::Debug {}
50
51// Implement for any type that already satisfies the bounds
52impl<T: Clone + std::fmt::Debug> CallBackData for T {}