使用 hmatrix(带矢量化)应用逐元素数学函数

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

我需要以一种有效的方式对 Haskell 中向量的元素应用一个函数,这意味着我不是在寻找这样的东西:

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

更具体地说,在使用 Haskell 的 hmatrix 中是否有 exp, log, ... etc 用于逐元素向量运算,类似于在 numpy 中使用 Python 的对应元素?如果我不使用矢量处理功能,我的代码运行速度会很慢。

如果您正在使用 hmatrixyou 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)))