numrs2 0.3.2

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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# NumRS2 Migration Guide

## Overview

This guide helps developers migrate from the legacy NumRS2 systems to the new enhanced architecture introduced in version 0.1.1. The migration maintains 100% backward compatibility while providing opt-in access to enhanced features.

## Error System Migration

### Legacy to Hierarchical Errors

#### Before (Legacy System)
```rust
use numrs::error::NumRs2Error;

fn legacy_function() -> Result<(), NumRs2Error> {
    // Old error handling
    Err(NumRs2Error::ShapeMismatch {
        expected: vec![2, 3],
        actual: vec![3, 2],
    })
}
```

#### After (Enhanced System)
```rust
use numrs::error::{CoreError, ErrorContext, OperationContext};

fn enhanced_function() -> Result<(), ErrorContext<CoreError>> {
    // Enhanced error with context
    let context = OperationContext::new("matrix_multiply")
        .with_shape(vec![2, 3])
        .with_shape(vec![3, 2])
        .with_parameter("method", "BLAS");
    
    Err(CoreError::shape_mismatch(vec![2, 3], vec![3, 2])
        .with_context(context)
        .with_suggestion("Ensure matrix dimensions are compatible for multiplication"))
}
```

#### Gradual Migration Strategy
```rust
use numrs::error::{NumRs2Error, CoreError};

// Option 1: Keep using legacy, auto-conversion happens
fn mixed_approach() -> Result<(), NumRs2Error> {
    let core_error = CoreError::shape_mismatch(vec![2, 3], vec![3, 2]);
    Err(NumRs2Error::Core(core_error)) // Automatic conversion
}

// Option 2: Use both systems during transition
fn transition_function() -> Result<(), NumRs2Error> {
    match some_operation() {
        Ok(result) => Ok(result),
        Err(legacy_err) => {
            // Convert to enhanced for logging/debugging
            let enhanced = legacy_err.with_context(
                OperationContext::new("transition_function")
            );
            eprintln!("Enhanced error info: {}", enhanced);
            Err(enhanced.into_inner()) // Convert back to legacy
        }
    }
}
```

### Error Categories and Severity

#### Understanding Error Categories
```rust
use numrs::error::{ErrorCategory, ErrorSeverity, NumRs2Error};

fn handle_error_by_category(err: &NumRs2Error) {
    match err.category() {
        ErrorCategory::Core => {
            // Handle shape mismatches, indexing errors
            eprintln!("Core library error: {}", err);
        },
        ErrorCategory::Computation => {
            // Handle numerical instability, convergence issues
            eprintln!("Computation error - may need different algorithm: {}", err);
        },
        ErrorCategory::Memory => {
            // Handle allocation failures, memory pressure
            eprintln!("Memory error - consider reducing data size: {}", err);
        },
        ErrorCategory::IO => {
            // Handle file operations, serialization issues
            eprintln!("I/O error - check file permissions and format: {}", err);
        },
    }
    
    // Handle by severity
    match err.severity() {
        ErrorSeverity::Critical => {
            // Immediately abort operation
            panic!("Critical error: {}", err);
        },
        ErrorSeverity::High => {
            // Log and try alternative approach
            log::error!("High severity error: {}", err);
        },
        ErrorSeverity::Medium | ErrorSeverity::Low => {
            // Log and continue with fallback
            log::warn!("Recoverable error: {}", err);
        },
    }
}
```

## Trait System Migration

### Legacy Array Operations
```rust
// Before - direct method calls
use numrs::Array;

fn legacy_operations() {
    let a = Array::zeros([3, 3]);
    let b = Array::ones([3, 3]);
    let result = a.add(&b); // Direct method call
}
```

### Enhanced Trait-Based Operations
```rust
// After - trait-based operations (backward compatible)
use numrs::{Array, traits::ArrayOps};

fn enhanced_operations() {
    let a = Array::zeros([3, 3]);
    let b = Array::ones([3, 3]);
    
    // Option 1: Still works (legacy)
    let result1 = a.add(&b);
    
    // Option 2: Trait-based (new)
    let result2 = ArrayOps::add(&a, &b).expect("Addition failed");
    
    // Option 3: Generic programming (new capability)
    fn generic_add<T, A>(x: &A, y: &A) -> A::Output 
    where 
        A: ArrayOps<T>,
        T: numrs::traits::NumericElement,
    {
        x.add(y).expect("Addition failed")
    }
}
```

