[ drop length [ <iota> ] keep ]
[ nip <reversed> ]
[ drop ] 2tri
- '[ _ _ <slice> _ v* sum ] map reverse! ;
+ '[ _ _ <slice> _ v. ] map reverse! ;
: p-sq ( p -- p^2 ) dup p* ; inline
! Inefficient
: calculate-row-major-index ( seq shape -- i )
- 1 [ * ] accumulate nip reverse v* sum ;
+ 1 [ * ] accumulate nip reverse v. ;
: calculate-column-major-index ( seq shape -- i )
- 1 [ * ] accumulate nip v* sum ;
+ 1 [ * ] accumulate nip v. ;
: set-shaped-row-major ( obj seq shaped -- )
shaped-bounds-check [ shape calculate-row-major-index ] [ underlying>> ] bi set-nth ;
:: project-pt-line ( p p0 p1 -- q )
p1 p0 v- :> vt
- p p0 v- vt v* sum
+ p p0 v- vt v.
vt norm-sq /
vt n*v p0 v+ ; inline
} cond ;
: det2 ( x y -- z )
- { 1 0 } vshuffle double-2{ 1 -1 } v* v* sum ; inline
+ { 1 0 } vshuffle double-2{ 1 -1 } v* v. ; inline
: det3 ( x y z -- w )
[ cross ] dip v. ; inline
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
-USING: kernel math math.functions math.statistics math.vectors
-sequences sequences.extras ;
+USING: kernel math math.functions math.similarity
+math.statistics math.vectors sequences sequences.extras ;
IN: math.distances
v- vabs supremum ;
: cosine-distance ( a b -- n )
- [ v* sum ] [ [ norm ] bi@ * ] 2bi / 1 swap - ;
+ cosine-similarity 1 swap - ;
: canberra-distance ( a b -- n )
[ v- vabs ] [ [ vabs ] bi@ v+ ] 2bi v/ sum ;
[ v- ] [ v+ ] 2bi [ vabs sum ] bi@ / ;
: correlation-distance ( a b -- n )
- [ demean ] bi@ [ v* sum ] [ [ norm ] bi@ * ] 2bi / 1 swap - ;
+ [ demean ] bi@ cosine-distance ;
over length 3 < [ 2drop 1.0 ] [ population-corr 0.5 * 0.5 + ] if ;
: cosine-similarity ( a b -- n )
- [ v* sum ] [ [ norm ] bi@ * ] 2bi / ;
+ [ v. ] [ [ norm ] bi@ * ] 2bi / ;