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

Unify rounding code #1540

Merged
merged 1 commit into from
Oct 8, 2023
Merged

Unify rounding code #1540

merged 1 commit into from
Oct 8, 2023

Conversation

fingolfin
Copy link
Member

@fingolfin fingolfin commented Sep 22, 2023

Systematically provide methods for ceil, floor, round, trunc with various source and/or target types we provide. Remove some (accidental?) type piracy.

For m a Matrix{BigFloat} instead of trunc(m) one now needs to write trunc(ZZMatrix, m) which avoids type piracy. On the upside, this is now supported for any Matrix{<:Real}.

Also add methods is_positive(::QQFieldElem) and is_negative(::QQFieldElem), move some functions to more appropriate places, and "optimize" (?) sign(::Type{Int}, a::QQFieldElem).

See also #1495

@codecov
Copy link

codecov bot commented Sep 22, 2023

Codecov Report

Merging #1540 (b4ec113) into master (a02b817) will increase coverage by 0.16%.
The diff coverage is 82.14%.

@@            Coverage Diff             @@
##           master    #1540      +/-   ##
==========================================
+ Coverage   84.03%   84.19%   +0.16%     
==========================================
  Files          94       94              
  Lines       36614    36614              
==========================================
+ Hits        30767    30826      +59     
+ Misses       5847     5788      -59     
Files Coverage Δ
src/HeckeMiscInteger.jl 22.59% <100.00%> (+7.98%) ⬆️
src/HeckeMoreStuff.jl 2.84% <ø> (+0.32%) ⬆️
src/flint/fmpz.jl 91.45% <66.66%> (+0.04%) ⬆️
src/flint/fmpq.jl 85.03% <79.48%> (-0.72%) ⬇️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@fingolfin
Copy link
Member Author

This PR got a bit out of hand 😅 but it should be there now.

@thofma
Copy link
Member

thofma commented Oct 4, 2023

It seems that code coverage for the changes is below 50%? Is this a fluke?

@fingolfin fingolfin force-pushed the mh/rounding branch 2 times, most recently from 90f256b to b37bd2d Compare October 4, 2023 20:29
@fingolfin
Copy link
Member Author

Part of this PR is moving a lot of untested code around. While I added some tests for new stuff I added, I did not e.g. add tests for the various round methods for QQFieldElem, so all of this drags the coverage down.

But I've now taken the time to add some (and of course once again found and fixed issues in the existing untested code 🙄 ).

@@ -215,35 +215,8 @@ function (::ZZRing)(x::Rational{Int})
return ZZRingElem(numerator(x))
end

function ceil(::Type{ZZRingElem}, a::BigFloat)
Copy link
Member Author

Choose a reason for hiding this comment

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

BTW this is now handled by generic code (in the macro) which also supports e.g. Float64 and more

Systematically provide methods for ceil, floor, round, trunc with
various source and/or target types we provide. Remove some (accidental?)
type piracy.

For `m` a `Matrix{BigFloat}` instead of `trunc(m)` one now needs to
write `trunc(ZZMatrix, m)` which avoids type piracy. On the upside,
this is now supported for any `Matrix{<:Real}`.

Also add methods `is_positive(::QQFieldElem)` and `is_negative(::QQFieldElem)`,
move some functions to more appropriate places, and "optimize" (?)
`sign(::Type{Int}, a::QQFieldElem)`.
@thofma
Copy link
Member

thofma commented Oct 8, 2023

The Oscar error looks real: https://github.com/Nemocas/Nemo.jl/actions/runs/6411567510/job/17444653341?pr=1540

Any idea what is going on?

@benlorenz
Copy link
Collaborator

The Oscar error looks real: Nemocas/Nemo.jl/actions/runs/6411567510/job/17444653341?pr=1540

Any idea what is going on?

https://github.com/thofma/Hecke.jl/blob/b3fdace2a0a75dc21d7bd1480298ebdfbae6237b/src/QuadForm/indefiniteLLL.jl#L300-L301:

  U3 = identity_matrix(ZZ,n)
  U3[n,1] = round(- G3[n,n]//2*1//G3[1,n])

The return value of the round seems to be a QQFieldElem now which doesn't want to be assigned to the ZZMatrix?

Old:

function Base.round(a::QQFieldElem)
    return round(ZZRingElem, a)
end

vs new:

Base.round(a::QQFieldElem) = round(QQFieldElem, a)

@lgoettgens
Copy link
Collaborator

To mimic the behavior of Base for Float, I would say that the new behavior is better (cf https://docs.julialang.org/en/v1/base/math/#Base.round-Tuple{Type,%20Any}).

@fingolfin
Copy link
Member Author

The documentation of round states:

round([T,] x, [r::RoundingMode])
round(x, [r::RoundingMode]; digits::Integer=0, base = 10)
round(x, [r::RoundingMode]; sigdigits::Integer, base = 10)

Rounds the number x.

Without keyword arguments, x is rounded to an integer value, returning a value of type
T, or of the same type of x if no T is provided.

That round(::QQFieldElem) returned a ZZRingElem violated that, and so I "fixed it".

But of course I can reverse that -- or mark this PR as "breaking" and we just update callsites, I don't think there'll be many, and the fix is trivial (just add ZZRingElem as first argument).

@thofma
Copy link
Member

thofma commented Oct 8, 2023

Let's mark this breaking and fix the callsites when bumping the dependency.

@thofma thofma added the breaking label Oct 8, 2023
@fingolfin
Copy link
Member Author

OK. I am going to release AA now, then Nemo, then... we'll see ;-)

@fingolfin fingolfin merged commit ffc0d7d into Nemocas:master Oct 8, 2023
12 of 15 checks passed
@fingolfin fingolfin deleted the mh/rounding branch October 8, 2023 20:04
@Krastanov
Copy link

I am seeing a ton of new errors reported by Aqua in my CI when it runs with the new version of Nemo.

https://github.com/QuantumSavory/QuantumClifford.jl/actions/runs/6451102837/job/17511289576?pr=193

If I understand correctly, it due to the changes made here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants