2012-06-25 64 views
-1

我的想法是在Haskell中編寫一個矩陣分區作爲Muliply A* Inverse B矩陣分區出現問題

我寫了一些代碼來做到這一點,但編譯器有阻止我的想法。它顯示以下錯誤:

Invalid type signature :MatrixDivision :: Matrix-> Matrix-> Matrix at line 86:1 
Should be of (variable)::(Type) 

我該怎麼做?這是代碼:

import List 

type Vector = [Int] 
type Matrix = [Vector] 

--basic constructions for vectors 

zeroVector :: Int -> Vector 
zeroVector n = replicate n 0 

--basic operations for vectors 

dotProduct :: Vector -> Vector -> Int 
dotProduct v w = sum (zipWith (*) v w) 

vectorSum :: Vector -> Vector -> Vector 
vectorSum = zipWith (+) 

vectorScalarProduct :: Int -> Vector -> Vector 
vectorScalarProduct n vec = [ n * x | x <- vec ] 

--basic constructions for matrices 

-- elemMatrix n i j v is the n-by-n elementary matrix with 
-- entry v in the (i,j) place 
elemMatrix :: Int -> Int -> Int -> Int -> Matrix 
elemMatrix n i j v = 
    [ [ entry row column | column <- [1..n] ] | row <- [1..n] ] 
    where 
    entry x y 
    | x == y   = 1 
    | x == i && y == j = v 
    | otherwise  = 0 

idMatrix :: Int -> Matrix 
idMatrix n = elemMatrix n 1 1 1 

zeroMatrix :: Int -> Int -> Matrix 
zeroMatrix i j = replicate i (zeroVector j) 

--basic operations for matrices 

matrixSum :: Matrix -> Matrix -> Matrix 
matrixSum = zipWith vectorSum 

matrixScalarProduct :: Int -> Matrix -> Matrix 
matrixScalarProduct n m = [ vectorScalarProduct n row | row <- m ] 

matrixProduct :: Matrix -> Matrix -> Matrix 
matrixProduct m n = [ map (dotProduct r) (transpose n) | r <- m ] 

{- The determinant and inverse functions given here are only for examples 
of Haskell syntax. Efficient versions using row operations are implemented 
in RowOperations.hs .-} 

--determinant using cofactors 

remove :: Matrix -> Int -> Int -> Matrix 
remove m i j 
    | m == [] || i < 1 || i > numRows m || j < 1 || j > numColumns m = 
    error "(i,j) out of range" 
    | otherwise = transpose (cut (transpose (cut m i)) j) 

determinant :: Matrix -> Int 
determinant [] = error "determinant: 0-by-0 matrix" 
determinant [[n]] = n 
determinant m = sum [ (-1)^(j+1) * (head m)!!(j-1) * determinant (remove m 1 j) | j <- [1..(numColumns m) ] ] 

--inverse 

cofactor :: Matrix -> Int -> Int -> Int 
cofactor m i j = (-1)^(i+j) * determinant (remove m i j) 

cofactorMatrix :: Matrix -> Matrix 
cofactorMatrix m = [ [ (cofactor m i j) | j <- [1..n] ] | i <- [1..n] ] 
    where 
    n = length m 

inverse :: Matrix -> Matrix 
inverse m = transpose [ [ quot x (determinant m) | 
    x <- row ] | row <- (cofactorMatrix m) ] 

--Matrix Division 

MatrixDivision :: Matrix -> Matrix -> Matrix 
MatrixDivision m n = matrixProduct m inverse(n) 

我寫了完整的代碼以提供更多信息。

+0

可能的重複[爲什麼Haskell強制數據構造函數的第一個字母是大寫?](http://stackoverflow.com/questions/6237775/why-does-haskell-force-data-constructors-first-letter-大寫) – Lambdageek

+0

..並且您需要在'elemMatrix'的'where'子句中的'entry xy'之後將守衛'|'縮進一個。 – AndrewC

回答

3

在表達式級別,以大寫字母開頭的名稱保留給數據構造函數。

MatrixDivision應該叫matrixDivision

見哈斯克爾報告Section 2.4

1

MatrixDivision不是有效的函數名稱,因爲它是大寫。改爲撥打matrixDivision

+0

行動中,剛剛看到Lambdageek答案! – lbolla

+0

謝謝你的幫助,但仍然存在問題 –

+0

你能更具體嗎?你看到什麼問題? – lbolla