/[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 312 by gross, Mon Dec 5 07:01:00 2005 UTC revision 378 by gross, Fri Dec 16 09:02:28 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 907  def wherePositive(arg): Line 908  def wherePositive(arg):
908     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
909     """     """
910     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
911        return numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))        if arg.rank==0:
912             if arg>0:
913               return numarray.array(1.)
914             else:
915               return numarray.array(0.)
916          else:
917             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 987  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        return numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))        if arg.rank==0:
998             if arg<0:
999               return numarray.array(1.)
1000             else:
1001               return numarray.array(0.)
1002          else:
1003             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 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))        if arg.rank==0:
1084             if arg<0:
1085               return numarray.array(0.)
1086             else:
1087               return numarray.array(1.)
1088          else:
1089             return numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))
1090     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1091        return arg._whereNonNegative()        return arg._whereNonNegative()
1092     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1095  def whereNonPositive(arg): Line 1114  def whereNonPositive(arg):
1114     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1115     """     """
1116     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1117        return numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))        if arg.rank==0:
1118             if arg>0:
1119               return numarray.array(0.)
1120             else:
1121               return numarray.array(1.)
1122          else:
1123             return numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))*1.
1124     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1125        return arg._whereNonPositive()        return arg._whereNonPositive()
1126     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 1125  def whereZero(arg,tol=0.): Line 1150  def whereZero(arg,tol=0.):
1150     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1151     """     """
1152     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1153        return numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))        if arg.rank==0:
1154             if abs(arg)<=tol:
1155               return numarray.array(1.)
1156             else:
1157               return numarray.array(0.)
1158          else:
1159             return numarray.less_equal(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1160     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1161        if tol>0.:        if tol>0.:
1162           return whereNegative(abs(arg)-tol)           return whereNegative(abs(arg)-tol)
# Line 1206  def whereNonZero(arg,tol=0.): Line 1237  def whereNonZero(arg,tol=0.):
1237     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1238     """     """
1239     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
1240        return numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))        if arg.rank==0:
1241            if abs(arg)>tol:
1242               return numarray.array(1.)
1243            else:
1244               return numarray.array(0.)
1245          else:
1246             return numarray.greater(abs(arg)-tol,numarray.zeros(arg.shape,numarray.Float))*1.
1247     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1248        if tol>0.:        if tol>0.:
1249           return 1.-whereZero(arg,tol)           return 1.-whereZero(arg,tol)
# Line 2587  def sign(arg): Line 2624  def sign(arg):
2624     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2625     """     """
2626     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
2627        return numarray.sign(arg)        return wherePositive(arg)-whereNegative(arg)
2628     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
2629        return arg._sign()        return arg._sign()
2630     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 2687  def minval(arg): Line 2724  def minval(arg):
2724     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2725     """     """
2726     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
2727        return arg.min()        if arg.rank==0:
2728             return float(arg)
2729          else:
2730             return arg.min()
2731     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
2732        return arg._minval()        return arg._minval()
2733     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 2761  def maxval(arg): Line 2801  def maxval(arg):
2801     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2802     """     """
2803     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
2804        return arg.max()        if arg.rank==0:
2805             return float(arg)
2806          else:
2807             return arg.max()
2808     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
2809        return arg._maxval()        return arg._maxval()
2810     elif isinstance(arg,float):     elif isinstance(arg,float):
# Line 3283  def minimum(*arg): Line 3326  def minimum(*arg):
3326            m=whereNegative(out-a)            m=whereNegative(out-a)
3327            out=m*out+(1.-m)*a            out=m*out+(1.-m)*a
3328      return out      return out
3329    
3330    def clip(arg,minval=0.,maxval=1.):
3331        """
3332        cuts the values of arg between minval and maxval
3333    
3334        @param arg: argument
3335        @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float}
3336        @param minval: lower range
3337        @type arg: C{float}
3338        @param maxval: uper range
3339        @type arg: C{float}
3340        @return: is on object with all its value between minval and maxval. value of the argument that greater then minval and
3341                 less then maxval are unchanged.
3342        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input
3343        """
3344        if minval>maxval:
3345           raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)
3346        return minimum(maximum(minval,arg),maxval)
3347    
3348        
3349  def inner(arg0,arg1):  def inner(arg0,arg1):
3350      """      """
# Line 3619  def interpolate(arg,where): Line 3681  def interpolate(arg,where):
3681      @param arg:    interpolant      @param arg:    interpolant
3682      @param where:  FunctionSpace to interpolate to      @param where:  FunctionSpace to interpolate to
3683      """      """
3684      if testForZero(arg):      if isinstance(arg,Symbol):
       return 0  
     elif isinstance(arg,Symbol):  
3685         return Interpolated_Symbol(arg,where)         return Interpolated_Symbol(arg,where)
3686      else:      else:
3687         return escript.Data(arg,where)         return escript.Data(arg,where)
# Line 3646  def jump(arg): Line 3706  def jump(arg):
3706                    to be calculated.                    to be calculated.
3707      """      """
3708      d=arg.getDomain()      d=arg.getDomain()
3709      return arg.interpolate(escript.FunctionOnContactOne())-arg.interpolate(escript.FunctionOnContactZero())      return arg.interpolate(escript.FunctionOnContactOne(d))-arg.interpolate(escript.FunctionOnContactZero(d))
3710    
3711  #=============================  #=============================
3712  #  #

Legend:
Removed from v.312  
changed lines
  Added in v.378

  ViewVC Help
Powered by ViewVC 1.1.26