From ee7d964198f9f0a43e82016c9234e5050c7a87dc Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 16 Aug 2021 11:29:22 +0200 Subject: [PATCH] Wrap more inplace operations for fmpz and fmpq --- src/flint/fmpq.jl | 44 +++++++++++++++++++++++++++++++++++++---- src/flint/fmpz.jl | 24 ++++++++++++++++++++++ test/flint/fmpq-test.jl | 37 ++++++++++++++++++++++++++++++++++ test/flint/fmpz-test.jl | 14 ++++++++++--- 4 files changed, 112 insertions(+), 7 deletions(-) diff --git a/src/flint/fmpq.jl b/src/flint/fmpq.jl index 6914392eb..1bfdfebd8 100644 --- a/src/flint/fmpq.jl +++ b/src/flint/fmpq.jl @@ -445,7 +445,7 @@ function inv(a::fmpq) return z end - ############################################################################### +############################################################################### # # Exact division # @@ -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 diff --git a/src/flint/fmpz.jl b/src/flint/fmpz.jl index ada597e4e..357ee1626 100644 --- a/src/flint/fmpz.jl +++ b/src/flint/fmpz.jl @@ -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) @@ -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) @@ -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) diff --git a/test/flint/fmpq-test.jl b/test/flint/fmpq-test.jl index 834eb5c43..46aa31fb4 100644 --- a/test/flint/fmpq-test.jl +++ b/test/flint/fmpq-test.jl @@ -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 diff --git a/test/flint/fmpz-test.jl b/test/flint/fmpz-test.jl index 3450c8a89..4590169a5 100644 --- a/test/flint/fmpz-test.jl +++ b/test/flint/fmpz-test.jl @@ -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