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
/* This file is part of bacon.
 * Copyright (c) Wyatt Campbell.
 *
 * See repository LICENSE for information.
 */
use nalgebra::{Const, Dim, DimName, Dyn, Matrix, Owned, Vector};
use thiserror::Error;

#[cfg(test)]
#[macro_use]
extern crate float_cmp;

/// A possibly dynamically sized vector
pub type BVector<T, D> = Vector<T, D, Owned<T, D, Const<1>>>;
/// A statically sized vector
pub type BSVector<T, const D: usize> = Vector<T, Const<D>, Owned<T, Const<D>, Const<1>>>;
/// A possibly dynamically sized matrix
pub type BMatrix<T, R, C> = Matrix<T, R, C, Owned<T, R, C>>;
/// A statically sized vector
pub type BSMatrix<T, const R: usize, const C: usize> =
    Matrix<T, Const<R>, Const<C>, Owned<T, Const<R>, Const<C>>>;

pub mod prelude {
    pub use crate::{BSVector, BVector};
    pub use nalgebra;
    pub use nalgebra::{Const, DimName, Dyn, U1};
    pub use num_complex;
    pub use num_traits;
}

pub mod constants;
pub mod ivp;
pub mod roots;
#[macro_use]
pub mod polynomial;
pub mod differentiate;
pub mod integrate;
pub mod interp;
pub mod optimize;
pub mod special;

#[cfg(test)]
mod tests;

#[derive(Debug, Error)]
pub enum DimensionError {
    #[error("attempted to build a dynamic solver with static dimension")]
    DynamicOnStatic,
    #[error("attempted to build a static solver with dynamic dimension")]
    StaticOnDynamic,
}

pub trait Dimension: Dim {
    fn dim() -> Result<Self, DimensionError>;
    fn dim_dyn(size: usize) -> Result<Self, DimensionError>;
}

impl<const C: usize> Dimension for Const<C> {
    fn dim() -> Result<Self, DimensionError> {
        Ok(Self::name())
    }

    fn dim_dyn(_size: usize) -> Result<Self, DimensionError> {
        Err(DimensionError::DynamicOnStatic)
    }
}

impl Dimension for Dyn {
    fn dim() -> Result<Self, DimensionError> {
        Err(DimensionError::StaticOnDynamic)
    }

    fn dim_dyn(size: usize) -> Result<Self, DimensionError> {
        Ok(Self::from_usize(size))
    }
}