lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! R7RS-large compatible high-performance numeric library
//!
//! This module provides a comprehensive implementation of numeric types and operations
//! following the R7RS-large specification with performance optimizations using Rust's
//! zero-cost abstractions and type system.
//!
//! ## Numeric Tower
//! - Integer → Rational → Real → Complex
//! - Automatic type promotion and coercion
//! - Precision preservation where possible
//!
//! ## Key Features
//! - Complex number arithmetic with optimizations
//! - Rational number system with GCD-based reduction
//! - Big integer support for arbitrary precision
//! - Advanced mathematical functions
//! - SIMD optimizations where applicable

/// Complex number implementation with arithmetic operations.
pub mod complex;
/// Rational number system with GCD-based reduction.
pub mod rational;
/// Arbitrary precision big integer implementation.
pub mod bigint;
/// Numeric tower with automatic type promotion and coercion.
pub mod tower;
/// Advanced mathematical functions and operations.
pub mod functions;
/// Mathematical constants and predefined values.
pub mod constants;
/// Primitive numeric operations and conversions.
pub mod primitives;
/// Integration with the language's evaluation system.
pub mod integration;
/// Performance optimizations and specialized algorithms.
pub mod optimization;
/// Demonstration and example code for numeric operations.
pub mod demo;
/// SIMD-optimized numeric operations for performance.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub mod simd_optimization;
/// Stub SIMD implementation for non-x86 architectures.
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
pub mod simd_optimization_stub;
/// SIMD performance benchmarking and analysis suite.
#[cfg(feature = "simd-benchmarks")]
pub mod simd_benchmarks;

pub use complex::*;
pub use rational::*;
pub use bigint::*;
pub use tower::*;
pub use functions::*;
pub use constants::*;
pub use primitives::*;
pub use integration::*;
pub use optimization::*;
pub use demo::*;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use simd_optimization::{
    SimdNumericOps, SimdOperationType, AlignedBuffer, CpuFeatures,
};

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
pub use simd_optimization_stub::{
    SimdNumericOps, SimdOperationType, AlignedBuffer, CpuFeatures,
};

// Re-export SIMD configuration and optimized functions
pub use SimdNumericOps as SimdConfig;

/// Optimized addition of numeric arrays using SIMD
pub fn add_numeric_arrays_optimized(a: &[f64], b: &[f64]) -> crate::diagnostics::Result<Vec<f64>> {
    let mut result = vec![0.0; a.len()];
    let simd_ops_guard = get_simd_ops();
    let mut simd_ops = simd_ops_guard.lock()
        .map_err(|_| crate::diagnostics::Error::runtime_error("Failed to acquire SIMD lock".to_string(), None))?;
    simd_ops.add_f64_arrays(a, b, &mut result)?;
    Ok(result)
}

/// Optimized dot product using SIMD  
pub fn dot_product_optimized(a: &[f64], b: &[f64]) -> crate::diagnostics::Result<f64> {
    let simd_ops_guard = get_simd_ops();
    let mut simd_ops = simd_ops_guard.lock()
        .map_err(|_| crate::diagnostics::Error::runtime_error("Failed to acquire SIMD lock".to_string(), None))?;
    simd_ops.dot_product_f64(a, b)
}

use crate::ast::Literal;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use once_cell::sync::Lazy;

/// Global SIMD optimization engine for high-performance numeric computations
static GLOBAL_SIMD_OPS: Lazy<Arc<Mutex<SimdNumericOps>>> = Lazy::new(|| {
    Arc::new(Mutex::new(SimdNumericOps::new()))
});

/// Gets the global SIMD operations engine
pub fn get_simd_ops() -> Arc<Mutex<SimdNumericOps>> {
    GLOBAL_SIMD_OPS.clone()
}

