Skip to content

Commit

Permalink
Use low-level helpers in yet more places (#1811)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Jul 1, 2024
1 parent 82519f3 commit baff84a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 102 deletions.
3 changes: 1 addition & 2 deletions src/HeckeMiscInteger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ function rand(rng::AbstractRNG, a::ZZRingElemUnitRange)
for i = 1:nl
ccall((:fmpz_mul_2exp, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), s, s, c)
ccall((:fmpz_add_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), s, s, rand(rng, Base.GMP.Limb))
add!(s, s, rand(rng, Base.GMP.Limb))
end
if high > 0
s = s << high
Expand Down
3 changes: 1 addition & 2 deletions src/HeckeMiscMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ diagonal(A::MatrixElem{T}) where {T} = T[A[i, i] for i in 1:min(nrows(A),ncols(A
#
################################################################################


function prod_diagonal(A::ZZMatrix)
a = one(ZZRingElem)
GC.@preserve a A begin
for i = 1:min(nrows(A),ncols(A))
b = mat_entry_ptr(A, i, i)
ccall((:fmpz_mul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{ZZRingElem}), a, a, b)
mul!(a, a, b)
end
end
return a
Expand Down
9 changes: 3 additions & 6 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,19 @@ end

function +(a::QQFieldElem, b::QQFieldElem)
z = QQFieldElem()
ccall((:fmpq_add, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b)
add!(z, a, b)
return z
end

function -(a::QQFieldElem, b::QQFieldElem)
z = QQFieldElem()
ccall((:fmpq_sub, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b)
sub!(z, a, b)
return z
end

function *(a::QQFieldElem, b::QQFieldElem)
z = QQFieldElem()
ccall((:fmpq_mul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b)
mul!(z, a, b)
return z
end

Expand Down
8 changes: 2 additions & 6 deletions src/flint/fmpq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,7 @@ reverse_cols(x::QQMatrix) = reverse_cols!(deepcopy(x))
function +(x::QQMatrix, y::QQMatrix)
check_parent(x, y)
z = similar(x)
ccall((:fmpq_mat_add, libflint), Nothing,
(Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}),
z, x, y)
add!(z, x, y)
return z
end

Expand Down Expand Up @@ -871,9 +869,7 @@ mul!(x::QQMatrix, y::IntegerUnion) = mul!(x, x, y)
mul!(z::QQMatrix, y::QQMatrix, x::Integer) = mul!(z, y, ZZ(x))

function addeq!(z::QQMatrix, x::QQMatrix)
ccall((:fmpq_mat_add, libflint), Nothing,
(Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), z, z, x)
return z
add!(z, z, x)
end

function zero!(z::QQMatrix)
Expand Down
58 changes: 16 additions & 42 deletions src/flint/fmpz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ canonical_unit(x::ZZRingElem) = x < 0 ? ZZRingElem(-1) : ZZRingElem(1)

function -(x::ZZRingElem)
z = ZZRingElem()
ccall((:fmpz_neg, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), z, x)
neg!(z, x)
return z
end

Expand Down Expand Up @@ -499,48 +499,28 @@ end

function +(x::ZZRingElem, c::Int)
z = ZZRingElem()
if c >= 0
ccall((:fmpz_add_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, c)
else
ccall((:fmpz_sub_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, -c)
end
add!(z, x, c)
return z
end

+(c::Int, x::ZZRingElem) = x + c

function -(x::ZZRingElem, c::Int)
z = ZZRingElem()
if c >= 0
ccall((:fmpz_sub_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, c)
else
ccall((:fmpz_add_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, -c)
end
sub!(z, x, c)
return z
end

function -(c::Int, x::ZZRingElem)
z = ZZRingElem()
if c >= 0
ccall((:fmpz_sub_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, c)
else
ccall((:fmpz_add_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, -c)
end
ccall((:fmpz_neg, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}), z, z)
sub!(z, x, c)
neg!(z, z)
return z
end

function *(x::ZZRingElem, c::Int)
z = ZZRingElem()
ccall((:fmpz_mul_si, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, c)
mul!(z, x, c)
return z
end

Expand Down Expand Up @@ -990,7 +970,7 @@ end

function mod(x::ZZRingElem, c::UInt)
c == 0 && throw(DivideError())
ccall((:fmpz_fdiv_ui, libflint), Base.GMP.Limb, (Ref{ZZRingElem}, Base.GMP.Limb), x, c)
ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), x, c)
end

@doc raw"""
Expand Down Expand Up @@ -2111,7 +2091,7 @@ function binomial(n::ZZRingElem, k::ZZRingElem)
else
z = _binomial(n, n - k)
end
return negz ? neg!(z, z) : z
return negz ? neg!(z) : z
end

@doc raw"""
Expand Down Expand Up @@ -2668,58 +2648,52 @@ end
add!(z::ZZRingElem, a::ZZRingElem, b::Integer) = add!(z, a, ZZRingElem(b))
add!(z::ZZRingElem, x::Int, y::ZZRingElem) = add!(z, y, x)

function neg!(z::ZZRingElem, a::ZZRingElem)
function neg!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr)
ccall((:fmpz_neg, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}),
z, a)
return z
end

function neg!(a::ZZRingElem)
return neg!(a,a)
end
neg!(a::ZZRingElemOrPtr) = neg!(a, a)

function sub!(z::ZZRingElem, a::ZZRingElem, b::ZZRingElem)
function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr)
ccall((:fmpz_sub, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}),
z, a, b)
return z
end

function sub!(z::ZZRingElem, a::ZZRingElem, b::Int)
function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Int)
ccall((:fmpz_sub_si, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int),
z, a, b)
return z
end

function sub!(z::ZZRingElem, a::ZZRingElem, b::UInt)
function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt)
ccall((:fmpz_sub_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, UInt),
z, a, b)
return z
end

function sub!(z::ZZRingElem, a::ZZRingElem, b::Integer)
function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer)
return sub!(z, a, ZZRingElem(b))
end

function sub!(z::ZZRingElem, b::Integer, a::ZZRingElem)
function sub!(z::ZZRingElemOrPtr, b::Integer, a::ZZRingElemOrPtr)
sub!(z, a, b)
return neg!(z, z)
end


function mul!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem)
function mul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr)
ccall((:fmpz_mul, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y)
return z
end

function mul!(a::Ref{ZZRingElem}, b::Ref{ZZRingElem}, c::ZZRingElem)
ccall((:fmpz_mul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), a, b, c)
end

function mul!(z::ZZRingElem, x::ZZRingElem, y::Int)
ccall((:fmpz_mul_si, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, y)
Expand Down
44 changes: 20 additions & 24 deletions src/flint/fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ canonical_unit(a::ZZMatrix) = canonical_unit(a[1, 1])

function -(x::ZZMatrix)
z = similar(x)
ccall((:fmpz_mat_neg, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}), z, x)
neg!(z, x)
return z
end

Expand Down Expand Up @@ -295,18 +294,14 @@ reverse_cols(x::ZZMatrix) = reverse_cols!(deepcopy(x))
function +(x::ZZMatrix, y::ZZMatrix)
check_parent(x, y)
z = similar(x)
ccall((:fmpz_mat_add, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}),
z, x, y)
add!(z, x, y)
return z
end

function -(x::ZZMatrix, y::ZZMatrix)
check_parent(x, y)
z = similar(x)
ccall((:fmpz_mat_sub, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}),
z, x, y)
sub!(z, x, y)
return z
end