### Implementing Custom Types
```rust
use numrs::traits::{NumericElement, ArrayOps};

// Custom numeric type
#[derive(Debug, Clone)]
struct MyNumber(f64);

impl NumericElement for MyNumber {
    fn zero() -> Self { MyNumber(0.0) }
    fn one() -> Self { MyNumber(1.0) }
    fn is_zero(&self) -> bool { self.0 == 0.0 }
    fn to_f64(&self) -> Option<f64> { Some(self.0) }
    fn from_f64(val: f64) -> Option<Self> { Some(MyNumber(val)) }
}

// Now MyNumber works with all NumRS2 generic operations
```

## Memory Management Migration

### Legacy Memory Usage
```rust
// Before - implicit memory management
let large_array = Array::zeros([10000, 10000]);
// Memory allocation happens behind the scenes
```

### Enhanced Memory Control
```rust
use numrs::memory_alloc::{ArenaAllocator, AllocationStrategy, IntelligentAllocationStrategy};

// Option 1: Use intelligent allocation strategy
let strategy = IntelligentAllocationStrategy::new()
    .with_arena_threshold(1024 * 1024) // Use arena for large allocations
    .with_cache_awareness(true);

// Option 2: Custom allocator for specific use cases
fn with_arena_allocator() {
    let arena = ArenaAllocator::new(1024 * 1024 * 100); // 100MB arena
    // Use arena for temporary calculations
    let temp_arrays = create_temporary_arrays_in_arena(&arena);
    // Arena automatically cleans up when dropped
}

// Option 3: Memory pressure monitoring
use numrs::error::{MemoryInfo, MemoryPressure};

fn monitor_memory_usage() {
    let memory_info = MemoryInfo {
        total_allocated: get_total_allocated(),
        peak_usage: get_peak_usage(),
        available_memory: Some(get_available_memory()),
        pressure_level: MemoryPressure::Medium,
    };
    
    match memory_info.pressure_level {
        MemoryPressure::High | MemoryPressure::Critical => {
            // Reduce memory usage
            cleanup_temporary_arrays();
        },
        _ => {
            // Continue normal operation
        }
    }
}
```

## Performance Migration

### SIMD Optimization Migration
```rust
// Before - manual SIMD usage
#[cfg(target_feature = "avx2")]
fn manual_simd_operation(data: &[f64]) -> Vec<f64> {
    // Manual SIMD implementation
    unimplemented!()
}

// After - automatic SIMD dispatch
use numrs::Array;

fn automatic_simd_operation(data: &Array<f64>) -> Array<f64> {
    // SIMD automatically used when available and beneficial
    data.map(|x| x * 2.0) // Automatically vectorized
}
```

### Memory Layout Optimization
```rust
use numrs::{Array, memory_alloc::AllocationStrategy};

// Configure memory layout for optimal performance
fn optimize_memory_layout() {
    let strategy = AllocationStrategy::cache_aware()
        .with_alignment(32) // AVX2 alignment
        .with_prefetch_hint(true);
    
    let array = Array::with_allocator(strategy)
        .zeros([1000, 1000]);
    
    // Array is automatically laid out for optimal cache performance
}
```

## API Migration Examples

### Random Number Generation
```rust
// Before
use numrs::random::RandomState;

fn legacy_random() {
    let mut rng = RandomState::new();
    let value = rng.random_range(0.0..1.0); // Old API
}

// After (backward compatible)
use numrs::random::RandomState;

fn enhanced_random() {
    let mut rng = RandomState::new();
    
    // Option 1: Legacy API still works
    let value1 = rng.random_range(0.0..1.0);
    
    // Option 2: Enhanced API with error handling
    let value2 = rng.try_random_range(0.0..1.0)
        .expect("Random generation failed");
    
    // Option 3: Seed management
    let seeded_rng = RandomState::with_seed(12345);
}
```

### Matrix Operations
```rust
// Before
use numrs::Array;

fn legacy_matrix_ops() {
    let a = Array::zeros([100, 100]);
    let b = Array::ones([100, 100]);
    let result = a.dot(&b);
}

// After - enhanced with error handling and context
use numrs::{Array, traits::LinearAlgebra, error::OperationContext};

fn enhanced_matrix_ops() -> Result<Array<f64>, numrs::error::NumRs2Error> {
    let a = Array::zeros([100, 100]);
    let b = Array::ones([100, 100]);
    
    // Enhanced operation with context
    let context = OperationContext::new("matrix_multiply")
        .with_shape(vec![100, 100])
        .with_shape(vec![100, 100])
        .with_parameter("algorithm", "BLAS");
    
    LinearAlgebra::dot(&a, &b)
        .map_err(|e| e.with_context(context))
}
```

## Testing Migration

### Enhanced Test Utilities
```rust
#[cfg(test)]
mod tests {
    use super::*;
    use numrs::error::{ErrorCategory, ErrorSeverity};
    
    #[test]
    fn test_error_handling() {
        let result = some_operation_that_fails();
        
        match result {
            Err(e) => {
                // Test error category
                assert_eq!(e.category(), ErrorCategory::Core);
                
                // Test severity
                assert_eq!(e.severity(), ErrorSeverity::High);
                
                // Test recovery suggestions
                assert!(!e.recovery_suggestions().is_empty());
            },
            Ok(_) => panic!("Expected error"),
        }
    }
    
    #[test]
    fn test_memory_pressure_handling() {
        // Test memory pressure scenarios
        let large_operation = || {
            // Operation that might cause memory pressure
            Array::zeros([10000, 10000])
        };
        
        // Monitor memory usage during test
        let result = large_operation();
        assert!(result.is_ok());
    }
}
```

## Migration Checklist

### Phase 1: Immediate (No Code Changes Required)
- [ ] Update to NumRS2 0.1.1
- [ ] Run existing tests - all should pass
- [ ] No breaking changes in public API

### Phase 2: Enhanced Error Handling (Optional)
- [ ] Replace manual error handling with error categories
- [ ] Add error severity checking
- [ ] Implement recovery suggestions
- [ ] Use error context for debugging

### Phase 3: Trait-Based Operations (Optional)
- [ ] Migrate to trait-based operations for new code
- [ ] Implement custom numeric types if needed
- [ ] Use generic programming for reusable components

### Phase 4: Memory Optimization (Performance Improvement)
- [ ] Configure allocation strategies for your use case
- [ ] Monitor memory pressure in critical paths
- [ ] Use arena allocators for temporary calculations

### Phase 5: Full Migration (Long-term)
- [ ] Fully adopt hierarchical error system
- [ ] Use enhanced memory management throughout
- [ ] Leverage trait system for extensibility
- [ ] Optimize performance with new features

## Common Migration Issues

### Issue 1: Import Path Changes
```rust
// Old
use numrs::error::NumRs2Error;

// New (both work)
use numrs::error::NumRs2Error; // Still works
use numrs::error::prelude::*;  // Recommended for new code
```

### Issue 2: Result Type Compatibility
```rust
// Functions returning old Result<T> work with new error types
fn legacy_result() -> numrs::error::Result<i32> {
    Ok(42)
}

fn new_code() {
    match legacy_result() {
        Ok(value) => println!("Success: {}", value),
        Err(e) => {
            // Can use new error features
            println!("Error category: {}", e.category());
            println!("Severity: {}", e.severity());
        }
    }
}
```

### Issue 3: Performance Regression Concerns
```rust
// New trait system has zero runtime cost
fn performance_test() {
    // Legacy call
    let result1 = array1.add(&array2);
    
    // Trait-based call - identical performance
    let result2 = ArrayOps::add(&array1, &array2).unwrap();
    
    // Both compile to identical assembly code
}
```

## Getting Help

### Documentation Resources
- `docs/ARCHITECTURE.md` - System architecture overview
- `docs/TRAIT_GUIDE.md` - Comprehensive trait system guide
- `docs/ERROR_HANDLING.md` - Error system documentation
- `docs/MEMORY_MANAGEMENT.md` - Memory management guide

### Community Support
- GitHub Issues: Report migration problems
- Documentation: In-code documentation for all public APIs
- Examples: See `examples/` directory for migration examples

### Performance Profiling
```rust
// Use built-in profiling for migration validation
use numrs::profiling::profile_operation;

let result = profile_operation("matrix_multiply", || {
    your_migrated_operation()
});

println!("Operation took: {:?}", result.duration);
println!("Memory used: {} bytes", result.peak_memory);
```