-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
72 lines (63 loc) · 2.13 KB
/
mod.rs
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
#![allow(missing_docs)]
pub mod differentiation;
use nalgebra::{DVector, Scalar};
use num_traits::Float;
use crate::model::builder::SeparableModelBuilder;
use crate::model::SeparableModel;
/// exponential decay f(t,tau) = exp(-t/tau)
pub fn exp_decay<ScalarType: Float + Scalar>(
tvec: &DVector<ScalarType>,
tau: ScalarType,
) -> DVector<ScalarType> {
tvec.map(|t| (-t / tau).exp())
}
/// derivative of exp decay with respect to tau
pub fn exp_decay_dtau<ScalarType: Scalar + Float>(
tvec: &DVector<ScalarType>,
tau: ScalarType,
) -> DVector<ScalarType> {
tvec.map(|t| (-t / tau).exp() * t / (tau * tau))
}
/// function sin (omega*t+phi)
pub fn sin_ometa_t_plus_phi<ScalarType: Scalar + Float>(
tvec: &DVector<ScalarType>,
omega: ScalarType,
phi: ScalarType,
) -> DVector<ScalarType> {
tvec.map(|t| (omega * t + phi).sin())
}
/// derivative d/d(omega) sin (omega*t+phi)
pub fn sin_ometa_t_plus_phi_domega<ScalarType: Scalar + Float>(
tvec: &DVector<ScalarType>,
omega: ScalarType,
phi: ScalarType,
) -> DVector<ScalarType> {
tvec.map(|t| t * (omega * t + phi).cos())
}
/// derivative d/d(phi) sin (omega*t+phi)
pub fn sin_ometa_t_plus_phi_dphi<ScalarType: Scalar + Float>(
tvec: &DVector<ScalarType>,
omega: ScalarType,
phi: ScalarType,
) -> DVector<ScalarType> {
tvec.map(|t| (omega * t + phi).cos())
}
/// A helper function that returns a double exponential decay model
/// f(x,tau1,tau2) = c1*exp(-x/tau1)+c2*exp(-x/tau2)+c3
/// Model parameters are: tau1, tau2
pub fn get_double_exponential_model_with_constant_offset(
x: DVector<f64>,
initial_params: Vec<f64>,
) -> SeparableModel<f64> {
let ones = |t: &DVector<_>| DVector::from_element(t.len(), 1.);
SeparableModelBuilder::new(&["tau1", "tau2"])
.function(&["tau2"], exp_decay)
.partial_deriv("tau2", exp_decay_dtau)
.function(&["tau1"], exp_decay)
.partial_deriv("tau1", exp_decay_dtau)
.invariant_function(ones)
.independent_variable(x)
.initial_parameters(initial_params)
.build()
.expect("double exponential model builder should produce a valid model")
}