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

Clean up *_mat_entry_set calls #1910

Merged
merged 7 commits into from
Oct 27, 2024
Merged
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
226 changes: 27 additions & 199 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5572,80 +5572,20 @@ mutable struct FqMatrix <: MatElem{FqFieldElem}
return z
end

function FqMatrix(r::Int, c::Int, arr::AbstractMatrix{FqFieldElem}, ctx::FqField)
function FqMatrix(arr::AbstractMatrix{<:Union{FqFieldElem,ZZRingElem,Integer}}, ctx::FqField)
(r, c) = size(arr)
z = FqMatrix(r, c, ctx)
for i = 1:r
for j = 1:c
ccall((:fq_default_mat_entry_set, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{FqFieldElem},
Ref{FqField}),
z, i - 1, j - 1, arr[i, j], ctx)
end
end
return z
end

function FqMatrix(r::Int, c::Int, arr::AbstractVector{FqFieldElem}, ctx::FqField)
z = FqMatrix(r, c, ctx)
for i = 1:r
for j = 1:c
ccall((:fq_default_mat_entry_set, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{FqFieldElem},
Ref{FqField}),
z, i - 1, j - 1, arr[(i - 1) * c + j], ctx)
end
end
return z
end

function FqMatrix(r::Int, c::Int, arr::AbstractMatrix{ZZRingElem}, ctx::FqField)
z = FqMatrix(r, c, ctx)
for i = 1:r
for j = 1:c
ccall((:fq_default_mat_entry_set_fmpz, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{ZZRingElem},
Ref{FqField}),
z, i - 1, j - 1, arr[i, j], ctx)
end
for i = 1:r, j = 1:c
@inbounds z[i, j] = arr[i, j]
end
return z
end

function FqMatrix(r::Int, c::Int, arr::AbstractVector{ZZRingElem}, ctx::FqField)
function FqMatrix(r::Int, c::Int, arr::AbstractVector{<:Union{FqFieldElem,ZZRingElem,Integer}}, ctx::FqField)
z = FqMatrix(r, c, ctx)
for i = 1:r
for j = 1:c
ccall((:fq_default_mat_entry_set_fmpz, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{ZZRingElem},
Ref{FqField}),
z, i - 1, j - 1, arr[(i - 1) * c + j], ctx)
end
end
return z
end

function FqMatrix(r::Int, c::Int, arr::AbstractMatrix{T}, ctx::FqField) where {T <: Integer}
z = FqMatrix(r, c, ctx)
for i = 1:r
for j = 1:c
ccall((:fq_default_mat_entry_set, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{FqFieldElem},
Ref{FqField}),
z, i - 1, j - 1, ctx(arr[i, j]), ctx)
end
end
return z
end

function FqMatrix(r::Int, c::Int, arr::AbstractVector{T}, ctx::FqField) where {T <: Integer}
z = FqMatrix(r, c, ctx)
for i = 1:r
for j = 1:c
ccall((:fq_default_mat_entry_set, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{FqFieldElem},
Ref{FqField}),
z, i - 1, j - 1, ctx(arr[(i - 1) * c + j]), ctx)
end
for i = 1:r, j = 1:c
el = arr[(i - 1) * c + j]
@inbounds z[i, j] = el
end
return z
end
Expand All @@ -5654,9 +5594,7 @@ mutable struct FqMatrix <: MatElem{FqFieldElem}
ctx = parent(d)
z = FqMatrix(r, c, ctx)
for i = 1:min(r, c)
ccall((:fq_default_mat_entry_set, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{FqFieldElem},
Ref{FqField}), z, i - 1, i - 1, d, ctx)
@inbounds z[i, i] = d
end
return z
end
Expand Down Expand Up @@ -5737,74 +5675,20 @@ mutable struct FqPolyRepMatrix <: MatElem{FqPolyRepFieldElem}
return z
end

function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractMatrix{FqPolyRepFieldElem}, ctx::FqPolyRepField)
z = FqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_mat_entry_set, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Int, Int, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
z, i - 1, j - 1, arr[i, j], ctx)
end
end
return z
end

function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{FqPolyRepFieldElem}, ctx::FqPolyRepField)
z = FqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_mat_entry_set, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Int, Int, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
z, i - 1, j - 1, arr[(i - 1) * c + j], ctx)
end
end
return z
end

function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractMatrix{ZZRingElem}, ctx::FqPolyRepField)
z = FqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
el = mat_entry_ptr(z, i, j)
ccall((:fq_set_fmpz, libflint), Nothing,
(Ptr{FqPolyRepFieldElem}, Ref{ZZRingElem}, Ref{FqPolyRepField}), el, arr[i, j], ctx)
end
end
return z
end

function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{ZZRingElem}, ctx::FqPolyRepField)
function FqPolyRepMatrix(arr::AbstractMatrix{<:Union{FqPolyRepFieldElem,ZZRingElem,Integer}}, ctx::FqPolyRepField)
(r, c) = size(arr)
z = FqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
el = mat_entry_ptr(z, i, j)
ccall((:fq_set_fmpz, libflint), Nothing,
(Ptr{FqPolyRepFieldElem}, Ref{ZZRingElem}, Ref{FqPolyRepField}), el, arr[(i - 1) * c + j], ctx)
end
end
return z
end

function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractMatrix{T}, ctx::FqPolyRepField) where {T <: Integer}
z = FqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_mat_entry_set, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Int, Int, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
z, i - 1, j - 1, ctx(arr[i, j]), ctx)
end
for i = 1:r, j = 1:c
@inbounds z[i, j] = arr[i, j]
end
return z
end

function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{T}, ctx::FqPolyRepField) where {T <: Integer}
function FqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{<:Union{FqPolyRepFieldElem,ZZRingElem,Integer}}, ctx::FqPolyRepField)
z = FqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_mat_entry_set, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Int, Int, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
z, i - 1, j - 1, ctx(arr[(i - 1) * c + j]), ctx)
end
for i = 1:r, j = 1:c
el = arr[(i - 1) * c + j]
@inbounds z[i, j] = el
end
return z
end
Expand All @@ -5813,8 +5697,7 @@ mutable struct FqPolyRepMatrix <: MatElem{FqPolyRepFieldElem}
ctx = parent(d)
z = FqPolyRepMatrix(r, c, ctx)
for i = 1:min(r, c)
ccall((:fq_mat_entry_set, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Int, Int, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), z, i - 1, i- 1, d, ctx)
@inbounds z[i, i] = d
end
return z
end
Expand Down Expand Up @@ -5870,74 +5753,20 @@ mutable struct fqPolyRepMatrix <: MatElem{fqPolyRepFieldElem}
return z
end

function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractMatrix{fqPolyRepFieldElem}, ctx::fqPolyRepField)
function fqPolyRepMatrix(arr::AbstractMatrix{<:Union{fqPolyRepFieldElem,ZZRingElem,Integer}}, ctx::fqPolyRepField)
(r, c) = size(arr)
z = fqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_nmod_mat_entry_set, libflint), Nothing,
(Ref{fqPolyRepMatrix}, Int, Int, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
z, i - 1, j - 1, arr[i, j], ctx)
end
end
return z
end

function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{fqPolyRepFieldElem}, ctx::fqPolyRepField)
z = fqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_nmod_mat_entry_set, libflint), Nothing,
(Ref{fqPolyRepMatrix}, Int, Int, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
z, i - 1, j - 1, arr[(i - 1) * c + j], ctx)
end
for i = 1:r, j = 1:c
@inbounds z[i, j] = arr[i, j]
end
return z
end

function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractMatrix{ZZRingElem}, ctx::fqPolyRepField)
function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{<:Union{fqPolyRepFieldElem,ZZRingElem,Integer}}, ctx::fqPolyRepField)
z = fqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
el = mat_entry_ptr(z, i, j)
ccall((:fq_nmod_set_fmpz, libflint), Nothing,
(Ptr{fqPolyRepFieldElem}, Ref{ZZRingElem}, Ref{fqPolyRepField}), el, arr[i, j], ctx)
end
end
return z
end

function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{ZZRingElem}, ctx::fqPolyRepField)
z = fqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
el = mat_entry_ptr(z, i, j)
ccall((:fq_nmod_set_fmpz, libflint), Nothing,
(Ptr{fqPolyRepFieldElem}, Ref{ZZRingElem}, Ref{fqPolyRepField}), el, arr[(i - 1) * c + j], ctx)
end
end
return z
end

function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractMatrix{T}, ctx::fqPolyRepField) where {T <: Integer}
z = fqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_nmod_mat_entry_set, libflint), Nothing,
(Ref{fqPolyRepMatrix}, Int, Int, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
z, i - 1, j - 1, ctx(arr[i, j]), ctx)
end
end
return z
end

