slatec 0.1.0

Safe Rust interface to selected SLATEC numerical routines
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Safe expert `SNLS1` and `DNLS1` nonlinear least-squares drivers.

use alloc::{vec, vec::Vec};

use slatec_core::to_fortran_integer;
use slatec_sys::FortranInteger;

use crate::callback_runtime::{
    self, CallbackRuntimeError, ExpertLeastSquaresCallbackFailure, ExpertLeastSquaresF32Callback,
    ExpertLeastSquaresF64Callback,
};
use crate::nonlinear::JacobianMut;

use super::{LeastSquaresError, LeastSquaresStatus};

/// Variable-scaling policy for expert nonlinear least squares.
///
/// This idiomatic enum replaces the raw `MODE` and `DIAG` arguments of
/// `SNLS1` and `DNLS1`. `Automatic` maps to `MODE=1`; `User` copies the
/// provided positive scaling values into `DIAG` and maps to `MODE=2`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LeastSquaresScaling<'a, T> {
    /// Ask SLATEC to derive positive variable scales from the initial
    /// Jacobian. This maps to Fortran `MODE=1`.
    Automatic,
    /// Use one strictly positive, finite scale per parameter. The wrapper
    /// copies the slice because the native routine may update `DIAG`.
    User(&'a [T]),
}

/// Expert controls for `SNLS1` and `DNLS1`.
///
/// The fields map directly to the documented native controls: `FTOL`, `XTOL`,
/// `GTOL`, `MAXFEV`, `EPSFCN`, `DIAG`/`MODE`, and `FACTOR`. The wrapper fixes
/// `NPRINT=0`, so legacy print/observer callbacks cannot enter Rust. For
/// forward differences, `finite_difference_step=None` maps `EPSFCN=0`, which
/// makes SLATEC use its validated machine precision. A positive value tells
/// SLATEC the estimated relative error in residual evaluations.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ExpertLeastSquaresOptions<'a, T = f64> {
    /// Relative reduction tolerance mapped to Fortran `FTOL`.
    pub function_tolerance: T,
    /// Relative parameter-step tolerance mapped to Fortran `XTOL`.
    pub parameter_tolerance: T,
    /// Residual/Jacobian orthogonality tolerance mapped to Fortran `GTOL`.
    /// Zero is the recommended conservative default.
    pub gradient_tolerance: T,
    /// Optional `MAXFEV` residual-callback budget. `None` selects the native
    /// recommendations: `200*(N+1)` for forward differences and `100*(N+1)`
    /// for a dense user Jacobian.
    pub maximum_function_evaluations: Option<usize>,
    /// Optional non-negative `EPSFCN` finite-difference relative-error
    /// estimate. It is ignored for a user-supplied Jacobian.
    pub finite_difference_step: Option<T>,
    /// Automatic or user-provided positive variable scaling (`MODE`/`DIAG`).
    pub scaling: LeastSquaresScaling<'a, T>,
    /// Positive `FACTOR` initial trust-region bound multiplier. SLATEC
    /// recommends a value in approximately `0.1..=100`; the safe layer accepts
    /// any finite positive value documented by the native contract.
    pub step_bound_factor: T,
}

impl Default for ExpertLeastSquaresOptions<'static, f64> {
    fn default() -> Self {
        Self {
            function_tolerance: 1.0e-10,
            parameter_tolerance: 1.0e-10,
            gradient_tolerance: 0.0,
            maximum_function_evaluations: None,
            finite_difference_step: None,
            scaling: LeastSquaresScaling::Automatic,
            step_bound_factor: 100.0,
        }
    }
}

impl ExpertLeastSquaresOptions<'static, f32> {
    /// Returns practical single-precision controls for `SNLS1`.
    pub const fn single_precision() -> Self {
        Self {
            function_tolerance: 1.0e-5,
            parameter_tolerance: 1.0e-5,
            gradient_tolerance: 0.0,
            maximum_function_evaluations: None,
            finite_difference_step: None,
            scaling: LeastSquaresScaling::Automatic,
            step_bound_factor: 100.0,
        }
    }
}

