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

revision 329 by gross, Wed Dec 7 04:42:43 2005 UTC revision 396 by gross, Wed Dec 21 05:08:25 2005 UTC
# 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.         return not Lsup(arg)>0.
369      except TypeError:      elif isinstance(arg,escript.Data):
370           return False
371        elif isinstance(arg,float):
372           return not Lsup(arg)>0.
373        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        return numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))        out=numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))
920          if isinstance(out,float): out=numarray.array(out)
921          return out
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 987  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        return numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))        out=numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))
1002          if isinstance(out,float): out=numarray.array(out)
1003          return out
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 1067  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        return numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))        out=numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))
1084          if isinstance(out,float): out=numarray.array(out)
1085          return out
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 1095  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        return numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))        out=numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.
1114          if isinstance(out,float): out=numarray.array(out)
1115          return out
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 1125  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        return numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))        out=numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1146          if isinstance(out,float): out=numarray.array(out)
1147          return out
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 1206  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        return numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))        out=numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1229          if isinstance(out,float): out=numarray.array(out)
1230          return out
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 2687  def minval(arg): Line 2708  def minval(arg):
2708     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2709     """     """
2710     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
2711        return arg.min()        if arg.rank==0:
2712             return float(arg)
2713          else:
2714             return arg.min()
2715     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
2716        return arg._minval()        return arg._minval()
2717     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 2761  def maxval(arg): Line 2785  def maxval(arg):
2785     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2786     """     """
2787     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
2788        return arg.max()        if arg.rank==0:
2789             return float(arg)
2790          else:
2791             return arg.max()
2792     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
2793        return arg._maxval()        return arg._maxval()
2794     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 3262  def maximum(*args): Line 3289  def maximum(*args):
3289         if out==None:         if out==None:
3290            out=a            out=a
3291         else:         else:
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 3280  def minimum(*arg): Line 3307  def minimum(*arg):
3307         if out==None:         if out==None:
3308            out=a            out=a
3309         else:         else:
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 3619  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 3646  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.329 changed lines Added in v.396