Principle:Avhz RustQuant Mathematical Sequences
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Numerical_Methods |
| Last Updated | 2026-02-07 21:00 GMT |
Overview
Mathematical sequence generation utilities providing R-style functions for creating arithmetic sequences, repetitions, linearly spaced vectors, logarithmically spaced vectors, and cumulative sums.
Description
Mathematical Sequences in RustQuant implements the Sequence trait, which provides a set of generic functions for generating and transforming numerical sequences. The trait is inspired by R's seq, rep, and related functions, and is implemented generically for any type satisfying Num + PartialOrd + Copy + FromPrimitive + ToPrimitive.
The trait defines five methods:
- seq(start, end, step) generates an arithmetic sequence from
starttoend(inclusive) with the given step size. For example,f64::seq(0.0, 1.0, 0.1)produces[0.0, 0.1, 0.2, ..., 1.0]. - rep(x, n) repeats the value
xa total ofntimes, returning a vector. - linspace(start, end, n) generates
nlinearly spaced values betweenstartandend(inclusive). The step size is computed as(end - start) / (n - 1). - logspace(start, end, n) is intended to generate
nlogarithmically spaced values betweenstartandend. This method is currently unimplemented (todo!()). - cumsum(v) computes the cumulative sum of a slice, producing a vector where each element is the running total up to that index.
The implementation uses a blanket impl so that all compatible numeric types (including f32, f64, i32, i64, u32, u64, usize) automatically gain these methods as associated functions.
Usage
Use these utilities when constructing grids for numerical methods (e.g., finite difference grids), generating time steps for simulations, creating evenly spaced evaluation points for curve plotting, or performing running total calculations. The generic implementation ensures the same interface works across all numeric types.
Theoretical Basis
An arithmetic sequence with start , step , and terms is:
For linearly spaced points between and with elements:
The cumulative sum of a sequence is: