Convert a distance value produced by `scalar_distance` into a similarity score where higher is better. | Metric | Conversion | |----------------|---------------------| | L2 | `−distance` | | Cosine | `1 − distance` | | InnerProduct | `−distance` (scalar_distance returns neg-IP) | | All others | `−distance` |
(d: f32, metric: DistanceMetric)
| 25 | /// | InnerProduct | `−distance` (scalar_distance returns neg-IP) | |
| 26 | /// | All others | `−distance` | |
| 27 | fn dist_to_sim(d: f32, metric: DistanceMetric) -> f32 { |
| 28 | match metric { |
| 29 | DistanceMetric::Cosine => 1.0 - d, |
| 30 | // All other metrics (and unknown future metrics) use negated distance. |
| 31 | _ => -d, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | // Public API |