Haskell hmatrix:使用向量/标量交互键入错误?

时间:2016-05-20 20:29:54

标签: haskell types hmatrix

我遇到了这些在hmatrix中表现奇怪的标量和矩阵的奇怪情况。如果我没有输入注释,那么该操作将自动运行ala matlab / python。但是如果我用“R”或类型或“Double”键入标注标量,我会收到类型错误。

为什么会这样?

λ> 4 +  ([1,2,3] :: Vector R) 
[5.0,6.0,7.0]
λ> (4 :: R) +  ([1,2,3] :: Vector R) 

<interactive>:155:14:
    Couldn't match type ‘Vector R’ with ‘Double’
    Expected type: R
      Actual type: Vector R
    In the second argument of ‘(+)’, namely ‘([1, 2, 3] :: Vector R)’
    In the expression: (4 :: R) + ([1, 2, 3] :: Vector R)
    In an equation for ‘it’: it = (4 :: R) + ([1, 2, 3] :: Vector R)
λ> (4 :: Double) +  ([1,2,3] :: Vector R) 

<interactive>:156:19:
    Couldn't match expected type ‘Double’ with actual type ‘Vector R’
    In the second argument of ‘(+)’, namely ‘([1, 2, 3] :: Vector R)’
    In the expression: (4 :: Double) + ([1, 2, 3] :: Vector R)
    In an equation for ‘it’:
        it = (4 :: Double) + ([1, 2, 3] :: Vector R)
λ> (4 :: R) *  ([1,2,3] :: Vector R) 

<interactive>:157:14:
    Couldn't match type ‘Vector R’ with ‘Double’
    Expected type: R
      Actual type: Vector R
    In the second argument of ‘(*)’, namely ‘([1, 2, 3] :: Vector R)’
    In the expression: (4 :: R) * ([1, 2, 3] :: Vector R)
    In an equation for ‘it’: it = (4 :: R) * ([1, 2, 3] :: Vector R)
λ> 4  *  ([1,2,3] :: Vector R) 
[4.0,8.0,12.0]
λ> 

1 个答案:

答案 0 :(得分:2)

我认为记录在案here

  

自动整形尺寸

     

在大多数操作中,单元素向量和矩阵(从   数字文字或使用标量),以及只有一行或的矩阵   列,自动展开以匹配另一个的尺寸   操作数

你必须匹配类型和大小,你的注释类型是标量,但期望在第二个操作数中有一个向量。