Skip to content

Commit 2722057

Browse files
authored
Merge pull request #3 from SymbolicML/faster-rationals
Use Ratios.jl for faster rationals
2 parents c9c68aa + 0fd9cc0 commit 2722057

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ authors = ["MilesCranmer <[email protected]> and contributors"]
44
version = "0.1.0"
55

66
[deps]
7+
Ratios = "c84ed2f1-dad5-54f0-aa8e-dbefe2724439"
78
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
89

910
[weakdeps]
@@ -13,13 +14,14 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
1314
DynamicUnitsUnitfulExt = "Unitful"
1415

1516
[compat]
17+
Ratios = "0.4"
1618
Requires = "1"
1719
Unitful = "1"
1820
julia = "1.6"
1921

2022
[extras]
21-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2223
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
24+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2325
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2426

2527
[targets]

src/DynamicUnits.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module DynamicUnits
33
export Quantity, Dimensions, ustrip, dimension, valid
44
export ulength, umass, utime, ucurrent, utemperature, uluminosity, uamount
55

6+
import Ratios: SimpleRatio
7+
68
include("types.jl")
79
include("utils.jl")
810
include("math.jl")

src/math.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Base.:^(l::Quantity, r::Quantity) =
2323
let rr = tryrationalize(Int, r.value)
2424
Quantity(l.value^rr, l.dimensions^rr, l.valid && r.valid && iszero(r.dimensions))
2525
end
26-
Base.:^(l::Dimensions, r::Rational{Int}) = @map_dimensions(Base.Fix1(*, r), l)
26+
Base.:^(l::Dimensions, r::R) = @map_dimensions(Base.Fix1(*, r), l)
2727
Base.:^(l::Dimensions, r::Number) = l^tryrationalize(Int, r)
2828
Base.:^(l::Quantity, r::Number) =
2929
let rr = tryrationalize(Int, r)

src/types.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const R = Rational{Int}
1+
import Ratios: SimpleRatio
2+
3+
const R = SimpleRatio{Int}
24
const DIMENSION_NAMES = (:length, :mass, :time, :current, :temperature, :luminosity, :amount)
35
const DIMENSION_SYNONYMS = (:𝐋, :𝐌, :𝐓, :𝐈, :𝚯, :𝐉, :𝐍)
46
const SYNONYM_MAPPING = NamedTuple(DIMENSION_NAMES .=> DIMENSION_SYNONYMS)

src/utils.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ Base.show(io::IO, d::Dimensions) =
6262
end
6363
Base.show(io::IO, q::Quantity) = q.valid ? print(io, q.value, " ", q.dimensions) : print(io, "INVALID")
6464

65-
tryround(x::Rational{Int}) = isinteger(x) ? round(Int, x) : x
66-
pretty_print_exponent(io::IO, x::Rational{Int}) =
65+
string_rational(x::Rational) = isinteger(x) ? string(x.num) : string(x)
66+
string_rational(x::SimpleRatio) = string_rational(x.num // x.den)
67+
pretty_print_exponent(io::IO, x::R) =
6768
let
68-
print(io, " ", to_superscript(string(tryround(x))))
69+
print(io, " ", to_superscript(string_rational(x)))
6970
end
7071
const SUPERSCRIPT_MAPPING = ['', '¹', '²', '³', '', '', '', '', '', '']
7172
const INTCHARS = ['0' + i for i = 0:9]
@@ -75,9 +76,10 @@ to_superscript(s::AbstractString) = join(
7576
end
7677
)
7778

78-
tryrationalize(::Type{T}, x::Rational{T}) where {T<:Integer} = x
79-
tryrationalize(::Type{T}, x::T) where {T<:Integer} = Rational{T}(x)
80-
tryrationalize(::Type{T}, x) where {T<:Integer} = rationalize(T, x)
79+
tryrationalize(::Type{<:Integer}, x::Rational) = R(x)
80+
tryrationalize(::Type{<:Integer}, x::Integer) = R(x)
81+
tryrationalize(::Type{<:Integer}, x) = simple_ratio_rationalize(x)
82+
simple_ratio_rationalize(x) = isinteger(x) ? R(round(Int, x)) : R(rationalize(Int, x))
8183

8284
"""
8385
ustrip(q::Quantity)

test/unittests.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ using Test
55

66
x = Quantity(0.2, length=1, mass=2.5)
77

8-
@test ulength(x) === 1 // 1
9-
@test umass(x) === 5 // 2
8+
@test ulength(x) == 1 // 1
9+
@test umass(x) == 5 // 2
1010
@test valid(x)
11-
@test ustrip(x) === 0.2
11+
@test ustrip(x) == 0.2
1212
@test dimension(x) == Dimensions(length=1, mass=5 // 2)
1313
@test typeof(x).parameters[1] == Float64
1414

1515
y = x^2
1616

17-
@test ulength(y) === 2 // 1
18-
@test umass(y) === 5 // 1
17+
@test ulength(y) == 2 // 1
18+
@test umass(y) == 5 // 1
1919
@test ustrip(y) 0.04
2020
@test valid(y)
2121
@test typeof(y).parameters[1] == Float64
2222

2323
y = x + x
2424

25-
@test ulength(y) === 1 // 1
26-
@test umass(y) === 5 // 2
25+
@test ulength(y) == 1 // 1
26+
@test umass(y) == 5 // 2
2727
@test ustrip(y) 0.4
2828
@test valid(y)
2929

@@ -36,21 +36,21 @@ using Test
3636

3737
y = inv(x)
3838

39-
@test ulength(y) === -1 // 1
40-
@test umass(y) === -5 // 2
39+
@test ulength(y) == -1 // 1
40+
@test umass(y) == -5 // 2
4141
@test ustrip(y) 5
4242
@test valid(y)
4343

4444
y = x - x
4545

46-
@test iszero(x) === false
47-
@test iszero(y) === true
48-
@test iszero(y.dimensions) === false
46+
@test iszero(x) == false
47+
@test iszero(y) == true
48+
@test iszero(y.dimensions) == false
4949

5050
y = x / x
5151

52-
@test iszero(x.dimensions) === false
53-
@test iszero(y.dimensions) === true
52+
@test iszero(x.dimensions) == false
53+
@test iszero(y.dimensions) == true
5454

5555
y = Quantity(2 // 10, length=1, mass=5 // 2)
5656

@@ -71,15 +71,15 @@ using Test
7171

7272
y = x^2.1
7373

74-
@test ulength(y) === 1 * (21 // 10)
74+
@test ulength(y) == 1 * (21 // 10)
7575
@test umass(y) == (5 // 2) * (21 // 10)
7676
@test utime(y) == 0
7777
@test ucurrent(y) == 0
7878
@test utemperature(y) == 0
7979
@test uluminosity(y) == 0
8080
@test uamount(y) == 0
8181
@test ustrip(y) 0.2^2.1
82-
@test ustrip(y) === 0.2^(21 // 10)
82+
@test ustrip(y) == 0.2^(21 // 10)
8383

8484
x1 = Quantity(0.5)
8585
x2 = Quantity(10.0, length=1)

0 commit comments

Comments
 (0)