Skip to content

Commit

Permalink
Fix MatrixView inheritance (#1687)
Browse files Browse the repository at this point in the history
* Fix MatrixView inheritance

* Consolidate `_MatTypes`
  • Loading branch information
lgoettgens committed Feb 26, 2024
1 parent 4a1abac commit 1c976c9
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/matrix.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Types of all Flint-backed matrices
const _FieldMatTypes = Union{QQMatrix, fpMatrix, FpMatrix, FqMatrix, fqPolyRepMatrix, FqPolyRepMatrix}
const _MatTypes = Union{_FieldMatTypes, ZZMatrix, zzModMatrix, ZZModMatrix}


################################################################################
#
# Support for view(A, :, i) and view(A, i, :)
#
################################################################################

# MatrixView{MatrixType, ElemType}
struct MatrixView{S, T} <: AbstractVector{T}
A::S
end
Expand All @@ -18,16 +24,14 @@ Base.setindex!(V::MatrixView, z, i::Int) = setindex!(V, ZZ(z), i)

Base.size(V::MatrixView) = (length(V.A), )

const _MatTypes = Union{ZZMatrix, QQMatrix, zzModMatrix, ZZModMatrix, fpMatrix, FpMatrix, FqMatrix, fqPolyRepMatrix, FqPolyRepMatrix}

function Base.view(x::_MatTypes, r::Int, c::UnitRange{Int})
A = view(x, r:r, c)
return MatrixView{typeof(x), typeof(base_ring(x))}(A)
return MatrixView{typeof(x), elem_type(base_ring(x))}(A)
end

function Base.view(x::_MatTypes, r::UnitRange{Int}, c::Int)
A = view(x, r, c:c)
return MatrixView{typeof(x), typeof(base_ring(x))}(A)
return MatrixView{typeof(x), elem_type(base_ring(x))}(A)
end

################################################################################
Expand All @@ -36,8 +40,6 @@ end
#
################################################################################

const _FieldMatTypes = Union{QQMatrix, fpMatrix, FpMatrix, FqMatrix, fqPolyRepMatrix, FqPolyRepMatrix}

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

Expand Down
7 changes: 6 additions & 1 deletion test/flint/fmpq_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,20 @@ end

A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(QQ)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(QQ)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])

end

@testset "QQMatrix.sub" begin
Expand Down
6 changes: 5 additions & 1 deletion test/flint/fmpz_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ end

A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(FlintZZ)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v[3] == 9
@test v isa AbstractVector{elem_type(FlintZZ)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fmpz_mod_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -728,12 +728,18 @@ end

A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fq_default_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,18 @@ end
S = matrix_space(F17, 3, 3)
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(F17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(F17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fq_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,18 @@ end
S = matrix_space(F17, 3, 3)
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(F17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(F17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fq_nmod_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,18 @@ end
S = matrix_space(F17, 3, 3)
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(F17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(F17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/gfp_fmpz_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,18 @@ end

A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/gfp_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,18 @@ end

A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down
6 changes: 6 additions & 0 deletions test/flint/nmod_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,18 @@ end

A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[2, :]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[1] == 4
@test collect(v) == [4, 5, 6]
v[2] = 7
@test A == S([1 2 3; 4 7 6; 7 8 9])
A = S([1 2 3; 4 5 6; 7 8 9])
v = @view A[:, 3]
@test v isa AbstractVector{elem_type(Z17)}
@test length(v) == 3
@test v[3] == 9
@test collect(v) == [3, 6, 9]
v[1] = 1
@test A == S([1 2 1; 4 5 6; 7 8 9])
end
Expand Down

0 comments on commit 1c976c9

Please sign in to comment.