I’ve been experimenting with toy implementations of popular libraries that rely on “template magic” and/or operator overloading, to test the boundaries of Rust’s capabilities.
An interesting set of such libraries are the various “extended numbers”. Examples:
- Dual numbers, which can be used to implement Automatic Differentiation
- Interval Arithmetic, which can be used to implement more robust implementations of many numeric algorithms.
- Probability Distributions, which are used for Bayesian inference, often using the Markov Chain Monte Carlo algorithm.
Using Traits and Templates, these can be combined in interesting ways, such as Dual numbers of Intervals, which can be used to differentiate functions while simultaneously reporting the accumulated rounding errors incurred by the computation.
What I’ve found to be missing:
- Rounding modes – The “round()” functions are insufficient to implement Interval Arithmetic. There is a need to set the rounding mode for all computations, not just casts. This is in the standard C and C++ APIs.
Trait Constants – the only available constant via a Trait is
Default, which is typically the numeric value “zero”, or the equivalent. There is no elegant way to specify a Trait for “a type with a multiplicative identity” (one) or the various inverses. The closest is
From<i32>, which allows the use of
From::from( 1 ). That may not make sense for some types, such as matrices or tensors.
- Integers in Templates – I know this has been requested by many people, but I’d like to throw my hat in the ring as well. Dual numbers for example are typically used with multiple variables, and could benefit from small constant sized arrays specified via template arguments.
While I recognize that Rust is targetted at “systems” and “web” programming, not scientific mathematics, the extended numbers described above are highly relevant to many common applications, such as machine learning, image filtering, robotics and sensor data processing, etc…
Interval Arithmetic in particular is a good match for a “robust” language like Rust!