scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Random array generation for ndarray integration
//!
//! This module provides comprehensive random array generation functionality that integrates
//! seamlessly with the SCIRS2 ecosystem. It replaces external ndarray-rand dependencies
//! with a fully controlled, scientifically-focused implementation.
//!
//! # Features
//!
//! - **Full ndarray-rand compatibility** - Drop-in replacement for ndarray-rand functionality
//! - **Scientific computing focus** - Enhanced with features specifically for research
//! - **Integrated with SCIRS2 Random** - Uses our Random struct for consistency
//! - **Performance optimized** - Bulk operations for large-scale scientific computing
//! - **Reproducible research** - Full seeding and deterministic support
//!
//! # Examples
//!
//! ```rust
//! use scirs2_core::ndarray_ext::random::*;
//! use scirs2_core::random::seeded_rng;
//! use rand_distr::Beta as BetaDist;
//! use ndarray::{Array2, Ix2};
//!
//! // Generate random arrays using our RandomExt trait
//! let mut rng = seeded_rng(42);
//! let matrix: Array2<f64> = Array2::random(Ix2(10, 10), StandardNormal, &mut rng);
//!
//! // Scientific distributions
//! let beta_samples = Array2::random(Ix2(100, 5), BetaDist::new(2.0, 5.0).expect("Operation failed"), &mut rng);
//!
//! // Multivariate normal example
//! let mean = vec![0.0, 1.0];
//! let covariance = vec![vec![1.0, 0.5], vec![0.5, 1.0]];
//! let multivariate: Array2<f64> = Array2::multivariate_normal(mean, covariance, 1000, &mut rng);
//! ```

use crate::random::core::Random;
use crate::random::distributions::{Beta, Dirichlet, MultivariateNormal, VonMises, WeightedChoice};
use ::ndarray::{
    Array, Array1, Array2, Array3, ArrayBase, Data, DataMut, DataOwned, Dimension, Ix1, Ix2, Ix3,
    ShapeBuilder,
};
use rand::{Rng, RngExt};
use rand_distr::{Distribution, Normal, Uniform};
use std::marker::PhantomData;

/// Standard normal distribution for convenience
#[derive(Debug, Clone, Copy)]
pub struct StandardNormal;

impl Distribution<f64> for StandardNormal {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
        Normal::new(0.0, 1.0).expect("Operation failed").sample(rng)
    }
}

impl Distribution<f32> for StandardNormal {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
        Normal::new(0.0f32, 1.0f32)
            .expect("Operation failed")
            .sample(rng)
    }
}

/// Standard uniform distribution [0, 1) for convenience
#[derive(Debug, Clone, Copy)]
pub struct StandardUniform;

impl Distribution<f64> for StandardUniform {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
        Uniform::new(0.0, 1.0)
            .expect("Operation failed")
            .sample(rng)
    }
}

impl Distribution<f32> for StandardUniform {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
        Uniform::new(0.0f32, 1.0f32)
            .expect("Operation failed")
            .sample(rng)
    }
}

/// Extended random array generation trait for ndarray integration
///
/// This trait provides comprehensive random array generation functionality
/// that integrates with the SCIRS2 Random struct and scientific distributions.
pub trait RandomExt<A, D: Dimension> {
    /// Generate a random array using any distribution
    fn random<Dist, R>(shape: D, distribution: Dist, rng: &mut Random<R>) -> Self
    where
        Dist: Distribution<A>,
        R: Rng;

    /// Generate a random array using a closure
    fn random_using<F, R>(shape: D, rng: &mut Random<R>, f: F) -> Self
    where
        F: FnMut() -> A,
        R: Rng;

