我正在尝试学习 Haskell,我决定通过编写一个简单的函数来练习反转 3x3 矩阵。这应该很容易,但是我尝试的所有内容都无法成功编译。
这是我的代码:
matInv3x3 :: [[Double]] -> [[Double]]
matInv3x3 m
| length m /= 3 = error "wrong number of rows"
| length (m !! 0) /= 3 = error "wrong number of elements in row 0"
| length (m !! 1) /= 3 = error "wrong number of elements in row 1"
| length (m !! 2) /= 3 = error "wrong number of elements in row 2"
| det == 0 = error "zero determinant"
| otherwise = mInv
where a = m !! 0 !! 0
b = m !! 0 !! 1
c = m !! 0 !! 2
d = m !! 1 !! 0
e = m !! 1 !! 1
f = m !! 1 !! 2
g = m !! 2 !! 0
h = m !! 2 !! 1
i = m !! 2 !! 2
det = a*(e*i - f*h) - b*(i*d - f*g) + c*(d*h - e*g)
A = (e*i - f*h) / det
B = -(d*i - f*g) / det
C = (d*h - e*g) / det
D = -(b*i - c*h) / det
E = (a*i - c*g) / det
F = -(a*h - b*g) / det
G = (b*f - c*e) / det
H = -(a*f - c*d) / det
I = (a*e - b*d) / det
mInv = [[A,B,C],[D,E,F],[G,H,I]]
我正在尝试防范一切可能出错的情况:不良的列表维度和零行列式。我参考了“学习您的...”书中的示例。如果矩阵具有零行列式,我正在尝试依赖惰性求值。
GHCi无法编译它,在第10行(定义b的位置)上引用了一个解析错误。我确定我缺少了一些简单而基本的东西。有人能指出我做错了什么吗?
更新:
我实现了评论中提出的修复方法,并纠正了我之前犯下的索引交换错误(之前没有注意到,因为代码无法编译)。这是已修复的代码,可以正确地反转3x3矩阵:
matInv3x3 :: [[Double]] -> [[Double]]
matInv3x3 m
| length m /= 3 = error "wrong number of rows"
| length (m !! 0) /= 3 = error "wrong number of elements in row 0"
| length (m !! 1) /= 3 = error "wrong number of elements in row 1"
| length (m !! 2) /= 3 = error "wrong number of elements in row 2"
| abs det < 1.0e-15 = error "zero or near-zero determinant"
| otherwise = mInv
where [[a,d,g],[b,e,h],[c,f,i]] = m
det = a*(e*i - f*h) - b*(i*d - f*g) + c*(d*h - e*g)
a' = (e*i - f*h) / det
b' = -(d*i - f*g) / det
c' = (d*h - e*g) / det
d' = -(b*i - c*h) / det
e' = (a*i - c*g) / det
f' = -(a*h - b*g) / det
g' = (b*f - c*e) / det
h' = -(a*f - c*d) / det
i' = (a*e - b*d) / det
mInv = [[a',b',c'],[d',e',f'],[g',h',i']]
A
、B
..I
都是无效的。你可以使用_A
。 - user2407038a'
到i'
。 GHC 会特别处理以_
开头的值 - 如果它们未使用,它不会发出警告。我不建议使用这种命名方式。 - Carl[[a,d,g],[b,e,h],[c,f,i]] = m
жқҘдёәзҹ©йҳөзҡ„жүҖжңүе…ғзҙ иҝӣиЎҢиөӢеҖјгҖӮ - max taldykin