Skip to content

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 9 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions src/DynamicQuantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module DynamicQuantities
export Quantity, Dimensions, DimensionError, ustrip, dimension, valid
export ulength, umass, utime, ucurrent, utemperature, uluminosity, uamount

include("fixed_rational.jl")
include("types.jl")
include("utils.jl")
include("math.jl")
Expand Down
30 changes: 30 additions & 0 deletions src/fixed_rational.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
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
FixedRational{T,den}(x::Integer) where {T,den} = unsafe_fixed_rational(x * den, T, Val(den))
FixedRational{T,den}(x::Rational) where {T,den} = unsafe_fixed_rational(widemul(x.num, den) ÷ x.den, T, Val(den))

Base.:*(l::F, r::F) where {T,den,F<:FixedRational{T,den}} = unsafe_fixed_rational(widemul(l.num, r.num) ÷ den, T, Val(den))
Base.:+(l::F, r::F) where {T,den,F<:FixedRational{T,den}} = unsafe_fixed_rational(l.num + r.num, T, Val(den))
Base.:-(l::F, r::F) where {T,den,F<:FixedRational{T,den}} = unsafe_fixed_rational(l.num - r.num, T, Val(den))
Base.:-(x::F) where {T,den,F<:FixedRational{T,den}} = unsafe_fixed_rational(-x.num, T, Val(den))
Base.inv(x::F) where {T,den,F<:FixedRational{T,den}} = unsafe_fixed_rational(widemul(den, den) ÷ x.num, T, Val(den))

Base.isinteger(x::F) where {T,den,F<:FixedRational{T,den}} = iszero(x.num % den)
Base.convert(::Type{FixedRational{T,den}}, x::Integer) where {T,den} = unsafe_fixed_rational(x * den, T, Val(den))
Base.convert(::Type{FixedRational{T,den}}, x::Rational) where {T,den} = FixedRational{T,den}(x)
Base.convert(::Type{Rational}, x::FixedRational{T,den}) where {T,den} = Rational{T}(x.num, den)
Base.convert(::Type{AF}, x::FixedRational{T,den}) where {AF<:AbstractFloat,T,den} = convert(AF, x.num / den)
Copy link
Collaborator

Choose a reason for hiding this comment

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

x.num//den

Copy link
Member Author

Choose a reason for hiding this comment

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

It converts to an AbstractFloat, so x.num//den would be significantly slower as it would have to compute divgcd first

Base.round(::Type{T}, x::F) where {T,T2,den,F<:FixedRational{T2,den}} = convert(T, div(x.num, den, RoundNearest))
Base.promote(x::T, y::F) where {T,T2,den,F<:FixedRational{T2,den}} = promote(x, convert(Rational, y))
Base.promote(x::F, y::T) where {T,T2,den,F<:FixedRational{T2,den}} = promote(convert(Rational, x), y)
Base.show(io::IO, x::F) where {T,den,F<:FixedRational{T,den}} = show(io, convert(Rational, x))
Base.zero(::Type{F}) where {T,den,F<:FixedRational{T,den}} = unsafe_fixed_rational(0, T, Val(den))
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DEFAULT_DIM_TYPE = Rational{Int16}
const DEFAULT_DIM_TYPE = FixedRational{Int32, 2^4 * 3^2 * 5^2 * 7}
const DEFAULT_VALUE_TYPE = Float64

"""
Expand Down
3 changes: 2 additions & 1 deletion test/test_unitful.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DynamicQuantities
using DynamicQuantities: DEFAULT_DIM_TYPE, DEFAULT_VALUE_TYPE
import Unitful
import Unitful: @u_str
import Ratios: SimpleRatio
Expand All @@ -15,7 +16,7 @@ risapprox(x::Unitful.Quantity, y::Unitful.Quantity; kws...) =

factor_for_preferred_units = 1e-3

for T in [Float16, Float32, Float64], R in [Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
for T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
x = DynamicQuantities.Quantity(T(0.2*factor_for_preferred_units), R, length=1, amount=2, current=-1 // 2, luminosity=2 // 5)
x_unitful = T(0.2)u"m*mol^2*A^(-1//2)*cd^(2//5)"

Expand Down
2 changes: 1 addition & 1 deletion test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Test

@testset "Basic utilities" begin

for T in [Float16, Float32, Float64], R in [Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
for T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
x = Quantity(T(0.2), R, length=1, mass=2.5)

@test typeof(x).parameters[1] == T
Expand Down