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

divrem in Julia is now defined differently to in Nemo #101

Closed
wbhart opened this issue Jun 8, 2018 · 20 comments
Closed

divrem in Julia is now defined differently to in Nemo #101

wbhart opened this issue Jun 8, 2018 · 20 comments

Comments

@wbhart
Copy link
Contributor

wbhart commented Jun 8, 2018

We probably need to define divmod and use it everywhere.

@thofma
Copy link
Member

thofma commented Sep 12, 2018

What to do you mean by "now"? I did not observe a change to divrem in julia.

@wbhart
Copy link
Contributor Author

wbhart commented Sep 12, 2018

I actually have no memory of writing this (though clearly I did).

But I assume divrem is defined as per rem, i.e. the % operator in C, which does not give the positive modulus, as we define it in Nemo. Therefore if you have negative values, divrem with Nemo ZZ will give a different answer than if you use BigInt's in Julia. This means AA will give a different answer to Nemo.

@thofma
Copy link
Member

thofma commented Sep 12, 2018

I cannot follow. They give the same answer. Do you have an example?

@wbhart
Copy link
Contributor Author

wbhart commented Sep 12, 2018 via email

@wbhart
Copy link
Contributor Author

wbhart commented Sep 12, 2018

I guess what I mean is, when we type divrem, we currently mean Euclidean division. But Julia does not give that. Therefore, we should instead use divmod in Nemo and make it return the right thing, instead of what Julia (and apparently Nemo), currently return.

@thofma
Copy link
Member

thofma commented Sep 12, 2018

I don't see the problem. We currently have divrem(x, y) = div(x, y), rem(x, y) for Int, BigInt and fmpz. What we return is a valid Euclidean divsion. I don't see the point of changing rem and introducing yet another function for division.

@wbhart
Copy link
Contributor Author

wbhart commented Sep 12, 2018 via email

@thofma
Copy link
Member

thofma commented Sep 12, 2018

Sorry, yes there would be not change to rem.

Currently mod(2, 5) and mod(-3, 5) return the same result.

@wbhart
Copy link
Contributor Author

wbhart commented Sep 12, 2018 via email

@thofma
Copy link
Member

thofma commented Sep 12, 2018 via email

@wbhart
Copy link
Contributor Author

wbhart commented Sep 12, 2018 via email

@thofma
Copy link
Member

thofma commented Sep 13, 2018

I think we have been bitten by this before (divrem not yielding something positive as the second argument) and I see how divmod would help with this. I would like to hear what @fieker thinks.

So the proposal is to introduce divmod, which would be the same as divrem for polynomial rings, but would always return a positive remainder for integers.

@fieker
Copy link
Contributor

fieker commented Sep 13, 2018 via email

@thofma
Copy link
Member

thofma commented Sep 13, 2018

I just did some more testing and searching and here is what I found out.

  1. Let's first look at %, mod and rem. There is no way we can be consistent with julia:
  • julias mod is not the same as our mod: mod(x, y) will always have the same sign as y, that is,
    mod(1, -2) = -1. On the other hand, for fmpz mod(x, y) is always positive.
  • % is just an alias for rem.
  1. More worrying is the following: Very often one needs a div(x, y) such that x - div(x, y) * y = mod(x, y). Think for example of reduction of a row in the Hermite form algorithm. At the moment, div(x, y) and divrem(x, y) won't give you such a division. Consequently you will find something like https://github.com/thofma/Hecke.jl/blob/c3e395397d4a5d890446b40ef477e85870fab793/src/Sparse/HNF.jl#L369-L374

I don't see a clear way forward. I don't mind too much about 1), but 2) is very annoying.

@wbhart
Copy link
Contributor Author

wbhart commented Sep 13, 2018 via email

@wbhart
Copy link
Contributor Author

wbhart commented Sep 13, 2018 via email

@fieker
Copy link
Contributor

fieker commented Sep 14, 2018 via email

@wbhart
Copy link
Contributor Author

wbhart commented Sep 14, 2018 via email

@thofma
Copy link
Member

thofma commented Sep 15, 2018

No, it is not true that julias mod and rem are the same.

julia> rem(3, -2)
1

julia> mod(3, -2)
-1

Same for BigInt.

@wbhart
Copy link
Contributor Author

wbhart commented Jun 9, 2020

The original ticket here is no longer valid. And we are basically doing this optimally, with our own internal hacked versions of div and divrem defined in terms of mod not rem. It's not possible to do better without breaking HNF and ResidueRing/Field.

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

No branches or pull requests

3 participants