Skip to content

Commit 049c51c

Browse files
fiekerthofma
authored andcommitted
feat: better sub for [ZZ|QQ]Matrix
1 parent 2a1d29b commit 049c51c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/matrix.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ function sub(x::_MatTypes, r::AbstractUnitRange{Int}, c::AbstractUnitRange{Int})
2020
return deepcopy(view(x, r, c))
2121
end
2222

23+
# flint does not support windows with non-continuous row or column indices
24+
# we overload some specific types to not hit the AA fallback
25+
for T in [ZZMatrix, QQMatrix]
26+
@eval begin
27+
# have to "duplicate" this, otherwise we get ambiguity errors
28+
function sub(x::$T, r::AbstractUnitRange{Int}, c::AbstractUnitRange{Int})
29+
return deepcopy(view(x, r, c))
30+
end
31+
32+
function sub(M::$T, r::AbstractVector{Int}, c::AbstractVector{Int})
33+
N = zero_matrix(base_ring(M), length(r), length(c))
34+
GC.@preserve M N begin
35+
for (ii, i) in enumerate(r)
36+
for (jj, j) in enumerate(c)
37+
set!(mat_entry_ptr(N, ii, jj), mat_entry_ptr(M, i, j))
38+
end
39+
end
40+
end
41+
return N
42+
end
43+
end
44+
end
45+
46+
# make x[r, c] sugar for sub(x, r, c)
2347
getindex(x::_MatTypes, r::AbstractUnitRange{Int}, c::AbstractUnitRange{Int}) = sub(x, r, c)
2448

2549
################################################################################

test/flint/fmpz_mat-test.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ end
259259
C[1, 1] = 20
260260
@test B == matrix_space(ZZ, 2, 2)([10 2; 4 5])
261261
@test A == S([1 2 3; 4 5 6; 7 8 9])
262+
263+
m = ZZ[1 2 4; 5 6 7]
264+
n = m[[1], [1, 3]]
265+
@test n == ZZ[1 4]
266+
n = m[1:2, 1:2]
267+
@test n == ZZ[1 2; 5 6]
262268
end
263269

264270
@testset "ZZMatrix.unary_ops" begin

0 commit comments

Comments
 (0)