StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

det(M) fails for rationals

Open mchristianl opened this issue 7 months ago • 1 comments

It seems that det(M) calls lu(M) for bigger matrices n ≥ 5 which is not able to handle rationals well when the determinant is zero.

Consider

using StaticArrays, LinearAlgebra

for n in 2:5
  println("n = $n")
  global M = MMatrix{n,n,Rational{Int64}}(LinearAlgebra.I)
  M[n-1,n-1] = 0
  det(M) # fails for n = 5
end

this works until n ≥ 5 and fails at det(M) with

ERROR: DivideError: integer division error
Stacktrace:
  [1] div
    @ ./int.jl:297 [inlined]
  [2] div
    @ ./int.jl:229 [inlined]
  [3] divgcd(x::Int64, y::Int64)
    @ Base ./rational.jl:54
  [4] *
    @ ./rational.jl:394 [inlined]
  [5] #282
    @ ~/.julia/packages/StaticArrays/lZeGV/src/linalg.jl:22 [inlined]
  [6] macro expansion
    @ ~/.julia/packages/StaticArrays/lZeGV/src/mapreduce.jl:77 [inlined]
  [7] _map
    @ ~/.julia/packages/StaticArrays/lZeGV/src/mapreduce.jl:42 [inlined]
  [8] map
    @ ~/.julia/packages/StaticArrays/lZeGV/src/mapreduce.jl:33 [inlined]
  [9] *
    @ ~/.julia/packages/StaticArrays/lZeGV/src/linalg.jl:22 [inlined]
 [10] __lu(A::MMatrix{2, 2, Rational{Int64}, 4}, ::Val{true})
    @ StaticArrays ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:182
 [11] __lu(A::MMatrix{3, 3, Rational{Int64}, 9}, ::Val{true})
    @ StaticArrays ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:189
 [12] __lu(A::MMatrix{4, 4, Rational{Int64}, 16}, ::Val{true})
    @ StaticArrays ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:189
 [13] __lu(A::MMatrix{5, 5, Rational{Int64}, 25}, ::Val{true})
    @ StaticArrays ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:189
 [14] macro expansion
    @ ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:81 [inlined]
 [15] _lu
    @ ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:73 [inlined]
 [16] #lu#503
    @ ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:50 [inlined]
 [17] lu
    @ ~/.julia/packages/StaticArrays/lZeGV/src/lu.jl:49 [inlined]
 [18] macro expansion
    @ ~/.julia/packages/StaticArrays/lZeGV/src/det.jl:63 [inlined]
 [19] _det
    @ ~/.julia/packages/StaticArrays/lZeGV/src/det.jl:58 [inlined]
 [20] det(A::MMatrix{5, 5, Rational{Int64}, 25})
    @ StaticArrays ~/.julia/packages/StaticArrays/lZeGV/src/det.jl:5
 [21] top-level scope
    @ REPL[14]:1

the matrix in question is

5×5 MMatrix{5, 5, Rational{Int64}, 25} with indices SOneTo(5)×SOneTo(5):
 1  0  0  0  0
 0  1  0  0  0
 0  0  1  0  0
 0  0  0  0  0
 0  0  0  0  1

mchristianl avatar Jun 21 '25 13:06 mchristianl