numra-core 0.1.4

Core traits and types for the Numra numerical methods library: Scalar, Vector, Signal, Uncertainty, error model.
Documentation
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Uncertainty propagation and sensitivity analysis.
//!
//! This module provides tools for:
//! - Representing uncertain values (mean + variance)
//! - First-order uncertainty propagation
//! - Parameter sensitivity analysis
//!
//! # Theory
//!
//! For a function y = f(x) where x has uncertainty σ_x, the first-order
//! approximation of the output uncertainty is:
//!
//! ```text
//! σ_y ≈ |∂f/∂x| * σ_x
//! ```
//!
//! For multiple parameters: σ_y² = Σ (∂f/∂xᵢ)² * σ_xᵢ²
//!
//! Author: Moussa Leblouba
//! Date: 8 February 2026
//! Modified: 2 May 2026

#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::Scalar;

/// An uncertain value with mean and variance.
///
/// # Example
///
/// ```rust
/// use numra_core::uncertainty::Uncertain;
///
/// // A measurement of 10.0 ± 0.5 (std dev)
/// let x: Uncertain<f64> = Uncertain::new(10.0, 0.25); // variance = 0.5²
///
/// // Scale by 2: mean scales, variance scales by 4
/// let y = x.scale(2.0);
/// assert!((y.mean - 20.0).abs() < 1e-10);
/// assert!((y.variance - 1.0).abs() < 1e-10);
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Uncertain<S: Scalar> {
    /// Mean value
    pub mean: S,
    /// Variance (σ²)
    pub variance: S,
}

impl<S: Scalar> Default for Uncertain<S> {
    fn default() -> Self {
        Self {
            mean: S::ZERO,
            variance: S::ZERO,
        }
    }
}

impl<S: Scalar> Uncertain<S> {
    /// Create a new uncertain value.
    pub fn new(mean: S, variance: S) -> Self {
        Self { mean, variance }
    }

    /// Create from mean and standard deviation.
    pub fn from_std(mean: S, std: S) -> Self {
        Self {
            mean,
            variance: std * std,
        }
    }

    /// Create a certain value (zero variance).
    pub fn certain(value: S) -> Self {
        Self {
            mean: value,
            variance: S::ZERO,
        }
    }

    /// Standard deviation.
    pub fn std(&self) -> S {
        self.variance.sqrt()
    }

    /// Coefficient of variation (std/mean).
    pub fn cv(&self) -> S {
        /// Threshold below which the mean is considered zero for CV computation.
        const CV_ZERO_THRESHOLD: f64 = 1e-15;
        if self.mean.abs() < S::from_f64(CV_ZERO_THRESHOLD) {
            S::INFINITY
        } else {
            self.std() / self.mean.abs()
        }
    }

    /// Scale by a constant: y = a*x.
    /// Variance scales by a².
    pub fn scale(&self, a: S) -> Self {
        Self {
            mean: a * self.mean,
            variance: a * a * self.variance,
        }
    }

    /// Add a constant: y = x + c.
    /// Variance unchanged.
    pub fn add_const(&self, c: S) -> Self {
        Self {
            mean: self.mean + c,
            variance: self.variance,
        }
    }

    /// Add another uncertain value (assuming independence).
    /// y = x1 + x2, Var(y) = Var(x1) + Var(x2).
    pub fn add(&self, other: &Self) -> Self {
        Self {
            mean: self.mean + other.mean,
            variance: self.variance + other.variance,
        }
    }

    /// Subtract another uncertain value (assuming independence).
    pub fn sub(&self, other: &Self) -> Self {
        Self {
            mean: self.mean - other.mean,
            variance: self.variance + other.variance,
        }
    }

    /// Multiply by another uncertain value (first-order approximation).
    /// For `y = x1 * x2`:
    /// `E[y] ≈ E[x1] * E[x2]`
    /// `Var(y) ≈ E[x2]² * Var(x1) + E[x1]² * Var(x2)`
    pub fn mul(&self, other: &Self) -> Self {
        let mean = self.mean * other.mean;
        let variance =
            other.mean * other.mean * self.variance + self.mean * self.mean * other.variance;
        Self { mean, variance }
    }

