Last active
December 24, 2025 23:24
-
-
Save ppamorim/439db5f7fa7d8443d7517661dab8dd50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::collections::HashMap; | |
| /// Represents a sequence indexed by natural numbers | |
| type Sequence = Vec<f64>; | |
| /// Computes the infimum (greatest lower bound) of a sequence starting from index k | |
| fn infimum_from(seq: &Sequence, k: usize) -> f64 { | |
| if k >= seq.len() { | |
| return f64::INFINITY; | |
| } | |
| seq[k..].iter() | |
| .copied() | |
| .fold(f64::INFINITY, f64::min) | |
| } | |
| /// Computes the limit inferior of a sequence | |
| /// lim inf x_i = lim (inf x_k where k >= n) as n -> infinity | |
| fn lim_inf(seq: &Sequence) -> f64 { | |
| let mut supremum = f64::NEG_INFINITY; | |
| // For each n, compute inf{x_k : k >= n} | |
| // Then take the supremum of these values (which is the limit as n -> infinity) | |
| for n in 0..seq.len() { | |
| let inf_from_n = infimum_from(seq, n); | |
| supremum = supremum.max(inf_from_n); | |
| } | |
| supremum | |
| } | |
| /// Left side: Sum of limit inferiors | |
| /// Σ(lim inf x_i^(n)) for i ∈ ℕ | |
| fn left_side(sequences: &HashMap<usize, Sequence>) -> f64 { | |
| sequences.values() | |
| .map(|seq| lim_inf(seq)) | |
| .sum() | |
| } | |
| /// Right side: Limit of sums of infimums | |
| /// lim (Σ inf x_i^(k) where k >= n) as n -> infinity | |
| fn right_side(sequences: &HashMap<usize, Sequence>) -> f64 { | |
| if sequences.is_empty() { | |
| return 0.0; | |
| } | |
| let max_len = sequences.values() | |
| .map(|seq| seq.len()) | |
| .max() | |
| .unwrap_or(0); | |
| let mut supremum = f64::NEG_INFINITY; | |
| // For each n, compute the sum of infimums starting from index n | |
| for n in 0..max_len { | |
| let sum_of_infs: f64 = sequences.values() | |
| .map(|seq| infimum_from(seq, n)) | |
| .sum(); | |
| supremum = supremum.max(sum_of_infs); | |
| } | |
| supremum | |
| } | |
| fn main() { | |
| // Example: Create indexed sequences | |
| let mut sequences: HashMap<usize, Sequence> = HashMap::new(); | |
| // Sequence 0: x_0^(n) = {1.0, 0.5, 0.6, 0.55, 0.58, ...} | |
| sequences.insert(0, vec![1.0, 0.5, 0.6, 0.55, 0.58, 0.57, 0.571]); | |
| // Sequence 1: x_1^(n) = {2.0, 1.0, 1.2, 1.1, 1.15, ...} | |
| sequences.insert(1, vec![2.0, 1.0, 1.2, 1.1, 1.15, 1.12, 1.13]); | |
| // Sequence 2: x_2^(n) = {3.0, 2.5, 2.6, 2.55, 2.57, ...} | |
| sequences.insert(2, vec![3.0, 2.5, 2.6, 2.55, 2.57, 2.56, 2.565]); | |
| let left = left_side(&sequences); | |
| let right = right_side(&sequences); | |
| println!("Left side (sum of lim infs): {:.6}", left); | |
| println!("Right side (lim of sum of infs): {:.6}", right); | |
| println!("Difference: {:.10}", (left - right).abs()); | |
| // Verify the equality | |
| const EPSILON: f64 = 1e-10; | |
| if (left - right).abs() < EPSILON { | |
| println!("\n✓ The equality holds!"); | |
| } else { | |
| println!("\n✗ The equality does not hold (might need more terms)"); | |
| } | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn test_infimum() { | |
| let seq = vec![3.0, 1.0, 2.0, 0.5, 1.5]; | |
| assert_eq!(infimum_from(&seq, 0), 0.5); | |
| assert_eq!(infimum_from(&seq, 2), 0.5); | |
| assert_eq!(infimum_from(&seq, 3), 0.5); | |
| } | |
| #[test] | |
| fn test_lim_inf() { | |
| // Sequence converging to 1.0 from below | |
| let seq = vec![0.5, 0.8, 0.9, 0.95, 0.98, 0.99]; | |
| let result = lim_inf(&seq); | |
| assert!((result - 0.99).abs() < 1e-10); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment