Determinant and permanent: Difference between revisions

Content added Content deleted
(added Ol)
m (Ol updated with "permanent")
Line 1,418: Line 1,418:
j))
j))


; superfunction for determinant and permanent
; det calculator
(define (det matrix)
(define (super matrix math)
(let loop ((n (length matrix)) (matrix matrix))
(let loop ((n (length matrix)) (matrix matrix))
(if (eq? n 1)
(if (eq? n 1)
(caar matrix)
(caar matrix)
(fold (lambda (x a j)
(fold (lambda (x a j)
(+ x (* a (lref '(-1 1) (mod j 2)) (det (rest matrix j 1)))))
(+ x (* a (lref math (mod j 2)) (super (rest matrix j 1) math))))
0
0
(car matrix)
(car matrix)
(iota n 1)))))
(iota n 1)))))


; det/per calculators
(define (det matrix) (super matrix '(-1 1)))
(define (per matrix) (super matrix '( 1 1)))


; ---=( testing )=---------------------
; ---=( testing )=---------------------
(print (det '(
(print (det '(
(1 2)
(1 2)
(3 4)))
(3 4))))
; ==> -2
; ==> -2

(print (per '(
(1 2)
(3 4))))
; ==> 10



(print (det '(
(print (det '(
Line 1,439: Line 1,450:
(-1 -1 -1 2)
(-1 -1 -1 2)
( 1 3 1 1)
( 1 3 1 1)
(-2 -2 0 -1)))
(-2 -2 0 -1))))
; ==> 26
; ==> 26

(print (per '(
( 1 2 3 1)
(-1 -1 -1 2)
( 1 3 1 1)
(-2 -2 0 -1))))
; ==> -10



(print (det '(
(print (det '(
Line 1,447: Line 1,466:
(10 11 12 13 14)
(10 11 12 13 14)
(15 16 17 18 19)
(15 16 17 18 19)
(20 21 22 23 24)))
(20 21 22 23 24))))
; ==> 0
; ==> 0

(print (per '(
( 0 1 2 3 4)
( 5 6 7 8 9)
(10 11 12 13 14)
(15 16 17 18 19)
(20 21 22 23 24))))
; ==> 6778800
</lang>
</lang>