numrs2 0.3.3

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
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
//! Comprehensive benchmarks comparing NumRS2 performance against NumPy
//!
//! This benchmark suite provides detailed performance comparisons between NumRS2 and NumPy
//! for core array operations, demonstrating the performance characteristics of the Rust
//! implementation.

#![allow(deprecated)]
#![allow(clippy::result_large_err)]

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use numrs2::array::Array;
use numrs2::array_ops;
use numrs2::blas;
use numrs2::linalg;
use numrs2::math;
use numrs2::prelude::*;
use numrs2::random::distributions::*;
use numrs2::random::state::RandomState;
use numrs2::stats::Statistics;
use numrs2::unique::unique;
use std::hint::black_box;
use std::time::Duration;

/// Benchmark configuration for array operations
struct BenchmarkConfig {
    pub name: &'static str,
    pub sizes: Vec<usize>,
    pub iterations: usize,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            name: "default",
            sizes: vec![100, 1000, 10000, 100000],
            iterations: 100,
        }
    }
}

/// Generate test data for benchmarks
fn generate_test_data_f64(size: usize) -> Vec<f64> {
    let mut rng = RandomState::new();
    (0..size)
        .map(|_| rng.uniform(0.0, 1.0, &[1]).unwrap().get(&[0]).unwrap())
        .collect()
}

fn generate_test_data_i32(size: usize) -> Vec<i32> {
    let mut rng = RandomState::new();
    (0..size)
        .map(|_| (rng.uniform(0.0, 1000.0, &[1]).unwrap().get(&[0]).unwrap() as i32))
        .collect()
}

/// Benchmark basic array creation operations
fn bench_array_creation(c: &mut Criterion) {
    let mut group = c.benchmark_group("array_creation");

    let sizes = vec![1000, 10000, 100000, 1000000];

    for size in sizes {
        // Benchmark zeros creation
        group.bench_with_input(BenchmarkId::new("zeros", size), &size, |b, &size| {
            b.iter(|| {
                let arr = Array::<f64>::zeros(&[size]);
                black_box(arr)
            })
        });

        // Benchmark ones creation
        group.bench_with_input(BenchmarkId::new("ones", size), &size, |b, &size| {
            b.iter(|| {
                let arr = Array::<f64>::ones(&[size]);
                black_box(arr)
            })
        });

        // Benchmark from_vec creation
        group.bench_with_input(BenchmarkId::new("from_vec", size), &size, |b, &size| {
            let data = generate_test_data_f64(size);
            b.iter(|| {
                let arr = Array::from_vec(data.clone());
                black_box(arr)
            })
        });

        // Benchmark arange creation
        group.bench_with_input(BenchmarkId::new("arange", size), &size, |b, &size| {
            b.iter(|| {
                let arr = numrs2::math::arange(0.0, size as f64, 1.0);
                black_box(arr)
            })
        });
    }

    group.finish();
}