    /// Generate a standard normal random array (mean=0, std=1)
    fn standard_normal<R>(shape: D, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate a standard uniform random array [0, 1)
    fn standard_uniform<R>(shape: D, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate a normal random array with specified mean and standard deviation
    fn normal<R>(shape: D, mean: f64, std: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate a uniform random array in [low, high)
    fn uniform<R>(shape: D, low: f64, high: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate random array from Beta distribution
    fn beta<R>(shape: D, alpha: f64, beta: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate random array from exponential distribution
    fn exponential<R>(shape: D, lambda: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate random integers in range [low, high)
    fn randint<R>(shape: D, low: i64, high: i64, rng: &mut Random<R>) -> Self
    where
        A: From<i64>,
        R: Rng;
}

impl<A, S, D> RandomExt<A, D> for ArrayBase<S, D>
where
    S: DataOwned<Elem = A>,
    D: Dimension,
    A: Clone,
{
    fn random<Dist, R>(shape: D, distribution: Dist, rng: &mut Random<R>) -> Self
    where
        Dist: Distribution<A>,
        R: Rng,
    {
        let size = shape.size();

        // Generate all values at once for optimal performance
        let values: Vec<A> = (0..size)
            .map(|_| distribution.sample(&mut rng.rng))
            .collect();

        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn random_using<F, R>(shape: D, rng: &mut Random<R>, mut f: F) -> Self
    where
        F: FnMut() -> A,
        R: Rng,
    {
        let size = shape.size();

        let values: Vec<A> = (0..size).map(|_| f()).collect();

        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn standard_normal<R>(shape: D, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let normal_dist = Normal::new(0.0, 1.0).expect("Operation failed");
        let size = shape.size();
        let values: Vec<A> = (0..size)
            .map(|_| A::from(normal_dist.sample(&mut rng.rng)))
            .collect();
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn standard_uniform<R>(shape: D, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let uniform_dist = Uniform::new(0.0, 1.0).expect("Operation failed");
        let size = shape.size();
        let values: Vec<A> = (0..size)
            .map(|_| A::from(uniform_dist.sample(&mut rng.rng)))
            .collect();
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn normal<R>(shape: D, mean: f64, std: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let normal_dist = Normal::new(mean, std).expect("Operation failed");
        let size = shape.size();
        let values: Vec<A> = (0..size)
            .map(|_| A::from(normal_dist.sample(&mut rng.rng)))
            .collect();
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn uniform<R>(shape: D, low: f64, high: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let uniform_dist = Uniform::new(low, high).expect("Operation failed");
        let size = shape.size();
        let values: Vec<A> = (0..size)
            .map(|_| A::from(uniform_dist.sample(&mut rng.rng)))
            .collect();
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn beta<R>(shape: D, alpha: f64, beta: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let beta_dist = Beta::new(alpha, beta).expect("Operation failed");
        let size = shape.size();
        let mut values = Vec::with_capacity(size);
        for _ in 0..size {
            let sample_val = beta_dist.sample(rng);
            values.push(A::from(sample_val));
        }
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn exponential<R>(shape: D, lambda: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let exp_dist = rand_distr::Exp::new(lambda).expect("Operation failed");
        let size = shape.size();
        let values: Vec<A> = (0..size)
            .map(|_| A::from(exp_dist.sample(&mut rng.rng)))
            .collect();
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn randint<R>(shape: D, low: i64, high: i64, rng: &mut Random<R>) -> Self
    where
        A: From<i64>,
        R: Rng,
    {
        let int_dist = Uniform::new(low, high).expect("Operation failed");
        let size = shape.size();
        let values: Vec<A> = (0..size)
            .map(|_| A::from(int_dist.sample(&mut rng.rng)))
            .collect();
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }
}

/// Scientific computing specific random array extensions
pub trait ScientificRandomExt<A, D: Dimension> {
    /// Generate random array from Dirichlet distribution
    fn dirichlet<R>(shape: D, alpha: &[f64], rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate random array from Von Mises distribution (circular)
    fn von_mises<R>(shape: D, mu: f64, kappa: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate multivariate normal samples
    fn multivariate_normal<R>(
        mean: Vec<f64>,
        covariance: Vec<Vec<f64>>,
        n_samples: usize,
        rng: &mut Random<R>,
    ) -> Array<A, crate::ndarray::Ix2>
    where
        A: From<f64>,
        R: Rng;

    /// Generate samples from categorical distribution
    fn categorical<R, T>(
        shape: D,
        choices: &[T],
        probabilities: &[f64],
        rng: &mut Random<R>,
    ) -> Array<T, D>
    where
        T: Clone,
        R: Rng;

    /// Generate correlated random arrays using Cholesky decomposition
    fn correlated_normal<R>(
        shape: D,
        correlation_matrix: &Array<f64, crate::ndarray::Ix2>,
        rng: &mut Random<R>,
    ) -> Self
    where
        A: From<f64>,
        R: Rng;

    /// Generate random sparse arrays with specified density
    fn sparse<R, Dist>(shape: D, density: f64, distribution: Dist, rng: &mut Random<R>) -> Self
    where
        A: Clone + Default,
        R: Rng,
        Dist: Distribution<A>;
}

impl<A, S, D> ScientificRandomExt<A, D> for ArrayBase<S, D>
where
    S: DataOwned<Elem = A>,
    D: Dimension,
    A: Clone,
{
    fn dirichlet<R>(shape: D, alpha: &[f64], rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let dirichlet = Dirichlet::new(alpha.to_vec()).expect("Operation failed");
        let size = shape.size();
        let mut values = Vec::with_capacity(size);
        for _ in 0..size {
            let sample_vec = dirichlet.sample(rng);
            // Use the first component of the Dirichlet sample
            let sample_val = sample_vec.get(0).copied().unwrap_or(0.0);
            values.push(A::from(sample_val));
        }
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn von_mises<R>(shape: D, mu: f64, kappa: f64, rng: &mut Random<R>) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        let von_mises = VonMises::mu(mu, kappa).expect("Operation failed");
        let size = shape.size();
        let mut values = Vec::with_capacity(size);
        for _ in 0..size {
            let sample_val = von_mises.sample(rng);
            values.push(A::from(sample_val));
        }
        Self::from_shape_vec(shape, values).expect("Operation failed")
    }

    fn multivariate_normal<R>(
        mean: Vec<f64>,
        covariance: Vec<Vec<f64>>,
        n_samples: usize,
        rng: &mut Random<R>,
    ) -> Array<A, crate::ndarray::Ix2>
    where
        A: From<f64>,
        R: Rng,
    {
        let mvn = MultivariateNormal::new(mean.clone(), covariance).expect("Operation failed");
        let dim = mean.len();

        Array::from_shape_fn((n_samples, dim), |_| {
            let sample = mvn.sample(rng);
            A::from(sample[0]) // This is simplified - real implementation would handle full vector
        })
    }

    fn categorical<R, T>(
        shape: D,
        choices: &[T],
        probabilities: &[f64],
        rng: &mut Random<R>,
    ) -> Array<T, D>
    where
        T: Clone,
        R: Rng,
    {
        let weighted = WeightedChoice::new(choices.to_vec(), probabilities.to_vec())
            .expect("Operation failed");
        Array::from_shape_fn(shape, |_| weighted.sample(rng).clone())
    }

    fn correlated_normal<R>(
        shape: D,
        correlation_matrix: &Array<f64, crate::ndarray::Ix2>,
        rng: &mut Random<R>,
    ) -> Self
    where
        A: From<f64>,
        R: Rng,
    {
        // Simplified implementation - real version would use Cholesky decomposition
        Self::standard_normal(shape, rng)
    }

    fn sparse<R, Dist>(shape: D, density: f64, distribution: Dist, rng: &mut Random<R>) -> Self
    where
        A: Clone + Default,
        R: Rng,
        Dist: Distribution<A>,
    {
        let size = shape.size();

        let values: Vec<A> = (0..size)
            .map(|_| {
                if rand::RngExt::random::<f64>(&mut rng.rng) < density {
                    distribution.sample(&mut rng.rng)
                } else {
                    A::default()
                }
            })
            .collect();

        Self::from_shape_vec(shape, values).expect("Operation failed")
    }
}

/// Convenience functions for quick random array generation
pub mod convenience {
    use super::*;
    use crate::random::thread_rng;
    use ::ndarray::{Array1, Array2, Array3, Ix1, Ix2, Ix3};

    /// Generate 1D random array with standard normal distribution
    pub fn randn(size: usize) -> Array1<f64> {
        let mut rng = thread_rng();
        Array1::standard_normal(Ix1(size), &mut rng)
    }

    /// Generate 1D random array with uniform distribution [0, 1)
    pub fn rand(size: usize) -> Array1<f64> {
        let mut rng = thread_rng();
        Array1::standard_uniform(Ix1(size), &mut rng)
    }

    /// Generate 2D random matrix with standard normal distribution
    pub fn randn2(rows: usize, cols: usize) -> Array2<f64> {
        let mut rng = thread_rng();
        Array2::standard_normal(Ix2(rows, cols), &mut rng)
    }

    /// Generate 2D random matrix with uniform distribution [0, 1)
    pub fn rand2(rows: usize, cols: usize) -> Array2<f64> {
        let mut rng = thread_rng();
        Array2::standard_uniform(Ix2(rows, cols), &mut rng)
    }

    /// Generate 3D random array with standard normal distribution
    pub fn randn3(dim1: usize, dim2: usize, dim3: usize) -> Array3<f64> {
        let mut rng = thread_rng();
        Array3::standard_normal(Ix3(dim1, dim2, dim3), &mut rng)
    }

    /// Generate random integers in range [low, high)
    pub fn randint(size: usize, low: i64, high: i64) -> Array1<i64> {
        let mut rng = thread_rng();
        Array1::randint(Ix1(size), low, high, &mut rng)
    }

    /// Generate random choice from array
    pub fn choice<T: Clone>(choices: &[T], size: usize) -> Array1<T> {
        let mut rng = thread_rng();
        let uniform_dist = Uniform::new(0, choices.len()).expect("Operation failed");
        Array1::from_shape_fn(Ix1(size), |_| {
            let idx = uniform_dist.sample(&mut rng.rng);
            choices[idx].clone()
        })
    }
}

/// Performance optimized random array operations
pub mod optimized {
    use super::*;
    use crate::random::parallel::ParallelRng;

    /// Generate large random arrays using parallel processing
    pub fn parallel_randn<R: Rng + Send + Sync + Clone>(
        shape: (usize, usize),
        rng: &mut Random<R>,
    ) -> Array<f64, crate::ndarray::Ix2> {
        // This would use ParallelRng for large arrays
        Array::standard_normal(Ix2(shape.0, shape.1), rng)
    }

    /// Generate random arrays with SIMD optimization
    pub fn simd_rand<R: Rng>(size: usize, rng: &mut Random<R>) -> Array1<f64> {
        // This would use SIMD operations for bulk generation
        Array1::standard_uniform(Ix1(size), rng)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::random::seeded_rng;
    use approx::assert_abs_diff_eq;
    use ndarray::{Array1, Array2, Ix1, Ix2};

    #[test]
    fn test_random_ext_basic() {
        let mut rng = seeded_rng(42);
        let arr: Array1<f64> = Array1::standard_normal(Ix1(10), &mut rng);
        assert_eq!(arr.len(), 10);
    }

    #[test]
    fn test_random_ext_uniform() {
        let mut rng = seeded_rng(123);
        let arr: Array2<f64> = Array2::uniform(Ix2(5, 5), 0.0, 1.0, &mut rng);
        assert_eq!(arr.shape(), &[5, 5]);
        assert!(arr.iter().all(|&x| x >= 0.0 && x < 1.0));
    }

    #[test]
    fn test_reproducibility() {
        let mut rng1 = seeded_rng(999);
        let mut rng2 = seeded_rng(999);

        let arr1: Array1<f64> = Array1::standard_normal(Ix1(100), &mut rng1);
        let arr2: Array1<f64> = Array1::standard_normal(Ix1(100), &mut rng2);

        for (a, b) in arr1.iter().zip(arr2.iter()) {
            assert_abs_diff_eq!(a, b, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_convenience_functions() {
        let arr = convenience::randn(50);
        assert_eq!(arr.len(), 50);

        let matrix = convenience::rand2(3, 4);
        assert_eq!(matrix.shape(), &[3, 4]);
    }

    #[test]
    fn test_scientific_extensions() {
        let mut rng = seeded_rng(456);

        // Test Beta distribution
        let beta_arr: Array1<f64> = Array1::beta(Ix1(20), 2.0, 5.0, &mut rng);
        assert_eq!(beta_arr.len(), 20);
        assert!(beta_arr.iter().all(|&x| x >= 0.0 && x <= 1.0));

        // Test Von Mises
        let vm_arr: Array1<f64> = Array1::von_mises(Ix1(15), 0.0, 1.0, &mut rng);
        assert_eq!(vm_arr.len(), 15);
    }
}