bacon_sci/
lib.rs

1/* This file is part of bacon.
2 * Copyright (c) Wyatt Campbell.
3 *
4 * See repository LICENSE for information.
5 */
6use nalgebra::{Const, Dim, DimName, Dyn, Matrix, Owned, Vector};
7use thiserror::Error;
8
9#[cfg(test)]
10#[macro_use]
11extern crate float_cmp;
12
13/// A possibly dynamically sized vector
14pub type BVector<T, D> = Vector<T, D, Owned<T, D, Const<1>>>;
15/// A statically sized vector
16pub type BSVector<T, const D: usize> = Vector<T, Const<D>, Owned<T, Const<D>, Const<1>>>;
17/// A possibly dynamically sized matrix
18pub type BMatrix<T, R, C> = Matrix<T, R, C, Owned<T, R, C>>;
19/// A statically sized vector
20pub type BSMatrix<T, const R: usize, const C: usize> =
21    Matrix<T, Const<R>, Const<C>, Owned<T, Const<R>, Const<C>>>;
22
23pub mod prelude {
24    pub use crate::{BSVector, BVector};
25    pub use nalgebra;
26    pub use nalgebra::{Const, DimName, Dyn, U1};
27    pub use num_complex;
28    pub use num_traits;
29}
30
31pub mod constants;
32pub mod ivp;
33pub mod roots;
34#[macro_use]
35pub mod polynomial;
36pub mod differentiate;
37pub mod integrate;
38pub mod interp;
39pub mod optimize;
40pub mod special;
41
42#[cfg(test)]
43mod tests;
44
45#[derive(Debug, Error)]
46pub enum DimensionError {
47    #[error("attempted to build a dynamic solver with static dimension")]
48    DynamicOnStatic,
49    #[error("attempted to build a static solver with dynamic dimension")]
50    StaticOnDynamic,
51}
52
53pub trait Dimension: Dim {
54    fn dim() -> Result<Self, DimensionError>;
55    fn dim_dyn(size: usize) -> Result<Self, DimensionError>;
56}
57
58impl<const C: usize> Dimension for Const<C> {
59    fn dim() -> Result<Self, DimensionError> {
60        Ok(Self::name())
61    }
62
63    fn dim_dyn(_size: usize) -> Result<Self, DimensionError> {
64        Err(DimensionError::DynamicOnStatic)
65    }
66}
67
68impl Dimension for Dyn {
69    fn dim() -> Result<Self, DimensionError> {
70        Err(DimensionError::StaticOnDynamic)
71    }
72
73    fn dim_dyn(size: usize) -> Result<Self, DimensionError> {
74        Ok(Self::from_usize(size))
75    }
76}