Determinant and permanent: Difference between revisions

Content added Content deleted
Line 852: Line 852:


===Calculation of the determinant===
===Calculation of the determinant===
The computation of the determinant and that of the permanent should be separate tasks. Here is code for computing the determinant (inefficiently, via [[wp:Cramer's rule|Cramer's rule]]):
Here is code for computing the determinant and permanent very inefficiently, via [[wp:Cramer's rule|Cramer's rule]] (for the determinant, as well as its analog for the permanent):


<lang Haskell>
<lang Haskell>
Line 878: Line 878:
delCol j m = (delRow j) <$> m
delCol j m = (delRow j) <$> m


-- Determinant:
adj :: Num a => [[a]] -> [[a]]
adj :: Num a => [[a]] -> [[a]]
adj [] = []
adj [] = []
Line 890: Line 891:
det [] = 1
det [] = 1
det m = (mul m (adj m)) !! 0 !! 0
det m = (mul m (adj m)) !! 0 !! 0

-- Permanent:
padj :: Num a => [[a]] -> [[a]]
padj [] = []
padj m =
[
[perm (delRow i $ delCol j m)
| i <- [0.. -1+length m]
]
| j <- [0.. -1+length m]
]
perm :: Num a => [[a]] -> a
perm [] = 1
perm m = (mul m (padj m)) !! 0 !! 0

</lang>
</lang>