@@ -96,28 +96,9 @@ function screen_unit(result)
9696    result
9797end 
9898
99- """ 
100- Test unit equivalence. 
101- """ 
102- equivalent (x, y) =  isequal (1  *  x, 1  *  y)
10399const  unitless =  Unitful. unit (1 )
104100
105- """ 
106- Find the unit of a symbolic item. 
107- """ 
108- get_unit (x:: Real ) =  unitless
109101get_unit (x:: Unitful.Quantity ) =  screen_unit (Unitful. unit (x))
110- get_unit (x:: AbstractArray ) =  map (get_unit, x)
111- get_unit (x:: MT.Num ) =  get_unit (value (x))
112- function  get_unit (x:: Union{Symbolics.ArrayOp, Symbolics.Arr, Symbolics.CallWithMetadata} )
113-     get_literal_unit (x)
114- end 
115- get_unit (op:: Differential , args) =  get_unit (args[1 ]) /  get_unit (op. x)
116- get_unit (op:: typeof (getindex), args) =  get_unit (args[1 ])
117- get_unit (x:: SciMLBase.NullParameters ) =  unitless
118- get_unit (op:: typeof (instream), args) =  get_unit (args[1 ])
119- 
120- get_literal_unit (x) =  screen_unit (getmetadata (x, VariableUnit, unitless))
121102
122103function  get_unit (op, args) #  Fallback
123104    result =  op (1  .*  get_unit .(args)... )
@@ -128,76 +109,4 @@ function get_unit(op, args) # Fallback
128109    end 
129110end 
130111
131- function  get_unit (op:: Integral , args)
132-     unit =  1 
133-     if  op. domain. variables isa  Vector
134-         for  u in  op. domain. variables
135-             unit *=  get_unit (u)
136-         end 
137-     else 
138-         unit *=  get_unit (op. domain. variables)
139-     end 
140-     return  get_unit (args[1 ]) *  unit
141- end 
142- 
143- function  get_unit (op:: Conditional , args)
144-     terms =  get_unit .(args)
145-     terms[1 ] ==  unitless || 
146-         throw (ValidationError (" , in $op , [$(terms[1 ]) ] is not dimensionless." 
147-     equivalent (terms[2 ], terms[3 ]) || 
148-         throw (ValidationError (" , in $op , units [$(terms[2 ]) ] and [$(terms[3 ]) ] do not match." 
149-     return  terms[2 ]
150- end 
151- 
152- function  get_unit (op:: typeof (Symbolics. _mapreduce), args)
153-     if  args[2 ] ==  + 
154-         get_unit (args[3 ])
155-     else 
156-         throw (ValidationError (" Unsupported array operation $op " 
157-     end 
158- end 
159- 
160- function  get_unit (op:: Comparison , args)
161-     terms =  get_unit .(args)
162-     equivalent (terms[1 ], terms[2 ]) || 
163-         throw (ValidationError (" , in comparison $op , units [$(terms[1 ]) ] and [$(terms[2 ]) ] do not match." 
164-     return  unitless
165- end 
166- 
167- function  get_unit (x:: Symbolic )
168-     if  issym (x)
169-         get_literal_unit (x)
170-     elseif  isadd (x)
171-         terms =  get_unit .(arguments (x))
172-         firstunit =  terms[1 ]
173-         for  other in  terms[2 : end ]
174-             termlist =  join (map (repr, terms), " , " 
175-             equivalent (other, firstunit) || 
176-                 throw (ValidationError (" , in sum $x , units [$termlist ] do not match." 
177-         end 
178-         return  firstunit
179-     elseif  ispow (x)
180-         pargs =  arguments (x)
181-         base, expon =  get_unit .(pargs)
182-         @assert  expon isa  Unitful. DimensionlessUnits
183-         if  base ==  unitless
184-             unitless
185-         else 
186-             pargs[2 ] isa  Number ?  base^ pargs[2 ] :  (1  *  base)^ pargs[2 ]
187-         end 
188-     elseif  iscall (x)
189-         op =  operation (x)
190-         if  issym (op) ||  (iscall (op) &&  iscall (operation (op))) #  Dependent variables, not function calls
191-             return  screen_unit (getmetadata (x, VariableUnit, unitless)) #  Like x(t) or x[i]
192-         elseif  iscall (op) &&  ! iscall (operation (op))
193-             gp =  getmetadata (x, Symbolics. GetindexParent, nothing ) #  Like x[1](t)
194-             return  screen_unit (getmetadata (gp, VariableUnit, unitless))
195-         end   #  Actual function calls:
196-         args =  arguments (x)
197-         return  get_unit (op, args)
198-     else  #  This function should only be reached by Terms, for which `iscall` is true
199-         throw (ArgumentError (" Unsupported value $x ." 
200-     end 
201- end 
202- 
203112end  #  module UnitfulUnitCheck
0 commit comments