scirs2-special 0.3.2

Special functions module for SciRS2 (scirs2-special)
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
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
//! Python interoperability module for migration assistance
//!
//! This module provides helpers for users migrating from SciPy to SciRS2,
//! including compatibility layers and migration guides.

#![allow(dead_code)]

use crate::{bessel, erf, gamma, statistical};
use std::collections::HashMap;

/// Migration guide for common SciPy special functions to SciRS2
pub struct MigrationGuide {
    mappings: HashMap<String, FunctionMapping>,
    reverse_mappings: HashMap<String, String>, // SciRS2 -> SciPy
}

/// Represents a function mapping from SciPy to SciRS2
#[derive(Clone)]
pub struct FunctionMapping {
    pub scipy_name: String,
    pub scirs2_name: String,
    pub module_path: String,
    pub signature_changes: Vec<String>,
    pub notes: Vec<String>,
}

impl Default for MigrationGuide {
    fn default() -> Self {
        Self::new()
    }
}

impl MigrationGuide {
    /// Create a comprehensive migration guide
    pub fn new() -> Self {
        let mut mappings = HashMap::new();
        let mut reverse_mappings = HashMap::new();

        // Gamma functions
        mappings.insert(
            "scipy.special.gamma".to_string(),
            FunctionMapping {
                scipy_name: "gamma".to_string(),
                scirs2_name: "gamma".to_string(),
                module_path: "scirs2_special::gamma".to_string(),
                signature_changes: vec![],
                notes: vec![
                    "Direct replacement, same signature".to_string(),
                    "Returns NaN for negative integers (poles)".to_string(),
                ],
            },
        );

        mappings.insert(
            "scipy.special.gammaln".to_string(),
            FunctionMapping {
                scipy_name: "gammaln".to_string(),
                scirs2_name: "gammaln".to_string(),
                module_path: "scirs2_special::gamma".to_string(),
                signature_changes: vec![],
                notes: vec!["Direct replacement".to_string()],
            },
        );

        mappings.insert(
            "scipy.special.beta".to_string(),
            FunctionMapping {
                scipy_name: "beta".to_string(),
                scirs2_name: "beta".to_string(),
                module_path: "scirs2_special::gamma".to_string(),
                signature_changes: vec![],
                notes: vec!["Direct replacement".to_string()],
            },
        );

        // Bessel functions
        mappings.insert(
            "scipy.special.j0".to_string(),
            FunctionMapping {
                scipy_name: "j0".to_string(),
                scirs2_name: "j0".to_string(),
                module_path: "scirs2_special::bessel".to_string(),
                signature_changes: vec![],
                notes: vec!["Direct replacement".to_string()],
            },
        );

        mappings.insert(
            "scipy.special.jv".to_string(),
            FunctionMapping {
                scipy_name: "jv".to_string(),
                scirs2_name: "jv".to_string(),
                module_path: "scirs2_special::bessel".to_string(),
                signature_changes: vec!["Order parameter v must implement Float trait".to_string()],
                notes: vec!["Supports both integer and fractional orders".to_string()],
            },
        );

        // Error functions
        mappings.insert(
            "scipy.special.erf".to_string(),
            FunctionMapping {
                scipy_name: "erf".to_string(),
                scirs2_name: "erf".to_string(),
                module_path: "scirs2_special::erf".to_string(),
                signature_changes: vec![],
                notes: vec![
                    "Direct replacement".to_string(),
                    "Complex version available as erf_complex".to_string(),
                ],
            },
        );

        mappings.insert(
            "scipy.special.erfc".to_string(),
            FunctionMapping {
                scipy_name: "erfc".to_string(),
                scirs2_name: "erfc".to_string(),
                module_path: "scirs2_special::erf".to_string(),
                signature_changes: vec![],
                notes: vec!["Direct replacement".to_string()],
            },
        );

        // Statistical functions
        mappings.insert(
            "scipy.special.expit".to_string(),
            FunctionMapping {
                scipy_name: "expit".to_string(),
                scirs2_name: "logistic".to_string(),
                module_path: "scirs2_special::statistical".to_string(),
                signature_changes: vec![],
                notes: vec![
                    "Name change: expit -> logistic".to_string(),
                    "Same mathematical function: 1/(1+exp(-x))".to_string(),
                ],
            },
        );

        mappings.insert(
            "scipy.special.softmax".to_string(),
            FunctionMapping {
                scipy_name: "softmax".to_string(),
                scirs2_name: "softmax".to_string(),
                module_path: "scirs2_special::statistical".to_string(),
                signature_changes: vec![
                    "Takes &[f64] slice instead of numpy array".to_string(),
                    "Returns Vec<f64> instead of numpy array".to_string(),
                ],
                notes: vec!["Use ndarray for array operations".to_string()],
            },
        );

        // Orthogonal polynomials
        mappings.insert(
            "scipy.special.legendre".to_string(),
            FunctionMapping {
                scipy_name: "legendre".to_string(),
                scirs2_name: "legendre_p".to_string(),
                module_path: "scirs2_special::orthogonal".to_string(),
                signature_changes: vec![
                    "Returns polynomial value, not polynomial object".to_string(),
                    "Use legendre_p(n, x) for evaluation".to_string(),
                ],
                notes: vec![
                    "SciPy returns polynomial object, SciRS2 evaluates directly".to_string()
                ],
            },
        );

        // Additional mappings for comprehensive coverage

        // Airy functions
        mappings.insert(
            "scipy.special.airy".to_string(),
            FunctionMapping {
                scipy_name: "airy".to_string(),
                scirs2_name: "ai, bi, aip, bip".to_string(),
                module_path: "scirs2_special::airy".to_string(),
                signature_changes: vec![
                    "SciPy returns tuple (Ai, Aip, Bi, Bip)".to_string(),
                    "SciRS2 has separate functions for each".to_string(),
                ],
                notes: vec!["Use individual functions: ai(x), bi(x), aip(x), bip(x)".to_string()],
            },
        );

        // Elliptic functions
        mappings.insert(
            "scipy.special.ellipk".to_string(),
            FunctionMapping {
                scipy_name: "ellipk".to_string(),
                scirs2_name: "elliptic_k".to_string(),
                module_path: "scirs2_special::elliptic".to_string(),
                signature_changes: vec![],
                notes: vec!["Name change: ellipk -> elliptic_k".to_string()],
            },
        );

        // Hypergeometric functions
        mappings.insert(
            "scipy.special.hyp1f1".to_string(),
            FunctionMapping {
                scipy_name: "hyp1f1".to_string(),
                scirs2_name: "hyp1f1".to_string(),
                module_path: "scirs2_special::hypergeometric".to_string(),
                signature_changes: vec![],
                notes: vec!["Direct replacement".to_string()],
            },
        );

        // Spherical harmonics
        mappings.insert(
            "scipy.special.sph_harm".to_string(),
            FunctionMapping {
                scipy_name: "sph_harm".to_string(),
                scirs2_name: "sph_harm_complex".to_string(),
                module_path: "scirs2_special::spherical_harmonics".to_string(),
                signature_changes: vec![
                    "Parameter order: (m, n, theta, phi) in SciPy".to_string(),
                    "Parameter order: (n, m, theta, phi) in SciRS2".to_string(),
                ],
                notes: vec!["Watch out for parameter order change".to_string()],
            },
        );

        // Build reverse mappings
        for (scipy_name, mapping) in &mappings {
            reverse_mappings.insert(mapping.scirs2_name.clone(), scipy_name.clone());
        }

        MigrationGuide {
            mappings,
            reverse_mappings,
        }
    }

