/[escript]/trunk-mpi-branch/escript/py_src/util.py
ViewVC logotype

Diff of /trunk-mpi-branch/escript/py_src/util.py

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

revision 443 by gross, Fri Jan 20 06:22:38 2006 UTC revision 492 by gross, Fri Feb 3 02:07:24 2006 UTC
# Line 44  import os Line 44  import os
44  # def matchType(arg0=0.,arg1=0.):  # def matchType(arg0=0.,arg1=0.):
45  # def matchShape(arg0,arg1):  # def matchShape(arg0,arg1):
46    
 # def transpose(arg,axis=None):  
47  # def reorderComponents(arg,index):  # def reorderComponents(arg,index):
48    
49  #  #
# Line 3012  class Trace_Symbol(DependendSymbol): Line 3011  class Trace_Symbol(DependendSymbol):
3011        else:        else:
3012           return trace(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1])           return trace(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1])
3013    
3014    def transpose(arg,axis_offset=None):
3015       """
3016       returns the transpose of arg by swaping the first axis_offset and the last rank-axis_offset components.
3017    
3018       @param arg: argument
3019       @type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}, C{float}, C{int}
3020       @param axis_offset: the first axis_offset components are swapped with rest. If C{axis_offset} must be non-negative and less or equal the rank of arg.
3021                           if axis_offset is not present C{int(r/2)} where r is the rank of arg is used.
3022       @type axis_offset: C{int}
3023       @return: transpose of arg
3024       @rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray},C{float}, C{int} depending on the type of arg.
3025       """
3026       if isinstance(arg,numarray.NumArray):
3027          if axis_offset==None: axis_offset=int(arg.rank/2)
3028          return numarray.transpose(arg,axes=range(axis_offset,arg.rank)+range(0,axis_offset))
3029       elif isinstance(arg,escript.Data):
3030          if axis_offset==None: axis_offset=int(arg.getRank()/2)
3031          return escript_transpose(arg,axis_offset)
3032       elif isinstance(arg,float):
3033          if not ( axis_offset==0 or axis_offset==None):
3034            raise ValueError,"transpose: axis_offset must be 0 for float argument"
3035          return arg
3036       elif isinstance(arg,int):
3037          if not ( axis_offset==0 or axis_offset==None):
3038            raise ValueError,"transpose: axis_offset must be 0 for int argument"
3039          return float(arg)
3040       elif isinstance(arg,Symbol):
3041          if axis_offset==None: axis_offset=int(arg.getRank()/2)
3042          return Transpose_Symbol(arg,axis_offset)
3043       else:
3044          raise TypeError,"transpose: Unknown argument type."
3045    
3046    def escript_transpose(arg,axis_offset): # this should be escript._transpose
3047          "arg si a Data objects!!!"
3048          r=arg.getRank()
3049          if axis_offset<0 or axis_offset>r:
3050            raise ValueError,"escript_transpose: axis_offset must be between 0 and %s"%r
3051          s=arg.getShape()
3052          s_out=s[axis_offset:]+s[:axis_offset]
3053          out=escript.Data(0.,s_out,arg.getFunctionSpace())
3054          if r==4:
3055             if axis_offset==1:
3056                for i0 in range(s_out[0]):
3057                   for i1 in range(s_out[1]):
3058                      for i2 in range(s_out[2]):
3059                         for i3 in range(s_out[3]):
3060                             out[i0,i1,i2,i3]=arg[i3,i0,i1,i2]
3061             elif axis_offset==2:
3062                for i0 in range(s_out[0]):
3063                   for i1 in range(s_out[1]):
3064                      for i2 in range(s_out[2]):
3065                         for i3 in range(s_out[3]):
3066                             out[i0,i1,i2,i3]=arg[i2,i3,i0,i1]
3067             elif axis_offset==3:
3068                for i0 in range(s_out[0]):
3069                   for i1 in range(s_out[1]):
3070                      for i2 in range(s_out[2]):
3071                         for i3 in range(s_out[3]):
3072                             out[i0,i1,i2,i3]=arg[i1,i2,i3,i0]
3073             else:
3074                for i0 in range(s_out[0]):
3075                   for i1 in range(s_out[1]):
3076                      for i2 in range(s_out[2]):
3077                         for i3 in range(s_out[3]):
3078                             out[i0,i1,i2,i3]=arg[i0,i1,i2,i3]
3079          elif r==3:
3080             if axis_offset==1:
3081                for i0 in range(s_out[0]):
3082                   for i1 in range(s_out[1]):
3083                      for i2 in range(s_out[2]):
3084                             out[i0,i1,i2]=arg[i2,i0,i1]
3085             elif axis_offset==2:
3086                for i0 in range(s_out[0]):
3087                   for i1 in range(s_out[1]):
3088                      for i2 in range(s_out[2]):
3089                             out[i0,i1,i2]=arg[i1,i2,i0]
3090             else:
3091                for i0 in range(s_out[0]):
3092                   for i1 in range(s_out[1]):
3093                      for i2 in range(s_out[2]):
3094                             out[i0,i1,i2]=arg[i0,i1,i2]
3095          elif r==2:
3096             if axis_offset==1:
3097                for i0 in range(s_out[0]):
3098                   for i1 in range(s_out[1]):
3099                             out[i0,i1]=arg[i1,i0]
3100             else:
3101                for i0 in range(s_out[0]):
3102                   for i1 in range(s_out[1]):
3103                             out[i0,i1]=arg[i0,i1]
3104          elif r==1:
3105              for i0 in range(s_out[0]):
3106                   out[i0]=arg[i0]
3107          elif r==0:
3108                 out=arg+0.
3109          return out
3110    class Transpose_Symbol(DependendSymbol):
3111       """
3112       L{Symbol} representing the result of the transpose function
3113       """
3114       def __init__(self,arg,axis_offset=None):
3115          """
3116          initialization of transpose L{Symbol} with argument arg
3117    
3118          @param arg: argument of function
3119          @type arg: L{Symbol}.
3120           @param axis_offset: the first axis_offset components are swapped with rest. If C{axis_offset} must be non-negative and less or equal the rank of arg.
3121                           if axis_offset is not present C{int(r/2)} where r is the rank of arg is used.
3122          @type axis_offset: C{int}
3123          """
3124          if axis_offset==None: axis_offset=int(arg.getRank()/2)
3125          if axis_offset<0 or axis_offset>arg.getRank():
3126            raise ValueError,"escript_transpose: axis_offset must be between 0 and %s"%r
3127          s=arg.getShape()
3128          super(Transpose_Symbol,self).__init__(args=[arg,axis_offset],shape=s[axis_offset:]+s[:axis_offset],dim=arg.getDim())
3129    
3130       def getMyCode(self,argstrs,format="escript"):
3131          """
3132          returns a program code that can be used to evaluate the symbol.
3133    
3134          @param argstrs: gives for each argument a string representing the argument for the evaluation.
3135          @type argstrs: C{str} or a C{list} of length 1 of C{str}.
3136          @param format: specifies the format to be used. At the moment only "escript" ,"text" and "str" are supported.
3137          @type format: C{str}
3138          @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3139          @rtype: C{str}
3140          @raise: NotImplementedError: if the requested format is not available
3141          """
3142          if format=="escript" or format=="str"  or format=="text":
3143             return "transpose(%s,axis_offset=%s)"%(argstrs[0],argstrs[1])
3144          else:
3145             raise NotImplementedError,"Transpose_Symbol does not provide program code for format %s."%format
3146    
3147       def substitute(self,argvals):
3148          """
3149          assigns new values to symbols in the definition of the symbol.
3150          The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.
3151    
3152          @param argvals: new values assigned to symbols
3153          @type argvals: C{dict} with keywords of type L{Symbol}.
3154          @return: result of the substitution process. Operations are executed as much as possible.
3155          @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution
3156          @raise TypeError: if a value for a L{Symbol} cannot be substituted.
3157          """
3158          if argvals.has_key(self):
3159             arg=argvals[self]
3160             if self.isAppropriateValue(arg):
3161                return arg
3162             else:
3163                raise TypeError,"%s: new value is not appropriate."%str(self)
3164          else:
3165             arg=self.getSubstitutedArguments(argvals)
3166             return transpose(arg[0],axis_offset=arg[1])
3167    
3168       def diff(self,arg):
3169          """
3170          differential of this object
3171    
3172          @param arg: the derivative is calculated with respect to arg
3173          @type arg: L{escript.Symbol}
3174          @return: derivative with respect to C{arg}
3175          @rtype: typically L{Symbol} but other types such as C{float}, L{escript.Data}, L{numarray.NumArray}  are possible.
3176          """
3177          if arg==self:
3178             return identity(self.getShape())
3179          else:
3180             return transpose(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1])
3181    
3182  def inverse(arg):  def inverse(arg):
3183      """      """
3184      returns the inverse of the square matrix arg.      returns the inverse of the square matrix arg.
# Line 4224  def L2(arg): Line 4391  def L2(arg):
4391      return sqrt(integrate(inner(arg,arg)))      return sqrt(integrate(inner(arg,arg)))
4392  #=============================  #=============================
4393  #  #
 # wrapper for various functions: if the argument has attribute the function name  
 # as an argument it calls the corresponding methods. Otherwise the corresponding  
 # numarray function is called.  
   
 # functions involving the underlying Domain:  
   
 def transpose(arg,axis=None):  
     """  
     Returns the transpose of the Data object arg.  
   
     @param arg:  
     """  
     if axis==None:  
        r=0  
        if hasattr(arg,"getRank"): r=arg.getRank()  
        if hasattr(arg,"rank"): r=arg.rank  
        axis=r/2  
     if isinstance(arg,Symbol):  
        return Transpose_Symbol(arg,axis=r)  
     if isinstance(arg,escript.Data):  
        # hack for transpose  
        r=arg.getRank()  
        if r!=2: raise ValueError,"Tranpose only avalaible for rank 2 objects"  
        s=arg.getShape()  
        out=escript.Data(0.,(s[1],s[0]),arg.getFunctionSpace())  
        for i in range(s[0]):  
           for j in range(s[1]):  
              out[j,i]=arg[i,j]  
        return out  
        # end hack for transpose  
        return arg.transpose(axis)  
     else:  
        return numarray.transpose(arg,axis=axis)  
   
   
4394    
4395  def reorderComponents(arg,index):  def reorderComponents(arg,index):
4396      """      """
4397      resorts the component of arg according to index      resorts the component of arg according to index
4398    
4399      """      """
4400      pass      raise NotImplementedError
4401  #  #
4402  # $Log: util.py,v $  # $Log: util.py,v $
4403  # Revision 1.14.2.16  2005/10/19 06:09:57  gross  # Revision 1.14.2.16  2005/10/19 06:09:57  gross

Legend:
Removed from v.443  
changed lines
  Added in v.492

  ViewVC Help
Powered by ViewVC 1.1.26