conspire/math/
mod.rs

1//! Mathematics library.
2
3#[cfg(test)]
4pub mod test;
5
6/// Special functions.
7pub mod special;
8
9/// Integration and ODEs.
10pub mod integrate;
11
12/// Interpolation schemes.
13pub mod interpolate;
14
15/// Optimization and root finding.
16pub mod optimize;
17
18mod matrix;
19mod tensor;
20
21pub use matrix::{
22    Matrix,
23    square::{Banded, SquareMatrix},
24    vector::Vector,
25};
26pub use tensor::{
27    Hessian, Jacobian, Rank2, Scalar, ScalarList, ScalarListVec, Scalars, Solution, Tensor,
28    TensorArray, TensorError, TensorVec,
29    list::TensorList,
30    rank_0::{TensorRank0, list::TensorRank0List, list_2d::TensorRank0List2D},
31    rank_1::{
32        TensorRank1, list::TensorRank1List, list_2d::TensorRank1List2D, vec::TensorRank1Vec,
33        vec_2d::TensorRank1Vec2D, zero as tensor_rank_1_zero,
34    },
35    rank_2::{
36        IDENTITY, IDENTITY_00, IDENTITY_10, IDENTITY_22, TensorRank2, ZERO, ZERO_10,
37        list::{TensorRank2List, vec::TensorRank2ListVec},
38        list_2d::TensorRank2List2D,
39        vec::TensorRank2Vec,
40        vec_2d::TensorRank2Vec2D,
41    },
42    rank_3::{LEVI_CIVITA, TensorRank3, levi_civita},
43    rank_4::{
44        ContractAllIndicesWithFirstIndicesOf, ContractFirstSecondIndicesWithSecondIndicesOf,
45        ContractFirstThirdFourthIndicesWithFirstIndicesOf,
46        ContractSecondFourthIndicesWithFirstIndicesOf, ContractSecondIndexWithFirstIndexOf,
47        ContractThirdFourthIndicesWithFirstSecondIndicesOf, ContractThirdIndexWithFirstIndexOf,
48        IDENTITY_1010, TensorRank4, list::TensorRank4List, vec::TensorRank4Vec,
49    },
50    test::{TestError, assert_eq, assert_eq_within, assert_eq_within_tols},
51    tuple::{
52        TensorTuple,
53        list::{TensorTupleList, vec::TensorTupleListVec, vec_2d::TensorTupleListVec2D},
54        vec::TensorTupleVec,
55    },
56    vec::{TensorRank1RefVec, TensorVector},
57};
58
59use std::fmt;
60
61fn write_tensor_rank_0(f: &mut fmt::Formatter, tensor_rank_0: &TensorRank0) -> fmt::Result {
62    let num = if tensor_rank_0.abs() > 1e-1 {
63        (tensor_rank_0 * 1e6).round() / 1e6
64    } else {
65        *tensor_rank_0
66    };
67    let num_abs = num.abs();
68    if num.is_nan() {
69        write!(f, "{num:>11}, ")
70    } else if num == 0.0 || num_abs == 1.0 {
71        let temp_1 = format!("{num:>11.6e}, ").to_string();
72        let mut temp_2 = temp_1.split("e");
73        let a = temp_2.next().unwrap();
74        let b = temp_2.next().unwrap();
75        write!(f, "{a}e+00{b}")
76    } else if num_abs <= 1e-100 {
77        write!(f, "{num:>14.6e}, ")
78    } else if num_abs >= 1e100 {
79        let temp_1 = format!("{num:>13.6e}, ").to_string();
80        let mut temp_2 = temp_1.split("e");
81        let a = temp_2.next().unwrap();
82        let b = temp_2.next().unwrap();
83        write!(f, "{a}e+{b}")
84    } else if num_abs < 1e-9 {
85        let temp_1 = format!("{num:>13.6e}, ").to_string();
86        let mut temp_2 = temp_1.split("e");
87        let a = temp_2.next().unwrap();
88        let b = temp_2.next().unwrap();
89        let mut c = b.split("-");
90        c.next();
91        let e = c.next().unwrap();
92        write!(f, "{a}e-0{e}")
93    } else if num_abs >= 1e10 {
94        let temp_1 = format!("{num:>12.6e}, ").to_string();
95        let mut temp_2 = temp_1.split("e");
96        let a = temp_2.next().unwrap();
97        let b = temp_2.next().unwrap();
98        write!(f, "{a}e+0{b}")
99    } else if num_abs <= 1e0 {
100        let temp_1 = format!("{num:>12.6e}, ").to_string();
101        let mut temp_2 = temp_1.split("e");
102        let a = temp_2.next().unwrap();
103        let b = temp_2.next().unwrap();
104        let mut c = b.split("-");
105        c.next();
106        let e = c.next().unwrap();
107        write!(f, "{a}e-00{e}")
108    } else {
109        let temp_1 = format!("{num:>11.6e}, ").to_string();
110        let mut temp_2 = temp_1.split("e");
111        let a = temp_2.next().unwrap();
112        let b = temp_2.next().unwrap();
113        write!(f, "{a}e+00{b}")
114    }
115}