1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Fundamental traits for ODE

use std::ops::*;
use num_complex::Complex;
use num_traits::Float;
use ndarray::*;

/// Equation of motion (EOM)
pub trait EOM<A, S, D>
    where S: Data<Elem = A>,
          D: Dimension
{
    /// calculate right hand side (rhs) of EOM from current state
    fn rhs(self, ArrayBase<S, D>) -> ArrayBase<S, D>;
}

/// Stiff equation with diagonalized linear part
pub trait Diag<A, D>
    where D: Dimension
{
    /// Linear part of EOM (assume to be diagonalized)
    fn diagonal(&self) -> RcArray<A, D>;
}

/// Time-evolution operator
pub trait TimeEvolution<A, S, D>
    where S: Data<Elem = A>,
          D: Dimension
{
    /// calculate next step
    fn iterate(self, ArrayBase<S, D>) -> ArrayBase<S, D>;
}

pub trait TimeStep {
    fn get_dt(&self) -> f64;
    fn set_dt(&mut self, dt: f64);
}

/// utility trait for easy implementation
pub trait OdeScalar<R: Ring>: LinalgScalar + RMod<R> {}
impl<A, R: Ring> OdeScalar<R> for A where A: LinalgScalar + RMod<R> {}

/// Ring (math)
pub trait Ring
    : Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Sized {
}
impl<A> Ring for A where A: Add<Output = A> + Sub<Output = A> + Mul<Output = A> + Sized {}

/// R-module
pub trait RMod<R: Ring>: Mul<R, Output = Self> + Sized {}
impl<A, R: Ring> RMod<R> for A where A: Mul<R, Output = A> + Sized {}

/// exponential function
pub trait Exponential: Clone + Copy + Sized {
    fn exp(self) -> Self;
}

impl Exponential for f32 {
    fn exp(self) -> Self {
        <Self>::exp(self)
    }
}

impl Exponential for f64 {
    fn exp(self) -> Self {
        <Self>::exp(self)
    }
}

impl<T> Exponential for Complex<T>
    where T: Clone + Float
{
    fn exp(self) -> Self {
        <Complex<T>>::exp(&self)
    }
}