• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geo-ant / varpro / 10126064836

27 Jul 2024 08:02PM UTC coverage: 82.236%. Remained the same
10126064836

Pull #36

github

web-flow
Merge 14f457c27 into 70eb1d40c
Pull Request #36: Feature/bump version

824 of 1002 relevant lines covered (82.24%)

1268.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/src/model/model_basis_function.rs
1
use std::collections::HashMap;
2

3
use crate::model::errors::ModelError;
4
use nalgebra::base::Scalar;
5
use nalgebra::DVector;
6

7
/// Helper type that is a typedef for a function `$f(\vec{x},\vec{\alpha})$`, where
8
/// the first argument is a location argument, and the second argument is the
9
/// (nonlinear) parameters. This is the most low level representation of how our
10
/// wrapped functions are actually stored inside the model functions
11
type BaseFuncType<ScalarType> =
12
    Box<dyn Fn(&DVector<ScalarType>, &[ScalarType]) -> DVector<ScalarType>>;
13

14
/// An internal type that is used to store basefunctions whose interface has been wrapped in
15
/// such a way that they can accept the location and the *complete model parameters as arguments*.
16
#[doc(hidden)]
17
pub struct ModelBasisFunction<ScalarType>
18
where
19
    ScalarType: Scalar,
20
{
21
    /// the function. Takes the full model parameters alpha.
22
    pub function: BaseFuncType<ScalarType>,
23
    /// the derivatives of the function by index (also taking the full parameters alpha).
24
    /// The index is based on the index of the parameters in the model function set.
25
    /// If a derivative with respect to a given model parameter index does not exist, it
26
    /// means this function does not depend on that model parameter
27
    pub derivatives: HashMap<usize, BaseFuncType<ScalarType>>,
28
}
29

30
impl<ScalarType: Scalar> std::fmt::Debug for ModelBasisFunction<ScalarType> {
31
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
32
        f.debug_struct("ModelBasisFunction")
×
33
            .field("function", &"/* omitted */")
×
34
            .field("derivatives", &"/* omitted */")
×
35
            .finish()
36
    }
37
}
38

39
impl<ScalarType> ModelBasisFunction<ScalarType>
40
where
41
    ScalarType: Scalar,
42
{
43
    /// Create a function that does not depend on any model parameters and just
44
    /// takes a location parameter as its argument.
45
    /// To create parameter dependent model basis functions use the [ModelBasisFunctionBuilder].
46
    pub fn parameter_independent<FuncType>(function: FuncType) -> Self
14✔
47
    where
48
        FuncType: Fn(&DVector<ScalarType>) -> DVector<ScalarType> + 'static,
49
    {
50
        Self {
51
            function: Box::new(move |x, _params| (function)(x)),
743✔
52
            derivatives: HashMap::default(),
14✔
53
        }
54
    }
55
}
56

57
#[inline]
58
/// Helper function to evaluate the given function with the location and parameters
59
/// and make sure that the output vector size of the function has the same length as
60
/// the location vector. Otherwise returns an error.
61
pub fn evaluate_and_check<ScalarType: Scalar>(
2,254✔
62
    func: &BaseFuncType<ScalarType>,
63
    location: &DVector<ScalarType>,
64
    parameters: &[ScalarType],
65
) -> Result<DVector<ScalarType>, ModelError> {
66
    let result = (func)(location, parameters);
2,254✔
67
    if result.len() == location.len() {
2,254✔
68
        Ok(result)
2,252✔
69
    } else {
70
        Err(ModelError::UnexpectedFunctionOutput {
2✔
71
            expected_length: location.len(),
2✔
72
            actual_length: result.len(),
2✔
73
        })
74
    }
75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc