private float cosineSimilarity ( float [ ] target , int pos , float [ ] vecs ) { float dotProd = 0 , tsqr = 0 , csqr = 0 ; for ( int i = 0 ; i < target . length ; i ++ ) { dotProd += target [ i ] * vecs [ pos + i ] ; tsqr += Math . pow ( target [ i ] , 2 ) ; csqr += Math . pow ( vecs [ pos + i ] , 2 ) ; } return ( float ) ( dotProd / ( Math . sqrt ( tsqr ) * Math . sqrt ( csqr ) ) ) ; }
Basic calculation of cosine similarity