function fqPolyRepMatrix(r::Int, c::Int, arr::AbstractVector{T}, ctx::fqPolyRepField) where {T <: Integer}
z = fqPolyRepMatrix(r, c, ctx)
GC.@preserve z for i = 1:r
for j = 1:c
ccall((:fq_nmod_mat_entry_set, libflint), Nothing,
(Ref{fqPolyRepMatrix}, Int, Int, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
z, i - 1, j - 1, ctx(arr[(i - 1) * c + j]), ctx)
end
for i = 1:r, j = 1:c
el = arr[(i - 1) * c + j]
@inbounds z[i, j] = el
end
return z
end
Expand All @@ -5946,8 +5775,7 @@ mutable struct fqPolyRepMatrix <: MatElem{fqPolyRepFieldElem}
ctx = parent(d)
z = fqPolyRepMatrix(r, c, ctx)
for i = 1:min(r, c)
ccall((:fq_nmod_mat_entry_set, libflint), Nothing,
(Ref{fqPolyRepMatrix}, Int, Int, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), z, i - 1, i- 1, d, ctx)
@inbounds z[i, i] = d
end
return z
end
Expand Down
36 changes: 12 additions & 24 deletions src/flint/fq_default_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ end
return z
end

@inline function setindex!(a::FqMatrix, u::FqFieldElem, i::Int, j::Int)
@inline function setindex!(a::FqMatrix, u::FqFieldElemOrPtr, i::Int, j::Int)
@boundscheck _checkbounds(a, i, j)
K = base_ring(a)
uu = K(u)
ccall((:fq_default_mat_entry_set, libflint), Nothing,
(Ref{FqMatrix}, Int, Int, Ref{FqFieldElem}, Ref{FqField}),
a, i - 1, j - 1, uu, base_ring(a))
nothing
uu = base_ring(a)(u)
@ccall libflint.fq_default_mat_entry_set(
a::Ref{FqMatrix}, (i-1)::Int, (j-1)::Int, uu::Ref{FqFieldElem}, base_ring(a)::Ref{FqField}
)::Nothing
end

@inline function setindex!(a::FqMatrix, u::ZZRingElem, i::Int, j::Int)
Expand All @@ -64,8 +62,7 @@ end
nothing
end

setindex!(a::FqMatrix, u::Integer, i::Int, j::Int) =
setindex!(a, base_ring(a)(u), i, j)
setindex!(a::FqMatrix, u::Integer, i::Int, j::Int) = setindex!(a, base_ring(a)(u), i, j)

function setindex!(a::FqMatrix, b::FqMatrix, r::UnitRange{Int64}, c::UnitRange{Int64})
_checkbounds(a, r, c)
Expand Down Expand Up @@ -677,30 +674,20 @@ function (a::FqMatrixSpace)(b::FqFieldElem)
return FqMatrix(nrows(a), ncols(a), b)
end

function (a::FqMatrixSpace)(arr::AbstractMatrix{T}) where {T <: Integer}
_check_dim(nrows(a), ncols(a), arr)
return FqMatrix(nrows(a), ncols(a), arr, base_ring(a))
end

function (a::FqMatrixSpace)(arr::AbstractVector{T}) where {T <: Integer}
function (a::FqMatrixSpace)(arr::AbstractMatrix{<:IntegerUnion})
_check_dim(nrows(a), ncols(a), arr)
return FqMatrix(nrows(a), ncols(a), arr, base_ring(a))
return FqMatrix(arr, base_ring(a))
end

function (a::FqMatrixSpace)(arr::AbstractMatrix{ZZRingElem})
_check_dim(nrows(a), ncols(a), arr)
return FqMatrix(nrows(a), ncols(a), arr, base_ring(a))
end

function (a::FqMatrixSpace)(arr::AbstractVector{ZZRingElem})
function (a::FqMatrixSpace)(arr::AbstractVector{<:IntegerUnion})
_check_dim(nrows(a), ncols(a), arr)
return FqMatrix(nrows(a), ncols(a), arr, base_ring(a))
end

function (a::FqMatrixSpace)(arr::AbstractMatrix{FqFieldElem})
_check_dim(nrows(a), ncols(a), arr)
(length(arr) > 0 && (base_ring(a) != parent(arr[1]))) && error("Elements must have same base ring")
return FqMatrix(nrows(a), ncols(a), arr, base_ring(a))
return FqMatrix(arr, base_ring(a))
end

function (a::FqMatrixSpace)(arr::AbstractVector{FqFieldElem})
Expand Down Expand Up @@ -735,7 +722,8 @@ end
###############################################################################

function matrix(R::FqField, arr::AbstractMatrix{<: Union{FqFieldElem, ZZRingElem, Integer}})
z = FqMatrix(size(arr, 1), size(arr, 2), arr, R)
Base.require_one_based_indexing(arr)
z = FqMatrix(arr, R)
return z
end

Expand Down
Loading
Loading