如何使用hmatrix构建零矩阵? [英] How to build matrix of zeros using hmatrix?

查看:145
本文介绍了如何使用hmatrix构建零矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 hmatrix 创建零迈克尔。
出于某种原因,当我在命令行上试用这个命令时,它可以工作:

  buildMatrix 2 3(\( r,c) - > fromInteral 0)

然而,当我尝试做同样的事情我的代码:

  type Dim =(Int,Int)

buildFull :: Matrix Double - > ; Vector Int - > Vector Int - >昏暗 - > Int
buildFull matrix basic nonbasic(m,n)= do
- 构建mxn零矩阵
let f = buildMatrix mn(\(r,c) - > fromInteral 0)
m

失败:

  Pivot.hs:23:17:
约束中含糊不清的类型变量`a0':使用`buildMatrix'$ b引起的
(元素a0) $ b at Pivot.hs:23:17-27
(Num a0)在Pivot.hs:23:44-55
处使用`fromIntegral'引起的可能修复:添加一个类型签名,修复这些类型变量
在表达式中:buildMatrix mn(\(r,c) - > fromIntegral 0)
在等式'f'中:
f = buildMatrix mn (\(r,c) - > fromInteral 0)
在表达式中:
do {let f = buildMatrix mn(\(r,c) - > ...);
m}
失败,模块加载:无。


解决方案

type Dim =(Int,Int)

buildFull :: Matrix Double - > Vector Int - > Vector Int - >昏暗 - > Int
buildFull matrix basic nonbasic(m,n)= do
- 构建mxn零矩阵
let f = buildMatrix mn(\(r,c) - > fromInteral 0)
m

首先,使用 do -notation,你需要一个monadic返回类型,所以即使修复了不明确的元素类型后也不会编译(就像我被@Carl提醒的那样,在这里只有一个表达式,所以没有(>> =)(>>))。



关于元素类型,在let-binding中,无法找出使用哪种类型,是否 fromIntegral 应返回 Double Integer 或其他。通常使用的类型可以从上下文中推断,也可以通过它使用的表达式来推断。在这里, f 没有用到,所以没有上下文。因此,在这种情况下,您必须通过签名来指定类型,可以是

  let f :: Matrix Double 
f = buildMatrix mn(const 0)

  let f = buildMatrix mn(\_  - >(0 :: Double))

如果您希望元素类型为 Double


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.

解决方案

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

First, to use do-notation, you need a monadic return type, so that won't compile even after fixing the ambiguous element type (as I was reminded by @Carl, it would be okay here while there's only a single expression so that no (>>=) or (>>) is needed).

Concerning the element type, in the let-binding, there is no way to find out which type to use, whether fromIntegral should return Double, Integer or whatever. Often the type to be used can be inferred from context, by the expressions it is used in. Here, f is nowhere used, so there's no context. Hence in this situation, you have to specify the type by a signature, that can be

let f :: Matrix Double
    f = buildMatrix m n (const 0)

or

let f = buildMatrix m n (\_ -> (0 :: Double))

if you want the element type o be Double.

这篇关于如何使用hmatrix构建零矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