Expand All @@ -317,9 +312,7 @@ function *(x::ZZMatrix, y::ZZMatrix)
else
z = similar(x, nrows(x), ncols(y))
end
ccall((:fmpz_mat_mul, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}),
z, x, y)
mul!(z, x, y)
return z
end

Expand Down Expand Up @@ -1514,8 +1507,8 @@ function Solve._can_solve_internal_no_check(A::ZZMatrix, b::ZZMatrix, task::Symb
H_ptr = mat_entry_ptr(H, j, k)
for h = k:ncols(H)
b_ptr = mat_entry_ptr(b, h, i)
ccall((:fmpz_mul, libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{ZZRingElem}), t, q, H_ptr)
ccall((:fmpz_sub, libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), b_ptr, b_ptr, t)
mul!(t, q, H_ptr)
sub!(b_ptr, b_ptr, t)
H_ptr += sizeof(ZZRingElem)
end
end
Expand Down Expand Up @@ -1799,7 +1792,7 @@ function AbstractAlgebra._solve_tril!(A::ZZMatrix, B::ZZMatrix, C::ZZMatrix, f::
B_ptr = mat_entry_ptr(B, j, 1)
for k = 1:j-1
A_ptr = mat_entry_ptr(B, k, i)
ccall((:fmpz_mul, libflint), Cvoid, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ptr{ZZRingElem}), s, A_ptr, B_ptr)
mul!(s, A_ptr, B_ptr)
B_ptr += sizeof(ZZRingElem)
sub!(t, t, s)
end
Expand Down Expand Up @@ -1894,15 +1887,17 @@ function mul!(z::ZZMatrix, x::ZZMatrix, y::ZZMatrix, fl::Bool = false)
end

function mul!(y::ZZMatrix, x::Int)
mul!(y, y, x)
end

function mul!(z::ZZMatrix, y::ZZMatrix, x::Int)
ccall((:fmpz_mat_scalar_mul_si, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}, Int), y, y, x)
return y
(Ref{ZZMatrix}, Ref{ZZMatrix}, Int), z, y, x)
return z
end