/// Result of an expert nonlinear least-squares solve.
///
/// `parameters` and `residuals` are the final Fortran `X` and `FVEC` values.
/// The wrapper computes the scaled Euclidean `residual_norm` from `FVEC` and
/// `cost = residual_norm^2 / 2` in Rust. The evaluation counts are the native
/// `NFEV` and `NJEV` outputs, checked against the contained Rust callback
/// invocation counts. In forward-difference mode `jacobian_evaluations` is
/// precisely zero, as documented by `SNLS1`/`DNLS1`.
#[derive(Clone, Debug, PartialEq)]
pub struct ExpertLeastSquaresResult<T = f64> {
    /// Final parameter estimate from Fortran argument `X`, of length `N`.
    pub parameters: Vec<T>,
    /// Final residual vector from Fortran argument `FVEC`, of length `M`.
    pub residuals: Vec<T>,
    /// One half of the recomputed residual sum of squares.
    pub cost: T,
    /// Recomputed Euclidean norm of [`Self::residuals`].
    pub residual_norm: T,
    /// Interpreted native `INFO` numerical completion state.
    pub status: LeastSquaresStatus,
    /// Exact native function-evaluation count `NFEV`.
    pub function_evaluations: usize,
    /// Exact native full-Jacobian evaluation count `NJEV`.
    pub jacobian_evaluations: usize,
}

struct Workspace<T> {
    jacobian: Vec<T>,
    scaling: Vec<T>,
    qtf: Vec<T>,
    wa1: Vec<T>,
    wa2: Vec<T>,
    wa3: Vec<T>,
    wa4: Vec<T>,
    pivots: Vec<FortranInteger>,
}

fn matrix_len(residual_count: usize, parameter_count: usize) -> Result<usize, LeastSquaresError> {
    residual_count
        .checked_mul(parameter_count)
        .ok_or(LeastSquaresError::WorkspaceOverflow)
}

fn native_integer(
    value: usize,
    argument: &'static str,
) -> Result<FortranInteger, LeastSquaresError> {
    to_fortran_integer(value).map_err(|_| LeastSquaresError::IntegerOverflow { argument })
}

fn native_count(value: FortranInteger, detail: &'static str) -> Result<usize, LeastSquaresError> {
    usize::try_from(value).map_err(|_| LeastSquaresError::NativeContractViolation { detail })
}

fn status(value: FortranInteger) -> Result<LeastSquaresStatus, LeastSquaresError> {
    match value {
        1 => Ok(LeastSquaresStatus::ConvergedResidual),
        2 => Ok(LeastSquaresStatus::ConvergedParameters),
        3 => Ok(LeastSquaresStatus::ConvergedResidualAndParameters),
        4 => Ok(LeastSquaresStatus::ConvergedOrthogonality),
        5 => Ok(LeastSquaresStatus::MaximumEvaluations),
        6 => Ok(LeastSquaresStatus::ResidualToleranceTooSmall),
        7 => Ok(LeastSquaresStatus::ParameterToleranceTooSmall),
        8 => Ok(LeastSquaresStatus::GradientToleranceTooSmall),
        value => Err(LeastSquaresError::NativeStatus { status: value }),
    }
}

fn callback_runtime_error(error: CallbackRuntimeError) -> LeastSquaresError {
    match error {
        CallbackRuntimeError::NestedCallback => LeastSquaresError::NestedNativeCallback,
    }
}

fn callback_failure(failure: ExpertLeastSquaresCallbackFailure) -> LeastSquaresError {
    match failure {
        ExpertLeastSquaresCallbackFailure::ResidualPanicked => LeastSquaresError::CallbackPanicked,
        ExpertLeastSquaresCallbackFailure::JacobianPanicked => LeastSquaresError::JacobianPanicked,
        ExpertLeastSquaresCallbackFailure::ResidualNonFinite { index } => {
            LeastSquaresError::CallbackReturnedNonFinite { index }
        }
        ExpertLeastSquaresCallbackFailure::JacobianNonFinite { row, column } => {
            LeastSquaresError::JacobianReturnedNonFinite { row, column }
        }
        ExpertLeastSquaresCallbackFailure::InvalidPointer => {
            LeastSquaresError::NativeContractViolation {
                detail: "expert least-squares callback pointer was null or aliased",
            }
        }
        ExpertLeastSquaresCallbackFailure::DimensionMismatch => {
            LeastSquaresError::NativeContractViolation {
                detail: "expert least-squares callback M or N differed from its registered context",
            }
        }
        ExpertLeastSquaresCallbackFailure::InvalidLeadingDimension => {
            LeastSquaresError::NativeContractViolation {
                detail: "expert least-squares callback LDFJAC was invalid",
            }
        }
        ExpertLeastSquaresCallbackFailure::UnexpectedFlag => {
            LeastSquaresError::NativeContractViolation {
                detail: "expert least-squares callback received an unsupported IFLAG",
            }
        }
    }
}

fn norm_f64(values: &[f64]) -> f64 {
    let mut scale = 0.0_f64;
    let mut sum = 1.0_f64;
    for value in values {
        let magnitude = value.abs();
        if magnitude != 0.0 {
            if scale < magnitude {
                sum = 1.0 + sum * (scale / magnitude).powi(2);
                scale = magnitude;
            } else {
                sum += (magnitude / scale).powi(2);
            }
        }
    }
    if scale == 0.0 {
        0.0
    } else {
        scale * sum.sqrt()
    }
}

fn norm_f32(values: &[f32]) -> f32 {
    let mut scale = 0.0_f32;
    let mut sum = 1.0_f32;
    for value in values {
        let magnitude = value.abs();
        if magnitude != 0.0 {
            if scale < magnitude {
                sum = 1.0 + sum * (scale / magnitude).powi(2);
                scale = magnitude;
            } else {
                sum += (magnitude / scale).powi(2);
            }
        }
    }
    if scale == 0.0 {
        0.0
    } else {
        scale * sum.sqrt()
    }
}

fn default_maximum(parameter_count: usize, analytic: bool) -> Result<usize, LeastSquaresError> {
    parameter_count
        .checked_add(1)
        .and_then(|value| value.checked_mul(if analytic { 100 } else { 200 }))
        .ok_or(LeastSquaresError::WorkspaceOverflow)
}

fn validate_f64(
    initial: &[f64],
    residual_count: usize,
    options: ExpertLeastSquaresOptions<'_, f64>,
) -> Result<(), LeastSquaresError> {
    if initial.is_empty() {
        return Err(LeastSquaresError::EmptyParameters);
    }
    if residual_count == 0 {
        return Err(LeastSquaresError::EmptyResiduals);
    }
    if residual_count < initial.len() {
        return Err(LeastSquaresError::Underdetermined {
            residuals: residual_count,
            parameters: initial.len(),
        });
    }
    if let Some((index, _)) = initial
        .iter()
        .enumerate()
        .find(|(_, value)| !value.is_finite())
    {
        return Err(LeastSquaresError::NonFiniteInitialValue { index });
    }
    if !options.function_tolerance.is_finite() || options.function_tolerance < 0.0 {
        return Err(LeastSquaresError::InvalidFunctionTolerance);
    }
    if !options.parameter_tolerance.is_finite() || options.parameter_tolerance < 0.0 {
        return Err(LeastSquaresError::InvalidParameterTolerance);
    }
    if !options.gradient_tolerance.is_finite() || options.gradient_tolerance < 0.0 {
        return Err(LeastSquaresError::InvalidGradientTolerance);
    }
    if options
        .finite_difference_step
        .is_some_and(|value| !value.is_finite() || value < 0.0)
    {
        return Err(LeastSquaresError::InvalidFiniteDifferenceStep);
    }
    if !options.step_bound_factor.is_finite() || options.step_bound_factor <= 0.0 {
        return Err(LeastSquaresError::InvalidStepBoundFactor);
    }
    if let LeastSquaresScaling::User(values) = options.scaling {
        if values.len() != initial.len() {
            return Err(LeastSquaresError::InvalidScalingLength {
                expected: initial.len(),
                actual: values.len(),
            });
        }
        if let Some((index, _)) = values
            .iter()
            .enumerate()
            .find(|(_, value)| !value.is_finite() || **value <= 0.0)
        {
            return Err(LeastSquaresError::InvalidScalingValue { index });
        }
    }
    Ok(())
}

