scirs2-core 0.4.2

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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
// Embedded systems support (v0.2.0) - no_std compatibility
// Enable no_std by default, opt-in to std with the "std" feature
#![cfg_attr(not(feature = "std"), no_std)]
#![recursion_limit = "512"]
// TODO: Remove dead code or justify why it's kept
#![allow(dead_code)]
// Clippy allow attributes for non-critical warnings
#![allow(clippy::empty_line_after_outer_attribute)]
#![allow(clippy::empty_line_after_doc_comments)]
#![allow(clippy::manual_clamp)]
#![allow(clippy::redundant_closure)]
#![allow(clippy::useless_format)]
#![allow(clippy::result_large_err)]
#![allow(clippy::manual_is_multiple_of)]
#![allow(clippy::manual_div_ceil)]
#![allow(clippy::enumerate_and_ignore)]
#![allow(clippy::redundant_pattern_matching)]
#![allow(clippy::or_fun_call)]
#![allow(clippy::unnecessary_lazy_evaluations)]
#![allow(clippy::needless_borrow)]
#![allow(clippy::derivable_impls)]
#![allow(clippy::new_without_default)]
#![allow(clippy::ptr_arg)]
#![allow(clippy::get_first)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::type_complexity)]
#![allow(clippy::assertions_on_constants)]
#![allow(clippy::needless_return)]
#![allow(clippy::unnecessary_cast)]
#![allow(clippy::manual_range_contains)]
#![allow(clippy::empty_line_after_outer_attribute_doc)]
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::excessive_precision)]
#![allow(clippy::needless_borrows_for_generic_args)]
#![allow(clippy::empty_line_after_outer_attr)]
#![allow(clippy::unused_enumerate_index)]
#![allow(clippy::unwrap_or_default)]

//! # SciRS2 Core - Foundation for Scientific Computing in Rust
//!
//! **scirs2-core** is the foundational crate for the SciRS2 scientific computing ecosystem,
//! providing essential utilities, abstractions, and optimizations used by all SciRS2 modules.
//!
//! ## 🎯 Design Philosophy
//!
//! - **Zero-Cost Abstractions**: Performance without compromising safety
//! - **Layered Architecture**: Clear separation between interface and implementation
//! - **Policy Compliance**: Enforce [SciRS2 POLICY](https://github.com/cool-japan/scirs/blob/master/SCIRS2_POLICY.md) - only scirs2-core uses external dependencies directly
//! - **Production Ready**: Enterprise-grade error handling, diagnostics, and stability guarantees
//!
//! ## 🚀 Key Features
//!
//! ### Performance Acceleration
//!
//! - **SIMD Operations**: CPU vector instructions (SSE, AVX, NEON) for array operations
//! - **Parallel Processing**: Multi-threaded execution with intelligent load balancing
//! - **GPU Acceleration**: Unified interface for CUDA, Metal, OpenCL, and WebGPU
//! - **Memory Efficiency**: Zero-copy views, memory-mapped arrays, adaptive chunking
//!
//! ### Core Utilities
//!
//! - **Error Handling**: ML-inspired diagnostics with recovery strategies
//! - **Validation**: Input checking with informative error messages
//! - **Caching**: Memoization and result caching for expensive computations
//! - **Profiling**: Performance monitoring and bottleneck detection
//!
//! ### Scientific Infrastructure
//!
//! - **Constants**: Physical and mathematical constants (via [`constants`] module)
//! - **Random Number Generation**: Consistent RNG interface across the ecosystem
//! - **Complex Numbers**: Type-safe complex arithmetic
//! - **Array Protocol**: Unified array interface for interoperability
//!
//! ## 📦 Module Overview
//!
//! ### Performance & Optimization
//!
//! | Module | Description |
//! |--------|-------------|
//! | `simd_ops` | SIMD-accelerated operations with platform detection |
//! | `parallel_ops` | Parallel processing primitives |
//! | `gpu` | GPU acceleration abstractions |
//! | `memory` | Memory management (buffer pools, zero-copy views) |
//! | `memory_efficient` | Memory-mapped arrays and lazy evaluation |
//!
//! ### Error Handling & Diagnostics
//!
//! | Module | Description |
//! |--------|-------------|
//! | `error` | Error types and traits |
//! | `validation` | Input validation utilities |
//! | `logging` | Structured logging for diagnostics |
//! | `profiling` | Performance profiling tools |
//!
//! ### Scientific Computing Basics
//!
//! | Module | Description |
//! |--------|-------------|
//! | `ndarray` | Unified ndarray interface (re-exports with SciRS2 extensions) |
//! | `numeric` | Generic numerical operations |
//! | `random` | Random number generation |
//! | `constants` | Mathematical and physical constants |
//!
//! ### Infrastructure
//!
//! | Module | Description |
//! |--------|-------------|
//! | `config` | Configuration management |
//! | `cache` | Result caching and memoization |
//! | `io` | I/O utilities |
//! | `cloud` | Cloud storage integration (S3, GCS, Azure) |
//!
//! ## 🚀 Quick Start
//!
//! ### Installation
//!
//! ```toml
//! [dependencies]
//! scirs2-core = { version = "0.4.2", features = ["simd", "parallel"] }
//! ```
//!
//! ### SIMD Operations
//!
//! ```rust
//! use scirs2_core::simd_ops::SimdUnifiedOps;
//! use ::ndarray::array;
//!
//! // Automatic SIMD acceleration based on CPU capabilities
//! let a = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
//! let b = array![8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0];
//!
//! // SIMD-accelerated element-wise addition
//! let result = f64::simd_add(&a.view(), &b.view());
//! ```
//!
//! ### Parallel Processing
//!
//! ```rust
//! # #[cfg(feature = "parallel")]
//! # {
//! use scirs2_core::parallel_ops::*;
//!
//! // Parallel iteration over chunks
//! let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
//! par_chunks(&data, 3).for_each(|chunk| {
//!     // Process each chunk in parallel
//!     let sum: i32 = chunk.iter().sum();
//!     println!("Chunk sum: {}", sum);
//! });
//! # }
//! ```
//!
//! ### Input Validation
//!
//! ```rust
//! use scirs2_core::validation::*;
//! use scirs2_core::error::CoreResult;
//! use ::ndarray::Array2;
//!
//! fn process_matrix(data: &Array2<f64>, k: usize) -> CoreResult<()> {
//!     // Validate inputs
//!     check_positive(k, "k")?;
//!     checkarray_finite(data, "data")?;
//!     checkshape(data, &[2, 3], "data")?;
//!
//!     // Process data...
//!     Ok(())
//! }
//! # let data = Array2::<f64>::zeros((2, 3));
//! # let _ = process_matrix(&data, 5);
//! ```
//!
//! ### Constants
//!
//! ```rust
//! use scirs2_core::constants::{math, physical};
//!
//! // Mathematical constants
//! let pi = math::PI;
//! let e = math::E;
//!
//! // Physical constants
//! let c = physical::SPEED_OF_LIGHT;
//! let h = physical::PLANCK;
//! ```
//!
//! ### Random Number Generation
//!
//! ```rust
//! # #[cfg(feature = "random")]
//! # {
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use scirs2_core::random::*;
//! use rand::{Rng, RngExt};
//!
//! // Standard distributions
//! let mut rng = rand::rng();
//! let normal = Normal::new(0.0, 1.0)?;
//! let samples: Vec<f64> = (0..1000).map(|_| normal.sample(&mut rng)).collect();
//!
//! let uniform = Uniform::new(0.0, 1.0)?;
//! let samples: Vec<f64> = (0..1000).map(|_| uniform.sample(&mut rng)).collect();
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ### Memory-Efficient Operations
//!
//! ```rust,no_run
//! # #[cfg(feature = "memory_efficient")]
//! # {
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use scirs2_core::memory_efficient::*;
//! use std::path::Path;
//! use ::ndarray::Array2;
//!
//! // Memory-mapped array for large datasets
//! let data = Array2::<f64>::zeros((1000, 1000));
//! let path = Path::new("/path/to/large_file.dat");
//! let mmap = create_mmap(&data, path, AccessMode::ReadWrite, 0)?;
//!
//! // Chunked operations for out-of-core processing
//! let result = chunk_wise_op(&data, |chunk| {
//!     chunk.mapv(|x| x * 2.0)
//! }, ChunkingStrategy::Fixed(10000))?;
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ## 🏗️ Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────┐
//! │     SciRS2 Ecosystem Modules            │
//! │  (linalg, stats, neural, vision, etc.)  │
//! └─────────────────────────────────────────┘
//!//! ┌─────────────────────────────────────────┐
//! │         scirs2-core (This Crate)        │
//! │  ┌────────────────────────────────────┐ │
//! │  │  High-Level Abstractions           │ │
//! │  │  - SIMD Operations                 │ │
//! │  │  - Parallel Processing             │ │
//! │  │  - GPU Acceleration                │ │
//! │  │  - Error Handling                  │ │
//! │  └────────────────────────────────────┘ │
//! │  ┌────────────────────────────────────┐ │
//! │  │  Core Utilities                    │ │
//! │  │  - Array Protocol                  │ │
//! │  │  - Validation                      │ │
//! │  │  - Memory Management               │ │
//! │  │  - Configuration                   │ │
//! │  └────────────────────────────────────┘ │
//! └─────────────────────────────────────────┘
//!//! ┌─────────────────────────────────────────┐
//! │     External Dependencies               │
//! │  (ndarray, rayon, BLAS, etc.)           │
//! │  ⚠️  Only scirs2-core depends directly  │
//! └─────────────────────────────────────────┘
//! ```
//!
//! ## 🎨 Feature Flags
//!
//! ### Performance Features
//!
//! - `simd` - SIMD acceleration (SSE, AVX, NEON)
//! - `parallel` - Multi-threaded execution via Rayon
//! - `gpu` - GPU acceleration (CUDA, Metal, OpenCL)
//!
//! ### Memory Management
//!
//! - `memory_management` - Advanced memory management (buffer pools, tracking)
//! - `memory_efficient` - Memory-mapped arrays and lazy evaluation
//! - `memory_metrics` - Memory usage tracking and profiling
//!
//! ### Scientific Computing
//!
//! - `array` - Scientific array types (MaskedArray, RecordArray)
//! - `random` - Random number generation
//! - `linalg` - Linear algebra with BLAS/LAPACK
//!
//! ### Development & Debugging
//!
//! - `validation` - Input validation (recommended for development)
//! - `logging` - Structured logging
//! - `profiling` - Performance profiling
//! - `testing` - Testing utilities
//!
//! ### Advanced Features
//!
//! - `cloud` - Cloud storage (S3, GCS, Azure)
//! - `jit` - Just-in-time compilation with LLVM
//! - `ml_pipeline` - ML pipeline integration
//!
//! ### Convenience
//!
//! - `default` - Commonly used features (parallel, validation)
//! - `all` - All features except backend-specific ones
//!
//! ## 🔒 SciRS2 Policy Compliance
//!
//! **Important**: scirs2-core is the **only** crate in the SciRS2 ecosystem that directly
//! depends on external crates like `ndarray`, `rand`, `rayon`, etc.
//!
//! All other SciRS2 crates **must** use abstractions provided by scirs2-core:
//!
//! ```rust,ignore
//! // ✅ CORRECT: Use scirs2-core abstractions
//! use scirs2_core::crate::ndarray::Array2;
//! use scirs2_core::random::Normal;
//! use scirs2_core::parallel_ops::*;
//!
//! // ❌ WRONG: Don't import external deps directly in other crates
//! // use ::ndarray::Array2;       // NO!
//! // use rand_distr::Normal;    // NO!
//! // use rayon::prelude::*;     // NO!
//! ```
//!
//! This policy ensures:
//! - Consistent APIs across the ecosystem
//! - Centralized version management
//! - Easy addition of SciRS2-specific extensions
//! - Better compile times through reduced duplication
//!
//! ## 📊 Performance
//!
//! scirs2-core provides multiple optimization levels:
//!
//! | Feature | Speedup | Use Case |
//! |---------|---------|----------|
//! | SIMD | 2-8x | Array operations, numerical computations |
//! | Parallel | 2-16x | Large datasets, independent operations |
//! | GPU | 10-100x | Massive parallelism, deep learning |
//! | Memory-mapped | ∞ | Out-of-core processing, datasets larger than RAM |
//!
//! ### Platform Detection
//!
//! Automatic CPU feature detection for optimal SIMD usage:
//!
//! ```rust
//! use scirs2_core::simd_ops::PlatformCapabilities;
//!
//! let caps = PlatformCapabilities::detect();
//! println!("SIMD available: {}", caps.simd_available);
//! println!("AVX2 available: {}", caps.avx2_available);
//! println!("GPU available: {}", caps.gpu_available);
//! ```
//!
//! ## 🔗 Integration
//!
//! scirs2-core integrates seamlessly with the Rust ecosystem:
//!
//! - **ndarray**: Core array operations
//! - **num-traits**: Generic numeric operations
//! - **rayon**: Parallel processing
//! - **BLAS/LAPACK**: Optimized linear algebra
//!
//! ## 🔒 Version
//!
//! Current version: **0.4.2**
//!
//! ## 📚 Examples
//!
//! See the [examples directory](https://github.com/cool-japan/scirs/tree/master/scirs2-core/examples)
//! for more detailed usage examples.