function mul!(y::ZZMatrix, x::ZZRingElem)
ccall((:fmpz_mat_scalar_mul_fmpz, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), y, y, x)
return y
mul!(y, y, x)
end

function mul!(z::ZZMatrix, y::ZZMatrix, x::ZZRingElem)
Expand All @@ -1924,8 +1919,7 @@ function addmul!(z::ZZMatrix, y::ZZMatrix, x::Int)
end

function addeq!(z::ZZMatrix, x::ZZMatrix)
ccall((:fmpz_mat_add, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}), z, z, x)
add!(z, z, x)
return z
end

Expand All @@ -1935,11 +1929,13 @@ function zero!(z::ZZMatrix)
return z
end

function neg!(w::ZZMatrix)
ccall((:fmpz_mat_neg, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), w, w)
return w
function neg!(z::ZZMatrix, w::ZZMatrix)
ccall((:fmpz_mat_neg, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), z, w)
return z
end

neg!(z::ZZMatrix) = neg!(z, z)

function mul!(z::Vector{ZZRingElem}, a::ZZMatrix, b::Vector{ZZRingElem})
ccall((:fmpz_mat_mul_fmpz_vec_ptr, libflint), Nothing,
(Ptr{Ref{ZZRingElem}}, Ref{ZZMatrix}, Ptr{Ref{ZZRingElem}}, Int),
Expand Down
2 changes: 1 addition & 1 deletion src/flint/fmpz_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ end
#
###############################################################################

for (jT, cN, cT) in ((ZZRingElem, :fmpz, Ref{ZZRingElem}), (Int, :si, Int))
for (jT, cN, cT) in ((ZZRingElem, :fmpz, Ref{ZZRingElem}), (Int, :si, Int), (UInt, :ui, UInt))
@eval begin
function +(a::ZZMPolyRingElem, b::($jT))
z = parent(a)()
Expand Down
20 changes: 10 additions & 10 deletions src/flint/fq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,28 +224,21 @@ end
function +(x::FqPolyRepMatrix, y::FqPolyRepMatrix)
check_parent(x,y)
z = similar(x)
ccall((:fq_mat_add, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}),
z, x, y, base_ring(x))
return z
add!(z, x, y)
end

function -(x::FqPolyRepMatrix, y::FqPolyRepMatrix)
check_parent(x,y)
z = similar(x)
ccall((:fq_mat_sub, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}),
z, x, y, base_ring(x))

sub!(z, x, y)
return z
end

function *(x::FqPolyRepMatrix, y::FqPolyRepMatrix)
(base_ring(x) != base_ring(y)) && error("Base ring must be equal")
(ncols(x) != nrows(y)) && error("Dimensions are wrong")
z = similar(x, nrows(x), ncols(y))
ccall((:fq_mat_mul, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), z, x, y, base_ring(x))
mul!(z, x, y)
return z
end

Expand All @@ -270,6 +263,13 @@ function add!(a::FqPolyRepMatrix, b::FqPolyRepMatrix, c::FqPolyRepMatrix)
return a
end

function sub!(a::FqPolyRepMatrix, b::FqPolyRepMatrix, c::FqPolyRepMatrix)
ccall((:fq_mat_sub, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}),
a, b, c, base_ring(a))
return a
end

function zero!(a::FqPolyRepMatrix)
ccall((:fq_mat_zero, libflint), Nothing,
(Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), a, base_ring(a))
Expand Down
Loading

0 comments on commit baff84a

Please sign in to comment.