fn validate_f32(
    initial: &[f32],
    residual_count: usize,
    options: ExpertLeastSquaresOptions<'_, f32>,
) -> Result<(), LeastSquaresError> {
    if initial.is_empty() {
        return Err(LeastSquaresError::EmptyParameters);
    }
    if residual_count == 0 {
        return Err(LeastSquaresError::EmptyResiduals);
    }
    if residual_count < initial.len() {
        return Err(LeastSquaresError::Underdetermined {
            residuals: residual_count,
            parameters: initial.len(),
        });
    }
    if let Some((index, _)) = initial
        .iter()
        .enumerate()
        .find(|(_, value)| !value.is_finite())
    {
        return Err(LeastSquaresError::NonFiniteInitialValue { index });
    }
    if !options.function_tolerance.is_finite() || options.function_tolerance < 0.0 {
        return Err(LeastSquaresError::InvalidFunctionTolerance);
    }
    if !options.parameter_tolerance.is_finite() || options.parameter_tolerance < 0.0 {
        return Err(LeastSquaresError::InvalidParameterTolerance);
    }
    if !options.gradient_tolerance.is_finite() || options.gradient_tolerance < 0.0 {
        return Err(LeastSquaresError::InvalidGradientTolerance);
    }
    if options
        .finite_difference_step
        .is_some_and(|value| !value.is_finite() || value < 0.0)
    {
        return Err(LeastSquaresError::InvalidFiniteDifferenceStep);
    }
    if !options.step_bound_factor.is_finite() || options.step_bound_factor <= 0.0 {
        return Err(LeastSquaresError::InvalidStepBoundFactor);
    }
    if let LeastSquaresScaling::User(values) = options.scaling {
        if values.len() != initial.len() {
            return Err(LeastSquaresError::InvalidScalingLength {
                expected: initial.len(),
                actual: values.len(),
            });
        }
        if let Some((index, _)) = values
            .iter()
            .enumerate()
            .find(|(_, value)| !value.is_finite() || **value <= 0.0)
        {
            return Err(LeastSquaresError::InvalidScalingValue { index });
        }
    }
    Ok(())
}

fn scaling_f64(
    parameter_count: usize,
    scaling: LeastSquaresScaling<'_, f64>,
) -> (Vec<f64>, FortranInteger) {
    match scaling {
        LeastSquaresScaling::Automatic => (vec![0.0; parameter_count], 1),
        LeastSquaresScaling::User(values) => (values.to_vec(), 2),
    }
}

fn scaling_f32(
    parameter_count: usize,
    scaling: LeastSquaresScaling<'_, f32>,
) -> (Vec<f32>, FortranInteger) {
    match scaling {
        LeastSquaresScaling::Automatic => (vec![0.0; parameter_count], 1),
        LeastSquaresScaling::User(values) => (values.to_vec(), 2),
    }
}

