The Challenge:To create a program that will calculate the determinant of any nxn matrix with entries in the Real space.
The program should take one argument - the matrix - which should be formulated as a list, for example:
2x2 Matrix:'( (1 2)
(3 4) )
3x3 Matrix:'( (1 2 3)
(3 4 5)
(5 6 7) )
As a little background on Matrices and Determinants....A Matrix (not the film) is purely an array of entries which can represent vectors (usually), coefficients of systems of equations, and other such quantities. They are used in a huge range of applications... from solving systems of simultaneous/differential equations... to applying transformations to vectors.
The determinant is also a useful calculation... geometrically, it can be thought of as the area of a parallelogram, the sides of which are the vectors of the matrix. And this can be extended to 3-dimensions with a 3x3 matrix, and thought of as the parallelepiped formed by the 3 vectors contained in the matrix.
The determinant is also a necessary element when calculating the Inverse of a matrix - a necessary component in matrix arithmetic.
Methods for Calculation...I believe the most common method for calculating the determinant is by
Laplace's Formula:
For a matrix of size
n. Where, when calculating the determinant along row
i,
Mij is the determinant of the matrix formed by removing row
i and column
j from the orignal matrix.
This is the route that I have followed.
But there are many other methods that can be used... many of which are outlined
here.
My Offering:
;; Matrix Determinant - Lee Mac
;; Args: m - nxn matrix
(defun detm ( m / i j )
(setq i -1 j 0)
(cond
( (null (cdr m)) (caar m))
( (null (cddr m)) (- (* (caar m) (cadadr m)) (* (cadar m) (caadr m))))
( (apply '+
(mapcar
'(lambda ( c ) (setq j (1+ j))
(* c (setq i (- i))
(detm
(mapcar
'(lambda ( x / k )
(setq k 0)
(vl-remove-if '(lambda ( y ) (= j (setq k (1+ k)))) x)
)
(cdr m)
)
)
)
)
(car m)
)
)
)
)
)
Enjoy!
Lee