-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LinearAlgebra: Add bareiss det for BigInt Matrices (#40128) :: Take 2 #40868
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1558,6 +1558,14 @@ function det(A::AbstractMatrix{T}) where T | |
end | ||
det(x::Number) = x | ||
|
||
# Resolve Issue #40128 | ||
function det(A::AbstractMatrix{BigInt}) | ||
m, n = size(A) | ||
if m == n || throw(DimensionMismatch("matrix is not square: dimensions are $(size(A))")) | ||
det_bareiss(A) | ||
end | ||
end | ||
|
||
""" | ||
logabsdet(M) | ||
|
||
|
@@ -1620,6 +1628,54 @@ logdet(A) = log(det(A)) | |
|
||
const NumberArray{T<:Number} = AbstractArray{T} | ||
|
||
exactdiv(a, b) = a/b | ||
exactdiv(a::Integer, b::Integer) = div(a, b) | ||
|
||
""" | ||
LinearAlgebra.det_bareiss!(M) | ||
|
||
Calculates the determinant of a matrix using the | ||
[Bareiss Algorithm](https://en.wikipedia.org/wiki/Bareiss_algorithm) using | ||
inplace operations. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> M = [1 0; 2 2] | ||
2×2 Matrix{Int64}: | ||
1 0 | ||
2 2 | ||
|
||
julia> LinearAlgebra.det_bareiss!(M) | ||
2 | ||
``` | ||
""" | ||
function det_bareiss!(M) | ||
dkarrasch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n, sign, prev = size(M,1), Int8(1), one(eltype(M)) | ||
for i in 1:n-1 | ||
if iszero(M[i,i]) # swap with another col to make nonzero | ||
swapto = findfirst(!iszero, @view M[i,i+1:end]) | ||
isnothing(swapto) && return zero(prev) | ||
sign = -sign | ||
Base.swapcols!(M, i, swapto) | ||
end | ||
for k in i+1:n, j in i+1:n | ||
M[j,k] = exactdiv(M[j,k]*M[i,i] - M[j,i]*M[i,k], prev) | ||
end | ||
prev = M[i,i] | ||
end | ||
return sign * M[end,end] | ||
end | ||
""" | ||
LinearAlgebra.det_bareiss(M) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't put the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change it, but This is the one example I could find though, should I change this too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would leave the |
||
|
||
Calculates the determinant of a matrix using the | ||
[Bareiss Algorithm](https://en.wikipedia.org/wiki/Bareiss_algorithm). | ||
Also refer to [`det_bareiss!`](@ref). | ||
""" | ||
det_bareiss(M) = det_bareiss!(copy(M)) | ||
|
||
|
||
|
||
""" | ||
promote_leaf_eltypes(itr) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -355,6 +355,10 @@ end | |
@test [[1,2, [3,4]], 5.0, [6im, [7.0, 8.0]]] ≈ [[1,2, [3,4]], 5.0, [6im, [7.0, 8.0]]] | ||
end | ||
|
||
@testset "Issue 40128" begin | ||
@test LinearAlgebra.det_bareiss(BigInt[1 2; 3 4]) == -2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the indenting here. (4 spaces) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also check that the result is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifically, you should probably test something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually the current LU algorithm gives the exact answer here: julia> det(BigInt[1 big(2)^65+1; 3 4]) - (4 - 3*(big(2)^65+1))
0.0 (Remember that it uses |
||
end | ||
|
||
# Minimal modulo number type - but not subtyping Number | ||
struct ModInt{n} | ||
k | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this test should be in
det_bareiss!
, and then just have:Note also that you can simply call
checksquare(A)
indet_bareiss!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reasoning behind this design choice? I'm trying to learn as I go along, so It would be helpful if you could shed some light on this.
I'll add a commit correcting this, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't matter whether someone calls
det_bareiss!
directly, or throughdet_bareiss
, or throughdet
— in all cases, it should check whether the matrix is square, because otherwise that function doesn't make sense.