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

Some matrix optimizations #1712

Merged
merged 10 commits into from
Apr 16, 2024
60 changes: 11 additions & 49 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2490,105 +2490,67 @@ mutable struct FqFieldElem <: FinFieldElem
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
if _fq_default_ctx_type(ctx) != _FQ_DEFAULT_NMOD
finalizer(_fq_default_clear_fn, d)
end
d.poly = nothing
d.parent = ctx
return d
end

function FqFieldElem(ctx::FqField, x::Int)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_si, libflint), Nothing,
(Ref{FqFieldElem}, Int, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::ZZRingElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_fmpz, libflint), Nothing,
(Ref{FqFieldElem}, Ref{ZZRingElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::ZZPolyRingElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_fmpz_poly, libflint), Nothing,
(Ref{FqFieldElem}, Ref{ZZPolyRingElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::zzModPolyRingElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_nmod_poly, libflint), Nothing,
(Ref{FqFieldElem}, Ref{zzModPolyRingElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::fpPolyRingElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_nmod_poly, libflint), Nothing,
(Ref{FqFieldElem}, Ref{fpPolyRingElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::ZZModPolyRingElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_fmpz_mod_poly, libflint), Nothing,
(Ref{FqFieldElem}, Ref{ZZModPolyRingElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::FpPolyRingElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set_fmpz_mod_poly, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FpPolyRingElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end

function FqFieldElem(ctx::FqField, x::FqFieldElem)
d = new()
ccall((:fq_default_init2, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqField}), d, ctx)
finalizer(_fq_default_clear_fn, d)
d = FqFieldElem(ctx)
ccall((:fq_default_set, libflint), Nothing,
(Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), d, x, ctx)
d.parent = ctx
d.poly = nothing
return d
end
end
Expand Down
19 changes: 19 additions & 0 deletions src/flint/fmpq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ Base.@propagate_inbounds setindex!(a::QQMatrix, d::Integer,
end
end

function setindex!(a::QQMatrix, b::QQMatrix, r::UnitRange{Int64}, c::UnitRange{Int64})
_checkbounds(a, r, c)
size(b) == (length(r), length(c)) || throw(DimensionMismatch("tried to assign a $(size(b, 1))x$(size(b, 2)) matrix to a $(length(r))x$(length(c)) destination"))
A = view(a, r, c)
ccall((:fmpq_mat_set, libflint), Nothing,
(Ref{QQMatrix}, Ref{QQMatrix}), A, b)
end

Base.@propagate_inbounds setindex!(a::QQMatrix, d::Rational,
r::Int, c::Int) =
setindex!(a, QQFieldElem(d), r, c)
Expand Down Expand Up @@ -893,6 +901,17 @@ function zero!(z::QQMatrix)
return z
end

function Generic.add_one!(a::QQMatrix, i::Int, j::Int)
@boundscheck Generic._checkbounds(a, i, j)
GC.@preserve a begin
x = mat_entry_ptr(a, i, j)
ccall((:fmpq_add_si, libflint), Nothing,
(Ptr{QQFieldElem}, Ptr{QQFieldElem}, Int),
x, x, 1)
end
return a
end

###############################################################################
#
# Parent object call overloads
Expand Down
18 changes: 18 additions & 0 deletions src/flint/fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ end
end
end

function setindex!(a::ZZMatrix, b::ZZMatrix, r::UnitRange{Int64}, c::UnitRange{Int64})
_checkbounds(a, r, c)
size(b) == (length(r), length(c)) || throw(DimensionMismatch("tried to assign a $(size(b, 1))x$(size(b, 2)) matrix to a $(length(r))x$(length(c)) destination"))
A = view(a, r, c)
ccall((:fmpz_mat_set, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}), A, b)
end

@inline number_of_rows(a::ZZMatrix) = a.r

@inline number_of_columns(a::ZZMatrix) = a.c
Expand Down Expand Up @@ -1713,6 +1721,16 @@ function mul!(z::Vector{ZZRingElem}, a::Vector{ZZRingElem}, b::ZZMatrix)
return z
end

function Generic.add_one!(a::ZZMatrix, i::Int, j::Int)
@boundscheck Generic._checkbounds(a, i, j)
GC.@preserve a begin
x = mat_entry_ptr(a, i, j)
ccall((:fmpz_add_si, libflint), Nothing,
(Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int),
x, x, 1)
end
return a
end

###############################################################################
#
Expand Down
29 changes: 29 additions & 0 deletions src/flint/fmpz_mod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ end
(Ref{T}, Int, Int, Ref{ZZRingElem}), a, i - 1, j - 1, u)
end

function setindex!(a::ZZModMatrix, b::ZZModMatrix, r::UnitRange{Int64}, c::UnitRange{Int64})
_checkbounds(a, r, c)
size(b) == (length(r), length(c)) || throw(DimensionMismatch("tried to assign a $(size(b, 1))x$(size(b, 2)) matrix to a $(length(r))x$(length(c)) destination"))
A = view(a, r, c)
ccall((:fmpz_mod_mat_set, libflint), Nothing,
(Ref{ZZModMatrix}, Ref{ZZModMatrix}), A, b)
end

function deepcopy_internal(a::ZZModMatrix, dict::IdDict)
z = ZZModMatrix(nrows(a), ncols(a), modulus(base_ring(a)))
if isdefined(a, :base_ring)
Expand Down Expand Up @@ -280,6 +288,17 @@ function mul!(z::Vector{ZZRingElem}, a::Vector{ZZRingElem}, b::T) where T <: Zmo
return z
end

function Generic.add_one!(a::ZZModMatrix, i::Int, j::Int)
@boundscheck Generic._checkbounds(a, i, j)
GC.@preserve a begin
x = mat_entry_ptr(a, i, j)
ccall((:fmpz_mod_add_si, libflint), Nothing,
(Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
x, x, 1, base_ring(a).ninv)
end
return a
end

################################################################################
#
# Ad hoc binary operators
Expand Down Expand Up @@ -949,3 +968,13 @@ function nullspace(M::ZZModMatrix)
(Ref{ZZModMatrix}, Ref{ZZModMatrix}), N, M)
return nullity, view(N, 1:nrows(N), 1:nullity)
end

################################################################################
#
# Entry pointers
#
################################################################################

@inline mat_entry_ptr(A::ZZModMatrix, i::Int, j::Int) =
ccall((:fmpz_mod_mat_entry, libflint), Ptr{ZZRingElem},
(Ref{ZZModMatrix}, Int, Int), A, i - 1, j - 1)
65 changes: 44 additions & 21 deletions src/flint/fq_default_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ end
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)
size(b) == (length(r), length(c)) || throw(DimensionMismatch("tried to assign a $(size(b, 1))x$(size(b, 2)) matrix to a $(length(r))x$(length(c)) destination"))
A = view(a, r, c)
ccall((:fq_default_mat_set, libflint), Nothing,
(Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), A, b, base_ring(a))
end

function deepcopy_internal(a::FqMatrix, dict::IdDict)
z = FqMatrix(nrows(a), ncols(a), base_ring(a))
ccall((:fq_default_mat_set, libflint), Nothing,
Expand Down Expand Up @@ -153,29 +161,25 @@ isequal(a::FqMatrix, b::FqMatrix) = ==(a, b)
################################################################################

function transpose(a::FqMatrix)
z = FqMatrix(ncols(a), nrows(a), base_ring(a))
for i in 1:nrows(a)
for j in 1:ncols(a)
z[j, i] = a[i, j]
end
end
return z
z = similar(a, ncols(a), nrows(a))
if _fq_default_ctx_type(base_ring(a)) == _FQ_DEFAULT_NMOD
ccall((:nmod_mat_transpose, libflint), Nothing, (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), z, a, base_ring(a))
return z
elseif _fq_default_ctx_type(base_ring(a)) == _FQ_DEFAULT_FMPZ_NMOD
ccall((:fmpz_mod_mat_transpose, libflint), Nothing, (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), z, a, base_ring(a))
return z
end
# There is no flint functionality for the other cases
t = base_ring(a)()
for i in 1:nrows(a)
for j in 1:ncols(a)
getindex!(t, a, i, j)
z[j, i] = t
end
end
return z
end

# There is no transpose for FqMatrix
#function transpose(a::FqMatrix)
# z = FqMatrixSpace(base_ring(a), ncols(a), nrows(a))()
# ccall((:fq_default_mat_transpose, libflint), Nothing,
# (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), z, a, base_ring(a))
# return z
#end
#
#function transpose!(a::FqMatrix)
# !is_square(a) && error("Matrix must be a square matrix")
# ccall((:fq_default_mat_transpose, libflint), Nothing,
# (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), a, a, base_ring(a))
#end

###############################################################################
#
# Row and column swapping
Expand Down Expand Up @@ -298,6 +302,25 @@ function zero!(a::FqMatrix)
return a
end

function Generic.add_one!(a::FqMatrix, i::Int, j::Int)
@boundscheck Generic._checkbounds(a, i, j)
F = base_ring(a)
GC.@preserve a begin
x = fq_default_mat_entry_ptr(a, i, j)
# There is no fq_default_add_one, but only ...sub_one
ccall((:fq_default_neg, libflint), Nothing,
(Ptr{FqFieldElem}, Ptr{FqFieldElem}, Ref{FqField}),
x, x, F)
ccall((:fq_default_sub_one, libflint), Nothing,
(Ptr{FqFieldElem}, Ptr{FqFieldElem}, Ref{FqField}),
x, x, F)
ccall((:fq_default_neg, libflint), Nothing,
(Ptr{FqFieldElem}, Ptr{FqFieldElem}, Ref{FqField}),
x, x, F)
end
return a
end

################################################################################
#
# Ad hoc binary operators
Expand Down
27 changes: 27 additions & 0 deletions src/flint/fq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ end
setindex!(a::FqPolyRepMatrix, u::Integer, i::Int, j::Int) =
setindex!(a, base_ring(a)(u), i, j)

function setindex!(a::FqPolyRepMatrix, b::FqPolyRepMatrix, r::UnitRange{Int64}, c::UnitRange{Int64})
_checkbounds(a, r, c)
size(b) == (length(r), length(c)) || throw(DimensionMismatch("tried to assign a $(size(b, 1))x$(size(b, 2)) matrix to a $(length(r))x$(length(c)) destination"))
A = view(a, r, c)
ccall((:fq_mat_set, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), A, b, base_ring(A))
end

function deepcopy_internal(a::FqPolyRepMatrix, dict::IdDict)
z = FqPolyRepMatrix(nrows(a), ncols(a), base_ring(a))
ccall((:fq_mat_set, libflint), Nothing,
Expand Down Expand Up @@ -308,6 +316,25 @@ function mul!(z::Vector{FqPolyRepFieldElem}, a::Vector{FqPolyRepFieldElem}, b::F
return z
end

function Generic.add_one!(a::FqPolyRepMatrix, i::Int, j::Int)
@boundscheck Generic._checkbounds(a, i, j)
F = base_ring(a)
GC.@preserve a begin
x = mat_entry_ptr(a, i, j)
# There is no fq_add_one, but only ...sub_one
ccall((:fq_neg, libflint), Nothing,
(Ptr{FqPolyRepFieldElem}, Ptr{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
x, x, F)
ccall((:fq_sub_one, libflint), Nothing,
(Ptr{FqPolyRepFieldElem}, Ptr{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
x, x, F)
ccall((:fq_neg, libflint), Nothing,
(Ptr{FqPolyRepFieldElem}, Ptr{FqPolyRepFieldElem}, Ref{FqPolyRepField}),
x, x, F)
end
return a
end

################################################################################
#
# Ad hoc binary operators
Expand Down
27 changes: 27 additions & 0 deletions src/flint/fq_nmod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ end
setindex!(a::fqPolyRepMatrix, u::Integer, i::Int, j::Int) =
setindex!(a, base_ring(a)(u), i, j)

function setindex!(a::fqPolyRepMatrix, b::fqPolyRepMatrix, r::UnitRange{Int64}, c::UnitRange{Int64})
_checkbounds(a, r, c)
size(b) == (length(r), length(c)) || throw(DimensionMismatch("tried to assign a $(size(b, 1))x$(size(b, 2)) matrix to a $(length(r))x$(length(c)) destination"))
A = view(a, r, c)
ccall((:fq_nmod_mat_set, libflint), Nothing,
(Ref{fqPolyRepMatrix}, Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), A, b, base_ring(A))
end

function deepcopy_internal(a::fqPolyRepMatrix, dict::IdDict)
z = fqPolyRepMatrix(nrows(a), ncols(a), base_ring(a))
ccall((:fq_nmod_mat_set, libflint), Nothing,
Expand Down Expand Up @@ -296,6 +304,25 @@ function mul!(z::Vector{fqPolyRepFieldElem}, a::Vector{fqPolyRepFieldElem}, b::f
return z
end

function Generic.add_one!(a::fqPolyRepMatrix, i::Int, j::Int)
@boundscheck Generic._checkbounds(a, i, j)
F = base_ring(a)
GC.@preserve a begin
x = mat_entry_ptr(a, i, j)
# There is no fq_nmod_add_one, but only ...sub_one
ccall((:fq_nmod_neg, libflint), Nothing,
(Ptr{fqPolyRepFieldElem}, Ptr{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
x, x, F)
ccall((:fq_nmod_sub_one, libflint), Nothing,
(Ptr{fqPolyRepFieldElem}, Ptr{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
x, x, F)
ccall((:fq_nmod_neg, libflint), Nothing,
(Ptr{fqPolyRepFieldElem}, Ptr{fqPolyRepFieldElem}, Ref{fqPolyRepField}),
x, x, F)
end
return a
end

################################################################################
#
# Ad hoc binary operators
Expand Down
Loading
Loading