我正在尝试编写一些简单的反向替换矩阵代码。
为此我写了一个fold
运算符,它用行折叠。
这是函数及其实现的非泛型类型签名:
foldMatrixByRow :: (Array D DIM1 Double -> Array D DIM1 Double -> Int -> Array D DIM1 Double)
-> Array D DIM1 Double
-> Array U DIM2 Double
-> Array D DIM1 Double
foldMatrixByRow f intial mat = foldMatrixByRowUtil f intial (mat, 0)
where foldMatrixByRowUtil f initial (mat, i)
| i == n = initial
| otherwise = foldMatrixByRowUtil f result (mat, i + 1)
where result = f matRow initial i
matRow = delayNRow mat i
(Z :. n :. _) = extent mat
foldS
,foldP
等与上述折叠的区别在于,折叠功能需要2 DIM1
延迟 数组作为参数。Array D DIM1 Double
)。以下是后置替换的代码
backsubstitution :: Array U DIM2 Double -> Array U DIM1 Double -> Array D DIM1 Double
backsubstitution mat bs = foldMatrixByRow f initial mat
where
f matRow sols i = fromFunction (extent bs) intArr
where
intArr (Z :. j) | i == j = rowComp | j < i = 0.0 | otherwise = sols R.! (Z :. j)
intc = ((bs ! (Z :. i)) - (rowComp)) / (matRow ! (Z :.i))
rowComp = sumAllS $ traverse2Backwards matRow sols g
g m s (i', _) = if i < i' then m * s else 0.0
initial = fromFunction (extent bs) (\ _ -> 0)
initial
延迟 。 matRow
由delayNRow
计算,它还计算 延迟 数组(delayNRow
代码在下一个项目符号后提供。) fromFunction
类型签名肯定会返回 延迟 数组。
delayNRow :: Array U DIM2 Double -> Int -> Array D DIM1 Double
delayNRow arr n = R.slice arr (Any :. n :. All)
但这是问题所在:
为什么会出现以下错误:
Couldn't match type `D' with `U'
Expected type: Array D DIM1 Double -> Array D DIM1 Double -> Int -> Array D DIM1 Double
Actual type: Array U DIM1 Double -> Array U DIM1 Double -> Int -> Array D DIM1 Double
In the first argument of `foldMatrixByRow', namely `f'
In the expression: foldMatrixByRow f initial mat
In an equation for `backsubstitution':
In an equation for `backsubstitution':
backsubstitution mat bs
= foldMatrixByRow f initial mat
where
f matRow sols i
= fromFunction (extent bs) intArr
where ...
tldr;
为什么我收到f
backsubstitution
中U
的输入为D
数组的错误,而我提供的数据 延迟 < / strong>({{1}})。