amari 0.19.1

Advanced mathematical computing library with geometric algebra, tropical algebra, and automatic differentiation
Documentation
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
//! Phase 4A: Runtime Verification System Prototype
//!
//! This prototype demonstrates adaptive verification that can work across
//! CPU, GPU, and WASM platforms by switching verification strategies based
//! on platform constraints and performance requirements.

use std::marker::PhantomData;
use std::time::{Duration, Instant};
use amari_core::Multivector;

/// Platform-specific verification constraints
#[derive(Debug, Clone, Copy)]
pub enum VerificationPlatform {
    /// Native CPU with full phantom type support
    NativeCpu,
    /// GPU with boundary verification constraints
    Gpu { max_overhead_ms: u64 },
    /// WASM with runtime contract limitations
    Wasm { js_interop: bool },
}

/// Verification levels that can be adapted to platform constraints
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VerificationLevel {
    /// Full phantom type verification (CPU only)
    Strict,
    /// Statistical sampling verification
    Statistical { sample_rate: f64 },
    /// Boundary-only verification
    Boundary,
    /// Minimal runtime checks
    Minimal,
}

/// Verification error types that work across platforms
#[derive(Debug, Clone)]
pub enum VerificationError {
    /// Mathematical invariant violation
    InvariantViolation { operation: String, details: String },
    /// Platform constraint violation
    PlatformConstraint { platform: String, limitation: String },
    /// Performance budget exceeded
    PerformanceBudget { actual: Duration, budget: Duration },
    /// Statistical verification failure
    StatisticalFailure { confidence: f64, threshold: f64 },
}

/// Adaptive verification context that switches strategies by platform
pub struct AdaptiveVerificationContext {
    platform: VerificationPlatform,
    level: VerificationLevel,
    performance_budget: Duration,
    operation_count: usize,
}

impl AdaptiveVerificationContext {
    /// Create verification context with platform detection
    pub fn new(platform: VerificationPlatform) -> Self {
        let level = Self::determine_optimal_level(platform);
        let budget = Self::determine_performance_budget(platform);

        Self {
            platform,
            level,
            performance_budget: budget,
            operation_count: 0,
        }
    }

    /// Determine optimal verification level for platform
    fn determine_optimal_level(platform: VerificationPlatform) -> VerificationLevel {
        match platform {
            VerificationPlatform::NativeCpu => VerificationLevel::Strict,
            VerificationPlatform::Gpu { max_overhead_ms } => {
                if max_overhead_ms < 5 {
                    VerificationLevel::Minimal
                } else if max_overhead_ms < 20 {
                    VerificationLevel::Boundary
                } else {
                    VerificationLevel::Statistical { sample_rate: 0.1 }
                }
            }
            VerificationPlatform::Wasm { js_interop } => {
                if js_interop {
                    VerificationLevel::Statistical { sample_rate: 0.05 }
                } else {
                    VerificationLevel::Boundary
                }
            }
        }
    }

    /// Determine performance budget based on platform
    fn determine_performance_budget(platform: VerificationPlatform) -> Duration {
        match platform {
            VerificationPlatform::NativeCpu => Duration::from_millis(50),
            VerificationPlatform::Gpu { max_overhead_ms } => Duration::from_millis(max_overhead_ms),
            VerificationPlatform::Wasm { .. } => Duration::from_millis(25),
        }
    }

    /// Verify mathematical operation with platform-appropriate strategy
    pub async fn verify_operation<F, R>(
        &mut self,
        operation_name: &str,
        operation: F,
    ) -> Result<R, VerificationError>
    where
        F: FnOnce() -> R + Send,
        R: Send,
    {
        let start_time = Instant::now();
        self.operation_count += 1;

        match self.level {
            VerificationLevel::Strict => {
                self.strict_verification(operation_name, operation).await
            }
            VerificationLevel::Statistical { sample_rate } => {
                self.statistical_verification(operation_name, operation, sample_rate).await
            }
            VerificationLevel::Boundary => {
                self.boundary_verification(operation_name, operation).await
            }
            VerificationLevel::Minimal => {
                self.minimal_verification(operation_name, operation).await
            }
        }
    }

    /// Strict verification with full phantom type guarantees (CPU only)
    async fn strict_verification<F, R>(
        &mut self,
        operation_name: &str,
        operation: F,
    ) -> Result<R, VerificationError>
    where
        F: FnOnce() -> R + Send,
        R: Send,
    {
        let start = Instant::now();

        // Pre-condition verification
        self.verify_preconditions(operation_name)?;

        // Execute operation
        let result = tokio::task::spawn_blocking(operation).await
            .map_err(|_| VerificationError::InvariantViolation {
                operation: operation_name.to_string(),
                details: "Operation panic".to_string(),
            })?;

        // Post-condition verification
        self.verify_postconditions(operation_name)?;

        // Check performance budget
        let elapsed = start.elapsed();
        if elapsed > self.performance_budget {
            return Err(VerificationError::PerformanceBudget {
                actual: elapsed,
                budget: self.performance_budget,
            });
        }

        Ok(result)
    }