#[allow(clippy::too_many_arguments)]
fn run_f64<F, J>(
    initial: &[f64],
    residual_count: usize,
    residual_callback: F,
    jacobian_callback: J,
    options: ExpertLeastSquaresOptions<'_, f64>,
    analytic: bool,
) -> Result<ExpertLeastSquaresResult<f64>, LeastSquaresError>
where
    F: FnMut(&[f64], &mut [f64]),
    J: FnMut(&[f64], &[f64], &mut [f64], usize),
{
    validate_f64(initial, residual_count, options)?;
    let parameter_count = initial.len();
    let maximum = options
        .maximum_function_evaluations
        .unwrap_or(default_maximum(parameter_count, analytic)?);
    if maximum == 0 {
        return Err(LeastSquaresError::InvalidMaximumFunctionEvaluations);
    }
    let matrix_len = matrix_len(residual_count, parameter_count)?;
    let (scaling, mut mode) = scaling_f64(parameter_count, options.scaling);
    let mut workspace = Workspace {
        jacobian: vec![0.0; matrix_len],
        scaling,
        qtf: vec![0.0; parameter_count],
        wa1: vec![0.0; parameter_count],
        wa2: vec![0.0; parameter_count],
        wa3: vec![0.0; parameter_count],
        wa4: vec![0.0; residual_count],
        pivots: vec![0; parameter_count],
    };
    let mut parameters = initial.to_vec();
    let mut residuals = vec![0.0; residual_count];
    let mut iopt = if analytic { 2 } else { 1 };
    let mut m = native_integer(residual_count, "residual count")?;
    let mut n = native_integer(parameter_count, "parameter count")?;
    let mut ldfjac = m;
    let mut ftol = options.function_tolerance;
    let mut xtol = options.parameter_tolerance;
    let mut gtol = options.gradient_tolerance;
    let mut maxfev = native_integer(maximum, "maximum function evaluations")?;
    let mut epsfcn = options.finite_difference_step.unwrap_or(0.0);
    let mut factor = options.step_bound_factor;
    let mut nprint = 0;
    let mut info = 0;
    let mut nfev = 0;
    let mut njev = 0;
    let invocation = callback_runtime::with_expert_least_squares_f64(
        parameter_count,
        residual_count,
        analytic,
        residual_callback,
        jacobian_callback,
        |callback: ExpertLeastSquaresF64Callback| {
            let _error_scope = crate::runtime::permit_recoverable_native_statuses();
            // SAFETY: M, N, LDFJAC=M and all exact native arrays are checked;
            // the scoped callback validates residual/Jacobian pointers and
            // dimensions; NPRINT=0; and the selected GNU MinGW profile owns
            // this reviewed DNLS1 ABI.
            unsafe {
                slatec_sys::least_squares::dnls1(
                    callback.ffi(),
                    &mut iopt,
                    &mut m,
                    &mut n,
                    parameters.as_mut_ptr(),
                    residuals.as_mut_ptr(),
                    workspace.jacobian.as_mut_ptr(),
                    &mut ldfjac,
                    &mut ftol,
                    &mut xtol,
                    &mut gtol,
                    &mut maxfev,
                    &mut epsfcn,
                    workspace.scaling.as_mut_ptr(),
                    &mut mode,
                    &mut factor,
                    &mut nprint,
                    &mut info,
                    &mut nfev,
                    &mut njev,
                    workspace.pivots.as_mut_ptr(),
                    workspace.qtf.as_mut_ptr(),
                    workspace.wa1.as_mut_ptr(),
                    workspace.wa2.as_mut_ptr(),
                    workspace.wa3.as_mut_ptr(),
                    workspace.wa4.as_mut_ptr(),
                );
            }
        },
    )
    .map_err(callback_runtime_error)?;
    if let Some(failure) = invocation.failure {
        return Err(callback_failure(failure));
    }
    let function_evaluations = native_count(nfev, "negative native NFEV")?;
    let jacobian_evaluations = native_count(njev, "negative native NJEV")?;
    if function_evaluations != invocation.residual_evaluations
        || jacobian_evaluations != invocation.jacobian_evaluations
    {
        return Err(LeastSquaresError::NativeContractViolation {
            detail: "native expert evaluation counts disagreed with the scoped callback counts",
        });
    }
    let residual_norm = norm_f64(&residuals);
    Ok(ExpertLeastSquaresResult {
        parameters,
        residuals,
        cost: 0.5 * residual_norm * residual_norm,
        residual_norm,
        status: status(info)?,
        function_evaluations,
        jacobian_evaluations,
    })
}

