amari-tropical
Tropical (max-plus) algebra implementation for optimization and neural network applications.
Overview
amari-tropical implements tropical algebra, also known as max-plus algebra, where the traditional operations (+, ×) are replaced with (max, +). This transformation converts expensive softmax and multiplication operations into simple max and addition operations, making it particularly useful for:
- Finding most likely sequences (Viterbi algorithm)
- Shortest path optimization
- Neural network inference optimization
- Dynamic programming
Features
- TropicalNumber: Core scalar type with max-plus operations
- TropicalMatrix: Matrix operations in tropical algebra
- TropicalMultivector: Integration with geometric algebra
- Viterbi Decoder: Efficient sequence decoding using tropical operations
- Tropical Polytopes: Geometric structures in tropical space
- High-Precision Support: Optional extended precision arithmetic
Installation
Add to your Cargo.toml:
[]
= "0.12"
Feature Flags
[]
# Default features
= "0.12"
# With serialization
= { = "0.12", = ["serialize"] }
# With GPU acceleration
= { = "0.12", = ["gpu"] }
# High-precision arithmetic
= { = "0.12", = ["high-precision"] }
Quick Start
use TropicalNumber;
// Create tropical numbers
let a = new;
let b = new;
// Tropical addition: max(3, 5) = 5
let sum = a.tropical_add;
assert_eq!;
// Tropical multiplication: 3 + 5 = 8
let product = a.tropical_mul;
assert_eq!;
// Tropical identities
let zero = tropical_zero; // -∞ (additive identity)
let one = tropical_one; // 0 (multiplicative identity)
Mathematical Background
Tropical Semiring
In tropical algebra, we define:
| Standard | Tropical |
|---|---|
| a + b | max(a, b) |
| a × b | a + b |
| 0 (additive identity) | -∞ |
| 1 (multiplicative identity) | 0 |
Why "Tropical"?
The name honors Brazilian mathematician Imre Simon, who pioneered this field. The algebra is particularly powerful because:
- Exponentiation becomes linear: e^(a+b) → max(a, b) in log-space
- Products become sums: Reduces computational complexity
- Softmax approximation: max approximates log-sum-exp
Applications
- Viterbi Algorithm: Find most likely state sequences in HMMs
- Shortest Paths: Tropical matrix multiplication solves all-pairs shortest paths
- Neural Networks: Approximate attention mechanisms efficiently
- Optimization: Linear programming in tropical geometry
Key Types
TropicalNumber
use TropicalNumber;
let x = new;
// Access the underlying value
let val = x.value;
// Tropical operations (take references)
let y = new;
let sum = x.tropical_add; // max(2.5, 3.0) = 3.0
let prod = x.tropical_mul; // 2.5 + 3.0 = 5.5
TropicalMatrix
use TropicalMatrix;
// Create matrices for shortest path computation
let distances = new;
// Tropical matrix multiplication gives shortest paths
let paths = distances.tropical_matmul;
Viterbi Decoder
use ViterbiDecoder;
// Efficient sequence decoding using tropical algebra
let decoder = new;
let best_path = decoder.decode;
Modules
| Module | Description |
|---|---|
types |
Core tropical number, matrix, and multivector types |
viterbi |
Viterbi algorithm for sequence decoding |
polytope |
Tropical polytopes and geometric structures |
verified |
Phantom types for compile-time verification |
error |
Error types for tropical operations |
Performance
Tropical algebra offers significant performance benefits:
- O(n) vs O(n log n): Max operation vs softmax
- No overflow: Log-space operations avoid numerical issues
- Parallelizable: Element-wise max operations are embarrassingly parallel
- GPU-friendly: Simple operations map well to GPU architectures
v0.12.0 API Changes
The API was updated in v0.12.0 for better encapsulation:
// Before (v0.11.x)
let a = TropicalNumber;
let sum = a.tropical_add;
let val = a.0;
// After (v0.12.0+)
let a = new;
let sum = a.tropical_add; // Now takes reference
let val = a.value;
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.
Part of Amari
This crate is part of the Amari mathematical computing library.