Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant Sequences

From Leeroopedia
Revision as of 14:33, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Avhz_RustQuant_Sequences.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Mathematics, Statistics
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete tool for generating sequences of numbers provided by the RustQuant library.

Description

The Sequence trait provides functions for generating numerical sequences in the style of R's seq and rep functions. It is implemented generically for any type T that satisfies the bounds Num + PartialOrd + Copy + FromPrimitive + ToPrimitive, making it usable with f64, f32, i32, i64, u32, u64, usize, and other numeric types.

Methods:

  • seq(start, end, step) -- Generates a sequence from start to end (inclusive) with the given step size. Returns a Vec<T>.
  • rep(x, n) -- Repeats the value x exactly n times. Returns a Vec<T>.
  • linspace(start, end, n) -- Generates n linearly spaced values from start to end (inclusive). Requires start < end and n > 0.
  • logspace(start, end, n) -- Generates n logarithmically spaced values. Currently unimplemented (todo!()).
  • cumsum(v) -- Computes the cumulative sum of a slice, returning a new Vec<T>. Each element is the running total of all elements up to that position.

The trait is called as associated functions on the type itself (e.g., f64::seq(0.0, 1.0, 0.1), i32::rep(1, 5)). The linspace function computes the step as (end - start) / (n - 1) and panics if start >= end or n == 0.

Usage

Use these functions for generating grids of evaluation points, time discretizations, and repeated values. In quantitative finance, sequence generation is essential for building time grids in Monte Carlo simulations, constructing strike price arrays for volatility surfaces, creating tenor schedules for yield curves, and initializing arrays for finite difference methods.

Code Reference

Source Location

Signature

pub trait Sequence<T: Num + PartialOrd + Copy + FromPrimitive + ToPrimitive> {
    /// Generate a sequence from start to end with step size.
    fn seq(start: T, end: T, step: T) -> Vec<T>;

    /// Repeat a value x, n times.
    fn rep(x: T, n: usize) -> Vec<T>;

    /// Generate n linearly spaced values from start to end.
    fn linspace(start: T, end: T, n: usize) -> Vec<T>;

    /// Generate n logarithmically spaced values from start to end.
    fn logspace(start: T, end: T, n: usize) -> Vec<T>; // todo!()

    /// Compute the cumulative sum of a vector.
    fn cumsum(v: &[T]) -> Vec<T>;
}

impl<T> Sequence<T> for T
where
    T: Num + PartialOrd + Copy + FromPrimitive + ToPrimitive;

Import

use RustQuant::math::Sequence;

I/O Contract

Inputs

Name Type Required Description
start T For seq/linspace Starting value of the sequence.
end T For seq/linspace Ending value of the sequence (inclusive).
step T For seq Step size between consecutive elements.
n usize For rep/linspace Number of repetitions or number of elements.
x T For rep Value to repeat.
v &[T] For cumsum Input slice for cumulative summation.

Outputs

Name Type Description
seq() Vec<T> A vector of values from start to end with the given step.
rep() Vec<T> A vector containing x repeated n times.
linspace() Vec<T> A vector of n linearly spaced values from start to end.
cumsum() Vec<T> A vector where each element is the running sum of the input.

Usage Examples

use RustQuant::math::Sequence;

// Generate a sequence from 0 to 1 with step 0.1
let seq = f64::seq(0.0, 1.0, 0.1);
// [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

// Integer sequence with step 2
let int_seq = i32::seq(0, 10, 2);
// [0, 2, 4, 6, 8, 10]

// Repeat a value
let ones = f64::rep(1.0, 5);
// [1.0, 1.0, 1.0, 1.0, 1.0]

// Linearly spaced values
let grid = f64::linspace(1.0, 5.0, 5);
// [1.0, 2.0, 3.0, 4.0, 5.0]

// Cumulative sum
let v = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let cs = f64::cumsum(&v);
// [1.0, 3.0, 6.0, 10.0, 15.0]

// Negative cumulative sum
let neg = vec![-1.0, -2.0, -3.0];
let neg_cs = f64::cumsum(&neg);
// [-1.0, -3.0, -6.0]

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment