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

define divrem for FieldElement instead of FieldElem #252

Merged
merged 7 commits into from
Jan 31, 2019
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
16 changes: 10 additions & 6 deletions src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import_exclude = [:import_exclude, :QQ, :ZZ, :RR,
:AbstractAlgebra,
:exp, :sqrt, :numerator, :denominator,
:promote_rule,
:Set, :Module, :Ring, :Group, :Field]
:Set, :Module, :Ring, :Group, :Field, :divrem]

import LinearAlgebra: det, norm, nullspace, rank, transpose!, hessenberg

import LinearAlgebra: lu, lu!, tr

export nullspace

# Do not import exp, sqrt, numerator or denominator as we define our own
# Do not import divrem, exp, sqrt, numerator or denominator as we define our own
import Base: Array, abs, acos, acosh, asin, asinh, atan, atanh,
bin, ceil, checkbounds, conj, convert, cmp, cos, cosh,
cospi, cot, coth, dec, deepcopy, deepcopy_internal,
div, divrem, expm1, exponent, fill, floor, gcd, gcdx,
div, expm1, exponent, fill, floor, gcd, gcdx,
getindex, hash, hcat, hex, hypot, intersect, inv, invmod, isequal,
isfinite, isless, isone, isqrt, isreal, iszero, lcm, ldexp, length,
log, log1p, mod, ndigits,
Expand Down Expand Up @@ -114,7 +114,7 @@ import .Generic: add!, addeq!, addmul!, base_ring, cached,
det_df, det_fflu, det_popov, dim, disable_cache!,
discriminant, displayed_with_minus_in_front,
divexact, divexact_left, divexact_right, divides,
divrem, domain, downscale,
domain, downscale,
elem_type, enable_cache!, evaluate, exp_gcd,
exponent, exponent_vector, exponent_vectors,
extended_weak_popov, extended_weak_popov_with_trafo, fflu!,
Expand Down Expand Up @@ -168,7 +168,7 @@ import .Generic: add!, addeq!, addmul!, base_ring, cached,
weak_popov_with_trafo, zero,
zero!, zero_matrix, @PolynomialRing

# Do not export exp, sqrt, numerator and denominator as we define our own
# Do not export divrem, exp, sqrt, numerator and denominator as we define our own
export add!, addeq!, addmul!, addmul_delayed_reduction!, base_ring, cached,
canonical_unit, change_base_ring, character,
characteristic, charpoly, charpoly_danilevsky!,
Expand All @@ -181,7 +181,7 @@ export add!, addeq!, addmul!, addmul_delayed_reduction!, base_ring, cached,
det_df, det_fflu, det_popov, dim, disable_cache!,
discriminant, displayed_with_minus_in_front,
divexact, divexact_left, divexact_right, divides,
divrem, domain, downscale,
domain, downscale,
elem_type, enable_cache!, evaluate, exp_gcd,
exponent, exponent_vector, exponent_vectors,
extended_weak_popov, extended_weak_popov_with_trafo, fflu!,
Expand Down Expand Up @@ -240,6 +240,10 @@ function sqrt(a::T) where T
return Base.sqrt(a)
end

function divrem(a::T, b::T) where T
return Base.divrem(a, b)
end

function numerator(a::T, canonicalise::Bool=true) where T
return Base.numerator(a, canonicalise)
end
Expand Down
7 changes: 4 additions & 3 deletions src/generic/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2550,7 +2550,7 @@ function div_monagan_pearce(a::MPoly{T}, b::MPoly{T}, bits::Int) where {T <: Rin
if !d1
k -= 1
else
tq, tr = divrem(qc, mb)
tq, tr = AbstractAlgebra.divrem(qc, mb)
if tq != 0
Qc[k] = tq
monomial_set!(Qe, k, texp, 1, N)
Expand Down Expand Up @@ -2766,7 +2766,7 @@ function divrem_monagan_pearce(a::MPoly{T}, b::MPoly{T}, bits::Int) where {T <:
monomial_set!(Re, l, exp_copy, 1, N)
k -= 1
else
tq, tr = divrem(qc, mb)
tq, tr = AbstractAlgebra.divrem(qc, mb)
if tr != 0
l += 1
if l >= r_alloc
Expand Down Expand Up @@ -2986,7 +2986,7 @@ function divrem_monagan_pearce(a::MPoly{T}, b::Array{MPoly{T}, 1}, bits::Int) wh
for w = 1:len
d1 = monomial_divides!(texp, 1, exp_copy, 1, b[w].exps, 1, mask, N)
if d1
tq, qc = divrem(qc, mb[w])
tq, qc = AbstractAlgebra.divrem(qc, mb[w])
div_flag = qc == 0
if tq != 0
k[w] += 1
Expand Down Expand Up @@ -3536,6 +3536,7 @@ function lcm(a::MPolyElem{T}, b::MPolyElem{T}) where {T <: RingElement}
return divrem(a * b, gcd(a,b))[1]
end
end

function term_gcd(a::MPoly{T}, b::MPoly{T}) where {T <: RingElement}
if a.length < 1
return b
Expand Down
2 changes: 1 addition & 1 deletion src/generic/SparsePoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ function divrem(a::SparsePoly{T}, b::SparsePoly{T}) where {T <: RingElement}
Re[l] = maxn - exp
k -= 1
else
tq, tr = divrem(qc, mb)
tq, tr = AbstractAlgebra.divrem(qc, mb)
if tr != 0
l += 1
if l >= r_alloc
Expand Down
4 changes: 4 additions & 0 deletions src/julia/Float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function divides(a::BigFloat, b::BigFloat)
end
end

function divrem(a::BigFloat, b::BigFloat)
return a/b, 0
end

###############################################################################
#
# GCD
Expand Down
10 changes: 10 additions & 0 deletions src/julia/Rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ function divides(a::Rational{T}, b::Rational{T}) where T <: Integer
return true, a//b
end

###############################################################################
#
# Division with remainder
#
###############################################################################

function divrem(a::Rational{T}, b::Rational{T}) where T <: Integer
return a//b, 0
end

###############################################################################
#
# Exact division
Expand Down
5 changes: 5 additions & 0 deletions test/generic/MPoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,12 @@ function test_gen_mpoly_euclidean_division()
@test q3*f + r3 == g
@test q3 == q4
@test (r3 == 0 && flag == true && q5 == q3) || (r3 != 0 && flag == false)

end

S, varlist = PolynomialRing(QQ, var_names, ordering = ord)
v = varlist[1+Int(round(rand() * (num_vars-1)))]
@test divrem(v, 2*v) == (1//2, 0)
end

println("PASS")
Expand Down
18 changes: 18 additions & 0 deletions test/julia/Floats-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ function test_Floats_exact_division()
println("PASS")
end

function test_Float_divrem()
print("Julia.Float.divrem...")

R = RealField

for iter = 1:1000
r = rand(R, -100:100)
s = zero(R)
while iszero(s)
s = rand(R, -100:100)
end

@test AbstractAlgebra.divrem(r,s) == (r/s,0)
end

println("PASS")
end

function test_Floats_gcd()
print("Julia.Floats.gcd...")

Expand Down
19 changes: 19 additions & 0 deletions test/julia/Rationals-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,32 @@ function test_Rationals_square_root()
println("PASS")
end

function test_Rationals_divrem()
print("Julia.Rationals.divrem...")

R = qq
S = QQ

for iter = 1:1000
r = rand(R, -100:100)
s = zero(R)
while iszero(s)
s = rand(R, -100:100)
end

@test AbstractAlgebra.divrem(r,s) == (r/s,0)
end

println("PASS")
end

function test_Rationals()
test_Rationals_constructors()
test_Rationals_manipulation()
test_Rationals_exact_division()
test_Rationals_gcd()
test_Rationals_square_root()
test_Rationals_divrem()

println("")
end