scirs2_core/
prelude.rs

1//! # SciRS2 Core Prelude
2//!
3//! The prelude module provides convenient access to the most commonly used items
4//! in the SciRS2 ecosystem. Import this module to get started quickly without
5//! needing to know the exact paths of all core functionality.
6//!
7//! ## Usage
8//!
9//! ```rust,no_run
10//! use scirs2_core::prelude::*;
11//!
12//! // Now you have access to all common functionality:
13//! let data = array![[1.0, 2.0], [3.0, 4.0]];  // Array creation
14//! let mean = data.mean().unwrap();             // Array operations
15//! let counter = Counter::new("requests".into()); // Metrics
16//! ```
17//!
18//! ## What's Included
19//!
20//! ### Array Types and Operations
21//! - `Array`, `Array1`, `Array2`, `ArrayD` - N-dimensional array types
22//! - `ArrayView`, `ArrayViewMut` - Array views
23//! - `Axis`, `Ix1`, `Ix2`, `IxDyn` - Shape and axis types
24//! - `array!`, `s!` - Convenient macros for array creation and slicing
25//!
26//! ### Random Number Generation
27//! - `random()` - Convenient random value generation
28//! - `Rng` - Random number generator trait
29//! - `SeedableRng` - Seedable RNG trait for reproducibility
30//! - `ChaCha8Rng`, `ChaCha12Rng`, `ChaCha20Rng` - Secure random number generators
31//! - Common distributions: `Normal`, `Uniform`, `Exponential`, `Gamma`, `Bernoulli`
32//!
33//! ### Validation Utilities
34//! - `check_positive()` - Validate positive values
35//! - `check_shape()` - Validate array shapes
36//! - `check_finite()` - Validate finite values
37//! - `check_in_bounds()` - Validate value bounds
38//!
39//! ### Metrics and Observability
40//! - `Counter` - Monotonically increasing metric
41//! - `Gauge` - Arbitrary up/down metric
42//! - `Histogram` - Distribution of values
43//! - `Timer` - Duration measurements
44//! - `global_metrics_registry()` - Global metrics collection
45//!
46//! ### Error Handling
47//! - `CoreError` - Main error type
48//! - `CoreResult<T>` - Result type alias
49//!
50//! ### Complex Numbers
51//! - `Complex`, `Complex32`, `Complex64` - Complex number types
52//!
53//! ## Examples
54//!
55//! ### Basic Array Operations
56//!
57//! ```rust
58//! use scirs2_core::prelude::*;
59//!
60//! // Create arrays
61//! let a = array![1.0, 2.0, 3.0, 4.0];
62//! let b = array![[1.0, 2.0], [3.0, 4.0]];
63//!
64//! // Array slicing
65//! let slice = b.slice(s![.., 0]);
66//!
67//! // Array operations
68//! let sum = a.sum();
69//! let mean = a.mean().unwrap();
70//! ```
71//!
72//! ### Random Number Generation
73//!
74//! ```rust,ignore
75//! use scirs2_core::prelude::*;
76//!
77//! // Quick random values
78//! let x: f64 = random();
79//! let y: bool = random();
80//!
81//! // Reproducible random generation
82//! let mut rng = ChaCha8Rng::seed_from_u64(42);
83//! let sample = rng.gen::<f64>();
84//!
85//! // Sample from distributions
86//! let normal = Normal::new(0.0, 1.0).unwrap();
87//! let value = normal.sample(&mut rng);
88//! ```
89//!
90//! ### Parameter Validation
91//!
92//! ```rust,ignore
93//! use scirs2_core::prelude::*;
94//!
95//! pub fn my_function(data: &Array2<f64>, k: usize) -> CoreResult<Array1<f64>> {
96//!     // Validate inputs
97//!     check_positive(k, "k")?;
98//!     checkarray_finite(data, "data")?;
99//!
100//!     // Your implementation here
101//!     Ok(Array1::zeros(k))
102//! }
103//! ```
104//!
105//! ### Metrics Collection
106//!
107//! ```rust
108//! use scirs2_core::prelude::*;
109//!
110//! // Create metrics
111//! let counter = Counter::new("requests_total".into());
112//! counter.inc();
113//!
114//! let gauge = Gauge::new("active_connections".into());
115//! gauge.set(42.0);
116//!
117//! let histogram = Histogram::new("response_time".into());
118//! histogram.observe(0.123);
119//!
120//! let timer = Timer::new("operation_duration".into());
121//! let _guard = timer.start(); // Auto-records on drop
122//! ```
123
124// ================================
125// Array Types and Operations
126// ================================
127
128/// Re-export core array types
129pub use crate::ndarray::{
130    Array,  // Generic N-dimensional array
131    Array1, // 1-dimensional array
132    Array2, // 2-dimensional array
133    ArrayD, // Dynamic-dimensional array
134    ArrayView,
135    ArrayView1,
136    ArrayView2,   // Immutable array views
137    ArrayViewMut, // Mutable array view
138    Axis,         // Array axis type
139    Ix1,          // 1-dimensional index
140    Ix2,          // 2-dimensional index
141    IxDyn,        // Dynamic index
142};
143
144/// Re-export array creation and manipulation macros
145pub use crate::ndarray::{
146    array, // Create arrays: array![[1, 2], [3, 4]]
147    s,     // Slice arrays: arr.slice(s![.., 0])
148};
149
150// ================================
151// Random Number Generation
152// ================================
153
154#[cfg(feature = "random")]
155pub use crate::random::{
156    random,       // Convenient random value generation: let x: f64 = random();
157    thread_rng,   // Thread-local RNG
158    Distribution, // Distribution trait
159    Rng,          // Random number generator trait
160    RngCore,      // Core RNG operations
161    SeedableRng,  // Seedable RNG trait for reproducibility
162};
163
164#[cfg(feature = "random")]
165pub use crate::random::{
166    ChaCha12Rng, // Balanced cryptographic RNG
167    ChaCha20Rng, // Secure cryptographic RNG
168    ChaCha8Rng,  // Fast cryptographic RNG
169};
170
171/// Common distributions for convenience
172#[cfg(feature = "random")]
173pub use crate::random::{
174    Bernoulli,   // Bernoulli distribution (coin flip)
175    Exponential, // Exponential distribution
176    Gamma,       // Gamma distribution
177    Normal,      // Normal/Gaussian distribution
178    Uniform,     // Uniform distribution
179};
180
181// ================================
182// Validation Utilities
183// ================================
184
185pub use crate::validation::{
186    check_finite,    // Validate finite values (no NaN/Inf)
187    check_in_bounds, // Validate value is within bounds
188    check_positive,  // Validate positive values
189};
190
191// For backwards compatibility, also provide the array validation functions
192pub use crate::validation::{
193    checkarray_finite as check_array_finite, // Validate all array values are finite
194    checkshape as check_shape,               // Validate array shape
195};
196
197// ================================
198// Metrics and Observability
199// ================================
200
201pub use crate::metrics::{
202    global_metrics_registry, // Access global metrics registry
203    Counter,                 // Monotonically increasing counter
204    Gauge,                   // Arbitrary up/down value
205    Histogram,               // Distribution of values
206    Timer,                   // Duration measurements
207};
208
209// ================================
210// Error Handling
211// ================================
212
213pub use crate::error::{
214    CoreError,  // Main error type
215    CoreResult, // Result<T, CoreError> alias
216};
217
218// ================================
219// Complex Numbers
220// ================================
221
222pub use num_complex::{
223    Complex,   // Generic complex number
224    Complex32, // 32-bit complex (f32 real/imag)
225    Complex64, // 64-bit complex (f64 real/imag)
226};
227
228// ================================
229// Common Traits
230// ================================
231
232/// Re-export commonly used numerical traits
233pub use num_traits::{
234    Float,         // Floating-point operations
235    FromPrimitive, // Convert from primitive types
236    Num,           // Basic numeric operations
237    NumCast,       // Numeric type conversions
238    One,           // Multiplicative identity (1)
239    ToPrimitive,   // Convert to primitive types
240    Zero,          // Additive identity (0)
241};
242
243// ================================
244// Configuration
245// ================================
246
247pub use crate::config::{
248    get_config,        // Get current configuration
249    set_global_config, // Set global configuration
250    Config,            // Configuration management
251};
252
253// ================================
254// Constants
255// ================================
256
257/// Mathematical constants (π, e, φ, etc.)
258pub use crate::constants::math;
259
260/// Physical constants (c, h, G, etc.)
261pub use crate::constants::physical;