// Use alloc when available but not in std mode
#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;

// Re-export std as alloc when std is enabled for compatibility
#[cfg(feature = "std")]
extern crate std;

pub mod api_freeze;
pub mod api_reference;
pub mod apiversioning;
#[cfg(feature = "array")]
pub mod array;
pub mod array_protocol;
#[cfg(feature = "arrow")]
pub mod arrow_compat;
#[cfg(feature = "types")]
pub mod batch_conversions;
/// Bioinformatics utilities: sequence analysis, alignment, statistics, phylogenetics.
pub mod bioinformatics;
#[cfg(feature = "cache")]
pub mod cache;
pub mod cache_ops;
pub mod chunking;
#[cfg(feature = "cloud")]
pub mod cloud;
pub mod concurrent;
pub mod config;
pub mod constants;
pub mod distributed;
pub mod ecosystem;
pub mod error;
pub mod out_of_core;
pub mod streaming_stats;
// Financial computing extensions (v0.3.0) — option pricing, fixed income, risk, portfolio analytics
pub mod finance;
// Computational physics tools (v0.3.0) — classical, thermodynamics, electrodynamics, quantum
pub mod physics;
// C ABI FFI module for calling SciRS2 from C, Julia, and other languages (v0.3.0)
#[cfg(feature = "ffi")]
pub mod ffi;
// Cross-module integration framework (v0.2.0)
#[cfg(feature = "gpu")]
pub mod gpu;
#[cfg(feature = "gpu")]
pub mod gpu_registry;
pub mod integration;
pub mod io;
#[cfg(feature = "jit")]
pub mod jit;
#[cfg(feature = "linalg")]
pub mod linalg;
#[cfg(feature = "logging")]
pub mod logging;
#[cfg(feature = "memory_management")]
pub mod memory;
#[cfg(feature = "memory_efficient")]
pub mod memory_efficient;
pub mod metrics;
#[cfg(feature = "ml_pipeline")]
pub mod ml_pipeline;
pub mod ndarray;
pub mod ndarray_ext;
pub mod numeric;
#[cfg(feature = "parallel")]
pub mod parallel;
#[cfg(feature = "parallel")]
pub mod parallel_ops;
pub mod performance;
pub mod performance_optimization;
#[cfg(feature = "profiling")]
pub mod profiling;
#[cfg(feature = "python")]
pub mod python;
#[cfg(feature = "random")]
pub mod random;
pub mod resource;
// Cross-language serialization protocol for .scirs2 files (v0.3.0 Phase 9.2)
#[cfg(feature = "serialization")]
pub mod serialization;
#[cfg(feature = "simd")]
pub mod simd;
pub mod simd_aligned;
pub mod simd_ops;
#[cfg(feature = "simd")]
pub mod simd_ops_polynomial;
#[cfg(feature = "testing")]
pub mod testing;
// Universal Functions (ufuncs) module
pub mod error_templates;
pub mod safe_ops;
#[cfg(feature = "types")]
pub mod types;
#[cfg(feature = "ufuncs")]
pub mod ufuncs;
pub mod units;
pub mod utils;
pub mod validation;

// Data preprocessing, splitting, type conversions, and string processing (v0.3.0)
pub mod data_split;
pub mod preprocessing;
pub mod string_ops;
pub mod type_convert;

// IDE integration: ergonomic builder patterns for array construction (v0.3.0)
pub mod builders;

// IDE integration: shorthand matrix/vector operations (v0.3.0)
pub mod ops;

// Embedded systems support (v0.2.0)
#[cfg(feature = "embedded")]
pub mod embedded;
#[cfg(feature = "fixed-point")]
pub mod fixed_point;

// Production-level features for enterprise deployments
pub mod enterprise;
pub mod observability;
pub mod stability;
pub mod versioning;

// Advanced optimization and AI features
pub mod neural_architecture_search;
pub mod quantum_optimization;

// Advanced Mode Ecosystem Integration
pub mod advanced_ecosystem_integration;

// Advanced JIT Compilation Framework
pub mod advanced_jit_compilation;

// Advanced Distributed Computing Framework
pub mod advanced_distributed_computing;

// Advanced Cloud Storage Framework
// pub mod distributed_storage; // Module not implemented yet

// Advanced Tensor Cores and Automatic Kernel Tuning Framework
pub mod advanced_tensor_cores;

// Tensor cores optimization modules
#[cfg(feature = "gpu")]
pub mod tensor_cores;

// Benchmarking module
#[cfg(feature = "benchmarking")]
pub mod benchmarking;

// Lightweight benchmarking utilities (always available, no feature gate)
pub mod bench_utils;

// Progress tracking for iterative algorithms (always available, no feature gate)
pub mod progress;

// SciPy/NumPy migration guide and equivalence reference.
pub mod scipy_migration;

// Arithmetic operations
pub mod arithmetic;
// Concurrent/lock-free collections (RRB-vector, bit vector, flat map, tiny vec)
pub mod collections;
// Advanced data structures (RRB-tree persistent vector, etc.)
pub mod data_structures;
// Reactive signals and dataflow
pub mod reactive;
// Task dependency graph with scheduling
pub mod task_graph;
// Lock-free concurrent data structures (skiplist, B-tree)
pub mod lock_free;
// GPU/CPU memory pool management
pub mod memory_pool;
// NUMA-aware task scheduling
pub mod numa_scheduler;
// Platform compatibility utilities
pub mod platform_compat;
// Structured logging framework
pub mod structured_logging;

// Re-exports
#[cfg(feature = "cache")]
pub use crate::cache::*;
#[cfg(feature = "cloud")]
pub use crate::cloud::{
    CloudConfig, CloudCredentials, CloudError, CloudObjectMetadata, CloudProvider,
    CloudStorageClient, EncryptionConfig, EncryptionMethod, HttpMethod, ListResult,
    TransferOptions,
};
pub use crate::config::production as config_production;
pub use crate::config::unified as config_unified;
pub use crate::config::{get_config, get_config_value, set_config_value, Config, ConfigValue};
pub use crate::constants::{math, physical, prefixes};
#[allow(ambiguous_glob_reexports)]
pub use crate::error::*;

// Re-export the array! macro for convenient array creation
// This addresses the common pain point where users expect array! to be available
// directly from scirs2_core instead of requiring import from scirs2_autograd
//
// # Example
//
// ```rust
// use scirs2_core::array;
//
// let matrix = array![[1, 2, 3], [4, 5, 6]];
// assert_eq!(matrix.shape(), &[2, 3]);
// ```
#[cfg(feature = "gpu")]
pub use crate::gpu::*;
pub use crate::io::*;
#[cfg(feature = "jit")]
pub use crate::jit::DataType as JitDataType;
#[cfg(feature = "jit")]
pub use crate::jit::{
    CompiledKernel, ExecutionProfile, JitBackend, JitCompiler, JitConfig, JitError, KernelLanguage,
    KernelSource, OptimizationLevel, TargetArchitecture,
};
#[cfg(feature = "logging")]
pub use crate::logging::*;
#[cfg(feature = "memory_management")]
pub use crate::memory::{
    format_memory_report, generate_memory_report, global_buffer_pool, track_allocation,
    track_deallocation, track_resize, BufferPool, ChunkProcessor, ChunkProcessor2D,
    GlobalBufferPool, ZeroCopyView,
};

#[cfg(feature = "mmap")]
pub use crate::memory::{MmapArray, MmapElement, MmapError};
// Legacy re-export from ndarray_ext (kept for backward compatibility)
pub use crate::ndarray_ext::array as array_legacy;

// Complete ndarray functionality through the unified module
// Use ndarray_ext which has the correct re-exports from ::ndarray
pub use crate::ndarray_ext::{
    arr1,
    arr2,
    // Essential macros - now available at crate root
    array,
    s,
    // Common types for convenience
    Array,
    Array1,
    Array2,
    ArrayD,
    ArrayView,
    ArrayView1,
    ArrayView2,
    ArrayViewMut,
    Axis,
    Ix1,
    Ix2,
    IxDyn,
};

#[cfg(feature = "leak_detection")]
pub use crate::memory::{
    LeakCheckGuard, LeakDetectionConfig, LeakDetector, LeakReport, LeakType, MemoryCheckpoint,
    MemoryLeak, ProfilerTool,
};

#[cfg(feature = "memory_efficient")]
pub use crate::memory_efficient::{
    chunk_wise_binary_op, chunk_wise_op, chunk_wise_reduce, create_disk_array, create_mmap,
    create_temp_mmap, diagonal_view, evaluate, load_chunks, open_mmap, register_fusion,
    transpose_view, view_as, view_mut_as, AccessMode, AdaptiveChunking, AdaptiveChunkingBuilder,
    AdaptiveChunkingParams, AdaptiveChunkingResult, ArithmeticOps, BroadcastOps, ChunkIter,
    ChunkedArray, ChunkingStrategy, DiskBackedArray, FusedOp, LazyArray, LazyOp, LazyOpKind,
    MemoryMappedArray, MemoryMappedChunkIter, MemoryMappedChunks, MemoryMappedSlice,
    MemoryMappedSlicing, OpFusion, OutOfCoreArray, ViewMut, ZeroCopyOps,
};

// Compression-related types are only available with the memory_compression feature
#[cfg(feature = "memory_compression")]
pub use crate::memory_efficient::{
    CompressedMemMapBuilder, CompressedMemMappedArray, CompressionAlgorithm,
};

// Re-export the parallel memory-mapped array capabilities
#[cfg(all(feature = "memory_efficient", feature = "parallel"))]
pub use crate::memory_efficient::MemoryMappedChunksParallel;

#[cfg(feature = "array")]
pub use crate::array::{
    is_masked, mask_array, masked_equal, masked_greater, masked_inside, masked_invalid,
    masked_less, masked_outside, masked_where, record_array_from_typed_arrays,
    record_array_fromrecords, ArrayError, FieldValue, MaskedArray, Record, RecordArray, NOMASK,
};

#[cfg(feature = "memory_metrics")]
pub use crate::memory::metrics::{
    clear_snapshots,
    compare_snapshots,
    // Utility functions
    format_bytes,
    format_duration,
    take_snapshot,
    MemoryEvent,
    MemoryEventType,
    // Core metrics types
    MemoryMetricsCollector,
    MemoryMetricsConfig,
    // Memory snapshots and leak detection
    MemorySnapshot,
    SnapshotDiff,
    // Tracked memory components
    TrackedBufferPool,
    TrackedChunkProcessor,
    TrackedChunkProcessor2D,
};

