1
- import Ratios: SimpleRatio
2
- import SaferIntegers: SafeInt
3
-
4
- const INT_TYPE = SafeInt
5
- const R = SimpleRatio{INT_TYPE}
6
- const ZERO = R (0 )
7
- const DIMENSION_NAMES = (:length , :mass , :time , :current , :temperature , :luminosity , :amount )
8
- const DIMENSION_SYNONYMS = (:𝐋 , :𝐌 , :𝐓 , :𝐈 , :𝚯 , :𝐉 , :𝐍 )
9
- const SYNONYM_MAPPING = NamedTuple (DIMENSION_NAMES .=> DIMENSION_SYNONYMS)
1
+ const DEFAULT_DIM_TYPE = Rational{Int16}
10
2
11
3
"""
12
4
Dimensions
@@ -17,15 +9,15 @@ example, the dimensions of velocity are `Dimensions(length=1, time=-1)`.
17
9
18
10
# Fields
19
11
20
- - `length::Rational{Int} `: length dimension (i.e., meters^(length))
21
- - `mass::Rational{Int} `: mass dimension (i.e., kg^(mass))
22
- - `time::Rational{Int} `: time dimension (i.e., s^(time))
23
- - `current::Rational{Int} `: current dimension (i.e., A^(current))
24
- - `temperature::Rational{Int} `: temperature dimension (i.e., K^(temperature))
25
- - `luminosity::Rational{Int} `: luminosity dimension (i.e., cd^(luminosity))
26
- - `amount::Rational{Int} `: amount dimension (i.e., mol^(amount))
12
+ - `length`: length dimension (i.e., meters^(length))
13
+ - `mass`: mass dimension (i.e., kg^(mass))
14
+ - `time`: time dimension (i.e., s^(time))
15
+ - `current`: current dimension (i.e., A^(current))
16
+ - `temperature`: temperature dimension (i.e., K^(temperature))
17
+ - `luminosity`: luminosity dimension (i.e., cd^(luminosity))
18
+ - `amount`: amount dimension (i.e., mol^(amount))
27
19
"""
28
- struct Dimensions
20
+ struct Dimensions{R <: Real }
29
21
length:: R
30
22
mass:: R
31
23
time:: R
@@ -34,19 +26,33 @@ struct Dimensions
34
26
luminosity:: R
35
27
amount:: R
36
28
37
- Dimensions (length:: R , mass:: R , time:: R , current:: R , temperature:: R , luminosity:: R , amount:: R ) =
38
- new (length, mass, time, current, temperature, luminosity, amount)
39
- Dimensions (; kws... ) = Dimensions (
40
- tryrationalize (R, get (kws, :length , ZERO)),
41
- tryrationalize (R, get (kws, :mass , ZERO)),
42
- tryrationalize (R, get (kws, :time , ZERO)),
43
- tryrationalize (R, get (kws, :current , ZERO)),
44
- tryrationalize (R, get (kws, :temperature , ZERO)),
45
- tryrationalize (R, get (kws, :luminosity , ZERO)),
46
- tryrationalize (R, get (kws, :amount , ZERO)),
29
+ function Dimensions (length:: _R ,
30
+ mass:: _R ,
31
+ time:: _R ,
32
+ current:: _R ,
33
+ temperature:: _R ,
34
+ luminosity:: _R ,
35
+ amount:: _R ) where {_R<: Real }
36
+ new {_R} (length, mass, time, current, temperature, luminosity, amount)
37
+ end
38
+ Dimensions (; kws... ) = Dimensions (DEFAULT_DIM_TYPE; kws... )
39
+ Dimensions (:: Type{_R} ; kws... ) where {_R} = Dimensions (
40
+ tryrationalize (_R, get (kws, :length , zero (_R))),
41
+ tryrationalize (_R, get (kws, :mass , zero (_R))),
42
+ tryrationalize (_R, get (kws, :time , zero (_R))),
43
+ tryrationalize (_R, get (kws, :current , zero (_R))),
44
+ tryrationalize (_R, get (kws, :temperature , zero (_R))),
45
+ tryrationalize (_R, get (kws, :luminosity , zero (_R))),
46
+ tryrationalize (_R, get (kws, :amount , zero (_R))),
47
47
)
48
+ Dimensions {_R} (; kws... ) where {_R} = Dimensions (_R; kws... )
49
+ Dimensions {_R} (args... ) where {_R} = Dimensions (Base. Fix1 (convert, _R).(args). .. )
48
50
end
49
51
52
+ const DIMENSION_NAMES = Base. fieldnames (Dimensions)
53
+ const DIMENSION_SYNONYMS = (:𝐋 , :𝐌 , :𝐓 , :𝐈 , :𝚯 , :𝐉 , :𝐍 )
54
+ const SYNONYM_MAPPING = NamedTuple (DIMENSION_NAMES .=> DIMENSION_SYNONYMS)
55
+
50
56
"""
51
57
Quantity{T}
52
58
@@ -67,13 +73,15 @@ including `*`, `+`, `-`, `/`, `^`, `sqrt`, and `cbrt`.
67
73
- `dimensions::Dimensions`: dimensions of the quantity
68
74
- `valid::Bool`: whether the quantity is valid or not
69
75
"""
70
- struct Quantity{T}
76
+ struct Quantity{T, R }
71
77
value:: T
72
- dimensions:: Dimensions
78
+ dimensions:: Dimensions{R}
73
79
valid:: Bool
74
80
75
- Quantity (x; kws... ) = new {typeof(x)} (x, Dimensions (; kws... ), true )
76
- Quantity (x, valid:: Bool ; kws... ) = new {typeof(x)} (x, Dimensions (; kws... ), valid)
77
- Quantity (x, d:: Dimensions ) = new {typeof(x)} (x, d, true )
78
- Quantity (x, d:: Dimensions , valid:: Bool ) = new {typeof(x)} (x, d, valid)
81
+ Quantity (x; kws... ) = new {typeof(x), DEFAULT_DIM_TYPE} (x, Dimensions (; kws... ), true )
82
+ Quantity (x, valid:: Bool ; kws... ) = new {typeof(x), DEFAULT_DIM_TYPE} (x, Dimensions (; kws... ), valid)
83
+ Quantity (x, :: Type{_R} , valid:: Bool ; kws... ) where {_R} = new {typeof(x), _R} (x, Dimensions (_R; kws... ), valid)
84
+ Quantity (x, :: Type{_R} ; kws... ) where {_R} = new {typeof(x), _R} (x, Dimensions (_R; kws... ), true )
85
+ Quantity (x, d:: Dimensions{_R} ) where {_R} = new {typeof(x), _R} (x, d, true )
86
+ Quantity (x, d:: Dimensions{_R} , valid:: Bool ) where {_R} = new {typeof(x), _R} (x, d, valid)
79
87
end
0 commit comments