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
use alloc::vec;

use crate::callback_runtime::{
    self, CallbackRuntimeError, VectorCallbackFailure, VectorF32Callback, VectorF64Callback,
};
use slatec_core::to_fortran_integer;
use slatec_sys::FortranInteger;

use super::{NonlinearError, NonlinearOptions, NonlinearResult, NonlinearStatus};

fn workspace_len(dimension: usize) -> Result<usize, NonlinearError> {
    let square = dimension
        .checked_mul(dimension)
        .ok_or(NonlinearError::WorkspaceOverflow)?;
    let quadratic = square
        .checked_mul(3)
        .ok_or(NonlinearError::WorkspaceOverflow)?;
    let linear = dimension
        .checked_mul(13)
        .ok_or(NonlinearError::WorkspaceOverflow)?;
    quadratic
        .checked_add(linear)
        .ok_or(NonlinearError::WorkspaceOverflow)
        .map(|numerator| numerator / 2)
}

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

fn validate_f64(initial: &[f64], options: NonlinearOptions<f64>) -> Result<(), NonlinearError> {
    if initial.is_empty() {
        return Err(NonlinearError::EmptySystem);
    }
    if let Some((index, _)) = initial
        .iter()
        .enumerate()
        .find(|(_, value)| !value.is_finite())
    {
        return Err(NonlinearError::NonFiniteInitialValue { index });
    }
    if !options.tolerance.is_finite() || options.tolerance < 0.0 {
        return Err(NonlinearError::InvalidTolerance);
    }
    Ok(())
}

fn validate_f32(initial: &[f32], options: NonlinearOptions<f32>) -> Result<(), NonlinearError> {
    if initial.is_empty() {
        return Err(NonlinearError::EmptySystem);
    }
    if let Some((index, _)) = initial
        .iter()
        .enumerate()
        .find(|(_, value)| !value.is_finite())
    {
        return Err(NonlinearError::NonFiniteInitialValue { index });
    }
    if !options.tolerance.is_finite() || options.tolerance < 0.0 {
        return Err(NonlinearError::InvalidTolerance);
    }
    Ok(())
}

fn callback_failure(failure: VectorCallbackFailure) -> NonlinearError {
    match failure {
        VectorCallbackFailure::Panicked => NonlinearError::CallbackPanicked,
        VectorCallbackFailure::NonFinite { index } => {
            NonlinearError::CallbackReturnedNonFinite { index }
        }
        VectorCallbackFailure::Cancelled => NonlinearError::NativeContractViolation {
            detail: "unexpected internal nonlinear callback cancellation",
        },
        VectorCallbackFailure::InvalidPointer => NonlinearError::NativeContractViolation {
            detail: "native callback pointer was null or overlapped",
        },
        VectorCallbackFailure::DimensionMismatch => NonlinearError::NativeContractViolation {
            detail: "native callback dimension did not match the registered system",
        },
    }
}

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

fn native_status(status: FortranInteger) -> Result<NonlinearStatus, NonlinearError> {
    match status {
        1 => Ok(NonlinearStatus::Converged),
        2 => Ok(NonlinearStatus::MaximumFunctionEvaluations),
        3 => Ok(NonlinearStatus::ToleranceTooSmall),
        4 => Ok(NonlinearStatus::SlowProgress),
        value => Err(NonlinearError::NativeStatus { status: value }),
    }
}

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) * (scale / magnitude);
                scale = magnitude;
            } else {
                sum += (magnitude / scale) * (magnitude / scale);
            }
        }
    }
    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) * (scale / magnitude);
                scale = magnitude;
            } else {
                sum += (magnitude / scale) * (magnitude / scale);
            }
        }
    }
    if scale == 0.0 {
        0.0
    } else {
        scale * sum.sqrt()
    }
}

unsafe extern "C" fn unused_jacobian_f64(
    _n: *const FortranInteger,
    _x: *const f64,
    _fvec: *const f64,
    _fjac: *mut f64,
    _ldfjac: *const FortranInteger,
    iflag: *mut FortranInteger,
) {
    if let Some(iflag) = unsafe { iflag.as_mut() } {
        *iflag = -1;
    }
}

unsafe extern "C" fn unused_jacobian_f32(
    _n: *const FortranInteger,
    _x: *const f32,
    _fvec: *const f32,
    _fjac: *mut f32,
    _ldfjac: *const FortranInteger,
    iflag: *mut FortranInteger,
) {
    if let Some(iflag) = unsafe { iflag.as_mut() } {
        *iflag = -1;
    }
}

