Skip to content

Commit 49f91e2

Browse files
lxvmstevengj
authored andcommitted
Fix overflow and type issues with generic det of a triangular matrix (#1418)
Fixes #1415 --------- Co-authored-by: Steven G. Johnson <[email protected]>
1 parent d2b7b41 commit 49f91e2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/generic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ julia> det(BigInt[1 0; 2 2]) # exact integer determinant
18211821
function det(A::AbstractMatrix{T}) where {T}
18221822
if istriu(A) || istril(A)
18231823
S = promote_type(T, typeof((one(T)*zero(T) + zero(T))/one(T)))
1824-
return convert(S, det(UpperTriangular(A)))
1824+
return prod(Base.Fix1(convert, S), @view A[diagind(A)]; init=one(S))
18251825
end
18261826
return det(lu(A; check = false))
18271827
end

test/generic.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ n = 5 # should be odd
103103
@testset "det with nonstandard Number type" begin
104104
elty <: Real && @test det(Dual.(triu(A), zero(A))) isa Dual
105105
end
106+
if elty <: Int
107+
@testset "det no overflow - triangular" begin
108+
A = diagm([typemax(elty), typemax(elty)])
109+
@test det(A) == det(float(A))
110+
end
111+
end
112+
@testset "det with units - triangular" begin
113+
for dim in 0:4
114+
A = diagm(Furlong.(ones(elty, dim)))
115+
@test det(A) == Furlong{dim}(one(elty))
116+
end
117+
end
106118
end
107119

108120
@testset "diag" begin

0 commit comments

Comments
 (0)