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
//! The norm module
//!
//! This module contains implementations of various linear algebra norms.
//! The implementations are contained within the `VectorNorm` and 
//! `MatrixNorm` traits. This module also contains `VectorMetric` and
//! `MatrixMetric` traits which are used to compute the metric distance.
//!
//! These traits can be used directly by importing implementors from
//! this module. In most cases it will be easier to use the `norm` and
//! `metric` functions which exist for both vectors and matrices. These
//! functions take generic arguments for the norm to be used.
//!
//! In general you should use the least generic norm that fits your purpose.
//! For example you would choose to use a `Euclidean` norm instead of an
//! `Lp(2.0)` norm - despite them being mathematically equivalent. 
//!
//! # Defining your own norm
//!
//! Note that these traits enforce no requirements on the norm. It is up
//! to the user to ensure that they define a norm correctly.
//!
//! To define your own norm you need to implement the `MatrixNorm`
//! and/or the `VectorNorm` on your own struct. When you have defined
//! a norm you get the _induced metric_ for free. This means that any
//! object which implements the `VectorNorm` or `MatrixNorm` will
//! automatically implement the `VectorMetric` and `MatrixMetric` traits
//! respectively. This induced metric will compute the norm of the
//! difference between the vectors or matrices.

use matrix::BaseMatrix;
use vector::Vector;
use utils;

use std::ops::Sub;
use libnum::Float;

/// Trait for vector norms
pub trait VectorNorm<T> {
    /// Computes the vector norm.
    fn norm(&self, v: &Vector<T>) -> T;
}

/// Trait for vector metrics.
pub trait VectorMetric<T> {
    /// Computes the metric distance between two vectors.
    fn metric(&self, v1: &Vector<T>, v2: &Vector<T>) -> T;
}

/// Trait for matrix norms.
pub trait MatrixNorm<T, M: BaseMatrix<T>> {
    /// Computes the matrix norm.
    fn norm(&self, m: &M) -> T;
}

/// Trait for matrix metrics.
pub trait MatrixMetric<'a, 'b, T, M1: 'a + BaseMatrix<T>, M2: 'b + BaseMatrix<T>> {
    /// Computes the metric distance between two matrices.
    fn metric(&self, m1: &'a M1, m2: &'b M2) -> T;
}

/// The induced vector metric
///
/// Given a norm `N`, the induced vector metric `M` computes
/// the metric distance, `d`, between two vectors `v1` and `v2`
/// as follows:
///
/// `d = M(v1, v2) = N(v1 - v2)`
impl<U, T> VectorMetric<T> for U
    where U: VectorNorm<T>, T: Copy + Sub<T, Output=T> {
    fn metric(&self, v1: &Vector<T>, v2: &Vector<T>) -> T {
        self.norm(&(v1 - v2))
    }
}

/// The induced matrix metric
///
/// Given a norm `N`, the induced matrix metric `M` computes
/// the metric distance, `d`, between two matrices `m1` and `m2`
/// as follows:
///
/// `d = M(m1, m2) = N(m1 - m2)`
impl<'a, 'b, U, T, M1, M2> MatrixMetric<'a, 'b, T, M1, M2> for U
    where U: MatrixNorm<T, ::matrix::Matrix<T>>,
    M1: 'a + BaseMatrix<T>,
    M2: 'b + BaseMatrix<T>,
    &'a M1: Sub<&'b M2, Output=::matrix::Matrix<T>> {

    fn metric(&self, m1: &'a M1, m2: &'b M2) -> T {
        self.norm(&(m1 - m2))
    }
}

/// The Euclidean norm
///
/// The Euclidean norm computes the square-root
/// of the sum of squares.
///
/// `||v|| = SQRT(SUM(v_i * v_i))`
#[derive(Debug)]
pub struct Euclidean;

impl<T: Float> VectorNorm<T> for Euclidean {
    fn norm(&self, v: &Vector<T>) -> T {
        utils::dot(v.data(), v.data()).sqrt()
    }
}

impl<T: Float, M: BaseMatrix<T>> MatrixNorm<T, M> for Euclidean {
    fn norm(&self, m: &M) -> T {
        let mut s = T::zero();

        for row in m.row_iter() {
            s = s + utils::dot(row.raw_slice(), row.raw_slice());
        }

        s.sqrt()
    }
}

/// The Lp norm
///
/// The
/// [Lp norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm)
/// computes the `p`th root of the sum of elements
/// to the `p`th power.
///
/// The Lp norm requires `p` to be greater than
/// or equal `1`.
///
/// We use an enum for this norm to allow us to explicitly handle
/// special cases at compile time. For example, we have an `Infinity`
/// variant which handles the special case when the `Lp` norm is a
/// supremum over absolute values. The `Integer` variant gives us a
/// performance boost when `p` is an integer.
///
/// You should avoid matching directly against this enum as it is likely
/// to grow.
#[derive(Debug)]
pub enum Lp<T: Float> {
    /// The L-infinity norm (supremum)
    Infinity,
    /// The Lp norm where p is an integer
    Integer(i32),
    /// The Lp norm where p is a float
    Float(T)
}

impl<T: Float> VectorNorm<T> for Lp<T> {
    fn norm(&self, v: &Vector<T>) -> T {
        match *self {
            Lp::Infinity => {
                // Compute supremum
                let mut abs_sup = T::zero();
                for d in v.iter().map(|d| d.abs()) {
                    if d > abs_sup {
                        abs_sup = d;
                    }
                }
                abs_sup
            },
            Lp::Integer(p) => {
                assert!(p >= 1, "p value in Lp norm must be >= 1");
                // Compute standard lp norm
                let mut s = T::zero();
                for x in v {
                    s = s + x.abs().powi(p);
                }
                s.powf(T::from(p).expect("Could not cast i32 to float").recip())
            },
            Lp::Float(p) => {
                assert!(p >= T::one(), "p value in Lp norm must be >= 1");
                // Compute standard lp norm
                let mut s = T::zero();
                for x in v {
                    s = s + x.abs().powf(p);
                }
                s.powf(p.recip())
            }
        }
    }
}

impl<T: Float, M: BaseMatrix<T>> MatrixNorm<T, M> for Lp<T> {
    fn norm(&self, m: &M) -> T {
        match *self {
            Lp::Infinity => {
                // Compute supremum
                let mut abs_sup = T::zero();
                for d in m.iter().map(|d| d.abs()) {
                    if d > abs_sup {
                        abs_sup = d;
                    }
                }
                abs_sup
            },
            Lp::Integer(p) => {
                assert!(p >= 1, "p value in Lp norm must be >= 1");
                // Compute standard lp norm
                let mut s = T::zero();
                for x in m.iter() {
                    s = s + x.abs().powi(p);
                }
                s.powf(T::from(p).expect("Could not cast i32 to float").recip())
            },
            Lp::Float(p) => {
                assert!(p >= T::one(), "p value in Lp norm must be >= 1");
                // Compute standard lp norm
                let mut s = T::zero();
                for x in m.iter() {
                    s = s + x.abs().powf(p);
                }
                s.powf(p.recip())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use libnum::Float;
    use std::f64;

    use super::*;
    use vector::Vector;
    use matrix::{Matrix, MatrixSlice};

    #[test]
    fn test_euclidean_vector_norm() {
        let v = vector![3.0, 4.0];
        assert_scalar_eq!(VectorNorm::norm(&Euclidean, &v), 5.0, comp = float);
    }

    #[test]
    fn test_euclidean_matrix_norm() {
        let m = matrix![3.0, 4.0;
                        1.0, 3.0];
        assert_scalar_eq!(MatrixNorm::norm(&Euclidean, &m), 35.0.sqrt(), comp = float);
    }

    #[test]
    fn test_euclidean_matrix_slice_norm() {
        let m = matrix![3.0, 4.0;
                        1.0, 3.0];

        let slice = MatrixSlice::from_matrix(&m, [0,0], 1, 2);
        assert_scalar_eq!(MatrixNorm::norm(&Euclidean, &slice), 5.0, comp = float);
    }

    #[test]
    fn test_euclidean_vector_metric() {
        let v = vector![3.0, 4.0];
        assert_scalar_eq!(VectorMetric::metric(&Euclidean, &v, &v), 0.0, comp = float);

        let v1 = vector![0.0, 0.0];
        assert_scalar_eq!(VectorMetric::metric(&Euclidean, &v, &v1), 5.0, comp = float);

        let v2 = vector![4.0, 3.0];
        assert_scalar_eq!(VectorMetric::metric(&Euclidean, &v, &v2), 2.0.sqrt(), comp = float);
    }

    #[test]
    #[should_panic]
    fn test_euclidean_vector_metric_bad_dim() {
        let v = vector![3.0, 4.0];
        let v2 = vector![1.0, 2.0, 3.0];

        VectorMetric::metric(&Euclidean, &v, &v2);
    }

    #[test]
    fn test_euclidean_matrix_metric() {
        let m = matrix![3.0, 4.0;
                        1.0, 3.0];
        assert_scalar_eq!(MatrixMetric::metric(&Euclidean, &m, &m), 0.0, comp = float);

        let m1 = Matrix::zeros(2, 2);
        assert_scalar_eq!(MatrixMetric::metric(&Euclidean, &m, &m1), 35.0.sqrt(), comp = float);

        let m2 = matrix![2.0, 3.0;
                         2.0, 4.0];
        assert_scalar_eq!(MatrixMetric::metric(&Euclidean, &m, &m2), 2.0, comp = float);
    }

    #[test]
    #[should_panic]
    fn test_euclidean_matrix_metric_bad_dim() {
        let m = matrix![3.0, 4.0];
        let m2 = matrix![1.0, 2.0, 3.0];

        MatrixMetric::metric(&Euclidean, &m, &m2);
    }

    #[test]
    fn test_euclidean_matrix_slice_metric() {
        let m = matrix![
            1.0, 1.0, 1.0;
            1.0, 1.0, 1.0;
            1.0, 1.0, 1.0
        ];

        let m2 = matrix![
            0.0, 0.0, 0.0;
            0.0, 0.0, 0.0;
            0.0, 0.0, 0.0
        ];

        let m_slice = MatrixSlice::from_matrix(
            &m, [0; 2], 1, 2
        );

        let m2_slice = MatrixSlice::from_matrix(
            &m2, [0; 2], 1, 2
        );

        assert_scalar_eq!(MatrixMetric::metric(&Euclidean, &m_slice, &m2_slice), 2.0.sqrt(), comp = exact);
    }

    #[test]
    #[should_panic]
    fn test_euclidean_matrix_slice_metric_bad_dim() {
        let m = matrix![3.0, 4.0];
        let m2 = matrix![1.0, 2.0, 3.0];

        let m_slice = MatrixSlice::from_matrix(
            &m, [0; 2], 1, 1
        );

        let m2_slice = MatrixSlice::from_matrix(
            &m2, [0; 2], 1, 2
        );

        MatrixMetric::metric(&Euclidean, &m_slice, &m2_slice);
    }

    #[test]
    fn test_lp_vector_supremum() {
        let v = vector![-5.0, 3.0];

        let sup = VectorNorm::norm(&Lp::Infinity, &v);
        assert_eq!(sup, 5.0);
    }

    #[test]
    fn test_lp_matrix_supremum() {
        let m = matrix![0.0, -2.0;
                        3.5, 1.0];

        let sup = MatrixNorm::norm(&Lp::Infinity, &m);
        assert_eq!(sup, 3.5);
    }

    #[test]
    fn test_lp_vector_one() {
        let v = vector![1.0, 2.0, -2.0];
        assert_eq!(VectorNorm::norm(&Lp::Integer(1), &v), 5.0);
    }

    #[test]
    fn test_lp_matrix_one() {
        let m = matrix![1.0, -2.0;
                        0.5, 1.0];
        assert_eq!(MatrixNorm::norm(&Lp::Integer(1), &m), 4.5);
    }

    #[test]
    fn test_lp_vector_float() {
        let v = vector![1.0, 2.0, -2.0];
        assert_eq!(VectorNorm::norm(&Lp::Float(1.0), &v), 5.0);
    }

    #[test]
    fn test_lp_matrix_float() {
        let m = matrix![1.0, -2.0;
                        0.5, 1.0];
        assert_eq!(MatrixNorm::norm(&Lp::Float(1.0), &m), 4.5);
    }

    #[test]
    #[should_panic]
    fn test_lp_vector_bad_p() {
        let v = Vector::new(vec![]);
        VectorNorm::norm(&Lp::Float(0.5), &v);
    }

    #[test]
    #[should_panic]
    fn test_lp_matrix_bad_p() {
        let m = matrix![];
        MatrixNorm::norm(&Lp::Float(0.5), &m);
    }

    #[test]
    #[should_panic]
    fn test_lp_vector_bad_int_p() {
        let v: Vector<f64> = Vector::new(vec![]);
        VectorNorm::norm(&Lp::Integer(0), &v);
    }

    #[test]
    #[should_panic]
    fn test_lp_matrix_bad_int_p() {
        let m: Matrix<f64> = matrix![];
        MatrixNorm::norm(&Lp::Integer(0), &m);
    }
}