Determinant and permanent: Difference between revisions

m (Ol updated with "permanent")
Line 2,148:
permanent: 6778800
</pre>
=={{header|Scala}}==
<lang scala>
def permutationsSgn[T]: List[T] => List[(Int,List[T])] = {
case Nil => List((1,Nil))
case xs => {
for {
(x, i) <- xs.zipWithIndex
(sgn,ys) <- permutationsSgn(xs.take(i) ++ xs.drop(1 + i))
} yield {
val sgni = sgn * (2 * (i%2) - 1)
(sgni, (x :: ys))
}
}
}
 
def det(m:List[List[Int]]) = {
val summands =
for {
(sgn,sigma) <- permutationsSgn((0 to m.length - 1).toList).toList
}
yield {
val factors =
for (i <- 0 to (m.length - 1))
yield m(i)(sigma(i))
factors.toList.foldLeft(sgn)({case (x,y) => x * y})
}
summands.toList.foldLeft(0)({case (x,y) => x + y})
</lang>
 
=={{header|Sidef}}==
The `determinant` method is provided by the Array class.
Anonymous user