/// Unified numeric value type that encompasses all numeric types in the tower
#[derive(Debug, Clone, PartialEq)]
pub enum NumericValue {
    /// Machine integer (i64) - fastest for small integers
    Integer(i64),
    /// Arbitrary precision integer
    BigInteger(BigInt),
    /// Exact rational number
    Rational(Rational),
    /// IEEE 754 double precision floating point
    Real(f64),
    /// Complex number (real + imaginary parts)
    Complex(Complex),
    /// Vector of numeric values for SIMD optimization
    Vector(Vec<NumericValue>),
}

/// Numeric type classification for the tower
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NumericType {
    /// Machine-sized integer (i64)
    Integer = 0,
    /// Arbitrary precision integer
    BigInteger = 1,
    /// Exact rational number (numerator/denominator)
    Rational = 2,
    /// IEEE 754 double precision floating point
    Real = 3,
    /// Complex number with real and imaginary parts
    Complex = 4,
    /// Vector of numeric values
    Vector = 5,
}

impl NumericValue {
    /// Creates an integer value
    pub fn integer(n: i64) -> Self {
        Self::Integer(n)
    }

    /// Creates a big integer value
    pub fn big_integer(n: BigInt) -> Self {
        Self::BigInteger(n)
    }

    /// Creates a rational value
    pub fn rational(num: i64, den: i64) -> Self {
        Self::Rational(Rational::new(num, den))
    }

    /// Creates a real value
    pub fn real(r: f64) -> Self {
        Self::Real(r)
    }

    /// Creates a complex value
    pub fn complex(real: f64, imag: f64) -> Self {
        Self::Complex(Complex::new(real, imag))
    }

    /// Creates a vector value
    pub fn vector(values: Vec<NumericValue>) -> Self {
        Self::Vector(values)
    }
    
    /// Creates a vector of real values (optimized for SIMD)
    pub fn real_vector(values: Vec<f64>) -> Self {
        let num_values: Vec<NumericValue> = values.into_iter()
            .map(NumericValue::real)
            .collect();
        Self::Vector(num_values)
    }

    /// Gets the numeric type for tower operations
    pub fn numeric_type(&self) -> NumericType {
        match self {
            Self::Integer(_) => NumericType::Integer,
            Self::BigInteger(_) => NumericType::BigInteger,
            Self::Rational(_) => NumericType::Rational,
            Self::Real(_) => NumericType::Real,
            Self::Complex(_) => NumericType::Complex,
            Self::Vector(_) => NumericType::Vector,
        }
    }

    /// Checks if this number is exact (rational or integer)
    pub fn is_exact(&self) -> bool {
        match self {
            Self::Integer(_) | Self::BigInteger(_) | Self::Rational(_) => true,
            Self::Vector(v) => v.iter().all(|x| x.is_exact()),
            _ => false,
        }
    }

    /// Checks if this number is inexact (real or complex)
    pub fn is_inexact(&self) -> bool {
        match self {
            Self::Real(_) | Self::Complex(_) => true,
            Self::Vector(v) => v.iter().any(|x| x.is_inexact()),
            _ => false,
        }
    }

    /// Checks if this number is real (not complex with non-zero imaginary part)
    pub fn is_real(&self) -> bool {
        match self {
            Self::Complex(c) => c.imaginary == 0.0,
            Self::Vector(v) => v.iter().all(|x| x.is_real()),
            _ => true,
        }
    }

    /// Checks if this number is an integer
    pub fn is_integer(&self) -> bool {
        match self {
            Self::Integer(_) | Self::BigInteger(_) => true,
            Self::Rational(r) => r.denominator == 1,
            Self::Real(r) => r.fract() == 0.0 && r.is_finite(),
            Self::Complex(c) => c.imaginary == 0.0 && c.real.fract() == 0.0 && c.real.is_finite(),
            Self::Vector(v) => v.iter().all(|x| x.is_integer()),
        }
    }

    /// Checks if this number is zero
    pub fn is_zero(&self) -> bool {
        match self {
            Self::Integer(n) => *n == 0,
            Self::BigInteger(n) => n.is_zero(),
            Self::Rational(r) => r.numerator == 0,
            Self::Real(r) => *r == 0.0,
            Self::Complex(c) => c.real == 0.0 && c.imaginary == 0.0,
            Self::Vector(v) => v.iter().all(|x| x.is_zero()),
        }
    }

