2

我遇到了这些奇怪的标量和矩阵在 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]
λ> 
4

1 回答 1

2

我认为它记录在这里

自动适应尺寸

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

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

于 2016-05-20T20:45:50.970 回答