#[cfg(feature = "types")]
pub use crate::batch_conversions::{
    utils as batch_utils, BatchConversionConfig, BatchConversionResult, BatchConverter,
    ElementConversionError,
};
#[cfg(all(feature = "memory_metrics", feature = "gpu"))]
pub use crate::memory::metrics::{setup_gpu_memory_tracking, TrackedGpuBuffer, TrackedGpuContext};
pub use crate::metrics::{
    global_healthmonitor, global_metrics_registry, Counter, Gauge, HealthCheck, HealthMonitor,
    Histogram, MetricPoint, MetricType, MetricValue, Timer,
};
#[cfg(feature = "ml_pipeline")]
pub use crate::ml_pipeline::DataType as MLDataType;
#[cfg(feature = "ml_pipeline")]
pub use crate::ml_pipeline::{
    DataBatch, DataSample, FeatureConstraint, FeatureSchema, FeatureTransformer, FeatureValue,
    MLPipeline, MLPipelineError, ModelPredictor, ModelType, PipelineConfig, PipelineMetrics,
    PipelineNode, TransformType,
};
pub use crate::numeric::*;
#[cfg(feature = "parallel")]
pub use crate::parallel::*;
#[cfg(feature = "parallel")]
pub use crate::parallel_ops::{
    is_parallel_enabled, num_threads, par_chunks, par_chunks_mut, par_join, par_scope,
};
// Re-export all parallel traits and types
#[cfg(feature = "parallel")]
pub use crate::parallel_ops::*;
#[cfg(feature = "profiling")]
pub use crate::profiling::{profiling_memory_tracker, Profiler};
#[cfg(feature = "random")]
#[allow(ambiguous_glob_reexports)]
pub use crate::random::*;
pub use crate::resource::{
    get_available_memory, get_performance_tier, get_recommended_chunk_size,
    get_recommended_thread_count, get_system_resources, get_total_memory, is_gpu_available,
    is_simd_supported, DiscoveryConfig, PerformanceTier, ResourceDiscovery, SystemResources,
};
#[cfg(feature = "simd")]
pub use crate::simd::*;
#[cfg(feature = "testing")]
pub use crate::testing::{TestConfig, TestResult, TestRunner, TestSuite};
#[cfg(feature = "types")]
pub use crate::types::{convert, ComplexConversionError, ComplexExt, ComplexOps};

// Re-export complex number types for SCIRS2 POLICY compliance
pub use num_complex::{Complex, Complex32, Complex64};

// Re-export RNG types for SCIRS2 POLICY compliance
pub use crate::units::{
    convert, global_unit_registry, unit_value, Dimension, UnitDefinition, UnitRegistry, UnitSystem,
    UnitValue,
};
pub use crate::utils::*;
pub use crate::validation::production as validation_production;
pub use crate::validation::{
    check_finite, check_in_bounds, check_positive, checkarray_finite, checkshape,
};
pub use rand_chacha::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};

// ================================
// IDE Integration: Builder Patterns
// ================================

// Re-export builders at crate root for ergonomic access
pub use crate::builders::{ArrayBuilder, MatrixBuilder, VectorBuilder};

// ================================
// IDE Integration: Ergonomic Ops
// ================================

// Re-export matrix/vector ops at crate root for ergonomic access
pub use crate::ops::{block_diag, dot as mat_dot, hstack, kron, outer, vstack};

// ================================
// Prelude Module
// ================================

/// Convenient re-exports of commonly used items
///
/// Import this module to get quick access to the most frequently used
/// types, traits, and functions in the SciRS2 ecosystem without needing
/// to remember specific import paths.
///
/// # Example
///
/// ```rust
/// use scirs2_core::prelude::*;
///
/// let data = array![[1.0, 2.0], [3.0, 4.0]];
/// ```
pub mod prelude;

#[cfg(feature = "data_validation")]
pub use crate::validation::data::DataType as ValidationDataType;
#[cfg(feature = "data_validation")]
pub use crate::validation::data::{
    Constraint, FieldDefinition, ValidationConfig, ValidationError, ValidationResult,
    ValidationRule, ValidationSchema, Validator,
};

// Production-level feature re-exports
pub use crate::observability::{audit, tracing};
pub use crate::stability::{
    global_stability_manager, ApiContract, BreakingChange, BreakingChangeType, ConcurrencyContract,
    MemoryContract, NumericalContract, PerformanceContract, StabilityGuaranteeManager,
    StabilityLevel, UsageContext,
};
pub use crate::versioning::{
    compatibility, deprecation, migration, negotiation, semantic, ApiVersion, CompatibilityLevel,
    SupportStatus, Version, VersionManager,
};