    /// Divide by another uncertain value (first-order approximation).
    /// For `y = x1 / x2`:
    /// `E[y] ≈ E[x1] / E[x2]`
    /// `Var(y) ≈ (1/E[x2]²) * Var(x1) + (E[x1]/E[x2]²)² * Var(x2)`
    pub fn div(&self, other: &Self) -> Self {
        let mean = self.mean / other.mean;
        let inv_mean2 = S::ONE / (other.mean * other.mean);
        let variance = inv_mean2 * self.variance
            + (self.mean * inv_mean2) * (self.mean * inv_mean2) * other.variance;
        Self { mean, variance }
    }

    /// Apply a function with known derivative.
    /// For y = f(x): Var(y) ≈ (f'(x))² * Var(x).
    pub fn apply<F, D>(&self, f: F, df: D) -> Self
    where
        F: Fn(S) -> S,
        D: Fn(S) -> S,
    {
        let mean = f(self.mean);
        let deriv = df(self.mean);
        let variance = deriv * deriv * self.variance;
        Self { mean, variance }
    }

    /// Square: y = x².
    pub fn square(&self) -> Self {
        self.apply(|x| x * x, |x| S::from_f64(2.0) * x)
    }

    /// Square root: y = √x.
    pub fn sqrt_unc(&self) -> Self {
        self.apply(|x| x.sqrt(), |x| S::ONE / (S::from_f64(2.0) * x.sqrt()))
    }

    /// Exponential: y = e^x.
    pub fn exp(&self) -> Self {
        self.apply(|x| x.exp(), |x| x.exp())
    }

    /// Natural log: y = ln(x).
    pub fn ln(&self) -> Self {
        self.apply(|x| x.ln(), |x| S::ONE / x)
    }

    /// Sine: y = sin(x).
    pub fn sin(&self) -> Self {
        self.apply(|x| x.sin(), |x| x.cos())
    }

    /// Cosine: y = cos(x).
    pub fn cos(&self) -> Self {
        self.apply(|x| x.cos(), |x| -x.sin())
    }
}

/// Importance of one parameter for a scalar pure-function output.
///
/// Computed by central finite differences inside [`compute_sensitivities`].
/// Distinct from the ODE forward-sensitivity matrix
/// `numra_ode::SensitivityResult` (which carries `dy(t)/dp` along a trajectory):
/// this type is for ranking parameters of a fixed scalar function `f(p) -> S`.
#[derive(Clone, Debug)]
pub struct ParameterSensitivity<S: Scalar> {
    /// Parameter name
    pub name: String,
    /// Sensitivity coefficient: ∂y/∂p
    pub coefficient: S,
    /// Normalized sensitivity: (p/y) * ∂y/∂p
    pub normalized: S,
}

/// Result of parameter-importance analysis on a scalar pure function.
///
/// Distinct from the ODE forward-sensitivity result type
/// `numra_ode::SensitivityResult`. See [`ParameterSensitivity`] for the
/// distinction.
#[derive(Clone, Debug)]
pub struct ParameterSensitivityResult<S: Scalar> {
    /// Output value at nominal parameters
    pub output: S,
    /// Sensitivities for each parameter
    pub sensitivities: Vec<ParameterSensitivity<S>>,
}

impl<S: Scalar> ParameterSensitivityResult<S> {
    /// Create a new sensitivity result.
    pub fn new(output: S, sensitivities: Vec<ParameterSensitivity<S>>) -> Self {
        Self {
            output,
            sensitivities,
        }
    }

    /// Find the most sensitive parameter.
    pub fn most_sensitive(&self) -> Option<&ParameterSensitivity<S>> {
        self.sensitivities
            .iter()
            .max_by(|a, b| a.normalized.abs().partial_cmp(&b.normalized.abs()).unwrap())
    }

    /// Propagate uncertainty from parameter uncertainties.
    /// Returns the output uncertainty (variance).
    pub fn propagate_uncertainty(&self, param_variances: &[S]) -> S {
        assert_eq!(param_variances.len(), self.sensitivities.len());
        let mut total_var = S::ZERO;
        for (sens, &var) in self.sensitivities.iter().zip(param_variances.iter()) {
            total_var += sens.coefficient * sens.coefficient * var;
        }
        total_var
    }
}

/// Compute finite difference sensitivities.
///
/// # Arguments
/// * `f` - Function to evaluate
/// * `params` - Nominal parameter values
/// * `names` - Parameter names
/// * `h` - Step size for finite differences (default: `cbrt(S::EPSILON)`,
///   the canonical central-FD step factor)
pub fn compute_sensitivities<S: Scalar, F>(
    f: F,
    params: &[S],
    names: &[&str],
    h: Option<S>,
) -> ParameterSensitivityResult<S>
where
    F: Fn(&[S]) -> S,
{
    let h = h.unwrap_or(S::EPSILON.cbrt());
    let output = f(params);

    let mut sensitivities = Vec::with_capacity(params.len());
    let mut params_pert = params.to_vec();

    for (i, name) in names.iter().enumerate() {
        let p0 = params[i];
        let step = h * (S::ONE + p0.abs());

        // Central difference
        params_pert[i] = p0 + step;
        let f_plus = f(&params_pert);
        params_pert[i] = p0 - step;
        let f_minus = f(&params_pert);
        params_pert[i] = p0;

        let coeff = (f_plus - f_minus) / (S::from_f64(2.0) * step);
        const NORMALIZED_ZERO_THRESHOLD: f64 = 1e-15;
        let normalized = if output.abs() > S::from_f64(NORMALIZED_ZERO_THRESHOLD) {
            (p0 / output) * coeff
        } else {
            S::ZERO
        };

        sensitivities.push(ParameterSensitivity {
            name: name.to_string(),
            coefficient: coeff,
            normalized,
        });
    }

    ParameterSensitivityResult::new(output, sensitivities)
}

/// Interval arithmetic for bounds propagation.
///
/// Represents an interval [lower, upper].
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Interval<S: Scalar> {
    /// Lower bound
    pub lower: S,
    /// Upper bound
    pub upper: S,
}

impl<S: Scalar> Interval<S> {
    /// Create a new interval.
    pub fn new(lower: S, upper: S) -> Self {
        debug_assert!(lower <= upper, "Invalid interval: lower > upper");
        Self { lower, upper }
    }

    /// Create a point interval [x, x].
    pub fn point(x: S) -> Self {
        Self { lower: x, upper: x }
    }

    /// Create from center and half-width.
    pub fn from_center(center: S, half_width: S) -> Self {
        Self {
            lower: center - half_width,
            upper: center + half_width,
        }
    }

    /// Width of the interval.
    pub fn width(&self) -> S {
        self.upper - self.lower
    }

    /// Center of the interval.
    pub fn center(&self) -> S {
        (self.lower + self.upper) / S::from_f64(2.0)
    }

    /// Check if interval contains a value.
    pub fn contains(&self, x: S) -> bool {
        x >= self.lower && x <= self.upper
    }

    /// Add two intervals.
    pub fn add(&self, other: &Self) -> Self {
        Self {
            lower: self.lower + other.lower,
            upper: self.upper + other.upper,
        }
    }

    /// Subtract two intervals.
    pub fn sub(&self, other: &Self) -> Self {
        Self {
            lower: self.lower - other.upper,
            upper: self.upper - other.lower,
        }
    }

    /// Multiply two intervals.
    pub fn mul(&self, other: &Self) -> Self {
        let products = [
            self.lower * other.lower,
            self.lower * other.upper,
            self.upper * other.lower,
            self.upper * other.upper,
        ];
        let lower = products.iter().fold(S::INFINITY, |a, &b| a.min(b));
        let upper = products.iter().fold(S::NEG_INFINITY, |a, &b| a.max(b));
        Self { lower, upper }
    }

    /// Scale by a constant.
    pub fn scale(&self, a: S) -> Self {
        if a >= S::ZERO {
            Self {
                lower: a * self.lower,
                upper: a * self.upper,
            }
        } else {
            Self {
                lower: a * self.upper,
                upper: a * self.lower,
            }
        }
    }

    /// Union of two intervals.
    pub fn union(&self, other: &Self) -> Self {
        Self {
            lower: self.lower.min(other.lower),
            upper: self.upper.max(other.upper),
        }
    }

