summaryrefslogtreecommitdiffstats
path: root/src/cos.rs
blob: a3a4f6cc1e7fc2a3e56be3c44d8ec4d0a97eab94 (plain)
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! [Cosine distance](https://en.wikipedia.org/wiki/Cosine_similarity).

use crate::coords::Coordinates;
use crate::distance::{Distance, Metric, Proximity, Value};

use num_traits::real::Real;
use num_traits::{one, zero};

use std::cmp::Ordering;
use std::convert::TryFrom;

/// Compute the [cosine *similarity*] between two points.
///
/// Use [cosine_distance] instead if you are implementing [`Proximity::distance()`].
///
/// ```math
/// \begin{aligned}
/// \mathrm{cosine\_similarity}(x, y) &= \frac{x \cdot y}{\|x\| \|y\|} \\
/// &= \frac{\sum_i x_i y_i}{\sqrt{\sum_i x_i^2} \sqrt{\sum_i y_i^2}} \\
/// &= \cos \theta
/// \end{aligned}
/// ```
///
/// [cosine *similarity*]: https://en.wikipedia.org/wiki/Cosine_similarity
/// [`Proximity::distance()`]: Proximity#tymethod.distance
pub fn cosine_similarity<T, U>(x: T, y: U) -> T::Value
where
    T: Coordinates,
    U: Coordinates<Value = T::Value>,
    T::Value: Real,
{
    debug_assert!(x.dims() == y.dims());

    let mut dot: T::Value = zero();
    let mut xx: T::Value = zero();
    let mut yy: T::Value = zero();

    for i in 0..x.dims() {
        let xi = x.coord(i);
        let yi = y.coord(i);
        dot += xi * yi;
        xx += xi * xi;
        yy += yi * yi;
    }

    dot / (xx * yy).sqrt()
}

/// Compute the [cosine distance] between two points.
///
/// ```math
/// \begin{aligned}
/// \mathrm{cosine\_distance}(x, y) &= 1 - \mathrm{cosine\_similarity}(x, y) \\
/// &= 1 - \frac{x \cdot y}{\|x\| \|y\|} \\
/// &= 1 - \frac{\sum_i x_i y_i}{\sqrt{\sum_i x_i^2} \sqrt{\sum_i y_i^2}} \\
/// &= 1 - \cos \theta
/// \end{aligned}
/// ```
///
/// [cosine distance]: https://en.wikipedia.org/wiki/Cosine_similarity
pub fn cosine_distance<T, U>(x: T, y: U) -> T::Value
where
    T: Coordinates,
    U: Coordinates<Value = T::Value>,
    T::Value: Real,
{
    let one: T::Value = one();
    one - cosine_similarity(x, y)
}

/// Equips any [coordinate space] with the [cosine distance] function.
///
/// [coordinate space]: Coordinates
/// [cosine distance]: cosine_distance
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Cosine<T>(pub T);

impl<T> Proximity for Cosine<T>
where
    T: Coordinates,
    T::Value: Real,
{
    type Distance = T::Value;

    fn distance(&self, other: &Self) -> Self::Distance {
        cosine_distance(&self.0, &other.0)
    }
}

impl<T> Proximity<T> for Cosine<T>
where
    T: Coordinates,
    T::Value: Real,
{
    type Distance = T::Value;

    fn distance(&self, other: &T) -> Self::Distance {
        cosine_distance(&self.0, other)
    }
}

impl<T> Proximity<Cosine<T>> for T
where
    T: Coordinates,
    T::Value: Real,
{
    type Distance = T::Value;

    fn distance(&self, other: &Cosine<T>) -> Self::Distance {
        cosine_distance(self, &other.0)
    }
}

/// Compute the [cosine *similarity*] between two pre-normalized (unit magnitude) points.
///
/// Use [`prenorm_cosine_distance()`] instead if you are implementing [`Proximity::distance()`].
///
/// ```math
/// \begin{aligned}
/// \mathrm{prenorm\_cosine\_similarity}(x, y) &= x \cdot y \\
/// &= \sum_i x_i y_i \\
/// &= \cos \theta
/// \end{aligned}
/// ```
///
/// [cosine *similarity*]: https://en.wikipedia.org/wiki/Cosine_similarity
/// [`Proximity::distance()`]: Proximity#tymethod.distance
pub fn prenorm_cosine_similarity<T, U>(x: T, y: U) -> T::Value
where
    T: Coordinates,
    U: Coordinates<Value = T::Value>,
    T::Value: Real,
{
    debug_assert!(x.dims() == y.dims());

    let mut dot: T::Value = zero();

    for i in 0..x.dims() {
        dot += x.coord(i) * y.coord(i);
    }

    dot
}

/// Compute the [cosine distance] between two pre-normalized (unit magnitude) points.
///
/// ```math
/// \begin{aligned}
/// \mathrm{prenorm\_cosine\_distance}(x, y) &= 1 - \mathrm{prenorm\_cosine\_similarity}(x, y) \\
/// &= 1 - x \cdot y \\
/// &= 1 - \sum_i x_i y_i \\
/// &= 1 - \cos \theta
/// \end{aligned}
/// ```
///
/// [cosine distance]: https://en.wikipedia.org/wiki/Cosine_similarity
pub fn prenorm_cosine_distance<T, U>(x: T, y: U) -> T::Value
where
    T: Coordinates,
    U: Coordinates<Value = T::Value>,
    T::Value: Real,
{
    let one: T::Value = one();
    one - prenorm_cosine_similarity(x, y)
}

/// Equips any [coordinate space] with the [cosine distance] function for pre-normalized (unit
/// magnitude) points.
///
/// [coordinate space]: Coordinates
/// [cosine distance]: prenorm_cosine_distance
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PrenormCosine<T>(pub T);

impl<T> Proximity for PrenormCosine<T>
where
    T: Coordinates,
    T::Value: Real,
{
    type Distance = T::Value;

    fn distance(&self, other: &Self) -> Self::Distance {
        prenorm_cosine_distance(&self.0, &other.0)
    }
}

impl<T> Proximity<T> for PrenormCosine<T>
where
    T: Coordinates,
    T::Value: Real,
{
    type Distance = T::Value;

    fn distance(&self, other: &T) -> Self::Distance {
        prenorm_cosine_distance(&self.0, other)
    }
}

impl<T> Proximity<PrenormCosine<T>> for T
where
    T: Coordinates,
    T::Value: Real,
{
    type Distance = T::Value;

    fn distance(&self, other: &PrenormCosine<T>) -> Self::Distance {
        prenorm_cosine_distance(self, &other.0)
    }
}

/// Compute the [angular distance] between two points.
///
/// ```math
/// \begin{aligned}
/// \mathrm{angular\_distance}(x, y) &= \arccos(\mathrm{cosine\_similarity}(x, y)) \\
/// &= \arccos \left( \frac{x \cdot y}{\|x\| \|y\|} \right) \\
/// &= \arccos \left( \frac{\sum_i x_i y_i}{\sqrt{\sum_i x_i^2} \sqrt{\sum_i y_i^2}} \right) \\
/// &= \theta
/// \end{aligned}
/// ```
///
/// [angular distance]: https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity
pub fn angular_distance<T, U>(x: T, y: U) -> AngularDistance<T::Value>
where
    T: Coordinates,
    U: Coordinates<Value = T::Value>,
    T::Value: Real,
{
    AngularDistance::from_cos(cosine_similarity(x, y))
}

/// Equips any [coordinate space] with the [angular distance] metric.
///
/// [coordinate space]: Coordinates
/// [angular distance]: angular_distance
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Angular<T>(pub T);

impl<T> Proximity for Angular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{
    type Distance = AngularDistance<T::Value>;

    fn distance(&self, other: &Self) -> Self::Distance {
        angular_distance(&self.0, &other.0)
    }
}

impl<T> Proximity<T> for Angular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{
    type Distance = AngularDistance<T::Value>;

    fn distance(&self, other: &T) -> Self::Distance {
        angular_distance(&self.0, other)
    }
}

impl<T> Proximity<Angular<T>> for T
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{
    type Distance = AngularDistance<T::Value>;

    fn distance(&self, other: &Angular<T>) -> Self::Distance {
        angular_distance(self, &other.0)
    }
}

/// Angular distance is a metric.
impl<T> Metric for Angular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{}

/// Angular distance is a metric.
impl<T> Metric<T> for Angular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{}

/// Angular distance is a metric.
impl<T> Metric<Angular<T>> for T
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{}

/// Compute the [angular distance] between two points.
///
/// ```math
/// \begin{aligned}
/// \mathrm{prenorm\_angular\_distance}(x, y) &= \arccos(\mathrm{prenorm\_cosine\_similarity}(x, y)) \\
/// &= \arccos(x \cdot y) \\
/// &= \arccos \left( \sum_i x_i y_i \right) \\
/// &= \theta
/// \end{aligned}
/// ```
///
/// [angular distance]: https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity
pub fn prenorm_angular_distance<T, U>(x: T, y: U) -> AngularDistance<T::Value>
where
    T: Coordinates,
    U: Coordinates<Value = T::Value>,
    T::Value: Real,
{
    AngularDistance::from_cos(prenorm_cosine_similarity(x, y))
}

/// Equips any [coordinate space] with the [angular distance] metric for pre-normalized (unit
/// magnitude) points.
///
/// [coordinate space]: Coordinates
/// [angular distance]: prenorm_angular_distance
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PrenormAngular<T>(pub T);

impl<T> Proximity for PrenormAngular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{
    type Distance = AngularDistance<T::Value>;

    fn distance(&self, other: &Self) -> Self::Distance {
        prenorm_angular_distance(&self.0, &other.0)
    }
}

impl<T> Proximity<T> for PrenormAngular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{
    type Distance = AngularDistance<T::Value>;

    fn distance(&self, other: &T) -> Self::Distance {
        prenorm_angular_distance(&self.0, other)
    }
}

impl<T> Proximity<PrenormAngular<T>> for T
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{
    type Distance = AngularDistance<T::Value>;

    fn distance(&self, other: &PrenormAngular<T>) -> Self::Distance {
        prenorm_angular_distance(self, &other.0)
    }
}

/// Angular distance is a metric.
impl<T> Metric for PrenormAngular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{}

/// Angular distance is a metric.
impl<T> Metric<T> for PrenormAngular<T>
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{}

/// Angular distance is a metric.
impl<T> Metric<PrenormAngular<T>> for T
where
    T: Coordinates,
    T::Value: Real,
    AngularDistance<T::Value>: Distance,
{}

/// An [angular distance].
///
/// This type stores the cosine of the angle, to avoid computing the expensive trancendental
/// `acos()` function until absolutely necessary.
///
///     # use acap::distance::Distance;
///     # use acap::cos::AngularDistance;
///     # use std::convert::TryFrom;
///     let zero = AngularDistance::from_cos(1.0);
///     let pi_2 = AngularDistance::from_cos(0.0);
///     let pi = AngularDistance::from_cos(-1.0);
///     assert!(zero < pi_2 && pi_2 < pi);
///
/// [angular distance]: https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AngularDistance<T>(T);

impl<T: Real + Value> AngularDistance<T> {
    /// Creates an `AngularDistance` from the cosine of an angle.
    pub fn from_cos(value: T) -> Self {
        Self(value)
    }

    /// Get the cosine of this angle.
    pub fn cos(self) -> T {
        self.0
    }
}

impl<T: PartialOrd> PartialOrd for AngularDistance<T> {
    fn partial_cmp(&self, other: &AngularDistance<T>) -> Option<Ordering> {
        // acos() is decreasing, so swap the comparison order
        other.0.partial_cmp(&self.0)
    }
}

/// Error type for failed conversions from angles outside of `$[0, \pi]$` to [`AngularDistance`].
#[derive(Debug)]
pub struct InvalidAngleError;

macro_rules! impl_distance {
    ($f:ident) => {
        impl TryFrom<$f> for AngularDistance<$f> {
            type Error = InvalidAngleError;

            #[inline]
            fn try_from(value: $f) -> Result<Self, Self::Error> {
                if value >= 0.0 && value <= std::$f::consts::PI {
                    Ok(Self(value.cos()))
                } else {
                    Err(InvalidAngleError)
                }
            }
        }

        impl From<AngularDistance<$f>> for $f {
            #[inline]
            fn from(value: AngularDistance<$f>) -> $f {
                value.0.acos()
            }
        }

        impl PartialOrd<$f> for AngularDistance<$f> {
            #[inline]
            fn partial_cmp(&self, other: &$f) -> Option<Ordering> {
                self.value().partial_cmp(other)
            }
        }

        impl PartialOrd<AngularDistance<$f>> for $f {
            #[inline]
            fn partial_cmp(&self, other: &AngularDistance<$f>) -> Option<Ordering> {
                self.partial_cmp(&other.value())
            }
        }

        impl PartialEq<$f> for AngularDistance<$f> {
            #[inline]
            fn eq(&self, other: &$f) -> bool {
                self.value() == *other
            }
        }

        impl PartialEq<AngularDistance<$f>> for $f {
            #[inline]
            fn eq(&self, other: &AngularDistance<$f>) -> bool {
                *self == other.value()
            }
        }

        impl Distance for AngularDistance<$f> {
            type Value = $f;
        }
    };
}

impl_distance!(f32);
impl_distance!(f64);

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

    use std::f64::consts::{FRAC_PI_2, FRAC_PI_4, PI, SQRT_2};

    #[test]
    fn test_cosine() {
        assert_eq!(cosine_distance([3.0, 4.0], [3.0, 4.0]), 0.0);
        assert_eq!(cosine_distance([3.0, 4.0], [-4.0, 3.0]), 1.0);
        assert_eq!(cosine_distance([3.0, 4.0], [-3.0, -4.0]), 2.0);
        assert_eq!(cosine_distance([3.0, 4.0], [4.0, -3.0]), 1.0);
    }

    #[test]
    fn test_prenorm_cosine() {
        assert_eq!(prenorm_cosine_distance([0.6, 0.8], [0.6, 0.8]), 0.0);
        assert_eq!(prenorm_cosine_distance([0.6, 0.8], [-0.8, 0.6]), 1.0);
        assert_eq!(prenorm_cosine_distance([0.6, 0.8], [-0.6, -0.8]), 2.0);
        assert_eq!(prenorm_cosine_distance([0.6, 0.8], [0.8, -0.6]), 1.0);
    }

    #[test]
    fn test_angular() {
        let zero = angular_distance([3.0, 4.0], [3.0, 4.0]);
        let pi_4 = Angular([0.0, 1.0]).distance(&Angular([1.0, 1.0]));
        let pi_2 = Angular([3.0, 4.0]).distance(&[-4.0, 3.0]);
        let pi = [3.0, 4.0].distance(&Angular([-3.0, -4.0]));

        assert_eq!(zero.cos(), 1.0);
        assert_eq!(pi_2.cos(), 0.0);
        assert_eq!(pi.cos(), -1.0);

        assert_eq!(zero, 0.0);

        assert!(zero < pi_4);
        assert!(zero < pi_2);
        assert!(zero < pi);

        assert!(pi_4 < pi_2);
        assert!(pi_4 < pi);

        assert!(pi_2 < pi);

        assert!(FRAC_PI_4 < pi_2);
        assert!(pi_2 > FRAC_PI_4);

        assert!(pi_2 < PI);
        assert!(PI > pi_2);

        assert!((pi_4.value() - FRAC_PI_4).abs() < 1.0e-9);
        assert!((pi_2.value() - FRAC_PI_2).abs() < 1.0e-9);
        assert!((pi.value() - PI).abs() < 1.0e-9);
    }

    #[test]
    fn test_prenorm_angular() {
        let sqrt_2_inv = 1.0 / SQRT_2;

        let zero = prenorm_angular_distance([0.6, 0.8], [0.6, 0.8]);
        let pi_4 = PrenormAngular([0.0, 1.0]).distance(&PrenormAngular([sqrt_2_inv, sqrt_2_inv]));
        let pi_2 = PrenormAngular([0.6, 0.8]).distance(&[-0.8, 0.6]);
        let pi = [0.6, 0.8].distance(&PrenormAngular([-0.6, -0.8]));

        assert_eq!(zero.cos(), 1.0);
        assert_eq!(pi_2.cos(), 0.0);
        assert_eq!(pi.cos(), -1.0);

        assert_eq!(zero, 0.0);

        assert!(zero < pi_4);
        assert!(zero < pi_2);
        assert!(zero < pi);

        assert!(pi_4 < pi_2);
        assert!(pi_4 < pi);

        assert!(pi_2 < pi);

        assert!(FRAC_PI_4 < pi_2);
        assert!(pi_2 > FRAC_PI_4);

        assert!(pi_2 < PI);
        assert!(PI > pi_2);

        assert!((pi_4.value() - FRAC_PI_4).abs() < 1.0e-9);
        assert!((pi_2.value() - FRAC_PI_2).abs() < 1.0e-9);
        assert!((pi.value() - PI).abs() < 1.0e-9);
    }
}