    /// Checks if this number is positive
    pub fn is_positive(&self) -> bool {
        match self {
            Self::Integer(n) => *n > 0,
            Self::BigInteger(n) => n.is_positive(),
            Self::Rational(r) => r.is_positive(),
            Self::Real(r) => *r > 0.0,
            Self::Complex(_) => false, // Complex numbers are not ordered
            Self::Vector(v) => v.iter().all(|x| x.is_positive()),
        }
    }

    /// Checks if this number is negative
    pub fn is_negative(&self) -> bool {
        match self {
            Self::Integer(n) => *n < 0,
            Self::BigInteger(n) => n.is_negative(),
            Self::Rational(r) => r.is_negative(),
            Self::Real(r) => *r < 0.0,
            Self::Complex(_) => false, // Complex numbers are not ordered
            Self::Vector(v) => v.iter().all(|x| x.is_negative()),
        }
    }

    /// Converts to f64 if possible (with potential precision loss)
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            Self::Integer(n) => Some(*n as f64),
            Self::BigInteger(n) => n.to_f64(),
            Self::Rational(r) => Some(r.to_f64()),
            Self::Real(r) => Some(*r),
            Self::Complex(c) if c.imaginary == 0.0 => Some(c.real),
            Self::Complex(_) => None,
            Self::Vector(_) => None, // Vectors don't convert to single f64
        }
    }

    /// Converts to i64 if possible (exact integers only)
    pub fn to_i64(&self) -> Option<i64> {
        match self {
            Self::Integer(n) => Some(*n),
            Self::BigInteger(n) => n.to_i64(),
            Self::Rational(r) if r.denominator == 1 => Some(r.numerator),
            Self::Real(r) if r.fract() == 0.0 && r.is_finite() => {
                let i = *r as i64;
                if i as f64 == *r { Some(i) } else { None }
            }
            Self::Complex(c) if c.imaginary == 0.0 && c.real.fract() == 0.0 && c.real.is_finite() => {
                let i = c.real as i64;
                if i as f64 == c.real { Some(i) } else { None }
            }
            Self::Vector(_) => None, // Vectors don't convert to single i64
            _ => None, // Other types don't convert to i64
        }
    }

    /// Converts from a Literal
    pub fn from_literal(lit: &Literal) -> Option<Self> {
        match lit {
            Literal::ExactInteger(n) => Some(Self::Integer(*n)),
            Literal::InexactReal(n) => Some(Self::Real(*n)),
            Literal::Rational { numerator, denominator } => {
                Some(Self::Rational(Rational::new(*numerator, *denominator)))
            }
            Literal::Complex { real, imaginary } => {
                Some(Self::Complex(Complex::new(*real, *imaginary)))
            }
            _ => None,
        }
    }

    /// Converts to a Literal
    pub fn to_literal(&self) -> Literal {
        match self {
            Self::Integer(n) => Literal::ExactInteger(*n),
            Self::BigInteger(n) => {
                // If it fits in i64 range, use ExactInteger; otherwise use InexactReal
                if let Some(i) = n.to_i64() {
                    Literal::ExactInteger(i)
                } else {
                    // For very large integers, convert to inexact representation
                    Literal::InexactReal(n.to_f64().unwrap_or(f64::INFINITY))
                }
            }
            Self::Rational(r) => Literal::Rational {
                numerator: r.numerator,
                denominator: r.denominator,
            },
            Self::Real(r) => Literal::InexactReal(*r),
            Self::Complex(c) => Literal::Complex {
                real: c.real,
                imaginary: c.imaginary,
            },
            Self::Vector(_) => {
                // Vectors are represented as strings for now
                // In the future, this could be a Vector literal type
                Literal::String(format!("{self}"))
            }
        }
    }

    /// Adds two numeric values using the numeric tower
    pub fn add(&self, other: &Self) -> Result<Self, String> {
        Ok(crate::numeric::tower::add(self, other))
    }

    /// Multiplies two numeric values using the numeric tower  
    pub fn multiply(&self, other: &Self) -> Result<Self, String> {
        Ok(crate::numeric::tower::multiply(self, other))
    }

    /// Divides two numeric values using the numeric tower
    pub fn divide(&self, other: &Self) -> Result<Self, String> {
        crate::numeric::tower::divide(self, other)
    }

    /// Subtracts two numeric values using the numeric tower
    pub fn subtract(&self, other: &Self) -> Result<Self, String> {
        Ok(crate::numeric::tower::subtract(self, other))
    }

    /// SIMD-optimized vector addition for compatible vectors
    pub fn simd_vector_add(&self, other: &Self) -> Result<Self, String> {
        match (self, other) {
            (Self::Vector(a), Self::Vector(b)) if a.len() == b.len() => {
                // Try to extract f64 vectors for SIMD optimization
                let a_f64: Result<Vec<f64>, _> = a.iter()
                    .map(|v| v.to_f64().ok_or("Not convertible to f64"))
                    .collect();
                let b_f64: Result<Vec<f64>, _> = b.iter()
                    .map(|v| v.to_f64().ok_or("Not convertible to f64"))
                    .collect();

                match (a_f64, b_f64) {
                    (Ok(a_vals), Ok(b_vals)) => {
                        // Use SIMD optimization
                        let simd_ops_arc = get_simd_ops();
                        let mut simd_ops = simd_ops_arc.lock()
                            .map_err(|_| "Failed to acquire SIMD lock")?;
                        let mut result = vec![0.0; a_vals.len()];
                        simd_ops.add_f64_arrays(&a_vals, &b_vals, &mut result)
                            .map_err(|e| format!("SIMD error: {e}"))?;
                        Ok(Self::real_vector(result))
                    }
                    _ => {
                        // Fallback to element-wise addition
                        let result: Result<Vec<_>, _> = a.iter()
                            .zip(b.iter())
                            .map(|(x, y)| x.add(y))
                            .collect();
                        Ok(Self::Vector(result?))
                    }
                }
            }
            _ => Err("Cannot perform SIMD vector addition on non-matching vectors".to_string())
        }
    }

    /// SIMD-optimized vector multiplication for compatible vectors
    pub fn simd_vector_multiply(&self, other: &Self) -> Result<Self, String> {
        match (self, other) {
            (Self::Vector(a), Self::Vector(b)) if a.len() == b.len() => {
                let a_f64: Result<Vec<f64>, _> = a.iter()
                    .map(|v| v.to_f64().ok_or("Not convertible to f64"))
                    .collect();
                let b_f64: Result<Vec<f64>, _> = b.iter()
                    .map(|v| v.to_f64().ok_or("Not convertible to f64"))
                    .collect();

                match (a_f64, b_f64) {
                    (Ok(a_vals), Ok(b_vals)) => {
                        let simd_ops_arc = get_simd_ops();
                        let mut simd_ops = simd_ops_arc.lock()
                            .map_err(|_| "Failed to acquire SIMD lock")?;
                        let mut result = vec![0.0; a_vals.len()];
                        simd_ops.multiply_f64_arrays(&a_vals, &b_vals, &mut result)
                            .map_err(|e| format!("SIMD error: {e}"))?;
                        Ok(Self::real_vector(result))
                    }
                    _ => {
                        let result: Result<Vec<_>, _> = a.iter()
                            .zip(b.iter())
                            .map(|(x, y)| x.multiply(y))
                            .collect();
                        Ok(Self::Vector(result?))
                    }
                }
            }
            _ => Err("Cannot perform SIMD vector multiplication on non-matching vectors".to_string())
        }
    }

    /// SIMD-optimized dot product for compatible vectors
    pub fn simd_dot_product(&self, other: &Self) -> Result<Self, String> {
        match (self, other) {
            (Self::Vector(a), Self::Vector(b)) if a.len() == b.len() => {
                let a_f64: Result<Vec<f64>, _> = a.iter()
                    .map(|v| v.to_f64().ok_or("Not convertible to f64"))
                    .collect();
                let b_f64: Result<Vec<f64>, _> = b.iter()
                    .map(|v| v.to_f64().ok_or("Not convertible to f64"))
                    .collect();

                match (a_f64, b_f64) {
                    (Ok(a_vals), Ok(b_vals)) => {
                        let simd_ops_arc = get_simd_ops();
                        let mut simd_ops = simd_ops_arc.lock()
                            .map_err(|_| "Failed to acquire SIMD lock")?;
                        let result = simd_ops.dot_product_f64(&a_vals, &b_vals)
                            .map_err(|e| format!("SIMD error: {e}"))?;
                        Ok(Self::Real(result))
                    }
                    _ => {
                        // Fallback: compute sum of element-wise products
                        let products: Result<Vec<_>, _> = a.iter()
                            .zip(b.iter())
                            .map(|(x, y)| x.multiply(y))
                            .collect();
                        let sum = products?.into_iter()
                            .try_fold(Self::Integer(0), |acc, x| acc.add(&x))?;
                        Ok(sum)
                    }
                }
            }
            _ => Err("Cannot perform dot product on non-matching vectors".to_string())
        }
    }

    /// Extracts f64 values from a numeric vector if possible
    pub fn to_f64_vector(&self) -> Option<Vec<f64>> {
        match self {
            Self::Vector(v) => {
                let f64_vec: Result<Vec<f64>, _> = v.iter()
                    .map(|x| x.to_f64().ok_or(()))
                    .collect();
                f64_vec.ok()
            }
            _ => None
        }
    }

    /// Gets vector length if this is a vector
    pub fn vector_length(&self) -> Option<usize> {
        match self {
            Self::Vector(v) => Some(v.len()),
            _ => None
        }
    }

    /// Checks if this value can benefit from SIMD optimization
    pub fn is_simd_optimizable(&self) -> bool {
        match self {
            Self::Vector(v) => {
                v.len() >= 8 && // Minimum size for SIMD benefit
                v.iter().all(|x| x.to_f64().is_some()) // All elements convertible to f64
            }
            _ => false
        }
    }
}

impl fmt::Display for NumericValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Integer(n) => write!(f, "{n}"),
            Self::BigInteger(n) => write!(f, "{n}"),
            Self::Rational(r) => write!(f, "{r}"),
            Self::Real(r) => {
                if r.fract() == 0.0 && r.is_finite() {
                    write!(f, "{}.0", *r as i64)
                } else {
                    write!(f, "{r}")
                }
            }
            Self::Complex(c) => write!(f, "{c}"),
            Self::Vector(v) => {
                write!(f, "#(")?;
                for (i, val) in v.iter().enumerate() {
                    if i > 0 {
                        write!(f, " ")?;
                    }
                    write!(f, "{val}")?;
                }
                write!(f, ")")
            }
        }
    }
}

impl Hash for NumericValue {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.numeric_type().hash(state);
        match self {
            Self::Integer(n) => n.hash(state),
            Self::BigInteger(n) => n.hash(state),
            Self::Rational(r) => r.hash(state),
            Self::Real(r) => r.to_bits().hash(state),
            Self::Complex(c) => c.hash(state),
            Self::Vector(v) => v.hash(state),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_numeric_value_creation() {
        let int_val = NumericValue::integer(42);
        let rat_val = NumericValue::rational(3, 4);
        let real_val = NumericValue::real(std::f64::consts::PI);
        let complex_val = NumericValue::complex(1.0, 2.0);

        assert_eq!(int_val.numeric_type(), NumericType::Integer);
        assert_eq!(rat_val.numeric_type(), NumericType::Rational);
        assert_eq!(real_val.numeric_type(), NumericType::Real);
        assert_eq!(complex_val.numeric_type(), NumericType::Complex);
    }

    #[test]
    fn test_numeric_predicates() {
        let int_val = NumericValue::integer(42);
        let rat_val = NumericValue::rational(3, 4);
        let real_val = NumericValue::real(std::f64::consts::PI);
        let complex_val = NumericValue::complex(1.0, 2.0);

        assert!(int_val.is_exact());
        assert!(rat_val.is_exact());
        assert!(real_val.is_inexact());
        assert!(complex_val.is_inexact());

        assert!(int_val.is_real());
        assert!(rat_val.is_real());
        assert!(real_val.is_real());
        assert!(!complex_val.is_real());

        assert!(int_val.is_integer());
        assert!(!rat_val.is_integer());
        assert!(!real_val.is_integer());
        assert!(!complex_val.is_integer());
    }

    #[test]
    fn test_literal_conversion() {
        let lit = Literal::Rational { numerator: 3, denominator: 4 };
        let num_val = NumericValue::from_literal(&lit).unwrap();
        let back_lit = num_val.to_literal();

        assert_eq!(lit, back_lit);
    }

    #[test]
    fn test_vector_creation_and_operations() {
        let vec_a = NumericValue::real_vector(vec![1.0, 2.0, 3.0, 4.0]);
        let vec_b = NumericValue::real_vector(vec![5.0, 6.0, 7.0, 8.0]);

        assert_eq!(vec_a.numeric_type(), NumericType::Vector);
        assert_eq!(vec_a.vector_length(), Some(4));
        assert!(vec_a.is_simd_optimizable());

        // Test SIMD vector addition
        let result = vec_a.simd_vector_add(&vec_b).unwrap();
        if let NumericValue::Vector(result_vals) = result {
            assert_eq!(result_vals.len(), 4);
            // Expected: [6.0, 8.0, 10.0, 12.0]
            assert_eq!(result_vals[0].to_f64().unwrap(), 6.0);
            assert_eq!(result_vals[1].to_f64().unwrap(), 8.0);
            assert_eq!(result_vals[2].to_f64().unwrap(), 10.0);
            assert_eq!(result_vals[3].to_f64().unwrap(), 12.0);
        } else {
            panic!("Expected vector result");
        }
    }

    #[test]
    fn test_simd_dot_product() {
        let vec_a = NumericValue::real_vector(vec![1.0, 2.0, 3.0, 4.0]);
        let vec_b = NumericValue::real_vector(vec![2.0, 3.0, 4.0, 5.0]);

        let result = vec_a.simd_dot_product(&vec_b).unwrap();
        // Expected: 1*2 + 2*3 + 3*4 + 4*5 = 2 + 6 + 12 + 20 = 40
        assert_eq!(result.to_f64().unwrap(), 40.0);
    }

    #[test]
    fn test_vector_predicates() {
        let positive_vec = NumericValue::real_vector(vec![1.0, 2.0, 3.0]);
        let mixed_vec = NumericValue::vector(vec![
            NumericValue::integer(1),
            NumericValue::real(2.5),
            NumericValue::integer(3)
        ]);

        assert!(positive_vec.is_positive());
        assert!(positive_vec.is_real());
        assert!(!positive_vec.is_exact());
        assert!(positive_vec.is_inexact());

        assert!(mixed_vec.is_positive());
        assert!(mixed_vec.is_real());
        assert!(!mixed_vec.is_exact()); // Contains real number
    }

    #[test]
    fn test_vector_display() {
        let vec = NumericValue::vector(vec![
            NumericValue::integer(1),
            NumericValue::real(2.5),
            NumericValue::rational(3, 4)
        ]);

        let display_str = format!("{}", vec);
        assert!(display_str.starts_with("#("));
        assert!(display_str.ends_with(")"));
    }

    #[test]
    fn test_vector_f64_extraction() {
        let real_vec = NumericValue::real_vector(vec![1.0, 2.0, 3.0]);
        let mixed_vec = NumericValue::vector(vec![
            NumericValue::integer(1),
            NumericValue::rational(3, 2), // 1.5
            NumericValue::complex(2.0, 1.0) // Not convertible
        ]);

        assert_eq!(real_vec.to_f64_vector(), Some(vec![1.0, 2.0, 3.0]));
        assert_eq!(mixed_vec.to_f64_vector(), None); // Contains complex number
    }
}