    /// Intersection of two intervals (None if empty).
    pub fn intersection(&self, other: &Self) -> Option<Self> {
        let lower = self.lower.max(other.lower);
        let upper = self.upper.min(other.upper);
        if lower <= upper {
            Some(Self { lower, upper })
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_uncertain_basic() {
        let x: Uncertain<f64> = Uncertain::from_std(10.0, 0.5);
        assert!((x.mean - 10.0).abs() < 1e-10);
        assert!((x.variance - 0.25).abs() < 1e-10);
        assert!((x.std() - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_scale() {
        let x: Uncertain<f64> = Uncertain::from_std(10.0, 1.0);
        let y = x.scale(2.0);
        assert!((y.mean - 20.0).abs() < 1e-10);
        assert!((y.std() - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_add() {
        let x: Uncertain<f64> = Uncertain::new(10.0, 1.0);
        let y: Uncertain<f64> = Uncertain::new(5.0, 0.5);
        let z = x.add(&y);
        assert!((z.mean - 15.0).abs() < 1e-10);
        assert!((z.variance - 1.5).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_mul() {
        // x = 10 ± 1, y = 5 ± 0.5
        // E[xy] ≈ 50
        // Var(xy) ≈ 25*1 + 100*0.25 = 25 + 25 = 50
        let x: Uncertain<f64> = Uncertain::new(10.0, 1.0);
        let y: Uncertain<f64> = Uncertain::new(5.0, 0.25);
        let z = x.mul(&y);
        assert!((z.mean - 50.0).abs() < 1e-10);
        assert!((z.variance - 50.0).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_exp() {
        // For y = e^x at x=0: dy/dx = e^0 = 1
        // So Var(y) ≈ 1² * Var(x) = Var(x)
        let x: Uncertain<f64> = Uncertain::new(0.0, 0.01);
        let y = x.exp();
        assert!((y.mean - 1.0).abs() < 1e-10);
        assert!((y.variance - 0.01).abs() < 1e-10);
    }

    #[test]
    fn test_sensitivity_analysis() {
        // f(a, b) = a * b
        // ∂f/∂a = b = 2, ∂f/∂b = a = 3
        let f = |p: &[f64]| p[0] * p[1];
        let params = [3.0, 2.0];
        let names = ["a", "b"];

        let result = compute_sensitivities(f, &params, &names, None);

        assert!((result.output - 6.0).abs() < 1e-10);
        assert!((result.sensitivities[0].coefficient - 2.0).abs() < 1e-5);
        assert!((result.sensitivities[1].coefficient - 3.0).abs() < 1e-5);
    }

    #[test]
    fn test_interval_basic() {
        let i: Interval<f64> = Interval::new(1.0, 3.0);
        assert!((i.width() - 2.0).abs() < 1e-10);
        assert!((i.center() - 2.0).abs() < 1e-10);
        assert!(i.contains(2.0));
        assert!(!i.contains(4.0));
    }

    #[test]
    fn test_interval_add() {
        let a: Interval<f64> = Interval::new(1.0, 2.0);
        let b: Interval<f64> = Interval::new(3.0, 4.0);
        let c = a.add(&b);
        assert!((c.lower - 4.0).abs() < 1e-10);
        assert!((c.upper - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_interval_mul() {
        let a: Interval<f64> = Interval::new(-1.0, 2.0);
        let b: Interval<f64> = Interval::new(1.0, 3.0);
        let c = a.mul(&b);
        // Products: -1*1=-1, -1*3=-3, 2*1=2, 2*3=6
        // So c = [-3, 6]
        assert!((c.lower + 3.0).abs() < 1e-10);
        assert!((c.upper - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_interval_intersection() {
        let a: Interval<f64> = Interval::new(1.0, 4.0);
        let b: Interval<f64> = Interval::new(2.0, 5.0);
        let c = a.intersection(&b).unwrap();
        assert!((c.lower - 2.0).abs() < 1e-10);
        assert!((c.upper - 4.0).abs() < 1e-10);

        let d: Interval<f64> = Interval::new(5.0, 6.0);
        assert!(a.intersection(&d).is_none());
    }

    // ============================================================================
    // Edge Case Tests
    // ============================================================================

    #[test]
    fn test_uncertain_zero_variance() {
        let x: Uncertain<f64> = Uncertain::certain(5.0);
        assert!(x.variance.abs() < 1e-15);
        assert!((x.std()).abs() < 1e-15);

        // Operations preserve certainty
        let y = x.scale(2.0);
        assert!(y.variance.abs() < 1e-15);

        let z = x.add_const(10.0);
        assert!(z.variance.abs() < 1e-15);
    }

    #[test]
    fn test_uncertain_zero_mean() {
        let x: Uncertain<f64> = Uncertain::new(0.0, 1.0);

        // CV should be infinity for zero mean
        assert!(x.cv().is_infinite());

        // Scaling zero still gives zero
        let y = x.scale(100.0);
        assert!(y.mean.abs() < 1e-15);
        assert!((y.variance - 10000.0).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_negative_values() {
        let x: Uncertain<f64> = Uncertain::from_std(-10.0, 2.0);
        assert!((x.mean + 10.0).abs() < 1e-10);

        // Scaling by negative flips the mean
        let y = x.scale(-1.0);
        assert!((y.mean - 10.0).abs() < 1e-10);
        assert!((y.variance - x.variance).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_small_values() {
        let x: Uncertain<f64> = Uncertain::from_std(1e-15, 1e-16);
        let y: Uncertain<f64> = Uncertain::from_std(1e-15, 1e-16);

        let z = x.add(&y);
        assert!(z.mean > 0.0);
        assert!(z.variance > 0.0);
    }

    #[test]
    fn test_uncertain_large_values() {
        let x: Uncertain<f64> = Uncertain::from_std(1e15, 1e14);
        let y = x.scale(0.5);
        assert!((y.mean - 5e14).abs() < 1e10);
    }

    #[test]
    fn test_interval_point() {
        let i: Interval<f64> = Interval::point(5.0);
        assert!((i.width()).abs() < 1e-15);
        assert!(i.contains(5.0));
        assert!(!i.contains(5.0 + 1e-10));
    }

    #[test]
    fn test_interval_negative() {
        let i: Interval<f64> = Interval::new(-5.0, -2.0);
        assert!(i.contains(-3.0));
        assert!(!i.contains(0.0));
        assert!((i.center() + 3.5).abs() < 1e-10);
    }

    #[test]
    fn test_interval_scale_negative() {
        let i: Interval<f64> = Interval::new(2.0, 4.0);
        let j = i.scale(-2.0);
        // [-2*4, -2*2] = [-8, -4]
        assert!((j.lower + 8.0).abs() < 1e-10);
        assert!((j.upper + 4.0).abs() < 1e-10);
    }

    #[test]
    fn test_interval_subtract() {
        let a: Interval<f64> = Interval::new(2.0, 4.0);
        let b: Interval<f64> = Interval::new(1.0, 3.0);
        let c = a.sub(&b);
        // [2-3, 4-1] = [-1, 3]
        assert!((c.lower + 1.0).abs() < 1e-10);
        assert!((c.upper - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_interval_union() {
        let a: Interval<f64> = Interval::new(1.0, 3.0);
        let b: Interval<f64> = Interval::new(5.0, 7.0);
        let c = a.union(&b);
        assert!((c.lower - 1.0).abs() < 1e-10);
        assert!((c.upper - 7.0).abs() < 1e-10);
    }

    #[test]
    fn test_interval_self_intersection() {
        let a: Interval<f64> = Interval::new(1.0, 5.0);
        let b = a.intersection(&a).unwrap();
        assert!((b.lower - a.lower).abs() < 1e-10);
        assert!((b.upper - a.upper).abs() < 1e-10);
    }

    #[test]
    fn test_uncertain_div_small_denominator() {
        let x: Uncertain<f64> = Uncertain::new(10.0, 1.0);
        let y: Uncertain<f64> = Uncertain::new(0.01, 0.0001);
        let z = x.div(&y);
        // Mean should be large
        assert!(z.mean > 100.0);
    }

    #[test]
    fn test_sensitivity_single_param() {
        // f(x) = x^2
        let f = |p: &[f64]| p[0] * p[0];
        let params = [3.0];
        let names = ["x"];

        let result = compute_sensitivities(f, &params, &names, None);
        // ∂f/∂x = 2x = 6
        assert!((result.sensitivities[0].coefficient - 6.0).abs() < 1e-4);
    }

    #[test]
    fn test_sensitivity_zero_output() {
        // f(x, y) = x - x = 0
        let f = |p: &[f64]| p[0] - p[0];
        let params = [5.0];
        let names = ["x"];

        let result = compute_sensitivities(f, &params, &names, None);
        assert!(result.output.abs() < 1e-15);
        // Normalized should be zero because output is zero
        assert!(result.sensitivities[0].normalized.abs() < 1e-10);
    }
}