@@ -60,6 +60,121 @@ Base.:+(a::Measure,b::Measurement) = cache(measure(a)-b)
60
60
Base.:- (a:: Number ,b:: Measure ) = cache (a- measure (b))
61
61
Base.:- (a:: Measure ,b:: Number ) = cache (measure (a)- b)
62
62
63
+ function round_extra (x)
64
+ l = length (string (x))
65
+ lp = length (string (prevfloat (x)))
66
+ ln = length (string (nextfloat (x)))
67
+ if ln < l && ln < lp
68
+ nextfloat (x)
69
+ elseif lp < l && lp < ln
70
+ prevfloat (x)
71
+ else
72
+ x
73
+ end
74
+ end
75
+
76
+ function showgroup (io:: IO ,x:: Group{:Measures,T,S,N} where S,u= basistext (x),c= ' 𝟙' ) where {T,N}
77
+ # back = T<:AbstractFloat && x.v[N]<0
78
+ # !back && printexpo(io, 10, x.v[N])
79
+ FieldAlgebra. printdims (io,x,u)
80
+ iz = iszero (FieldAlgebra. norm (x. v))
81
+ xc = coef (x)
82
+ iz && (isone (xc)|| abs (measure (xc))< 1 ) && print (io, c)
83
+ # back && printexpo(io, 10, last(x.v))
84
+ if ! isone (xc)
85
+ if float (abs (measure (xc)))< 1 && ! FieldAlgebra. isgroup (xc)
86
+ print (io, ' /' )
87
+ print_special (io, makeint (inv (xc)))
88
+ else
89
+ ! iz && print (io, ' ⋅' )
90
+ if FieldAlgebra. isgroup (xc)
91
+ print (io, ' (' )
92
+ print_special (io, makeint (xc))
93
+ print (io, ' )' )
94
+ else
95
+ print_special (io, makeint (xc))
96
+ end
97
+ end
98
+ end
99
+ print (io, " = " )
100
+ print_special (io, product (x))
101
+ end
102
+
103
+ import FieldAlgebra: special_print, print_special
104
+ function special_print (io:: IO , M:: Measurement , error_digits:: Int = get (io,:error_digits ,2 ))
105
+ isinf (M) && (return print (io," \\ infty " ))
106
+ err_digits = - Base. hidigit (M. err, 10 ) + error_digits
107
+ digits = if isfinite (M. val)
108
+ max (- Base. hidigit (M. val, 10 ) + 2 , err_digits)
109
+ else
110
+ err_digits
111
+ end
112
+ val = if iszero (M. err) || ! isfinite (M. err)
113
+ M. val
114
+ else
115
+ round_extra (round (M. val, digits = digits))
116
+ end
117
+ err = round_extra (round (M. err, sigdigits= error_digits))
118
+ sval = string (val)
119
+ if ' e' ∈ sval
120
+ serr = string (err)
121
+ ' e' ∈ serr && (serr = match (r" (\d +.\d +)[e](-?\d +)" ,serr). captures[1 ])
122
+ m = match (r" (\d +.\d +)[e](-?\d +)" ,string (val)). captures
123
+ ms = replace (serr,' .' => " " )
124
+ zs = digits+ 1 + Base. hidigit (M. val, 10 )+ (M. val< 0 )- length (m[1 ])
125
+ z = join (string .(zeros (Int,zs)))
126
+ print (io," $(m[1 ])$z ($(ms[1 ]≠ ' 0' ? ms[1 : error_digits] : ms[end - error_digits+ 1 ]≠ ' 0' ? ms[end - error_digits+ 1 : end ] : join (string (ms[end ]," 0" ))) ) \\ times 10^{$(m[2 ]) }" )
127
+ else
128
+ mz = match (r" 0\. 0*" ,sval)
129
+ zs = digits+ 1 + Base. hidigit (M. val, 10 )+ (M. val< 0 )- length (sval)+ (isnothing (mz) ? 0 : length (mz. match)- 1 )
130
+ if zs< 0 && sval[end ]== ' 0' && sval[end - 1 ]== ' .'
131
+ print (io, val, " (\\ pm " )
132
+ special_print (io,err)
133
+ print (io, ' )' )
134
+ else
135
+ z,ms = join (string .(zeros (Int,zs))),replace (string (err),' .' => " " )
136
+ print (io," $sval$z ($(ms[1 ]≠ ' 0' ? ms[1 : error_digits] : ms[end - error_digits+ 1 ]≠ ' 0' ? ms[end - error_digits+ 1 : end ] : join (string (ms[end ]," 0" ))) )" )
137
+ end
138
+ end
139
+ end
140
+ function print_special (io:: IO , M:: Measurement , error_digits:: Int = get (io,:error_digits ,2 ))
141
+ isinf (M) && (return print (io," Inf" ))
142
+ err_digits = - Base. hidigit (M. err, 10 ) + error_digits
143
+ digits = if isfinite (M. val)
144
+ max (- Base. hidigit (M. val, 10 ) + 2 , err_digits)
145
+ else
146
+ err_digits
147
+ end
148
+ val = if iszero (M. err) || ! isfinite (M. err)
149
+ M. val
150
+ else
151
+ round_extra (round (M. val, digits = digits))
152
+ end
153
+ err = round_extra (round (M. err, sigdigits= error_digits))
154
+ sval = string (val)
155
+ if ' e' ∈ sval
156
+ serr = string (err)
157
+ ' e' ∈ serr && (serr = match (r" (\d +.\d +)[e](-?\d +)" ,serr). captures[1 ])
158
+ m = match (r" (\d +.\d +)[e](-?\d +)" ,string (val)). captures
159
+ ms = replace (serr,' .' => " " )
160
+ zs = digits+ 1 + Base. hidigit (M. val, 10 )+ (M. val< 0 )- length (m[1 ])
161
+ z = join (string .(zeros (Int,zs)))
162
+ print (io," $(m[1 ])$z ($(ms[1 ]≠ ' 0' ? ms[1 : error_digits] : ms[end - error_digits+ 1 ]≠ ' 0' ? ms[end - error_digits+ 1 : end ] : join (string (ms[end ])," 0" )) ) × 10" )
163
+ FieldAlgebra. printexpo (io,Meta. parse (m[2 ]))
164
+ else
165
+ mz = match (r" 0\. 0*" ,sval)
166
+ zs = digits+ 1 + Base. hidigit (M. val, 10 )+ (M. val< 0 )- length (sval)+ (isnothing (mz) ? 0 : length (mz. match)- 1 )
167
+ if zs< 0 && sval[end ]== ' 0' && sval[end - 1 ]== ' .'
168
+ print (io, val, " (±" )
169
+ print_special (io,err)
170
+ print (io, ' )' )
171
+ else
172
+ z,ms = join (string .(zeros (Int,zs))),replace (string (err),' .' => " " )
173
+ print (io," $sval$z ($(ms[1 ]≠ ' 0' ? ms[1 : error_digits] : ms[end - error_digits+ 1 ]≠ ' 0' ? ms[end - error_digits+ 1 : end ] : join (string (ms[end ]," 0" ))) )" )
174
+ end
175
+ end
176
+ end
177
+
63
178
# unit systems
64
179
65
180
const usingSimilitude = true # UnitSystems.similitude()
82
197
for unit ∈ (:boltzmann ,:planckreduced ,:lightspeed ,:vacuumpermeability ,:electronmass ,:molarmass )
83
198
@eval @pure $ unit (U:: UnitSystem ,C:: Coupling ) = $ unit (U)
84
199
end
85
- for unit ∈ (Constants... ,:vacuumpermeability )
200
+ for unit ∈ (Constants... ,:permeability )
86
201
unit≠ :planck && @eval @pure $ unit (U:: UnitSystem ) = UnitSystems.$ unit (U)
87
202
unit≠ :angle && (@eval @pure $ unit (U:: UnitSystem ,S:: UnitSystem ) = unit ($ unit (S)/ $ unit (U)))
88
203
end
@@ -93,7 +208,7 @@ for unit ∈ Convert
93
208
@pure @inline $ unit (v:: Real ,U:: UnitSystem{kB,ħ,𝘤,μ₀,mₑ,Mᵤ,extra} ,S:: UnitSystem{kB,ħ,𝘤,μ₀,mₑ,Mᵤ,extra} ) where {kB,ħ,𝘤,μ₀,mₑ,Mᵤ,extra} = v
94
209
end
95
210
unit≠ :angle && (@eval @pure @inline $ unit (U:: UnitSystem{kB,ħ,𝘤,μ₀,mₑ,Mᵤ,extra} ,S:: UnitSystem{kB,ħ,𝘤,μ₀,mₑ,Mᵤ,extra} ) where {kB,ħ,𝘤,μ₀,mₑ,Mᵤ,extra} = 𝟏)
96
- if unit ∉ (Constants... ,:permittivity ,:angle )
211
+ if unit ∉ (Constants... ,:permittivity ,:permeability , : angle )
97
212
@eval @pure @inline $ unit (U:: UnitSystem ) = $ unit (Natural,U)
98
213
end
99
214
end
108
223
# fundamental constants, αinv = (34259-1/4366.8123)/250 # 137.036 exactly?
109
224
110
225
if usingSimilitude
111
- export Similitude, 𝟙, Unified
226
+ export Similitude, 𝟙, Unified, quotient
112
227
import Similitude
113
- import Similitude: Unified, coefprod, promoteint, USQ
228
+ import Similitude: Unified, coefprod, promoteint, USQ, quotient,dimlatex, morphism
114
229
import Similitude: Group,AbelianGroup,LogGroup,ExpGroup,Quantity,Dimension,Quantities,𝟙,usq
115
230
import Similitude: Values,value,vals,basis,valueat,showgroup,ratio,isq,dims,dimtext
116
231
import FieldAlgebra: makeint, product
@@ -119,6 +234,7 @@ for D ∈ (:F,:M,:L,:T,:Q,:Θ,:N,:J,:A,:R,:C)
119
234
end
120
235
FieldAlgebra. makeint (x:: MeasureSystems.Measurements.Measurement ) = x
121
236
FieldAlgebra. promoteint (x:: Measure ) = x
237
+ FieldAlgebra. latext (:: Group{:Measures} ) = Similitude. usqlatex
122
238
@group Measures begin
123
239
kB = UnitSystems. kB
124
240
NA = UnitSystems. NA
0 commit comments