3 (declaim (optimize (speed 3) (debug 0)))
5 (declaim (inline diff-of-squares
))
6 (declaim (ftype (function (number number
) float
) square-of-difference
))
7 (defun square-of-difference (a b
)
11 (declaim (inline diff-of-squares
))
12 (declaim (ftype (function (number number
) float
) absolute-difference
))
13 (defun absolute-difference (a b
)
16 (defmacro def-vec2fun
((name) documentation
&body body
)
17 "Declare a function 'name' of two vectors, a and b.
18 Add the optiomization declare and array dimension verification"
19 `(declaim (ftype (function (simple-array simple-array
) float
) ,name
))
22 (assert (= (array-dimension a
0)
23 (array-dimension b
0)))
30 (def-vec2fun (euclidean-distance)
31 "Calculate the Euclidean distance between two vectors"
32 (sqrt (reduce '+ (map 'vector
#'square-of-difference a b
))))
34 (def-vec2fun (manhatan-distance)
35 "Manhatan, or city block, or norm-1 distance"
36 (reduce '+ (map 'vector
#'absolute-difference a b
)))
41 (defun vectors-statistics (a b
)
42 (loop for i from
0 to
(1- (array-dimension a
0))
47 summing
(sq x
) into Sx2
48 summing
(sq y
) into Sy2
49 summing
(* x y
) into Sxy
50 finally
(return (values Sx Sy Sx2 Sy2 Sxy
))))
53 (def-vec2fun (pearson-correlation)
55 (let ((N (array-dimension a
0)))
56 (mvbind (Sx Sy Sx2 Sy2 Sxy
) (vectors-statistics a b
)
57 (/ (- Sxy
(/ (* Sx Sy
) N
))
58 (sqrt (* (- Sx2
(/ (sq Sx
) N
))
59 (- Sy2
(/ (sq Sy
) N
))))))))
61 (def-vec2fun (pearson-distance)
63 (- 1.0 (pearson-correlation a b
)))
65 (def-vec2fun (absolute-pearson-distance)
66 "Absolute Pearson distance"
67 (- 1.0 (abs (pearson-correlation a b
))))
69 (def-vec2fun (square-pearson-distance)
70 "Square Pearson distance"
71 (- 1.0 (sq (pearson-correlation a b
))))