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

Wrap more inplace operations for fmpz and fmpq #1169

Merged
merged 1 commit into from
Sep 8, 2021
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
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 @@ -414,3 +414,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 @@ -638,14 +638,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