Skip to content

Commit

Permalink
Wrap more inplace operations for fmpz and fmpq (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Sep 8, 2021
1 parent aef041a commit 4096f8d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
44 changes: 40 additions & 4 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ function inv(a::fmpq)
return z
end

###############################################################################
###############################################################################
#
# Exact division
#
Expand Down Expand Up @@ -802,18 +802,54 @@ function mul!(c::fmpq, a::fmpq, b::fmpq)
return c
end

function addeq!(c::fmpq, a::fmpq)
ccall((:fmpq_add, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Ref{fmpq}), c, c, a)
function mul!(c::fmpq, a::fmpq, b::fmpz)
ccall((:fmpq_mul_fmpz, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Ref{fmpz}), c, a, b)
return c
end

mul!(c::fmpq, a::fmpz, b::fmpq) = mul!(c, b, a)

function mul!(c::fmpq, a::fmpq, b::Int)
ccall((:fmpq_mul_si, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Int), c, a, b)
return c
end

mul!(c::fmpq, a::Int, b::fmpq) = mul!(c, b, a)


function addmul!(c::fmpq, a::fmpq, b::fmpq)
ccall((:fmpq_addmul, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Ref{fmpq}), c, a, b)
return c
end


addeq!(c::fmpq, a::fmpq) = add!(c, c, a)

function add!(c::fmpq, a::fmpq, b::fmpq)
ccall((:fmpq_add, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Ref{fmpq}), c, a, b)
return c
end

function add!(c::fmpq, a::fmpq, b::fmpz)
ccall((:fmpq_add_fmpz, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Ref{fmpz}), c, a, b)
return c
end

add!(c::fmpq, a::fmpz, b::fmpq) = add!(c, b, a)

function add!(c::fmpq, a::fmpq, b::Int)
ccall((:fmpq_add_si, libflint), Nothing,
(Ref{fmpq}, Ref{fmpq}, Int), c, a, b)
return c
end

add!(c::fmpq, a::Int, b::fmpq) = add!(c, b, a)

###############################################################################
#
# Conversions to/from flint Julia rationals
Expand Down
24 changes: 24 additions & 0 deletions src/flint/fmpz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,14 @@ function mul!(z::fmpz, x::fmpz, y::fmpz)
return z
end

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

mul!(z::fmpz, x::Int, y::fmpz) = mul!(z, y, x)

function addmul!(z::fmpz, x::fmpz, y::fmpz)
ccall((:fmpz_addmul, libflint), Nothing,
(Ref{fmpz}, Ref{fmpz}, Ref{fmpz}), z, x, y)
Expand All @@ -2008,6 +2016,14 @@ end

addmul!(z::fmpz, x::fmpz, y::fmpz, ::fmpz) = addmul!(z, x, y)

function addmul!(z::fmpz, x::fmpz, y::Int)
ccall((:fmpz_addmul_si, libflint), Nothing,
(Ref{fmpz}, Ref{fmpz}, Int), z, x, y)
return z
end

addmul!(z::fmpz, x::fmpz, y::Int, ::fmpz) = addmul!(z, x, y)

function addeq!(z::fmpz, x::fmpz)
ccall((:fmpz_add, libflint), Nothing,
(Ref{fmpz}, Ref{fmpz}, Ref{fmpz}), z, z, x)
Expand All @@ -2020,6 +2036,14 @@ function add!(z::fmpz, x::fmpz, y::fmpz)
return z
end

function add!(z::fmpz, x::fmpz, y::Int)
ccall((:fmpz_add_si, libflint), Nothing,
(Ref{fmpz}, Ref{fmpz}, Int), z, x, y)
return z
end

add!(z::fmpz, x::Int, y::fmpz) = add!(z, y, x)

function zero!(z::fmpz)
ccall((:fmpz_zero, libflint), Nothing,
(Ref{fmpz},), z)
Expand Down
37 changes: 37 additions & 0 deletions test/flint/fmpq-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,40 @@ end
@test simplest_between(fmpq(1//10), fmpq(3//10)) == 1//4
@test simplest_between(fmpq(11//10), fmpq(21//10)) == 2
end


@testset "fmpq.unsafe" begin
a = fmpq(32//17)
b = fmpq(23//11)
c = one(FlintQQ)
b_copy = deepcopy(b)
c_copy = deepcopy(c)

zero!(a)
@test iszero(a)
mul!(a, a, b)
@test iszero(a)

add!(a, a, b)
@test a == b
add!(a, a, 1)
@test a == b + 1
add!(a, a, fmpz(0))
@test a == b + 1

addeq!(a, b^2)
@test a == 1 + b + b^2

mul!(a, a, b)
@test a == (1 + b + b^2) * b
mul!(a, a, 3)
@test a == (1 + b + b^2) * b * 3
mul!(a, a, fmpz(3))
@test a == (1 + b + b^2) * b * 9

addmul!(a, a, c)
@test a == 2 * (1 + b + b^2) * b * 9

@test b_copy == b
@test c_copy == c
end
14 changes: 11 additions & 3 deletions test/flint/fmpz-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,22 @@ end
@test iszero(a)
mul!(a, a, b)
@test iszero(a)

add!(a, a, b)
@test a == b
add!(a, a, 1)
@test a == b + 1

addeq!(a, b^2)
@test a == b + b^2
@test a == 1 + b + b^2

mul!(a, a, b)
@test a == (b + b^2) * b
@test a == (1 + b + b^2) * b
mul!(a, a, 3)
@test a == (1 + b + b^2) * b * 3

addmul!(a, a, c)
@test a == 2 * (b + b^2) * b
@test a == 2 * (1 + b + b^2) * b * 3

@test b_copy == b
@test c_copy == c
Expand Down

0 comments on commit 4096f8d

Please sign in to comment.