我刚刚开始学习 Haskell,但遇到了问题 使用 hmatrix 库。我想写一些简单的代码 计算 eigenvalue使用power iteration方法。 我从以下内容开始:
c = fromList [4.0, 4.0, 4.0, 4.0]::Vector Double
n = norm2 c
它创建一个向量 c 并找到该向量的 2-范数。
与c
相乘:
c * 2 (Works)
c * 0.5 (Works)
c * pi (Works)
c * n (Error)
我检查过:
>:t pi
pi :: Floating a => a
>:t n
n :: Double
问题出在类型上,但我不知道如何获取 围绕它。
在这种情况下我需要定义自己的 (/)
函数吗?
更新: 我从 ghci 获得的错误:
Couldn't match expected type `Vector Double'
with actual type `Double'
In the second argument of `(*)', namely `n'
In the expression: c * n
In an equation for `it': it = c * n
最佳答案
通过检查类型,您正在做正确的事情。如果我们更明确一点,我们就能看到发生了什么。
Prelude Numeric.LinearAlgebra> :t let a = 2; b = c * a in a
let a = 2; b = c * a in a :: Vector Double
问题是 norm2 c
的类型是 Double
,因此无法制成 Vector Double
让我们看看之前的多态 2
的值。
Prelude Numeric.LinearAlgebra> let a = 2; b = c * a in a
fromList [2.0]
因此,n = fromList [norm2 c]
编辑:同一个库公开了您应该研究的函数scalar
和scale
。
关于haskell - 在haskell中使用hmatrix求向量范数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827791/