Skip to content

Commit fb7bee4

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 4fe709d commit fb7bee4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ using .Main.FillArrays
2525
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
2626
using .Main.SizedArrays
2727

28+
isdefined(Main, :Furlongs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Furlongs.jl"))
29+
using .Main.Furlongs
30+
2831
Random.seed!(123)
2932

3033
n = 5 # should be odd
@@ -103,6 +106,18 @@ n = 5 # should be odd
103106
@testset "det with nonstandard Number type" begin
104107
elty <: Real && @test det(Dual.(triu(A), zero(A))) isa Dual
105108
end
109+
if elty <: Int
110+
@testset "det no overflow - triangular" begin
111+
A = diagm([typemax(elty), typemax(elty)])
112+
@test det(A) == det(float(A))
113+
end
114+
end
115+
@testset "det with units - triangular" begin
116+
for dim in 0:4
117+
A = diagm(Furlong.(ones(elty, dim)))
118+
@test det(A) == Furlong{dim}(one(elty))
119+
end
120+
end
106121
end
107122

108123
@testset "diag" begin

0 commit comments

Comments
 (0)