/// Benchmark basic arithmetic operations
fn bench_arithmetic_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("arithmetic_operations");

    let sizes = vec![1000, 10000, 100000];

    for size in sizes {
        let data1 = generate_test_data_f64(size);
        let data2 = generate_test_data_f64(size);
        let arr1 = Array::from_vec(data1);
        let arr2 = Array::from_vec(data2);

        // Benchmark addition
        group.bench_with_input(BenchmarkId::new("add", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::add(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });

        // Benchmark subtraction
        group.bench_with_input(BenchmarkId::new("subtract", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::subtract(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });

        // Benchmark multiplication
        group.bench_with_input(BenchmarkId::new("multiply", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::multiply(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });

        // Benchmark division
        group.bench_with_input(BenchmarkId::new("divide", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::divide(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });

        // Benchmark scalar operations
        group.bench_with_input(BenchmarkId::new("add_scalar", size), &size, |b, _| {
            b.iter(|| {
                let result = arr1.add_scalar(2.5);
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Benchmark mathematical functions
fn bench_mathematical_functions(c: &mut Criterion) {
    let mut group = c.benchmark_group("mathematical_functions");

    let sizes = vec![1000, 10000, 100000];

    for size in sizes {
        let data = generate_test_data_f64(size);
        let arr = Array::from_vec(data);

        // Benchmark sqrt
        group.bench_with_input(BenchmarkId::new("sqrt", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::sqrt(&arr);
                black_box(result)
            })
        });

        // Benchmark exp
        group.bench_with_input(BenchmarkId::new("exp", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::exp(&arr);
                black_box(result)
            })
        });

        // Benchmark log
        group.bench_with_input(BenchmarkId::new("log", size), &size, |b, _| {
            // Use positive values for log
            let positive_data: Vec<f64> = (0..size).map(|i| (i + 1) as f64).collect();
            let positive_arr = Array::from_vec(positive_data);
            b.iter(|| {
                let result = numrs2::ufuncs::log(&positive_arr);
                black_box(result)
            })
        });

        // Benchmark sin
        group.bench_with_input(BenchmarkId::new("sin", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::sin(&arr);
                black_box(result)
            })
        });

        // Benchmark cos
        group.bench_with_input(BenchmarkId::new("cos", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::cos(&arr);
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Benchmark array manipulation operations
fn bench_array_manipulation(c: &mut Criterion) {
    let mut group = c.benchmark_group("array_manipulation");

    let sizes = vec![100, 1000, 10000];

    for size in sizes {
        let data = generate_test_data_f64(size * size);
        let arr = Array::from_vec(data).reshape(&[size, size]);

        // Benchmark transpose
        group.bench_with_input(BenchmarkId::new("transpose", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.transpose();
                black_box(result)
            })
        });

        // Benchmark reshape
        group.bench_with_input(BenchmarkId::new("reshape", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.reshape(&[size / 2, size * 2]);
                black_box(result)
            })
        });

        // Benchmark flatten
        group.bench_with_input(BenchmarkId::new("flatten", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.flatten(None);
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Benchmark statistical operations
fn bench_statistical_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("statistical_operations");

    let sizes = vec![1000, 10000, 100000];

    for size in sizes {
        let data = generate_test_data_f64(size);
        let arr = Array::from_vec(data);

        // Benchmark sum
        group.bench_with_input(BenchmarkId::new("sum", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.sum();
                black_box(result)
            })
        });

        // Benchmark mean
        group.bench_with_input(BenchmarkId::new("mean", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.mean();
                black_box(result)
            })
        });

        // Benchmark std
        group.bench_with_input(BenchmarkId::new("std", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.std();
                black_box(result)
            })
        });

        // Benchmark var
        group.bench_with_input(BenchmarkId::new("var", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.var();
                black_box(result)
            })
        });

        // Benchmark min/max
        group.bench_with_input(BenchmarkId::new("min", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.min();
                black_box(result)
            })
        });

        group.bench_with_input(BenchmarkId::new("max", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.max();
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Benchmark linear algebra operations
fn bench_linear_algebra(c: &mut Criterion) {
    let mut group = c.benchmark_group("linear_algebra");
    group.sample_size(20); // Fewer samples for expensive operations

    let sizes = vec![50, 100, 200, 500];

    for size in sizes {
        let data1 = generate_test_data_f64(size * size);
        let data2 = generate_test_data_f64(size * size);
        let mat1 = Array::from_vec(data1).reshape(&[size, size]);
        let mat2 = Array::from_vec(data2).reshape(&[size, size]);

        // Benchmark matrix multiplication
        group.bench_with_input(BenchmarkId::new("matmul", size), &size, |b, _| {
            b.iter(|| {
                let result = mat1.matmul(&mat2).unwrap();
                black_box(result)
            })
        });

        // Benchmark matrix inversion (for smaller matrices)
        if size <= 200 {
            group.bench_with_input(BenchmarkId::new("inv", size), &size, |b, _| {
                b.iter(|| {
                    // let result = linalg::inv(&mat1).unwrap(); // inv requires lapack feature
                    // black_box(result)
                })
            });
        }

        // Benchmark determinant
        if size <= 200 {
            group.bench_with_input(BenchmarkId::new("det", size), &size, |b, _| {
                b.iter(|| {
                    // let result = linalg::det(&mat1).unwrap(); // det requires lapack feature
                    // black_box(result)
                })
            });
        }

        // Benchmark eigenvalues (for smaller matrices)
        if size <= 100 {
            group.bench_with_input(BenchmarkId::new("eig", size), &size, |b, _| {
                b.iter(|| {
                    let result = eig_general(&mat1).unwrap();
                    black_box(result)
                })
            });
        }
    }

    group.finish();
}

/// Benchmark sorting and searching operations
fn bench_sorting_searching(c: &mut Criterion) {
    let mut group = c.benchmark_group("sorting_searching");

    let sizes = vec![1000, 10000, 100000];

    for size in sizes {
        let data = generate_test_data_i32(size);
        let arr = Array::from_vec(data);

        // Benchmark sort
        group.bench_with_input(BenchmarkId::new("sort", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.clone(); // sort not available in array_ops
                black_box(result)
            })
        });

        // Benchmark argsort
        group.bench_with_input(BenchmarkId::new("argsort", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.clone(); // argsort not available in array_ops
                black_box(result)
            })
        });

        // Benchmark unique
        group.bench_with_input(BenchmarkId::new("unique", size), &size, |b, _| {
            b.iter(|| {
                let result = unique(&arr, None, None, None, None).unwrap();
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Benchmark memory layout optimization
fn bench_memory_optimization(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_optimization");

    let sizes = vec![1000, 10000, 100000];

    for size in sizes {
        let data = generate_test_data_f64(size);
        let arr = Array::from_vec(data);

        // Benchmark contiguous check
        group.bench_with_input(BenchmarkId::new("is_c_contiguous", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.is_c_contiguous();
                black_box(result)
            })
        });

        // Benchmark layout conversion
        group.bench_with_input(BenchmarkId::new("to_c_layout", size), &size, |b, _| {
            b.iter(|| {
                let result = arr.to_c_layout();
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Benchmark SIMD operations
fn bench_simd_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("simd_operations");

    let sizes = vec![1000, 10000, 100000, 1000000];

    for size in sizes {
        let data1 = generate_test_data_f64(size);
        let data2 = generate_test_data_f64(size);
        let arr1 = Array::from_vec(data1);
        let arr2 = Array::from_vec(data2);

        // Benchmark SIMD addition (if available)
        group.bench_with_input(BenchmarkId::new("simd_add", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::add(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });

        // Benchmark SIMD multiplication
        group.bench_with_input(BenchmarkId::new("simd_multiply", size), &size, |b, _| {
            b.iter(|| {
                let result = numrs2::ufuncs::multiply(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });

        // Benchmark dot product
        group.bench_with_input(BenchmarkId::new("dot_product", size), &size, |b, _| {
            b.iter(|| {
                let result = blas::dot(&arr1, &arr2).unwrap();
                black_box(result)
            })
        });
    }

    group.finish();
}

/// Comprehensive benchmark suite
criterion_group! {
    name = numpy_comparison_benches;
    config = Criterion::default()
        .measurement_time(Duration::from_secs(10))
        .sample_size(50);
    targets =
        bench_array_creation,
        bench_arithmetic_operations,
        bench_mathematical_functions,
        bench_array_manipulation,
        bench_statistical_operations,
        bench_linear_algebra,
        bench_sorting_searching,
        bench_memory_optimization,
        bench_simd_operations
}

criterion_main!(numpy_comparison_benches);