/[escript]/trunk/escript/py_src/unitsSI.py
ViewVC logotype

Diff of /trunk/escript/py_src/unitsSI.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2398 by gross, Tue Apr 28 04:41:53 2009 UTC revision 2399 by gross, Tue Apr 28 12:04:34 2009 UTC
# Line 84  some tools supporting the usage of symbo Line 84  some tools supporting the usage of symbo
84  @var R_Earth: Earth's radius  @var R_Earth: Earth's radius
85  @var v_light: speed of light  @var v_light: speed of light
86  """  """
87    class Unit(object):
88       """
89       a general class to define a physical unit and a linear converter a+b*x to get transfert to a referene unit system (typically SI)
90       """
91       def __init__(self, name, longname,a ,b ):
92           """
93           initializes the unit
94          
95           @param name: short name of the unit or prefix
96           @type name: C{str}
97           @param longname: long name of the unit or prefix
98           @type longname: C{str}
99           @param a: absolute value in transformation
100           @type a: C{float}
101           @param b: slop in translation
102           @type b: C{float}
103           """
104           self.setName(name)
105           self.setLongName(longname)
106           self.__a=a
107           self.__b=b
108    
109       def __str__(self):
110           return self.getName()
111    
112       def getName(self):
113           """
114           Returns the name of the unit
115    
116           @return: name of the unit
117           @rtype: C{str}
118           """
119           return self.__name
120    
121       def setName(self, name):
122           """
123           Sets the name of the unit
124    
125           @param name: new name of the unit
126           @type name: C{str}
127           """
128           self.__name=name
129    
130       def getLongName(self):
131           """
132           Returns the long name of the unit
133    
134           @return: name of the unit
135           @rtype: C{str}
136           """
137           return self.__longname
138    
139       def setLongName(self, name):
140           """
141           Sets the long name of the unit
142    
143           @param name: new long name of the unit
144           @type name: C{str}
145           """
146           self.__longname=name
147    
148       def __call__(self,x):
149           """
150           Converts a value x in the unit self to SI
151    
152           @param x: value to convert
153           @type x: an arithmetic object
154           """
155           return self.__b*x+self.__a
156    
157       def __mul__(self,other):
158           """
159           Performs self*other operation for two L{Unit} objects
160    
161           @param other: an other unit
162           @type other: L{Unit}
163           @rtype: L{Unit} or C{NotImplemented}
164           """
165           if isinstance(other, Unit):
166              a=self(other(0.))
167              b=(self(other(1.))-a)
168              if isinstance(other, PowUnit) or isinstance(self,DivUnit) or  isinstance(self, PowUnit):
169                return ProdUnit(self.getName()+" "+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
170              else:
171                return ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
172           else:
173              return NotImplemented
174    
175       def __rmul__(self,other):
176           """
177           Performs other*self operation
178    
179           @param other: an other L{Unit} or an arithmetic object
180           @type other: L{Unit}
181           @rtype: L{Unit} or an arithmetic object
182           """
183           if isinstance(other, Unit):
184              a=other(self(0.))
185              b=(other(self(1.))-a)
186              if isinstance(other, PowUnit) or  isinstance(self, PowUnit) or isinstance(other, DivUnit):
187                 return ProdUnit(other.getName()+" "+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
188              else:
189                 return ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
190           else:
191              return self(other)
192    
193       def __div__(self,other):
194           """
195           Performs self*other operation for two L{Unit} objects
196    
197           @param other: an other unit
198           @type other: L{Unit}
199           @rtype: L{Unit} or C{NotImplemented}
200           """
201           if isinstance(other, Unit):
202              if abs(self(0.))+abs(other(0.))>0:
203                  raise ValueError,"Division of units requires 0 absolute values"
204              if  isinstance(other, (ProdUnit, DivUnit)):
205                  # X/(c*d) or X/(c/d) requires brackets:
206                  return DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))
207              else:
208                  return DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))
209           else:
210              return NotImplemented
211       def __rdiv__(self,other):
212           """
213           Performs other/self operation
214    
215           @param other: an other L{Unit} or an arithmetic object
216           @type other: L{Unit} or an arithmetic object
217           @rtype: L{Unit} or an arithmetic object
218           """
219           if isinstance(other, Unit):
220              if abs(self(0.))+abs(other(0.))>0:
221                  raise ValueError,"Division of units requires 0 absolute values"
222              if  isinstance(self, (ProdUnit, DivUnit)):
223                  # X/(a*b) or X/(a/b) requires brackets:
224                  return DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))
225              else:
226                  return DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))
227           else:
228              return (other-self(0.))/(self(1.)-self(0.))
229       def __pow__(self,other):
230           """
231           Performs self**other operation
232    
233           @param other: an exponent
234           @type other: C{int} or C{float}
235           @rtype: L{Unit}
236           """
237           if isinstance(other, float) or isinstance(other, int):
238              if abs(self(0.))>0:
239                  raise ValueError,"Power of unit requires 0 absolute values"
240              if  isinstance(self, (ProdUnit, DivUnit, PowUnit)):
241                  return PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)
242              else:
243                  return PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)
244           else:
245              return NotImplemented
246    
247    class ProdUnit(Unit):
248        pass
249    class DivUnit(Unit):
250        pass
251    class PowUnit(Unit):
252        pass
253  #  #
254  #  prefixes:  #  prefixes:
255  #  #

Legend:
Removed from v.2398  
changed lines
  Added in v.2399

  ViewVC Help
Powered by ViewVC 1.1.26