# Determinant of an Integer Matrix

Given a square integer matrix as input, output the determinant of the matrix.

## Rules

• You may assume that all elements in the matrix, the determinant of the matrix, and the total number of elements in the matrix are within the representable range of integers for your language.
• Outputting a decimal/float value with a fractional part of 0 is allowed (e.g. 42.0 instead of 42).
• Builtins are allowed, but you are encouraged to include a solution that does not use builtins.

## Test Cases

[[42]] -> 42
[[2, 3], [1, 4]] -> 5
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 0
[[13, 17, 24], [19, 1, 3], [-5, 4, 0]] -> 1533
[[372, -152, 244], [-97, -191, 185], [-53, -397, -126]] -> 46548380
[[100, -200, 58, 4], [1, -90, -55, -165], [-67, -83, 239, 182], [238, -283, 384, 392]] -> 571026450
[[432, 45, 330, 284, 276], [-492, 497, 133, -289, -28], [-443, -400, 56, 150, -316], [-344, 316, 92, 205, 104], [277, 307, -464, 244, -422]] -> -51446016699154
[[416, 66, 340, 250, -436, -146], [-464, 68, 104, 471, -335, -442], [159, -407, 310, -489, -248, 370], [62, 277, 446, -325, 47, -193], [460, 460, -418, -28, 234, -374], [249, 375, 489, 172, -423, 125]] -> 39153009069988024
[[-246, -142, 378, -156, -373, 444], [186, 186, -23, 50, 349, -413], [216, 1, -418, 38, 47, -192], [109, 345, -356, -296, -47, -498], [-283, 91, 258, 66, -127, 79], [218, 465, -420, -326, -445, 19]] -> -925012040475554
[[-192, 141, -349, 447, -403, -21, 34], [260, -307, -333, -373, -324, 144, -190], [301, 277, 25, 8, -177, 180, 405], [-406, -9, -318, 337, -118, 44, -123], [-207, 33, -189, -229, -196, 58, -491], [-426, 48, -24, 72, -250, 160, 359], [-208, 120, -385, 251, 322, -349, -448]] -> -4248003140052269106
[[80, 159, 362, -30, -24, -493, 410, 249, -11, -109], [-110, -123, -461, -34, -266, 199, -437, 445, 498, 96], [175, -405, 432, -7, 157, 169, 336, -276, 337, -200], [-106, -379, -157, -199, 123, -172, 141, 329, 158, 309], [-316, -239, 327, -29, -482, 294, -86, -326, 490, -295], [64, -201, -155, 238, 131, 182, -487, -462, -312, 196], [-297, -75, -206, 471, -94, -46, -378, 334, 407, -97], [-140, -137, 297, -372, 228, 318, 251, -93, 117, 286], [-95, -300, -419, 41, -140, -205, 29, -481, -372, -49], [-140, -281, -88, -13, -128, -264, 165, 261, -469, -62]] -> 297434936630444226910432057

• – Mego Nov 10 '17 at 13:50
• is there a maximum size of matrix that needs to be supported or is it arbitrary? – Taylor Scott Nov 10 '17 at 14:59
• @TaylorScott First rule listed: You may assume that all elements in the matrix, the determinant of the matrix, and the total number of elements in the matrix are within the representable range of integers for your language. – Mego Nov 10 '17 at 15:00
• You know you have an interesting challenge when you have 4 Jelly answers consecutively out-golfing each other... – totallyhuman Nov 10 '17 at 18:21

# Jelly, 2 bytes

ÆḊ


Try it online!

Currently only built-in solution. :( Looks like others have managed to find out interesting non-built-in solutions.

# TI-Basic (83 series), 137 133 bytes

Ans→[A]
For(N,max(dim(Ans)),2,-1
Matr▶list(Ans,N,C
1+sum(not(cumSum(not(not(∟C→R
If R>N
Then
0[A]→[A]
Else
rowSwap([A],R,N
*row(cos(π(R≠N))Ans(N,N),Ans,1
*row(-Ans(N,N)⁻¹,Ans,N
For(I,1,N
*row+(Ans(I,N),Ans,N,I→[A]
End
End
End
Ans(1,1


(Indentation is purely decorative and doesn't appear on the calculator.)

Instead of the built-in det(, does row reduction by hand. The pivots are built backwards, starting at the (n,n)th entry and going back to the (1,1)th (or is that (1,1)st?).

Every time we need to divide by a number to set the pivot to 1, we multiply the first row by the same number (possibly negated, if we needed to swap rows), so that the determinant of the matrix stays the same. (If we didn't find a pivot, we zero out the entire matrix.) As a result, at the end, the (1,1)th (1,1)st top left entry holds the determinant.

# Excel VBA, 58 50 Bytes

Returns the determinant of the matrix entered into the the upper left corner of the default Sheet1 object.

?Evaluate("MDeterm("+Sheet1.UsedRange.Address+")")


-8 Bytes thanks to Greedo

• Save 7 with ?Evaluate("MDeterm("&Sheet1.UsedRange.Address &")") – Greedo Nov 12 '17 at 12:43

## Python 3, 153 149 Bytes

def d(n):
l=len(n);r=range(0,l)
if l==1:return n[0][0]
else:return sum([(-1)**i*n[0][i]*d([[n[a][b]for b in r if b!=i]for a in r[1:]])for i in r])


I'm new to code golf, so I wouldn't be surprised if this could be improved. It's calculated in a pretty basic way; by summing the product of each element of the first row with the determinant of its respective minor, with a sign change based on the element's position.

Edit: Realized I could shorten it by using a single character for the function name :/

• Suggest range(l) instead of range(0,l) and replace the if l==1: with if l>1: and swap the resulting expressions. – ceilingcat Nov 25 '17 at 9:06

## Clojure, 141 bytes

(fn D[A](apply +(map *(cycle[1 -1])(first A)(if(next A)(for[R[(range(count A))]i R](D(for[a(rest A)](for[j(remove #{i}R)](nth a j)))))[1]))))


Laplace's formula, longer than I'd like...

# MY, 3 (4?) bytes

ω∥↵


Try it online!

Precision is a little weird (the second test case is 5.000000000000001), this can be fixed at a cost of one byte: ω∥⌊↵

• Right argument, parallel, return... what? – totallyhuman Nov 10 '17 at 16:14
• Right argument, determinant, output with new line. – Zacharý Nov 10 '17 at 16:45

# APL(NARS), 64 chars, 128 bytes

{1≥k←↑⍴w←⍵:⍵⋄2=k:-/⌽w[2;]×⌽w[1;]⋄v←⍳k⋄-/w[1;]×∇¨{w[v∼1;v∼⍵]}¨⍳k}


This would be the formula used in the schools, it is easy, much easier than i tought... if w is a 2x2 matrix -/⌽w[2;]×⌽w[1;] rotate first row multiply with second row than reverse and make difference in pratice it does: (w[1;1]×w[2;2])-w[1;2]×w[2;1] else if it is a matrix nxn with n>2 multiply the first row with its adjunt Matrix (if i remember well the name), and make -/; test:

  D←{1≥k←↑⍴w←⍵:⍵⋄2=k:-/⌽w[2;]×⌽w[1;]⋄v←⍳k⋄-/w[1;]×∇¨{w[v∼1;v∼⍵]}¨⍳k}
D ⊃(1 9 3)(4 ¯5  6)(7 8 9)
162
D 3 3 ⍴ ⍳20
0
D ⊃(1 0)(0 1)
1
D ,89
89
D  1
1
D 'ac'
RANK ERROR