function dotp($arr1, $arr2){ return array_sum(array_map(create_function('$a, $b', 'return $a * $b;'), $arr1, $arr2)); } function cosineSim($arr1, $arr2){ return dotp($arr1,$arr2)/sqrt(dotp($arr1,$arr1)*dotp($arr2,$arr2)); }