ここにはいくつかの問題があるようです。まず、l
inの定義mmult
が不完全なようです。リストを開始してペアを開始しますが、それらを閉じないでください。おそらくあなたは次のことを意味しました:
l = [((i, j), sum [x ! (i, k) * y ! (k, j) | k <- kr]) | i <- ir, j <- jr]
あなたが言ったように、これら2つのモジュールをリンクすることに固有のもう1つの問題は、2つのモジュールのマトリックスに異なるタイプを使用していることです。マトリックスを逆にする最初のものは、それをリストのリストとして扱います。2 つの行列を乗算する 2 つ目は、配列を使用します。2 つを組み合わせるには、表現間で変換できる必要があります。基本的に、次の 2 つの操作が必要です。
fromListMatrix :: [[Rational]] -> Array (Int, Int) Rational
toListMatrix :: Array (Int, Int) Rational -> [[Rational]]
これらがあれば、行列除算を非常に簡単に実装できます。
divideMatrix :: Array (Int, Int) Rational -> Array (Int, Int) Rational -> Array (Int, Int) Rational
divideMatrix a b = mmult a (fromListMatrix (invert (toListMatrix b)))
toListMatrix
実装面では、簡単なので から始めましょう。
toListMatrix mat =
ここで、配列の境界が必要になるので、
toListMatrix mat = let ((x0, x1), (x0', x1')) = bounds mat in
行ごとに作成します。
toListMatrix mat = let ((x0, x1), (x0', x1')) = bounds mat in
[row | rowNum <- range (x1, x1')]
各行は、固定行番号を持つ単純な行列の要素です。
toListMatrix mat = let ((x0, x1), (x0', x1')) = bounds mat in
[[mat ! (pos, rowNum) | pos <- range (x0, x0')] | rowNum <- range (x1, x1')]
に移るfromlistMatrix
:
fromListMatrix mat =
各要素を位置に関連付けて、結果を にフィードしたいarray
ので、次のようにします。
fromListMatrix mat = array ((1, 1), (length (head mat), length mat)) indexedElems where
indexedElems =
まず、行番号を適切に取得する必要があるため、次のようになります。
fromListMatrix mat = array (length (head mat), length mat) indexedElems where
indexedElems = someFunction (zip [1..] mat)
次に、位置番号を入れます。
fromListMatrix mat = array (length (head mat), length mat) indexedElems where
indexedElems = someFunction (map addPositions (zip [1..] mat))
addPositions (rowNum, elems) = zip [(pos, rowNum) | pos <- [1..]] elems
これで、インデックス付き要素の行のリストができました。これを単一のリストに連結する必要があります。
fromListMatrix mat = array (length (head mat), length mat) indexedElems where
indexedElems = concat (map addPositions (zip [1..] mat))
addPositions (rowNum, elems) = zip [(pos, rowNum) | pos <- [1..]] elems
最後に、次map addPositions (zip [1..] mat)
のように単純な形式に変更してコードをクリーンアップしzipWith
ます。
fromListMatrix mat = array (length (head mat), length mat) indexedElems where
indexedElems = concat (zipWith addPositions [1..] mat)
addPositions rowNum elems = zip [(pos, rowNum) | pos <- [1..]] elems
あなたは終わった!