fn run_f64<F>(
    initial: &[f64],
    mut function: F,
    options: NonlinearOptions<f64>,
) -> Result<NonlinearResult<f64>, NonlinearError>
where
    F: FnMut(&[f64], &mut [f64]),
{
    validate_f64(initial, options)?;
    let dimension = initial.len();
    let mut x = initial.to_vec();
    let mut residual = vec![0.0; dimension];
    let workspace_length = workspace_len(dimension)?;
    let mut workspace = vec![0.0; workspace_length];
    let mut n = native_integer(dimension, "dimension")?;
    let mut lwa = native_integer(workspace_length, "workspace length")?;
    let mut iopt = 2;
    let mut tolerance = options.tolerance;
    let mut nprint = 0;
    let mut info = 0;

    let invocation = callback_runtime::with_vector_f64(
        dimension,
        move |x, residual| {
            function(x, residual);
            true
        },
        |callback: VectorF64Callback| {
            // SAFETY: IOPT is fixed to the reviewed finite-difference mode,
            // all scalar and workspace pointers are valid for the documented
            // `(3*n*n + 13*n)/2` elements, the callback is lexically scoped,
            // and the validated GNU MinGW native profile is selected.
            unsafe {
                slatec_sys::nonlinear::dnsqe(
                    callback.ffi(),
                    unused_jacobian_f64,
                    &mut iopt,
                    &mut n,
                    x.as_mut_ptr(),
                    residual.as_mut_ptr(),
                    &mut tolerance,
                    &mut nprint,
                    &mut info,
                    workspace.as_mut_ptr(),
                    &mut lwa,
                );
            }
        },
    )
    .map_err(callback_runtime_error)?;
    if let Some(failure) = invocation.failure {
        return Err(callback_failure(failure));
    }
    Ok(NonlinearResult {
        solution: x,
        residual: residual.clone(),
        residual_norm: norm_f64(&residual),
        function_evaluations: invocation.evaluations,
        status: native_status(info)?,
    })
}

fn run_f32<F>(
    initial: &[f32],
    mut function: F,
    options: NonlinearOptions<f32>,
) -> Result<NonlinearResult<f32>, NonlinearError>
where
    F: FnMut(&[f32], &mut [f32]),
{
    validate_f32(initial, options)?;
    let dimension = initial.len();
    let mut x = initial.to_vec();
    let mut residual = vec![0.0; dimension];
    let workspace_length = workspace_len(dimension)?;
    let mut workspace = vec![0.0; workspace_length];
    let mut n = native_integer(dimension, "dimension")?;
    let mut lwa = native_integer(workspace_length, "workspace length")?;
    let mut iopt = 2;
    let mut tolerance = options.tolerance;
    let mut nprint = 0;
    let mut info = 0;

    let invocation = callback_runtime::with_vector_f32(
        dimension,
        move |x, residual| {
            function(x, residual);
            true
        },
        |callback: VectorF32Callback| {
            // SAFETY: see run_f64; this uses the reviewed SNSQE single-
            // precision ABI with the same finite-difference workspace rule.
            unsafe {
                slatec_sys::nonlinear::snsqe(
                    callback.ffi(),
                    unused_jacobian_f32,
                    &mut iopt,
                    &mut n,
                    x.as_mut_ptr(),
                    residual.as_mut_ptr(),
                    &mut tolerance,
                    &mut nprint,
                    &mut info,
                    workspace.as_mut_ptr(),
                    &mut lwa,
                );
            }
        },
    )
    .map_err(callback_runtime_error)?;
    if let Some(failure) = invocation.failure {
        return Err(callback_failure(failure));
    }
    Ok(NonlinearResult {
        solution: x,
        residual: residual.clone(),
        residual_norm: norm_f32(&residual),
        function_evaluations: invocation.evaluations,
        status: native_status(info)?,
    })
}

/// Solves a square nonlinear system with the finite-difference `DNSQE` driver.
///
/// Wraps the double-precision SLATEC routine `DNSQE` from the nonlinear-system
/// family. It seeks an `x` for which the callback writes `f(x) = 0`; `initial`
/// corresponds to Fortran `X`, the callback corresponds to `FCN`, and
/// `options.tolerance` corresponds to `TOL`. The wrapper fixes `IOPT = 2`, so
/// SLATEC estimates the Jacobian by finite differences and never calls a user
/// Jacobian. It allocates hidden `WA` and `LWA` according to
/// `(3*n*n + 13*n) / 2` and fixes `NPRINT = 0`.
///
/// `initial` must be a nonempty finite vector. The callback receives the
/// current iterate and a residual slice with exactly the same length; it must
/// overwrite every residual component with finite values. The historical
/// negative-`IFLAG` cancellation path reaches a fatal SLATEC error exit on the
/// validated profile, so cancellation is deliberately not exposed.
/// Calls allocate, serialize access to the selected process-global Fortran
/// runtime, and reject nested callback-based SLATEC calls. This wrapper is
/// available only with `std`, `alloc`, `nonlinear-easy`, and the validated GNU
/// MinGW x86_64 native profile.
///
/// The result retains the native final `X`, native final `FVEC`, a
/// Rust-recomputed Euclidean residual norm, counted callback calls, and the
/// precise reviewed native completion status. `MaximumFunctionEvaluations`,
/// `ToleranceTooSmall`, and `SlowProgress` are returned as statuses so callers
/// can inspect the final iterate; callback panic, cancellation, non-finite
/// residuals, and invalid Rust inputs return [`NonlinearError`].
///
/// # Example
///
/// ```no_run
/// # fn example() -> Result<(), slatec::nonlinear::NonlinearError> {
/// use slatec::nonlinear::{NonlinearOptions, solve_system};
///
/// let result = solve_system(&[1.0], |x, residual| {
///     residual[0] = x[0] * x[0] - 2.0;
/// }, NonlinearOptions::default())?;
/// assert!((result.solution[0] - 2.0_f64.sqrt()).abs() < 1.0e-8);
/// # Ok::<(), slatec::nonlinear::NonlinearError>(())
/// # }
/// ```
pub fn solve_system<F>(
    initial: &[f64],
    function: F,
    options: NonlinearOptions<f64>,
) -> Result<NonlinearResult<f64>, NonlinearError>
where
    F: FnMut(&[f64], &mut [f64]),
{
    run_f64(initial, function, options)
}

/// Solves a square nonlinear system with the finite-difference `SNSQE` driver.
///
/// Wraps the single-precision SLATEC routine `SNSQE`. Its argument mapping,
/// callback containment, workspace allocation, runtime serialization, and
/// error behavior are the same as [`solve_system`], but all numerical values
/// use `f32`. Use [`NonlinearOptions::single_precision`] for a practical
/// initial tolerance.
///
/// # Example
///
/// ```no_run
/// # fn example() -> Result<(), slatec::nonlinear::NonlinearError> {
/// use slatec::nonlinear::{NonlinearOptions, solve_system_f32};
///
/// let result = solve_system_f32(&[1.0_f32], |x, residual| {
///     residual[0] = x[0] * x[0] - 2.0;
/// }, NonlinearOptions::single_precision())?;
/// assert!((result.solution[0] - 2.0_f32.sqrt()).abs() < 2.0e-4);
/// # Ok::<(), slatec::nonlinear::NonlinearError>(())
/// # }
/// ```
pub fn solve_system_f32<F>(
    initial: &[f32],
    function: F,
    options: NonlinearOptions<f32>,
) -> Result<NonlinearResult<f32>, NonlinearError>
where
    F: FnMut(&[f32], &mut [f32]),
{
    run_f32(initial, function, options)
}

#[cfg(test)]
mod tests {
    use super::{NonlinearError, NonlinearOptions, native_status, validate_f64, workspace_len};
    use crate::nonlinear::NonlinearStatus;

    #[test]
    fn workspace_formula_is_checked_and_exact() {
        assert_eq!(workspace_len(1), Ok(8));
        assert_eq!(workspace_len(2), Ok(19));
        assert!(matches!(
            workspace_len(usize::MAX),
            Err(NonlinearError::WorkspaceOverflow)
        ));
    }

    #[test]
    fn validation_and_status_mapping_are_deterministic() {
        assert_eq!(
            validate_f64(&[], NonlinearOptions::default()),
            Err(NonlinearError::EmptySystem)
        );
        assert_eq!(
            validate_f64(&[f64::NAN], NonlinearOptions::default()),
            Err(NonlinearError::NonFiniteInitialValue { index: 0 })
        );
        assert_eq!(
            validate_f64(&[1.0], NonlinearOptions { tolerance: -1.0 }),
            Err(NonlinearError::InvalidTolerance)
        );
        assert_eq!(native_status(1), Ok(NonlinearStatus::Converged));
        assert_eq!(native_status(4), Ok(NonlinearStatus::SlowProgress));
        assert!(matches!(
            native_status(0),
            Err(NonlinearError::NativeStatus { .. })
        ));
    }
}