src/types.rs

branch
dev
changeset 35
b087e3eab191
parent 34
efa60bc4f743
equal deleted inserted replaced
34:efa60bc4f743 35:b087e3eab191
2 2
3 use numeric_literals::replace_float_literals; 3 use numeric_literals::replace_float_literals;
4 4
5 use colored::ColoredString; 5 use colored::ColoredString;
6 use serde::{Serialize, Deserialize}; 6 use serde::{Serialize, Deserialize};
7 use clap::ValueEnum;
8 use alg_tools::iterate::LogRepr; 7 use alg_tools::iterate::LogRepr;
9 use alg_tools::euclidean::Euclidean; 8 use alg_tools::euclidean::Euclidean;
10 use alg_tools::norms::{Norm, L1}; 9 use alg_tools::norms::{Norm, L1};
11 10
12 pub use alg_tools::types::*; 11 pub use alg_tools::types::*;
13 pub use alg_tools::loc::Loc; 12 pub use alg_tools::loc::Loc;
14 pub use alg_tools::sets::Cube; 13 pub use alg_tools::sets::Cube;
15 14
16 use crate::measures::DiscreteMeasure; 15 // use crate::measures::DiscreteMeasure;
17 16
18 /// [`Float`] with extra display and string conversion traits such that [`clap`] doesn't choke up. 17 /// [`Float`] with extra display and string conversion traits such that [`clap`] doesn't choke up.
19 pub trait ClapFloat : Float 18 pub trait ClapFloat : Float
20 + std::str::FromStr<Err=std::num::ParseFloatError> 19 + std::str::FromStr<Err=std::num::ParseFloatError>
21 + std::fmt::Display {} 20 + std::fmt::Display {}
29 pub value : F, 28 pub value : F,
30 /// Number of spikes 29 /// Number of spikes
31 pub n_spikes : usize, 30 pub n_spikes : usize,
32 /// Number of iterations this statistic covers 31 /// Number of iterations this statistic covers
33 pub this_iters : usize, 32 pub this_iters : usize,
33 /// Number of spikes inserted since last IterInfo statistic
34 pub inserted : usize,
34 /// Number of spikes removed by merging since last IterInfo statistic 35 /// Number of spikes removed by merging since last IterInfo statistic
35 pub merged : usize, 36 pub merged : usize,
36 /// Number of spikes removed by pruning since last IterInfo statistic 37 /// Number of spikes removed by pruning since last IterInfo statistic
37 pub pruned : usize, 38 pub pruned : usize,
38 /// Number of inner iterations since last IterInfo statistic 39 /// Number of inner iterations since last IterInfo statistic
41 pub untransported_fraction : Option<(F, F)>, 42 pub untransported_fraction : Option<(F, F)>,
42 /// Tuple of (|destination mass - untransported_mass|, transported mass) 43 /// Tuple of (|destination mass - untransported_mass|, transported mass)
43 pub transport_error : Option<(F, F)>, 44 pub transport_error : Option<(F, F)>,
44 /// Current tolerance 45 /// Current tolerance
45 pub ε : F, 46 pub ε : F,
46 /// Solve fin.dim problem for this measure to get the optimal `value`. 47 // /// Solve fin.dim problem for this measure to get the optimal `value`.
47 pub postprocessing : Option<DiscreteMeasure<Loc<F, N>, F>>, 48 // pub postprocessing : Option<RNDM<F, N>>,
48 } 49 }
49 50
50 impl<F : Float, const N : usize> IterInfo<F, N> { 51 impl<F : Float, const N : usize> IterInfo<F, N> {
51 /// Initialise statistics with zeros. `ε` and `value` are unspecified. 52 /// Initialise statistics with zeros. `ε` and `value` are unspecified.
52 pub fn new() -> Self { 53 pub fn new() -> Self {
53 IterInfo { 54 IterInfo {
54 value : F::NAN, 55 value : F::NAN,
55 n_spikes : 0, 56 n_spikes : 0,
56 this_iters : 0, 57 this_iters : 0,
57 merged : 0, 58 merged : 0,
59 inserted : 0,
58 pruned : 0, 60 pruned : 0,
59 inner_iters : 0, 61 inner_iters : 0,
60 ε : F::NAN, 62 ε : F::NAN,
61 postprocessing : None, 63 // postprocessing : None,
62 untransported_fraction : None, 64 untransported_fraction : None,
63 transport_error : None, 65 transport_error : None,
64 } 66 }
65 } 67 }
66 } 68 }
67 69
68 #[replace_float_literals(F::cast_from(literal))] 70 #[replace_float_literals(F::cast_from(literal))]
69 impl<F, const N : usize> LogRepr for IterInfo<F, N> where F : LogRepr + Float { 71 impl<F, const N : usize> LogRepr for IterInfo<F, N> where F : LogRepr + Float {
70 fn logrepr(&self) -> ColoredString { 72 fn logrepr(&self) -> ColoredString {
71 format!("{}\t| N = {}, ε = {:.8}, inner_iters_mean = {}, merged+pruned_mean = {}+{}{}{}", 73 format!("{}\t| N = {}, ε = {:.8}, 𝔼inner_it = {}, 𝔼ins/mer/pru = {}/{}/{}{}{}",
72 self.value.logrepr(), 74 self.value.logrepr(),
73 self.n_spikes, 75 self.n_spikes,
74 self.ε, 76 self.ε,
75 self.inner_iters as float / self.this_iters as float, 77 self.inner_iters as float / self.this_iters.max(1) as float,
76 self.merged as float / self.this_iters as float, 78 self.inserted as float / self.this_iters.max(1) as float,
77 self.pruned as float / self.this_iters as float, 79 self.merged as float / self.this_iters.max(1) as float,
80 self.pruned as float / self.this_iters.max(1) as float,
78 match self.untransported_fraction { 81 match self.untransported_fraction {
79 None => format!(""), 82 None => format!(""),
80 Some((a, b)) => if b > 0.0 { 83 Some((a, b)) => if b > 0.0 {
81 format!(", untransported {:.2}%", 100.0*a/b) 84 format!(", untransported {:.2}%", 100.0*a/b)
82 } else { 85 } else {
115 } 118 }
116 } 119 }
117 } 120 }
118 121
119 /// Data term type 122 /// Data term type
120 #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug, ValueEnum)] 123 #[derive(Clone, Copy, PartialEq, Serialize, Deserialize, Debug)]
121 pub enum DataTerm { 124 pub enum DataTerm {
122 /// $\\|z\\|\_2^2/2$ 125 /// $\\|z\\|\_2^2/2$
123 L2Squared, 126 L2Squared,
124 /// $\\|z\\|\_1$ 127 /// $\\|z\\|\_1$
125 L1, 128 L1,
138 /// Type for indicating norm-2-squared data fidelity or transport cost. 141 /// Type for indicating norm-2-squared data fidelity or transport cost.
139 #[derive(Clone, Copy, Serialize, Deserialize)] 142 #[derive(Clone, Copy, Serialize, Deserialize)]
140 pub struct L2Squared; 143 pub struct L2Squared;
141 144
142 /// Trait for indicating that `Self` is Lipschitz with respect to the (semi)norm `D`. 145 /// Trait for indicating that `Self` is Lipschitz with respect to the (semi)norm `D`.
143 pub trait Lipschitz<D> { 146 pub trait Lipschitz<M> {
144 /// The type of floats 147 /// The type of floats
145 type FloatType : Float; 148 type FloatType : Float;
146 149
147 /// Returns the Lipschitz factor of `self` with respect to the (semi)norm `D`. 150 /// Returns the Lipschitz factor of `self` with respect to the (semi)norm `D`.
148 fn lipschitz_factor(&self, seminorm : D) -> Option<Self::FloatType>; 151 fn lipschitz_factor(&self, seminorm : M) -> Option<Self::FloatType>;
149 } 152 }
153
154 /// Trait for norm-bounded functions.
155 pub trait NormBounded<M> {
156 type FloatType : Float;
157
158 /// Returns a bound on the values of this function object in the `M`-norm.
159 fn norm_bound(&self, m : M) -> Self::FloatType;
160 }

mercurial