Skip to content

Commit

Permalink
Add Solve.kernel for some more types (using HeckeMiscMatrix)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschmitt committed Feb 8, 2024
1 parent d9fc380 commit f1fe6ec
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/flint/fmpz_mod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,38 @@ function matrix_space(R::ZZModRing, r::Int, c::Int; cached::Bool = true)
ZZModMatrixSpace(R, r, c)
end

################################################################################
#
# Kernel
#
################################################################################

function AbstractAlgebra.Solve.kernel(M::ZZModMatrix; side::Symbol = :right)
AbstractAlgebra.Solve.check_option(side, [:right, :left], "side")

if side === :left
K = AbstractAlgebra.Solve.kernel(transpose(M), side = :right)
return transpose(K)

Check warning on line 930 in src/flint/fmpz_mod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_mat.jl#L929-L930

Added lines #L929 - L930 were not covered by tests
end

R = base_ring(M)
N = hcat(transpose(M), identity_matrix(R, ncols(M)))
if nrows(N) < ncols(N)
N = vcat(N, zero_matrix(R, ncols(N) - nrows(N), ncols(N)))
end
howell_form!(N)
H = N
nr = 1
while nr <= nrows(H) && !is_zero_row(H, nr)
nr += 1
end
nr -= 1
h = view(H, 1:nr, 1:nrows(M))
for i = 1:nrows(h)
if is_zero_row(h, i)
k = view(H, i:nrows(h), nrows(M) + 1:ncols(H))
return transpose(k)
end
end
return zero_matrix(R, ncols(M), 0)
end
19 changes: 19 additions & 0 deletions src/flint/gfp_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,22 @@ function matrix_space(R::fpField, r::Int, c::Int; cached::Bool = true)
# TODO/FIXME: `cached` is ignored and only exists for backwards compatibility
fpMatrixSpace(R, r, c)
end

################################################################################
#
# Kernel
#
################################################################################

function AbstractAlgebra.Solve.kernel(A::fpMatrix; side::Symbol = :right)
AbstractAlgebra.Solve.check_option(side, [:right, :left], "side")

if side === :left
K = AbstractAlgebra.Solve.kernel(transpose(A), side = :right)
return transpose(K)

Check warning on line 523 in src/flint/gfp_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/gfp_mat.jl#L522-L523

Added lines #L522 - L523 were not covered by tests
end

K = zero_matrix(base_ring(A), ncols(A), max(nrows(A), ncols(A)))
n = ccall((:nmod_mat_nullspace, libflint), Int, (Ref{fpMatrix}, Ref{fpMatrix}), K, A)
return view(K, 1:nrows(K), 1:n)
end
40 changes: 40 additions & 0 deletions src/flint/nmod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,43 @@ function matrix_space(R::zzModRing, r::Int, c::Int; cached::Bool = true)
zzModMatrixSpace(R, r, c)
end

################################################################################
#
# Kernel
#
################################################################################

function AbstractAlgebra.Solve.kernel(M::zzModMatrix; side::Symbol = :right)
AbstractAlgebra.Solve.check_option(side, [:right, :left], "side")

Check warning on line 929 in src/flint/nmod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_mat.jl#L928-L929

Added lines #L928 - L929 were not covered by tests

if side === :left
K = AbstractAlgebra.Solve.kernel(transpose(M), side = :right)
return transpose(K)

Check warning on line 933 in src/flint/nmod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_mat.jl#L931-L933

Added lines #L931 - L933 were not covered by tests
end

R = base_ring(M)
if is_prime(modulus(R))
k = zero_matrix(R, ncols(M), ncols(M))
n = ccall((:nmod_mat_nullspace, libflint), Int, (Ref{zzModMatrix}, Ref{zzModMatrix}), k, M)
return view(k, 1:nrows(k), 1:n)

Check warning on line 940 in src/flint/nmod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_mat.jl#L936-L940

Added lines #L936 - L940 were not covered by tests
end

H = hcat(transpose(M), identity_matrix(R, ncols(M)))
if nrows(H) < ncols(H)
H = vcat(H, zero_matrix(R, ncols(H) - nrows(H), ncols(H)))

Check warning on line 945 in src/flint/nmod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_mat.jl#L943-L945

Added lines #L943 - L945 were not covered by tests
end
howell_form!(H)
nr = 1
while nr <= nrows(H) && !is_zero_row(H, nr)
nr += 1
end
nr -= 1
h = view(H, 1:nr, 1:nrows(M))
for i = 1:nrows(h)
if is_zero_row(h, i)
k = view(H, i:nrows(h), nrows(M) + 1:ncols(H))
return transpose(k)

Check warning on line 957 in src/flint/nmod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_mat.jl#L947-L957

Added lines #L947 - L957 were not covered by tests
end
end
return zero_matrix(R, ncols(M), 0)

Check warning on line 960 in src/flint/nmod_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_mat.jl#L959-L960

Added lines #L959 - L960 were not covered by tests
end

0 comments on commit f1fe6ec

Please sign in to comment.