    /// Statistical verification through sampling (GPU/WASM)
    async fn statistical_verification<F, R>(
        &mut self,
        operation_name: &str,
        operation: F,
        sample_rate: f64,
    ) -> Result<R, VerificationError>
    where
        F: FnOnce() -> R + Send,
        R: Send,
    {
        let should_sample = self.should_sample_operation(sample_rate);

        if should_sample {
            // Perform full verification on sampled operations
            return self.strict_verification(operation_name, operation).await;
        } else {
            // Minimal verification for non-sampled operations
            return self.minimal_verification(operation_name, operation).await;
        }
    }

    /// Boundary verification at platform transitions (GPU/WASM)
    async fn boundary_verification<F, R>(
        &mut self,
        operation_name: &str,
        operation: F,
    ) -> Result<R, VerificationError>
    where
        F: FnOnce() -> R + Send,
        R: Send,
    {
        // Verify only at critical boundaries (input/output validation)
        self.verify_boundary_conditions(operation_name)?;

        let result = tokio::task::spawn_blocking(operation).await
            .map_err(|_| VerificationError::InvariantViolation {
                operation: operation_name.to_string(),
                details: "Boundary violation".to_string(),
            })?;

        Ok(result)
    }

    /// Minimal verification for performance-critical operations
    async fn minimal_verification<F, R>(
        &mut self,
        operation_name: &str,
        operation: F,
    ) -> Result<R, VerificationError>
    where
        F: FnOnce() -> R + Send,
        R: Send,
    {
        // Only basic safety checks
        let result = tokio::task::spawn_blocking(operation).await
            .map_err(|_| VerificationError::InvariantViolation {
                operation: operation_name.to_string(),
                details: "Minimal verification failure".to_string(),
            })?;

        Ok(result)
    }

    /// Check if operation should be sampled for statistical verification
    fn should_sample_operation(&self, sample_rate: f64) -> bool {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        self.operation_count.hash(&mut hasher);
        let hash = hasher.finish();

        (hash as f64 / u64::MAX as f64) < sample_rate
    }

    /// Verify pre-conditions for strict verification
    fn verify_preconditions(&self, operation_name: &str) -> Result<(), VerificationError> {
        // Platform-specific pre-condition checks
        match self.platform {
            VerificationPlatform::NativeCpu => {
                // Full phantom type verification available
                self.verify_cpu_preconditions(operation_name)
            }
            VerificationPlatform::Gpu { .. } => {
                // GPU-specific constraints
                self.verify_gpu_preconditions(operation_name)
            }
            VerificationPlatform::Wasm { .. } => {
                // WASM runtime constraints
                self.verify_wasm_preconditions(operation_name)
            }
        }
    }

    /// Verify post-conditions for strict verification
    fn verify_postconditions(&self, operation_name: &str) -> Result<(), VerificationError> {
        // Similar platform-specific post-condition checks
        match self.platform {
            VerificationPlatform::NativeCpu => Ok(()),
            VerificationPlatform::Gpu { .. } => Ok(()),
            VerificationPlatform::Wasm { .. } => Ok(()),
        }
    }

    /// Verify boundary conditions for boundary verification
    fn verify_boundary_conditions(&self, operation_name: &str) -> Result<(), VerificationError> {
        // Check only input/output contract compliance
        Ok(())
    }

    /// CPU-specific precondition verification
    fn verify_cpu_preconditions(&self, _operation_name: &str) -> Result<(), VerificationError> {
        // Full mathematical invariant checking
        Ok(())
    }

    /// GPU-specific precondition verification
    fn verify_gpu_preconditions(&self, operation_name: &str) -> Result<(), VerificationError> {
        // Check GPU memory constraints, batch size limits
        if operation_name.contains("batch") && self.operation_count > 1000000 {
            return Err(VerificationError::PlatformConstraint {
                platform: "GPU".to_string(),
                limitation: "Batch size too large for GPU memory".to_string(),
            });
        }
        Ok(())
    }

    /// WASM-specific precondition verification
    fn verify_wasm_preconditions(&self, operation_name: &str) -> Result<(), VerificationError> {
        // Check WASM memory constraints, JS interop limitations
        if operation_name.contains("large_batch") {
            return Err(VerificationError::PlatformConstraint {
                platform: "WASM".to_string(),
                limitation: "Large batches may exceed WASM memory limit".to_string(),
            });
        }
        Ok(())
    }

    /// Get verification statistics
    pub fn get_stats(&self) -> VerificationStats {
        VerificationStats {
            platform: self.platform,
            level: self.level,
            operation_count: self.operation_count,
            budget_remaining: self.performance_budget,
        }
    }
}

/// Verification statistics for monitoring
#[derive(Debug)]
pub struct VerificationStats {
    pub platform: VerificationPlatform,
    pub level: VerificationLevel,
    pub operation_count: usize,
    pub budget_remaining: Duration,
}

/// Cross-platform verified multivector wrapper
pub struct CrossPlatformVerifiedMultivector<const P: usize, const Q: usize, const R: usize> {
    inner: Multivector<P, Q, R>,
    verification_context: PhantomData<(P, Q, R)>,
    platform_verified: bool,
}

