numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Computation and numerical errors - instability, convergence, overflow

use super::context::{ErrorSeverity, OperationContext};
use thiserror::Error;

/// Computation and numerical errors
#[derive(Error, Debug, Clone)]
pub enum ComputationError {
    /// Numerical instability detected
    #[error("Numerical instability in {operation}: {details}")]
    NumericalInstability {
        operation: String,
        details: String,
        condition_number: Option<f64>,
        context: OperationContext,
    },

    /// Algorithm failed to converge
    #[error("Convergence failure in {algorithm}: {reason} after {iterations} iterations")]
    ConvergenceFailure {
        algorithm: String,
        reason: String,
        iterations: usize,
        tolerance: Option<f64>,
        residual: Option<f64>,
        context: OperationContext,
    },

    /// Numerical overflow
    #[error("Numerical overflow in {operation}: {details}")]
    Overflow {
        operation: String,
        details: String,
        value: Option<String>,
        context: OperationContext,
    },

    /// Numerical underflow
    #[error("Numerical underflow in {operation}: {details}")]
    Underflow {
        operation: String,
        details: String,
        value: Option<String>,
        context: OperationContext,
    },

    /// Invalid mathematical operation
    #[error("Invalid mathematical operation: {operation} - {reason}")]
    InvalidMathOperation {
        operation: String,
        reason: String,
        suggested_alternative: Option<String>,
        context: OperationContext,
    },

    /// Singular matrix or non-invertible operation
    #[error("Singular matrix in {operation}: matrix is not invertible")]
    SingularMatrix {
        operation: String,
        condition_number: Option<f64>,
        rank: Option<usize>,
        expected_rank: Option<usize>,
        context: OperationContext,
    },

    /// BLAS/LAPACK library error
    #[error("BLAS/LAPACK error in {operation}: {library} returned code {error_code}")]
    LinearAlgebraLibraryError {
        operation: String,
        library: String, // "BLAS", "LAPACK", etc.
        error_code: i32,
        details: Option<String>,
        context: OperationContext,
    },

    /// Eigenvalue/eigenvector computation error
    #[error("Eigenvalue computation failed in {operation}: {reason}")]
    EigenvalueError {
        operation: String,
        reason: String,
        matrix_properties: Option<String>, // "symmetric", "hermitian", etc.
        context: OperationContext,
    },

    /// SVD computation error
    #[error("SVD computation failed: {reason}")]
    SVDError {
        reason: String,
        matrix_shape: Vec<usize>,
        requested_components: Option<usize>,
        context: OperationContext,
    },

    /// FFT computation error
    #[error("FFT computation failed: {reason}")]
    FFTError {
        reason: String,
        signal_length: usize,
        transform_type: String, // "DFT", "RFFT", "DCT", etc.
        context: OperationContext,
    },

    /// Optimization algorithm error
    #[error("Optimization failed in {algorithm}: {reason}")]
    OptimizationError {
        algorithm: String,
        reason: String,
        objective_value: Option<f64>,
        gradient_norm: Option<f64>,
        context: OperationContext,
    },

    /// Integration/ODE solver error
    #[error("Integration failed: {reason}")]
    IntegrationError {
        reason: String,
        method: String,
        step_size: Option<f64>,
        error_estimate: Option<f64>,
        context: OperationContext,
    },

    /// Interpolation error
    #[error("Interpolation failed: {reason}")]
    InterpolationError {
        reason: String,
        method: String,
        data_points: usize,
        context: OperationContext,
    },

    /// Random number generation error
    #[error("Random number generation failed: {reason}")]
    RandomGenerationError {
        reason: String,
        distribution: Option<String>,
        parameters: Option<String>,
        context: OperationContext,
    },
}

impl ComputationError {
    /// Get the severity level of this error
    pub fn severity(&self) -> ErrorSeverity {
        match self {
            ComputationError::NumericalInstability {
                condition_number, ..
            } => {
                // High condition number indicates severe instability
                if let Some(cond) = condition_number {
                    if *cond > 1e12 {
                        ErrorSeverity::High
                    } else {
                        ErrorSeverity::Medium
                    }
                } else {
                    ErrorSeverity::Medium
                }
            }
            ComputationError::ConvergenceFailure { .. } => ErrorSeverity::Medium,
            ComputationError::Overflow { .. } => ErrorSeverity::High,
            ComputationError::Underflow { .. } => ErrorSeverity::Medium,
            ComputationError::InvalidMathOperation { .. } => ErrorSeverity::High,
            ComputationError::SingularMatrix { .. } => ErrorSeverity::High,
            ComputationError::LinearAlgebraLibraryError { error_code, .. } => {
                // Critical LAPACK errors are typically > 0
                if *error_code > 0 {
                    ErrorSeverity::High
                } else {
                    ErrorSeverity::Medium
                }
            }
            ComputationError::EigenvalueError { .. } => ErrorSeverity::Medium,
            ComputationError::SVDError { .. } => ErrorSeverity::Medium,
            ComputationError::FFTError { .. } => ErrorSeverity::Medium,
            ComputationError::OptimizationError { .. } => ErrorSeverity::Medium,
            ComputationError::IntegrationError { .. } => ErrorSeverity::Medium,
            ComputationError::InterpolationError { .. } => ErrorSeverity::Medium,
            ComputationError::RandomGenerationError { .. } => ErrorSeverity::Low,
        }
    }

    /// Get the operation context
    pub fn context(&self) -> &OperationContext {
        match self {
            ComputationError::NumericalInstability { context, .. } => context,
            ComputationError::ConvergenceFailure { context, .. } => context,
            ComputationError::Overflow { context, .. } => context,
            ComputationError::Underflow { context, .. } => context,
            ComputationError::InvalidMathOperation { context, .. } => context,
            ComputationError::SingularMatrix { context, .. } => context,
            ComputationError::LinearAlgebraLibraryError { context, .. } => context,
            ComputationError::EigenvalueError { context, .. } => context,
            ComputationError::SVDError { context, .. } => context,
            ComputationError::FFTError { context, .. } => context,
            ComputationError::OptimizationError { context, .. } => context,
            ComputationError::IntegrationError { context, .. } => context,
            ComputationError::InterpolationError { context, .. } => context,
            ComputationError::RandomGenerationError { context, .. } => context,
        }
    }

    /// Check if this error might be recoverable with different parameters
    pub fn is_potentially_recoverable(&self) -> bool {
        matches!(
            self,
            ComputationError::ConvergenceFailure { .. }
                | ComputationError::NumericalInstability { .. }
                | ComputationError::OptimizationError { .. }
                | ComputationError::IntegrationError { .. }
        )
    }

    /// Get suggested recovery actions
    pub fn recovery_suggestions(&self) -> Vec<String> {
        match self {
            ComputationError::NumericalInstability {
                condition_number, ..
            } => {
                let mut suggestions = vec![
                    "Use higher precision arithmetic (f64 instead of f32)".to_string(),
                    "Consider iterative refinement techniques".to_string(),
                    "Check input data for extreme values".to_string(),
                ];
                if let Some(cond) = condition_number {
                    if *cond > 1e12 {
                        suggestions.push(
                            "Matrix is severely ill-conditioned, consider regularization"
                                .to_string(),
                        );
                    }
                }
                suggestions
            }
            ComputationError::SingularMatrix { .. } => {
                vec![
                    "Use pseudo-inverse (pinv) instead of regular inverse".to_string(),
                    "Add regularization to make matrix invertible".to_string(),
                    "Check for linearly dependent rows/columns".to_string(),
                    "Use SVD-based solution methods".to_string(),
                ]
            }
            ComputationError::ConvergenceFailure {
                algorithm,
                iterations,
                tolerance,
                ..
            } => {
                vec![
                    format!("Increase iteration limit (current: {})", iterations),
                    if let Some(tol) = tolerance {
                        format!("Relax tolerance (current: {:.2e})", tol)
                    } else {
                        "Adjust convergence tolerance".to_string()
                    },
                    format!("Try different algorithm instead of {}", algorithm),
                    "Improve initial guess or starting conditions".to_string(),
                ]
            }
            ComputationError::Overflow { .. } => {
                vec![
                    "Scale input data to prevent overflow".to_string(),
                    "Use logarithmic representation for large numbers".to_string(),
                    "Check for infinite or very large intermediate values".to_string(),
                ]
            }
            ComputationError::LinearAlgebraLibraryError {
                error_code,
                library,
                ..
            } => {
                vec![
                    format!(
                        "Check {} documentation for error code {}",
                        library, error_code
                    ),
                    "Verify input matrix properties (dimensions, data type)".to_string(),
                    "Ensure matrix storage format is correct".to_string(),
                ]
            }
            ComputationError::FFTError { signal_length, .. } => {
                vec![
                    "Ensure signal length is supported by FFT implementation".to_string(),
                    format!(
                        "Try padding signal to power of 2 (current length: {})",
                        signal_length
                    ),
                    "Check for NaN or infinite values in input signal".to_string(),
                ]
            }
            _ => vec!["Check algorithm parameters and input data validity".to_string()],
        }
    }
}

/// Convenience constructors for common computation errors
impl ComputationError {
    /// Create a numerical instability error
    pub fn numerical_instability(operation: &str, details: &str) -> Self {
        ComputationError::NumericalInstability {
            operation: operation.to_string(),
            details: details.to_string(),
            condition_number: None,
            context: OperationContext::new(operation),
        }
    }

    /// Create a convergence failure error
    pub fn convergence_failure(algorithm: &str, reason: &str, iterations: usize) -> Self {
        ComputationError::ConvergenceFailure {
            algorithm: algorithm.to_string(),
            reason: reason.to_string(),
            iterations,
            tolerance: None,
            residual: None,
            context: OperationContext::new(algorithm),
        }
    }

    /// Create a singular matrix error
    pub fn singular_matrix(operation: &str) -> Self {
        ComputationError::SingularMatrix {
            operation: operation.to_string(),
            condition_number: None,
            rank: None,
            expected_rank: None,
            context: OperationContext::new(operation),
        }
    }

    /// Create a BLAS error
    pub fn blas_error(operation: &str, error_code: i32) -> Self {
        ComputationError::LinearAlgebraLibraryError {
            operation: operation.to_string(),
            library: "BLAS".to_string(),
            error_code,
            details: None,
            context: OperationContext::new(operation),
        }
    }

    /// Create a LAPACK error
    pub fn lapack_error(operation: &str, error_code: i32) -> Self {
        ComputationError::LinearAlgebraLibraryError {
            operation: operation.to_string(),
            library: "LAPACK".to_string(),
            error_code,
            details: None,
            context: OperationContext::new(operation),
        }
    }

    /// Create an overflow error
    pub fn overflow(operation: &str, details: &str) -> Self {
        ComputationError::Overflow {
            operation: operation.to_string(),
            details: details.to_string(),
            value: None,
            context: OperationContext::new(operation),
        }
    }

    /// Create an FFT error
    pub fn fft_error(reason: &str, signal_length: usize, transform_type: &str) -> Self {
        ComputationError::FFTError {
            reason: reason.to_string(),
            signal_length,
            transform_type: transform_type.to_string(),
            context: OperationContext::new("FFT"),
        }
    }
}

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

    #[test]
    fn test_numerical_instability_severity() {
        let stable_err = ComputationError::NumericalInstability {
            operation: "test".to_string(),
            details: "test".to_string(),
            condition_number: Some(1e6),
            context: OperationContext::default(),
        };
        assert_eq!(stable_err.severity(), ErrorSeverity::Medium);

        let unstable_err = ComputationError::NumericalInstability {
            operation: "test".to_string(),
            details: "test".to_string(),
            condition_number: Some(1e15),
            context: OperationContext::default(),
        };
        assert_eq!(unstable_err.severity(), ErrorSeverity::High);
    }

    #[test]
    fn test_singular_matrix_suggestions() {
        let err = ComputationError::singular_matrix("matrix_inverse");
        let suggestions = err.recovery_suggestions();
        assert!(suggestions.iter().any(|s| s.contains("pseudo-inverse")));
        assert!(suggestions.iter().any(|s| s.contains("regularization")));
    }

    #[test]
    fn test_convergence_failure() {
        let err =
            ComputationError::convergence_failure("newton_raphson", "gradient too small", 100);
        assert!(err.is_potentially_recoverable());

        if let ComputationError::ConvergenceFailure { iterations, .. } = &err {
            assert_eq!(*iterations, 100);
        } else {
            panic!("Expected ConvergenceFailure variant");
        }
    }

    #[test]
    fn test_blas_error_severity() {
        let info_err = ComputationError::blas_error("dgemm", -1);
        assert_eq!(info_err.severity(), ErrorSeverity::Medium);

        let critical_err = ComputationError::blas_error("dgemm", 5);
        assert_eq!(critical_err.severity(), ErrorSeverity::High);
    }

    #[test]
    fn test_fft_error() {
        let err = ComputationError::fft_error("Invalid signal length", 1023, "DFT");
        let suggestions = err.recovery_suggestions();
        assert!(suggestions.iter().any(|s| s.contains("power of 2")));
        assert!(suggestions.iter().any(|s| s.contains("1023")));
    }
}