Dyalog APL, 53 58 59 characters
{1=≢⍵:⊃⍵⋄-/⍵[1;]×∇¨{w[f~1;f~⍵]}¨f←⍳⊃⍴w←⍵}U⍪↑{⎕}¨1↓⍳⍴U←⎕
I'm pretty late to the party here.
Please, alert me if there are any issues with this code!
Takes negatives as input via ¯ (the APL negative sign). I think values by default go to STDOUT in APL, but I may be wrong.
Uses Laplace expansion to find the determinant of the matrix.
This program is separated into three main parts: an input section, and the case of a 1x1 matrix, and the actual recursive function.
Input program:
U⍪↑{⎕}¨1↓⍳⍴U←⎕
⎕ - Takes evaluated input, space separated numbers are vectors in APL.
U← - Stores the input into variable `U`.
⍳⍴ - The array of integers from 1 to the length of `U` (inclusive).
1↓ - Drops the first element of the result.
{⎕}¨ - Receives a new input for each element in the vector. (Result is a vector of vectors)
↑ - Transforms the vector vector into a 2D-array.
U⍪ - The original line was stored then used for its length, so we stick it on top of the new array.
1x1 case:
{1=≢⍵:⊃⍵⋄...}
⍵ - The input
⊃ - The first element of the input
: - Return that if and only if ...
≢⍵ - The largest dimension of the input matrix
1= - Has equality with 1.
⋄ - Statement separator
{ } - Function enclosure brackets.
Actual determinant program:
{...-/⍵[1;]×∇¨{w[f~1;f~⍵]}¨f←⍳⊃⍴w←⍵}
w←⍵ - The input stored in `w`
⍴ - The dimensions of `w`
⊃ - The first dimension
⍳ - 1 to the result of last operation
f← - Store the result in `f`
{ }¨ - Do what is inside the brackets to each member of `f`
w[f~1;f~⍵] - Return the minor matrix.
∇¨ - Find determinant of each element (recursive)
⍵[1;]× - Multiply elementwise by the first row of the input matrix
-/ - Reduction using subtraction, since APL evaluates right to left, it comes out to be + - + - ...
14I'm 4'6". Do I win? – Gareth – 2012-09-21T11:52:08.753
4
Looks like
– mellamokb – 2012-09-21T13:03:16.367Chandra Bahadur Dangi
wins this competition at1 ft 11 in
. First competition ever won by somebody who's probably never heard of the site :)2Is the matrix over any particular ring? – Peter Taylor – 2012-09-24T15:14:12.233
Just Q I assume. – scleaver – 2012-09-24T15:49:06.783
NB this question is superseded by https://codegolf.stackexchange.com/q/147668/194
– Peter Taylor – 2018-11-27T12:52:26.247