// Advanced optimization and AI feature re-exports
pub use crate::neural_architecture_search::{
    ActivationType, Architecture, ArchitecturePerformance, ConnectionType, HardwareConstraints,
    LayerType, NASStrategy, NeuralArchitectureSearch, OptimizationObjectives, OptimizerType,
    SearchResults, SearchSpace,
};
pub use crate::quantum_optimization::{
    OptimizationResult, QuantumOptimizer, QuantumParameters, QuantumState, QuantumStrategy,
};

// Cross-module integration re-exports (v0.2.0)
pub use crate::integration::{
    // Configuration
    config::{
        global_config, update_global_config, DiagnosticsConfig, EcosystemConfig,
        EcosystemConfigBuilder, LogLevel, MemoryConfig, ModuleConfig, NumericConfig,
        ParallelConfig, Precision, PrecisionConfig,
    },
    // Type conversion
    conversion::{
        ArrayConvert, ConversionError, ConversionOptions, ConversionResult, CrossModuleConvert,
        DataFlowConverter, LosslessConvert, LossyConvert, TypeAdapter,
    },
    // Interface traits
    traits::{
        ApiVersion as IntegrationApiVersion, Capability, Composable, Configurable,
        CrossModuleOperator, DataConsumer, DataProvider, Diagnosable, DiagnosticInfo,
        DiagnosticLevel, Identifiable, ModuleCapability, ModuleInterface, ResourceAware,
        ResourceUsage, Serializable, VersionedInterface,
    },
    // Zero-copy operations
    zero_copy::{
        Alignment, ArrayBridge, BorrowedArray, BufferMut, BufferRef, ContiguousMemory,
        MemoryLayout, OwnedArray, SharedArrayView, SharedArrayViewMut, TypedBuffer, ZeroCopyBuffer,
        ZeroCopySlice,
    },
    // Error types
    IntegrationError,
    IntegrationResult,
};

// Advanced JIT Compilation re-exports
// pub use crate::advanced_jit_compilation::{
//     AdaptiveCodeGenerator, CompilationStatistics, JitAnalytics, JitCompilerConfig, JitProfiler,
//     KernelCache, KernelMetadata, KernelPerformance, LlvmCompilationEngine, OptimizationResults,
//     PerformanceImprovement, RuntimeOptimizer, advancedJitCompiler,
// }; // Missing module

// Advanced Cloud Storage re-exports
// pub use crate::distributed_storage::{
//     AdaptiveStreamingEngine, CloudPerformanceAnalytics, CloudProviderConfig, CloudProviderId,
//     CloudProviderType, CloudSecurityManager, CloudStorageMonitoring, CloudStorageProvider,
//     DataOptimizationEngine, DownloadRequest, DownloadResponse, IntelligentCacheSystem,
//     ParallelTransferManager, StreamRequest, advancedCloudConfig,
//     advancedCloudStorageCoordinator, UploadRequest, UploadResponse,
// };

// Benchmarking re-exports
#[cfg(feature = "benchmarking")]
pub use crate::benchmarking::{
    BenchmarkConfig, BenchmarkMeasurement, BenchmarkResult, BenchmarkRunner, BenchmarkStatistics,
    BenchmarkSuite,
};

/// ``SciRS2`` core version information
pub const fn _version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Initialize the library (called automatically)
#[doc(hidden)]
#[allow(dead_code)]
pub fn __init() {
    use std::sync::Once;
    static INIT: Once = Once::new();

    INIT.call_once(|| {
        // Initialize API freeze registry
        crate::api_freeze::initialize_api_freeze();
    });
}

// Ensure initialization happens
#[doc(hidden)]
#[used]
#[cfg_attr(target_os = "linux", link_section = ".init_array")]
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
static INIT: extern "C" fn() = {
    extern "C" fn __init_wrapper() {
        __init();
    }
    __init_wrapper
};