-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.jl
580 lines (534 loc) · 23.6 KB
/
main.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
@use "./utils.jl" get_param set_param seperate Magnitude exponents prefixs LogNumber
@use "github.com/jkroso/Prospects.jl" group mapcat
@use MacroTools: @capture
@use Dates
abstract type Unit <: Number end
abstract type Dimension <: Unit end
struct Exponent{dimension<:Unit, n} <: Unit
value::Number
end
abstract type AbstractCombination{dimensions<:Tuple{Vararg{Exponent}}} <: Unit end
abstract type DerivedUnit{magnitude, dimensions} <: AbstractCombination{dimensions} end
"""
Used to represent composite types like `kg*m^2/s^3`. The seperation of dimensions from units
allows it to also be used to represent dimensionless units like `cm/m` while also keeping
track of the magnitude of each dimension. Which is useful in units like `kW*hr`
"""
struct Combination{dimensions<:Tuple{Vararg{Exponent}}, units<:Tuple{Vararg{Exponent}}} <: AbstractCombination{dimensions}
value::Number
end
abstract type Time <: Dimension end
abstract type Length <: Dimension end
abstract type Mass <: Dimension end
abstract type Current <: Dimension end
abstract type Temperature <: Dimension end
abstract type Substance <: Dimension end
abstract type Luminosity <: Dimension end
abstract type Substance <: Dimension end
"Get the concrete units for a given dimension"
get_units(D::Type{<:Dimension}, c::Combination) = get_units(D, typeof(c))
get_units(D::Type{<:Dimension}, C::Type{<:Combination}) = begin
units = subunits(C)
map(simplify, units[findall(x->abstract_dimension(x) <: D, units)])
end
function short_name end
const Units = @__MODULE__()
macro defunit(type, short_name)
@capture type (name_ <: super_)|name_
if isnothing(super) super = :($Units.Dimension) end
T = esc(name)
if Meta.isexpr(short_name, :call, 3) && short_name.args[1] == :*
@assert Meta.isexpr(short_name.args[2], :hcat) || Meta.isexpr(short_name.args[2], :vect)
magnitudes = short_name.args[2].args
short_name = short_name.args[3]
else
magnitudes = Any[]
end
(abbrev, superscript) = match(r"([^⁽]+)(⁽[²³]+⁾)?", string(short_name)).captures
abbrev = String(abbrev)
superscript = isnothing(superscript) ? Int[] : Int[x-175 for x in Char[superscript...][2:end-1]]
quote
Base.@__doc__ struct $(esc(:($name{magnitude} <: $super))) value::Number end
$Units.short_name(::Type{<:$T}) = $abbrev
$Units.scaler(::Type{$T{m}}) where m = m
Base.promote_rule(::Type{$T{m1}}, ::Type{$T{m2}}) where {m1,m2} = $T{m1<m2 ? m1 : m2}
const $(esc(Symbol(abbrev))) = $T{Magnitude(0)}
$(map(superscript) do s
:(const $(esc(Symbol(abbrev, exponents[s]))) = $T{Magnitude(0)}^$(s-1))
end...)
$(map(magnitudes) do m
for (n, sym) in prefixs
sym != m && continue
return quote
const $(esc(Symbol(m, abbrev))) = $T{Magnitude($n)}
$(map(superscript) do s
:(const $(esc(Symbol(m, abbrev, exponents[s]))) = $T{Magnitude($n)}^$(s-1))
end...)
end
end
error("unknown prefix $m")
end...)
end
end
macro abbreviate(name, def)
quote
const $(esc(name)) = $(esc(def))
$Units.abbr(::Type{$(esc(name))}) = $(string(name))
end
end
parameters(D::DataType) = D.parameters
parameters(U::UnionAll) = parameters(U.body)
parameters(V::TypeVar) = parameters(V.ub)
ub(D::DataType) = D
ub(D::TypeVar) = D.ub
ub(D::UnionAll) = D.body
isabstract(::Union{TypeVar,UnionAll}) = true
isabstract(D::DataType) = isabstracttype(D) || (!isempty(D.parameters) && any(x->x isa TypeVar, D.parameters))
wrap(T, e) = isabstract(T) ? Exponent{D, e} where D<:ub(T) : Exponent{T,e}
Base.inv(::Type{Exponent{d,n}}) where {d,n} = wrap(d, -n)
Base.inv(E::Type{<:Exponent}) = wrap(get_param(E, 1), -get_param(E, 2))
Base.inv(::Type{D}) where D<:Dimension = wrap(D, -1)
Base.inv(C::Type{<:Combination}) = map_combo(inv, C)
Base.inv(u::Unit) = inv(u.value)inv(typeof(u))
Base.:^(D::Type{<:Unit}, e) = wrap(D, e)
Base.:^(::Type{Exponent{d, e1}}, e2) where {d,e1} = wrap(d, e1 * e2)
Base.:^(E::Type{<:Exponent}, e) = wrap(get_param(E, 1), get_param(E, 2) * e)
Base.:^(C::Type{<:Combination}, e) = map_combo(d->d^e, C)
tounionall(U::UnionAll) = U
tounionall(U::DataType) = U.name.wrapper
Base.:/(U::Type{<:Unit}, n::Real) = begin
D = tounionall(unwrap(U))
m1 = convert(LogNumber, scaler(U))
m2 = convert(LogNumber, n^(1//power(U)))
simplify(wrap(D{m1/m2}, power(U)))
end
Base.:*(U::Type{<:Unit}, n::Real) = begin
D = tounionall(unwrap(U))
m1 = convert(LogNumber, scaler(U))
m2 = convert(LogNumber, n^(1//power(U)))
simplify(wrap(D{m1*m2}, power(U)))
end
map_combo(f, C::Type{<:Combination}) = begin
dims, units = parameters(get_param(C, 1)), parameters(get_param(C, 2))
Combination{Tuple{map(f, dims)...}, Tuple{map(f, units)...}}
end
Base.promote_rule(::Type{Exponent{d1,e}}, ::Type{Exponent{d2,e}}) where {d1,d2,e} = wrap(promote_type(d1, d2), e)
Base.promote_rule(A::Type{<:Combination}, B::Type{<:Unit}) = simplify(promote_rule(A, to_combo(B)))
Base.promote_rule(A::Type{<:Exponent}, B::Type{<:Dimension}) = promote_rule(A, wrap(B, 1))
Base.promote_rule(::Type{A}, ::Type{B}) where {A<:Combination,B<:Combination} = begin
dims_a, dims_b = sorted_dims(A), sorted_dims(B)
@assert dims_a == dims_b "dimension mismatch"
Combination{Tuple{dims_a...}, Tuple{map(promote_type, sorted_subunits(A), sorted_subunits(B))...}}
end
sorted_dims(a) = sort!(collect(dimensions(a)), by=string ∘ abstract_dimension)
sorted_subunits(a) = sort!(collect(subunits(a)), by=string ∘ abstract_dimension)
abbr(_) = ""
abbr(m::Magnitude) = string(get(prefixs, m.value, ""))
scaler(U::Type{<:Exponent{d}}) where d = scaler(d)
scaler(U::Type{<:DerivedUnit{d}}) where d = d
scaler(U::Type{<:Unit}) = 1
prefix(T::Type{<:Dimension}) = abbr(scaler(T))
abbr(T::Type{<:Dimension}) = prefix(T) * short_name(T)
abbr(::Type{Exponent{d,n}}) where {d,n} = begin
n == 1 && return abbr(d)
n >= 0 && return string(abbr(d), exponents[Int(n)+1])
string(abbr(d), '⁻', exponents[Int(abs(n))+1])
end
abbr(T::Type{<:AbstractCombination}) = begin
negative, positive = group(u->power(u) < 0, subunits(T))
str = join(map(abbr, positive), '·')
isempty(negative) && return str
str * '/' * join(map(abbr ∘ inv, negative), '·')
end
abbr(::Type{D}) where {m,_,D<:DerivedUnit{m, _}} = abbr(m) * short_name(D)
Base.:-(n::Unit) = typeof(n)(-n.value)
Base.:*(n::Real, ::Type{T}) where T<:Unit = T(n)
Base.:/(n::Real, ::Type{T}) where T<:Unit = inv(T)(n)
Base.:^(u::Unit, n::Integer) = (typeof(u)^n)(u.value^n)
Base.convert(::Type{U}, n::Real) where U<:Unit = U(n)
Base.convert(::Type{N}, u::U) where {N<:Real,U<:Unit} = convert(N, u.value * basefactor(U))
Base.convert(::Type{U}, n::Unit) where U<:Unit = begin
ad = map(dimension, sorted_dims(typeof(n)))
bd = map(dimension, sorted_dims(U))
if ad == bd
U(n.value * conversion_factor(typeof(n), U))
elseif map(inv, ad) == bd
n = inv(n)
U(n.value * conversion_factor(typeof(n), U))
else
error("$(abbr(U)) not dimensionally compatable with $(abbr(typeof(n)))")
end
end
Base.convert(::Type{Combination}, d::D) where D<:Unit = to_combo(D)(d.value)
Base.show(io::IO, t::Unit) = (write(io, seperate(t.value), abbr(typeof(t))); nothing)
Base.isinteger(u::Unit) = isinteger(u.value)
Base.abs(u::Unit) = abs(u.value)typeof(u)
Base.isapprox(a::Unit, b::Unit) = isapprox(map(x->x.value, promote(a, b))...)
Base.div(a::Unit, b::Unit) = Int(floor(a/b))
Base.div(a::T, b::Real) where T<:Unit = div(a, convert(T, b))
Base.rem(a::Unit, b::Unit) = a-(floor(a/b)*b)
Base.rem(a::T, b::Real) where T<:Unit = rem(a, convert(T, b))
Base.floor(a::Unit) = typeof(a)(floor(a.value))
Base.ceil(a::Unit) = typeof(a)(ceil(a.value))
Base.round(A::Type{<:Unit}, a::Unit) = round(convert(A, a))
Base.round(u::Unit; kwargs...) = typeof(u)(round(u.value; kwargs...))
Base.sqrt(s::Exponent{T,d}) where {d,T} = begin
n = Int(d/2)
n == 1 ? T(sqrt(s.value)) : wrap(T, n)(sqrt(s.value))
end
for λ in (:*, :/, :+, :-)
@eval Base.$λ(n::Real, u::T) where T<:Unit = T($λ(n, u.value))
@eval Base.$λ(u::T, n::Real) where T<:Unit = T($λ(u.value, n))
end
for λ in (:+, :-)
@eval Base.$λ(a::T, b::T) where T<:Unit = T($λ(a.value, b.value))
@eval Base.$λ(a::Unit, b::Unit) = $λ(promote(a, b)...)
end
for λ in (:<, :>, :(==), :isless)
@eval Base.$λ(a::Real,b::Unit) = $λ(a, convert(Real, b))
@eval Base.$λ(a::Unit,b::Real) = $λ(convert(Real, a), b)
@eval Base.$λ(a::T,b::T) where T<:Unit = $λ(a.value, b.value)
@eval Base.$λ(a::Unit,b::Unit) = begin
@assert dimension(typeof(a)) == dimension(typeof(b))
$λ(promote(a, b)...)
end
end
Base.convert(::Type{T}, n::D) where {D<:Dimension, T<:Dimension} = n isa T ? n : T(n.value * conversion_factor(D, T))
basefactor(D::Type{<:Dimension}) = scaler(D)
basefactor(D::Type{<:DerivedUnit}) = scaler(D)
basefactor(E::Type{Exponent{d,e}}) where {d,e} = basefactor(d)^e
basefactor(C::Type{<:Combination}) = begin
units = parameters(get_param(C, 2))
mapreduce(basefactor, *, units, init=1)
end
conversion_factor(A::Type{<:Unit}, B::Type{<:Unit}) = conversion_factor(to_combo(A), to_combo(B))
conversion_factor(::Type{A}, ::Type{B}) where {A<:Dimension,B<:Dimension} = basefactor(A)/basefactor(B)
conversion_factor(::Type{Exponent{da,ea}}, ::Type{Exponent{db,eb}}) where {da,db,ea,eb} = error("dimension mismatch")
conversion_factor(::Type{Exponent{da,e}}, ::Type{Exponent{db,e}}) where {da,db,e} = (basefactor(da)/basefactor(db))^e
conversion_factor(A::Type{<:AbstractCombination{DA}}, B::Type{<:AbstractCombination{DB}}) where {DA,DB} = begin
(units_a, scaler_a), (units_b, scaler_b) = simple_units(A), simple_units(B)
sort!(units_a, by=string ∘ abstract_dimension)
sort!(units_b, by=string ∘ abstract_dimension)
mapreduce(conversion_factor, *, units_a, units_b, init=scaler_a/scaler_b)
end
abstract type NullDimension <: Dimension end
to_combo(U::Type{<:NullDimension}) = Combination{Tuple{},Tuple{wrap(U, 1)}}
dimensions(::Type{<:NullDimension}) = Any[]
dimension(::Type{<:NullDimension}) = wrap(NullDimension, 1)
abstract_dimension(::Type{<:NullDimension}) = NullDimension
"unpack derived units, dedupe dimensions, and keep track of the resulting scale difference"
simple_units(x) = [Exponent{x,1}], 1
simple_units(C::Type{<:AbstractCombination}) = begin
units, scale = flatten_units(C)
dims = map(abstract_dimension, units)
nulls = units[findall(==(NullDimension), dims)]
scale *= mapreduce(scaler, *, nulls, init=1)
output = map(unique!(filter(!=(NullDimension), dims))) do d
like_units = units[findall(==(d), dims)]
T = promote_type(map(unwrap, like_units)...)
out = Exponent{T, mapreduce(power, +, like_units)}
out, mapreduce(u->conversion_factor(u, Exponent{T,power(u)}), *, like_units)
end
filter!(!ispointless, map(first, output)), mapreduce(last, *, output) * scale
end
typebelow(child, stop) = begin
while supertype(child).name.wrapper != stop; child = supertype(child) end
child
end
abstract_dimension(E::Type{<:Exponent}) = abstract_dimension(unwrap(E))
abstract_dimension(D::Type{<:Dimension}) = typebelow(D, Dimension)
abstract_dimension(U::Type{<:AbstractCombination}) = typebelow(U, AbstractCombination)
abstract_dimension(U::Type{<:DerivedUnit}) = U.name.wrapper
dimension(D::Type{<:Dimension}) = wrap(abstract_dimension(D), 1)
dimension(D::Type{<:Exponent}) = wrap(abstract_dimension(unwrap(D)),power(D))
dimension(C::Type{<:Combination}) = begin
dims = map(dimension, get_param(C, 1).parameters)
sort!(dims, by=string ∘ abstract_dimension)
AbstractCombination{dimensions} where dimensions<:Tuple{dims...}
end
unwrap(E::Type{<:Exponent}) = ub(get_param(E, 1))
unwrap(D::Type{<:Dimension}) = D
unwrap(D::Type{<:DerivedUnit}) = D
to_combo(D::Type{<:AbstractCombination}) = D
to_combo(D::Type{<:DerivedUnit}) = Combination{Tuple{dimensions(D)...}, Tuple{D^1}}
to_combo(D::Type{<:Dimension}) = to_combo(D^1)
to_combo(D::Type{<:Exponent}) = begin
d, e = get_param(D, 1), get_param(D, 2)
if isabstract(d)
AbstractCombination{Dimensions} where Dimensions<:Tuple{D}
else
Combination{Tuple{dimension(D)}, Tuple{D}}
end
end
subunits(::Type{Exponent{D,n}}) where {D<:DerivedUnit,n} = DataType[wrap(unwrap(x), power(x) * n) for x in subunits(D)]
subunits(D::Type{<:Exponent}) = DataType[D]
subunits(C::Type{<:Combination}) = parameters(parameters(C)[2])
subunits(D::Type{<:AbstractCombination}) = parameters(parameters(D)[1])
subunits(D::Type{<:DerivedUnit{m,units}}) where {m,units} = parameters(units)
flatten_units(C::Type{<:AbstractCombination}) = accum_units(map(flatten_units, parameters(get_param(C, 1))), 1)
flatten_units(::Type{<:Combination{d,u}}) where {d,u} = accum_units(map(flatten_units, parameters(u)), 1)
flatten_units(::Type{<:DerivedUnit{m,u}}) where {m,u} = accum_units(map(flatten_units, parameters(u)), m)
flatten_units(::Type{<:Exponent{d,e}}) where {d,e} = begin
units, scale = flatten_units(d)
map(u->wrap(unwrap(u),power(u)*e), units), scale
end
flatten_units(D::Type{<:Dimension}) = Any[D], 1
accum_units(results, scale) = begin
reduce(results, init=(AnyType[],scale)) do out, (units, scale)
push!(out[1], units...), out[2] * scale
end
end
const AnyType = Union{UnionAll,DataType}
dimensions(D::Type{<:Dimension}) = AnyType[wrap(D, 1)]
dimensions(E::Type{<:Exponent{D,e}}) where {D<:Unit,e} = map(d->wrap(unwrap(d), power(d) * e), dimensions(D))
dimensions(C::Type{<:Combination}) = parameters(parameters(C)[1])
dimensions(C::Type{<:DerivedUnit{m,units}}) where {m,units} = map(dimension, units.parameters)
ispointless(d) = power(d) == 0
power(E::Type{<:Exponent}) = get_param(E, 2)
power(::Type{<:Unit}) = 1
"Reduces a complex unit to a simpler but equivelent one"
simplify(T::Type{<:Unit}) = T
simplify(E::Type{Exponent{d,n}}) where {d,n} = n == 1 ? d : E
simplify(C::Type{<:Combination}) = begin
dims = Any[x for x in get_param(C, 1).parameters if !ispointless(x)]
units = Any[x for x in get_param(C, 2).parameters if !ispointless(x)]
isempty(units) && return Real
length(units) == 1 && return simplify(units[1])
Combination{Tuple{dims...}, Tuple{units...}}
end
"deduplicate units in a combination while also keeping track of the scale difference between input and output types"
prune(T::Type{<:Unit}) = T, 1
prune(::Type{Combination{Ds, units}}) where {Ds, units} = begin
dims = map(abstract_dimension, units.parameters)
isempty(dims) && return Combination{Tuple{}, Tuple{}}, 1
output = map(unique(dims)) do d
like_units = units.parameters[findall(==(d), dims)]
T = promote_type(map(unwrap, like_units)...)
out = Exponent{T, mapreduce(power, +, like_units)}
out, mapreduce(u->conversion_factor(u, Exponent{T,power(u)}), *, like_units)
end
Combination{Ds, Tuple{filter!(!ispointless, map(first, output))...}}, mapreduce(last, *, output)
end
"Takes a Unit and a value and produces an instance of the simplest version of that type"
pruned(T, v) = begin
T, scaler = prune(T)
simplify(T)(v * scaler)
end
unpacked(c::Combination) = begin
units, scale = simple_units(typeof(c))
simplify(Combination{Tuple{map(dimension, units)...}, Tuple{units...}})(c.value * scale)
end
for λ in (:*, :/)
@eval Base.$λ(a::A, ::Type{B}) where {A<:Unit,B<:Unit} = pruned($λ(A,B), a.value)
@eval Base.$λ(::Type{A}, b::B) where {A<:Unit,B<:Unit} = pruned($λ(A,B), b.value)
@eval Base.$λ(::Type{A}, ::Type{B}) where {A<:Unit,B<:Unit} = $λ(to_combo(A), to_combo(B))
@eval Base.$λ(A::Type{<:DerivedUnit}, B::Type{<:DerivedUnit}) = $λ(to_combo(A), to_combo(B))
@eval Base.$λ(A::Type{<:DerivedUnit}, B::Type{<:AbstractCombination}) = $λ(to_combo(A), B)
@eval Base.$λ(A::Type{<:AbstractCombination}, B::Type{<:DerivedUnit}) = $λ(A, to_combo(B))
@eval Base.$λ(A::Type{<:AbstractCombination}, B::Type{<:AbstractCombination}) = begin
dims_a, dims_b = parameters(get_param(A, 1)), parameters(get_param(B, 1))
Das, Dbs = map(abstract_dimension, dims_a), map(abstract_dimension, dims_b)
Ds = map(union(Das, Dbs)) do D
ia, ib = findfirst(==(D), Das), findfirst(==(D), Dbs)
isnothing(ia) && return $(λ == :/ ? inv : identity)(dims_b[ib])
isnothing(ib) && return dims_a[ia]
wrap(D, $(λ == :* ? :+ : :-)(power(dims_a[ia]), power(dims_b[ib])))
end
AbstractCombination{Dimensions} where Dimensions<:Tuple{filter!(!ispointless, Ds)...}
end
@eval Base.$λ(A::Type{<:Combination}, B::Type{<:Combination}) = begin
units_a, units_b = subunits(A), subunits(B)
naked_a, naked_b = map(unwrap, units_a), map(unwrap, units_b)
units = filter!(!ispointless, map(union(naked_a, naked_b)) do u
ia,ib = findfirst(==(u), naked_a), findfirst(==(u), naked_b)
isnothing(ia) && return $(λ == :/ ? inv : identity)(units_b[ib])
isnothing(ib) && return units_a[ia]
wrap(u, $(λ == :* ? :+ : :-)(power(units_a[ia]), power(units_b[ib])))
end)
dims = mapcat(dimensions, units)
wrapped = map(unique!(map(abstract_dimension, dims))) do AD
e = sum((power(u) for u in dims if abstract_dimension(u) == AD))
wrap(AD, e)
end
combine(filter!(!ispointless, wrapped), units)
end
@eval Base.$λ(a::Unit, b::Unit) = $λ(convert(Combination, a), convert(Combination, b))
@eval Base.$λ(a::Unit, b::Combination) = $λ(convert(Combination, a), b)
@eval Base.$λ(a::Combination, b::Unit) = $λ(a, convert(Combination, b))
@eval Base.$λ(a::A, b::B) where {A<:Combination, B<:Combination} = begin
(UA, scaler_a), (UB, scaler_b) = prune(A), prune(B)
pruned($λ(UA, UB), $λ(a.value * scaler_a, b.value * scaler_b))
end
end
combine(dims, units) = begin
length(units) == 1 && return simplify(units[1])
Combination{Tuple{dims...}, Tuple{units...}}
end
const Energy = Mass * Length^2 / Time^2
const Power = Energy/Time
const Area = Length^2
const Volume = Length^3
const Density = Mass/Volume
const Force = Mass*Length/Time^2
const Pressure = Force/Area
const Speed = Length/Time
const Acceleration = Speed/Time
const Jerk = Acceleration/Time
const Snap = Jerk/Time
const Crackle = Snap/Time
const Pop = Crackle/Time
@defunit Meter <: Length [μ c m k]m⁽²³⁾
@abbreviate litre m³/1e3
@abbreviate ml litre/1e3
@abbreviate μl litre/1e6
@abbreviate hectare Area{Meter{Magnitude(2)}}
@defunit Gram <: Mass [μ m k]g
@abbreviate ton Gram*1e6
@defunit Mole <: Substance n
const Nₐ = 6.02214076*10^23/n
@defunit Ampere <: Current [m]A
@defunit Lumen <: Luminosity lm
@abbreviate lx lm/m²
@defunit Second <: Time [n m]s
abbr(S::Type{Second{m}}) where m = begin
m isa Magnitude && return abbr(m) * "s"
error("unknown magnitude $m")
end
scaler(::Type{Second{m}}) where m = exponentiable(m)
exponentiable(m::Magnitude) = m
exponentiable(m::Integer) = m//1
exponentiable(m) = m
@abbreviate minute Second{60}
@abbreviate hr Second{3600}
@abbreviate day Second{convert(Int, 24scaler(hr))}
@abbreviate week Second{convert(Int, 7scaler(day))}
@abbreviate yr Second{convert(Int, 365scaler(day))}
@abbreviate month Second{convert(Int, scaler(yr)/12)}
Base.sleep(duration::Second) = sleep(convert(Real, duration))
@defunit Kelvin <: Temperature K
@defunit Celsius <: Temperature °C
@defunit Fahrenheit <: Temperature °F
basefactor(::Type{Fahrenheit{m}}) where m = 5//9 * m
baseoffset(::Type{Kelvin{m}}) where m = 0//1
baseoffset(::Type{Fahrenheit{m}}) where m = rationalize(459.67)
baseoffset(::Type{Celsius{m}}) where m = rationalize(273.15)
Base.promote_rule(::Type{<:Temperature}, ::Type{<:Temperature}) = Kelvin
Base.convert(K::Type{<:Kelvin}, t::T) where T<:Union{Celsius,Fahrenheit} =
K((t.value + baseoffset(T)) * basefactor(T))
abstract type Data <: Dimension end
@defunit Byte <: Data [k M G T]b
@defunit Bit <: Data [k M G T]bit
Base.convert(B::Type{Bit{m}}, b::Byte) where m = B(8convert(Real, b)/basefactor(B))
Base.convert(B::Type{Byte{m}}, b::Bit) where m = B((convert(Real, b)/8)/basefactor(B))
macro deriveunit(name, units, abbrev)
N = esc(name)
if Meta.isexpr(abbrev, :call)
short = abbrev.args[3]
magnitudes = abbrev.args[2].args
else
short = abbrev
magnitudes = []
end
quote
subunits, scale = simple_units($(esc(units)))
@assert scale == 1 "derived units should be defined with no scale"
Base.@__doc__ struct $N{magnitude} <: DerivedUnit{magnitude, Tuple{subunits...}}
value::Number
end
$Units.short_name(::Type{$N{m}}) where m = $(string(short))
$Units.scaler(::Type{$N{m}}) where m = m
Base.promote_rule(::Type{$N{m1}}, ::Type{$N{m2}}) where {m1,m2} = $N{m1<m2 ? m1 : m2}
const $(esc(short)) = $N{Magnitude(0)}
$(map(magnitudes) do m
for (n, sym) in prefixs
sym == m && return :(const $(esc(Symbol(m, short))) = $N{Magnitude($n)})
end
error("unknown prefix $m")
end...)
end
end
macro scaledunit(name, unit)
type_name = string(name, "Unit")
N = esc(Symbol(type_name))
x = eval(__module__, unit)
subunits, scale = simple_units(typeof(x))
scale *= x.value
quote
Base.@__doc__ struct $N{scale} <: DerivedUnit{scale, Tuple{$(subunits...)}} value::Number end
$Units.short_name(::Type{$N{s}}) where s = $(string(name))
$Units.scaler(::Type{$N{s}}) where s = s
const $(esc(name)) = $N{$scale}
end
end
@deriveunit Joule kg*m²/s^2 [k M]J
@deriveunit Watt kg*m²/s^3 [m k M]W
@abbreviate Wh W*hr
@abbreviate kWh kW*hr
@deriveunit Newton kg*m/s^2 [k]N
@deriveunit Pascal N/m² [k M]Pa
const gravity = 9.80665m/s^2 # http://physics.nist.gov/cgi-bin/cuu/Value?gn
@scaledunit kgf 1kg*gravity
@abbreviate tonf kgf*1e3
@abbreviate bar Pascal*1e5
@abbreviate mbar bar/1e3
@deriveunit Coulomb A*s C
@deriveunit Volt J/C V
@abbreviate kV Volt*1e3
@deriveunit Ohm V/A Ω
@abbreviate Hz inv(s)
@abbreviate kHz inv(ms)
@abbreviate MHz inv(ns)
struct ScalingUnit{magnitude} <: NullDimension
value::Number
end
scaler(::Type{ScalingUnit{m}}) where m = m
scaler(s::ScalingUnit{m}) where m = s.value*m
const Percent = ScalingUnit/1e2
abbr(::Type{Percent}) = "%"
const Permille = ScalingUnit/1e3
abbr(::Type{Permille}) = "‰"
@abbreviate ppm ScalingUnit/1e6
@abbreviate ppb ScalingUnit/1e9
Base.:-(a::Real, b::ScalingUnit) = a - a*scaler(b)
Base.:-(a::Unit, b::ScalingUnit) = a - a*scaler(b)
Base.:+(a::Real, b::ScalingUnit) = a + a*scaler(b)
Base.:+(a::Unit, b::ScalingUnit) = a + a*scaler(b)
abstract type Angle <: NullDimension end
baseunit(::Type{Angle}) = Radian
struct Degree <: Angle value::Number end
@abbreviate ° Degree
struct Radian <: Angle value::Number end
@abbreviate rad Radian
basefactor(::Type{Degree}) = π/180
Base.promote_rule(::Type{<:Angle}, ::Type{<:Angle}) = Radian
Base.convert(::Type{Radian}, d::Degree) = Radian(d.value * basefactor(Degree))
Base.convert(::Type{Degree}, r::Radian) = Degree(r.value / basefactor(Degree))
for λ in (:sin,:cos,:tan)
@eval Base.$λ(n::Angle) = $λ(convert(Radian, n))
@eval Base.$λ(n::Radian) = $λ(n.value)
end
scale(x::Unit) = begin
T = typeof(x)
trailing_zeros = findfirst(!=(0), digits(round(Int, x.value); base=10)) - 1
m = Magnitude(trailing_zeros ÷ 3 * 3)
(T*m)(x.value/m)
end
abstract type TimeOfDay{name} end
const am = TimeOfDay{:am}
const pm = TimeOfDay{:pm}
Base.:*(t::Integer, ::Type{TimeOfDay{:am}}) = t>12 ? Dates.Time(0,t) : Dates.Time(t)
Base.:*(t::Integer, ::Type{TimeOfDay{:pm}}) = Dates.Time(t+12)
Base.:*(t::UnitRange, ::Type{TimeOfDay{:am}}) = Dates.Time(t.start, t.stop)
Base.:*(t::UnitRange, ::Type{TimeOfDay{:pm}}) = Dates.Time(t.start+12, t.stop)
Base.:(:)(t::Int, time::Dates.Time) = begin
m = Dates.minute(time)
Dates.Time(t, m == 0 ? Dates.hour(time) : m)
end
Base.show(io::IO, ::MIME"text/plain", ns::Dates.Nanosecond) = show(io, Dates.canonicalize(ns))
Base.show(io::IO, ::MIME"text/plain", c::Dates.CompoundPeriod) = show(io, MIME("text/plain"), convert(Dates.Nanosecond, c))
Base.promote_rule(T::Type{<:Dates.Period}, ::Type{<:Time}) = T
Base.convert(T::Type{<:Dates.Period}, t::Time) = convert(T, Dates.Second(convert(Float64, t)))
Base.:-(a::Dates.Period, b::Time) = -(promote(a,b)...)
Base.:+(a::Dates.Period, b::Time) = +(promote(a,b)...)
Base.:-(a::Time, b::Dates.Period) = -(promote(a,b)...)
Base.:+(a::Time, b::Dates.Period) = +(promote(a,b)...)