    /// Get mapping for a SciPy function
    pub fn get_mapping(&self, scipyfunc: &str) -> Option<&FunctionMapping> {
        self.mappings.get(scipyfunc)
    }

    /// Get reverse mapping (SciRS2 to SciPy)
    pub fn get_reverse_mapping(&self, scirs2func: &str) -> Option<&String> {
        self.reverse_mappings.get(scirs2func)
    }

    /// List all available mappings
    pub fn list_all_mappings(&self) -> Vec<(&String, &FunctionMapping)> {
        self.mappings.iter().collect()
    }

    /// Generate migration report for a list of SciPy functions
    pub fn generate_migration_report(&self, scipyfunctions: &[&str]) -> String {
        let mut report = String::from("SciPy to SciRS2 Migration Report\n");
        report.push_str("================================\n\n");

        for &func in scipyfunctions {
            if let Some(mapping) = self.get_mapping(func) {
                report.push_str(&format!("## {func}\n"));
                report.push_str(&format!("SciRS2 equivalent: `{}`\n", mapping.module_path));

                if !mapping.signature_changes.is_empty() {
                    report.push_str("\nSignature changes:\n");
                    for change in &mapping.signature_changes {
                        report.push_str(&format!("- {change}\n"));
                    }
                }

                if !mapping.notes.is_empty() {
                    report.push_str("\nNotes:\n");
                    for note in &mapping.notes {
                        report.push_str(&format!("- {note}\n"));
                    }
                }

                report.push('\n');
            } else {
                report.push_str(&format!("## {func}\n"));
                report.push_str(
                    "⚠️  No direct mapping found. May require custom implementation.\n\n",
                );
            }
        }

        report
    }
}

/// Compatibility layer providing SciPy-like function signatures
pub mod compat {
    use super::*;
    use scirs2_core::ndarray::{Array1, ArrayView1};

    /// SciPy-compatible gamma function for arrays
    pub fn gamma_array(x: &ArrayView1<f64>) -> Array1<f64> {
        x.mapv(gamma::gamma)
    }

    /// SciPy-compatible erf function for arrays
    pub fn erf_array(x: &ArrayView1<f64>) -> Array1<f64> {
        x.mapv(erf::erf)
    }

    /// SciPy-compatible j0 function for arrays
    pub fn j0_array(x: &ArrayView1<f64>) -> Array1<f64> {
        x.mapv(bessel::j0)
    }

    /// SciPy-compatible softmax with axis parameter
    pub fn softmax_axis(x: &ArrayView1<f64>, _axis: Option<usize>) -> Vec<f64> {
        // Note: This is simplified for 1D arrays
        // Full implementation would handle multi-dimensional arrays
        match statistical::softmax(x.view()) {
            Ok(result) => result.to_vec(),
            Err(_) => vec![],
        }
    }
}

/// Code generation helpers for migration
pub mod codegen {
    use super::*;
    #[cfg(feature = "python-interop")]
    use regex::Regex;

    /// Generate Rust code equivalent to SciPy code
    pub fn generate_rust_equivalent(_scipycode: &str) -> Result<String, String> {
        #[cfg(feature = "python-interop")]
        {
            generate_rust_equivalent_regex(_scipycode)
        }

        #[cfg(not(feature = "python-interop"))]
        {
            generate_rust_equivalent_simple(_scipycode)
        }
    }

    #[cfg(feature = "python-interop")]
    fn generate_rust_equivalent_regex(_scipycode: &str) -> Result<String, String> {
        let guide = MigrationGuide::new();
        let mut rust_code = String::new();
        let mut imports = std::collections::HashSet::new();
        let mut code_lines = Vec::new();

        // Regex patterns for common SciPy function calls
        let patterns = vec![
            (r"scipy\.special\.(\w+)\s*\(", "scipy.special."),
            (r"from scipy\.special import (\w+(?:,\s*\w+)*)", ""),
            (r"special\.(\w+)\s*\(", "scipy.special."),
        ];

        // Extract function names
        let mut found_functions = Vec::new();
        for (pattern, prefix) in patterns {
            let re = Regex::new(pattern).map_err(|e| e.to_string())?;
            for cap in re.captures_iter(_scipycode) {
                if let Some(func_match) = cap.get(1) {
                    let funcs = func_match.as_str();
                    for func in funcs.split(',') {
                        let func = func.trim();
                        let full_name = format!("{prefix}{func}");
                        if let Some(mapping) = guide.get_mapping(&full_name) {
                            found_functions.push((func.to_string(), mapping.clone()));
                            imports.insert(mapping.module_path.clone());
                        }
                    }
                }
            }
        }

        // Generate imports
        for import in &imports {
            rust_code.push_str(&format!("use {import};\n"));
        }

        if !imports.is_empty() {
            rust_code.push('\n');
        }

        // Generate _code transformation hints
        let mut transformed = _scipycode.to_string();
        for (scipyfunc, mapping) in &found_functions {
            // Add transformation comments
            code_lines.push(format!("// {} -> {}", scipyfunc, mapping.scirs2_name));

            // Simple replacement (this is a simplified example)
            transformed =
                transformed.replace(&format!("scipy.special.{scipyfunc}"), &mapping.scirs2_name);
            transformed =
                transformed.replace(&format!("special.{scipyfunc}"), &mapping.scirs2_name);
        }

        // Add transformation notes
        if !found_functions.is_empty() {
            rust_code.push_str("// Transformed _code:\n");
            rust_code.push_str(&format!("// {transformed}\n"));

            rust_code.push_str("\n// Notes:\n");
            for (_, mapping) in &found_functions {
                for note in &mapping.notes {
                    rust_code.push_str(&format!("// - {note}\n"));
                }
            }
        }

        if rust_code.is_empty() {
            Err("No recognized SciPy functions found".to_string())
        } else {
            Ok(rust_code)
        }
    }

    fn generate_rust_equivalent_simple(_scipycode: &str) -> Result<String, String> {
        let guide = MigrationGuide::new();
        let mut rust_code = String::new();

        // Simple pattern matching for common cases without regex
        let known_functions = vec!["gamma", "erf", "j0", "j1", "beta", "gammaln"];

        for func in known_functions {
            let scipy_pattern = format!("scipy.special.{func}");
            if _scipycode.contains(&scipy_pattern) {
                let full_name = format!("scipy.special.{func}");
                if let Some(mapping) = guide.get_mapping(&full_name) {
                    let module_path = &mapping.module_path;
                    rust_code.push_str(&format!("use {module_path};\n"));
                    let scirs2_name = &mapping.scirs2_name;
                    rust_code.push_str(&format!("// Replace {scipy_pattern} with {scirs2_name}\n"));
                }
            }
        }

        if rust_code.is_empty() {
            Err("No recognized SciPy functions found".to_string())
        } else {
            Ok(rust_code)
        }
    }

    /// Generate import statements for common migrations
    pub fn generate_imports(_scipyimports: &[&str]) -> String {
        let mut _imports = String::from("// SciRS2 _imports\n");

        for &import in _scipyimports {
            match import {
                "gamma" | "gammaln" | "beta" => {
                    _imports.push_str("use scirs2_special::gamma::{gamma, gammaln, beta};\n");
                }
                "j0" | "j1" | "jv" => {
                    _imports.push_str("use scirs2_special::bessel::{j0, j1, jv};\n");
                }
                "erf" | "erfc" => {
                    _imports.push_str("use scirs2_special::erf::{erf, erfc};\n");
                }
                _ => {}
            }
        }

        _imports
    }
}

/// Performance comparison utilities
pub mod performance {
    /// Structure to hold performance comparison data
    pub struct PerformanceComparison {
        pub function_name: String,
        pub scipy_time_ms: f64,
        pub scirs2_time_ms: f64,
        pub speedup: f64,
        pub accuracy_difference: f64,
    }

    impl PerformanceComparison {
        /// Generate a performance report
        pub fn report(&self) -> String {
            format!(
                "{}: SciRS2 is {:.1}x {} (accuracy diff: {:.2e})",
                self.function_name,
                if self.speedup > 1.0 {
                    self.speedup
                } else {
                    1.0 / self.speedup
                },
                if self.speedup > 1.0 {
                    "faster"
                } else {
                    "slower"
                },
                self.accuracy_difference
            )
        }
    }
}

/// Migration examples
pub mod examples {
    /// Example: Migrating gamma function usage
    pub fn gamma_migration_example() -> String {
        r#"
// SciPy Python code:
// from scipy.special import gamma
// result = gamma(5.5)

// SciRS2 Rust equivalent:
use scirs2_special::gamma;

let result = gamma(5.5_f64);

// For array operations:
use scirs2_core::ndarray::Array1;

let x = Array1::linspace(0.1, 10.0, 100);
let gamma_values = x.mapv(gamma);
"#
        .to_string()
    }

    /// Example: Migrating Bessel function usage
    pub fn bessel_migration_example() -> String {
        r#"
// SciPy Python code:
// from scipy.special import j0, jv
// y1 = j0(2.5)
// y2 = jv(1.5, 3.0)

// SciRS2 Rust equivalent:
use scirs2_special::bessel::{j0, jv};

let y1 = j0(2.5_f64);
let y2 = jv(1.5_f64, 3.0_f64);
"#
        .to_string()
    }

    /// Example: Migrating statistical functions
    pub fn statistical_migration_example() -> String {
        r#"
// SciPy Python code:
// from scipy.special import expit, softmax
// sigmoid = expit(x)
// probs = softmax(logits)

// SciRS2 Rust equivalent:
use scirs2_special::statistical::{logistic, softmax};

let sigmoid = logistic(x);
let probs = softmax(&logits);  // Note: takes a slice

// For ndarray:
use scirs2_core::ndarray::Array1;

let x_array = Array1::from_vec(vec![1.0, 2.0, 3.0]);
let sigmoid_array = x_array.mapv(logistic);
"#
        .to_string()
    }
}

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

    #[test]
    fn test_migration_guide() {
        let guide = MigrationGuide::new();

        // Test known mappings
        let gamma_mapping = guide
            .get_mapping("scipy.special.gamma")
            .expect("Operation failed");
        assert_eq!(gamma_mapping.scirs2_name, "gamma");

        let expit_mapping = guide
            .get_mapping("scipy.special.expit")
            .expect("Operation failed");
        assert_eq!(expit_mapping.scirs2_name, "logistic");
    }

    #[test]
    fn test_migration_report() {
        let guide = MigrationGuide::new();
        let functions = vec![
            "scipy.special.gamma",
            "scipy.special.erf",
            "scipy.special.unknown",
        ];
        let report = guide.generate_migration_report(&functions);

        assert!(report.contains("gamma"));
        assert!(report.contains("erf"));
        assert!(report.contains("No direct mapping found"));
    }

    #[test]
    fn test_codegen() {
        let _scipycode = "result = scipy.special.gamma(x)";
        let rust_code = codegen::generate_rust_equivalent(_scipycode).expect("Operation failed");

        assert!(rust_code.contains("use scirs2_special::gamma"));
    }

    #[test]
    fn test_performance_comparison() {
        let comparison = performance::PerformanceComparison {
            function_name: "gamma".to_string(),
            scipy_time_ms: 10.0,
            scirs2_time_ms: 2.0,
            speedup: 5.0,
            accuracy_difference: 1e-15,
        };

        let report = comparison.report();
        assert!(report.contains("5.0x faster"));
    }
}