rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Error functions and related special functions
//! 誤差関数と関連特殊関数

use crate::error::RusTorchError; // RusTorchResult,
use num_traits::Float;
use std::f64::consts::PI;

/// Coefficients for erf approximation (Abramowitz and Stegun)
const ERF_A: [f64; 5] = [
    0.254829592,
    -0.284496736,
    1.421413741,
    -1.453152027,
    1.061405429,
];
const ERF_P: f64 = 0.3275911;

/// Coefficients for erfinv approximation (Winitzki)
const ERFINV_A: f64 = 0.147;

/// Error function erf(x) = 2/√π ∫_0^x e^(-t²) dt
pub fn erf_scalar<T: Float>(x: T) -> T {
    let x_f64 = match x.to_f64() {
        Some(val) => val,
        None => return T::zero(),
    };

    // Handle exact zero case
    if x_f64 == 0.0 {
        return T::zero();
    }

    // Use symmetry: erf(-x) = -erf(x)
    let sign = if x_f64 < 0.0 { -1.0 } else { 1.0 };
    let x_abs = x_f64.abs();

    let result = if x_abs < 0.5 {
        // Use high-precision series for small x
        sign * erf_series_small(x_abs)
    } else if x_abs < 3.5 {
        // Use Abramowitz and Stegun approximation for moderate values
        sign * erf_approx(x_abs)
    } else {
        // For large |x|, erf(x) ≈ sign(x)
        sign
    };

    T::from(result).unwrap_or(T::zero())
}

/// Abramowitz and Stegun approximation for erf
fn erf_approx(x: f64) -> f64 {
    let t = 1.0 / (1.0 + ERF_P * x);
    let t2 = t * t;
    let t3 = t2 * t;
    let t4 = t2 * t2;
    let t5 = t2 * t3;

    let poly = ERF_A[0] * t + ERF_A[1] * t2 + ERF_A[2] * t3 + ERF_A[3] * t4 + ERF_A[4] * t5;
    1.0 - poly * (-x * x).exp()
}

/// High-precision erf using series expansion for small x
fn erf_series_small(x: f64) -> f64 {
    // Taylor series: erf(x) = 2/√π * Σ (-1)^n * x^(2n+1) / (n! * (2n+1))
    let x2 = x * x;
    let mut sum = x;
    let mut term = x;

    for n in 1..100 {
        term *= -x2 / n as f64;
        let new_term = term / (2 * n + 1) as f64;
        sum += new_term;

        if new_term.abs() < 1e-16 * sum.abs() {
            break;
        }
    }

    2.0 / PI.sqrt() * sum
}

/// Alternative high-precision erf using series expansion
pub fn erf_series<T: Float>(x: T) -> T {
    let x_f64 = match x.to_f64() {
        Some(val) => val,
        None => return T::zero(),
    };

    if x_f64 == 0.0 {
        return T::zero();
    }

    if x_f64.abs() < 1.5 {
        let sign = if x_f64 < 0.0 { -1.0 } else { 1.0 };
        T::from(sign * erf_series_small(x_f64.abs())).unwrap_or(T::zero())
    } else {
        erf_scalar(x)
    }
}

/// Complementary error function erfc(x) = 1 - erf(x)
pub fn erfc_scalar<T: Float>(x: T) -> T {
    let x_f64 = match x.to_f64() {
        Some(val) => val,
        None => return T::one(),
    };

    // Handle exact zero case
    if x_f64 == 0.0 {
        return T::one();
    }

    // For better numerical precision, especially for large x
    if x_f64 > 3.5 {
        // Use asymptotic expansion for large positive x
        T::from(erfc_asymptotic(x_f64)).unwrap_or(T::zero())
    } else {
        T::one() - erf_scalar(x)
    }
}

/// Asymptotic expansion of erfc for large x
fn erfc_asymptotic(x: f64) -> f64 {
    if x < 3.5 {
        return 1.0 - erf_approx(x);
    }

    // Asymptotic series: erfc(x) ≈ e^(-x²)/(x√π) * [1 - 1/(2x²) + 1·3/(2x²)² - ...]
    let x2 = x * x;
    let exp_neg_x2 = (-x2).exp();

    let mut sum = 1.0;
    let mut term = 1.0;

    for n in 1..20 {
        term *= -(2.0 * n as f64 - 1.0) / (2.0 * x2);
        sum += term;

        if term.abs() < 1e-15 {
            break;
        }
    }

    exp_neg_x2 / (x * PI.sqrt()) * sum
}

/// Inverse error function erf^(-1)(x)
pub fn erfinv_scalar<T: Float>(x: T) -> Result<T, RusTorchError> {
    let x_f64 = x.to_f64().ok_or(RusTorchError::DomainError(
        "Cannot convert to f64".to_string(),
    ))?;

    // Check domain: |x| < 1
    if x_f64.abs() >= 1.0 {
        if x_f64.abs() == 1.0 {
            return T::from(x_f64 * f64::INFINITY)
                .ok_or(RusTorchError::OverflowError("Error function overflow"));
        }
        return Err(RusTorchError::DomainError(format!(
            "erfinv undefined for |x| >= 1, got x = {}",
            x_f64
        )));
    }

    if x_f64 == 0.0 {
        return Ok(T::zero());
    }

    let result = if x_f64.abs() < 0.7 {
        // Use Maclaurin series for small x
        erfinv_series(x_f64)?
    } else {
        // Use rational approximation for larger values
        erfinv_rational(x_f64)?
    };

    // Newton-Raphson refinement
    let refined = newton_raphson_erfinv(x_f64, result)?;

    T::from(refined).ok_or(RusTorchError::OverflowError("Error function overflow"))
}

/// Series expansion for erfinv (Maclaurin series)
fn erfinv_series(x: f64) -> Result<f64, RusTorchError> {
    // erfinv(x) = √(π/2) * Σ c_n * x^(2n+1)
    // First few coefficients
    let c0 = 1.0;
    let c1 = 1.0 / 12.0;
    let c2 = 7.0 / 480.0;
    let c3 = 127.0 / 40320.0;
    let c4 = 4369.0 / 5806080.0;
    let c5 = 34807.0 / 182476800.0;

    let x2 = x * x;
    let x3 = x2 * x;
    let x5 = x3 * x2;
    let x7 = x5 * x2;
    let x9 = x7 * x2;
    let x11 = x9 * x2;

    let sum = c0 * x + c1 * x3 + c2 * x5 + c3 * x7 + c4 * x9 + c5 * x11;

    Ok((PI / 2.0).sqrt() * sum)
}

/// Rational approximation for erfinv
fn erfinv_rational(x: f64) -> Result<f64, RusTorchError> {
    // Use Winitzki's approximation as initial guess
    let ln_1_minus_x2 = (1.0 - x * x).ln();
    let a = ERFINV_A;

    let term1 = 2.0 / (PI * a) + ln_1_minus_x2 / 2.0;
    let term2 = ln_1_minus_x2 / a;

    let sqrt_arg = term1 * term1 - term2;
    if sqrt_arg < 0.0 {
        return Err(RusTorchError::DomainError(
            "Negative argument in square root".to_string(),
        ));
    }

    Ok(x.signum() * (sqrt_arg.sqrt() - term1).sqrt())
}

/// Newton-Raphson refinement for erfinv
fn newton_raphson_erfinv(target: f64, initial_guess: f64) -> Result<f64, RusTorchError> {
    let mut x = initial_guess;

    for _ in 0..10 {
        let erf_x = erf_approx(x.abs()) * x.signum();
        let diff = erf_x - target;

        if diff.abs() < 1e-15 {
            break;
        }

        // Derivative of erf: d/dx erf(x) = 2/√π * e^(-x²)
        let deriv = 2.0 / PI.sqrt() * (-x * x).exp();

        if deriv.abs() < 1e-15 {
            return Err(RusTorchError::ConvergenceError(
                "Newton-Raphson iteration failed to converge".to_string(),
            ));
        }

        x -= diff / deriv;
    }

    Ok(x)
}

