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
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
//! Enhanced statistical distributions for WebAssembly
//! WebAssembly向け強化統計分布

use crate::distributions::{
    bernoulli::Bernoulli, beta::Beta, exponential::Exponential, gamma::Gamma, normal::Normal,
    uniform::Uniform, DistributionTrait,
};
use crate::tensor::Tensor;
use crate::wasm::math::special::*; // Import special functions
use num_traits::Float;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;

/// WASM-compatible random number generator using Linear Congruential Generator
/// WASM互換の線形合同法乱数生成器
#[wasm_bindgen]
pub struct WasmRng {
    seed: u32,
}

#[wasm_bindgen]
impl WasmRng {
    /// Create new RNG with seed
    #[wasm_bindgen(constructor)]
    pub fn new(seed: u32) -> Self {
        Self { seed }
    }

    /// Generate next random u32
    #[wasm_bindgen]
    pub fn next_u32(&mut self) -> u32 {
        // Linear Congruential Generator: a=1103515245, c=12345, m=2^32
        self.seed = self.seed.wrapping_mul(1103515245).wrapping_add(12345);
        self.seed
    }

    /// Generate random f32 in [0, 1)
    #[wasm_bindgen]
    pub fn next_f32(&mut self) -> f32 {
        (self.next_u32() as f32) / (u32::MAX as f32)
    }

    /// Generate random f32 in [0, 1) (alternative name for consistency)
    #[wasm_bindgen]
    pub fn uniform(&mut self) -> f32 {
        self.next_f32()
    }
}

// Enhanced distribution implementations / 強化分布実装

#[wasm_bindgen]
pub struct NormalDistributionWasm {
    mean: f64,
    std: f64,
}

#[wasm_bindgen]
impl NormalDistributionWasm {
    #[wasm_bindgen(constructor)]
    pub fn new(mean: f64, std: f64) -> NormalDistributionWasm {
        NormalDistributionWasm { mean, std }
    }

    #[wasm_bindgen]
    pub fn sample(&self) -> f64 {
        let mean_tensor = Tensor::from_vec(vec![self.mean], vec![1]);
        let std_tensor = Tensor::from_vec(vec![self.std], vec![1]);
        let normal = Normal::new(mean_tensor, std_tensor, false).unwrap();
        let sample_tensor = normal.sample(Some(&[1])).unwrap();
        sample_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn sample_array(&self, n: usize) -> Vec<f64> {
        let mean_tensor = Tensor::from_vec(vec![self.mean], vec![1]);
        let std_tensor = Tensor::from_vec(vec![self.std], vec![1]);
        let normal = Normal::new(mean_tensor, std_tensor, false).unwrap();
        (0..n)
            .map(|_| {
                let sample_tensor = normal.sample(Some(&[1])).unwrap();
                sample_tensor.data[0]
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn log_prob(&self, x: f64) -> f64 {
        let mean_tensor = Tensor::from_vec(vec![self.mean], vec![1]);
        let std_tensor = Tensor::from_vec(vec![self.std], vec![1]);
        let normal = Normal::new(mean_tensor, std_tensor, false).unwrap();
        let x_tensor = Tensor::from_vec(vec![x], vec![1]);
        let log_prob_tensor = normal.log_prob(&x_tensor).unwrap();
        log_prob_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn log_prob_array(&self, values: &[f64]) -> Vec<f64> {
        let mean_tensor = Tensor::from_vec(vec![self.mean], vec![1]);
        let std_tensor = Tensor::from_vec(vec![self.std], vec![1]);
        let normal = Normal::new(mean_tensor, std_tensor, false).unwrap();
        values
            .iter()
            .map(|&x| {
                let x_tensor = Tensor::from_vec(vec![x], vec![1]);
                let log_prob_tensor = normal.log_prob(&x_tensor).unwrap();
                log_prob_tensor.data[0]
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn mean(&self) -> f64 {
        self.mean
    }

    #[wasm_bindgen]
    pub fn variance(&self) -> f64 {
        self.std * self.std
    }

    #[wasm_bindgen]
    pub fn std_dev(&self) -> f64 {
        self.std
    }
}

#[wasm_bindgen]
pub struct UniformDistributionWasm {
    low: f64,
    high: f64,
}

#[wasm_bindgen]
impl UniformDistributionWasm {
    #[wasm_bindgen(constructor)]
    pub fn new(low: f64, high: f64) -> UniformDistributionWasm {
        UniformDistributionWasm { low, high }
    }

    #[wasm_bindgen]
    pub fn sample(&self) -> f64 {
        let low_tensor = Tensor::from_vec(vec![self.low], vec![1]);
        let high_tensor = Tensor::from_vec(vec![self.high], vec![1]);
        let uniform = Uniform::new(low_tensor, high_tensor, false).unwrap();
        let sample_tensor = uniform.sample(Some(&[1])).unwrap();
        sample_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn sample_array(&self, n: usize) -> Vec<f64> {
        let low_tensor = Tensor::from_vec(vec![self.low], vec![1]);
        let high_tensor = Tensor::from_vec(vec![self.high], vec![1]);
        let uniform = Uniform::new(low_tensor, high_tensor, false).unwrap();
        (0..n)
            .map(|_| {
                let sample_tensor = uniform.sample(Some(&[1])).unwrap();
                sample_tensor.data[0]
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn log_prob(&self, x: f64) -> f64 {
        let low_tensor = Tensor::from_vec(vec![self.low], vec![1]);
        let high_tensor = Tensor::from_vec(vec![self.high], vec![1]);
        let uniform = Uniform::new(low_tensor, high_tensor, false).unwrap();
        let x_tensor = Tensor::from_vec(vec![x], vec![1]);
        let log_prob_tensor = uniform.log_prob(&x_tensor).unwrap();
        log_prob_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn mean(&self) -> f64 {
        (self.low + self.high) / 2.0
    }

    #[wasm_bindgen]
    pub fn variance(&self) -> f64 {
        let range = self.high - self.low;
        range * range / 12.0
    }
}

#[wasm_bindgen]
pub struct ExponentialDistributionWasm {
    rate: f64,
}

#[wasm_bindgen]
impl ExponentialDistributionWasm {
    #[wasm_bindgen(constructor)]
    pub fn new(rate: f64) -> ExponentialDistributionWasm {
        ExponentialDistributionWasm { rate }
    }

    #[wasm_bindgen]
    pub fn sample(&self) -> f64 {
        let rate_tensor = Tensor::from_vec(vec![self.rate], vec![1]);
        let exp = Exponential::new(rate_tensor, false).unwrap();
        let sample_tensor = exp.sample(Some(&[1])).unwrap();
        sample_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn sample_array(&self, n: usize) -> Vec<f64> {
        let rate_tensor = Tensor::from_vec(vec![self.rate], vec![1]);
        let exp = Exponential::new(rate_tensor, false).unwrap();
        (0..n)
            .map(|_| {
                let sample_tensor = exp.sample(Some(&[1])).unwrap();
                sample_tensor.data[0]
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn log_prob(&self, x: f64) -> f64 {
        let rate_tensor = Tensor::from_vec(vec![self.rate], vec![1]);
        let exp = Exponential::new(rate_tensor, false).unwrap();
        let x_tensor = Tensor::from_vec(vec![x], vec![1]);
        let log_prob_tensor = exp.log_prob(&x_tensor).unwrap();
        log_prob_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn mean(&self) -> f64 {
        1.0 / self.rate
    }

    #[wasm_bindgen]
    pub fn variance(&self) -> f64 {
        1.0 / (self.rate * self.rate)
    }
}

#[wasm_bindgen]
pub struct GammaDistributionWasm {
    shape: f64,
    scale: f64,
}

#[wasm_bindgen]
impl GammaDistributionWasm {
    #[wasm_bindgen(constructor)]
    pub fn new(shape: f64, scale: f64) -> GammaDistributionWasm {
        GammaDistributionWasm { shape, scale }
    }

    #[wasm_bindgen]
    pub fn sample(&self) -> f64 {
        let shape_tensor = Tensor::from_vec(vec![self.shape], vec![1]);
        let scale_tensor = Tensor::from_vec(vec![self.scale], vec![1]);
        let gamma = Gamma::from_concentration_scale(shape_tensor, scale_tensor, false).unwrap();
        let sample_tensor = gamma.sample(Some(&[1])).unwrap();
        sample_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn sample_array(&self, n: usize) -> Vec<f64> {
        let shape_tensor = Tensor::from_vec(vec![self.shape], vec![1]);
        let scale_tensor = Tensor::from_vec(vec![self.scale], vec![1]);
        let gamma = Gamma::from_concentration_scale(shape_tensor, scale_tensor, false).unwrap();
        (0..n)
            .map(|_| {
                let sample_tensor = gamma.sample(Some(&[1])).unwrap();
                sample_tensor.data[0]
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn log_prob(&self, x: f64) -> f64 {
        let shape_tensor = Tensor::from_vec(vec![self.shape], vec![1]);
        let scale_tensor = Tensor::from_vec(vec![self.scale], vec![1]);
        let gamma = Gamma::from_concentration_scale(shape_tensor, scale_tensor, false).unwrap();
        let x_tensor = Tensor::from_vec(vec![x], vec![1]);
        let log_prob_tensor = gamma.log_prob(&x_tensor).unwrap();
        log_prob_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn mean(&self) -> f64 {
        self.shape * self.scale
    }

    #[wasm_bindgen]
    pub fn variance(&self) -> f64 {
        self.shape * self.scale * self.scale
    }
}

#[wasm_bindgen]
pub struct BetaDistributionWasm {
    alpha: f64,
    beta: f64,
}

#[wasm_bindgen]
impl BetaDistributionWasm {
    #[wasm_bindgen(constructor)]
    pub fn new(alpha: f64, beta: f64) -> BetaDistributionWasm {
        BetaDistributionWasm { alpha, beta }
    }

    #[wasm_bindgen]
    pub fn sample(&self) -> f64 {
        let alpha_tensor = Tensor::from_vec(vec![self.alpha], vec![1]);
        let beta_tensor = Tensor::from_vec(vec![self.beta], vec![1]);
        let beta = Beta::new(alpha_tensor, beta_tensor, false).unwrap();
        let sample_tensor = beta.sample(Some(&[1])).unwrap();
        sample_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn sample_array(&self, n: usize) -> Vec<f64> {
        let alpha_tensor = Tensor::from_vec(vec![self.alpha], vec![1]);
        let beta_tensor = Tensor::from_vec(vec![self.beta], vec![1]);
        let beta = Beta::new(alpha_tensor, beta_tensor, false).unwrap();
        (0..n)
            .map(|_| {
                let sample_tensor = beta.sample(Some(&[1])).unwrap();
                sample_tensor.data[0]
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn log_prob(&self, x: f64) -> f64 {
        let alpha_tensor = Tensor::from_vec(vec![self.alpha], vec![1]);
        let beta_tensor = Tensor::from_vec(vec![self.beta], vec![1]);
        let beta = Beta::new(alpha_tensor, beta_tensor, false).unwrap();
        let x_tensor = Tensor::from_vec(vec![x], vec![1]);
        let log_prob_tensor = beta.log_prob(&x_tensor).unwrap();
        log_prob_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn mean(&self) -> f64 {
        self.alpha / (self.alpha + self.beta)
    }

    #[wasm_bindgen]
    pub fn variance(&self) -> f64 {
        let sum = self.alpha + self.beta;
        (self.alpha * self.beta) / (sum * sum * (sum + 1.0))
    }
}

#[wasm_bindgen]
pub struct BernoulliDistributionWasm {
    p: f64,
}

#[wasm_bindgen]
impl BernoulliDistributionWasm {
    #[wasm_bindgen(constructor)]
    pub fn new(p: f64) -> BernoulliDistributionWasm {
        BernoulliDistributionWasm { p }
    }

    #[wasm_bindgen]
    pub fn sample(&self) -> bool {
        let bernoulli = Bernoulli::from_scalar_prob(self.p, false).unwrap();
        let sample_tensor = bernoulli.sample(Some(&[1])).unwrap();
        sample_tensor.data[0] > 0.5
    }

    #[wasm_bindgen]
    pub fn sample_array(&self, n: usize) -> Vec<u8> {
        (0..n)
            .map(|_| {
                let bernoulli = Bernoulli::from_scalar_prob(self.p, false).unwrap();
                let sample_tensor = bernoulli.sample(Some(&[1])).unwrap();
                if sample_tensor.data[0] > 0.5 {
                    1u8
                } else {
                    0u8
                }
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn sample_f64(&self) -> f64 {
        if self.sample() {
            1.0
        } else {
            0.0
        }
    }

    #[wasm_bindgen]
    pub fn sample_f64_array(&self, n: usize) -> Vec<f64> {
        (0..n)
            .map(|_| {
                let bernoulli = Bernoulli::from_scalar_prob(self.p, false).unwrap();
                let sample_tensor = bernoulli.sample(Some(&[1])).unwrap();
                if sample_tensor.data[0] > 0.5 {
                    1.0
                } else {
                    0.0
                }
            })
            .collect()
    }

    #[wasm_bindgen]
    pub fn log_prob(&self, x: bool) -> f64 {
        let bernoulli = Bernoulli::from_scalar_prob(self.p, false).unwrap();
        let x_tensor = Tensor::from_vec(vec![if x { 1.0 } else { 0.0 }], vec![1]);
        let log_prob_tensor = bernoulli.log_prob(&x_tensor).unwrap();
        log_prob_tensor.data[0]
    }

    #[wasm_bindgen]
    pub fn mean(&self) -> f64 {
        self.p
    }

    #[wasm_bindgen]
    pub fn variance(&self) -> f64 {
        self.p * (1.0 - self.p)
    }
}

// Utility functions for statistical analysis / 統計解析ユーティリティ関数
#[wasm_bindgen]
pub fn normal_cdf_wasm(x: f64, mean: f64, std: f64) -> f64 {
    let z = (x - mean) / std;
    0.5 * (1.0 + erf_wasm(z / std::f64::consts::SQRT_2))
}

#[wasm_bindgen]
pub fn normal_quantile_wasm(p: f64, mean: f64, std: f64) -> f64 {
    if p <= 0.0 || p >= 1.0 {
        return f64::NAN;
    }
    let z = std::f64::consts::SQRT_2 * erfinv_wasm(2.0 * p - 1.0);
    mean + std * z
}

// Fast statistical computations / 高速統計計算
#[wasm_bindgen]
pub fn quick_stats_wasm(values: &[f64]) -> Vec<f64> {
    if values.is_empty() {
        return vec![f64::NAN; 4]; // mean, variance, skewness, kurtosis
    }

    let n = values.len() as f64;
    let mean = values.iter().sum::<f64>() / n;

    let variance = values.iter().map(|&x| (x - mean) * (x - mean)).sum::<f64>() / (n - 1.0);

    let std_dev = variance.sqrt();

    let skewness = if std_dev > 0.0 {
        values
            .iter()
            .map(|&x| {
                let z = (x - mean) / std_dev;
                z * z * z
            })
            .sum::<f64>()
            / n
    } else {
        0.0
    };

    let kurtosis = if std_dev > 0.0 {
        values
            .iter()
            .map(|&x| {
                let z = (x - mean) / std_dev;
                z * z * z * z
            })
            .sum::<f64>()
            / n
            - 3.0 // Excess kurtosis
    } else {
        0.0
    };

    vec![mean, variance, skewness, kurtosis]
}

// Performance testing utilities / パフォーマンステストユーティリティ
#[wasm_bindgen]
pub fn benchmark_special_functions_wasm(iterations: usize) -> Vec<f64> {
    let start = web_sys::window().unwrap().performance().unwrap().now();

    // Benchmark gamma function
    let gamma_start = web_sys::window().unwrap().performance().unwrap().now();
    for i in 0..iterations {
        let x = 1.0 + (i as f64) / (iterations as f64) * 10.0;
        gamma_wasm(x);
    }
    let gamma_time = web_sys::window().unwrap().performance().unwrap().now() - gamma_start;

    // Benchmark bessel function
    let bessel_start = web_sys::window().unwrap().performance().unwrap().now();
    for i in 0..iterations {
        let x = 0.5 + (i as f64) / (iterations as f64) * 5.0;
        bessel_j_wasm(0.0, x);
    }
    let bessel_time = web_sys::window().unwrap().performance().unwrap().now() - bessel_start;

    // Benchmark error function
    let erf_start = web_sys::window().unwrap().performance().unwrap().now();
    for i in 0..iterations {
        let x = -3.0 + (i as f64) / (iterations as f64) * 6.0;
        erf_wasm(x);
    }
    let erf_time = web_sys::window().unwrap().performance().unwrap().now() - erf_start;

    let total_time = web_sys::window().unwrap().performance().unwrap().now() - start;

    vec![gamma_time, bessel_time, erf_time, total_time]
}