#[allow(clippy::too_many_arguments)]
fn run_f32<F, J>(
    initial: &[f32],
    residual_count: usize,
    residual_callback: F,
    jacobian_callback: J,
    options: ExpertLeastSquaresOptions<'_, f32>,
    analytic: bool,
) -> Result<ExpertLeastSquaresResult<f32>, LeastSquaresError>
where
    F: FnMut(&[f32], &mut [f32]),
    J: FnMut(&[f32], &[f32], &mut [f32], usize),
{
    validate_f32(initial, residual_count, options)?;
    let parameter_count = initial.len();
    let maximum = options
        .maximum_function_evaluations
        .unwrap_or(default_maximum(parameter_count, analytic)?);
    if maximum == 0 {
        return Err(LeastSquaresError::InvalidMaximumFunctionEvaluations);
    }
    let matrix_len = matrix_len(residual_count, parameter_count)?;
    let (scaling, mut mode) = scaling_f32(parameter_count, options.scaling);
    let mut workspace = Workspace {
        jacobian: vec![0.0; matrix_len],
        scaling,
        qtf: vec![0.0; parameter_count],
        wa1: vec![0.0; parameter_count],
        wa2: vec![0.0; parameter_count],
        wa3: vec![0.0; parameter_count],
        wa4: vec![0.0; residual_count],
        pivots: vec![0; parameter_count],
    };
    let mut parameters = initial.to_vec();
    let mut residuals = vec![0.0; residual_count];
    let mut iopt = if analytic { 2 } else { 1 };
    let mut m = native_integer(residual_count, "residual count")?;
    let mut n = native_integer(parameter_count, "parameter count")?;
    let mut ldfjac = m;
    let mut ftol = options.function_tolerance;
    let mut xtol = options.parameter_tolerance;
    let mut gtol = options.gradient_tolerance;
    let mut maxfev = native_integer(maximum, "maximum function evaluations")?;
    let mut epsfcn = options.finite_difference_step.unwrap_or(0.0);
    let mut factor = options.step_bound_factor;
    let mut nprint = 0;
    let mut info = 0;
    let mut nfev = 0;
    let mut njev = 0;
    let invocation = callback_runtime::with_expert_least_squares_f32(
        parameter_count,
        residual_count,
        analytic,
        residual_callback,
        jacobian_callback,
        |callback: ExpertLeastSquaresF32Callback| {
            let _error_scope = crate::runtime::permit_recoverable_native_statuses();
            // SAFETY: f32 equivalent of the fully checked DNLS1 call above,
            // using the reviewed SNLS1 ABI and exact M-by-N FJAC allocation.
            unsafe {
                slatec_sys::least_squares::snls1(
                    callback.ffi(),
                    &mut iopt,
                    &mut m,
                    &mut n,
                    parameters.as_mut_ptr(),
                    residuals.as_mut_ptr(),
                    workspace.jacobian.as_mut_ptr(),
                    &mut ldfjac,
                    &mut ftol,
                    &mut xtol,
                    &mut gtol,
                    &mut maxfev,
                    &mut epsfcn,
                    workspace.scaling.as_mut_ptr(),
                    &mut mode,
                    &mut factor,
                    &mut nprint,
                    &mut info,
                    &mut nfev,
                    &mut njev,
                    workspace.pivots.as_mut_ptr(),
                    workspace.qtf.as_mut_ptr(),
                    workspace.wa1.as_mut_ptr(),
                    workspace.wa2.as_mut_ptr(),
                    workspace.wa3.as_mut_ptr(),
                    workspace.wa4.as_mut_ptr(),
                );
            }
        },
    )
    .map_err(callback_runtime_error)?;
    if let Some(failure) = invocation.failure {
        return Err(callback_failure(failure));
    }
    let function_evaluations = native_count(nfev, "negative native NFEV")?;
    let jacobian_evaluations = native_count(njev, "negative native NJEV")?;
    if function_evaluations != invocation.residual_evaluations
        || jacobian_evaluations != invocation.jacobian_evaluations
    {
        return Err(LeastSquaresError::NativeContractViolation {
            detail: "native expert evaluation counts disagreed with the scoped callback counts",
        });
    }
    let residual_norm = norm_f32(&residuals);
    Ok(ExpertLeastSquaresResult {
        parameters,
        residuals,
        cost: 0.5 * residual_norm * residual_norm,
        residual_norm,
        status: status(info)?,
        function_evaluations,
        jacobian_evaluations,
    })
}

/// Minimizes a double-precision residual sum of squares with `DNLS1` and a
/// native forward-difference Jacobian.
///
/// This solves `minimize 1/2 Σ r_i(x)^2` for `M = residual_count` and
/// `N = initial.len()` with `M >= N`. It wraps original SLATEC `DNLS1`, unlike
/// [`super::least_squares`] (`DNLS1E`), and exposes independent `FTOL`, `XTOL`,
/// `GTOL`, `MAXFEV`, `EPSFCN`, scaling, and `FACTOR` controls through
/// [`ExpertLeastSquaresOptions`]. The residual closure receives immutable
/// parameters and exactly `M` mutable residuals. The wrapper selects `IOPT=1`
/// and `NPRINT=0`, allocates `FJAC[M*N]`, `IPVT[N]`, `QTF[N]`, `DIAG[N]`, three
/// `N` work vectors, and one `M` work vector internally.
///
/// Calls serialize the process-global GNU Fortran runtime. Callback panics and
/// non-finite residuals are contained; nested callback-based SLATEC calls are
/// rejected. A scoped `XGETF`/`XSETF` guard permits documented level-one
/// numerical statuses to return as [`LeastSquaresStatus`] and restores the
/// caller's legacy-error setting before returning.
///
/// # Errors
///
/// Returns [`LeastSquaresError`] for invalid dimensions/options, callback
/// failure, checked allocation/integer overflow, nesting, or an ABI-contract
/// violation. Numerical completion remains in the returned `status`.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), slatec::least_squares::LeastSquaresError> {
/// use slatec::least_squares::{ExpertLeastSquaresOptions, least_squares_expert};
/// let fit = least_squares_expert(&[0.0, 0.0], 3, |p, r| {
///     r.copy_from_slice(&[p[0] - 1.0, p[0] + p[1] - 3.0, p[0] + 2.0 * p[1] - 5.0]);
/// }, ExpertLeastSquaresOptions::default())?;
/// assert!((fit.parameters[1] - 2.0).abs() < 1.0e-8);
/// # Ok(()) }
/// ```
pub fn least_squares_expert<F>(
    initial: &[f64],
    residual_count: usize,
    residuals: F,
    options: ExpertLeastSquaresOptions<'_, f64>,
) -> Result<ExpertLeastSquaresResult<f64>, LeastSquaresError>
where
    F: FnMut(&[f64], &mut [f64]),
{
    run_f64(
        initial,
        residual_count,
        residuals,
        |_, _, _, _| {},
        options,
        false,
    )
}

/// Single-precision `SNLS1` forward-difference counterpart of
/// [`least_squares_expert`].
///
/// The objective, `M >= N` contract, expert controls, checked workspaces,
/// serialization, callback containment, and status semantics match the f64
/// API. Use [`ExpertLeastSquaresOptions::single_precision`] as a practical
/// initial control set and allow for f32 rounding in assertions.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), slatec::least_squares::LeastSquaresError> {
/// use slatec::least_squares::{ExpertLeastSquaresOptions, least_squares_expert_f32};
/// let fit = least_squares_expert_f32(&[0.0, 0.0], 3, |p, r| {
///     r.copy_from_slice(&[p[0] - 1.0, p[0] + p[1] - 3.0, p[0] + 2.0 * p[1] - 5.0]);
/// }, ExpertLeastSquaresOptions::single_precision())?;
/// assert!((fit.parameters[0] - 1.0).abs() < 2.0e-3);
/// # Ok(()) }
/// ```
pub fn least_squares_expert_f32<F>(
    initial: &[f32],
    residual_count: usize,
    residuals: F,
    options: ExpertLeastSquaresOptions<'_, f32>,
) -> Result<ExpertLeastSquaresResult<f32>, LeastSquaresError>
where
    F: FnMut(&[f32], &mut [f32]),
{
    run_f32(
        initial,
        residual_count,
        residuals,
        |_, _, _, _| {},
        options,
        false,
    )
}

