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

add divrem for EuclideanRingResidueRing #1921

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions src/generic/Residue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,56 @@
c.data = mod(data(a) + data(b), modulus(a))
return c
end

euclid(n::EuclideanRingResidueRingElem) = degree(gcd(data(n), modulus(n)))

Check warning on line 150 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L150

Added line #L150 was not covered by tests

#horrible - and copied from fmpz_mod
#don't know how to seriously simplify it
#maybe a durect gcdx should be added as well
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#maybe a durect gcdx should be added as well
#maybe a direct gcdx should be added as well

function Base.divrem(n::T, m::T) where {T <: EuclideanRingResidueRingElem}
@assert !iszero(m)
R = parent(n)
e = euclid(m)
if iszero(e)
fl, q = divides(n, m)
@assert fl
return q, zero(R)

Check warning on line 162 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L155-L162

Added lines #L155 - L162 were not covered by tests
end

S = typeof(data(n))

Check warning on line 165 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L165

Added line #L165 was not covered by tests

cp = coprime_base(S[data(n), data(m), modulus(m)])::Vector{S}

Check warning on line 167 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L167

Added line #L167 was not covered by tests

q = Vector{Tuple{S, S}}()
inf = -1
for i=1:length(cp)
is_unit(cp[i]) && continue
v = valuation(modulus(R), cp[i])::Int
if v != 0
pk = cp[i]^v
nv = iszero(data(n) % pk) ? inf : valuation(data(n) % pk, cp[i])
mv = iszero(data(m) % pk) ? inf : valuation(data(m) % pk, cp[i])
if nv < mv
push!(q, (pk, zero(data(n))))

Check warning on line 179 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L169-L179

Added lines #L169 - L179 were not covered by tests
else
if nv === inf
push!(q, (pk, one(data(n))))

Check warning on line 182 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L181-L182

Added lines #L181 - L182 were not covered by tests
else
push!(q, (pk, divexact(data(n) % pk, cp[i]^nv)))

Check warning on line 184 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L184

Added line #L184 was not covered by tests
end
end
end
end
qq = R(crt([x[2] for x = q], [x[1] for x = q])::S)::T

Check warning on line 189 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L188-L189

Added lines #L188 - L189 were not covered by tests
#need to adjust the leading term of qq so it cancelles:
# x * lc(qq)*lc(m) = lc(n)
# so x = lc(n)/lc(qq)/lc(m)
if !is_zero(qq)
qq *= leading_coefficient(data(n)) //leading_coefficient(data(qq)) // leading_coefficient(data(m))

Check warning on line 194 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L193-L194

Added lines #L193 - L194 were not covered by tests
end
rr = n-qq*m
@assert n == qq*m+rr
@assert rr == 0 || euclid(rr) < e
return (qq,rr)::Tuple{T, T}

Check warning on line 199 in src/generic/Residue.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Residue.jl#L196-L199

Added lines #L196 - L199 were not covered by tests
end

Loading