num-traits
num-traits copied to clipboard
Add traits for generic norms and distances.
In many iterative numerical algorithms, one has to calculate the distance between a solution candidate and a reference value in order to check for convergence. But currently there is no trait which would enable the computation of such a distance generically.
There is Signed which provides an abs function, but this trait can only be implemented for types with a notion of signedness. The correct mathematical concept, however, are norms and metrics, with the latter being reasonable for basically any numeric type.
So I propose adding two traits, Norm and Distance, each with one method of the same name. The norm-function of the Norm trait returns the norm of a single value, the distance-function of the Distance trait returns the distance between two values.
In my branch dist I have written a draft of these traits and implemented them for the primitive numeric types.
It's a reasonable idea. Can you open it as a PR for discussion?
One thing to consider is that there are multiple possible definitions of norm, especially when more dimensions are added like in Complex<T>. e.g. Euclidean norm vs. Manhattan norm.
It's a reasonable idea. Can you open it as a PR for discussion?
One thing to consider is that there are multiple possible definitions of norm, especially when more dimensions are added like in
Complex<T>. e.g. Euclidean norm vs. Manhattan norm.
Are you sure that it must be the responsibility of num-traits? Creating another managed crate (for example, "num-maths-traits") which would contain category-, group-, field-, ring-, space-, and other more general traits sounds a better idea. In this case, trait inheritance from more general traits can be an opt-in feature enabled by default.
The problem is not even that num-traits may end up becoming too of a heavy dependency in attempts to fit all needs but that num-traits' purpose statement ("Numeric traits for generic mathematics in Rust"), would, in this case, become unclear to me and, perhaps, someone else.
Are you sure that it must be the responsibility of num-traits?
Not at all. If there are interested parties like yourself who want to explore this in a new crate (and not get bogged down in num-traits' conservative API), then feel free!