Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

haskell - How to build matrix of zeros using hmatrix?

Trying to use hmatrix, to create a zero marix. For some reason, when I try this on command line, it works:

buildMatrix 2 3 ((r,c) -> fromIntegral 0)

However, when I try to do the same thing in my code:

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

it fails:

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can also use konst from Numeric.Container:

import Numeric.LinearAlgebra

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

v = konst 7 10 :: Vector (Complex Float)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...