impl<const P: usize, const Q: usize, const R: usize> CrossPlatformVerifiedMultivector<P, Q, R> {
    /// Create verified multivector with platform adaptation
    pub fn new_verified(
        multivector: Multivector<P, Q, R>,
        context: &AdaptiveVerificationContext,
    ) -> Result<Self, VerificationError> {
        // Platform-specific validation
        match context.platform {
            VerificationPlatform::NativeCpu => {
                // Full phantom type verification
                Self::verify_cpu_creation(&multivector)?;
            }
            VerificationPlatform::Gpu { .. } => {
                // GPU boundary verification
                Self::verify_gpu_boundary(&multivector)?;
            }
            VerificationPlatform::Wasm { .. } => {
                // WASM runtime verification
                Self::verify_wasm_runtime(&multivector)?;
            }
        }

        Ok(Self {
            inner: multivector,
            verification_context: PhantomData,
            platform_verified: true,
        })
    }

    /// Verified geometric product with adaptive verification
    pub async fn geometric_product_verified(
        &self,
        other: &Self,
        context: &mut AdaptiveVerificationContext,
    ) -> Result<Self, VerificationError> {
        let operation = || {
            let result = self.inner.geometric_product(&other.inner);
            Self {
                inner: result,
                verification_context: PhantomData,
                platform_verified: true,
            }
        };

        context.verify_operation("geometric_product", operation).await
    }

    /// Platform-specific verification methods
    fn verify_cpu_creation(mv: &Multivector<P, Q, R>) -> Result<(), VerificationError> {
        // Full mathematical property verification
        if !mv.magnitude().is_finite() {
            return Err(VerificationError::InvariantViolation {
                operation: "creation".to_string(),
                details: "Non-finite magnitude".to_string(),
            });
        }
        Ok(())
    }

    fn verify_gpu_boundary(mv: &Multivector<P, Q, R>) -> Result<(), VerificationError> {
        // Basic numerical stability checks for GPU transfer
        if mv.magnitude() > 1e10 {
            return Err(VerificationError::PlatformConstraint {
                platform: "GPU".to_string(),
                limitation: "Magnitude too large for GPU precision".to_string(),
            });
        }
        Ok(())
    }

    fn verify_wasm_runtime(mv: &Multivector<P, Q, R>) -> Result<(), VerificationError> {
        // JavaScript interop compatibility checks
        for i in 0..8 {
            if !mv.get(i).is_finite() {
                return Err(VerificationError::PlatformConstraint {
                    platform: "WASM".to_string(),
                    limitation: "Non-finite coefficients not supported in JavaScript".to_string(),
                });
            }
        }
        Ok(())
    }

    /// Get underlying multivector (platform-verified)
    pub fn inner(&self) -> &Multivector<P, Q, R> {
        &self.inner
    }

    /// Check if platform verification is active
    pub fn is_platform_verified(&self) -> bool {
        self.platform_verified
    }
}

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

    #[tokio::test]
    async fn test_cpu_strict_verification() {
        let mut context = AdaptiveVerificationContext::new(VerificationPlatform::NativeCpu);

        let result = context.verify_operation("test_operation", || {
            // Simulate mathematical operation
            42
        }).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 42);
    }

    #[tokio::test]
    async fn test_gpu_boundary_verification() {
        let mut context = AdaptiveVerificationContext::new(
            VerificationPlatform::Gpu { max_overhead_ms: 10 }
        );

        let result = context.verify_operation("gpu_operation", || {
            // Simulate GPU operation
            vec![1.0, 2.0, 3.0]
        }).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_wasm_statistical_verification() {
        let mut context = AdaptiveVerificationContext::new(
            VerificationPlatform::Wasm { js_interop: true }
        );

        // Run multiple operations to test sampling
        for i in 0..100 {
            let result = context.verify_operation("wasm_operation", || i).await;
            assert!(result.is_ok());
        }

        let stats = context.get_stats();
        assert_eq!(stats.operation_count, 100);
    }

    #[tokio::test]
    async fn test_cross_platform_multivector() {
        let context = AdaptiveVerificationContext::new(VerificationPlatform::NativeCpu);
        let mv = Multivector::<3, 0, 0>::basis_vector(0);

        let verified_mv = CrossPlatformVerifiedMultivector::new_verified(mv, &context);
        assert!(verified_mv.is_ok());
        assert!(verified_mv.unwrap().is_platform_verified());
    }

    #[test]
    fn test_platform_optimal_levels() {
        let cpu_level = AdaptiveVerificationContext::determine_optimal_level(
            VerificationPlatform::NativeCpu
        );
        assert_eq!(cpu_level, VerificationLevel::Strict);

        let gpu_level = AdaptiveVerificationContext::determine_optimal_level(
            VerificationPlatform::Gpu { max_overhead_ms: 2 }
        );
        assert_eq!(gpu_level, VerificationLevel::Minimal);

        let wasm_level = AdaptiveVerificationContext::determine_optimal_level(
            VerificationPlatform::Wasm { js_interop: true }
        );
        assert!(matches!(wasm_level, VerificationLevel::Statistical { .. }));
    }
}