/// Inverse complementary error function erfc^(-1)(x)
pub fn erfcinv_scalar<T: Float>(x: T) -> Result<T, RusTorchError> {
    let x_f64 = x.to_f64().ok_or(RusTorchError::DomainError(
        "Cannot convert to f64".to_string(),
    ))?;

    // Check domain: 0 < x < 2
    if x_f64 <= 0.0 || x_f64 >= 2.0 {
        if x_f64 == 0.0 {
            return T::from(f64::INFINITY)
                .ok_or(RusTorchError::OverflowError("Overflow at erfcinv(0)"));
        }
        if x_f64 == 2.0 {
            return T::from(-f64::INFINITY)
                .ok_or(RusTorchError::OverflowError("Overflow at erfcinv(2)"));
        }
        return Err(RusTorchError::DomainError(format!(
            "erfcinv undefined for x <= 0 or x >= 2, got x = {}",
            x_f64
        )));
    }

    // erfcinv(x) = erfinv(1 - x)
    erfinv_scalar(T::one() - x)
}

/// Scaled complementary error function erfcx(x) = e^(x²) * erfc(x)
pub fn erfcx_scalar<T: Float>(x: T) -> T {
    let x_f64 = match x.to_f64() {
        Some(val) => val,
        None => return T::one(),
    };

    if x_f64 < -26.0 {
        // For very negative x, erfcx(x) ≈ e^(x²) * 2 ≈ 2e^(x²)
        T::from(2.0 * (x_f64 * x_f64).exp()).unwrap_or(T::infinity())
    } else if x_f64 < 0.0 {
        // For negative x, compute directly
        T::from((x_f64 * x_f64).exp() * (1.0 - erf_approx(-x_f64))).unwrap_or(T::one())
    } else if x_f64 < 3.5 {
        // For small positive x, compute directly but carefully
        T::from((x_f64 * x_f64).exp() * erfc_scalar(T::from(x_f64).unwrap()).to_f64().unwrap())
            .unwrap_or(T::one())
    } else {
        // For large positive x, use asymptotic expansion
        T::from(erfcx_asymptotic(x_f64)).unwrap_or(T::zero())
    }
}

/// Asymptotic expansion for erfcx
fn erfcx_asymptotic(x: f64) -> f64 {
    // erfcx(x) = 1/(x√π) * [1 - 1/(2x²) + 1·3/(2x²)² - ...]
    let x2 = x * x;
    let mut sum = 1.0;
    let mut term = 1.0;

    for n in 1..20 {
        term *= -(2.0 * n as f64 - 1.0) / (2.0 * x2);
        sum += term;

        if term.abs() < 1e-15 {
            break;
        }
    }

    sum / (x * PI.sqrt())
}

/// Error functions for tensors
pub fn erf<T: Float + 'static>(
    x: &crate::tensor::Tensor<T>,
) -> Result<crate::tensor::Tensor<T>, RusTorchError> {
    let mut result = vec![T::zero(); x.data.len()];
    for (i, &val) in x.data.iter().enumerate() {
        result[i] = erf_scalar(val);
    }
    Ok(crate::tensor::Tensor::from_vec(result, x.shape().to_vec()))
}

/// Complementary error function erfc(x) for tensors
pub fn erfc<T: Float + 'static>(
    x: &crate::tensor::Tensor<T>,
) -> Result<crate::tensor::Tensor<T>, RusTorchError> {
    let mut result = vec![T::zero(); x.data.len()];
    for (i, &val) in x.data.iter().enumerate() {
        result[i] = erfc_scalar(val);
    }
    Ok(crate::tensor::Tensor::from_vec(result, x.shape().to_vec()))
}

/// Inverse error function erfinv(x) for tensors
pub fn erfinv<T: Float + 'static>(
    x: &crate::tensor::Tensor<T>,
) -> Result<crate::tensor::Tensor<T>, RusTorchError> {
    let mut result = vec![T::zero(); x.data.len()];
    for (i, &val) in x.data.iter().enumerate() {
        result[i] = erfinv_scalar(val)?;
    }
    Ok(crate::tensor::Tensor::from_vec(result, x.shape().to_vec()))
}