/// Minimizes double-precision residuals with `DNLS1` and a dense analytic
/// Jacobian.
///
/// The residual callback receives `N` parameters and writes `M` residuals.
/// The Jacobian callback receives the same parameters, the valid native
/// residual vector, and a checked rectangular column-major
/// [`JacobianMut`] with `M` rows, `N` columns, and `LDFJAC=M`. It must write
/// each logical entry `∂r_row/∂x_column`; padding is not exposed. The wrapper
/// selects `IOPT=2`, ignores `EPSFCN`, fixes `NPRINT=0`, and otherwise maps
/// [`ExpertLeastSquaresOptions`] directly to the documented expert controls.
///
/// # Errors
///
/// In addition to [`least_squares_expert`] errors, a Jacobian panic, an
/// unwritten entry, or a NaN/infinite derivative produces a contained
/// [`LeastSquaresError::JacobianPanicked`] or
/// [`LeastSquaresError::JacobianReturnedNonFinite`].
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), slatec::least_squares::LeastSquaresError> {
/// use slatec::least_squares::{ExpertLeastSquaresOptions, least_squares_with_jacobian};
/// let fit = least_squares_with_jacobian(
///     &[0.0, 0.0], 3,
///     |p, r| r.copy_from_slice(&[p[0] - 1.0, p[0] + p[1] - 3.0, p[0] + 2.0*p[1] - 5.0]),
///     |_, _, mut j| { j.set(0, 0, 1.0).unwrap(); j.set(0, 1, 0.0).unwrap();
///                      j.set(1, 0, 1.0).unwrap(); j.set(1, 1, 1.0).unwrap();
///                      j.set(2, 0, 1.0).unwrap(); j.set(2, 1, 2.0).unwrap(); },
///     ExpertLeastSquaresOptions::default(),
/// )?;
/// assert!(fit.cost < 1.0e-12);
/// # Ok(()) }
/// ```
pub fn least_squares_with_jacobian<F, J>(
    initial: &[f64],
    residual_count: usize,
    residuals: F,
    mut jacobian: J,
    options: ExpertLeastSquaresOptions<'_, f64>,
) -> Result<ExpertLeastSquaresResult<f64>, LeastSquaresError>
where
    F: FnMut(&[f64], &mut [f64]),
    J: FnMut(&[f64], &[f64], JacobianMut<'_, f64>),
{
    let parameter_count = initial.len();
    run_f64(
        initial,
        residual_count,
        residuals,
        move |x, f, matrix, leading_dimension| {
            if let Some(view) =
                JacobianMut::new(matrix, residual_count, parameter_count, leading_dimension)
            {
                jacobian(x, f, view);
            }
        },
        options,
        true,
    )
}

/// Single-precision `SNLS1` dense-analytic-Jacobian counterpart of
/// [`least_squares_with_jacobian`].
///
/// The checked `M × N` column-major Jacobian layout, expert controls,
/// allocation, serialization, callback containment, and error behavior match
/// the f64 API.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), slatec::least_squares::LeastSquaresError> {
/// use slatec::least_squares::{ExpertLeastSquaresOptions, least_squares_with_jacobian_f32};
/// let fit = least_squares_with_jacobian_f32(
///     &[0.0, 0.0], 3,
///     |p, r| r.copy_from_slice(&[p[0] - 1.0, p[0] + p[1] - 3.0, p[0] + 2.0*p[1] - 5.0]),
///     |_, _, mut j| { j.set(0, 0, 1.0).unwrap(); j.set(0, 1, 0.0).unwrap();
///                      j.set(1, 0, 1.0).unwrap(); j.set(1, 1, 1.0).unwrap();
///                      j.set(2, 0, 1.0).unwrap(); j.set(2, 1, 2.0).unwrap(); },
///     ExpertLeastSquaresOptions::single_precision(),
/// )?;
/// assert!(fit.cost < 1.0e-4);
/// # Ok(()) }
/// ```
pub fn least_squares_with_jacobian_f32<F, J>(
    initial: &[f32],
    residual_count: usize,
    residuals: F,
    mut jacobian: J,
    options: ExpertLeastSquaresOptions<'_, f32>,
) -> Result<ExpertLeastSquaresResult<f32>, LeastSquaresError>
where
    F: FnMut(&[f32], &mut [f32]),
    J: FnMut(&[f32], &[f32], JacobianMut<'_, f32>),
{
    let parameter_count = initial.len();
    run_f32(
        initial,
        residual_count,
        residuals,
        move |x, f, matrix, leading_dimension| {
            if let Some(view) =
                JacobianMut::new(matrix, residual_count, parameter_count, leading_dimension)
            {
                jacobian(x, f, view);
            }
        },
        options,
        true,
    )
}

#[cfg(test)]
mod tests {
    use super::{ExpertLeastSquaresOptions, LeastSquaresScaling, default_maximum, matrix_len};
    use crate::least_squares::LeastSquaresError;

    #[test]
    fn rectangular_workspace_and_defaults_are_checked() {
        assert_eq!(matrix_len(4, 2), Ok(8));
        assert_eq!(default_maximum(2, false), Ok(600));
        assert_eq!(default_maximum(2, true), Ok(300));
        assert!(matches!(
            matrix_len(usize::MAX, 2),
            Err(LeastSquaresError::WorkspaceOverflow)
        ));
        let scaling = [1.0, 2.0];
        let options = ExpertLeastSquaresOptions {
            scaling: LeastSquaresScaling::User(&scaling),
            ..ExpertLeastSquaresOptions::default()
        };
        assert_eq!(options.scaling, LeastSquaresScaling::User(&scaling));
    }
}