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

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

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

revision 341 by gross, Mon Dec 12 05:26:10 2005 UTC revision 396 by gross, Wed Dec 21 05:08:25 2005 UTC
# Line 24  Utility functions for escript Line 24  Utility functions for escript
24  __author__="Lutz Gross, l.gross@uq.edu.au"  __author__="Lutz Gross, l.gross@uq.edu.au"
25  __licence__="contact: esys@access.uq.edu.au"  __licence__="contact: esys@access.uq.edu.au"
26  __url__="http://www.iservo.edu.au/esys/escript"  __url__="http://www.iservo.edu.au/esys/escript"
27  __version__="$Revision: 329 $"  __version__="$Revision$"
28  __date__="$Date$"  __date__="$Date$"
29    
30    
# Line 45  import os Line 45  import os
45    
46  # def maximum(arg0,arg1):  # def maximum(arg0,arg1):
47  # def minimum(arg0,arg1):  # def minimum(arg0,arg1):
48    # def clip(arg,minval,maxval)
49    
50  # def transpose(arg,axis=None):  # def transpose(arg,axis=None):
51  # def trace(arg,axis0=0,axis1=1):  # def trace(arg,axis0=0,axis1=1):
# Line 363  def testForZero(arg): Line 364  def testForZero(arg):
364      @return : True if the argument is identical to zero.      @return : True if the argument is identical to zero.
365      @rtype : C{bool}      @rtype : C{bool}
366      """      """
367      try:      if isinstance(arg,numarray.NumArray):
368           return not Lsup(arg)>0.
369        elif isinstance(arg,escript.Data):
370           return False
371        elif isinstance(arg,float):
372         return not Lsup(arg)>0.         return not Lsup(arg)>0.
373      except TypeError:      elif isinstance(arg,int):
374           return not Lsup(arg)>0.
375        elif isinstance(arg,Symbol):
376           return False
377        else:
378         return False         return False
379    
380  def matchType(arg0=0.,arg1=0.):  def matchType(arg0=0.,arg1=0.):
# Line 907  def wherePositive(arg): Line 916  def wherePositive(arg):
916     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
917     """     """
918     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
919        if arg.rank==0:        out=numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))
920           if arg>0:        if isinstance(out,float): out=numarray.array(out)
921             return numarray.array(1.)        return out
          else:  
            return numarray.array(0.)  
       else:  
          return numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))  
922     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
923        return arg._wherePositive()        return arg._wherePositive()
924     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 993  def whereNegative(arg): Line 998  def whereNegative(arg):
998     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
999     """     """
1000     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1001        if arg.rank==0:        out=numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))
1002           if arg<0:        if isinstance(out,float): out=numarray.array(out)
1003             return numarray.array(1.)        return out
          else:  
            return numarray.array(0.)  
       else:  
          return numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))  
1004     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1005        return arg._whereNegative()        return arg._whereNegative()
1006     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1079  def whereNonNegative(arg): Line 1080  def whereNonNegative(arg):
1080     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1081     """     """
1082     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1083        if arg.rank==0:        out=numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))
1084           if arg<0:        if isinstance(out,float): out=numarray.array(out)
1085             return numarray.array(0.)        return out
          else:  
            return numarray.array(1.)  
       else:  
          return numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))  
1086     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1087        return arg._whereNonNegative()        return arg._whereNonNegative()
1088     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1113  def whereNonPositive(arg): Line 1110  def whereNonPositive(arg):
1110     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1111     """     """
1112     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1113        if arg.rank==0:        out=numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.
1114           if arg>0:        if isinstance(out,float): out=numarray.array(out)
1115             return numarray.array(0.)        return out
          else:  
            return numarray.array(1.)  
       else:  
          return numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.  
1116     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1117        return arg._whereNonPositive()        return arg._whereNonPositive()
1118     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1149  def whereZero(arg,tol=0.): Line 1142  def whereZero(arg,tol=0.):
1142     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1143     """     """
1144     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1145        if arg.rank==0:        out=numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1146           if abs(arg)<=tol:        if isinstance(out,float): out=numarray.array(out)
1147             return numarray.array(1.)        return out
          else:  
            return numarray.array(0.)  
       else:  
          return numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.  
1148     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1149        if tol>0.:        if tol>0.:
1150           return whereNegative(abs(arg)-tol)           return whereNegative(abs(arg)-tol)
# Line 1236  def whereNonZero(arg,tol=0.): Line 1225  def whereNonZero(arg,tol=0.):
1225     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1226     """     """
1227     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1228        if arg.rank==0:        out=numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1229          if abs(arg)>tol:        if isinstance(out,float): out=numarray.array(out)
1230             return numarray.array(1.)        return out
         else:  
            return numarray.array(0.)  
       else:  
          return numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.  
1231     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1232        if tol>0.:        if tol>0.:
1233           return 1.-whereZero(arg,tol)           return 1.-whereZero(arg,tol)
# Line 3304  def maximum(*args): Line 3289  def maximum(*args):
3289         if out==None:         if out==None:
3290            out=a            out=a
3291         else:         else:
3292            m=whereNegative(out-a)            diff=add(a,-out)
3293            out=m*a+(1.-m)*out            out=add(out,mult(wherePositive(diff),diff))
3294      return out      return out
3295        
3296  def minimum(*arg):  def minimum(*args):
3297      """      """
3298      the minimum over arguments args      the minimum over arguments args
3299    
# Line 3322  def minimum(*arg): Line 3307  def minimum(*arg):
3307         if out==None:         if out==None:
3308            out=a            out=a
3309         else:         else:
3310            m=whereNegative(out-a)            diff=add(a,-out)
3311            out=m*out+(1.-m)*a            out=add(out,mult(whereNegative(diff),diff))
3312      return out      return out
3313    
3314    def clip(arg,minval=0.,maxval=1.):
3315        """
3316        cuts the values of arg between minval and maxval
3317    
3318        @param arg: argument
3319        @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float}
3320        @param minval: lower range
3321        @type arg: C{float}
3322        @param maxval: uper range
3323        @type arg: C{float}
3324        @return: is on object with all its value between minval and maxval. value of the argument that greater then minval and
3325                 less then maxval are unchanged.
3326        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input
3327        """
3328        if minval>maxval:
3329           raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)
3330        return minimum(maximum(minval,arg),maxval)
3331    
3332        
3333  def inner(arg0,arg1):  def inner(arg0,arg1):
3334      """      """
# Line 3661  def interpolate(arg,where): Line 3665  def interpolate(arg,where):
3665      @param arg:    interpolant      @param arg:    interpolant
3666      @param where:  FunctionSpace to interpolate to      @param where:  FunctionSpace to interpolate to
3667      """      """
3668      if testForZero(arg):      if isinstance(arg,Symbol):
       return 0  
     elif isinstance(arg,Symbol):  
3669         return Interpolated_Symbol(arg,where)         return Interpolated_Symbol(arg,where)
3670      else:      else:
3671         return escript.Data(arg,where)         return escript.Data(arg,where)
# Line 3688  def jump(arg): Line 3690  def jump(arg):
3690                    to be calculated.                    to be calculated.
3691      """      """
3692      d=arg.getDomain()      d=arg.getDomain()
3693      return arg.interpolate(escript.FunctionOnContactOne())-arg.interpolate(escript.FunctionOnContactZero())      return arg.interpolate(escript.FunctionOnContactOne(d))-arg.interpolate(escript.FunctionOnContactZero(d))
3694    
3695  #=============================  #=============================
3696  #  #

Legend:
Removed from v.341  
changed lines
  Added in v.396

  ViewVC Help
Powered by ViewVC 1.1.26