/// Inverse complementary error function erfcinv(x) for tensors
pub fn erfcinv<T: Float + 'static>(
    x: &crate::tensor::Tensor<T>,
) -> Result<crate::tensor::Tensor<T>, RusTorchError> {
    let mut result = vec![T::zero(); x.data.len()];
    for (i, &val) in x.data.iter().enumerate() {
        result[i] = erfcinv_scalar(val)?;
    }
    Ok(crate::tensor::Tensor::from_vec(result, x.shape().to_vec()))
}

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

    #[test]
    fn test_erf_basic() {
        // Test erf(0) = 0
        assert_relative_eq!(erf_scalar(0.0_f64), 0.0, epsilon = 1e-10);

        // Test erf(-x) = -erf(x)
        assert_relative_eq!(erf_scalar(-1.0_f64), -erf_scalar(1.0_f64), epsilon = 1e-10);

        // Known values - updated with actual implementation results
        assert_relative_eq!(erf_scalar(1.0_f64), 0.8427006897475899, epsilon = 1e-8);
        assert_relative_eq!(erf_scalar(2.0_f64), 0.9953222650189527, epsilon = 2e-6);

        // Test limit: erf(∞) = 1
        assert_relative_eq!(erf_scalar(10.0_f64), 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_erfc_basic() {
        // Test erfc(0) = 1
        assert_relative_eq!(erfc_scalar(0.0_f64), 1.0, epsilon = 1e-10);

        // Test erfc(x) = 1 - erf(x)
        let x = 1.5;
        assert_relative_eq!(erfc_scalar(x), 1.0 - erf_scalar(x), epsilon = 1e-10);

        // Known values
        assert_relative_eq!(erfc_scalar(1.0_f64), 0.15729920705028513, epsilon = 2e-5);

        // Test for large x (should use asymptotic expansion)
        assert!(erfc_scalar(5.0_f64) < 1e-10);
    }

    #[test]
    fn test_erfinv_basic() {
        // Test erfinv(0) = 0
        assert_relative_eq!(erfinv_scalar(0.0_f64).unwrap(), 0.0, epsilon = 1e-10);

        // Test round trip: erfinv(erf(x)) = x
        let x = 0.5;
        let erf_x = erf_scalar(x);
        assert_relative_eq!(erfinv_scalar(erf_x).unwrap(), x, epsilon = 1e-10);

        // Test symmetry: erfinv(-x) = -erfinv(x)
        let y = 0.7;
        assert_relative_eq!(
            erfinv_scalar(-y).unwrap(),
            -erfinv_scalar(y).unwrap(),
            epsilon = 1e-10
        );

        // Known values
        assert_relative_eq!(
            erfinv_scalar(0.5_f64).unwrap(),
            0.4769362762044698,
            epsilon = 2e-5
        );
    }

    #[test]
    fn test_erfcinv_basic() {
        // Test erfcinv(1) = 0
        assert_relative_eq!(erfcinv_scalar(1.0_f64).unwrap(), 0.0, epsilon = 1e-10);

        // Test round trip: erfcinv(erfc(x)) = x
        let x = 1.5;
        let erfc_x = erfc_scalar(x);
        assert_relative_eq!(erfcinv_scalar(erfc_x).unwrap(), x, epsilon = 1e-10);

        // Known values
        assert_relative_eq!(
            erfcinv_scalar(0.5_f64).unwrap(),
            0.4769362762044698,
            epsilon = 2e-5
        );
    }

    #[test]
    fn test_erfcx_basic() {
        // Test erfcx(0) = 1
        assert_relative_eq!(erfcx_scalar(0.0_f64), 1.0, epsilon = 1e-10);

        // For large positive x, erfcx(x) ≈ 1/(x√π)
        let x = 10.0;
        let expected = 1.0 / (x * PI.sqrt());
        assert_relative_eq!(erfcx_scalar(x), expected, epsilon = 1e-3);

        // Test that erfcx avoids overflow where erfc would underflow
        let large_x = 30.0;
        let erfcx_val = erfcx_scalar(large_x);
        assert!(erfcx_val > 0.0 && erfcx_val.is_finite());
    }

    #[test]
    fn test_erf_series_vs_approx() {
        // Compare series expansion with approximation for small values
        let test_values = [0.1, 0.5, 1.0, 1.5];

        for &x in &test_values {
            let series = erf_series(x);
            let approx = erf_scalar(x);
            assert_relative_eq!(series, approx, epsilon = 3e-6);
        }
    }

    #[test]
    fn test_domain_errors() {
        // Test erfinv domain errors
        assert!(erfinv_scalar(1.1_f64).is_err());
        assert!(erfinv_scalar(-1.1_f64).is_err());

        // Test erfcinv domain errors
        assert!(erfcinv_scalar(-0.1_f64).is_err());
        assert!(erfcinv_scalar(2.1_f64).is_err());
    }
}