Module prelude

Module prelude 

Source
Expand description

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

use scirs2_core::prelude::*;

let data = array![[1.0, 2.0], [3.0, 4.0]];

§SciRS2 Core Prelude

The prelude module provides convenient access to the most commonly used items in the SciRS2 ecosystem. Import this module to get started quickly without needing to know the exact paths of all core functionality.

§Usage

use scirs2_core::prelude::*;

// Now you have access to all common functionality:
let data = array![[1.0, 2.0], [3.0, 4.0]];  // Array creation
let mean = data.mean().unwrap();             // Array operations
let counter = Counter::new("requests".into()); // Metrics

§What’s Included

§Array Types and Operations

  • Array, Array1, Array2, ArrayD - N-dimensional array types
  • ArrayView, ArrayViewMut - Array views
  • Axis, Ix1, Ix2, IxDyn - Shape and axis types
  • array!, s! - Convenient macros for array creation and slicing

§Random Number Generation

  • random() - Convenient random value generation
  • Rng - Random number generator trait
  • SeedableRng - Seedable RNG trait for reproducibility
  • ChaCha8Rng, ChaCha12Rng, ChaCha20Rng - Secure random number generators
  • Common distributions: Normal, Uniform, Exponential, Gamma, Bernoulli

§Validation Utilities

  • check_positive() - Validate positive values
  • check_shape() - Validate array shapes
  • check_finite() - Validate finite values
  • check_in_bounds() - Validate value bounds

§Metrics and Observability

  • Counter - Monotonically increasing metric
  • Gauge - Arbitrary up/down metric
  • Histogram - Distribution of values
  • Timer - Duration measurements
  • global_metrics_registry() - Global metrics collection

§Error Handling

  • CoreError - Main error type
  • CoreResult<T> - Result type alias

§Complex Numbers

  • Complex, Complex32, Complex64 - Complex number types

§Examples

§Basic Array Operations

use scirs2_core::prelude::*;

// Create arrays
let a = array![1.0, 2.0, 3.0, 4.0];
let b = array![[1.0, 2.0], [3.0, 4.0]];

// Array slicing
let slice = b.slice(s![.., 0]);

// Array operations
let sum = a.sum();
let mean = a.mean().unwrap();

§Random Number Generation

use scirs2_core::prelude::*;

// Quick random values
let x: f64 = random();
let y: bool = random();

// Reproducible random generation
let mut rng = ChaCha8Rng::seed_from_u64(42);
let sample = rng.gen::<f64>();

// Sample from distributions
let normal = Normal::new(0.0, 1.0).unwrap();
let value = normal.sample(&mut rng);

§Parameter Validation

use scirs2_core::prelude::*;

pub fn my_function(data: &Array2<f64>, k: usize) -> CoreResult<Array1<f64>> {
    // Validate inputs
    check_positive(k, "k")?;
    checkarray_finite(data, "data")?;

    // Your implementation here
    Ok(Array1::zeros(k))
}

§Metrics Collection

use scirs2_core::prelude::*;

// Create metrics
let counter = Counter::new("requests_total".into());
counter.inc();

let gauge = Gauge::new("active_connections".into());
gauge.set(42.0);

let histogram = Histogram::new("response_time".into());
histogram.observe(0.123);

let timer = Timer::new("operation_duration".into());
let _guard = timer.start(); // Auto-records on drop

Re-exports§

pub use crate::validation::check_finite;
pub use crate::validation::check_in_bounds;
pub use crate::validation::check_positive;
pub use crate::validation::checkarray_finite as check_array_finite;
pub use crate::validation::checkshape as check_shape;
pub use crate::metrics::global_metrics_registry;
pub use crate::metrics::Counter;
pub use crate::metrics::Gauge;
pub use crate::metrics::Histogram;
pub use crate::metrics::Timer;
pub use crate::error::CoreError;
pub use crate::error::CoreResult;
pub use crate::config::get_config;
pub use crate::config::set_global_config;
pub use crate::config::Config;
pub use crate::constants::math;
pub use crate::constants::physical;

Macros§

array
Re-export array creation and manipulation macros Re-export essential macros that were missing in some modules Create an Array with one, two, three, four, five, or six dimensions.
s
Re-export array creation and manipulation macros Re-export essential macros that were missing in some modules Slice argument constructor.

Structs§

Axis
Re-export core array types An axis index.
Complex
A complex number in Cartesian form.

Traits§

Float
Re-export commonly used numerical traits Generic trait for floating point numbers
FromPrimitive
Re-export commonly used numerical traits A generic trait for converting a number to a value.
Num
Re-export commonly used numerical traits The base trait for numeric types, covering 0 and 1 values, comparisons, basic numeric operations, and string conversion.
NumCast
Re-export commonly used numerical traits An interface for casting between machine scalars.
One
Re-export commonly used numerical traits Defines a multiplicative identity element for Self.
ToPrimitive
Re-export commonly used numerical traits A generic trait for converting a value to a number.
Zero
Re-export commonly used numerical traits Defines an additive identity element for Self.

Functions§

Ix1
Re-export core array types Create a one-dimensional index
Ix2
Re-export core array types Create a two-dimensional index
IxDyn
Re-export core array types Create a dynamic-dimensional index

Type Aliases§

Array
Re-export core array types An array that owns its data uniquely.
Array1
Re-export core array types one-dimensional array
Array2
Re-export core array types two-dimensional array
ArrayD
Re-export core array types dynamic-dimensional array
ArrayView
Re-export core array types A read-only array view.
ArrayView1
Re-export core array types one-dimensional array view
ArrayView2
Re-export core array types two-dimensional array view
ArrayViewMut
Re-export core array types A read-write array view.
Complex32
Alias for a Complex<f32>
Complex64
Alias for a Complex<f64>
Ix1
Re-export core array types one-dimensional
Ix2
Re-export core array types two-dimensional
IxDyn
Re-export core array types dynamic-dimensional