如何使用hmatrix构建零矩阵?

And*_*yuk 2 haskell

尝试使用hmatrix,创建零marix.出于某种原因,当我在命令行上尝试此操作时,它可以工作:

buildMatrix 2 3 (\(r,c) -> fromIntegral 0)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在我的代码中执行相同的操作时:

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
Run Code Online (Sandbox Code Playgroud)

它失败:

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.
Run Code Online (Sandbox Code Playgroud)

小智 5

您还可以使用konstNumeric.Container:

import Numeric.LinearAlgebra

m = konst 0 (2,3) :: Matrix Double

v = konst 7 10 :: Vector (Complex Float)
Run Code Online (Sandbox Code Playgroud)