-1

I had the idea to write a matrix division in Haskell as Muliply A* Inverse B.

I wrote some code to do that, but the compiler had the idea to stop me. It shows the following error:

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

What do I wrong? This is the code:

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)

I wrote the full code to give more information.

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
  • possible duplicate of [Why does Haskell force data constructor's first letter to be upper case?](http://stackoverflow.com/questions/6237775/why-does-haskell-force-data-constructors-first-letter-to-be-upper-case) – Lambdageek Jun 25 '12 at 16:35
  • ..and you need to indent the guards `|` by one after `entry x y` in the `where` clause of `elemMatrix`. – AndrewC Nov 17 '12 at 23:46

2 Answers2

3

At the expression level, names beginning with an Uppercase letter are reserved for data constructors.

Your MatrixDivision should be called matrixDivision

See Section 2.4 of the Haskell Report.

Lambdageek
  • 12,465
  • 1
  • 25
  • 33
1

MatrixDivision is not a valid function name, because it's uppercase. Call it matrixDivision, instead.

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
lbolla
  • 5,387
  • 1
  • 22
  • 35