Apply element-wise mathematical function using hmatrix (with vectorization)

367 Views Asked by At

I need to apply a function on elements of a vector in Haskell in an efficient way, which implies that i am not looking for something like this:

sigmoid :: [Float] -> [Float]
sigmoid [] = []
sigmoid (z:zs) = ( 1/(1+exp (-z)) ):(sigmoid zs) 

To be more specific, are there exp, log, ... etc for element-wise vector operations in hmatrix using Haskell, similarly to their counterparts in numpy using Python? My code runs very slowly if I am not using vector processing capabilities.

1

There are 1 best solutions below

5
duplode On BEST ANSWER

If you are using hmatrix, you are probably looking for cmap:

cmap :: (Element b, Container c e) => (e -> b) -> c e -> c b

like fmap (cannot implement instance Functor because of Element class constraint)

sigmoid :: Vector Double -> Vector Double
sigmoid = cmap (\z -> 1/(1+exp (-z)))