简体   繁体   English

Haskell-hMatrix上的实例声明

[英]Haskell - Instance Declaration on hMatrix

I'm using the function fromBlocks from hMatrix over a list whose elements are determined by functions of type Int -> Int -> Int -> Matrix Int . 我在列表上使用fromBlocks的fromBlocks函数,其元素由Int-> Int-> Int- Int -> Int -> Int -> Matrix Int类型的函数确定。 However, GHC complains saying that: 但是,GHC抱怨说:

No instance for (Element Int) arising from a use of `fromBlocks'
    Possible fix: add an instance declaration for (Element Int)
    In the expression:
      fromBlocks [[matrixCreate n m d], [rowZero n m d]]

I tried to tell GHC the type of the result of this computation with :: Matrix Int but it didn't work, and I don't understand how to declare the type when using the function. 我试图用:: Matrix Int告诉GHC该计算结果的类型,但是它不起作用,而且我不理解如何在使用函数时声明类型。

No - there is really no instance for Element Int - see here: http://hackage.haskell.org/package/hmatrix-0.16.0.3/docs/Numeric-LinearAlgebra-HMatrix.html#t:Element 否-确实没有Element Int实例-参见此处: http : //hackage.haskell.org/package/hmatrix-0.16.0.3/docs/Numeric-LinearAlgebra-HMatrix.html#t : Element Int

Just go for Matrix Float or Matrix Double if you can 如果可以的话,就去Matrix FloatMatrix Double

Just declare an instance Element Int as described in [1]. 只需声明一个instance Element Int ,如[1]中所述。 Be warned that many of the fancier functions are only defined for Double and Float . 请注意,许多高级功能仅针对DoubleFloat定义。

[1] https://github.com/albertoruiz/hmatrix/issues/28 [1] https://github.com/albertoruiz/hmatrix/issues/28

Edit : add Alberto's comment: 编辑 :添加阿尔贝托的评论:

instance Element Int

a = (2><3) [1..] :: Matrix Int

z = (1><1) [0] :: Matrix Int

m = fromBlocks [[a,z],[a,a]]

> m

(4><6)
 [ 1, 2, 3, 0, 0, 0
 , 4, 5, 6, 0, 0, 0
 , 1, 2, 3, 1, 2, 3
 , 4, 5, 6, 4, 5, 6 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM