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

geo-ant / varpro / 4842747987

pending completion
4842747987

Pull #21

github

Unknown Committer
Unknown Commit Message
Pull Request #21: Feature/improved model api

349 of 349 new or added lines in 14 files covered. (100.0%)

592 of 669 relevant lines covered (88.49%)

966.01 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
pub(crate) struct ModelBasisFunction<ScalarType>
17
where
18
    ScalarType: Scalar,
19
{
20
    /// the function. Takes the full model parameters alpha.
21
    pub function: BaseFuncType<ScalarType>,
22
    /// the derivatives of the function by index (also taking the full parameters alpha).
23
    /// The index is based on the index of the parameters in the model function set.
24
    /// If a derivative with respect to a given model parameter index does not exist, it
25
    /// means this function does not depend on that model parameter
26
    pub derivatives: HashMap<usize, BaseFuncType<ScalarType>>,
27
}
28

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

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

56
#[inline]
57
/// Helper function to evaluate the given function with the location and parameters
58
/// and make sure that the output vector size of the function has the same length as
59
/// the location vector. Otherwise returns an error.
60
pub fn evaluate_and_check<ScalarType: Scalar>(
2,171✔
61
    func: &BaseFuncType<ScalarType>,
62
    location: &DVector<ScalarType>,
63
    parameters: &[ScalarType],
64
) -> Result<DVector<ScalarType>, ModelError> {
65
    let result = (func)(location, parameters);
2,171✔
66
    if result.len() == location.len() {
2,171✔
67
        Ok(result)
2,169✔
68
    } else {
69
        Err(ModelError::UnexpectedFunctionOutput {
2✔
70
            expected_length: location.len(),
2✔
71
            actual_length: result.len(),
2✔
72
        })
73
    }
74
}
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