尝试使用hmatrix创建一个零矩阵。出于某些原因,当我在命令行上尝试时,它可以工作:
buildMatrix 2 3 (\(r,c) -> fromIntegral 0)然而,当我尝试在我的代码中做同样的事情时:
type Dim = (Int, Int)
buildFull :: Matrix Double -> Vector Int -> Vector Int -> Dim -> Int
buildFull matrix basic nonbasic (m, n) = do
-- Build mxn matrix of zeroes
let f = buildMatrix m n (\(r,c) -> fromIntegral 0)
m它失败了:
Pivot.hs:23:17:
Ambiguous type variable `a0' in the constraints:
(Element a0) arising from a use of `buildMatrix'
at Pivot.hs:23:17-27
(Num a0) arising from a use of `fromIntegral' at Pivot.hs:23:44-55
Probable fix: add a type signature that fixes these type variable(s)
In the expression: buildMatrix m n (\ (r, c) -> fromIntegral 0)
In an equation for `f':
f = buildMatrix m n (\ (r, c) -> fromIntegral 0)
In the expression:
do { let f = buildMatrix m n (\ (r, c) -> ...);
m }
Failed, modules loaded: none.发布于 2011-12-09 01:20:50
type Dim = (Int, Int)
buildFull :: Matrix Double -> Vector Int -> Vector Int -> Dim -> Int
buildFull matrix basic nonbasic (m, n) = do
-- Build mxn matrix of zeroes
let f = buildMatrix m n (\(r,c) -> fromIntegral 0)
m首先,要使用do-notation,您需要一个一元返回类型,这样即使在修复了不明确的元素类型之后也不会进行编译( @Carl提醒我,这里只有一个表达式,所以不需要(>>=)或(>>) )。
关于元素类型,在let绑定中,没有办法知道应该使用哪种类型,fromIntegral应该返回Double、Integer还是其他任何类型。通常,要使用的类型可以通过使用它的表达式从上下文中推断出来。在这里,没有地方使用f,所以没有上下文。因此,在这种情况下,您必须通过签名指定类型,这可以是
let f :: Matrix Double
f = buildMatrix m n (const 0)或
let f = buildMatrix m n (\_ -> (0 :: Double))如果您希望元素类型为Double。
发布于 2011-12-09 02:12:46
您也可以使用Numeric.Container中的konst
import Numeric.LinearAlgebra
m = konst 0 (2,3) :: Matrix Double
v = konst 7 10 :: Vector (Complex Float)发布于 2011-12-09 01:17:35
用0::Double替换fromIntegral 0。否则,您想要构建的矩阵类型将受到不足的约束。在提示符下,扩展的默认规则可能正在为您解决这个问题。
https://stackoverflow.com/questions/8434808
复制相似问题