怎么了?此代码应该通过查找a
时间b
倒数来进行矩阵除法。我试图了解它的错误是什么,但我看不到如何链接它。
import List
import Ratio
inverse :: [[Rational]] -> [[Rational]]
inverse mat = sweep ([], zipWith (++) mat unit) where
unit = map (take (length mat)) $ iterate (0 :) (1 : [0,0..])
sweep (xss, []) = xss
sweep (xss, yss) = sweep (xss' ++ [ws], filter (any (/= 0)) yss') where
Just (x : xs) = find ((/= 0) . head) yss
ws = map (/ x) xs
[xss', yss'] = map (map f) [xss, yss]
f (y : ys) = zipWith (\d e -> e - d * y) ws ys
我怎样才能将它与
import Data.Array
mmult :: (Ix i, Num a) => Array (i, i) a -> Array (i, i) a -> Array (i, i) a
mmult x y
| x1 /= y0 || x1' /= y0' = error "range mismatch"
| otherwise = array ((x0, y1), (x0', y1')) l where
((x0, x1), (x0', x1')) = bounds x
((y0, y1), (y0', y1')) = bounds y
ir = range (x0, x0')
jr = range (y1, y1')
kr = range (x1, x1')
l = [((i, j), sum [x ! (i, k) * y ! (k, j) | k <- kr
通过乘以a
和b
的逆来进行矩阵除法。