[Haskell-cafe] Library for Sparse Vectors?

Henning Thielemann lemming at henning-thielemann.de
Wed Jul 27 00:35:12 CEST 2011


On Tue, 26 Jul 2011, Alexander Solla wrote:

> Given two (IntMap Double)s a and b, I would compute the projection of a along b as
> 
> cosineSimilarity :: IntMap Double -> IntMap Double -> Double
> cosineSimilarity a b  = (dot a b) / ((norm a) * (norm b)) where
>                  dot  = sum . elems . intersectionWith (*)
>                  norm = (**0.5) . sum . fmap (**2) . elems

Never write (**2) and (**0.5)! Use (^2) and sqrt!

http://www.haskell.org/haskellwiki/Power_function



More information about the Haskell-Cafe mailing list