/[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 413 by gross, Tue Jan 3 08:27:19 2006 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 43  import os Line 43  import os
43  # def matchType(arg0=0.,arg1=0.):  # def matchType(arg0=0.,arg1=0.):
44  # def matchShape(arg0,arg1):  # def matchShape(arg0,arg1):
45    
 # def maximum(arg0,arg1):  
 # def minimum(arg0,arg1):  
   
46  # def transpose(arg,axis=None):  # def transpose(arg,axis=None):
47  # def trace(arg,axis0=0,axis1=1):  # def trace(arg,axis0=0,axis1=1):
48  # def reorderComponents(arg,index):  # def reorderComponents(arg,index):
# Line 363  def testForZero(arg): Line 360  def testForZero(arg):
360      @return : True if the argument is identical to zero.      @return : True if the argument is identical to zero.
361      @rtype : C{bool}      @rtype : C{bool}
362      """      """
363      try:      if isinstance(arg,numarray.NumArray):
364           return not Lsup(arg)>0.
365        elif isinstance(arg,escript.Data):
366           return False
367        elif isinstance(arg,float):
368           return not Lsup(arg)>0.
369        elif isinstance(arg,int):
370         return not Lsup(arg)>0.         return not Lsup(arg)>0.
371      except TypeError:      elif isinstance(arg,Symbol):
372           return False
373        else:
374         return False         return False
375    
376  def matchType(arg0=0.,arg1=0.):  def matchType(arg0=0.,arg1=0.):
# Line 907  def wherePositive(arg): Line 912  def wherePositive(arg):
912     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
913     """     """
914     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
915        if arg.rank==0:        out=numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))*1.
916           if arg>0:        if isinstance(out,float): out=numarray.array(out)
917             return numarray.array(1.)        return out
          else:  
            return numarray.array(0.)  
       else:  
          return numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))  
918     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
919        return arg._wherePositive()        return arg._wherePositive()
920     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 993  def whereNegative(arg): Line 994  def whereNegative(arg):
994     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
995     """     """
996     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
997        if arg.rank==0:        out=numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))*1.
998           if arg<0:        if isinstance(out,float): out=numarray.array(out)
999             return numarray.array(1.)        return out
          else:  
            return numarray.array(0.)  
       else:  
          return numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))  
1000     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1001        return arg._whereNegative()        return arg._whereNegative()
1002     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1079  def whereNonNegative(arg): Line 1076  def whereNonNegative(arg):
1076     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1077     """     """
1078     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1079        if arg.rank==0:        out=numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.
1080           if arg<0:        if isinstance(out,float): out=numarray.array(out)
1081             return numarray.array(0.)        return out
          else:  
            return numarray.array(1.)  
       else:  
          return numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))  
1082     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1083        return arg._whereNonNegative()        return arg._whereNonNegative()
1084     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1113  def whereNonPositive(arg): Line 1106  def whereNonPositive(arg):
1106     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1107     """     """
1108     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1109        if arg.rank==0:        out=numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.
1110           if arg>0:        if isinstance(out,float): out=numarray.array(out)
1111             return numarray.array(0.)        return out
          else:  
            return numarray.array(1.)  
       else:  
          return numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.  
1112     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1113        return arg._whereNonPositive()        return arg._whereNonPositive()
1114     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1149  def whereZero(arg,tol=0.): Line 1138  def whereZero(arg,tol=0.):
1138     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1139     """     """
1140     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1141        if arg.rank==0:        out=numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1142           if abs(arg)<=tol:        if isinstance(out,float): out=numarray.array(out)
1143             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.  
1144     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1145        if tol>0.:        if tol>0.:
1146           return whereNegative(abs(arg)-tol)           return whereNegative(abs(arg)-tol)
# Line 1236  def whereNonZero(arg,tol=0.): Line 1221  def whereNonZero(arg,tol=0.):
1221     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1222     """     """
1223     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1224        if arg.rank==0:        out=numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1225          if abs(arg)>tol:        if isinstance(out,float): out=numarray.array(out)
1226             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.  
1227     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1228        if tol>0.:        if tol>0.:
1229           return 1.-whereZero(arg,tol)           return 1.-whereZero(arg,tol)
# Line 3304  def maximum(*args): Line 3285  def maximum(*args):
3285         if out==None:         if out==None:
3286            out=a            out=a
3287         else:         else:
3288            m=whereNegative(out-a)            diff=add(a,-out)
3289            out=m*a+(1.-m)*out            out=add(out,mult(wherePositive(diff),diff))
3290      return out      return out
3291        
3292  def minimum(*arg):  def minimum(*args):
3293      """      """
3294      the minimum over arguments args      the minimum over arguments args
3295    
# Line 3322  def minimum(*arg): Line 3303  def minimum(*arg):
3303         if out==None:         if out==None:
3304            out=a            out=a
3305         else:         else:
3306            m=whereNegative(out-a)            diff=add(a,-out)
3307            out=m*out+(1.-m)*a            out=add(out,mult(whereNegative(diff),diff))
3308      return out      return out
3309    
3310    def clip(arg,minval=0.,maxval=1.):
3311        """
3312        cuts the values of arg between minval and maxval
3313    
3314        @param arg: argument
3315        @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float}
3316        @param minval: lower range
3317        @type arg: C{float}
3318        @param maxval: upper range
3319        @type arg: C{float}
3320        @return: is on object with all its value between minval and maxval. value of the argument that greater then minval and
3321                 less then maxval are unchanged.
3322        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input
3323        @raise ValueError: if minval>maxval
3324        """
3325        if minval>maxval:
3326           raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)
3327        return minimum(maximum(minval,arg),maxval)
3328    
3329        
3330  def inner(arg0,arg1):  def inner(arg0,arg1):
3331      """      """
# Line 3567  def escript_generalTensorProduct(arg0,ar Line 3568  def escript_generalTensorProduct(arg0,ar
3568      if not shape01==shape10:      if not shape01==shape10:
3569          raise ValueError,"dimensions of last %s components in left argument don't match the first %s components in the right argument."%(offset,offset)          raise ValueError,"dimensions of last %s components in left argument don't match the first %s components in the right argument."%(offset,offset)
3570    
3571        # whatr function space should be used? (this here is not good!)
3572        fs=(escript.Scalar(0.,arg0.getFunctionSpace())+escript.Scalar(0.,arg1.getFunctionSpace())).getFunctionspace()
3573      # create return value:      # create return value:
3574      out=escript.Data(0.,tuple(shape0+shape1),arg0.getFunctionSpace())      out=escript.Data(0.,tuple(shape0+shape1),fs)
3575      #      #
3576      s0=[[]]      s0=[[]]
3577      for k in shape0:      for k in shape0:
# Line 3591  def escript_generalTensorProduct(arg0,ar Line 3594  def escript_generalTensorProduct(arg0,ar
3594    
3595      for i0 in s0:      for i0 in s0:
3596         for i1 in s1:         for i1 in s1:
3597           s=escript.Scalar(0.,arg0.getFunctionSpace())           s=escript.Scalar(0.,fs)
3598           for i01 in s01:           for i01 in s01:
3599              s+=arg0.__getitem__(tuple(i0+i01))*arg1.__getitem__(tuple(i01+i1))              s+=arg0.__getitem__(tuple(i0+i01))*arg1.__getitem__(tuple(i01+i1))
3600           out.__setitem__(tuple(i0+i1),s)           out.__setitem__(tuple(i0+i1),s)
# Line 3661  def interpolate(arg,where): Line 3664  def interpolate(arg,where):
3664      @param arg:    interpolant      @param arg:    interpolant
3665      @param where:  FunctionSpace to interpolate to      @param where:  FunctionSpace to interpolate to
3666      """      """
3667      if testForZero(arg):      if isinstance(arg,Symbol):
       return 0  
     elif isinstance(arg,Symbol):  
3668         return Interpolated_Symbol(arg,where)         return Interpolated_Symbol(arg,where)
3669      else:      else:
3670         return escript.Data(arg,where)         return escript.Data(arg,where)
# Line 3688  def jump(arg): Line 3689  def jump(arg):
3689                    to be calculated.                    to be calculated.
3690      """      """
3691      d=arg.getDomain()      d=arg.getDomain()
3692      return arg.interpolate(escript.FunctionOnContactOne())-arg.interpolate(escript.FunctionOnContactZero())      return arg.interpolate(escript.FunctionOnContactOne(d))-arg.interpolate(escript.FunctionOnContactZero(d))
3693    
3694  #=============================  #=============================
3695  #  #

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

  ViewVC Help
Powered by ViewVC 1.1.26