-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FlintInt and FlintRat helpers
There are a bunch of FLINT function that come in multiple varieties. E.g. fmpz_add, fmpz_add_ui, fmpz_add_si all take one fmpz and one additional argument of type fmpz / UInt / Int, respectively. So naturally we define different `+` and `add!` methods in Julia using them. But which method to invoke when adding a `ZZRingElem` and a Julia integer of a type different from `UInt` and `Int`, such as e.g. `UInt32` ? In that case we currently always convert that into a `ZZRingElem`. But that's inefficient, it would be be better to convert that value to a `UInt` or `Int` value and then use the optimized `fmpz_add_ui/_si` method. Of course this can be done, but getting it right is tedious, repetitive and it is easy to overlook a case. This is where `FlintInt` comes in: this is defined as `Union{Int, ZZRingElem}`. We then provide constructors which convert any `Integer` to a `FlintInt` in an optimal way (at least for a bunch of built-in integer types). This then can be used to write optimal dispatch for Integer types like this: add!(x::ZRingElem, y::ZRingElem) = ... add!(x::ZRingElem, y::Int) = ... add!(x::ZRingElem, y::UInt) = ... # fallback code add!(x::ZRingElem, y::Integer) = add!(x, FlintInt(y)) It also works for types that accept ZZRingElem and Int, but not UInt: add!(x::ZPolyRingElem, y::ZPolyRingElem) = ... add!(x::ZPolyRingElem, y::ZRingElem) = ... add!(x::ZPolyRingElem, y::Int) = ... # fallback code add!(x::ZPolyRingElem, y::Integer) = add!(x, FlintInt(y)) Of course I might have missed optimal conversion for some `Integer` subtype. But then we can fix this by simply adding another `FlintInt` constructor in a single central place. Finally, the new type `FlintRat` plays a similar role for FLINT functions which also take a `QQFieldElem`. While `RationalUnion` is a counterpart to `IntegerUnion`.
- Loading branch information
Showing
4 changed files
with
92 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters