RustedSciThe 0.4.20

Rust framework for symbolic and numerical computing:BVP ( Newton-Raphson frozen/damped/with collocations ), IVP( BDF, Radau, Backward Euler, LSODE, LSODA, RK45, DoPri), nonlinear equations ( Levenberg, Gavin) and more
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
//! Legacy BVP Lambdify runtime support.
//!
//! This module deliberately contains only compatibility instrumentation and
//! small shared helpers for the historical Expr-based callbacks.  It is kept
//! separate from the direct no-Mutex runtime so benchmark results can identify
//! which implementation produced them.

use super::parameter_binding::BvpParameterBindingHandle;
use super::telemetry::{BvpLambdifyExecutionPolicy, flatten_lambdify_args};
use crate::global::THRESHOLD as T;
use crate::numerical::BVP_Damp::BVP_traits::{Fun, FunEnum, Jac, JacEnum};
use crate::symbolic::symbolic_engine::Expr;
use faer::col::{Col, ColRef};
use faer::sparse::{SparseColMat, Triplet};
use nalgebra::{DMatrix, DVector};
use rayon::prelude::*;

/// Compatibility names retained for callers of the old legacy module.
pub use super::telemetry::{
    BvpLambdifyTelemetry as LegacyLambdifyTelemetry,
    BvpLambdifyTelemetrySnapshot as LegacyLambdifyTelemetrySnapshot,
};

/// Installs the historical Expr-based dense Jacobian callback.
///
/// The public `Jacobian::lambdify_jacobian_DMatrix_par` method delegates here
/// so the legacy callback implementation and its telemetry are physically
/// separate from symbolic preparation and from the direct AtomView runtime.
pub(crate) fn dense_jacobian(
    jac: Vec<Vec<Expr>>,
    rows: usize,
    cols: usize,
    bandwidth: Option<(usize, usize)>,
    argument_names: Vec<String>,
    parameter_values: Option<Vec<f64>>,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Jac> {
    let binding = BvpParameterBindingHandle::new(parameter_values);
    dense_jacobian_with_binding(
        jac,
        rows,
        cols,
        bandwidth,
        argument_names,
        binding,
        parameter_count,
        telemetry,
    )
}

/// Dense ExprLegacy Jacobian callback using a reusable numeric parameter bind.
pub(crate) fn dense_jacobian_with_binding(
    jac: Vec<Vec<Expr>>,
    rows: usize,
    cols: usize,
    bandwidth: Option<(usize, usize)>,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Jac> {
    dense_jacobian_with_binding_and_policy(
        jac,
        rows,
        cols,
        bandwidth,
        argument_names,
        binding,
        parameter_count,
        telemetry,
        BvpLambdifyExecutionPolicy::default(),
    )
}

/// Dense ExprLegacy Jacobian callback with an explicit runtime policy.
pub(crate) fn dense_jacobian_with_binding_and_policy(
    jac: Vec<Vec<Expr>>,
    rows: usize,
    cols: usize,
    bandwidth: Option<(usize, usize)>,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
    execution_policy: BvpLambdifyExecutionPolicy,
) -> Box<dyn Jac> {
    let jacobian_positions: Vec<(usize, usize, Box<dyn Fn(&[f64]) -> f64 + Send + Sync>)> = (0
        ..rows)
        .into_par_iter()
        .flat_map(|i| {
            let (right_border, left_border) = if let Some((kl, ku)) = bandwidth {
                let right_border = std::cmp::min(i + ku + 1, cols);
                let left_border = i.saturating_sub(kl + 1);
                (right_border, left_border)
            } else {
                (cols, 0)
            };
            (left_border..right_border)
                .filter_map(|j| {
                    let derivative = &jac[i][j];
                    (!derivative.is_zero()).then(|| {
                        let names = argument_names
                            .iter()
                            .map(String::as_str)
                            .collect::<Vec<_>>();
                        let compiled =
                            Expr::lambdify_borrowed_thread_safe(derivative, names.as_slice());
                        (i, j, compiled)
                    })
                })
                .collect::<Vec<_>>()
        })
        .collect();

    let callback = Box::new(move |_x: f64, values: &DVector<f64>| -> DMatrix<f64> {
        let started = telemetry.start_timing();
        let parameter_values = binding.snapshot();
        let args = flatten_lambdify_args(parameter_values.as_deref(), values.as_slice());
        assert!(
            parameter_count == 0 || parameter_values.is_some(),
            "parameter values must be provided when parameters are configured"
        );
        let mut matrix = DMatrix::zeros(rows, cols);
        telemetry.record_dispatch(execution_policy, jacobian_positions.len());
        if execution_policy.should_parallel(jacobian_positions.len()) {
            let values: Vec<(usize, usize, f64)> = jacobian_positions
                .par_iter()
                .map(|(row, col, compiled)| (*row, *col, compiled(args.as_slice())))
                .collect();
            for (row, col, value) in values {
                if value.abs() > T {
                    matrix[(row, col)] = value;
                }
            }
        } else {
            for (row, col, compiled) in &jacobian_positions {
                let value = compiled(args.as_slice());
                if value.abs() > T {
                    matrix[(*row, *col)] = value;
                }
            }
        }
        telemetry.record_jacobian_sample(started);
        matrix
    });
    Box::new(JacEnum::Dense(callback))
}

/// Installs the historical Expr-based dense residual callback.
pub(crate) fn dense_residual(
    functions: Vec<Expr>,
    argument_names: Vec<String>,
    parameter_values: Option<Vec<f64>>,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Fun> {
    let binding = BvpParameterBindingHandle::new(parameter_values);
    dense_residual_with_binding(
        functions,
        argument_names,
        binding,
        parameter_count,
        telemetry,
    )
}

/// Dense ExprLegacy residual callback using a reusable numeric parameter bind.
pub(crate) fn dense_residual_with_binding(
    functions: Vec<Expr>,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Fun> {
    dense_residual_with_binding_and_policy(
        functions,
        argument_names,
        binding,
        parameter_count,
        telemetry,
        BvpLambdifyExecutionPolicy::default(),
    )
}

/// Dense ExprLegacy residual callback with an explicit runtime policy.
pub(crate) fn dense_residual_with_binding_and_policy(
    functions: Vec<Expr>,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
    execution_policy: BvpLambdifyExecutionPolicy,
) -> Box<dyn Fun> {
    let compiled_functions: Vec<Box<dyn Fn(&[f64]) -> f64 + Send + Sync>> = functions
        .par_iter()
        .map(|function| {
            let names = argument_names
                .iter()
                .map(String::as_str)
                .collect::<Vec<_>>();
            Expr::lambdify_borrowed_thread_safe(function, names.as_slice())
        })
        .collect();
    let callback = Box::new(move |_x: f64, values: &DVector<f64>| -> DVector<f64> {
        let started = telemetry.start_timing();
        let parameter_values = binding.snapshot();
        let args = flatten_lambdify_args(parameter_values.as_deref(), values.as_slice());
        assert!(
            parameter_count == 0 || parameter_values.is_some(),
            "parameter values must be provided when parameters are configured"
        );
        telemetry.record_dispatch(execution_policy, compiled_functions.len());
        let result: Vec<f64> = if execution_policy.should_parallel(compiled_functions.len()) {
            compiled_functions
                .par_iter()
                .map(|function| function(args.as_slice()))
                .collect()
        } else {
            compiled_functions
                .iter()
                .map(|function| function(args.as_slice()))
                .collect()
        };
        telemetry.record_residual_sample(started);
        DVector::from_vec(result)
    });
    Box::new(FunEnum::Dense(callback))
}

/// Installs the historical parallel faer sparse Jacobian callback.
pub(crate) fn faer_sparse_jacobian(
    entries: Vec<(usize, usize, Expr)>,
    rows: usize,
    cols: usize,
    argument_names: Vec<String>,
    parameter_values: Option<Vec<f64>>,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Jac> {
    let binding = BvpParameterBindingHandle::new(parameter_values);
    faer_sparse_jacobian_with_binding(
        entries,
        rows,
        cols,
        argument_names,
        binding,
        parameter_count,
        telemetry,
    )
}

/// Sparse ExprLegacy Jacobian callback using a reusable numeric parameter bind.
pub(crate) fn faer_sparse_jacobian_with_binding(
    entries: Vec<(usize, usize, Expr)>,
    rows: usize,
    cols: usize,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Jac> {
    faer_sparse_jacobian_with_binding_and_policy(
        entries,
        rows,
        cols,
        argument_names,
        binding,
        parameter_count,
        telemetry,
        BvpLambdifyExecutionPolicy::default(),
    )
}

/// Sparse ExprLegacy Jacobian callback with an explicit runtime policy.
pub(crate) fn faer_sparse_jacobian_with_binding_and_policy(
    entries: Vec<(usize, usize, Expr)>,
    rows: usize,
    cols: usize,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
    execution_policy: BvpLambdifyExecutionPolicy,
) -> Box<dyn Jac> {
    let positions: Vec<(usize, usize, Box<dyn Fn(&[f64]) -> f64 + Send + Sync>)> = entries
        .into_par_iter()
        .map(|(row, col, derivative)| {
            let names = argument_names
                .iter()
                .map(String::as_str)
                .collect::<Vec<_>>();
            let compiled = Expr::lambdify_borrowed_thread_safe(&derivative, names.as_slice());
            (row, col, compiled)
        })
        .collect();
    let callback = Box::new(
        move |_x: f64, values: &Col<f64>| -> SparseColMat<usize, f64> {
            let started = telemetry.start_timing();
            let dense_values: Vec<f64> = values.iter().copied().collect();
            let parameter_values = binding.snapshot();
            assert!(
                parameter_count == 0 || parameter_values.is_some(),
                "parameter values must be provided when parameters are configured"
            );
            let args = flatten_lambdify_args(parameter_values.as_deref(), dense_values.as_slice());
            telemetry.record_dispatch(execution_policy, positions.len());
            let triplets: Vec<Triplet<usize, usize, f64>> =
                if execution_policy.should_parallel(positions.len()) {
                    positions
                        .par_iter()
                        .filter_map(|(row, col, function)| {
                            let value = function(args.as_slice());
                            (value.abs() > T).then(|| Triplet::new(*row, *col, value))
                        })
                        .collect()
                } else {
                    positions
                        .iter()
                        .filter_map(|(row, col, function)| {
                            let value = function(args.as_slice());
                            (value.abs() > T).then(|| Triplet::new(*row, *col, value))
                        })
                        .collect()
                };
            let matrix = SparseColMat::try_new_from_triplets(rows, cols, triplets.as_slice())
                .expect("legacy sparse Jacobian triplets must have valid coordinates");
            telemetry.record_jacobian_sample(started);
            matrix
        },
    );
    Box::new(JacEnum::Sparse_3(callback))
}

/// Installs the historical parallel faer sparse residual callback.
pub(crate) fn faer_sparse_residual(
    functions: Vec<Expr>,
    argument_names: Vec<String>,
    parameter_values: Option<Vec<f64>>,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Fun> {
    let binding = BvpParameterBindingHandle::new(parameter_values);
    faer_sparse_residual_with_binding(
        functions,
        argument_names,
        binding,
        parameter_count,
        telemetry,
    )
}

/// Sparse ExprLegacy residual callback using a reusable numeric parameter bind.
pub(crate) fn faer_sparse_residual_with_binding(
    functions: Vec<Expr>,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
) -> Box<dyn Fun> {
    faer_sparse_residual_with_binding_and_policy(
        functions,
        argument_names,
        binding,
        parameter_count,
        telemetry,
        BvpLambdifyExecutionPolicy::default(),
    )
}

/// Sparse ExprLegacy residual callback with an explicit runtime policy.
pub(crate) fn faer_sparse_residual_with_binding_and_policy(
    functions: Vec<Expr>,
    argument_names: Vec<String>,
    binding: BvpParameterBindingHandle,
    parameter_count: usize,
    telemetry: LegacyLambdifyTelemetry,
    execution_policy: BvpLambdifyExecutionPolicy,
) -> Box<dyn Fun> {
    let compiled_functions: Vec<Box<dyn Fn(&[f64]) -> f64 + Send + Sync>> = functions
        .par_iter()
        .map(|function| {
            let names = argument_names
                .iter()
                .map(String::as_str)
                .collect::<Vec<_>>();
            Expr::lambdify_borrowed_thread_safe(function, names.as_slice())
        })
        .collect();
    let callback = Box::new(move |_x: f64, values: &Col<f64>| -> Col<f64> {
        let started = telemetry.start_timing();
        let dense_values: Vec<f64> = values.iter().copied().collect();
        let parameter_values = binding.snapshot();
        assert!(
            parameter_count == 0 || parameter_values.is_some(),
            "parameter values must be provided when parameters are configured"
        );
        let args = flatten_lambdify_args(parameter_values.as_deref(), dense_values.as_slice());
        telemetry.record_dispatch(execution_policy, compiled_functions.len());
        let result: Vec<f64> = if execution_policy.should_parallel(compiled_functions.len()) {
            compiled_functions
                .par_iter()
                .map(|function| function(args.as_slice()))
                .collect()
        } else {
            compiled_functions
                .iter()
                .map(|function| function(args.as_slice()))
                .collect()
        };
        telemetry.record_residual_sample(started);
        ColRef::from_slice(result.as_slice()).to_owned()
    });
    Box::new(FunEnum::Sparse_3(callback))
}

#[cfg(test)]
mod tests {
    use super::{
        LegacyLambdifyTelemetry, dense_residual_with_binding, faer_sparse_jacobian_with_binding,
        faer_sparse_residual_with_binding,
    };
    use crate::numerical::BVP_Damp::BVP_traits::{Fun, Jac, MatrixType, VectorType};
    use crate::symbolic::bvp::parameter_binding::BvpParameterBindingHandle;
    use crate::symbolic::symbolic_engine::Expr;
    use faer::col::Col;
    use nalgebra::DVector;
    use std::time::Duration;

    #[test]
    fn legacy_lambdify_telemetry_accumulates_typed_callback_costs() {
        let telemetry = LegacyLambdifyTelemetry::new();
        telemetry.record_residual(Duration::from_nanos(3));
        telemetry.record_residual(Duration::from_nanos(5));
        telemetry.record_jacobian(Duration::from_nanos(7));

        let snapshot = telemetry.snapshot();
        assert_eq!(snapshot.residual_calls, 2);
        assert_eq!(snapshot.residual_elapsed, Duration::from_nanos(8));
        assert_eq!(snapshot.jacobian_calls, 1);
        assert_eq!(snapshot.jacobian_elapsed, Duration::from_nanos(7));
    }

    #[test]
    fn exprlegacy_callback_reuses_compiled_functions_after_numeric_rebind() {
        let binding = BvpParameterBindingHandle::new(Some(vec![1.0]));
        let mut callback = dense_residual_with_binding(
            vec![Expr::parse_expression("p + x")],
            vec!["p".to_string(), "x".to_string()],
            binding.clone(),
            1,
            LegacyLambdifyTelemetry::new(),
        );
        let first = callback
            .try_call(0.0, &DVector::from_vec(vec![2.0]))
            .expect("initial parameter binding should evaluate")
            .to_DVectorType();
        binding.replace(Some(vec![4.0]));
        let second = callback
            .try_call(0.0, &DVector::from_vec(vec![2.0]))
            .expect("rebound parameter should evaluate")
            .to_DVectorType();
        assert_eq!(first[0], 3.0);
        assert_eq!(second[0], 6.0);
    }

    #[test]
    fn exprlegacy_sparse_callbacks_rebind_numeric_parameters() {
        let binding = BvpParameterBindingHandle::new(Some(vec![2.0]));
        let arguments = vec!["p".to_string(), "x".to_string()];
        let mut jacobian = faer_sparse_jacobian_with_binding(
            vec![(0, 0, Expr::parse_expression("p"))],
            1,
            1,
            arguments.clone(),
            binding.clone(),
            1,
            LegacyLambdifyTelemetry::new(),
        );
        let residual = faer_sparse_residual_with_binding(
            vec![Expr::parse_expression("p*x")],
            arguments,
            binding.clone(),
            1,
            LegacyLambdifyTelemetry::new(),
        );
        let x = Col::from_fn(1, |_| 3.0);

        let first_jacobian = jacobian
            .try_call(0.0, &x)
            .expect("sparse Jacobian should evaluate")
            .to_DMatrixType();
        let first_residual = residual
            .try_call(0.0, &x)
            .expect("sparse residual should evaluate")
            .to_DVectorType();
        binding.replace(Some(vec![5.0]));
        let second_jacobian = jacobian
            .try_call(0.0, &x)
            .expect("rebound sparse Jacobian should evaluate")
            .to_DMatrixType();
        let second_residual = residual
            .try_call(0.0, &x)
            .expect("rebound sparse residual should evaluate")
            .to_DVectorType();

        assert_eq!(first_jacobian[(0, 0)], 2.0);
        assert_eq!(first_residual[0], 6.0);
        assert_eq!(second_jacobian[(0, 0)], 5.0);
        assert_eq!(second_residual[0], 15.0);
    }
}