-
Notifications
You must be signed in to change notification settings - Fork 25
Create FixedRational
type for faster dimensions
#21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d29dfaf
Create `FixedRational` type for faster dimensions
MilesCranmer 76d0b8d
Faster conversions
MilesCranmer 40bd428
Implement faster version of `round`
MilesCranmer 7759cc0
Speed up convert(::Float)
MilesCranmer 1211ca5
Speed up round
MilesCranmer be38918
Speed up rationalize for `FixedRational`
MilesCranmer 1e5ceb2
Avoid promoting to Rational
MilesCranmer 612115b
Fix == on FixedRational
MilesCranmer 1b8602e
Ensure `den` of FixedRational{T,den} is type T
MilesCranmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
FixedRational{T,den} | ||
|
||
A rational number with a fixed denominator. Significantly | ||
faster than `Rational{T}`, as it never needs to compute | ||
the `gcd` apart from when printing. | ||
""" | ||
struct FixedRational{T<:Integer,den} <: Real | ||
num::T | ||
global unsafe_fixed_rational(num::Integer, ::Type{T}, ::Val{den}) where {T,den} = new{T,den}(num) | ||
end | ||
|
||
""" | ||
denom(F::FixedRational) | ||
|
||
Since `den` can be a different type than `T`, this function | ||
is used to get the denominator as a `T`. | ||
""" | ||
denom(::Type{F}) where {T,den,F<:FixedRational{T,den}} = convert(T, den) | ||
|
||
# But, for Val(den), we need to use the same type as at init. | ||
# Otherwise, we would have type instability. | ||
val_denom(::Type{F}) where {T,den,F<:FixedRational{T,den}} = Val(den) | ||
|
||
Base.eltype(::Type{F}) where {T,den,F<:FixedRational{T,den}} = T | ||
|
||
(::Type{F})(x::Integer) where {F<:FixedRational} = unsafe_fixed_rational(x * denom(F), eltype(F), val_denom(F)) | ||
(::Type{F})(x::Rational) where {F<:FixedRational} = unsafe_fixed_rational(widemul(x.num, denom(F)) ÷ x.den, eltype(F), val_denom(F)) | ||
|
||
Base.:*(l::F, r::F) where {F<:FixedRational} = unsafe_fixed_rational(widemul(l.num, r.num) ÷ denom(F), eltype(F), val_denom(F)) | ||
Base.:+(l::F, r::F) where {F<:FixedRational} = unsafe_fixed_rational(l.num + r.num, eltype(F), val_denom(F)) | ||
Base.:-(l::F, r::F) where {F<:FixedRational} = unsafe_fixed_rational(l.num - r.num, eltype(F), val_denom(F)) | ||
Base.:-(x::F) where {F<:FixedRational} = unsafe_fixed_rational(-x.num, eltype(F), val_denom(F)) | ||
Base.inv(x::F) where {F<:FixedRational} = unsafe_fixed_rational(widemul(denom(F), denom(F)) ÷ x.num, eltype(F), val_denom(F)) | ||
|
||
Base.:(==)(x::F, y::F) where {F<:FixedRational} = x.num == y.num | ||
Base.iszero(x::FixedRational) = iszero(x.num) | ||
Base.isinteger(x::F) where {F<:FixedRational} = iszero(x.num % denom(F)) | ||
Base.convert(::Type{F}, x::Integer) where {F<:FixedRational} = unsafe_fixed_rational(x * denom(F), eltype(F), val_denom(F)) | ||
Base.convert(::Type{F}, x::Rational) where {F<:FixedRational} = F(x) | ||
Base.convert(::Type{Rational}, x::F) where {F<:FixedRational} = Rational{eltype(F)}(x.num, denom(F)) | ||
Base.convert(::Type{AF}, x::F) where {AF<:AbstractFloat,F<:FixedRational} = convert(AF, x.num) / convert(AF, denom(F)) | ||
Base.round(::Type{T}, x::F) where {T,F<:FixedRational} = div(convert(T, x.num), convert(T, denom(F)), RoundNearest) | ||
Base.promote(x, y::F) where {F<:FixedRational} = promote(x, convert(Rational, y)) | ||
Base.promote(x::F, y) where {F<:FixedRational} = promote(convert(Rational, x), y) | ||
Base.show(io::IO, x::F) where {F<:FixedRational} = show(io, convert(Rational, x)) | ||
Base.zero(::Type{F}) where {F<:FixedRational} = unsafe_fixed_rational(0, eltype(F), val_denom(F)) | ||
|
||
tryrationalize(::Type{F}, x::F) where {F<:FixedRational} = x | ||
tryrationalize(::Type{F}, x::Union{Rational,Integer}) where {F<:FixedRational} = convert(F, x) | ||
tryrationalize(::Type{F}, x) where {F<:FixedRational} = unsafe_fixed_rational(round(eltype(F), x * denom(F)), eltype(F), val_denom(F)) |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.