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

geo-ant / varpro / 4092757635

pending completion
4092757635

push

github

Unknown Committer
Unknown Commit Message

13 of 13 new or added lines in 1 file covered. (100.0%)

338 of 410 relevant lines covered (82.44%)

5.01 hits per line

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

100.0
/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> ModelBasisFunction<ScalarType>
30
where
31
    ScalarType: Scalar,
32
{
33
    /// Create a function that does not depend on any model parameters and just
34
    /// takes a location parameter as its argument.
35
    /// To create parameter dependent model basis functions use the [ModelBasisFunctionBuilder].
36
    pub fn parameter_independent<FuncType>(function: FuncType) -> Self
6✔
37
    where
38
        FuncType: Fn(&DVector<ScalarType>) -> DVector<ScalarType> + 'static,
39
    {
40
        Self {
41
            function: Box::new(move |x, _params| (function)(x)),
14✔
42
            derivatives: HashMap::default(),
6✔
43
        }
44
    }
45
}
46

47
#[inline]
48
/// Helper function to evaluate the given function with the location and parameters
49
/// and make sure that the output vector size of the function has the same length as
50
/// the location vector. Otherwise returns an error.
51
pub fn evaluate_and_check<ScalarType: Scalar>(
2✔
52
    func: &BaseFuncType<ScalarType>,
53
    location: &DVector<ScalarType>,
54
    parameters: &[ScalarType],
55
) -> Result<DVector<ScalarType>, ModelError> {
56
    let result = (func)(location, parameters);
2✔
57
    if result.len() == location.len() {
5✔
58
        Ok(result)
2✔
59
    } else {
60
        Err(ModelError::UnexpectedFunctionOutput {
1✔
61
            expected_length: location.len(),
1✔
62
            actual_length: result.len(),
1✔
63
        })
64
    }
65
}
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

© 2026 Coveralls, Inc