Skip to content
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.det improvements #282

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions src/det.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
function LinearAlgebra.det(M::Matrix{<:AbstractPolynomialLike})
m = size(M)[1]
if m > 2
return sum(
(-1)^(i - 1) * M[i, 1] * LinearAlgebra.det(M[1:end.!=i, 2:end]) for
i in 1:m
)
elseif m == 2
return M[1, 1] * M[2, 2] - M[2, 1] * M[1, 2]
# Scalar determinant, used for recursive computation of the determinant
LinearAlgebra.det(p::AbstractPolynomialLike{<:Number}) = p

# Matrix determinant by cofactor expansion, adapted from
# `LinearAlgebraX.cofactor_det`.
function det_impl(A::AbstractMatrix{T}) where {T}
get_elem = let A = A
(i, j) -> LinearAlgebra.det(A[i, j])
end
r = first(size(A))
if 1 < r
total = LinearAlgebra.det(zero(T))
for i in Base.OneTo(r)
a = get_elem(i, 1)
if !iszero(a)
ii = Base.OneTo(r) .!= i
jj = 2:r
B = A[ii, jj]
x = det_impl(B)
x = MA.operate!!(*, x, a)
if iseven(i)
x = MA.operate!!(-, x)
end
total = MA.operate!!(+, total, x)
end
end
total
elseif isone(r)
MA.copy_if_mutable(get_elem(1, 1))
else
error("unexpected")
end
end

collect_if_not_already_matrix(m::Matrix) = m
collect_if_not_already_matrix(m::AbstractMatrix) = collect(m)

function det_impl_outer(m::AbstractMatrix{T}) where {T}
if 0 < LinearAlgebra.checksquare(m)
det_impl(collect_if_not_already_matrix(m))
else
return M[1, 1]
LinearAlgebra.det(one(T))
end
end

# Determinants of narrow integer type: `LinearAlgebra` seems to
# promote these to `Float64` to prevent them from overflowing. We
# instead promote to `BigInt` to keep things exact. In the case of
# `Bool` we also need to promote for type stability.

const NarrowIntegerTypes =
Union{Bool,UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,UInt128,Int128}

const NarrowIntegerPolynomialLike =
AbstractPolynomialLike{T} where {T<:NarrowIntegerTypes}

promote_if_narrow(m::AbstractMatrix{<:AbstractPolynomialLike}) = m

function promote_if_narrow(m::AbstractMatrix{<:NarrowIntegerPolynomialLike})
return map((p -> polynomial(p, BigInt)), m)
end

# For type stability, we want to promote termlikes to polynomiallikes
# before attempting to calculate the determinant.
promote_if_termlike(m::AbstractMatrix{<:AbstractPolynomialLike}) = m
promote_if_termlike(m::AbstractMatrix{<:AbstractTermLike}) = map(polynomial, m)

promote_if_necessary(m) = promote_if_termlike(promote_if_narrow(m))

function LinearAlgebra.det(m::AbstractMatrix{<:AbstractPolynomialLike})
return det_impl_outer(promote_if_necessary(m))
end
13 changes: 10 additions & 3 deletions test/det.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
@testset "Det" begin
Mod.@polyvar x y

@test det([x 1; y 1]) == x - y
@test det([x 1; x 1]) == 0
@test det([x 1 1 1; x y 1 2; 0 0 0 1; x 0 y 0]) == -x * y^2 + 2 * x * y - x
@testset "zero-one $T" for T in (Bool, Int, Float64, BigInt, BigFloat)
z = zero(T)
o = one(T)
@test (@inferred det([x o; y o])) == x - y
@test (@inferred det([x o; x o])) == 0
@test (@inferred det([x+y y; z y])) == (x + y)*y
end

@test (@inferred det([x 1 1 1; x y 1 2; 0 0 0 1; x 0 y 0])) ==
-x * y^2 + 2 * x * y - x
end